SimpleHull.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 System.Collections.Generic;
  29. using OpenSim.Region.Physics.Manager;
  30. namespace OpenSim.Region.Physics.Meshing
  31. {
  32. // A simple hull is a set of vertices building up to simplices that border a region
  33. // The word simple referes to the fact, that this class assumes, that all simplices
  34. // do not intersect
  35. // Simple hulls can be added and subtracted.
  36. // Vertices can be checked to lie inside a hull
  37. // Also note, that the sequence of the vertices is important and defines if the region that
  38. // is defined by the hull lies inside or outside the simplex chain
  39. public class SimpleHull
  40. {
  41. //private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  42. private List<Vertex> vertices = new List<Vertex>();
  43. private List<Vertex> holeVertices = new List<Vertex>(); // Only used, when the hull is hollow
  44. // Adds a vertex to the end of the list
  45. public void AddVertex(Vertex v)
  46. {
  47. vertices.Add(v);
  48. }
  49. public override String ToString()
  50. {
  51. String result = String.Empty;
  52. foreach (Vertex v in vertices)
  53. {
  54. result += "b:" + v.ToString() + "\n";
  55. }
  56. return result;
  57. }
  58. public List<Vertex> getVertices()
  59. {
  60. List<Vertex> newVertices = new List<Vertex>();
  61. newVertices.AddRange(vertices);
  62. newVertices.Add(null);
  63. newVertices.AddRange(holeVertices);
  64. return newVertices;
  65. }
  66. public SimpleHull Clone()
  67. {
  68. SimpleHull result = new SimpleHull();
  69. foreach (Vertex v in vertices)
  70. {
  71. result.AddVertex(v.Clone());
  72. }
  73. foreach (Vertex v in holeVertices)
  74. {
  75. result.holeVertices.Add(v.Clone());
  76. }
  77. return result;
  78. }
  79. public bool IsPointIn(Vertex v1)
  80. {
  81. int iCounter = 0;
  82. List<Simplex> simplices = buildSimplexList();
  83. foreach (Simplex s in simplices)
  84. {
  85. // Send a ray along the positive X-Direction
  86. // Note, that this direction must correlate with the "below" interpretation
  87. // of handling for the special cases below
  88. PhysicsVector intersection = s.RayIntersect(v1, new PhysicsVector(1.0f, 0.0f, 0.0f), true);
  89. if (intersection == null)
  90. continue; // No intersection. Done. More tests to follow otherwise
  91. // Did we hit the end of a simplex?
  92. // Then this can be one of two special cases:
  93. // 1. we go through a border exactly at a joint
  94. // 2. we have just marginally touched a corner
  95. // 3. we can slide along a border
  96. // Solution: If the other vertex is "below" the ray, we don't count it
  97. // Thus corners pointing down are counted twice, corners pointing up are not counted
  98. // borders are counted once
  99. if (intersection.IsIdentical(s.v1, 0.001f))
  100. {
  101. if (s.v2.Y < v1.Y)
  102. continue;
  103. }
  104. // Do this for the other vertex two
  105. if (intersection.IsIdentical(s.v2, 0.001f))
  106. {
  107. if (s.v1.Y < v1.Y)
  108. continue;
  109. }
  110. iCounter++;
  111. }
  112. return iCounter%2 == 1; // Point is inside if the number of intersections is odd
  113. }
  114. public bool containsPointsFrom(SimpleHull otherHull)
  115. {
  116. foreach (Vertex v in otherHull.vertices)
  117. {
  118. if (IsPointIn(v))
  119. return true;
  120. }
  121. return false;
  122. }
  123. private List<Simplex> buildSimplexList()
  124. {
  125. List<Simplex> result = new List<Simplex>();
  126. // Not asserted but assumed: at least three vertices
  127. for (int i = 0; i < vertices.Count - 1; i++)
  128. {
  129. Simplex s = new Simplex(vertices[i], vertices[i + 1]);
  130. result.Add(s);
  131. }
  132. Simplex s1 = new Simplex(vertices[vertices.Count - 1], vertices[0]);
  133. result.Add(s1);
  134. if (holeVertices.Count == 0)
  135. return result;
  136. // Same here. At least three vertices in hole assumed
  137. for (int i = 0; i < holeVertices.Count - 1; i++)
  138. {
  139. Simplex s = new Simplex(holeVertices[i], holeVertices[i + 1]);
  140. result.Add(s);
  141. }
  142. s1 = new Simplex(holeVertices[holeVertices.Count - 1], holeVertices[0]);
  143. result.Add(s1);
  144. return result;
  145. }
  146. // TODO: unused
  147. // private bool InsertVertex(Vertex v, int iAfter)
  148. // {
  149. // vertices.Insert(iAfter + 1, v);
  150. // return true;
  151. // }
  152. private Vertex getNextVertex(Vertex currentVertex)
  153. {
  154. int iCurrentIndex;
  155. iCurrentIndex = vertices.IndexOf(currentVertex);
  156. // Error handling for iCurrentIndex==-1 should go here (and probably never will)
  157. iCurrentIndex++;
  158. if (iCurrentIndex == vertices.Count)
  159. iCurrentIndex = 0;
  160. return vertices[iCurrentIndex];
  161. }
  162. public Vertex FindVertex(Vertex vBase, float tolerance)
  163. {
  164. foreach (Vertex v in vertices)
  165. {
  166. if (v.IsIdentical(vBase, tolerance))
  167. return v;
  168. }
  169. return null;
  170. }
  171. public void FindIntersection(Simplex s, ref Vertex Intersection, ref Vertex nextVertex)
  172. {
  173. Vertex bestIntersection = null;
  174. float distToV1 = Single.PositiveInfinity;
  175. Simplex bestIntersectingSimplex = null;
  176. List<Simplex> simple = buildSimplexList();
  177. foreach (Simplex sTest in simple)
  178. {
  179. PhysicsVector vvTemp = Simplex.Intersect(sTest, s, -.001f, -.001f, 0.999f, .999f);
  180. Vertex vTemp = null;
  181. if (vvTemp != null)
  182. vTemp = new Vertex(vvTemp);
  183. if (vTemp != null)
  184. {
  185. PhysicsVector diff = (s.v1 - vTemp);
  186. float distTemp = diff.length();
  187. if (bestIntersection == null || distTemp < distToV1)
  188. {
  189. bestIntersection = vTemp;
  190. distToV1 = distTemp;
  191. bestIntersectingSimplex = sTest;
  192. }
  193. }
  194. }
  195. Intersection = bestIntersection;
  196. if (bestIntersectingSimplex != null)
  197. nextVertex = bestIntersectingSimplex.v2;
  198. else
  199. nextVertex = null;
  200. }
  201. public static SimpleHull SubtractHull(SimpleHull baseHull, SimpleHull otherHull)
  202. {
  203. SimpleHull baseHullClone = baseHull.Clone();
  204. SimpleHull otherHullClone = otherHull.Clone();
  205. bool intersects = false;
  206. //m_log.Debug("State before intersection detection");
  207. //m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
  208. //m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
  209. {
  210. int iBase, iOther;
  211. // Insert into baseHull
  212. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  213. {
  214. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  215. Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
  216. for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
  217. {
  218. int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
  219. Simplex sOther =
  220. new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
  221. PhysicsVector intersect = Simplex.Intersect(sBase, sOther, 0.001f, -.001f, 0.999f, 1.001f);
  222. if (intersect != null)
  223. {
  224. Vertex vIntersect = new Vertex(intersect);
  225. baseHullClone.vertices.Insert(iBase + 1, vIntersect);
  226. sBase.v2 = vIntersect;
  227. intersects = true;
  228. }
  229. }
  230. }
  231. }
  232. //m_log.Debug("State after intersection detection for the base hull");
  233. //m_log.DebugFormat("The baseHull is:\n{1}", 0, baseHullClone.ToString());
  234. {
  235. int iOther, iBase;
  236. // Insert into otherHull
  237. for (iOther = 0; iOther < otherHullClone.vertices.Count; iOther++)
  238. {
  239. int iOtherNext = (iOther + 1)%otherHullClone.vertices.Count;
  240. Simplex sOther = new Simplex(otherHullClone.vertices[iOther], otherHullClone.vertices[iOtherNext]);
  241. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  242. {
  243. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  244. Simplex sBase = new Simplex(baseHullClone.vertices[iBase], baseHullClone.vertices[iBaseNext]);
  245. PhysicsVector intersect = Simplex.Intersect(sBase, sOther, -.001f, 0.001f, 1.001f, 0.999f);
  246. if (intersect != null)
  247. {
  248. Vertex vIntersect = new Vertex(intersect);
  249. otherHullClone.vertices.Insert(iOther + 1, vIntersect);
  250. sOther.v2 = vIntersect;
  251. intersects = true;
  252. }
  253. }
  254. }
  255. }
  256. //m_log.Debug("State after intersection detection for the base hull");
  257. //m_log.DebugFormat("The otherHull is:\n{1}", 0, otherHullClone.ToString());
  258. bool otherIsInBase = baseHullClone.containsPointsFrom(otherHullClone);
  259. if (!intersects && otherIsInBase)
  260. {
  261. // We have a hole here
  262. baseHullClone.holeVertices = otherHullClone.vertices;
  263. return baseHullClone;
  264. }
  265. SimpleHull result = new SimpleHull();
  266. // Find a good starting Simplex from baseHull
  267. // A good starting simplex is one that is outside otherHull
  268. // Such a simplex must exist, otherwise the result will be empty
  269. Vertex baseStartVertex = null;
  270. {
  271. int iBase;
  272. for (iBase = 0; iBase < baseHullClone.vertices.Count; iBase++)
  273. {
  274. int iBaseNext = (iBase + 1)%baseHullClone.vertices.Count;
  275. Vertex center = new Vertex((baseHullClone.vertices[iBase] + baseHullClone.vertices[iBaseNext])/2.0f);
  276. bool isOutside = !otherHullClone.IsPointIn(center);
  277. if (isOutside)
  278. {
  279. baseStartVertex = baseHullClone.vertices[iBaseNext];
  280. break;
  281. }
  282. }
  283. }
  284. if (baseStartVertex == null) // i.e. no simplex fulfilled the "outside" condition.
  285. // In otherwords, subtractHull completely embraces baseHull
  286. {
  287. return result;
  288. }
  289. // The simplex that *starts* with baseStartVertex is outside the cutting hull,
  290. // so we can start our walk with the next vertex without loosing a branch
  291. Vertex V1 = baseStartVertex;
  292. bool onBase = true;
  293. // And here is how we do the magic :-)
  294. // Start on the base hull.
  295. // Walk the vertices in the positive direction
  296. // For each vertex check, whether it is a vertex shared with the other hull
  297. // if this is the case, switch over to walking the other vertex list.
  298. // Note: The other hull *must* go backwards to our starting point (via several orther vertices)
  299. // Thus it is important that the cutting hull has the inverse directional sense than the
  300. // base hull!!!!!!!!! (means if base goes CW around it's center cutting hull must go CCW)
  301. bool done = false;
  302. while (!done)
  303. {
  304. result.AddVertex(V1);
  305. Vertex nextVertex = null;
  306. if (onBase)
  307. {
  308. nextVertex = otherHullClone.FindVertex(V1, 0.001f);
  309. }
  310. else
  311. {
  312. nextVertex = baseHullClone.FindVertex(V1, 0.001f);
  313. }
  314. if (nextVertex != null) // A node that represents an intersection
  315. {
  316. V1 = nextVertex; // Needed to find the next vertex on the other hull
  317. onBase = !onBase;
  318. }
  319. if (onBase)
  320. V1 = baseHullClone.getNextVertex(V1);
  321. else
  322. V1 = otherHullClone.getNextVertex(V1);
  323. if (V1 == baseStartVertex)
  324. done = true;
  325. }
  326. //m_log.DebugFormat("The resulting Hull is:\n{1}", 0, result.ToString());
  327. return result;
  328. }
  329. }
  330. }