00001 #ifndef SHAPE_MESH3D_HPP
00002 #define SHAPE_MESH3D_HPP
00003
00004 # include <shape/vertex.hpp>
00005 # include <shape/edge.hpp>
00006 # include <shape/face.hpp>
00007
00008 # define TMPL template<class C,class V>
00009 # define SELF mesh3d<C,V>
00010
00011
00012 namespace mmx { namespace shape {
00013
00014
00015 struct mesh3d_def {};
00016
00017 TMPL class mesh3d;
00018
00019 template<class C>
00020 struct use<mesh3d_def,C>
00021 {
00022 typedef vertex<C> Point;
00023 typedef edge<C,default_env,Point> Edge;
00024 typedef face<C,default_env,Point> Face;
00025 typedef mesh3d<C,default_env> Mesh;
00026 };
00027
00028
00029 template<class C,class V=default_env>
00030 class mesh3d {
00031 public:
00032
00033 typedef typename use<mesh3d_def,C,V>::Point Point;
00034 typedef typename use<mesh3d_def,C,V>::Edge Edge;
00035 typedef typename use<mesh3d_def,C,V>::Face Face;
00036
00037 mesh3d();
00038 ~mesh3d(void) ;
00039
00041 virtual void clear();
00042
00043 virtual void insert_vertex(Point*);
00044 virtual void insert_edge(Point*, Point*);
00045 virtual void insert_edge(Edge *);
00046 virtual void insert_face(Face *);
00047 virtual void insert_face(Point*, Point*, Point*);
00048 virtual void insert_face(Point*, Point*, Point*, Point*);
00049
00050 void push_back_vertex(Point* p) {this->insert_vertex(p);}
00051 void push_back_edge(int b, int e) {this->insert_edge(this->vertex(b), this->vertex(e));}
00052
00054 unsigned nbv() const { return m_vertices.size(); }
00056 unsigned nbe() const { return m_edges.size(); }
00058 unsigned nbf() const { return m_faces.size(); }
00059
00060 inline Seq<Point *> vertices(void) const { return m_vertices ; }
00061 inline Seq<Edge *> edges(void) const { return m_edges ; }
00062 inline Seq<Face *> faces(void) const { return m_faces ; }
00063
00064 virtual Point* vertex(int i) { return m_vertices[i] ; }
00065 virtual Edge* edge (int i) { return m_edges[i] ; }
00066 virtual Face* face (int i) { return m_faces[i] ; }
00067
00068
00069
00070 Seq<Point *> m_vertices ;
00071 Seq<Edge *> m_edges ;
00072 Seq<Face *> m_faces ;
00073
00074 int m_nbv;
00075 };
00076
00077 TMPL SELF::mesh3d(void) : m_nbv(0) {
00078
00079 }
00080
00081 TMPL SELF::~mesh3d(void) {
00082 }
00083
00084 TMPL void
00085 SELF::insert_vertex(Point * p) {
00086
00087 if (p->index()<0) {
00088 p->set_index(m_nbv);
00089 m_vertices << p;
00090 m_nbv++;
00091 }
00092 }
00093
00094
00095 TMPL void
00096 SELF::insert_edge(Point * p1, Point * p2) {
00097 if (p1->index()<0) this->insert_vertex(p1);
00098 if (p2->index()<0) this->insert_vertex(p2);
00099 m_edges<< new Edge(p1,p2);
00100 }
00101
00102
00103 TMPL void
00104 SELF::insert_edge(Edge* e) {
00105 m_edges<<e;
00106 }
00107
00108 TMPL void
00109 SELF::insert_face(Face * f) {
00110 m_faces<<f;
00111 }
00112
00113 TMPL void
00114 SELF::insert_face(Point * p1, Point* p2, Point* p3) {
00115 Face* f= new Face;
00116 if (p1->index()<0) this->insert_vertex(p1);
00117 if (p2->index()<0) this->insert_vertex(p2);
00118 if (p3->index()<0) this->insert_vertex(p3);
00119 f->insert(p1);
00120 f->insert(p2);
00121 f->insert(p3);
00122 m_faces<<f;
00123 }
00124
00125 TMPL void
00126 SELF::insert_face(Point * p1, Point* p2, Point* p3, Point* p4) {
00127 Face* f= new Face;
00128 if (p1->index()<0) this->insert_vertex(p1);
00129 if (p2->index()<0) this->insert_vertex(p2);
00130 if (p3->index()<0) this->insert_vertex(p3);
00131 if (p4->index()<0) this->insert_vertex(p4);
00132 f->insert(p1);
00133 f->insert(p2);
00134 f->insert(p3);
00135 f->insert(p4);
00136 m_faces<<f;
00137 }
00138
00139 TMPL void
00140 SELF::clear() {
00141
00142 m_faces.resize( 0 );
00143 m_edges.resize( 0 );
00144 m_vertices.resize( 0 );
00145 }
00146
00147
00148 template<class VIEWER, class C, class V>
00149 VIEWER& print_as_axl(VIEWER& out, const SELF& tp, unsigned d=0) {
00150
00151 typedef typename SELF::Point Point;
00152 typedef typename SELF::Edge Edge;
00153 typedef typename SELF::Face Face;
00154
00155 if (d<1 && tp.nbv()>0){
00156 out<<"<pointset color=\"rgb\">\n";
00157 out<<"<numberofpoints>"<<tp.nbv()<<"</numberofpoints>\n";
00158 out<<"<points>\n";
00159 foreach(Point* p, tp.vertices()) {
00160 out <<" "<<p->x()<<" "<<p->y()<<" "<<p->z()<<" 75 75 255\n";
00161 }
00162 out<<"</points>\n";
00163 out<<" </pointset>\n ";
00164 }
00165
00166
00167 if (d<2 && tp.nbe()>0){
00168 out<<" <curve type=\"mesh\">\n<vect>\nVECT\n";
00169 out<<tp.nbe()<<" "
00170 <<2*tp.nbe()<<" "
00171 <<tp.nbe()<<"\n";
00172
00173 for(unsigned i=0; i<tp.nbe();i++) out<<"2 ";
00174 out<<"\n";
00175 for(unsigned i=0; i<tp.nbe();i++) out<<"1 ";
00176 out<<"\n";
00177 foreach(Edge* e, tp.edges()) {
00178 out <<e->source()->x()<<" "<<e->source()->y()<<" "<<e->source()->z()<<" "
00179 <<e->destination()->x()<<" "<<e->destination()->y()<<" "<<e->destination()->z()
00180 <<"\n";
00181 }
00182 for(unsigned i=0; i<tp.edges().size();i++)
00183 out<< "0.814 0.279 0.2 1\n";
00184 out<<" </vect>\n </curve>\n";
00185 }
00186
00187 if(tp.nbf()>0) {
00188 std::map<Point*,int> index;
00189 int c=0;
00190 foreach(Point* p, tp.vertices()) {
00191 index[p]=c;c++;
00192 }
00193
00194 out<<"<mesh type=\"off\" color=\"255 10 0\">\n";
00195 out<<c<<" "<<tp.nbf()<<" 0\n";
00196 foreach(Point* p, tp.vertices()) {
00197 out <<p->x()<<" "<<p->y()<<" "<<p->z()<<" \n";
00198 }
00199
00200 foreach(Face* f, tp.faces()) {
00201
00202 out<<f->size()<<" ";
00203 int n=0;
00204 foreach(Point* p, f->points())
00205
00206 {
00207 out <<index[p]<<" ";
00208 n++;
00209 }
00210 out <<"\n";
00211 }
00212 out<<"</mesh>\n";
00213 }
00214
00215 return out;
00216 }
00217
00218 } ;
00219 } ;
00220
00221 # undef TMPL
00222 # undef SELF
00223 # endif // SHAPE_MESH3D_HPP