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

  1.  
  2. #ifndef __WINDING2D_H__
  3. #define __WINDING2D_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     A 2D winding is an arbitrary convex 2D polygon defined by an array of points.
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. #define    MAX_POINTS_ON_WINDING_2D        16
  14.  
  15.  
  16. class idWinding2D {
  17. public:
  18.                     idWinding2D( void );
  19.  
  20.     idWinding2D &    operator=( const idWinding2D &winding );
  21.     const idVec2 &    operator[]( const int index ) const;
  22.     idVec2 &        operator[]( const int index );
  23.  
  24.     void            Clear( void );
  25.     void            AddPoint( const idVec2 &point );
  26.     int                GetNumPoints( void ) const;
  27.  
  28.     void            Expand( const float d );
  29.     void            ExpandForAxialBox( const idVec2 bounds[2] );
  30.  
  31.                     // splits the winding into a front and back winding, the winding itself stays unchanged
  32.                     // returns a SIDE_?
  33.     int                Split( const idVec3 &plane, const float epsilon, idWinding2D **front, idWinding2D **back ) const;
  34.                     // cuts off the part at the back side of the plane, returns true if some part was at the front
  35.                     // if there is nothing at the front the number of points is set to zero
  36.     bool            ClipInPlace( const idVec3 &plane, const float epsilon = ON_EPSILON, const bool keepOn = false );
  37.  
  38.     idWinding2D *    Copy( void ) const;
  39.     idWinding2D *    Reverse( void ) const;
  40.  
  41.     float            GetArea( void ) const;
  42.     idVec2            GetCenter( void ) const;
  43.     float            GetRadius( const idVec2 ¢er ) const;
  44.     void            GetBounds( idVec2 bounds[2] ) const;
  45.  
  46.     bool            IsTiny( void ) const;
  47.     bool            IsHuge( void ) const;    // base winding for a plane is typically huge
  48.     void            Print( void ) const;
  49.  
  50.     float            PlaneDistance( const idVec3 &plane ) const;
  51.     int                PlaneSide( const idVec3 &plane, const float epsilon = ON_EPSILON ) const;
  52.  
  53.     bool            PointInside( const idVec2 &point, const float epsilon ) const;
  54.     bool            LineIntersection( const idVec2 &start, const idVec2 &end ) const;
  55.     bool            RayIntersection( const idVec2 &start, const idVec2 &dir, float &scale1, float &scale2, int *edgeNums = NULL ) const;
  56.  
  57.     static idVec3    Plane2DFromPoints( const idVec2 &start, const idVec2 &end, const bool normalize = false );
  58.     static idVec3    Plane2DFromVecs( const idVec2 &start, const idVec2 &dir, const bool normalize = false );
  59.     static bool        Plane2DIntersection( const idVec3 &plane1, const idVec3 &plane2, idVec2 &point );
  60.  
  61. private:
  62.     int                numPoints;
  63.     idVec2            p[MAX_POINTS_ON_WINDING_2D];
  64. };
  65.  
  66. ID_INLINE idWinding2D::idWinding2D( void ) {
  67.     numPoints = 0;
  68. }
  69.  
  70. ID_INLINE idWinding2D &idWinding2D::operator=( const idWinding2D &winding ) {
  71.     int i;
  72.  
  73.     for ( i = 0; i < winding.numPoints; i++ ) {
  74.         p[i] = winding.p[i];
  75.     }
  76.     numPoints = winding.numPoints;
  77.     return *this;
  78. }
  79.  
  80. ID_INLINE const idVec2 &idWinding2D::operator[]( const int index ) const {
  81.     return p[ index ];
  82. }
  83.  
  84. ID_INLINE idVec2 &idWinding2D::operator[]( const int index ) {
  85.     return p[ index ];
  86. }
  87.  
  88. ID_INLINE void idWinding2D::Clear( void ) {
  89.     numPoints = 0;
  90. }
  91.  
  92. ID_INLINE void idWinding2D::AddPoint( const idVec2 &point ) {
  93.     p[numPoints++] = point;
  94. }
  95.  
  96. ID_INLINE int idWinding2D::GetNumPoints( void ) const {
  97.     return numPoints;
  98. }
  99.  
  100. ID_INLINE idVec3 idWinding2D::Plane2DFromPoints( const idVec2 &start, const idVec2 &end, const bool normalize ) {
  101.     idVec3 plane;
  102.     plane.x = start.y - end.y;
  103.     plane.y = end.x - start.x;
  104.     if ( normalize ) {
  105.         plane.ToVec2().Normalize();
  106.     }
  107.     plane.z = - ( start.x * plane.x + start.y * plane.y );
  108.     return plane;
  109. }
  110.  
  111. ID_INLINE idVec3 idWinding2D::Plane2DFromVecs( const idVec2 &start, const idVec2 &dir, const bool normalize ) {
  112.     idVec3 plane;
  113.     plane.x = -dir.y;
  114.     plane.y = dir.x;
  115.     if ( normalize ) {
  116.         plane.ToVec2().Normalize();
  117.     }
  118.     plane.z = - ( start.x * plane.x + start.y * plane.y );
  119.     return plane;
  120. }
  121.  
  122. ID_INLINE bool idWinding2D::Plane2DIntersection( const idVec3 &plane1, const idVec3 &plane2, idVec2 &point ) {
  123.     float n00, n01, n11, det, invDet, f0, f1;
  124.  
  125.     n00 = plane1.x * plane1.x + plane1.y * plane1.y;
  126.     n01 = plane1.x * plane2.x + plane1.y * plane2.y;
  127.     n11 = plane2.x * plane2.x + plane2.y * plane2.y;
  128.     det = n00 * n11 - n01 * n01;
  129.  
  130.     if ( idMath::Fabs(det) < 1e-6f ) {
  131.         return false;
  132.     }
  133.  
  134.     invDet = 1.0f / det;
  135.     f0 = ( n01 * plane2.z - n11 * plane1.z ) * invDet;
  136.     f1 = ( n01 * plane1.z - n00 * plane2.z ) * invDet;
  137.     point.x = f0 * plane1.x + f1 * plane2.x;
  138.     point.y = f0 * plane1.y + f1 * plane2.y;
  139.     return true;
  140. }
  141.  
  142. #endif /* !__WINDING2D_H__ */
  143.