Simplex.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using OpenSim.Region.Physics.Manager;
  29. namespace OpenSim.Region.Physics.Meshing
  30. {
  31. // A simplex is a section of a straight line.
  32. // It is defined by its endpoints, i.e. by two vertices
  33. // Operation on vertices are
  34. public class Simplex : IComparable<Simplex>
  35. {
  36. public Vertex v1;
  37. public Vertex v2;
  38. public Simplex(Vertex _v1, Vertex _v2)
  39. {
  40. v1 = _v1;
  41. v2 = _v2;
  42. }
  43. public int CompareTo(Simplex other)
  44. {
  45. Vertex lv1, lv2, ov1, ov2, temp;
  46. lv1 = v1;
  47. lv2 = v2;
  48. ov1 = other.v1;
  49. ov2 = other.v2;
  50. if (lv1 > lv2)
  51. {
  52. temp = lv1;
  53. lv1 = lv2;
  54. lv2 = temp;
  55. }
  56. if (ov1 > ov2)
  57. {
  58. temp = ov1;
  59. ov1 = ov2;
  60. ov2 = temp;
  61. }
  62. if (lv1 > ov1)
  63. {
  64. return 1;
  65. }
  66. if (lv1 < ov1)
  67. {
  68. return -1;
  69. }
  70. if (lv2 > ov2)
  71. {
  72. return 1;
  73. }
  74. if (lv2 < ov2)
  75. {
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. private static void intersectParameter(PhysicsVector p1, PhysicsVector r1, PhysicsVector p2, PhysicsVector r2,
  81. ref float lambda, ref float mu)
  82. {
  83. // Intersects two straights
  84. // p1, p2, points on the straight
  85. // r1, r2, directional vectors of the straight. Not necessarily of length 1!
  86. // note, that l, m can be scaled such, that the range 0..1 is mapped to the area between two points,
  87. // thus allowing to decide whether an intersection is between two points
  88. float r1x = r1.X;
  89. float r1y = r1.Y;
  90. float r2x = r2.X;
  91. float r2y = r2.Y;
  92. float denom = r1y*r2x - r1x*r2y;
  93. float p1x = p1.X;
  94. float p1y = p1.Y;
  95. float p2x = p2.X;
  96. float p2y = p2.Y;
  97. float z1 = -p2x*r2y + p1x*r2y + (p2y - p1y)*r2x;
  98. float z2 = -p2x*r1y + p1x*r1y + (p2y - p1y)*r1x;
  99. if (denom == 0.0f) // Means the straights are parallel. Either no intersection or an infinite number of them
  100. {
  101. if (z1 == 0.0f)
  102. {
  103. // Means they are identical -> many, many intersections
  104. lambda = Single.NaN;
  105. mu = Single.NaN;
  106. }
  107. else
  108. {
  109. lambda = Single.PositiveInfinity;
  110. mu = Single.PositiveInfinity;
  111. }
  112. return;
  113. }
  114. lambda = z1/denom;
  115. mu = z2/denom;
  116. }
  117. // Intersects the simplex with another one.
  118. // the borders are used to deal with float inaccuracies
  119. // As a rule of thumb, the borders are
  120. // lowerBorder1 : 0.0
  121. // lowerBorder2 : 0.0
  122. // upperBorder1 : 1.0
  123. // upperBorder2 : 1.0
  124. // Set these to values near the given parameters (e.g. 0.001 instead of 1 to exclude simplex starts safely, or to -0.001 to include them safely)
  125. public static PhysicsVector Intersect(
  126. Simplex s1,
  127. Simplex s2,
  128. float lowerBorder1,
  129. float lowerBorder2,
  130. float upperBorder1,
  131. float upperBorder2)
  132. {
  133. PhysicsVector firstSimplexDirection = s1.v2 - s1.v1;
  134. PhysicsVector secondSimplexDirection = s2.v2 - s2.v1;
  135. float lambda = 0.0f;
  136. float mu = 0.0f;
  137. // Give us the parameters of an intersection. This subroutine does *not* take the constraints
  138. // (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
  139. // into account. We do that afterwards.
  140. intersectParameter(s1.v1, firstSimplexDirection, s2.v1, secondSimplexDirection, ref lambda, ref mu);
  141. if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
  142. return null;
  143. if (Single.IsNaN(lambda)) // Special case. many, many intersections.
  144. return null;
  145. if (lambda > upperBorder1) // We're behind v2
  146. return null;
  147. if (lambda < lowerBorder1)
  148. return null;
  149. if (mu < lowerBorder2) // outside simplex 2
  150. return null;
  151. if (mu > upperBorder2) // outside simplex 2
  152. return null;
  153. return s1.v1 + lambda*firstSimplexDirection;
  154. }
  155. // Intersects the simplex with a ray. The ray is defined as all p=origin + lambda*direction
  156. // where lambda >= 0
  157. public PhysicsVector RayIntersect(Vertex origin, PhysicsVector direction, bool bEndsIncluded)
  158. {
  159. PhysicsVector simplexDirection = v2 - v1;
  160. float lambda = 0.0f;
  161. float mu = 0.0f;
  162. // Give us the parameters of an intersection. This subroutine does *not* take the constraints
  163. // (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
  164. // into account. We do that afterwards.
  165. intersectParameter(v1, simplexDirection, origin, direction, ref lambda, ref mu);
  166. if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
  167. return null;
  168. if (Single.IsNaN(lambda)) // Special case. many, many intersections.
  169. return null;
  170. if (mu < 0.0) // We're on the wrong side of the ray
  171. return null;
  172. if (lambda > 1.0) // We're behind v2
  173. return null;
  174. if (lambda == 1.0 && !bEndsIncluded)
  175. return null; // The end of the simplices are not included
  176. if (lambda < 0.0f) // we're before v1;
  177. return null;
  178. return v1 + lambda*simplexDirection;
  179. }
  180. }
  181. }