home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / geometry / Surface.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  6KB  |  207 lines

  1.  
  2. #ifndef __SURFACE_H__
  3. #define __SURFACE_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     Surface base class.
  9.  
  10.     A surface is tesselated to a triangle mesh with each edge shared by
  11.     at most two triangles.
  12.  
  13. ===============================================================================
  14. */
  15.  
  16. typedef struct surfaceEdge_s {
  17.     int                        verts[2];    // edge vertices always with ( verts[0] < verts[1] )
  18.     int                        tris[2];    // edge triangles
  19. } surfaceEdge_t;
  20.  
  21.  
  22. class idSurface {
  23. public:
  24.                             idSurface( void );
  25.                             explicit idSurface( const idSurface &surf );
  26.                             explicit idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes );
  27.                             ~idSurface( void );
  28.  
  29.     const idDrawVert &        operator[]( const int index ) const;
  30.     idDrawVert &            operator[]( const int index );
  31.     idSurface &                operator+=( const idSurface &surf );
  32.  
  33.     int                        GetNumIndexes( void ) const { return indexes.Num(); }
  34.     const int *                GetIndexes( void ) const { return indexes.Ptr(); }
  35.     int                        GetNumVertices( void ) const { return verts.Num(); }
  36.     const idDrawVert *        GetVertices( void ) const { return verts.Ptr(); }
  37.     const int *                GetEdgeIndexes( void ) const { return edgeIndexes.Ptr(); }
  38.     const surfaceEdge_t *    GetEdges( void ) const { return edges.Ptr(); }
  39.  
  40.     void                    Clear( void );
  41.     void                    SwapTriangles( idSurface &surf );
  42.     void                    TranslateSelf( const idVec3 &translation );
  43.     void                    RotateSelf( const idMat3 &rotation );
  44.  
  45.                             // splits the surface into a front and back surface, the surface itself stays unchanged
  46.                             // frontOnPlaneEdges and backOnPlaneEdges optionally store the indexes to the edges that lay on the split plane
  47.                             // returns a SIDE_?
  48.     int                        Split( const idPlane &plane, const float epsilon, idSurface **front, idSurface **back, int *frontOnPlaneEdges = NULL, int *backOnPlaneEdges = NULL ) const;
  49.                             // cuts off the part at the back side of the plane, returns true if some part was at the front
  50.                             // if there is nothing at the front the number of points is set to zero
  51.     bool                    ClipInPlace( const idPlane &plane, const float epsilon = ON_EPSILON, const bool keepOn = false );
  52.  
  53.                             // returns true if each triangle can be reached from any other triangle by a traversal
  54.     bool                    IsConnected( void ) const;
  55.                             // returns true if the surface is closed
  56.     bool                    IsClosed( void ) const;
  57.                             // returns true if the surface is a convex hull
  58.     bool                    IsPolytope( const float epsilon = 0.1f ) const;
  59.  
  60.     float                    PlaneDistance( const idPlane &plane ) const;
  61.     int                        PlaneSide( const idPlane &plane, const float epsilon = ON_EPSILON ) const;
  62.  
  63.                             // returns true if the line intersects one of the surface triangles
  64.     bool                    LineIntersection( const idVec3 &start, const idVec3 &end, bool backFaceCull = false ) const;
  65.                             // intersection point is start + dir * scale
  66.     bool                    RayIntersection( const idVec3 &start, const idVec3 &dir, float &scale, bool backFaceCull = false ) const;
  67.  
  68. protected:
  69.     idList<idDrawVert>        verts;            // vertices
  70.     idList<int>                indexes;        // 3 references to vertices for each triangle
  71.     idList<surfaceEdge_t>    edges;            // edges
  72.     idList<int>                edgeIndexes;    // 3 references to edges for each triangle, may be negative for reversed edge
  73.  
  74. protected:
  75.     void                    GenerateEdgeIndexes( void );
  76.     int                        FindEdge( int v1, int v2 ) const;
  77. };
  78.  
  79. /*
  80. ====================
  81. idSurface::idSurface
  82. ====================
  83. */
  84. ID_INLINE idSurface::idSurface( void ) {
  85. }
  86.  
  87. /*
  88. =================
  89. idSurface::idSurface
  90. =================
  91. */
  92. ID_INLINE idSurface::idSurface( const idDrawVert *verts, const int numVerts, const int *indexes, const int numIndexes ) {
  93.     assert( verts != NULL && indexes != NULL && numVerts > 0 && numIndexes > 0 );
  94.     this->verts.SetNum( numVerts );
  95.     memcpy( this->verts.Ptr(), verts, numVerts * sizeof( verts[0] ) );
  96.     this->indexes.SetNum( numIndexes );
  97.     memcpy( this->indexes.Ptr(), indexes, numIndexes * sizeof( indexes[0] ) );
  98.     GenerateEdgeIndexes();
  99. }
  100.  
  101. /*
  102. ====================
  103. idSurface::idSurface
  104. ====================
  105. */
  106. ID_INLINE idSurface::idSurface( const idSurface &surf ) {
  107.     this->verts = surf.verts;
  108.     this->indexes = surf.indexes;
  109.     this->edges = surf.edges;
  110.     this->edgeIndexes = surf.edgeIndexes;
  111. }
  112.  
  113. /*
  114. ====================
  115. idSurface::~idSurface
  116. ====================
  117. */
  118. ID_INLINE idSurface::~idSurface( void ) {
  119. }
  120.  
  121. /*
  122. =================
  123. idSurface::operator[]
  124. =================
  125. */
  126. ID_INLINE const idDrawVert &idSurface::operator[]( const int index ) const {
  127.     return verts[ index ];
  128. };
  129.  
  130. /*
  131. =================
  132. idSurface::operator[]
  133. =================
  134. */
  135. ID_INLINE idDrawVert &idSurface::operator[]( const int index ) {
  136.     return verts[ index ];
  137. };
  138.  
  139. /*
  140. =================
  141. idSurface::operator+=
  142. =================
  143. */
  144. ID_INLINE idSurface &idSurface::operator+=( const idSurface &surf ) {
  145.     int i, m, n;
  146.     n = verts.Num();
  147.     m = indexes.Num();
  148.     verts.Append( surf.verts );            // merge verts where possible ?
  149.     indexes.Append( surf.indexes );
  150.     for ( i = m; i < indexes.Num(); i++ ) {
  151.         indexes[i] += n;
  152.     }
  153.     GenerateEdgeIndexes();
  154.     return *this;
  155. }
  156.  
  157. /*
  158. =================
  159. idSurface::Clear
  160. =================
  161. */
  162. ID_INLINE void idSurface::Clear( void ) {
  163.     verts.Clear();
  164.     indexes.Clear();
  165.     edges.Clear();
  166.     edgeIndexes.Clear();
  167. }
  168.  
  169. /*
  170. =================
  171. idSurface::SwapTriangles
  172. =================
  173. */
  174. ID_INLINE void idSurface::SwapTriangles( idSurface &surf ) {
  175.     verts.Swap( surf.verts );
  176.     indexes.Swap( surf.indexes );
  177.     edges.Swap( surf.edges );
  178.     edgeIndexes.Swap( surf.edgeIndexes );
  179. }
  180.  
  181. /*
  182. =================
  183. idSurface::TranslateSelf
  184. =================
  185. */
  186. ID_INLINE void idSurface::TranslateSelf( const idVec3 &translation ) {
  187.     for ( int i = 0; i < verts.Num(); i++ ) {
  188.         verts[i].xyz += translation;
  189.     }
  190. }
  191.  
  192. /*
  193. =================
  194. idSurface::RotateSelf
  195. =================
  196. */
  197. ID_INLINE void idSurface::RotateSelf( const idMat3 &rotation ) {
  198.     for ( int i = 0; i < verts.Num(); i++ ) {
  199.         verts[i].xyz *= rotation;
  200.         verts[i].normal *= rotation;
  201.         verts[i].tangents[0] *= rotation;
  202.         verts[i].tangents[1] *= rotation;
  203.     }
  204. }
  205.  
  206. #endif /* !__SURFACE_H__ */
  207.