BSP_internal.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_BSP_INTERNAL_H
  13. #define SE_INCL_BSP_INTERNAL_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. extern FLOAT mth_fCSGEpsilon;
  18. /*
  19. * Type used to identify BSP-node locations
  20. */
  21. enum BSPNodeLocation {
  22. BNL_ILLEGAL=0, // for illegal value
  23. BNL_INSIDE, // inside leaf node
  24. BNL_OUTSIDE, // outside leaf node
  25. BNL_BRANCH, // branch node, unspecified location
  26. };
  27. /*
  28. * Template class for BSP vertex
  29. */
  30. template<class Type, int iDimensions>
  31. class BSPVertex : public Vector<Type, iDimensions> {
  32. public:
  33. /* Default constructor. */
  34. inline BSPVertex(void) {};
  35. /* Assignment operator with coordinates only. */
  36. inline BSPVertex<Type, iDimensions> &operator=(const Vector<Type, iDimensions> &vCoordinates);
  37. };
  38. /*
  39. * Template class for BSP vertex container
  40. */
  41. template<class Type, int iDimensions>
  42. class BSPVertexContainer {
  43. public:
  44. INDEX bvc_iMaxAxis; // index of largest axis of direction
  45. Type bvc_tMaxAxisSign; // sign of largest axis of direction
  46. CStaticStackArray<BSPVertex<Type, iDimensions> > bvc_aVertices; // array of vertices
  47. public:
  48. Vector<Type, iDimensions> bvc_vDirection; // direction of the split line
  49. /* Default constructor. */
  50. BSPVertexContainer(void);
  51. /* Initialize for a direction. */
  52. void Initialize(const Vector<Type, iDimensions> &vDirection);
  53. /* Uninitialize. */
  54. void Uninitialize(void);
  55. /* Check if this container is in an unusable state (polygon coplanar with the splitter).*/
  56. inline BOOL IsPlannar(void) { return bvc_iMaxAxis==0; };
  57. /* Add a new vertex. */
  58. inline void AddVertex(const Vector<Type, iDimensions> &vPoint);
  59. /* Sort vertices in this container along the largest axis of container direction. */
  60. void Sort(void);
  61. /* Elliminate paired vertices. */
  62. void ElliminatePairedVertices(void);
  63. /* Create edges from vertices in one container -- must be sorted before. */
  64. void CreateEdges(CDynamicArray<BSPEdge<Type, iDimensions> > &abedAll, ULONG ulEdgeTag);
  65. };
  66. /*
  67. * Template class for BSP edge
  68. */
  69. template<class Type, int iDimensions>
  70. class BSPEdge {
  71. public:
  72. Vector<Type, iDimensions> bed_vVertex0; // edge vertices
  73. Vector<Type, iDimensions> bed_vVertex1;
  74. ULONG bed_ulEdgeTag; // tags for BSPs with tagged edges/planes
  75. /* Default constructor. */
  76. inline BSPEdge(void) {};
  77. /* Constructor with two vectors. */
  78. inline BSPEdge(const Vector<Type, iDimensions> &vVertex0, const Vector<Type, iDimensions> &vVertex1, ULONG ulTag);
  79. /* Clear the object. */
  80. inline void Clear(void) {};
  81. // remove all edges marked for removal
  82. static void RemoveMarkedBSPEdges(CDynamicArray<BSPEdge<Type, iDimensions> > &abed);
  83. // optimize a polygon made out of BSP edges using tag information
  84. static void OptimizeBSPEdges(CDynamicArray<BSPEdge<Type, iDimensions> > &abed);
  85. };
  86. /*
  87. * Template class for polygons used in creating BSP-trees
  88. */
  89. template<class Type, int iDimensions>
  90. class BSPPolygon : public Plane<Type, iDimensions> {
  91. public:
  92. CDynamicArray<BSPEdge<Type, iDimensions> > bpo_abedPolygonEdges; // array of edges in the polygon
  93. ULONG bpo_ulPlaneTag; // tags for BSPs with tagged planes (-1 for no tag)
  94. /* Add an edge to the polygon. */
  95. inline void AddEdge(const Vector<Type, iDimensions> &vPoint0, const Vector<Type, iDimensions> &vPoint1, ULONG ulTag);
  96. /* Default constructor. */
  97. inline BSPPolygon(void) : bpo_ulPlaneTag(-1) {};
  98. /* Constructor with array of edges and plane. */
  99. inline BSPPolygon(
  100. Plane<Type, iDimensions> &plPlane, CDynamicArray<BSPEdge<Type, iDimensions> > abedPolygonEdges, ULONG ulPlaneTag)
  101. : Plane<Type, iDimensions>(plPlane)
  102. , bpo_abedPolygonEdges(abedPolygonEdges)
  103. , bpo_ulPlaneTag(ulPlaneTag)
  104. {};
  105. /* Clear the object. */
  106. inline void Clear(void) {bpo_abedPolygonEdges.Clear();};
  107. };
  108. template<class Type, int iDimensions>
  109. class BSPLine {
  110. public:
  111. Type bl_tMin;
  112. Type bl_tMax;
  113. };
  114. /*
  115. * Template class for BSP-tree node of arbitrary dimensions and arbitrary type of members
  116. */
  117. template<class Type, int iDimensions>
  118. class BSPNode : public Plane<Type, iDimensions> { // split plane
  119. public:
  120. enum BSPNodeLocation bn_bnlLocation; // location of bsp node
  121. BSPNode<Type, iDimensions> *bn_pbnFront; // pointer to child node in front of split plane
  122. BSPNode<Type, iDimensions> *bn_pbnBack; // pointer to child node behind split plane
  123. ULONG bn_ulPlaneTag; // tags for BSPs with tagged planes (-1 for no tag)
  124. public:
  125. /* Defualt constructor (for arrays only). */
  126. inline BSPNode(void) {};
  127. /* Constructor for a leaf node. */
  128. inline BSPNode(enum BSPNodeLocation bnl);
  129. /* Constructor for a branch node. */
  130. inline BSPNode(const Plane<Type, iDimensions> &plSplitPlane, ULONG ulPlaneTag,
  131. BSPNode<Type, iDimensions> &bnFront, BSPNode<Type, iDimensions> &bnBack);
  132. /* Constructor for cloning a bsp (sub)tree. */
  133. BSPNode(BSPNode<Type, iDimensions> &bnRoot);
  134. /* Recursive destructor. */
  135. void DeleteBSPNodeRecursively(void);
  136. // find minimum/maximum parameters of points on a line that are inside - recursive
  137. void FindLineMinMax(BSPLine<Type, iDimensions> &bl,
  138. const Vector<Type, iDimensions> &v0,
  139. const Vector<Type, iDimensions> &v1,
  140. Type t0, Type t1);
  141. /* Test if a sphere is inside, outside, or intersecting. (Just a trivial rejection test) */
  142. FLOAT TestSphere(const Vector<Type, iDimensions> &vSphereCenter, Type tSphereRadius) const;
  143. /* Test if a box is inside, outside, or intersecting. (Just a trivial rejection test) */
  144. FLOAT TestBox(const OBBox<Type> &box) const;
  145. };
  146. /*
  147. * Template class that performs polygon cuts using BSP-tree
  148. */
  149. template<class Type, int iDimensions>
  150. class BSPCutter {
  151. public:
  152. /* Split an edge with a plane. */
  153. static inline void SplitEdge(const Vector<Type, iDimensions> &vPoint0, const Vector<Type, iDimensions> &vPoint1, ULONG ulEdgeTag,
  154. const Plane<Type, iDimensions> &plSplitPlane,
  155. BSPPolygon<Type, iDimensions> &abedFront, BSPPolygon<Type, iDimensions> &abedBack,
  156. BSPVertexContainer<Type, iDimensions> &bvcFront, BSPVertexContainer<Type, iDimensions> &bvcBack);
  157. /* Cut a polygon with a BSP tree. */
  158. void CutPolygon(BSPPolygon<Type, iDimensions> &bpoPolygon, BSPNode<Type, iDimensions> &bn);
  159. public:
  160. CDynamicArray<BSPEdge<Type, iDimensions> > bc_abedInside; // edges of inside part of polygon
  161. CDynamicArray<BSPEdge<Type, iDimensions> > bc_abedOutside; // edges of outside part of polygon
  162. CDynamicArray<BSPEdge<Type, iDimensions> > bc_abedBorderInside; // edges of border part of polygon facing inwards
  163. CDynamicArray<BSPEdge<Type, iDimensions> > bc_abedBorderOutside;// edges of border part of polygon facing outwards
  164. /* Split a polygon with a plane. */
  165. static inline BOOL SplitPolygon(BSPPolygon<Type, iDimensions> &bpoPolygon, const Plane<Type, iDimensions> &plPlane, ULONG ulPlaneTag,
  166. BSPPolygon<Type, iDimensions> &bpoFront, BSPPolygon<Type, iDimensions> &bpoBack);
  167. /* Constructor for splitting a polygon with a BSP tree. */
  168. BSPCutter(BSPPolygon<Type, iDimensions> &bpoPolygon, BSPNode<Type, iDimensions> &bnRoot);
  169. /* Destructor. */
  170. ~BSPCutter(void);
  171. };
  172. #endif /* include-once check. */