bestfitobb.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <assert.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. #include "bestfitobb.h"
  35. #include "float_math.h"
  36. // computes the OBB for this set of points relative to this transform matrix.
  37. void computeOBB(unsigned int vcount,const float *points,unsigned int pstride,float *sides,const float *matrix)
  38. {
  39. const char *src = (const char *) points;
  40. float bmin[3] = { 1e9, 1e9, 1e9 };
  41. float bmax[3] = { -1e9, -1e9, -1e9 };
  42. for (unsigned int i=0; i<vcount; i++)
  43. {
  44. const float *p = (const float *) src;
  45. float t[3];
  46. fm_inverseRT(matrix, p, t ); // inverse rotate translate
  47. if ( t[0] < bmin[0] ) bmin[0] = t[0];
  48. if ( t[1] < bmin[1] ) bmin[1] = t[1];
  49. if ( t[2] < bmin[2] ) bmin[2] = t[2];
  50. if ( t[0] > bmax[0] ) bmax[0] = t[0];
  51. if ( t[1] > bmax[1] ) bmax[1] = t[1];
  52. if ( t[2] > bmax[2] ) bmax[2] = t[2];
  53. src+=pstride;
  54. }
  55. sides[0] = bmax[0];
  56. sides[1] = bmax[1];
  57. sides[2] = bmax[2];
  58. if ( fabsf(bmin[0]) > sides[0] ) sides[0] = fabsf(bmin[0]);
  59. if ( fabsf(bmin[1]) > sides[1] ) sides[1] = fabsf(bmin[1]);
  60. if ( fabsf(bmin[2]) > sides[2] ) sides[2] = fabsf(bmin[2]);
  61. sides[0]*=2.0f;
  62. sides[1]*=2.0f;
  63. sides[2]*=2.0f;
  64. }
  65. void computeBestFitOBB(unsigned int vcount,const float *points,unsigned int pstride,float *sides,float *matrix)
  66. {
  67. float bmin[3];
  68. float bmax[3];
  69. fm_getAABB(vcount,points,pstride,bmin,bmax);
  70. float center[3];
  71. center[0] = (bmax[0]-bmin[0])*0.5f + bmin[0];
  72. center[1] = (bmax[1]-bmin[1])*0.5f + bmin[1];
  73. center[2] = (bmax[2]-bmin[2])*0.5f + bmin[2];
  74. float ax = 0;
  75. float ay = 0;
  76. float az = 0;
  77. float sweep = 45.0f; // 180 degree sweep on all three axes.
  78. float steps = 8.0f; // 16 steps on each axis.
  79. float bestVolume = 1e9;
  80. float angle[3]={0.f,0.f,0.f};
  81. while ( sweep >= 1 )
  82. {
  83. bool found = false;
  84. float stepsize = sweep / steps;
  85. for (float x=ax-sweep; x<=ax+sweep; x+=stepsize)
  86. {
  87. for (float y=ay-sweep; y<=ay+sweep; y+=stepsize)
  88. {
  89. for (float z=az-sweep; z<=az+sweep; z+=stepsize)
  90. {
  91. float pmatrix[16];
  92. fm_eulerMatrix( x*FM_DEG_TO_RAD, y*FM_DEG_TO_RAD, z*FM_DEG_TO_RAD, pmatrix );
  93. pmatrix[3*4+0] = center[0];
  94. pmatrix[3*4+1] = center[1];
  95. pmatrix[3*4+2] = center[2];
  96. float psides[3];
  97. computeOBB( vcount, points, pstride, psides, pmatrix );
  98. float volume = psides[0]*psides[1]*psides[2]; // the volume of the cube
  99. if ( volume <= bestVolume )
  100. {
  101. bestVolume = volume;
  102. sides[0] = psides[0];
  103. sides[1] = psides[1];
  104. sides[2] = psides[2];
  105. angle[0] = ax;
  106. angle[1] = ay;
  107. angle[2] = az;
  108. memcpy(matrix,pmatrix,sizeof(float)*16);
  109. found = true; // yes, we found an improvement.
  110. }
  111. }
  112. }
  113. }
  114. if ( found )
  115. {
  116. ax = angle[0];
  117. ay = angle[1];
  118. az = angle[2];
  119. sweep*=0.5f; // sweep 1/2 the distance as the last time.
  120. }
  121. else
  122. {
  123. break; // no improvement, so just
  124. }
  125. }
  126. }