bestfit.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <math.h>
  7. /*----------------------------------------------------------------------
  8. Copyright (c) 2004 Open Dynamics Framework Group
  9. www.physicstools.org
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  12. that the following conditions are met:
  13. Redistributions of source code must retain the above copyright notice, this list of conditions
  14. and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  19. be used to endorse or promote products derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  25. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. -----------------------------------------------------------------------*/
  28. // http://codesuppository.blogspot.com
  29. //
  30. // mailto: [email protected]
  31. //
  32. // http://www.amillionpixels.us
  33. //
  34. // Geometric Tools, Inc.
  35. // http://www.geometrictools.com
  36. // Copyright (c) 1998-2006. All Rights Reserved
  37. //
  38. // The Wild Magic Library (WM3) source code is supplied under the terms of
  39. // the license agreement
  40. // http://www.geometrictools.com/License/WildMagic3License.pdf
  41. // and may not be copied or disclosed except in accordance with the terms
  42. // of that agreement.
  43. #include "bestfit.h"
  44. namespace BestFit
  45. {
  46. class Vec3
  47. {
  48. public:
  49. Vec3(void) { };
  50. Vec3(float _x,float _y,float _z) { x = _x; y = _y; z = _z; };
  51. float dot(const Vec3 &v)
  52. {
  53. return x*v.x + y*v.y + z*v.z; // the dot product
  54. }
  55. float x;
  56. float y;
  57. float z;
  58. };
  59. class Eigen
  60. {
  61. public:
  62. void DecrSortEigenStuff(void)
  63. {
  64. Tridiagonal(); //diagonalize the matrix.
  65. QLAlgorithm(); //
  66. DecreasingSort();
  67. GuaranteeRotation();
  68. }
  69. void Tridiagonal(void)
  70. {
  71. float fM00 = mElement[0][0];
  72. float fM01 = mElement[0][1];
  73. float fM02 = mElement[0][2];
  74. float fM11 = mElement[1][1];
  75. float fM12 = mElement[1][2];
  76. float fM22 = mElement[2][2];
  77. m_afDiag[0] = fM00;
  78. m_afSubd[2] = 0;
  79. if (fM02 != (float)0.0)
  80. {
  81. float fLength = sqrtf(fM01*fM01+fM02*fM02);
  82. float fInvLength = ((float)1.0)/fLength;
  83. fM01 *= fInvLength;
  84. fM02 *= fInvLength;
  85. float fQ = ((float)2.0)*fM01*fM12+fM02*(fM22-fM11);
  86. m_afDiag[1] = fM11+fM02*fQ;
  87. m_afDiag[2] = fM22-fM02*fQ;
  88. m_afSubd[0] = fLength;
  89. m_afSubd[1] = fM12-fM01*fQ;
  90. mElement[0][0] = (float)1.0;
  91. mElement[0][1] = (float)0.0;
  92. mElement[0][2] = (float)0.0;
  93. mElement[1][0] = (float)0.0;
  94. mElement[1][1] = fM01;
  95. mElement[1][2] = fM02;
  96. mElement[2][0] = (float)0.0;
  97. mElement[2][1] = fM02;
  98. mElement[2][2] = -fM01;
  99. m_bIsRotation = false;
  100. }
  101. else
  102. {
  103. m_afDiag[1] = fM11;
  104. m_afDiag[2] = fM22;
  105. m_afSubd[0] = fM01;
  106. m_afSubd[1] = fM12;
  107. mElement[0][0] = (float)1.0;
  108. mElement[0][1] = (float)0.0;
  109. mElement[0][2] = (float)0.0;
  110. mElement[1][0] = (float)0.0;
  111. mElement[1][1] = (float)1.0;
  112. mElement[1][2] = (float)0.0;
  113. mElement[2][0] = (float)0.0;
  114. mElement[2][1] = (float)0.0;
  115. mElement[2][2] = (float)1.0;
  116. m_bIsRotation = true;
  117. }
  118. }
  119. bool QLAlgorithm(void)
  120. {
  121. const int iMaxIter = 32;
  122. for (int i0 = 0; i0 <3; i0++)
  123. {
  124. int i1;
  125. for (i1 = 0; i1 < iMaxIter; i1++)
  126. {
  127. int i2;
  128. for (i2 = i0; i2 <= (3-2); i2++)
  129. {
  130. float fTmp = fabsf(m_afDiag[i2]) + fabsf(m_afDiag[i2+1]);
  131. if ( fabsf(m_afSubd[i2]) + fTmp == fTmp )
  132. break;
  133. }
  134. if (i2 == i0)
  135. {
  136. break;
  137. }
  138. float fG = (m_afDiag[i0+1] - m_afDiag[i0])/(((float)2.0) * m_afSubd[i0]);
  139. float fR = sqrtf(fG*fG+(float)1.0);
  140. if (fG < (float)0.0)
  141. {
  142. fG = m_afDiag[i2]-m_afDiag[i0]+m_afSubd[i0]/(fG-fR);
  143. }
  144. else
  145. {
  146. fG = m_afDiag[i2]-m_afDiag[i0]+m_afSubd[i0]/(fG+fR);
  147. }
  148. float fSin = (float)1.0, fCos = (float)1.0, fP = (float)0.0;
  149. for (int i3 = i2-1; i3 >= i0; i3--)
  150. {
  151. float fF = fSin*m_afSubd[i3];
  152. float fB = fCos*m_afSubd[i3];
  153. if (fabsf(fF) >= fabsf(fG))
  154. {
  155. fCos = fG/fF;
  156. fR = sqrtf(fCos*fCos+(float)1.0);
  157. m_afSubd[i3+1] = fF*fR;
  158. fSin = ((float)1.0)/fR;
  159. fCos *= fSin;
  160. }
  161. else
  162. {
  163. fSin = fF/fG;
  164. fR = sqrtf(fSin*fSin+(float)1.0);
  165. m_afSubd[i3+1] = fG*fR;
  166. fCos = ((float)1.0)/fR;
  167. fSin *= fCos;
  168. }
  169. fG = m_afDiag[i3+1]-fP;
  170. fR = (m_afDiag[i3]-fG)*fSin+((float)2.0)*fB*fCos;
  171. fP = fSin*fR;
  172. m_afDiag[i3+1] = fG+fP;
  173. fG = fCos*fR-fB;
  174. for (int i4 = 0; i4 < 3; i4++)
  175. {
  176. fF = mElement[i4][i3+1];
  177. mElement[i4][i3+1] = fSin*mElement[i4][i3]+fCos*fF;
  178. mElement[i4][i3] = fCos*mElement[i4][i3]-fSin*fF;
  179. }
  180. }
  181. m_afDiag[i0] -= fP;
  182. m_afSubd[i0] = fG;
  183. m_afSubd[i2] = (float)0.0;
  184. }
  185. if (i1 == iMaxIter)
  186. {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. void DecreasingSort(void)
  193. {
  194. //sort eigenvalues in decreasing order, e[0] >= ... >= e[iSize-1]
  195. for (int i0 = 0, i1; i0 <= 3-2; i0++)
  196. {
  197. // locate maximum eigenvalue
  198. i1 = i0;
  199. float fMax = m_afDiag[i1];
  200. int i2;
  201. for (i2 = i0+1; i2 < 3; i2++)
  202. {
  203. if (m_afDiag[i2] > fMax)
  204. {
  205. i1 = i2;
  206. fMax = m_afDiag[i1];
  207. }
  208. }
  209. if (i1 != i0)
  210. {
  211. // swap eigenvalues
  212. m_afDiag[i1] = m_afDiag[i0];
  213. m_afDiag[i0] = fMax;
  214. // swap eigenvectors
  215. for (i2 = 0; i2 < 3; i2++)
  216. {
  217. float fTmp = mElement[i2][i0];
  218. mElement[i2][i0] = mElement[i2][i1];
  219. mElement[i2][i1] = fTmp;
  220. m_bIsRotation = !m_bIsRotation;
  221. }
  222. }
  223. }
  224. }
  225. void GuaranteeRotation(void)
  226. {
  227. if (!m_bIsRotation)
  228. {
  229. // change sign on the first column
  230. for (int iRow = 0; iRow <3; iRow++)
  231. {
  232. mElement[iRow][0] = -mElement[iRow][0];
  233. }
  234. }
  235. }
  236. float mElement[3][3];
  237. float m_afDiag[3];
  238. float m_afSubd[3];
  239. bool m_bIsRotation;
  240. };
  241. }
  242. using namespace BestFit;
  243. bool getBestFitPlane(unsigned int vcount,
  244. const float *points,
  245. unsigned int vstride,
  246. const float *weights,
  247. unsigned int wstride,
  248. float *plane)
  249. {
  250. bool ret = false;
  251. Vec3 kOrigin(0,0,0);
  252. float wtotal = 0;
  253. if ( 1 )
  254. {
  255. const char *source = (const char *) points;
  256. const char *wsource = (const char *) weights;
  257. for (unsigned int i=0; i<vcount; i++)
  258. {
  259. const float *p = (const float *) source;
  260. float w = 1;
  261. if ( wsource )
  262. {
  263. const float *ws = (const float *) wsource;
  264. w = *ws; //
  265. wsource+=wstride;
  266. }
  267. kOrigin.x+=p[0]*w;
  268. kOrigin.y+=p[1]*w;
  269. kOrigin.z+=p[2]*w;
  270. wtotal+=w;
  271. source+=vstride;
  272. }
  273. }
  274. float recip = 1.0f / wtotal; // reciprocol of total weighting
  275. kOrigin.x*=recip;
  276. kOrigin.y*=recip;
  277. kOrigin.z*=recip;
  278. float fSumXX=0;
  279. float fSumXY=0;
  280. float fSumXZ=0;
  281. float fSumYY=0;
  282. float fSumYZ=0;
  283. float fSumZZ=0;
  284. if ( 1 )
  285. {
  286. const char *source = (const char *) points;
  287. const char *wsource = (const char *) weights;
  288. for (unsigned int i=0; i<vcount; i++)
  289. {
  290. const float *p = (const float *) source;
  291. float w = 1;
  292. if ( wsource )
  293. {
  294. const float *ws = (const float *) wsource;
  295. w = *ws; //
  296. wsource+=wstride;
  297. }
  298. Vec3 kDiff;
  299. kDiff.x = w*(p[0] - kOrigin.x); // apply vertex weighting!
  300. kDiff.y = w*(p[1] - kOrigin.y);
  301. kDiff.z = w*(p[2] - kOrigin.z);
  302. fSumXX+= kDiff.x * kDiff.x; // sume of the squares of the differences.
  303. fSumXY+= kDiff.x * kDiff.y; // sume of the squares of the differences.
  304. fSumXZ+= kDiff.x * kDiff.z; // sume of the squares of the differences.
  305. fSumYY+= kDiff.y * kDiff.y;
  306. fSumYZ+= kDiff.y * kDiff.z;
  307. fSumZZ+= kDiff.z * kDiff.z;
  308. source+=vstride;
  309. }
  310. }
  311. fSumXX *= recip;
  312. fSumXY *= recip;
  313. fSumXZ *= recip;
  314. fSumYY *= recip;
  315. fSumYZ *= recip;
  316. fSumZZ *= recip;
  317. // setup the eigensolver
  318. Eigen kES;
  319. kES.mElement[0][0] = fSumXX;
  320. kES.mElement[0][1] = fSumXY;
  321. kES.mElement[0][2] = fSumXZ;
  322. kES.mElement[1][0] = fSumXY;
  323. kES.mElement[1][1] = fSumYY;
  324. kES.mElement[1][2] = fSumYZ;
  325. kES.mElement[2][0] = fSumXZ;
  326. kES.mElement[2][1] = fSumYZ;
  327. kES.mElement[2][2] = fSumZZ;
  328. // compute eigenstuff, smallest eigenvalue is in last position
  329. kES.DecrSortEigenStuff();
  330. Vec3 kNormal;
  331. kNormal.x = kES.mElement[0][2];
  332. kNormal.y = kES.mElement[1][2];
  333. kNormal.z = kES.mElement[2][2];
  334. // the minimum energy
  335. plane[0] = kNormal.x;
  336. plane[1] = kNormal.y;
  337. plane[2] = kNormal.z;
  338. plane[3] = 0 - kNormal.dot(kOrigin);
  339. return ret;
  340. }
  341. float getBoundingRegion(unsigned int vcount,const float *points,unsigned int pstride,float *bmin,float *bmax) // returns the diagonal distance
  342. {
  343. const unsigned char *source = (const unsigned char *) points;
  344. bmin[0] = points[0];
  345. bmin[1] = points[1];
  346. bmin[2] = points[2];
  347. bmax[0] = points[0];
  348. bmax[1] = points[1];
  349. bmax[2] = points[2];
  350. for (unsigned int i=1; i<vcount; i++)
  351. {
  352. source+=pstride;
  353. const float *p = (const float *) source;
  354. if ( p[0] < bmin[0] ) bmin[0] = p[0];
  355. if ( p[1] < bmin[1] ) bmin[1] = p[1];
  356. if ( p[2] < bmin[2] ) bmin[2] = p[2];
  357. if ( p[0] > bmax[0] ) bmax[0] = p[0];
  358. if ( p[1] > bmax[1] ) bmax[1] = p[1];
  359. if ( p[2] > bmax[2] ) bmax[2] = p[2];
  360. }
  361. float dx = bmax[0] - bmin[0];
  362. float dy = bmax[1] - bmin[1];
  363. float dz = bmax[2] - bmin[2];
  364. return sqrtf( dx*dx + dy*dy + dz*dz );
  365. }
  366. bool overlapAABB(const float *bmin1,const float *bmax1,const float *bmin2,const float *bmax2) // return true if the two AABB's overlap.
  367. {
  368. if ( bmax2[0] < bmin1[0] ) return false; // if the maximum is less than our minimum on any axis
  369. if ( bmax2[1] < bmin1[1] ) return false;
  370. if ( bmax2[2] < bmin1[2] ) return false;
  371. if ( bmin2[0] > bmax1[0] ) return false; // if the minimum is greater than our maximum on any axis
  372. if ( bmin2[1] > bmax1[1] ) return false; // if the minimum is greater than our maximum on any axis
  373. if ( bmin2[2] > bmax1[2] ) return false; // if the minimum is greater than our maximum on any axis
  374. return true; // the extents overlap
  375. }