csgeom/trimesh.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csgeom/trimesh.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2002 by Jorrit Tyberghein
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_TRIMESH_H__
    00020 #define __CS_TRIMESH_H__
    00021
    00022 #include "csextern.h"
    00023 #include "csutil/scf_implementation.h"
    00024
    00025 #include "csgeom/tri.h"
    00026 #include "csgeom/vector3.h"
    00027 #include "csgeom/box.h"
    00028 #include "igeom/trimesh.h"
    00029
    00030 #include "csutil/array.h"
    00031 #include "csutil/flags.h"
    00032 #include "csutil/dirtyaccessarray.h"
    00033
    00045 class CS_CRYSTALSPACE_EXPORT csTriangleMesh :
    00046   public scfImplementation1<csTriangleMesh, iTriangleMesh>
    00047 {
    00048 protected:
    00050   csDirtyAccessArray<csTriangle> triangles;
    00051   // The vertices.
    00052   csDirtyAccessArray<csVector3> vertices;
    00053
    00054   uint32 change_nr;
    00055   csFlags flags;
    00056
    00057 public:
    00059   csTriangleMesh () : scfImplementationType (this), change_nr (0) { }
    00061   csTriangleMesh (const csTriangleMesh& mesh);
    00063   virtual ~csTriangleMesh ();
    00064
    00066   void AddVertex (const csVector3& v);
    00068   virtual size_t GetVertexCount () { return vertices.GetSize (); }
    00070   size_t GetVertexCount () const { return vertices.GetSize (); }
    00072   virtual csVector3* GetVertices () { return vertices.GetArray (); }
    00074   const csVector3* GetVertices () const { return vertices.GetArray (); }
    00075
    00077   void AddTriangle (int a, int b, int c);
    00079         void AddTriangleMesh(const csTriangleMesh& tm);
    00081   virtual csTriangle* GetTriangles () { return triangles.GetArray (); }
    00083   const csTriangle* GetTriangles () const { return triangles.GetArray (); }
    00085   csTriangle& GetTriangle (int i) { return triangles[i]; }
    00087   size_t GetTriangleCount () const { return triangles.GetSize (); }
    00089   virtual size_t GetTriangleCount () { return triangles.GetSize (); }
    00090
    00092   void Clear ();
    00094   void SetSize (int count);
    00096   void SetTriangles (csTriangle const* trigs, int count);
    00097
    00098   virtual void Lock () { }
    00099   virtual void Unlock () { }
    00100   virtual csFlags& GetFlags () { return flags; }
    00101   virtual uint32 GetChangeNumber () const { return change_nr; }
    00102
    00104         csTriangleMesh& operator+=(const csTriangleMesh& tm);
    00105 };
    00106
    00112 class CS_CRYSTALSPACE_EXPORT csTriangleVertex
    00113 {
    00114 public:
    00116   csVector3 pos;
    00118   int idx;
    00119
    00121   csArray<size_t> con_triangles;
    00122
    00124   csArray<int> con_vertices;
    00125
    00127   csTriangleVertex () { }
    00129   ~csTriangleVertex () { }
    00131   void AddTriangle (size_t idx);
    00133   void AddVertex (int idx);
    00134 };
    00135
    00140 class CS_CRYSTALSPACE_EXPORT csTriangleVertices
    00141 {
    00142 protected:
    00143   csTriangleVertex* vertices;
    00144   int num_vertices;
    00145
    00146 public:
    00148   csTriangleVertices (csTriangleMesh* mesh, csVector3* verts, int num_verts);
    00150   ~csTriangleVertices ();
    00155   void UpdateVertices (csVector3* verts);
    00156
    00158   int GetVertexCount () const { return num_vertices; }
    00160   csTriangleVertex& GetVertex (int idx) { return vertices[idx]; }
    00161 };
    00162
    00166 class CS_CRYSTALSPACE_EXPORT csTriangleMeshBox :
    00167   public scfImplementation1<csTriangleMeshBox,iTriangleMesh>
    00168 {
    00169 private:
    00170   csVector3 vertices[8];
    00171   csTriangle triangles[12];
    00172   uint32 change_nr;
    00173   csFlags flags;
    00174
    00175 public:
    00179   csTriangleMeshBox (const csBox3& box) : scfImplementationType(this)
    00180   {
    00181     change_nr = 0;
    00182     triangles[0].Set (4, 5, 1);
    00183     triangles[1].Set (4, 1, 0);
    00184     triangles[2].Set (5, 7, 3);
    00185     triangles[3].Set (5, 3, 1);
    00186     triangles[4].Set (7, 6, 2);
    00187     triangles[5].Set (7, 2, 3);
    00188     triangles[6].Set (6, 4, 0);
    00189     triangles[7].Set (6, 0, 2);
    00190     triangles[8].Set (6, 7, 5);
    00191     triangles[9].Set (6, 5, 4);
    00192     triangles[10].Set (0, 1, 3);
    00193     triangles[11].Set (0, 3, 2);
    00194     SetBox (box);
    00195
    00196     flags.SetAll (CS_TRIMESH_CLOSED | CS_TRIMESH_CONVEX);
    00197   }
    00198
    00199   virtual ~csTriangleMeshBox ()
    00200   {
    00201   }
    00202
    00206   void SetBox (const csBox3& box)
    00207   {
    00208     change_nr++;
    00209     vertices[0] = box.GetCorner (0);
    00210     vertices[1] = box.GetCorner (1);
    00211     vertices[2] = box.GetCorner (2);
    00212     vertices[3] = box.GetCorner (3);
    00213     vertices[4] = box.GetCorner (4);
    00214     vertices[5] = box.GetCorner (5);
    00215     vertices[6] = box.GetCorner (6);
    00216     vertices[7] = box.GetCorner (7);
    00217   }
    00218
    00219   virtual size_t GetVertexCount () { return 8; }
    00220   virtual csVector3* GetVertices () { return vertices; }
    00221   virtual size_t GetTriangleCount () { return 12; }
    00222   virtual csTriangle* GetTriangles () { return triangles; }
    00223   virtual void Lock () { }
    00224   virtual void Unlock () { }
    00225   virtual csFlags& GetFlags () { return flags; }
    00226   virtual uint32 GetChangeNumber () const { return change_nr; }
    00227 };
    00228
    00236 class CS_CRYSTALSPACE_EXPORT csTriangleMeshPointer :
    00237   public scfImplementation1<csTriangleMeshPointer,iTriangleMesh>
    00238 {
    00239 private:
    00240   csVector3* vertices;
    00241   size_t num_vertices;
    00242   csTriangle* triangles;
    00243   size_t num_triangles;
    00244   uint32 change_nr;
    00245   csFlags flags;
    00246
    00247 public:
    00251   csTriangleMeshPointer (csVector3* vertices, size_t num_vertices,
    00252       csTriangle* triangles, size_t num_triangles)
    00253     : scfImplementationType(this)
    00254   {
    00255     change_nr = 0;
    00256     csTriangleMeshPointer::vertices = vertices;
    00257     csTriangleMeshPointer::num_vertices = num_vertices;
    00258     csTriangleMeshPointer::triangles = triangles;
    00259     csTriangleMeshPointer::num_triangles = num_triangles;
    00260   }
    00261
    00262   virtual ~csTriangleMeshPointer ()
    00263   {
    00264   }
    00265
    00266   virtual size_t GetVertexCount () { return num_vertices; }
    00267   virtual csVector3* GetVertices () { return vertices; }
    00268   virtual size_t GetTriangleCount () { return num_triangles; }
    00269   virtual csTriangle* GetTriangles () { return triangles; }
    00270   virtual void Lock () { }
    00271   virtual void Unlock () { }
    00272   virtual csFlags& GetFlags () { return flags; }
    00273   virtual uint32 GetChangeNumber () const { return change_nr; }
    00274 };
    00275
    00278 #endif // __CS_TRIMESH_H__
    00279