ShapeOps.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //---------------------------------------------------------------------------------------------------------------------
  2. // ShapeOps.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //---------------------------------------------------------------------------------------------------------------------
  7. using System;
  8. using CollisionSample;
  9. using Microsoft.Xna.Framework;
  10. namespace UnitTests
  11. {
  12. /// <summary>
  13. /// IShapeOps provides an interface to consistently manipulate various types of bounding shapes,
  14. /// to avoid duplication of complex test code. Note that this is not a complete and generic
  15. /// set of operations; it only includes methods necessary to implement our test cases.
  16. /// </summary>
  17. interface IShapeOps<T>
  18. {
  19. // The shape being managed by this instance
  20. T Shape { get; set; }
  21. // Initialize Shape to a randomly sized, positioned, and oriented new object.
  22. // The "scale" parameter should provide an approximate size.
  23. void CreateRandomShape(Random random, float scale);
  24. // Move Shape by the given vector
  25. void Translate(Vector3 translation);
  26. // Scale shape uniformly around the origin by the given amount
  27. void Scale(float scale);
  28. // Return a point from inside Shape chosen uniformly at random
  29. Vector3 RandomInteriorPoint(Random random);
  30. // Returns true iff the given point is inside Shape
  31. bool ContainsPoint(Vector3 point);
  32. // Return an array of points whose bounding hull contains Shape.
  33. Vector3[] GetHull();
  34. // Return the minimum distance from Shape to the given plane
  35. // (which will be negative if Shape intersects the halfspace
  36. // defined by the plane.)
  37. float MinimumDistanceFromPlane(Plane plane);
  38. }
  39. // Implement IShapeOps for BoundingSphere
  40. class BoundingSphereOps : IShapeOps<BoundingSphere>
  41. {
  42. BoundingSphere shape;
  43. public BoundingSphere Shape { get { return shape; } set { shape = value; } }
  44. public override string ToString()
  45. {
  46. return String.Format("BoundingSphere(new Vector3({0},{1},{2}), {3})", shape.Center.X, shape.Center.Y, shape.Center.Z, shape.Radius);
  47. }
  48. public void CreateRandomShape(Random random, float scale)
  49. {
  50. shape.Radius = random.NextFloat(0, scale);
  51. shape.Center = random.PointInCube() * scale;
  52. }
  53. public void Translate(Vector3 translation)
  54. {
  55. shape.Center += translation;
  56. }
  57. public void Scale(float scale)
  58. {
  59. shape.Radius *= scale;
  60. shape.Center *= scale;
  61. }
  62. public Vector3 RandomInteriorPoint(Random random)
  63. {
  64. return random.PointInSphere() * shape.Radius + shape.Center;
  65. }
  66. public bool ContainsPoint(Vector3 point)
  67. {
  68. return shape.Contains(point) != ContainmentType.Disjoint;
  69. }
  70. public Vector3[] GetHull()
  71. {
  72. Vector3 c = shape.Center;
  73. float r = shape.Radius;
  74. return new Vector3[]
  75. {
  76. c + new Vector3(r, r, r),
  77. c + new Vector3(-r, r, r),
  78. c + new Vector3(r, -r, r),
  79. c + new Vector3(-r, -r, r),
  80. c + new Vector3(r, r, -r),
  81. c + new Vector3(-r, r, -r),
  82. c + new Vector3(r, -r, -r),
  83. c + new Vector3(-r, -r, -r)
  84. };
  85. }
  86. public float MinimumDistanceFromPlane(Plane plane)
  87. {
  88. return plane.DotCoordinate(shape.Center) - shape.Radius;
  89. }
  90. }
  91. // Implement IShapeOps for Triangle
  92. class TriangleOps : IShapeOps<Triangle>
  93. {
  94. Triangle shape;
  95. public Triangle Shape { get { return shape; } set { shape = value; } }
  96. public override string ToString()
  97. {
  98. return String.Format("Triangle(new Vector3({0},{1},{2}), new Vector3({3},{4},{5}), new Vector3({6},{7},{8}))",
  99. shape.V0.X, shape.V0.Y, shape.V0.Z,
  100. shape.V1.X, shape.V1.Y, shape.V1.Z,
  101. shape.V2.X, shape.V2.Y, shape.V2.Z);
  102. }
  103. public void CreateRandomShape(Random random, float scale)
  104. {
  105. shape.V0 = random.PointInCube() * scale;
  106. shape.V1 = random.PointInCube() * scale;
  107. shape.V2 = random.PointInCube() * scale;
  108. }
  109. public void Translate(Vector3 translation)
  110. {
  111. shape.V0 += translation;
  112. shape.V1 += translation;
  113. shape.V2 += translation;
  114. }
  115. public void Scale(float scale)
  116. {
  117. shape.V0 *= scale;
  118. shape.V1 *= scale;
  119. shape.V2 *= scale;
  120. }
  121. public Vector3 RandomInteriorPoint(Random random)
  122. {
  123. float u = (float)random.NextDouble();
  124. float v = (float)random.NextDouble();
  125. if (u + v > 1.0f)
  126. {
  127. u = 1 - u;
  128. v = 1 - v;
  129. }
  130. return (1 - u - v) * shape.V0 + u * shape.V1 + v * shape.V2;
  131. }
  132. public bool ContainsPoint(Vector3 point)
  133. {
  134. throw new InvalidOperationException();
  135. }
  136. public Vector3[] GetHull()
  137. {
  138. return new Vector3[] { shape.V0, shape.V1, shape.V2 };
  139. }
  140. public float MinimumDistanceFromPlane(Plane plane)
  141. {
  142. float d = float.MaxValue;
  143. foreach (Vector3 v in GetHull())
  144. d = Math.Min(d, plane.DotCoordinate(v));
  145. return d;
  146. }
  147. }
  148. // Implement IShapeOps for BoundingOrientedBox
  149. class BoundingOrientedBoxOps : IShapeOps<BoundingOrientedBox>
  150. {
  151. BoundingOrientedBox shape;
  152. public BoundingOrientedBox Shape { get { return shape; } set { shape = value; } }
  153. public void CreateRandomShape(Random random, float scale)
  154. {
  155. shape.Center = random.PointInCube() * scale;
  156. shape.HalfExtent = (random.PointInCube() + Vector3.One * 1.01f) * scale * 0.25f;
  157. shape.Orientation = random.Orientation();
  158. }
  159. public void Translate(Vector3 translation)
  160. {
  161. shape.Center += translation;
  162. }
  163. public void Scale(float scale)
  164. {
  165. shape.HalfExtent *= scale;
  166. shape.Center *= scale;
  167. }
  168. public Vector3 RandomInteriorPoint(Random random)
  169. {
  170. Vector3 p = new Vector3(
  171. random.NextFloat(-shape.HalfExtent.X, shape.HalfExtent.X),
  172. random.NextFloat(-shape.HalfExtent.Y, shape.HalfExtent.Y),
  173. random.NextFloat(-shape.HalfExtent.Z, shape.HalfExtent.Z));
  174. Vector3.Transform(ref p, ref shape.Orientation, out p);
  175. Vector3.Add(ref p, ref shape.Center, out p);
  176. return p;
  177. }
  178. public bool ContainsPoint(Vector3 point)
  179. {
  180. return shape.Contains(ref point);
  181. }
  182. public Vector3[] GetHull()
  183. {
  184. return shape.GetCorners();
  185. }
  186. public float MinimumDistanceFromPlane(Plane plane)
  187. {
  188. float d = float.MaxValue;
  189. foreach (Vector3 v in GetHull())
  190. d = Math.Min(d, plane.DotCoordinate(v));
  191. return d;
  192. }
  193. }
  194. // Implement IShapeOps for BoundingBox
  195. class BoundingBoxOps : IShapeOps<BoundingBox>
  196. {
  197. BoundingBox shape;
  198. public BoundingBox Shape { get { return shape; } set { shape = value; } }
  199. public void CreateRandomShape(Random random, float scale)
  200. {
  201. Vector3 center = random.PointInCube() * scale;
  202. Vector3 size = (random.PointInCube() + Vector3.One * 1.01f) * scale * 0.25f;
  203. shape.Min = center - size;
  204. shape.Max = center + size;
  205. }
  206. public void SetRandom(Vector3 position, Vector3 scale, Quaternion orientation)
  207. {
  208. shape = new BoundingBox(position - scale, position + scale);
  209. }
  210. public void Translate(Vector3 translation)
  211. {
  212. shape.Min += translation;
  213. shape.Max += translation;
  214. }
  215. public void Scale(float scale)
  216. {
  217. shape.Min *= scale;
  218. shape.Max *= scale;
  219. }
  220. public Vector3 RandomInteriorPoint(Random random)
  221. {
  222. Vector3 p = new Vector3(
  223. random.NextFloat(shape.Min.X, shape.Max.X),
  224. random.NextFloat(shape.Min.Y, shape.Max.Y),
  225. random.NextFloat(shape.Min.Z, shape.Max.Z));
  226. return p;
  227. }
  228. public bool ContainsPoint(Vector3 point)
  229. {
  230. return shape.Contains(point) != ContainmentType.Disjoint;
  231. }
  232. public Vector3[] GetHull()
  233. {
  234. return shape.GetCorners();
  235. }
  236. public float MinimumDistanceFromPlane(Plane plane)
  237. {
  238. float d = float.MaxValue;
  239. foreach (Vector3 v in GetHull())
  240. d = Math.Min(d, plane.DotCoordinate(v));
  241. return d;
  242. }
  243. }
  244. }