bounds.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/rng.h>
  6. #include <bx/fpumath.h>
  7. #include "bounds.h"
  8. void aabbToObb(Obb& _obb, const Aabb& _aabb)
  9. {
  10. memset(_obb.m_mtx, 0, sizeof(_obb.m_mtx) );
  11. _obb.m_mtx[ 0] = (_aabb.m_max[0] - _aabb.m_min[0]) * 0.5f;
  12. _obb.m_mtx[ 5] = (_aabb.m_max[1] - _aabb.m_min[1]) * 0.5f;
  13. _obb.m_mtx[10] = (_aabb.m_max[2] - _aabb.m_min[2]) * 0.5f;
  14. _obb.m_mtx[12] = (_aabb.m_min[0] + _aabb.m_max[0]) * 0.5f;
  15. _obb.m_mtx[13] = (_aabb.m_min[1] + _aabb.m_max[1]) * 0.5f;
  16. _obb.m_mtx[14] = (_aabb.m_min[2] + _aabb.m_max[2]) * 0.5f;
  17. _obb.m_mtx[15] = 1.0f;
  18. }
  19. void sphereToAabb(Aabb& _aabb, const Sphere& _sphere)
  20. {
  21. float xx = _sphere.m_center[0];
  22. float yy = _sphere.m_center[1];
  23. float zz = _sphere.m_center[2];
  24. float radius = _sphere.m_radius;
  25. _aabb.m_min[0] = xx - radius;
  26. _aabb.m_min[1] = yy - radius;
  27. _aabb.m_min[2] = zz - radius;
  28. _aabb.m_max[0] = xx + radius;
  29. _aabb.m_max[1] = yy + radius;
  30. _aabb.m_max[2] = zz + radius;
  31. }
  32. void aabbTransformToObb(Obb& _obb, const Aabb& _aabb, const float* _mtx)
  33. {
  34. aabbToObb(_obb, _aabb);
  35. float result[16];
  36. bx::mtxMul(result, _obb.m_mtx, _mtx);
  37. memcpy(_obb.m_mtx, result, sizeof(result) );
  38. }
  39. float calcAreaAabb(Aabb& _aabb)
  40. {
  41. float ww = _aabb.m_max[0] - _aabb.m_min[0];
  42. float hh = _aabb.m_max[1] - _aabb.m_min[1];
  43. float dd = _aabb.m_max[2] - _aabb.m_min[2];
  44. return 2.0f * (ww*hh + ww*dd + hh*dd);
  45. }
  46. void calcAabb(Aabb& _aabb, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  47. {
  48. float min[3], max[3];
  49. uint8_t* vertex = (uint8_t*)_vertices;
  50. float* position = (float*)vertex;
  51. min[0] = max[0] = position[0];
  52. min[1] = max[1] = position[1];
  53. min[2] = max[2] = position[2];
  54. vertex += _stride;
  55. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  56. {
  57. position = (float*)vertex;
  58. vertex += _stride;
  59. float xx = position[0];
  60. float yy = position[1];
  61. float zz = position[2];
  62. min[0] = bx::fmin(xx, min[0]);
  63. min[1] = bx::fmin(yy, min[1]);
  64. min[2] = bx::fmin(zz, min[2]);
  65. max[0] = bx::fmax(xx, max[0]);
  66. max[1] = bx::fmax(yy, max[1]);
  67. max[2] = bx::fmax(zz, max[2]);
  68. }
  69. _aabb.m_min[0] = min[0];
  70. _aabb.m_min[1] = min[1];
  71. _aabb.m_min[2] = min[2];
  72. _aabb.m_max[0] = max[0];
  73. _aabb.m_max[1] = max[1];
  74. _aabb.m_max[2] = max[2];
  75. }
  76. void calcAabb(Aabb& _aabb, const float* _mtx, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  77. {
  78. float min[3], max[3];
  79. uint8_t* vertex = (uint8_t*)_vertices;
  80. float position[3];
  81. bx::vec3MulMtx(position, (float*)vertex, _mtx);
  82. min[0] = max[0] = position[0];
  83. min[1] = max[1] = position[1];
  84. min[2] = max[2] = position[2];
  85. vertex += _stride;
  86. for (uint32_t ii = 1; ii < _numVertices; ++ii)
  87. {
  88. bx::vec3MulMtx(position, (float*)vertex, _mtx);
  89. vertex += _stride;
  90. float xx = position[0];
  91. float yy = position[1];
  92. float zz = position[2];
  93. min[0] = bx::fmin(xx, min[0]);
  94. min[1] = bx::fmin(yy, min[1]);
  95. min[2] = bx::fmin(zz, min[2]);
  96. max[0] = bx::fmax(xx, max[0]);
  97. max[1] = bx::fmax(yy, max[1]);
  98. max[2] = bx::fmax(zz, max[2]);
  99. }
  100. _aabb.m_min[0] = min[0];
  101. _aabb.m_min[1] = min[1];
  102. _aabb.m_min[2] = min[2];
  103. _aabb.m_max[0] = max[0];
  104. _aabb.m_max[1] = max[1];
  105. _aabb.m_max[2] = max[2];
  106. }
  107. void aabbExpand(Aabb& _aabb, float _factor)
  108. {
  109. _aabb.m_min[0] -= _factor;
  110. _aabb.m_min[1] -= _factor;
  111. _aabb.m_min[2] -= _factor;
  112. _aabb.m_max[0] += _factor;
  113. _aabb.m_max[1] += _factor;
  114. _aabb.m_max[2] += _factor;
  115. }
  116. uint32_t aabbOverlapTest(Aabb& _aabb0, Aabb& _aabb1)
  117. {
  118. const uint32_t ltMinX = _aabb0.m_max[0] < _aabb1.m_min[0];
  119. const uint32_t gtMaxX = _aabb0.m_min[0] > _aabb1.m_max[0];
  120. const uint32_t ltMinY = _aabb0.m_max[1] < _aabb1.m_min[1];
  121. const uint32_t gtMaxY = _aabb0.m_min[1] > _aabb1.m_max[1];
  122. const uint32_t ltMinZ = _aabb0.m_max[2] < _aabb1.m_min[2];
  123. const uint32_t gtMaxZ = _aabb0.m_min[2] > _aabb1.m_max[2];
  124. return 0
  125. | (ltMinX<<0)
  126. | (gtMaxX<<1)
  127. | (ltMinY<<2)
  128. | (gtMaxY<<3)
  129. | (ltMinZ<<4)
  130. | (gtMaxZ<<5)
  131. ;
  132. }
  133. void calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
  134. {
  135. Aabb aabb;
  136. calcAabb(aabb, _vertices, _numVertices, _stride);
  137. float minArea = calcAreaAabb(aabb);
  138. Obb best;
  139. aabbToObb(best, aabb);
  140. float angleStep = float(bx::piHalf/_steps);
  141. float ax = 0.0f;
  142. float mtx[16];
  143. for (uint32_t ii = 0; ii < _steps; ++ii)
  144. {
  145. float ay = 0.0f;
  146. for (uint32_t jj = 0; jj < _steps; ++jj)
  147. {
  148. float az = 0.0f;
  149. for (uint32_t kk = 0; kk < _steps; ++kk)
  150. {
  151. bx::mtxRotateXYZ(mtx, ax, ay, az);
  152. float mtxT[16];
  153. bx::mtxTranspose(mtxT, mtx);
  154. calcAabb(aabb, mtxT, _vertices, _numVertices, _stride);
  155. float area = calcAreaAabb(aabb);
  156. if (area < minArea)
  157. {
  158. minArea = area;
  159. aabbTransformToObb(best, aabb, mtx);
  160. }
  161. az += angleStep;
  162. }
  163. ay += angleStep;
  164. }
  165. ax += angleStep;
  166. }
  167. memcpy(&_obb, &best, sizeof(Obb) );
  168. }
  169. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  170. {
  171. Aabb aabb;
  172. calcAabb(aabb, _vertices, _numVertices, _stride);
  173. float center[3];
  174. center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f;
  175. center[1] = (aabb.m_min[1] + aabb.m_max[1]) * 0.5f;
  176. center[2] = (aabb.m_min[2] + aabb.m_max[2]) * 0.5f;
  177. float maxDistSq = 0.0f;
  178. uint8_t* vertex = (uint8_t*)_vertices;
  179. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  180. {
  181. float* position = (float*)vertex;
  182. vertex += _stride;
  183. float xx = position[0] - center[0];
  184. float yy = position[1] - center[1];
  185. float zz = position[2] - center[2];
  186. float distSq = xx*xx + yy*yy + zz*zz;
  187. maxDistSq = bx::fmax(distSq, maxDistSq);
  188. }
  189. _sphere.m_center[0] = center[0];
  190. _sphere.m_center[1] = center[1];
  191. _sphere.m_center[2] = center[2];
  192. _sphere.m_radius = sqrtf(maxDistSq);
  193. }
  194. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  195. {
  196. bx::RngMwc rng;
  197. uint8_t* vertex = (uint8_t*)_vertices;
  198. float center[3];
  199. float* position = (float*)&vertex[0];
  200. center[0] = position[0];
  201. center[1] = position[1];
  202. center[2] = position[2];
  203. position = (float*)&vertex[1*_stride];
  204. center[0] += position[0];
  205. center[1] += position[1];
  206. center[2] += position[2];
  207. center[0] *= 0.5f;
  208. center[1] *= 0.5f;
  209. center[2] *= 0.5f;
  210. float xx = position[0] - center[0];
  211. float yy = position[1] - center[1];
  212. float zz = position[2] - center[2];
  213. float maxDistSq = xx*xx + yy*yy + zz*zz;
  214. float radiusStep = _step * 0.37f;
  215. bool done;
  216. do
  217. {
  218. done = true;
  219. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  220. {
  221. position = (float*)&vertex[index*_stride];
  222. float xx = position[0] - center[0];
  223. float yy = position[1] - center[1];
  224. float zz = position[2] - center[2];
  225. float distSq = xx*xx + yy*yy + zz*zz;
  226. if (distSq > maxDistSq)
  227. {
  228. done = false;
  229. center[0] += xx * radiusStep;
  230. center[1] += yy * radiusStep;
  231. center[2] += zz * radiusStep;
  232. maxDistSq = bx::flerp(maxDistSq, distSq, _step);
  233. break;
  234. }
  235. }
  236. } while (!done);
  237. _sphere.m_center[0] = center[0];
  238. _sphere.m_center[1] = center[1];
  239. _sphere.m_center[2] = center[2];
  240. _sphere.m_radius = sqrtf(maxDistSq);
  241. }