/BulletNoXna/BulletNoXna/BulletCollision/GImpact/ClipPolygon.cs

https://bitbucket.org/cjrgaming/bullet-noxna · C# · 187 lines · 110 code · 35 blank · 42 comment · 6 complexity · e8a1e9571b585949207f8bf67198bdfc MD5 · raw file

  1. /*
  2. *
  3. * C# / XNA port of Bullet (c) 2011 Mark Neale <xexuxjy@hotmail.com>
  4. *
  5. This source file is part of GIMPACT Library.
  6. For the latest info, see http://gimpact.sourceforge.net/
  7. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  8. email: projectileman@yahoo.com
  9. This software is provided 'as-is', without any express or implied warranty.
  10. In no event will the authors be held liable for any damages arising from the use of this software.
  11. Permission is granted to anyone to use this software for any purpose,
  12. including commercial applications, and to alter it and redistribute it freely,
  13. subject to the following restrictions:
  14. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using BulletXNA.LinearMath;
  19. namespace BulletXNA.BulletCollision
  20. {
  21. public class ClipPolygon
  22. {
  23. public static float DistancePointPlane(ref Vector4 plane, ref Vector3 point)
  24. {
  25. return point.Dot(new Vector3(plane.X, plane.Y, plane.Z)) - plane.W;
  26. }
  27. /*! Vector blending
  28. Takes two vectors a, b, blends them together*/
  29. public static void VecBlend(ref Vector3 vr, ref Vector3 va, ref Vector3 vb, float blend_factor)
  30. {
  31. vr = (1 - blend_factor) * va + blend_factor * vb;
  32. }
  33. //fixme - see if this can take a raw array.
  34. //! This function calcs the distance from a 3D plane
  35. public static void PlaneClipPolygonCollect(
  36. ref Vector3 point0,
  37. ref Vector3 point1,
  38. float dist0,
  39. float dist1,
  40. Vector3[] clipped,
  41. ref int clipped_count)
  42. {
  43. bool _prevclassif = (dist0 > MathUtil.SIMD_EPSILON);
  44. bool _classif = (dist1 > MathUtil.SIMD_EPSILON);
  45. if (_classif != _prevclassif)
  46. {
  47. float blendfactor = -dist0 / (dist1 - dist0);
  48. VecBlend(ref clipped[clipped_count], ref point0, ref point1, blendfactor);
  49. clipped_count++;
  50. }
  51. if (!_classif)
  52. {
  53. clipped[clipped_count] = point1;
  54. clipped_count++;
  55. }
  56. }
  57. //! Clips a polygon by a plane
  58. /*!
  59. *\return The count of the clipped counts
  60. */
  61. public static int PlaneClipPolygon(
  62. ref Vector4 plane,
  63. Vector3[] polygon_points,
  64. int polygon_point_count,
  65. Vector3[] clipped)
  66. {
  67. int clipped_count = 0;
  68. //Vector3[] rawPoints = polygon_points.GetRawArray();
  69. //clip first point
  70. float firstdist = DistancePointPlane(ref plane, ref polygon_points[0]); ;
  71. if (!(firstdist > MathUtil.SIMD_EPSILON))
  72. {
  73. clipped[clipped_count] = polygon_points[0];
  74. clipped_count++;
  75. }
  76. float olddist = firstdist;
  77. for (int i = 1; i < polygon_point_count; i++)
  78. {
  79. float dist = DistancePointPlane(ref plane, ref polygon_points[i]);
  80. PlaneClipPolygonCollect(
  81. ref polygon_points[i - 1], ref polygon_points[i],
  82. olddist,
  83. dist,
  84. clipped,
  85. ref clipped_count);
  86. olddist = dist;
  87. }
  88. //RETURN TO FIRST point
  89. PlaneClipPolygonCollect(
  90. ref polygon_points[polygon_point_count - 1], ref polygon_points[0],
  91. olddist,
  92. firstdist,
  93. clipped,
  94. ref clipped_count);
  95. return clipped_count;
  96. }
  97. //! Clips a polygon by a plane
  98. /*!
  99. *\param clipped must be an array of 16 points.
  100. *\return The count of the clipped counts
  101. */
  102. public static int PlaneClipTriangle(
  103. ref Vector4 plane,
  104. ref Vector3 point0,
  105. ref Vector3 point1,
  106. ref Vector3 point2,
  107. Vector3[] clipped // an allocated array of 16 points at least
  108. )
  109. {
  110. int clipped_count = 0;
  111. //clip first point0
  112. float firstdist = DistancePointPlane(ref plane, ref point0);
  113. if (!(firstdist > MathUtil.SIMD_EPSILON))
  114. {
  115. clipped[clipped_count] = point0;
  116. clipped_count++;
  117. }
  118. // point 1
  119. float olddist = firstdist;
  120. float dist = DistancePointPlane(ref plane, ref point1);
  121. PlaneClipPolygonCollect(
  122. ref point0, ref point1,
  123. olddist,
  124. dist,
  125. clipped,
  126. ref clipped_count);
  127. olddist = dist;
  128. // point 2
  129. dist = DistancePointPlane(ref plane, ref point2);
  130. PlaneClipPolygonCollect(
  131. ref point1, ref point2,
  132. olddist,
  133. dist,
  134. clipped,
  135. ref clipped_count);
  136. olddist = dist;
  137. //RETURN TO FIRST point0
  138. PlaneClipPolygonCollect(
  139. ref point2, ref point0,
  140. olddist,
  141. firstdist,
  142. clipped,
  143. ref clipped_count);
  144. return clipped_count;
  145. }
  146. }
  147. }