Mesh.h

Go to the documentation of this file.
00001 #ifndef __MESH_H
00002 #define __MESH_H
00003 
00004 #include <stdlib.h>
00005 #include <esg/Definitions.h>
00006 
00007 /*
00008  * Polygonal mesh.
00009  *
00010  * ATTENTION:
00011  *    Since a mesh may be very complex the clone() operation is therefore
00012  *    not defined. When clonning, re-create mesh from original geometry instead
00013  *
00014  *    edge loops:
00015  *    ----------
00016  *
00017  *               right in  \            /  right out
00018  *                          \          /
00019  *                           <---------
00020  *                          /          \
00021  *               left out  /            \ left in
00022  */
00023 
00024 
00025 namespace esg {
00026 
00027 class OGSCENE_EXPORT Mesh {
00028 public:
00029     // Declarations:
00030     struct Vert;
00031     struct Edge;
00032     struct Plane;
00033     struct Solid;
00034     
00035     // Definitions:
00036     struct MiniMax { float xmin, xmax, ymin, ymax, zmin, zmax; };
00037     
00038     struct Vert {
00039         int             jmeno_vrcholu;    // Name of vertex
00040         float           fxyz;             // Result of ax+by+cz+d=0
00041         int             qi;               // ?
00042         int             valid;            // Is this vertex valid ?
00043         struct Vert    *next_vertex;      // Pointer to next vertex in list
00044         struct Vert    *previous_vertex;  // Pointer to previous vertex in list
00045         float           x,y,z;            // Coordinates
00046         Vector3         normal;           // Normal vector
00047 
00048         bool hasValidNormal (void) const
00049         {
00050             if (normal.x!=0 || normal.y!=0 || normal.z!=0) return true;
00051             else return false;
00052         }
00053     };
00054     
00055     enum EdgeType{
00056       ZAKRYTA    = 1,      // Hidden
00057       OBRYSOVA   = 2,      // Contour
00058       VNITRNI    = 4,      // Inner
00059       ODPADOVA   = 8,      // Outher
00060       NULOVA     = 0x10,   // Null
00061       TELESOVA   = 0x20    // Solid's
00062     };
00063     
00064     struct Edge {
00065         int              jmeno_hrany;   // Name of edge
00066         enum   EdgeType  druh_hrany;    // Type of edge
00067         struct Edge     *next_edge;     // Pointer to next edge in list
00068         struct Edge     *previous_edge; // Pointer to previus edge in list
00069         struct Vert     *V1;            // Pointer to edge's first vertex
00070         struct Vert     *V2;            // Pointer to edge's second vertex
00071         struct Plane    *LeftLoop;      // Pointer to left plane
00072         struct Plane    *RightLoop;     // Pointer to right plane
00073         struct Edge     *LeftIn;        // Pointers to neibors edges
00074         struct Edge     *RightOut;
00075         struct Edge     *LeftOut;
00076         struct Edge     *RightIn;
00077     };
00078     
00079     struct Plane {
00080         int            jmeno_roviny;    // Name of plane
00081         struct Plane  *next_plane;      // Pointer to next plane in list
00082         struct Plane  *previous_plane;  // Pointer to previous plane in list
00083         struct Edge   *any_edge;        // Pointer to some edge of this plane
00084         float          a,b,c,d;         // a,b,c = normal vector directed out
00085         float          skalar;          // Scalar = a*x + b*y + c*z +d
00086     };
00087     
00088     struct Solid {
00089         int              solid_name;      // Name of solid
00090         struct Vert     *first_vertex;    // Pointer to first vertex in list
00091         struct Edge     *first_edge;      // Pointer to first edge in list
00092         struct Plane    *first_plane;     // Pointer to first plane in list
00093         struct Solid    *next_solid;      // Pointer to next solid
00094         struct Solid    *previous_solid;  // Pointer to previous solid
00095         struct MiniMax   cover;           // Cover
00096         Solid (int n = 0)
00097             : solid_name(n),
00098               first_vertex(NULL),
00099               first_edge(NULL),
00100               first_plane(NULL),
00101               next_solid(NULL),
00102               previous_solid(NULL) {}
00103     };
00104     
00105     
00106 
00107   protected:
00108     int           pocet_vrcholu;  // Number of all verteces
00109     int           pocet_hran;     // Number of edges
00110     int           pocet_rovin;    // Number of planes
00111     struct Solid* pSolid;         // Pointer to solid
00112     bool          outwardNormals; // All plane normals are outward oriented
00113                                   // (in contrast to random orientation)
00114 
00115     Vert*  pActVert;
00116     Edge*  pActEdge;
00117     Plane* pActPlane;
00118     Solid* pActSolid;
00119     
00120   protected:
00121     // Methods for manipulation with objects in solid's lists:
00122     void AppendVertex (Solid *S, Vert  *V);
00123     void AppendEdge   (Solid *S, Edge  *E);
00124     void AppendFace   (Solid *S, Plane *NP);
00125     void DeleteVertex (Solid *S, Vert  *V);
00126     void DeleteEdge   (Solid *S, Edge  *E);
00127     void DeleteFace   (Solid *S, Plane *NP);
00128     void MoveVertex   (Solid *S, Solid *SM, Vert  *V);
00129     void MoveEdge     (Solid *S, Solid *SM, Edge  *E);
00130     void MoveFace     (Solid *S, Solid *SM, Plane *NP);
00131 
00132     // Allocation of new primitive and adding to list:
00133     Vert   *NewVertex (Solid *S, float xp, float yp, float zp);
00134     Edge   *NewEdge   (Solid *S, Vert *V1p, Vert *V2p, Plane *P1p, Plane *P2p);
00135     Plane  *NewPlane  (Solid *S, float a1, float b1, float c1, float d1);
00136 
00137     // Manipulation with solids:
00138     void Copy_solid   (Solid *Sorigin, Solid *Scopy);
00139     void Delete_solid (Solid **S);
00140     
00141     // Help methods:
00142     int Paralel_planes (float  a,
00143                         float  b,
00144                         float  c,
00145                         float  ar,
00146                         float  br,
00147                         float  cr,
00148                         int&   same_orientation);
00149     
00150     void Vertex_Plane_position (struct Solid& S,
00151                                 float         ar,
00152                                 float         br,
00153                                 float         cr,
00154                                 float         dr,
00155                                 int&          Pozitiv,
00156                                 int&          Negativ);
00157     
00158     void  Orient_Edge           (Edge  *E,  Plane *F, Vert **Vin, Vert **Vout);
00159     Edge* Next_Edge_in_loop     (Edge  *CE, Vert **Vin, Vert **Vout);
00160     Edge* Previous_Edge_in_loop (Edge  *CE, Vert **Vin, Vert **Vout);
00161     void  ClasifyEdges          (Solid *S);
00162     
00163     // Allocation of primitives.
00164     // M = make, V = vertex, F = face, S = solid :
00165     Vert*   MV   (float xp, float yp, float zp);
00166     Plane*  MF   (float a1, float b1, float c1, float d1);
00167     Solid*  MVSF (Plane* F, Vert* V);
00168     void    MEV  (Solid *S, Vert* V1p, Vert* V2p, Edge** E, Plane* F);
00169     
00170     // E1 and E have shared vertex; replace E by E2:
00171     void ReplaceEdge (Edge* E1, Edge* E, Edge* E2);
00172     
00173     // Split edge E1 to two edges by vertex V:
00174     void SEMV   (Solid *S, Edge* E1, Vert* V, Edge** E2);
00175     void MEF    (Solid *S,Vert* V1p,Vert* V2p,Plane* F1,Plane* F2,Edge** E);
00176     void MEnotF (Solid *S,Vert* V1p,Vert* V2p,Plane* F1,Plane* F2,Edge** E);
00177     
00178     Vert*  Vertex_of_cut  (Vert* V1, Vert* V2);
00179     Plane* This_loop      (Edge* E, Vert* V);
00180     void   ConnectEdge    (Edge* E1, Edge* E, Edge* E2, Plane* P);
00181     void   KE             (Edge* E, Vert* Vin, Plane* P1,Plane* P2);
00182     void   SeparateSolids (Solid** NS, Solid** Odpad);
00183   
00184 
00185   
00186 public:
00187     Mesh (bool n = true) {
00188         pocet_vrcholu = 0; pocet_hran = 0; pocet_rovin = 0;
00189         pSolid = NULL; outwardNormals = n;
00190     }
00191     virtual ~Mesh() { if (pSolid) Delete_solid(&pSolid); }
00192 
00193     virtual void rotate    (const Matrix3&);
00194     virtual void translate (const Vector3&);
00195     virtual void transform (const Matrix4&);
00196     virtual void scale     (float, float, float);
00197     
00198     virtual void     resetActSolid();
00199     virtual int      goToNextSolid();
00200     virtual void     resetActPlane();
00201     virtual Plane*   getActPlane();
00202     virtual int      goToNextPlane();
00203     virtual void     resetActEdge();
00204     virtual int      goToNextEdge();
00205 
00206     // the bool parameter desides if the edge is properly re-oriented
00207     // before vertex inspection. When set to true then actual plane
00208     // must be set.
00209     virtual Vertex3  getActVert1(bool); // first vertex of actual edge
00210     virtual Vertex3  getActVert2(bool); // second vertex of actual edge
00211     virtual Vertex3  getActVert();      // actual vertex
00212     virtual Vertex3  getActVertNormal1(bool);
00213     virtual Vertex3  getActVertNormal2(bool);
00214     virtual Vertex3  getActVertNormal();
00215 
00216     virtual int getActVertID1(bool);
00217     virtual int getActVertID2(bool);
00218     virtual int getActVertID();
00219 
00220     virtual int     getActPlaneID();
00221     virtual Vertex3 getActPlaneNormal() const;
00222     virtual Vector3 getActPlaneCentroid();
00223     virtual double  getActPlaneArea();
00224     
00225     virtual void resetEdgeWalkInSolid();
00226     virtual int  stepInEdgeWalkInSolid();
00227 
00228     virtual void resetVertWalkInSolid();
00229     virtual bool stepInVertWalkInSolid();
00230 
00231     virtual int numberOfVertices (void) const { return pocet_vrcholu; }
00232     virtual int numberOfEdges    (void) const { return pocet_hran; }
00233 
00234     virtual bool hasOutwardNormals (void) const { return outwardNormals; }
00235 
00236     virtual void turnInsideOut (void); // invert normals
00237 }; // class Mesh
00238 
00239 }; // namespace
00240 
00241 #endif // __MESH_H

Generated on Wed Jun 28 12:24:28 2006 for esg by  doxygen 1.4.6