bounds.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bx/rng.h>
  6. #include "bounds.h"
  7. #include "fpumath.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. 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] = fminf(xx, min[0]);
  63. min[1] = fminf(yy, min[1]);
  64. min[2] = fminf(zz, min[2]);
  65. max[0] = fmaxf(xx, max[0]);
  66. max[1] = fmaxf(yy, max[1]);
  67. max[2] = fmaxf(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. 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. 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] = fminf(xx, min[0]);
  94. min[1] = fminf(yy, min[1]);
  95. min[2] = fminf(zz, min[2]);
  96. max[0] = fmaxf(xx, max[0]);
  97. max[1] = fmaxf(yy, max[1]);
  98. max[2] = fmaxf(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 calcObb(Obb& _obb, const void* _vertices, uint32_t _numVertices, uint32_t _stride, uint32_t _steps)
  108. {
  109. Aabb aabb;
  110. calcAabb(aabb, _vertices, _numVertices, _stride);
  111. float minArea = calcAreaAabb(aabb);
  112. Obb best;
  113. aabbToObb(best, aabb);
  114. float angleStep = float(M_PI_2/_steps);
  115. float ax = 0.0f;
  116. float mtx[16];
  117. for (uint32_t ii = 0; ii < _steps; ++ii)
  118. {
  119. float ay = 0.0f;
  120. for (uint32_t jj = 0; jj < _steps; ++jj)
  121. {
  122. float az = 0.0f;
  123. for (uint32_t kk = 0; kk < _steps; ++kk)
  124. {
  125. mtxRotateXYZ(mtx, ax, ay, az);
  126. float mtxT[16];
  127. mtxTranspose(mtxT, mtx);
  128. calcAabb(aabb, mtxT, _vertices, _numVertices, _stride);
  129. float area = calcAreaAabb(aabb);
  130. if (area < minArea)
  131. {
  132. minArea = area;
  133. aabbTransformToObb(best, aabb, mtx);
  134. }
  135. az += angleStep;
  136. }
  137. ay += angleStep;
  138. }
  139. ax += angleStep;
  140. }
  141. memcpy(&_obb, &best, sizeof(Obb) );
  142. }
  143. void calcMaxBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
  144. {
  145. Aabb aabb;
  146. calcAabb(aabb, _vertices, _numVertices, _stride);
  147. float center[3];
  148. center[0] = (aabb.m_min[0] + aabb.m_max[0]) * 0.5f;
  149. center[1] = (aabb.m_min[1] + aabb.m_max[1]) * 0.5f;
  150. center[2] = (aabb.m_min[2] + aabb.m_max[2]) * 0.5f;
  151. float maxDistSq = 0.0f;
  152. uint8_t* vertex = (uint8_t*)_vertices;
  153. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  154. {
  155. float* position = (float*)vertex;
  156. vertex += _stride;
  157. float xx = position[0] - center[0];
  158. float yy = position[1] - center[1];
  159. float zz = position[2] - center[2];
  160. float distSq = xx*xx + yy*yy + zz*zz;
  161. maxDistSq = fmaxf(distSq, maxDistSq);
  162. }
  163. _sphere.m_center[0] = center[0];
  164. _sphere.m_center[1] = center[1];
  165. _sphere.m_center[2] = center[2];
  166. _sphere.m_radius = sqrtf(maxDistSq);
  167. }
  168. void calcMinBoundingSphere(Sphere& _sphere, const void* _vertices, uint32_t _numVertices, uint32_t _stride, float _step)
  169. {
  170. bx::RngMwc rng;
  171. uint8_t* vertex = (uint8_t*)_vertices;
  172. float center[3];
  173. float* position = (float*)&vertex[0];
  174. center[0] = position[0];
  175. center[1] = position[1];
  176. center[2] = position[2];
  177. position = (float*)&vertex[1*_stride];
  178. center[0] += position[0];
  179. center[1] += position[1];
  180. center[2] += position[2];
  181. center[0] *= 0.5f;
  182. center[1] *= 0.5f;
  183. center[2] *= 0.5f;
  184. float xx = position[0] - center[0];
  185. float yy = position[1] - center[1];
  186. float zz = position[2] - center[2];
  187. float maxDistSq = xx*xx + yy*yy + zz*zz;
  188. float radiusStep = _step * 0.37f;
  189. bool done;
  190. do
  191. {
  192. done = true;
  193. for (uint32_t ii = 0, index = rng.gen()%_numVertices; ii < _numVertices; ++ii, index = (index + 1)%_numVertices)
  194. {
  195. position = (float*)&vertex[index*_stride];
  196. float xx = position[0] - center[0];
  197. float yy = position[1] - center[1];
  198. float zz = position[2] - center[2];
  199. float distSq = xx*xx + yy*yy + zz*zz;
  200. if (distSq > maxDistSq)
  201. {
  202. done = false;
  203. center[0] += xx * radiusStep;
  204. center[1] += yy * radiusStep;
  205. center[2] += zz * radiusStep;
  206. maxDistSq = flerp(maxDistSq, distSq, _step);
  207. break;
  208. }
  209. }
  210. } while (!done);
  211. _sphere.m_center[0] = center[0];
  212. _sphere.m_center[1] = center[1];
  213. _sphere.m_center[2] = center[2];
  214. _sphere.m_radius = sqrtf(maxDistSq);
  215. }