HullCreation.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 OpenSimulator 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 System.Linq;
  30. using System.Text;
  31. using NUnit.Framework;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.PhysicsModule.BulletS;
  35. using OpenSim.Region.PhysicsModules.SharedBase;
  36. using OpenSim.Tests.Common;
  37. using OpenMetaverse;
  38. namespace OpenSim.Region.PhysicsModule.BulletS.Tests
  39. {
  40. [TestFixture]
  41. public class HullCreation : OpenSimTestCase
  42. {
  43. // Documentation on attributes: http://www.nunit.org/index.php?p=attributes&r=2.6.1
  44. // Documentation on assertions: http://www.nunit.org/index.php?p=assertions&r=2.6.1
  45. BSScene PhysicsScene { get; set; }
  46. Vector3 ObjectInitPosition;
  47. [TestFixtureSetUp]
  48. public void Init()
  49. {
  50. }
  51. [TestFixtureTearDown]
  52. public void TearDown()
  53. {
  54. if (PhysicsScene != null)
  55. {
  56. // The Dispose() will also free any physical objects in the scene
  57. PhysicsScene.Dispose();
  58. PhysicsScene = null;
  59. }
  60. }
  61. [TestCase(7, 2, 5f, 5f, 32, 0f)] /* default hull parameters */
  62. public void GeomHullConvexDecomp( int maxDepthSplit,
  63. int maxDepthSplitForSimpleShapes,
  64. float concavityThresholdPercent,
  65. float volumeConservationThresholdPercent,
  66. int maxVertices,
  67. float maxSkinWidth)
  68. {
  69. // Setup the physics engine to use the C# version of convex decomp
  70. Dictionary<string, string> engineParams = new Dictionary<string, string>();
  71. engineParams.Add("MeshSculptedPrim", "true"); // ShouldMeshSculptedPrim
  72. engineParams.Add("ForceSimplePrimMeshing", "false"); // ShouldForceSimplePrimMeshing
  73. engineParams.Add("UseHullsForPhysicalObjects", "true"); // ShouldUseHullsForPhysicalObjects
  74. engineParams.Add("ShouldRemoveZeroWidthTriangles", "true");
  75. engineParams.Add("ShouldUseBulletHACD", "false");
  76. engineParams.Add("ShouldUseSingleConvexHullForPrims", "true");
  77. engineParams.Add("ShouldUseGImpactShapeForPrims", "false");
  78. engineParams.Add("ShouldUseAssetHulls", "true");
  79. engineParams.Add("CSHullMaxDepthSplit", maxDepthSplit.ToString());
  80. engineParams.Add("CSHullMaxDepthSplitForSimpleShapes", maxDepthSplitForSimpleShapes.ToString());
  81. engineParams.Add("CSHullConcavityThresholdPercent", concavityThresholdPercent.ToString());
  82. engineParams.Add("CSHullVolumeConservationThresholdPercent", volumeConservationThresholdPercent.ToString());
  83. engineParams.Add("CSHullMaxVertices", maxVertices.ToString());
  84. engineParams.Add("CSHullMaxSkinWidth", maxSkinWidth.ToString());
  85. PhysicsScene = BulletSimTestsUtil.CreateBasicPhysicsEngine(engineParams);
  86. PrimitiveBaseShape pbs;
  87. Vector3 pos;
  88. Vector3 size;
  89. Quaternion rot;
  90. bool isPhys;
  91. // Cylinder
  92. pbs = PrimitiveBaseShape.CreateCylinder();
  93. pos = new Vector3(100.0f, 100.0f, 0f);
  94. pos.Z = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos) + 10f;
  95. ObjectInitPosition = pos;
  96. size = new Vector3(2f, 2f, 2f);
  97. pbs.Scale = size;
  98. rot = Quaternion.Identity;
  99. isPhys = true;
  100. uint cylinderLocalID = 123;
  101. PhysicsScene.AddPrimShape("testCylinder", pbs, pos, size, rot, isPhys, cylinderLocalID);
  102. BSPrim primTypeCylinder = (BSPrim)PhysicsScene.PhysObjects[cylinderLocalID];
  103. // Hollow Cylinder
  104. pbs = PrimitiveBaseShape.CreateCylinder();
  105. pbs.ProfileHollow = (ushort)(0.70f * 50000);
  106. pos = new Vector3(110.0f, 110.0f, 0f);
  107. pos.Z = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos) + 10f;
  108. ObjectInitPosition = pos;
  109. size = new Vector3(2f, 2f, 2f);
  110. pbs.Scale = size;
  111. rot = Quaternion.Identity;
  112. isPhys = true;
  113. uint hollowCylinderLocalID = 124;
  114. PhysicsScene.AddPrimShape("testHollowCylinder", pbs, pos, size, rot, isPhys, hollowCylinderLocalID);
  115. BSPrim primTypeHollowCylinder = (BSPrim)PhysicsScene.PhysObjects[hollowCylinderLocalID];
  116. // Torus
  117. // ProfileCurve = Circle, PathCurve = Curve1
  118. pbs = PrimitiveBaseShape.CreateSphere();
  119. pbs.ProfileShape = (byte)ProfileShape.Circle;
  120. pbs.PathCurve = (byte)Extrusion.Curve1;
  121. pbs.PathScaleX = 100; // default hollow info as set in the viewer
  122. pbs.PathScaleY = (int)(.25f / 0.01f) + 200;
  123. pos = new Vector3(120.0f, 120.0f, 0f);
  124. pos.Z = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos) + 10f;
  125. ObjectInitPosition = pos;
  126. size = new Vector3(2f, 4f, 4f);
  127. pbs.Scale = size;
  128. rot = Quaternion.Identity;
  129. isPhys = true;
  130. uint torusLocalID = 125;
  131. PhysicsScene.AddPrimShape("testTorus", pbs, pos, size, rot, isPhys, torusLocalID);
  132. BSPrim primTypeTorus = (BSPrim)PhysicsScene.PhysObjects[torusLocalID];
  133. // The actual prim shape creation happens at taint time
  134. PhysicsScene.ProcessTaints();
  135. // Check out the created hull shapes and report their characteristics
  136. ReportShapeGeom(primTypeCylinder);
  137. ReportShapeGeom(primTypeHollowCylinder);
  138. ReportShapeGeom(primTypeTorus);
  139. }
  140. [TestCase]
  141. public void GeomHullBulletHACD()
  142. {
  143. // Cylinder
  144. // Hollow Cylinder
  145. // Torus
  146. }
  147. private void ReportShapeGeom(BSPrim prim)
  148. {
  149. if (prim != null)
  150. {
  151. if (prim.PhysShape.HasPhysicalShape)
  152. {
  153. BSShape physShape = prim.PhysShape;
  154. string shapeType = physShape.GetType().ToString();
  155. switch (shapeType)
  156. {
  157. case "OpenSim.Region.Physics.BulletSPlugin.BSShapeNative":
  158. BSShapeNative nShape = physShape as BSShapeNative;
  159. prim.PhysScene.DetailLog("{0}, type={1}", prim.Name, shapeType);
  160. break;
  161. case "OpenSim.Region.Physics.BulletSPlugin.BSShapeMesh":
  162. BSShapeMesh mShape = physShape as BSShapeMesh;
  163. prim.PhysScene.DetailLog("{0}, mesh, shapeInfo={1}", prim.Name, mShape.shapeInfo);
  164. break;
  165. case "OpenSim.Region.Physics.BulletSPlugin.BSShapeHull":
  166. // BSShapeHull hShape = physShape as BSShapeHull;
  167. // prim.PhysScene.DetailLog("{0}, hull, shapeInfo={1}", prim.Name, hShape.shapeInfo);
  168. break;
  169. case "OpenSim.Region.Physics.BulletSPlugin.BSShapeConvexHull":
  170. BSShapeConvexHull chShape = physShape as BSShapeConvexHull;
  171. prim.PhysScene.DetailLog("{0}, convexHull, shapeInfo={1}", prim.Name, chShape.shapeInfo);
  172. break;
  173. case "OpenSim.Region.Physics.BulletSPlugin.BSShapeCompound":
  174. BSShapeCompound cShape = physShape as BSShapeCompound;
  175. prim.PhysScene.DetailLog("{0}, type={1}", prim.Name, shapeType);
  176. break;
  177. default:
  178. prim.PhysScene.DetailLog("{0}, type={1}", prim.Name, shapeType);
  179. break;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }