gim_clip_polygon.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef GIM_CLIP_POLYGON_H_INCLUDED
  2. #define GIM_CLIP_POLYGON_H_INCLUDED
  3. /*! \file gim_tri_collision.h
  4. \author Francisco Leon Najera
  5. */
  6. /*
  7. -----------------------------------------------------------------------------
  8. This source file is part of GIMPACT Library.
  9. For the latest info, see http://gimpact.sourceforge.net/
  10. Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
  11. email: projectileman@yahoo.com
  12. This library is free software; you can redistribute it and/or
  13. modify it under the terms of EITHER:
  14. (1) The GNU Lesser General Public License as published by the Free
  15. Software Foundation; either version 2.1 of the License, or (at
  16. your option) any later version. The text of the GNU Lesser
  17. General Public License is included with this library in the
  18. file GIMPACT-LICENSE-LGPL.TXT.
  19. (2) The BSD-style license that is included with this library in
  20. the file GIMPACT-LICENSE-BSD.TXT.
  21. (3) The zlib/libpng license that is included with this library in
  22. the file GIMPACT-LICENSE-ZLIB.TXT.
  23. This library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
  26. GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
  27. -----------------------------------------------------------------------------
  28. */
  29. //! This function calcs the distance from a 3D plane
  30. class DISTANCE_PLANE_3D_FUNC
  31. {
  32. public:
  33. template<typename CLASS_POINT,typename CLASS_PLANE>
  34. inline GREAL operator()(const CLASS_PLANE & plane, const CLASS_POINT & point)
  35. {
  36. return DISTANCE_PLANE_POINT(plane, point);
  37. }
  38. };
  39. template<typename CLASS_POINT>
  40. SIMD_FORCE_INLINE void PLANE_CLIP_POLYGON_COLLECT(
  41. const CLASS_POINT & point0,
  42. const CLASS_POINT & point1,
  43. GREAL dist0,
  44. GREAL dist1,
  45. CLASS_POINT * clipped,
  46. GUINT & clipped_count)
  47. {
  48. GUINT _prevclassif = (dist0>G_EPSILON);
  49. GUINT _classif = (dist1>G_EPSILON);
  50. if(_classif!=_prevclassif)
  51. {
  52. GREAL blendfactor = -dist0/(dist1-dist0);
  53. VEC_BLEND(clipped[clipped_count],point0,point1,blendfactor);
  54. clipped_count++;
  55. }
  56. if(!_classif)
  57. {
  58. VEC_COPY(clipped[clipped_count],point1);
  59. clipped_count++;
  60. }
  61. }
  62. //! Clips a polygon by a plane
  63. /*!
  64. *\return The count of the clipped counts
  65. */
  66. template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
  67. SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON_GENERIC(
  68. const CLASS_PLANE & plane,
  69. const CLASS_POINT * polygon_points,
  70. GUINT polygon_point_count,
  71. CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func)
  72. {
  73. GUINT clipped_count = 0;
  74. //clip first point
  75. GREAL firstdist = distance_func(plane,polygon_points[0]);;
  76. if(!(firstdist>G_EPSILON))
  77. {
  78. VEC_COPY(clipped[clipped_count],polygon_points[0]);
  79. clipped_count++;
  80. }
  81. GREAL olddist = firstdist;
  82. for(GUINT _i=1;_i<polygon_point_count;_i++)
  83. {
  84. GREAL dist = distance_func(plane,polygon_points[_i]);
  85. PLANE_CLIP_POLYGON_COLLECT(
  86. polygon_points[_i-1],polygon_points[_i],
  87. olddist,
  88. dist,
  89. clipped,
  90. clipped_count);
  91. olddist = dist;
  92. }
  93. //RETURN TO FIRST point
  94. PLANE_CLIP_POLYGON_COLLECT(
  95. polygon_points[polygon_point_count-1],polygon_points[0],
  96. olddist,
  97. firstdist,
  98. clipped,
  99. clipped_count);
  100. return clipped_count;
  101. }
  102. //! Clips a polygon by a plane
  103. /*!
  104. *\return The count of the clipped counts
  105. */
  106. template<typename CLASS_POINT,typename CLASS_PLANE, typename DISTANCE_PLANE_FUNC>
  107. SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE_GENERIC(
  108. const CLASS_PLANE & plane,
  109. const CLASS_POINT & point0,
  110. const CLASS_POINT & point1,
  111. const CLASS_POINT & point2,
  112. CLASS_POINT * clipped,DISTANCE_PLANE_FUNC distance_func)
  113. {
  114. GUINT clipped_count = 0;
  115. //clip first point
  116. GREAL firstdist = distance_func(plane,point0);;
  117. if(!(firstdist>G_EPSILON))
  118. {
  119. VEC_COPY(clipped[clipped_count],point0);
  120. clipped_count++;
  121. }
  122. // point 1
  123. GREAL olddist = firstdist;
  124. GREAL dist = distance_func(plane,point1);
  125. PLANE_CLIP_POLYGON_COLLECT(
  126. point0,point1,
  127. olddist,
  128. dist,
  129. clipped,
  130. clipped_count);
  131. olddist = dist;
  132. // point 2
  133. dist = distance_func(plane,point2);
  134. PLANE_CLIP_POLYGON_COLLECT(
  135. point1,point2,
  136. olddist,
  137. dist,
  138. clipped,
  139. clipped_count);
  140. olddist = dist;
  141. //RETURN TO FIRST point
  142. PLANE_CLIP_POLYGON_COLLECT(
  143. point2,point0,
  144. olddist,
  145. firstdist,
  146. clipped,
  147. clipped_count);
  148. return clipped_count;
  149. }
  150. template<typename CLASS_POINT,typename CLASS_PLANE>
  151. SIMD_FORCE_INLINE GUINT PLANE_CLIP_POLYGON3D(
  152. const CLASS_PLANE & plane,
  153. const CLASS_POINT * polygon_points,
  154. GUINT polygon_point_count,
  155. CLASS_POINT * clipped)
  156. {
  157. return PLANE_CLIP_POLYGON_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,polygon_points,polygon_point_count,clipped,DISTANCE_PLANE_3D_FUNC());
  158. }
  159. template<typename CLASS_POINT,typename CLASS_PLANE>
  160. SIMD_FORCE_INLINE GUINT PLANE_CLIP_TRIANGLE3D(
  161. const CLASS_PLANE & plane,
  162. const CLASS_POINT & point0,
  163. const CLASS_POINT & point1,
  164. const CLASS_POINT & point2,
  165. CLASS_POINT * clipped)
  166. {
  167. return PLANE_CLIP_TRIANGLE_GENERIC<CLASS_POINT,CLASS_PLANE>(plane,point0,point1,point2,clipped,DISTANCE_PLANE_3D_FUNC());
  168. }
  169. #endif // GIM_TRI_COLLISION_H_INCLUDED