1
0

Simplex.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. */
  28. using System;
  29. using OpenSim.Region.Physics.Manager;
  30. namespace OpenSim.Region.Physics.Meshing
  31. {
  32. // A simplex is a section of a straight line.
  33. // It is defined by its endpoints, i.e. by two vertices
  34. // Operation on vertices are
  35. public class Simplex : IComparable<Simplex>
  36. {
  37. public Vertex v1;
  38. public Vertex v2;
  39. public Simplex(Vertex _v1, Vertex _v2)
  40. {
  41. v1 = _v1;
  42. v2 = _v2;
  43. }
  44. public int CompareTo(Simplex other)
  45. {
  46. Vertex lv1, lv2, ov1, ov2, temp;
  47. lv1 = v1;
  48. lv2 = v2;
  49. ov1 = other.v1;
  50. ov2 = other.v2;
  51. if (lv1 > lv2)
  52. {
  53. temp = lv1;
  54. lv1 = lv2;
  55. lv2 = temp;
  56. }
  57. if (ov1 > ov2)
  58. {
  59. temp = ov1;
  60. ov1 = ov2;
  61. ov2 = temp;
  62. }
  63. if (lv1 > ov1)
  64. {
  65. return 1;
  66. }
  67. if (lv1 < ov1)
  68. {
  69. return -1;
  70. }
  71. if (lv2 > ov2)
  72. {
  73. return 1;
  74. }
  75. if (lv2 < ov2)
  76. {
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. private static void intersectParameter(PhysicsVector p1, PhysicsVector r1, PhysicsVector p2, PhysicsVector r2,
  82. ref float lambda, ref float mu)
  83. {
  84. // Intersects two straights
  85. // p1, p2, points on the straight
  86. // r1, r2, directional vectors of the straight. Not necessarily of length 1!
  87. // note, that l, m can be scaled such, that the range 0..1 is mapped to the area between two points,
  88. // thus allowing to decide whether an intersection is between two points
  89. float r1x = r1.X;
  90. float r1y = r1.Y;
  91. float r2x = r2.X;
  92. float r2y = r2.Y;
  93. float denom = r1y*r2x - r1x*r2y;
  94. float p1x = p1.X;
  95. float p1y = p1.Y;
  96. float p2x = p2.X;
  97. float p2y = p2.Y;
  98. float z1 = -p2x*r2y + p1x*r2y + (p2y - p1y)*r2x;
  99. float z2 = -p2x*r1y + p1x*r1y + (p2y - p1y)*r1x;
  100. if (denom == 0.0f) // Means the straights are parallel. Either no intersection or an infinite number of them
  101. {
  102. if (z1 == 0.0f)
  103. {
  104. // Means they are identical -> many, many intersections
  105. lambda = Single.NaN;
  106. mu = Single.NaN;
  107. }
  108. else
  109. {
  110. lambda = Single.PositiveInfinity;
  111. mu = Single.PositiveInfinity;
  112. }
  113. return;
  114. }
  115. lambda = z1/denom;
  116. mu = z2/denom;
  117. }
  118. // Intersects the simplex with another one.
  119. // the borders are used to deal with float inaccuracies
  120. // As a rule of thumb, the borders are
  121. // lowerBorder1 : 0.0
  122. // lowerBorder2 : 0.0
  123. // upperBorder1 : 1.0
  124. // upperBorder2 : 1.0
  125. // 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)
  126. public static PhysicsVector Intersect(
  127. Simplex s1,
  128. Simplex s2,
  129. float lowerBorder1,
  130. float lowerBorder2,
  131. float upperBorder1,
  132. float upperBorder2)
  133. {
  134. PhysicsVector firstSimplexDirection = s1.v2 - s1.v1;
  135. PhysicsVector secondSimplexDirection = s2.v2 - s2.v1;
  136. float lambda = 0.0f;
  137. float mu = 0.0f;
  138. // Give us the parameters of an intersection. This subroutine does *not* take the constraints
  139. // (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
  140. // into account. We do that afterwards.
  141. intersectParameter(s1.v1, firstSimplexDirection, s2.v1, secondSimplexDirection, ref lambda, ref mu);
  142. if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
  143. return null;
  144. if (Single.IsNaN(lambda)) // Special case. many, many intersections.
  145. return null;
  146. if (lambda > upperBorder1) // We're behind v2
  147. return null;
  148. if (lambda < lowerBorder1)
  149. return null;
  150. if (mu < lowerBorder2) // outside simplex 2
  151. return null;
  152. if (mu > upperBorder2) // outside simplex 2
  153. return null;
  154. return s1.v1 + lambda*firstSimplexDirection;
  155. }
  156. // Intersects the simplex with a ray. The ray is defined as all p=origin + lambda*direction
  157. // where lambda >= 0
  158. public PhysicsVector RayIntersect(Vertex origin, PhysicsVector direction, bool bEndsIncluded)
  159. {
  160. PhysicsVector simplexDirection = v2 - v1;
  161. float lambda = 0.0f;
  162. float mu = 0.0f;
  163. // Give us the parameters of an intersection. This subroutine does *not* take the constraints
  164. // (intersection must be between v1 and v2 and it must be in the positive direction of the ray)
  165. // into account. We do that afterwards.
  166. intersectParameter(v1, simplexDirection, origin, direction, ref lambda, ref mu);
  167. if (Single.IsInfinity(lambda)) // Special case. No intersection at all. directions parallel.
  168. return null;
  169. if (Single.IsNaN(lambda)) // Special case. many, many intersections.
  170. return null;
  171. if (mu < 0.0) // We're on the wrong side of the ray
  172. return null;
  173. if (lambda > 1.0) // We're behind v2
  174. return null;
  175. if (lambda == 1.0 && !bEndsIncluded)
  176. return null; // The end of the simplices are not included
  177. if (lambda < 0.0f) // we're before v1;
  178. return null;
  179. return v1 + lambda*simplexDirection;
  180. }
  181. }
  182. }