fitsphere.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <math.h>
  6. #include "fitsphere.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. /*
  35. An Efficient Bounding Sphere
  36. by Jack Ritter
  37. from "Graphics Gems", Academic Press, 1990
  38. */
  39. /* Routine to calculate tight bounding sphere over */
  40. /* a set of points in 3D */
  41. /* This contains the routine find_bounding_sphere(), */
  42. /* the struct definition, and the globals used for parameters. */
  43. /* The abs() of all coordinates must be < BIGNUMBER */
  44. /* Code written by Jack Ritter and Lyle Rains. */
  45. #define BIGNUMBER 100000000.0 /* hundred million */
  46. static inline void Set(float *n,float x,float y,float z)
  47. {
  48. n[0] = x;
  49. n[1] = y;
  50. n[2] = z;
  51. }
  52. static inline void Copy(float *dest,const float *source)
  53. {
  54. dest[0] = source[0];
  55. dest[1] = source[1];
  56. dest[2] = source[2];
  57. }
  58. float computeBoundingSphere(unsigned int vcount,const float *points,float *center)
  59. {
  60. float mRadius;
  61. float mRadius2;
  62. float xmin[3];
  63. float xmax[3];
  64. float ymin[3];
  65. float ymax[3];
  66. float zmin[3];
  67. float zmax[3];
  68. float dia1[3];
  69. float dia2[3];
  70. /* FIRST PASS: find 6 minima/maxima points */
  71. Set(xmin,BIGNUMBER,BIGNUMBER,BIGNUMBER);
  72. Set(xmax,-BIGNUMBER,-BIGNUMBER,-BIGNUMBER);
  73. Set(ymin,BIGNUMBER,BIGNUMBER,BIGNUMBER);
  74. Set(ymax,-BIGNUMBER,-BIGNUMBER,-BIGNUMBER);
  75. Set(zmin,BIGNUMBER,BIGNUMBER,BIGNUMBER);
  76. Set(zmax,-BIGNUMBER,-BIGNUMBER,-BIGNUMBER);
  77. for (unsigned i=0; i<vcount; i++)
  78. {
  79. const float *caller_p = &points[i*3];
  80. if (caller_p[0]<xmin[0])
  81. Copy(xmin,caller_p); /* New xminimum point */
  82. if (caller_p[0]>xmax[0])
  83. Copy(xmax,caller_p);
  84. if (caller_p[1]<ymin[1])
  85. Copy(ymin,caller_p);
  86. if (caller_p[1]>ymax[1])
  87. Copy(ymax,caller_p);
  88. if (caller_p[2]<zmin[2])
  89. Copy(zmin,caller_p);
  90. if (caller_p[2]>zmax[2])
  91. Copy(zmax,caller_p);
  92. }
  93. /* Set xspan = distance between the 2 points xmin & xmax (squared) */
  94. float dx = xmax[0] - xmin[0];
  95. float dy = xmax[1] - xmin[1];
  96. float dz = xmax[2] - xmin[2];
  97. float xspan = dx*dx + dy*dy + dz*dz;
  98. /* Same for y & z spans */
  99. dx = ymax[0] - ymin[0];
  100. dy = ymax[1] - ymin[1];
  101. dz = ymax[2] - ymin[2];
  102. float yspan = dx*dx + dy*dy + dz*dz;
  103. dx = zmax[0] - zmin[0];
  104. dy = zmax[1] - zmin[1];
  105. dz = zmax[2] - zmin[2];
  106. float zspan = dx*dx + dy*dy + dz*dz;
  107. /* Set points dia1 & dia2 to the maximally separated pair */
  108. Copy(dia1,xmin);
  109. Copy(dia2,xmax); /* assume xspan biggest */
  110. float maxspan = xspan;
  111. if (yspan>maxspan)
  112. {
  113. maxspan = yspan;
  114. Copy(dia1,ymin);
  115. Copy(dia2,ymax);
  116. }
  117. if (zspan>maxspan)
  118. {
  119. Copy(dia1,zmin);
  120. Copy(dia2,zmax);
  121. }
  122. /* dia1,dia2 is a diameter of initial sphere */
  123. /* calc initial center */
  124. center[0] = (dia1[0]+dia2[0])*0.5f;
  125. center[1] = (dia1[1]+dia2[1])*0.5f;
  126. center[2] = (dia1[2]+dia2[2])*0.5f;
  127. /* calculate initial radius**2 and radius */
  128. dx = dia2[0]-center[0]; /* x component of radius vector */
  129. dy = dia2[1]-center[1]; /* y component of radius vector */
  130. dz = dia2[2]-center[2]; /* z component of radius vector */
  131. mRadius2 = dx*dx + dy*dy + dz*dz;
  132. mRadius = float(sqrt(mRadius2));
  133. /* SECOND PASS: increment current sphere */
  134. if ( 1 )
  135. {
  136. for (unsigned i=0; i<vcount; i++)
  137. {
  138. const float *caller_p = &points[i*3];
  139. dx = caller_p[0]-center[0];
  140. dy = caller_p[1]-center[1];
  141. dz = caller_p[2]-center[2];
  142. float old_to_p_sq = dx*dx + dy*dy + dz*dz;
  143. if (old_to_p_sq > mRadius2) /* do r**2 test first */
  144. { /* this point is outside of current sphere */
  145. float old_to_p = float(sqrt(old_to_p_sq));
  146. /* calc radius of new sphere */
  147. mRadius = (mRadius + old_to_p) * 0.5f;
  148. mRadius2 = mRadius*mRadius; /* for next r**2 compare */
  149. float old_to_new = old_to_p - mRadius;
  150. /* calc center of new sphere */
  151. float recip = 1.0f /old_to_p;
  152. float cx = (mRadius*center[0] + old_to_new*caller_p[0]) * recip;
  153. float cy = (mRadius*center[1] + old_to_new*caller_p[1]) * recip;
  154. float cz = (mRadius*center[2] + old_to_new*caller_p[2]) * recip;
  155. Set(center,cx,cy,cz);
  156. }
  157. }
  158. }
  159. return mRadius;
  160. }