gee_triangle_mesh.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /* COPYRIGHT
00004  *
00005  * This file is part of the Visual Effects Engine - VEE
00006  *
00007  * Read the "VEE-LICENSE" file for the license.
00008  *
00009  * Authors & Copyright:   
00010  *
00011  * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 
00012  * Janne Kontkanen 2001-2004
00013  *
00014  * Additional copyrights: Tekes 2003-2004
00015  *
00016  * firstname.lastname@hut.fi
00017  *
00018  */
00019 
00020 #ifndef GEE_TRIANGLE_MESH_H
00021 #define GEE_TRIANGLE_MESH_H
00022 
00023 #include <gee_geometry.h>
00024 
00025 #include <vee_bounding_box.h>
00026 #include <vee_vector2.h>
00027 #include <vee_vector3.h>
00028 #include <vee_vector4.h>
00029 
00030 #include <map>
00031 #include <vector>
00032 
00033 class GEE_Io;
00034 
00039 class GEE_PolygonVertex
00040 {
00041 public:
00042   GEE_PolygonVertex(const VEE_Vector3 & location = VEE_Vector3ZERO,
00043         const VEE_Vector3 & normal   = VEE_Vector3ZERO,
00044         const VEE_Vector2 & uv       = VEE_Vector2ZERO,
00045         const VEE_Vector4 & rgba     = VEE_Vector4ONE)
00046     : m_location(location), m_normal(normal), m_uv(uv), m_rgba(rgba),
00047       m_selectionState(0)
00048   {}
00049 
00050   void addPolygon(int p);
00051   bool removePolygon(int);
00052 
00053   bool read(GEE_Io *);
00054   bool write(GEE_Io *);
00055   
00057   VEE_Vector3 m_location;
00059   VEE_Vector3 m_normal;
00061   VEE_Vector2 m_uv;
00062 
00063   VEE_Vector4 m_rgba;
00064 
00066   std::vector<int> m_polygons;
00067   uchar m_selectionState;
00068 };
00069 
00070 class GEE_TriangleIndices
00071 {
00072 public:
00073 
00074   GEE_TriangleIndices(int v1 = -1, int v2 = -1, int v3 = -1, 
00075           bool continues = false, uint stripIndex = 0)
00076     : m_continues(continues), 
00077       m_selectionState(0),
00078       m_stripIndex(stripIndex)
00079   {
00080     m_indices[0] = v1; m_indices[1] = v2; m_indices[2] = v3; 
00081   }
00082 
00083   bool read(GEE_Io *);
00084   bool write(GEE_Io *);
00085   
00086   void make(int v1, int v2, int v3)
00087   { m_indices[0] = v1; m_indices[1] = v2; m_indices[2] = v3; }
00088 
00089   int   m_indices[3];
00090 
00091   bool  m_continues;
00092   uchar m_selectionState;
00093   uint  m_stripIndex;
00094 };
00095 
00096 enum GEE_TriMeshItem {
00097   GEE_TRIMESH_UNKNOWN,
00099   GEE_TRIMESH_TRILIST,
00101   GEE_TRIMESH_TRIS,
00103   GEE_TRIMESH_END,
00104   GEE_TRIMESH_COLOR,
00105   GEE_TRIMESH_NORMAL,
00106   GEE_TRIMESH_LOCATION,
00107   GEE_TRIMESH_UV
00108 };
00109 
00111 class GEE_TriMeshListItem
00112 {
00113 public:
00114   GEE_TriMeshListItem(GEE_TriMeshItem type = GEE_TRIMESH_UNKNOWN) 
00115     : m_type(type ) {}
00116 
00117   GEE_TriMeshItem m_type;
00118   VEE_Vector4     m_data;
00119 };
00120 
00124 class GEE_TriMeshList
00125 {
00126 public:
00127   typedef std::vector<GEE_TriMeshListItem> container;
00128   typedef container::iterator iterator;
00129 
00130   GEE_TriMeshList() : m_size(0), m_generation(-1) {}
00131   ~GEE_TriMeshList() {}
00132 
00133   void reserve(uint n) { if(m_list.size() < n) m_list.resize(n); }
00134   void prepareNew() { m_size = 0; }
00135   void addItem(const GEE_TriMeshListItem & item)
00136   {
00137     if(m_size + 1 > m_list.size())
00138       m_list.resize(m_size + 100);
00139     m_list[m_size] = item;
00140     m_size++;
00141   }
00142 
00143   void addItem2(GEE_TriMeshItem type) { addItem(GEE_TriMeshListItem(type)); }
00144 
00145   iterator begin() { return m_list.begin(); }
00146   iterator end()   { return m_list.begin() + m_size; }
00147 
00148   container m_list;
00149   
00150   uint m_size;
00151 
00152   int m_generation;
00153 };
00154 
00173 class GEE_TriangleMesh : public GEE_Geometry
00174 {
00175 public: 
00176   typedef std::map<int, GEE_PolygonVertex>::iterator vertexIterator;
00177   typedef std::map<int, GEE_TriangleIndices>::iterator triangleIterator;
00178 
00179   GEE_TriangleMesh();
00180   virtual ~GEE_TriangleMesh();
00181 
00182   static GEE_Geometry * create();
00183   virtual GEE_Geometry * clone() const;
00184   virtual bool eraseData(GEE_Containment *);
00185   virtual bool intersects(GEE_Containment *);
00186   virtual void selectVertices(GEE_Containment *,  
00187             uint mask, 
00188             uint values);
00189   virtual void selectFaces(GEE_Containment *,  
00190          uint mask, 
00191          uint values);
00192   virtual bool colorData(GEE_ReColor *);
00193   virtual bool moveData(GEE_Mover *);  
00194   virtual void render(GEE_Renderer *);
00195   virtual bool getBoundingBox(VEE_BoundingBox *);
00196   virtual bool move(const VEE_Vector3 & offset);
00197 
00198   virtual bool smooth(GEE_Containment *);
00199 
00200   virtual bool read(GEE_Io *);
00201   virtual bool write(GEE_Io *);
00202 
00203   GEE_Geometry * removeData(const VEE_Vector3 &loc, float radius);
00204   void clear();
00205 
00206   int addVertex(const GEE_PolygonVertex &);
00207   void setVertex(int, const GEE_PolygonVertex &);
00208   inline GEE_PolygonVertex * getVertex(int);
00209   bool removeVertex(int index);
00210   uint vertexCounter() const { return m_vertexCounter; }
00211 
00212   int addTriangle(const GEE_TriangleIndices &, int id = -1);
00213   GEE_TriangleIndices * getTriangle(int);
00214 
00215   triangleIterator beginTriangle() { return m_triangles.begin(); }
00216   triangleIterator endTriangle()   { return m_triangles.end(); }
00217   uint triangleCount() const { return m_triangles.size(); }
00218 
00219   vertexIterator beginVertex() { return m_vertices.begin(); }
00220   vertexIterator endVertex()   { return m_vertices.end(); }
00221   uint vertexCount() const { return m_vertices.size(); }
00222 
00223   static GEE_TriangleIndices & getTriangle(triangleIterator &it)
00224   { return (*it).second; }
00225 
00226   void autoNormalize();
00227   void normalizeVertex(vertexIterator &it, bool adjustGeneration = true);
00228   bool normalizeVertex(int index, bool adjustGeneration = true)
00229   {
00230     vertexIterator it = m_vertices.find(index);
00231     if(it != m_vertices.end()) {
00232       normalizeVertex(it, adjustGeneration);
00233       return true;
00234     }
00235     return false;
00236   }
00237 
00238   void createRectangularGrid(uint xdivision, uint ydivision,
00239            float width = 1.0f, float height = 1.0f);
00240 
00241   static GEE_TriangleMesh * cast(GEE_Geometry *d) 
00242   { return dynamic_cast<GEE_TriangleMesh *> (d); }
00243 
00244   void setUseStrips(bool useStrips) { m_useStrips =  useStrips; }
00245   bool useStrips() const { return m_useStrips; }
00246 
00249   void vertexWasMoved(const VEE_Vector3 & newLocation)
00250   { m_bbox.expand(newLocation); }
00251 
00252   VEE_Vector3 triangleNormal(int, int);
00253 
00254   float vertexWrinkliness(int vertex);
00255   float vertexWrinklinessLinked(int vertex);
00256   VEE_Vector3 smoothVertex(int vertex, float effect);
00257   VEE_Vector3 smoothVertex2(int vertex, int iterations, float effect);
00258 
00259   GEE_TriMeshList & list() { return m_list; }
00260   bool genList();
00261 
00262 protected:
00263 
00264   void listifyVertex(const GEE_PolygonVertex &);
00265 
00268   void eraseTriangleInternal(int tri)
00269   {
00270     triangleIterator it = m_triangles.find(tri);
00271     if(it != m_triangles.end())
00272       m_triangles.erase(it);
00273   }
00274 
00275   std::map<int, GEE_PolygonVertex>   m_vertices;
00276   std::map<int, GEE_TriangleIndices> m_triangles;
00277 
00278   int m_vertexCounter;
00279   int m_triangleCounter;
00280 
00281   bool m_useStrips;
00282 
00283   VEE_BoundingBox m_bbox;
00284 
00285   GEE_TriMeshList m_list;
00286 };
00287 
00290 inline GEE_PolygonVertex * GEE_TriangleMesh::getVertex(int id)
00291 {
00292   vertexIterator it = m_vertices.find(id);
00293   
00294   if(it != m_vertices.end()) {
00295     return & (*it).second;
00296   }
00297 
00298   return 0;
00299 }
00300 
00301 #endif

Generated on Mon Mar 12 21:09:00 2007 for VEE - The Visual Effects Engine by  doxygen 1.4.6