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