planetri.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "planetri.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. static inline float DistToPt(const float *p,const float *plane)
  35. {
  36. float x = p[0];
  37. float y = p[1];
  38. float z = p[2];
  39. float d = x*plane[0] + y*plane[1] + z*plane[2] + plane[3];
  40. return d;
  41. }
  42. static PlaneTriResult getSidePlane(const float *p,const float *plane,float epsilon)
  43. {
  44. float d = DistToPt(p,plane);
  45. if ( (d+epsilon) > 0 )
  46. return PTR_FRONT; // it is 'in front' within the provided epsilon value.
  47. return PTR_BACK;
  48. }
  49. static void add(const float *p,float *dest,unsigned int tstride,unsigned int &pcount)
  50. {
  51. char *d = (char *) dest;
  52. d = d + pcount*tstride;
  53. dest = (float *) d;
  54. dest[0] = p[0];
  55. dest[1] = p[1];
  56. dest[2] = p[2];
  57. pcount++;
  58. assert( pcount <= 4 );
  59. }
  60. // assumes that the points are on opposite sides of the plane!
  61. static void intersect(const float *p1,const float *p2,float *split,const float *plane)
  62. {
  63. float dp1 = DistToPt(p1,plane);
  64. float dir[3];
  65. dir[0] = p2[0] - p1[0];
  66. dir[1] = p2[1] - p1[1];
  67. dir[2] = p2[2] - p1[2];
  68. float dot1 = dir[0]*plane[0] + dir[1]*plane[1] + dir[2]*plane[2];
  69. float dot2 = dp1 - plane[3];
  70. float t = -(plane[3] + dot2 ) / dot1;
  71. split[0] = (dir[0]*t)+p1[0];
  72. split[1] = (dir[1]*t)+p1[1];
  73. split[2] = (dir[2]*t)+p1[2];
  74. }
  75. PlaneTriResult planeTriIntersection(const float *plane, // the plane equation in Ax+By+Cz+D format
  76. const float *triangle, // the source triangle.
  77. unsigned int tstride, // stride in bytes of the input and output triangles
  78. float epsilon, // the co-planer epsilon value.
  79. float *front, // the triangle in front of the
  80. unsigned int &fcount, // number of vertices in the 'front' triangle
  81. float *back, // the triangle in back of the plane
  82. unsigned int &bcount) // the number of vertices in the 'back' triangle.
  83. {
  84. fcount = 0;
  85. bcount = 0;
  86. const char *tsource = (const char *) triangle;
  87. // get the three vertices of the triangle.
  88. const float *p1 = (const float *) (tsource);
  89. const float *p2 = (const float *) (tsource+tstride);
  90. const float *p3 = (const float *) (tsource+tstride*2);
  91. PlaneTriResult r1 = getSidePlane(p1,plane,epsilon); // compute the side of the plane each vertex is on
  92. PlaneTriResult r2 = getSidePlane(p2,plane,epsilon);
  93. PlaneTriResult r3 = getSidePlane(p3,plane,epsilon);
  94. if ( r1 == r2 && r1 == r3 ) // if all three vertices are on the same side of the plane.
  95. {
  96. if ( r1 == PTR_FRONT ) // if all three are in front of the plane, then copy to the 'front' output triangle.
  97. {
  98. add(p1,front,tstride,fcount);
  99. add(p2,front,tstride,fcount);
  100. add(p3,front,tstride,fcount);
  101. }
  102. else
  103. {
  104. add(p1,back,tstride,bcount); // if all three are in 'abck' then copy to the 'back' output triangle.
  105. add(p2,back,tstride,bcount);
  106. add(p3,back,tstride,bcount);
  107. }
  108. return r1; // if all three points are on the same side of the plane return result
  109. }
  110. // ok.. we need to split the triangle at the plane.
  111. // First test ray segment P1 to P2
  112. if ( r1 == r2 ) // if these are both on the same side...
  113. {
  114. if ( r1 == PTR_FRONT )
  115. {
  116. add( p1, front, tstride, fcount );
  117. add( p2, front, tstride, fcount );
  118. }
  119. else
  120. {
  121. add( p1, back, tstride, bcount );
  122. add( p2, back, tstride, bcount );
  123. }
  124. }
  125. else
  126. {
  127. float split[3]; // split the point
  128. intersect(p1,p2,split,plane);
  129. if ( r1 == PTR_FRONT )
  130. {
  131. add(p1, front, tstride, fcount );
  132. add(split, front, tstride, fcount );
  133. add(split, back, tstride, bcount );
  134. add(p2, back, tstride, bcount );
  135. }
  136. else
  137. {
  138. add(p1, back, tstride, bcount );
  139. add(split, back, tstride, bcount );
  140. add(split, front, tstride, fcount );
  141. add(p2, front, tstride, fcount );
  142. }
  143. }
  144. // Next test ray segment P2 to P3
  145. if ( r2 == r3 ) // if these are both on the same side...
  146. {
  147. if ( r3 == PTR_FRONT )
  148. {
  149. add( p3, front, tstride, fcount );
  150. }
  151. else
  152. {
  153. add( p3, back, tstride, bcount );
  154. }
  155. }
  156. else
  157. {
  158. float split[3]; // split the point
  159. intersect(p2,p3,split,plane);
  160. if ( r3 == PTR_FRONT )
  161. {
  162. add(split, front, tstride, fcount );
  163. add(split, back, tstride, bcount );
  164. add(p3, front, tstride, fcount );
  165. }
  166. else
  167. {
  168. add(split, front, tstride, fcount );
  169. add(split, back, tstride, bcount );
  170. add(p3, back, tstride, bcount );
  171. }
  172. }
  173. // Next test ray segment P3 to P1
  174. if ( r3 != r1 ) // if these are both on the same side...
  175. {
  176. float split[3]; // split the point
  177. intersect(p3,p1,split,plane);
  178. if ( r1 == PTR_FRONT )
  179. {
  180. add(split, front, tstride, fcount );
  181. add(split, back, tstride, bcount );
  182. }
  183. else
  184. {
  185. add(split, front, tstride, fcount );
  186. add(split, back, tstride, bcount );
  187. }
  188. }
  189. return PTR_SPLIT;
  190. }