Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

MeshBuilderGeometry3DVisitor.h

Go to the documentation of this file.
00001 
00002 //                            MeshBuilderGeometry3DVisitor.h                  //
00004 
00005 #ifndef MESHBUILDERGEOMETRY3DVISITOR_H
00006 #define MESHBUILDERGEOMETRY3DVISITOR_H
00007 
00008 #include "Geometry3DVisitor.h"
00009 #include "MeshBuilderGlobalVariables.h"
00010 #include "Coordinate.h"
00011 #include "Box.h"
00012 #include "Cone.h"
00013 #include "Cylinder.h"
00014 #include "IndexedFaceSet.h"
00015 #include "Sphere.h"
00016 #include "TemplateMesh.h"
00017 
00018 namespace X3DTK {
00019 namespace X3D {
00020 
00022 
00023 template<class MData, class VData, class EData, class FData>
00024 class MeshBuilderGeometry3DVisitor : public Geometry3DVisitor
00025 {
00026 public:
00028   MeshBuilderGeometry3DVisitor()
00029   : Geometry3DVisitor()
00030   {
00031     // Enter functions.
00032     defineNewEnterFunction<MeshBuilderGeometry3DVisitor, Box>(&MeshBuilderGeometry3DVisitor::enterBox);
00033     defineNewEnterFunction<MeshBuilderGeometry3DVisitor, Cone>(&MeshBuilderGeometry3DVisitor::enterCone);
00034     defineNewEnterFunction<MeshBuilderGeometry3DVisitor, Cylinder>(&MeshBuilderGeometry3DVisitor::enterCylinder);
00035     defineNewEnterFunction<MeshBuilderGeometry3DVisitor, IndexedFaceSet>(&MeshBuilderGeometry3DVisitor::enterIndexedFaceSet);
00036     defineNewEnterFunction<MeshBuilderGeometry3DVisitor, Sphere>(&MeshBuilderGeometry3DVisitor::enterSphere);
00037 
00038     // GlobalVariables assignation.
00039     globalVariables = GVManager::getInstanceOf<MeshBuilderGlobalVariables>();
00040   };
00041   
00043   virtual ~MeshBuilderGeometry3DVisitor()
00044   {
00045   };
00046 
00048   virtual void enterBox(Box *B) const
00049   {
00050   };
00052   virtual void enterCone(Cone *C) const
00053   {
00054   };
00056   virtual void enterCylinder(Cylinder *C) const
00057   {
00058   };
00060   virtual void enterIndexedFaceSet(IndexedFaceSet *I) const
00061   {
00062     // future optim? reserve the vertices and edges.
00063   
00064     typedef Mesh::GenericVertex<VData, EData, FData> Vertex;
00065     typedef Mesh::GenericEdge<EData, FData, VData> Edge;
00066     typedef Mesh::MGenericEdge<EData, FData, VData> MEdge;
00067     typedef Mesh::GenericFace<FData, VData, EData> Face;
00068     typedef Mesh::MGenericFace<FData, VData, EData> MFace;
00069            
00070     Mesh::TemplateMesh<MData, VData, EData, FData> *TM = new Mesh::TemplateMesh<MData, VData, EData, FData>();
00071     globalVariables->getTop()->addChild(TM);
00072     globalVariables->pushNode(TM);
00073     
00074     // filling the mesh.
00075     // points creation
00076     Coordinate *CoordNode = static_cast<Coordinate *>(I->getCoord());
00077     if (CoordNode == NULL)
00078       return;
00079     
00080     MFVec3f pointArray = CoordNode->getPoint();
00081     if (pointArray.empty())
00082       return;
00083     
00084     // starting the algorithm.
00085     TM->start();
00086     
00087     // iterating the points
00088     for (MFVec3f::const_iterator itPoint = pointArray.begin(); itPoint != pointArray.end(); ++itPoint)
00089       TM->createVertex();
00090     
00091     SFBool ccw = I->getCcw();
00092     
00093     //iterating the faces
00094     MFInt32 coordIndex = I->getCoordIndex();
00095     MFInt32::const_iterator itCoordIndex = coordIndex.begin();
00096     
00097     while (itCoordIndex != coordIndex.end())
00098     {
00099       Vertex *A, *B;      
00100       Edge *E;
00101       // creating the edges of the face.
00102       Vertex *start = TM->getVertices()[*itCoordIndex];
00103       
00104       A = start;
00105       ++itCoordIndex;
00106 
00107       std::vector<Edge *> faceEdgeList;      
00108       while (*itCoordIndex != -1)
00109       {
00110         B = TM->getVertices()[*itCoordIndex];
00111         ++itCoordIndex;
00112       
00113         E = TM->getEdge(A, B);
00114         if (E == 0)
00115         {
00116           if (ccw)  
00117             E = TM->createEdge(A, B);
00118           else  
00119             E = TM->createEdge(B, A);
00120         }
00121         
00122         faceEdgeList.push_back(E);
00123         // moving to the next edge.
00124         A = B;
00125       }
00126       // ending the loop
00127       B = start;
00128       E = TM->getEdge(A, B);
00129       if (E == 0)
00130       {
00131         if (ccw)  
00132           E = TM->createEdge(A, B);
00133         else  
00134           E = TM->createEdge(B, A);
00135       }
00136         
00137       // creating a new face.
00138       MEdge me(faceEdgeList, ccw);
00139       
00140       Face *F = TM->createFace(me);
00141       
00142       // processing the new face.
00143       TM->processNewFace(F);
00144 
00145       ++itCoordIndex;
00146     }
00147     
00148     // ending the algorithm.
00149     TM->end();
00150   };
00152   virtual void enterSphere(Sphere *S) const
00153   {
00154   };
00155 
00156   
00157 protected:
00158   MeshBuilderGlobalVariables *globalVariables;
00159 };
00160 
00161 }
00162 }
00163 
00164 #endif

Generated on Mon Jun 16 13:50:56 2003 for X3DToolKit by doxygen1.3