OPC_TreeBuilders.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains code for tree builders.
  11. * \file OPC_TreeBuilders.cpp
  12. * \author Pierre Terdiman
  13. * \date March, 20, 2001
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**
  18. * A builder for AABB-trees of vertices.
  19. *
  20. * \class AABBTreeOfVerticesBuilder
  21. * \author Pierre Terdiman
  22. * \version 1.3
  23. * \date March, 20, 2001
  24. */
  25. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. /**
  28. * A builder for AABB-trees of AABBs.
  29. *
  30. * \class AABBTreeOfAABBsBuilder
  31. * \author Pierre Terdiman
  32. * \version 1.3
  33. * \date March, 20, 2001
  34. */
  35. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  37. /**
  38. * A builder for AABB-trees of triangles.
  39. *
  40. * \class AABBTreeOfTrianglesBuilder
  41. * \author Pierre Terdiman
  42. * \version 1.3
  43. * \date March, 20, 2001
  44. */
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. // Precompiled Header
  48. #include "Stdafx.h"
  49. using namespace Opcode;
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51. /**
  52. * Computes the AABB of a set of primitives.
  53. * \param primitives [in] list of indices of primitives
  54. * \param nb_prims [in] number of indices
  55. * \param global_box [out] global AABB enclosing the set of input primitives
  56. * \return true if success
  57. */
  58. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  59. bool AABBTreeOfAABBsBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  60. {
  61. // Checkings
  62. if(!primitives || !nb_prims) return false;
  63. // Initialize global box
  64. global_box = mAABBArray[primitives[0]];
  65. // Loop through boxes
  66. for(udword i=1;i<nb_prims;i++)
  67. {
  68. // Update global box
  69. global_box.Add(mAABBArray[primitives[i]]);
  70. }
  71. return true;
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /**
  75. * Computes the splitting value along a given axis for a given primitive.
  76. * \param index [in] index of the primitive to split
  77. * \param axis [in] axis index (0,1,2)
  78. * \return splitting value
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. float AABBTreeOfAABBsBuilder::GetSplittingValue(udword index, udword axis) const
  82. {
  83. // For an AABB, the splitting value is the middle of the given axis,
  84. // i.e. the corresponding component of the center point
  85. return mAABBArray[index].GetCenter(axis);
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. /**
  89. * Computes the splitting value along a given axis for a given primitive.
  90. * \param index [in] index of the primitive to split
  91. * \return splitting value
  92. */
  93. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  94. Point AABBTreeOfAABBsBuilder::GetSplittingValues(udword index) const
  95. {
  96. // For an AABB, the splitting value is the middle of the given axis,
  97. // i.e. the corresponding component of the center point
  98. Point p;
  99. mAABBArray[index].GetCenter(p);
  100. return p;
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103. /**
  104. * Computes the AABB of a set of primitives.
  105. * \param primitives [in] list of indices of primitives
  106. * \param nb_prims [in] number of indices
  107. * \param global_box [out] global AABB enclosing the set of input primitives
  108. * \return true if success
  109. */
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. bool AABBTreeOfTrianglesBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  112. {
  113. // Checkings
  114. if(!primitives || !nb_prims) return false;
  115. // Initialize global box
  116. Point Min(MAX_FLOAT, MAX_FLOAT, MAX_FLOAT);
  117. Point Max(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);
  118. // Loop through triangles
  119. VertexPointers VP;
  120. ConversionArea VC;
  121. while(nb_prims--)
  122. {
  123. // Get current triangle-vertices
  124. mIMesh->GetTriangle(VP, *primitives++, VC);
  125. // Update global box
  126. Min.Min(*VP.Vertex[0]).Min(*VP.Vertex[1]).Min(*VP.Vertex[2]);
  127. Max.Max(*VP.Vertex[0]).Max(*VP.Vertex[1]).Max(*VP.Vertex[2]);
  128. }
  129. global_box.SetMinMax(Min, Max);
  130. return true;
  131. }
  132. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. /**
  134. * Computes the splitting value along a given axis for a given primitive.
  135. * \param index [in] index of the primitive to split
  136. * \return splitting value
  137. */
  138. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. Point AABBTreeOfTrianglesBuilder::GetSplittingValues(udword index) const
  140. {
  141. VertexPointers VP;
  142. ConversionArea VC;
  143. mIMesh->GetTriangle(VP, index, VC);
  144. return ( *VP.Vertex[0] + *VP.Vertex[1] + *VP.Vertex[2] ) * INV3;
  145. }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  147. /**
  148. * Computes the splitting value along a given axis for a given primitive.
  149. * \param index [in] index of the primitive to split
  150. * \param axis [in] axis index (0,1,2)
  151. * \return splitting value
  152. */
  153. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. float AABBTreeOfTrianglesBuilder::GetSplittingValue(udword index, udword axis) const
  155. {
  156. /* // Compute center of triangle
  157. Point Center;
  158. mTriList[index].Center(mVerts, Center);
  159. // Return value
  160. return Center[axis];*/
  161. // Compute correct component from center of triangle
  162. // return (mVerts[mTriList[index].mVRef[0]][axis]
  163. // +mVerts[mTriList[index].mVRef[1]][axis]
  164. // +mVerts[mTriList[index].mVRef[2]][axis])*INV3;
  165. VertexPointers VP;
  166. ConversionArea VC;
  167. mIMesh->GetTriangle(VP, index, VC);
  168. // Compute correct component from center of triangle
  169. return ((*VP.Vertex[0])[axis]
  170. +(*VP.Vertex[1])[axis]
  171. +(*VP.Vertex[2])[axis])*INV3;
  172. }
  173. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  174. /**
  175. * Computes the splitting value along a given axis for a given node.
  176. * \param primitives [in] list of indices of primitives
  177. * \param nb_prims [in] number of indices
  178. * \param global_box [in] global AABB enclosing the set of input primitives
  179. * \param axis [in] axis index (0,1,2)
  180. * \return splitting value
  181. */
  182. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183. float AABBTreeOfTrianglesBuilder::GetSplittingValue(const dTriIndex* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  184. {
  185. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  186. {
  187. // Loop through triangles
  188. float SplitValue = 0.0f;
  189. VertexPointers VP;
  190. ConversionArea VC;
  191. for(udword i=0;i<nb_prims;i++)
  192. {
  193. // Get current triangle-vertices
  194. mIMesh->GetTriangle(VP, primitives[i], VC);
  195. // Update split value
  196. SplitValue += (*VP.Vertex[0])[axis];
  197. SplitValue += (*VP.Vertex[1])[axis];
  198. SplitValue += (*VP.Vertex[2])[axis];
  199. }
  200. return SplitValue / float(nb_prims*3);
  201. }
  202. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  203. }
  204. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205. /**
  206. * Computes the AABB of a set of primitives.
  207. * \param primitives [in] list of indices of primitives
  208. * \param nb_prims [in] number of indices
  209. * \param global_box [out] global AABB enclosing the set of input primitives
  210. * \return true if success
  211. */
  212. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  213. bool AABBTreeOfVerticesBuilder::ComputeGlobalBox(const dTriIndex* primitives, udword nb_prims, AABB& global_box) const
  214. {
  215. // Checkings
  216. if(!primitives || !nb_prims) return false;
  217. // Initialize global box
  218. global_box.SetEmpty();
  219. // Loop through vertices
  220. for(udword i=0;i<nb_prims;i++)
  221. {
  222. // Update global box
  223. global_box.Extend(mVertexArray[primitives[i]]);
  224. }
  225. return true;
  226. }
  227. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  228. /**
  229. * Computes the splitting value along a given axis for a given primitive.
  230. * \param index [in] index of the primitive to split
  231. * \param axis [in] axis index (0,1,2)
  232. * \return splitting value
  233. */
  234. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. float AABBTreeOfVerticesBuilder::GetSplittingValue(udword index, udword axis) const
  236. {
  237. // For a vertex, the splitting value is simply the vertex coordinate.
  238. return mVertexArray[index][axis];
  239. }
  240. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  241. /**
  242. * Computes the splitting value along a given axis for a given primitive.
  243. * \param index [in] index of the primitive to split
  244. * \param axis [in] axis index (0,1,2)
  245. * \return splitting value
  246. */
  247. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  248. Point AABBTreeOfVerticesBuilder::GetSplittingValues(udword index) const
  249. {
  250. // For a vertex, the splitting value is simply the vertex coordinate.
  251. return mVertexArray[index];
  252. }
  253. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. /**
  255. * Computes the splitting value along a given axis for a given node.
  256. * \param primitives [in] list of indices of primitives
  257. * \param nb_prims [in] number of indices
  258. * \param global_box [in] global AABB enclosing the set of input primitives
  259. * \param axis [in] axis index (0,1,2)
  260. * \return splitting value
  261. */
  262. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  263. float AABBTreeOfVerticesBuilder::GetSplittingValue(const dTriIndex* primitives, udword nb_prims, const AABB& global_box, udword axis) const
  264. {
  265. if(mSettings.mRules&SPLIT_GEOM_CENTER)
  266. {
  267. // Loop through vertices
  268. float SplitValue = 0.0f;
  269. for(udword i=0;i<nb_prims;i++)
  270. {
  271. // Update split value
  272. SplitValue += mVertexArray[primitives[i]][axis];
  273. }
  274. return SplitValue / float(nb_prims);
  275. }
  276. else return AABBTreeBuilder::GetSplittingValue(primitives, nb_prims, global_box, axis);
  277. }