00001
00002
00004
00005 #ifndef TEMPLATEMESH_H
00006 #define TEMPLATEMESH_H
00007
00008 #include "Mesh_X3DNode.h"
00009 #include "TemplateVertex.h"
00010 #include "TemplateDirectEdge.h"
00011 #include "TemplateIndirectEdge.h"
00012 #include "TemplateFace.h"
00013
00014 #include <iostream>
00015
00016 namespace X3DTK {
00017 namespace MESH {
00018
00022
00023 template<class MData, class VData, class EData, class FData>
00024 class TemplateMesh : public X3DNode
00025 {
00026 public:
00028 typedef TemplateVertex<VData, EData, FData> Vertex;
00030 typedef MTemplateVertex<VData, EData, FData> MVertex;
00032 typedef TemplateEdge<EData, FData, VData> Edge;
00034 typedef MTemplateEdge<EData, FData, VData> MEdge;
00036 typedef TemplateFace<FData, VData, EData> Face;
00038 typedef MTemplateFace<FData, VData, EData> MFace;
00039
00041 TemplateMesh()
00042 : X3DNode()
00043 {
00044 };
00046 virtual SFNode clone() const
00047 {
00048 return new TemplateMesh(*this);
00049 };
00051 virtual ~TemplateMesh()
00052 {
00053
00054 for (typename MVertex::iterator it = _vertices.begin(); it != _vertices.end(); ++it)
00055 delete (*it);
00056
00057
00058 for (typename MEdge::iterator it = _edges.begin(); it != _edges.end(); ++it)
00059 {
00060 delete (*it)->_symetric;
00061 delete (*it)->_edgeContent;
00062 delete (*it);
00063 }
00064
00065
00066 for (typename MFace::iterator it = _faces.begin(); it != _faces.end(); ++it)
00067 delete (*it);
00068 };
00069
00071 inline const MVertex &getVertices() const {return _vertices;};
00073 Edge *getEdge(Vertex *from, Vertex *to)
00074 {
00075 if ((from == 0) || (to == 0))
00076 return 0;
00077
00078 MEdge from_edges = from->getEdges();
00079
00080 for (typename MEdge::const_iterator it = from_edges.begin(); it != static_cast<MEdge::const_iterator>(from_edges.end()); ++it)
00081 {
00082 if ((*it)->getToVertex() == to)
00083 return (*it);
00084 }
00085
00086 return 0;
00087 };
00088
00090 inline const MEdge &getEdges() const {return _edges;};
00092 inline const MFace &getFaces() const {return _faces;};
00094 inline MData &data() {return _data;};
00096 inline const MData &data() const {return _data;};
00097
00099 Vertex *createVertex()
00100 {
00101 Vertex *V = new Vertex(_vertices.size());
00102 _vertices.back() = V;
00103 _vertices.push_back((Vertex *)(0x1));
00104
00105 return V;
00106 };
00108 Vertex *createVertex(unsigned int i)
00109 {
00110 if (i > _vertices.size())
00111 {
00112 _vertices.back() = 0;
00113 _vertices.resize(i + 2, 0);
00114 _vertices[i + 1] = (Vertex *)(0x1);
00115 }
00116 else
00117 {
00118 if (_vertices[i] != 0)
00119 {
00120 std::cerr << "TemplateMesh::createVertex: cannot create a Vertex at index " << i << ", a Vertex already exists!" << std::endl;
00121 return 0;
00122 }
00123 }
00124
00125 Vertex *V = new Vertex(i);
00126 _vertices[i] = V;
00127
00128 return V;
00129 };
00131 Edge *createEdge(Vertex *from, Vertex *to)
00132 {
00133 if ((from == 0) || (to == 0))
00134 return 0;
00135
00136 TemplateEdgeContent<EData, FData, VData> *content = new TemplateEdgeContent<EData, FData, VData>(from, to);
00137 Edge *E1 = new TemplateDirectEdge<EData, FData, VData>(content);
00138 Edge *E2 = new TemplateIndirectEdge<EData, FData, VData>(content);
00139
00140 E1->setSymetric(E2);
00141 E2->setSymetric(E1);
00142
00143 _edges.back() = E1;
00144 _edges.push_back((Edge *)(0x1));
00145
00146
00147
00148 from->addEdge(E1);
00149 to->addEdge(E2);
00150
00151 return E1;
00152 };
00153
00155 Face *createFace(MEdge &edges)
00156 {
00157 Face *F = new Face(_faces.size(), edges);
00158 _faces.back() = F;
00159 _faces.push_back((Face *)(0x1));
00160
00161
00162 for (typename MEdge::const_iterator it = edges.begin(); it != static_cast<MEdge::const_iterator>(edges.end()); ++it)
00163 {
00164 (*it)->addLeftFace(F);
00165 (*it)->getFromVertex()->addFace(F);
00166 }
00167
00168 return F;
00169 };
00171 Face *createFace(unsigned int i, MEdge &edges)
00172 {
00173 if (i > _faces.size())
00174 {
00175 _faces.back() = 0;
00176 _faces.resize(i + 2, 0);
00177 _faces[i + 1] = (Face *)(0x1);
00178 }
00179 else
00180 {
00181 if (_faces[i] != 0)
00182 {
00183 std::cerr << "TemplateMesh::createFace: cannot create a Face at index " << i << ", a Face already exists!" << std::endl;
00184 return;
00185 }
00186 }
00187
00188 Face *F = new Face(i, edges);
00189 _faces[i] = F;
00190
00191
00192 for (typename MEdge::const_iterator it = edges.begin(); it != edges.end(); ++it)
00193 {
00194 (*it)->addLeftFace();
00195 (*it)->getFromVertex()->addFace(F);
00196 }
00197
00198 return F;
00199 };
00200
00202 template<class N>
00203 void start(N *) {};
00204
00206 template<class N>
00207 void processNewFace(N *, Face *F) {};
00208
00210 template<class N>
00211 void end(N *) {};
00212
00213 protected:
00215 TemplateMesh(const TemplateMesh &N)
00216 : X3DNode(N), _vertices(N._vertices), _edges(N._edges), _faces(N._faces), _data(N._data)
00217 {
00218 };
00219
00220 private:
00221 MVertex _vertices;
00222 MEdge _edges;
00223 MFace _faces;
00224 MData _data;
00225 };
00226
00227 }
00228 }
00229
00230 #endif