1
0

CCOBB.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "3d/CCOBB.h"
  21. NS_CC_BEGIN
  22. #define ROTATE(a,i,j,k,l) g=a.m[i + 4 * j]; h=a.m[k + 4 * l]; a.m[i + 4 * j]=(float)(g-s*(h+g*tau)); a.m[k + 4 * l]=(float)(h+s*(g-h*tau));
  23. static Mat4 _getConvarianceMatrix(const Vec3* vertPos, int vertCount)
  24. {
  25. int i;
  26. Mat4 Cov;
  27. double S1[3];
  28. double S2[3][3];
  29. S1[0] = S1[1] = S1[2] = 0.0;
  30. S2[0][0] = S2[1][0] = S2[2][0] = 0.0;
  31. S2[0][1] = S2[1][1] = S2[2][1] = 0.0;
  32. S2[0][2] = S2[1][2] = S2[2][2] = 0.0;
  33. // get center of mass
  34. for(i=0; i<vertCount; i++)
  35. {
  36. S1[0] += vertPos[i].x;
  37. S1[1] += vertPos[i].y;
  38. S1[2] += vertPos[i].z;
  39. S2[0][0] += vertPos[i].x * vertPos[i].x;
  40. S2[1][1] += vertPos[i].y * vertPos[i].y;
  41. S2[2][2] += vertPos[i].z * vertPos[i].z;
  42. S2[0][1] += vertPos[i].x * vertPos[i].y;
  43. S2[0][2] += vertPos[i].x * vertPos[i].z;
  44. S2[1][2] += vertPos[i].y * vertPos[i].z;
  45. }
  46. float n = (float)vertCount;
  47. // now get covariances
  48. Cov.m[0] = (float)(S2[0][0] - S1[0]*S1[0] / n) / n;
  49. Cov.m[5] = (float)(S2[1][1] - S1[1]*S1[1] / n) / n;
  50. Cov.m[10] = (float)(S2[2][2] - S1[2]*S1[2] / n) / n;
  51. Cov.m[4] = (float)(S2[0][1] - S1[0]*S1[1] / n) / n;
  52. Cov.m[9] = (float)(S2[1][2] - S1[1]*S1[2] / n) / n;
  53. Cov.m[8] = (float)(S2[0][2] - S1[0]*S1[2] / n) / n;
  54. Cov.m[1] = Cov.m[4];
  55. Cov.m[2] = Cov.m[8];
  56. Cov.m[6] = Cov.m[9];
  57. return Cov;
  58. }
  59. static float& _getElement( Vec3& point, int index)
  60. {
  61. if (index == 0)
  62. return point.x;
  63. if (index == 1)
  64. return point.y;
  65. if (index == 2)
  66. return point.z;
  67. CC_ASSERT(0);
  68. return point.x;
  69. }
  70. static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a)
  71. {
  72. int n = 3;
  73. int j,iq,ip,i;
  74. double tresh, theta, tau, t, sm, s, h, g, c;
  75. int nrot;
  76. Vec3 b;
  77. Vec3 z;
  78. Mat4 v;
  79. Vec3 d;
  80. v = Mat4::IDENTITY;
  81. for(ip = 0; ip < n; ip++)
  82. {
  83. _getElement(b, ip) = a.m[ip + 4 * ip];
  84. _getElement(d, ip) = a.m[ip + 4 * ip];
  85. _getElement(z, ip) = 0.0;
  86. }
  87. nrot = 0;
  88. for(i = 0; i < 50; i++)
  89. {
  90. sm = 0.0;
  91. for(ip = 0; ip < n; ip++) for(iq = ip+1; iq < n; iq++) sm += fabs(a.m[ip + 4 * iq]);
  92. if( fabs(sm) < FLT_EPSILON )
  93. {
  94. v.transpose();
  95. *vout = v;
  96. *dout = d;
  97. return;
  98. }
  99. if (i < 3)
  100. tresh = 0.2 * sm / (n*n);
  101. else
  102. tresh = 0.0;
  103. for(ip = 0; ip < n; ip++)
  104. {
  105. for(iq = ip + 1; iq < n; iq++)
  106. {
  107. g = 100.0 * fabs(a.m[ip + iq * 4]);
  108. float dmip = _getElement(d, ip);
  109. float dmiq = _getElement(d, iq);
  110. if( i>3 && fabs(dmip) + g == fabs(dmip) && fabs(dmiq) + g == fabs(dmiq) )
  111. {
  112. a.m[ip + 4 * iq] = 0.0;
  113. }
  114. else if (fabs(a.m[ip + 4 * iq]) > tresh)
  115. {
  116. h = dmiq - dmip;
  117. if (fabs(h) + g == fabs(h))
  118. {
  119. t=(a.m[ip + 4 * iq])/h;
  120. }
  121. else
  122. {
  123. theta = 0.5 * h / (a.m[ip + 4 * iq]);
  124. t=1.0 / (fabs(theta) + sqrt(1.0 + theta * theta));
  125. if (theta < 0.0) t = -t;
  126. }
  127. c = 1.0 / sqrt(1+t*t);
  128. s = t*c;
  129. tau = s / (1.0+c);
  130. h = t * a.m[ip + 4 * iq];
  131. _getElement(z, ip) -= (float)h;
  132. _getElement(z, iq) += (float)h;
  133. _getElement(d, ip) -= (float)h;
  134. _getElement(d, iq) += (float)h;
  135. a.m[ip + 4 * iq]=0.0;
  136. for(j = 0; j < ip; j++) { ROTATE(a,j,ip,j,iq); }
  137. for(j = ip + 1; j < iq; j++) { ROTATE(a,ip,j,j,iq); }
  138. for(j = iq + 1; j < n; j++) { ROTATE(a,ip,j,iq,j); }
  139. for(j = 0; j < n; j++) { ROTATE(v,j,ip,j,iq); }
  140. nrot++;
  141. }
  142. }
  143. }
  144. for(ip = 0; ip < n; ip++)
  145. {
  146. _getElement(b, ip) += _getElement(z, ip);
  147. _getElement(d, ip) = _getElement(b, ip);
  148. _getElement(z, ip) = 0.0f;
  149. }
  150. }
  151. v.transpose();
  152. *vout = v;
  153. *dout = d;
  154. return;
  155. }
  156. static Mat4 _getOBBOrientation(const Vec3* vertPos, int num)
  157. {
  158. Mat4 Cov;
  159. if (num <= 0)
  160. return Mat4::IDENTITY;
  161. Cov = _getConvarianceMatrix(vertPos, num);
  162. // now get eigenvectors
  163. Mat4 Evecs;
  164. Vec3 Evals;
  165. _getEigenVectors(&Evecs, &Evals, Cov);
  166. Evecs.transpose();
  167. return Evecs;
  168. }
  169. OBB::OBB()
  170. {
  171. reset();
  172. }
  173. OBB::OBB(const AABB& aabb)
  174. {
  175. reset();
  176. _center = (aabb._min + aabb._max);
  177. _center.scale(0.5f);
  178. _xAxis.set(1.0f, 0.0f, 0.0f);
  179. _yAxis.set(0.0f, 1.0f, 0.0f);
  180. _zAxis.set(0.0f, 0.0f, 1.0f);
  181. _extents = aabb._max - aabb._min;
  182. _extents.scale(0.5f);
  183. computeExtAxis();
  184. }
  185. OBB::OBB(const Vec3* verts, int num)
  186. {
  187. if (!verts) return;
  188. reset();
  189. Mat4 matTransform = _getOBBOrientation(verts, num);
  190. // For matTransform is orthogonal, so the inverse matrix is just rotate it;
  191. matTransform.transpose();
  192. Vec3 vecMax = matTransform * Vec3(verts[0].x, verts[0].y, verts[0].z);
  193. Vec3 vecMin = vecMax;
  194. for (int i = 1; i < num; i++)
  195. {
  196. Vec3 vect = matTransform * Vec3(verts[i].x, verts[i].y, verts[i].z);
  197. vecMax.x = vecMax.x > vect.x ? vecMax.x : vect.x;
  198. vecMax.y = vecMax.y > vect.y ? vecMax.y : vect.y;
  199. vecMax.z = vecMax.z > vect.z ? vecMax.z : vect.z;
  200. vecMin.x = vecMin.x < vect.x ? vecMin.x : vect.x;
  201. vecMin.y = vecMin.y < vect.y ? vecMin.y : vect.y;
  202. vecMin.z = vecMin.z < vect.z ? vecMin.z : vect.z;
  203. }
  204. matTransform.transpose();
  205. _xAxis.set(matTransform.m[0], matTransform.m[1], matTransform.m[2]);
  206. _yAxis.set(matTransform.m[4], matTransform.m[5], matTransform.m[6]);
  207. _zAxis.set(matTransform.m[8], matTransform.m[9], matTransform.m[10]);
  208. _center = 0.5f * (vecMax + vecMin);
  209. _center *= matTransform;
  210. _xAxis.normalize();
  211. _yAxis.normalize();
  212. _zAxis.normalize();
  213. _extents = 0.5f * (vecMax - vecMin);
  214. computeExtAxis();
  215. }
  216. bool OBB::containPoint(const Vec3& point) const
  217. {
  218. Vec3 vd = point - _center;
  219. float d = vd.dot(_xAxis);
  220. if (d > _extents.x || d < -_extents.x)
  221. return false;
  222. d = vd.dot(_yAxis);
  223. if (d > _extents.y || d < -_extents.y)
  224. return false;
  225. d = vd.dot(_zAxis);
  226. if (d > _extents.z || d < -_extents.z)
  227. return false;
  228. return true;
  229. }
  230. void OBB::set(const Vec3& center, const Vec3& xAxis, const Vec3& yAxis, const Vec3& zAxis, const Vec3& extents)
  231. {
  232. _center = center;
  233. _xAxis = xAxis;
  234. _yAxis = yAxis;
  235. _zAxis = zAxis;
  236. _extents = extents;
  237. }
  238. void OBB::reset()
  239. {
  240. memset(this, 0, sizeof(OBB));
  241. }
  242. void OBB::getCorners(Vec3* verts) const
  243. {
  244. verts[0] = _center - _extentX + _extentY + _extentZ; // left top front
  245. verts[1] = _center - _extentX - _extentY + _extentZ; // left bottom front
  246. verts[2] = _center + _extentX - _extentY + _extentZ; // right bottom front
  247. verts[3] = _center + _extentX + _extentY + _extentZ; // right top front
  248. verts[4] = _center + _extentX + _extentY - _extentZ; // right top back
  249. verts[5] = _center + _extentX - _extentY - _extentZ; // right bottom back
  250. verts[6] = _center - _extentX - _extentY - _extentZ; // left bottom back
  251. verts[7] = _center - _extentX + _extentY - _extentZ; // left top back
  252. }
  253. float OBB::projectPoint(const Vec3& point, const Vec3& axis)const
  254. {
  255. float dot = axis.dot(point);
  256. float ret = dot * point.length();
  257. return ret;
  258. }
  259. void OBB::getInterval(const OBB& box, const Vec3& axis, float &min, float &max)const
  260. {
  261. Vec3 corners[8];
  262. box.getCorners(corners);
  263. float value;
  264. min = max = projectPoint(axis, corners[0]);
  265. for(int i = 1; i < 8; i++)
  266. {
  267. value = projectPoint(axis, corners[i]);
  268. min = MIN(min, value);
  269. max = MAX(max, value);
  270. }
  271. }
  272. Vec3 OBB::getEdgeDirection(int index)const
  273. {
  274. Vec3 corners[8];
  275. getCorners(corners);
  276. Vec3 tmpLine;
  277. switch(index)
  278. {
  279. case 0:// edge with x axis
  280. tmpLine = corners[5] - corners[6];
  281. tmpLine.normalize();
  282. break;
  283. case 1:// edge with y axis
  284. tmpLine = corners[7] - corners[6];
  285. tmpLine.normalize();
  286. break;
  287. case 2:// edge with z axis
  288. tmpLine = corners[1] - corners[6];
  289. tmpLine.normalize();
  290. break;
  291. default:
  292. CCASSERT(0, "Invalid index!");
  293. break;
  294. }
  295. return tmpLine;
  296. }
  297. Vec3 OBB::getFaceDirection(int index) const
  298. {
  299. Vec3 corners[8];
  300. getCorners(corners);
  301. Vec3 faceDirection, v0, v1;
  302. switch(index)
  303. {
  304. case 0:// front and back
  305. v0 = corners[2] - corners[1];
  306. v1 = corners[0] - corners[1];
  307. Vec3::cross(v0, v1, &faceDirection);
  308. faceDirection.normalize();
  309. break;
  310. case 1:// left and right
  311. v0 = corners[5] - corners[2];
  312. v1 = corners[3] - corners[2];
  313. Vec3::cross(v0, v1, &faceDirection);
  314. faceDirection.normalize();
  315. break;
  316. case 2:// top and bottom
  317. v0 = corners[1] - corners[2];
  318. v1 = corners[5] - corners[2];
  319. Vec3::cross(v0, v1, &faceDirection);
  320. faceDirection.normalize();
  321. break;
  322. default:
  323. CCASSERT(0, "Invalid index!");
  324. break;
  325. }
  326. return faceDirection;
  327. }
  328. bool OBB::intersects(const OBB& box) const
  329. {
  330. float min1, max1, min2, max2;
  331. for (int i = 0; i < 3; i++)
  332. {
  333. getInterval(*this, getFaceDirection(i), min1, max1);
  334. getInterval(box, getFaceDirection(i), min2, max2);
  335. if (max1 < min2 || max2 < min1) return false;
  336. }
  337. for (int i = 0; i < 3; i++)
  338. {
  339. getInterval(*this, box.getFaceDirection(i), min1, max1);
  340. getInterval(box, box.getFaceDirection(i), min2, max2);
  341. if (max1 < min2 || max2 < min1) return false;
  342. }
  343. for (int i = 0; i < 3; i++)
  344. {
  345. for (int j = 0; j < 3; j++)
  346. {
  347. Vec3 axis;
  348. Vec3::cross(getEdgeDirection(i), box.getEdgeDirection(j), &axis);
  349. getInterval(*this, axis, min1, max1);
  350. getInterval(box, axis, min2, max2);
  351. if (max1 < min2 || max2 < min1) return false;
  352. }
  353. }
  354. return true;
  355. }
  356. void OBB::transform(const Mat4& mat)
  357. {
  358. Vec4 newcenter = mat * Vec4(_center.x, _center.y, _center.z, 1.0f);// center;
  359. _center.x = newcenter.x;
  360. _center.y = newcenter.y;
  361. _center.z = newcenter.z;
  362. _xAxis = mat * _xAxis;
  363. _yAxis = mat * _yAxis;
  364. _zAxis = mat * _zAxis;
  365. _xAxis.normalize();
  366. _yAxis.normalize();
  367. _zAxis.normalize();
  368. Vec3 scale, trans;
  369. Quaternion quat;
  370. mat.decompose(&scale, &quat, &trans);
  371. _extents.x *= scale.x;
  372. _extents.y *= scale.y;
  373. _extents.z *= scale.z;
  374. computeExtAxis();
  375. }
  376. NS_CC_END