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