Winding2D.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __WINDING2D_H__
  21. #define __WINDING2D_H__
  22. /*
  23. ===============================================================================
  24. A 2D winding is an arbitrary convex 2D polygon defined by an array of points.
  25. ===============================================================================
  26. */
  27. #define MAX_POINTS_ON_WINDING_2D 16
  28. class idWinding2D {
  29. public:
  30. idWinding2D();
  31. idWinding2D & operator=( const idWinding2D &winding );
  32. const idVec2 & operator[]( const int index ) const;
  33. idVec2 & operator[]( const int index );
  34. void Clear();
  35. void AddPoint( const idVec2 &point );
  36. int GetNumPoints() const;
  37. void Expand( const float d );
  38. void ExpandForAxialBox( const idVec2 bounds[2] );
  39. // splits the winding into a front and back winding, the winding itself stays unchanged
  40. // returns a SIDE_?
  41. int Split( const idVec3 &plane, const float epsilon, idWinding2D **front, idWinding2D **back ) const;
  42. // cuts off the part at the back side of the plane, returns true if some part was at the front
  43. // if there is nothing at the front the number of points is set to zero
  44. bool ClipInPlace( const idVec3 &plane, const float epsilon = ON_EPSILON, const bool keepOn = false );
  45. idWinding2D * Copy() const;
  46. idWinding2D * Reverse() const;
  47. float GetArea() const;
  48. idVec2 GetCenter() const;
  49. float GetRadius( const idVec2 &center ) const;
  50. void GetBounds( idVec2 bounds[2] ) const;
  51. bool IsTiny() const;
  52. bool IsHuge() const; // base winding for a plane is typically huge
  53. void Print() const;
  54. float PlaneDistance( const idVec3 &plane ) const;
  55. int PlaneSide( const idVec3 &plane, const float epsilon = ON_EPSILON ) const;
  56. bool PointInside( const idVec2 &point, const float epsilon ) const;
  57. bool LineIntersection( const idVec2 &start, const idVec2 &end ) const;
  58. bool RayIntersection( const idVec2 &start, const idVec2 &dir, float &scale1, float &scale2, int *edgeNums = NULL ) const;
  59. static idVec3 Plane2DFromPoints( const idVec2 &start, const idVec2 &end, const bool normalize = false );
  60. static idVec3 Plane2DFromVecs( const idVec2 &start, const idVec2 &dir, const bool normalize = false );
  61. static bool Plane2DIntersection( const idVec3 &plane1, const idVec3 &plane2, idVec2 &point );
  62. private:
  63. int numPoints;
  64. idVec2 p[MAX_POINTS_ON_WINDING_2D];
  65. };
  66. ID_INLINE idWinding2D::idWinding2D() {
  67. numPoints = 0;
  68. }
  69. ID_INLINE idWinding2D &idWinding2D::operator=( const idWinding2D &winding ) {
  70. int i;
  71. for ( i = 0; i < winding.numPoints; i++ ) {
  72. p[i] = winding.p[i];
  73. }
  74. numPoints = winding.numPoints;
  75. return *this;
  76. }
  77. ID_INLINE const idVec2 &idWinding2D::operator[]( const int index ) const {
  78. return p[ index ];
  79. }
  80. ID_INLINE idVec2 &idWinding2D::operator[]( const int index ) {
  81. return p[ index ];
  82. }
  83. ID_INLINE void idWinding2D::Clear() {
  84. numPoints = 0;
  85. }
  86. ID_INLINE void idWinding2D::AddPoint( const idVec2 &point ) {
  87. p[numPoints++] = point;
  88. }
  89. ID_INLINE int idWinding2D::GetNumPoints() const {
  90. return numPoints;
  91. }
  92. ID_INLINE idVec3 idWinding2D::Plane2DFromPoints( const idVec2 &start, const idVec2 &end, const bool normalize ) {
  93. idVec3 plane;
  94. plane.x = start.y - end.y;
  95. plane.y = end.x - start.x;
  96. if ( normalize ) {
  97. plane.ToVec2().Normalize();
  98. }
  99. plane.z = - ( start.x * plane.x + start.y * plane.y );
  100. return plane;
  101. }
  102. ID_INLINE idVec3 idWinding2D::Plane2DFromVecs( const idVec2 &start, const idVec2 &dir, const bool normalize ) {
  103. idVec3 plane;
  104. plane.x = -dir.y;
  105. plane.y = dir.x;
  106. if ( normalize ) {
  107. plane.ToVec2().Normalize();
  108. }
  109. plane.z = - ( start.x * plane.x + start.y * plane.y );
  110. return plane;
  111. }
  112. ID_INLINE bool idWinding2D::Plane2DIntersection( const idVec3 &plane1, const idVec3 &plane2, idVec2 &point ) {
  113. float n00, n01, n11, det, invDet, f0, f1;
  114. n00 = plane1.x * plane1.x + plane1.y * plane1.y;
  115. n01 = plane1.x * plane2.x + plane1.y * plane2.y;
  116. n11 = plane2.x * plane2.x + plane2.y * plane2.y;
  117. det = n00 * n11 - n01 * n01;
  118. if ( idMath::Fabs(det) < 1e-6f ) {
  119. return false;
  120. }
  121. invDet = 1.0f / det;
  122. f0 = ( n01 * plane2.z - n11 * plane1.z ) * invDet;
  123. f1 = ( n01 * plane1.z - n00 * plane2.z ) * invDet;
  124. point.x = f0 * plane1.x + f1 * plane2.x;
  125. point.y = f0 * plane1.y + f1 * plane2.y;
  126. return true;
  127. }
  128. #endif /* !__WINDING2D_H__ */