bool ISimulationDataPhysicsConvexStorageInterface.TryGetValue(ObjectPart.PrimitiveShape primShape, out PhysicsConvexShape shape) { using (var conn = new NpgsqlConnection(m_ConnectionString)) { conn.Open(); using (var cmd = new NpgsqlCommand("SELECT \"ConvexData\" FROM primphysics WHERE \"ShapeKey\"=@id LIMIT 1", conn)) { cmd.Parameters.AddParameter("@id", primShape.Serialization); using (NpgsqlDataReader dbReader = cmd.ExecuteReader()) { if (dbReader.Read()) { shape = new PhysicsConvexShape { SerializedData = dbReader.GetBytes("ConvexData") }; return(true); } } } } shape = null; return(false); }
public bool Run() { PhysicsShapeReference physicsShapeRef; DumpParams(m_Shape); ObjectPart.PrimitiveShape ps = new ObjectPart.PrimitiveShape { DecodedParams = m_Shape }; m_Log.Info("---- Transformed to physics shape manager accepted format ----"); m_Log.InfoFormat("PhysicsShapeType: {0}", m_PhysicsShapeType); DumpParams(ps.DecodedParams); if (!m_PhysicsShapeManager.TryGetConvexShape(m_PhysicsShapeType, ps, out physicsShapeRef)) { m_Log.Error("Could not generate physics hull shape"); return(false); } PhysicsConvexShape convexShape = physicsShapeRef; int hullidx = 0; foreach (PhysicsConvexShape.ConvexHull hull in convexShape.Hulls) { m_Log.InfoFormat("Hull {0}: Generated vertices: {1}", hullidx, hull.Vertices.Count); m_Log.InfoFormat("Hull {0}: Generated triangles: {1}", hullidx, hull.Triangles.Count / 3); ++hullidx; } m_Log.InfoFormat("Generated hulls: {0}", hullidx); /* write a blender .raw */ ((PhysicsConvexShape)physicsShapeRef).DumpToBlenderRaw(m_OutputFileName); return(true); }
bool ISimulationDataPhysicsConvexStorageInterface.TryGetValue(UUID meshid, PrimitivePhysicsShapeType physicsShape, out PhysicsConvexShape shape) { using (var conn = new NpgsqlConnection(m_ConnectionString)) { conn.Open(); using (var cmd = new NpgsqlCommand("SELECT \"ConvexData\" FROM meshphysics WHERE \"MeshID\"=@id AND \"PhysicsShape\"=@stype LIMIT 1", conn)) { cmd.Parameters.AddParameter("@id", meshid); cmd.Parameters.AddParameter("@stype", physicsShape); using (NpgsqlDataReader dbReader = cmd.ExecuteReader()) { if (dbReader.Read()) { shape = new PhysicsConvexShape { SerializedData = dbReader.GetBytes("ConvexData") }; return(true); } } } } shape = null; return(false); }
internal void DecrementUseCount(ObjectPart.PrimitiveShape primshape, PhysicsConvexShape shape) { if (0 == Interlocked.Decrement(ref shape.UseCount)) { m_Lock.AcquireWriterLock(() => m_ConvexShapesByPrimShape.RemoveIf(primshape, (PhysicsConvexShape s) => s.UseCount == 0)); } }
public override bool Run() { ISimulationDataPhysicsConvexStorageInterface m_PhysicsStorage = SimulationData.PhysicsConvexShapes; var shape = new PhysicsConvexShape(); var hull = new PhysicsConvexShape.ConvexHull(); hull.Vertices.Add(Vector3.UnitX); hull.Vertices.Add(Vector3.UnitY); hull.Vertices.Add(Vector3.UnitZ); hull.Triangles.Add(0); hull.Triangles.Add(1); hull.Triangles.Add(2); shape.Hulls.Add(hull); var shapeKey = new ObjectPart.PrimitiveShape(); PhysicsConvexShape test; m_Log.Info("Testing non-existence 1"); if (m_PhysicsStorage.ContainsKey(shapeKey)) { return(false); } m_Log.Info("Testing non-existence 2"); if (m_PhysicsStorage.TryGetValue(shapeKey, out test)) { return(false); } m_Log.Info("Testing non-existence 3"); try { shape = m_PhysicsStorage[shapeKey]; return(false); } catch (KeyNotFoundException) { /* we should see this one */ } m_Log.Info("Testing non-existence 4"); if (m_PhysicsStorage.Remove(shapeKey)) { return(false); } m_Log.Info("Create physics convex shape"); m_PhysicsStorage[shapeKey] = shape; m_Log.Info("Testing existence 1"); if (!m_PhysicsStorage.ContainsKey(shapeKey)) { return(false); } m_Log.Info("Testing existence 2"); if (!m_PhysicsStorage.TryGetValue(shapeKey, out test)) { return(false); } m_Log.Info("Testing equality"); if (!test.SerializedData.SequenceEqual(shape.SerializedData)) { return(false); } m_Log.Info("Testing existence 3"); shape = m_PhysicsStorage[shapeKey]; m_Log.Info("Testing equality"); if (!test.SerializedData.SequenceEqual(shape.SerializedData)) { return(false); } m_Log.Info("Testing existence 4"); if (!m_PhysicsStorage.Remove(shapeKey)) { return(false); } return(true); }
public bool Run() { PhysicsShapeReference physicsShapeRef; bool success = true; foreach (PrimitiveShapeType shapeType in new PrimitiveShapeType[] { PrimitiveShapeType.Box, PrimitiveShapeType.Cylinder, PrimitiveShapeType.Prism, PrimitiveShapeType.Ring, PrimitiveShapeType.Sphere, PrimitiveShapeType.Torus, PrimitiveShapeType.Tube }) { foreach (PrimitiveProfileShape profileShape in new PrimitiveProfileShape[] { PrimitiveProfileShape.Circle, PrimitiveProfileShape.EquilateralTriangle, PrimitiveProfileShape.HalfCircle, PrimitiveProfileShape.IsometricTriangle, PrimitiveProfileShape.RightTriangle, PrimitiveProfileShape.Square }) { foreach (PrimitiveProfileHollowShape hollowShape in new PrimitiveProfileHollowShape[] { PrimitiveProfileHollowShape.Circle, PrimitiveProfileHollowShape.Same, PrimitiveProfileHollowShape.Square, PrimitiveProfileHollowShape.Triangle }) { foreach (PrimitivePhysicsShapeType physicsShapeType in new PrimitivePhysicsShapeType[] { PrimitivePhysicsShapeType.Convex, PrimitivePhysicsShapeType.Prim }) { foreach (PrimitiveExtrusion pathCurve in new PrimitiveExtrusion[] { PrimitiveExtrusion.Curve1, PrimitiveExtrusion.Curve2, PrimitiveExtrusion.Default, PrimitiveExtrusion.Straight }) { for (double pathbegin = 0; pathbegin < 1; pathbegin += 0.1) { for (double pathend = 0; pathend < 1; pathend += 0.1) { for (double profilebegin = 0; profilebegin < 1; profilebegin += 0.1) { for (double profileend = 0; profileend < 1; profileend += 0.1) { for (double revolutions = 1; revolutions < 4; revolutions += 0.5) { for (double twistbegin = -1; twistbegin < 1; twistbegin += 0.1) { for (double twistend = -1; twistend < 1; twistend += 0.1) { /* * public double RadiusOffset; */ for (double hollow = 0; hollow < 0.90; hollow += 0.1) { for (double skew = -0.9; skew < 0.9; skew += 0.1) { for (double topshearx = -1; topshearx < 1; topshearx += 0.1) { for (double topsheary = -1; topsheary < 1; topsheary += 0.1) { for (double taperx = -1; taperx < 1; taperx += 0.1) { for (double tapery = -1; tapery < 1; tapery += 0.1) { for (double pathscalex = 0; pathscalex < 1; pathscalex += 0.1) { for (double pathscaley = 0; pathscaley < 1; pathscaley += 0.1) { var shape = new ObjectPart.PrimitiveShape.Decoded(); shape.ProfileShape = profileShape; shape.ShapeType = shapeType; shape.HoleShape = hollowShape; shape.ProfileHollow = hollow; shape.PathCurve = pathCurve; shape.Revolutions = revolutions; shape.PathBegin = pathbegin; shape.PathEnd = pathend; shape.TwistBegin = twistbegin; shape.TwistEnd = twistend; shape.ProfileBegin = profilebegin; shape.ProfileEnd = profileend; shape.TopShear.X = topshearx; shape.TopShear.Y = topsheary; shape.Taper.X = taperx; shape.Taper.Y = tapery; shape.PathScale.X = pathscalex; shape.PathScale.Y = pathscaley; shape.Skew = skew; var ps = new ObjectPart.PrimitiveShape { DecodedParams = shape }; m_Log.Info("---- Transformed to physics shape manager accepted format ----"); m_Log.InfoFormat("PhysicsShapeType: {0}/{1}", physicsShapeType, shapeType); m_Log.Info($"Serialized data {physicsShapeType.ToString()}/{ps.Serialization.ToHexString()}"); try { if (!m_PhysicsShapeManager.TryGetConvexShape(physicsShapeType, ps, out physicsShapeRef)) { DumpParams(shape); m_Log.Error("Could not generate physics hull shape"); success = false; continue; } } catch (Exception e) { DumpParams(shape); m_Log.Error("Could not generate physics hull shape", e); continue; } PhysicsConvexShape convexShape = physicsShapeRef; int hullidx = 0; foreach (PhysicsConvexShape.ConvexHull hull in convexShape.Hulls) { m_Log.InfoFormat("Hull {0}: Generated vertices: {1}", hullidx, hull.Vertices.Count); m_Log.InfoFormat("Hull {0}: Generated triangles: {1}", hullidx, hull.Triangles.Count / 3); ++hullidx; } m_Log.InfoFormat("Generated hulls: {0}", hullidx); } } } } } } } } } } } } } } } /* write a blender .raw */ // ((PhysicsConvexShape)physicsShapeRef).DumpToBlenderRaw(m_OutputFileName); } } } } } return(success); }
internal PhysicsShapePrimShapeReference(ObjectPart.PrimitiveShape primshape, PhysicsShapeManager manager, PhysicsConvexShape shape) : base(manager, shape) { m_Shape = new ObjectPart.PrimitiveShape(primshape); }
internal PhysicsShapeDefaultAvatarReference(PhysicsShapeManager manager, PhysicsConvexShape shape) : base(manager, shape) { }
internal PhysicsShapeMeshReference(UUID id, PrimitivePhysicsShapeType shapeType, PhysicsShapeManager manager, PhysicsConvexShape shape) : base(manager, shape) { m_ID = id; m_ShapeType = shapeType; }
private PhysicsConvexShape ConvertToMesh(PrimitivePhysicsShapeType physicsShape, ObjectPart.PrimitiveShape shape) { PhysicsConvexShape convexShape = null; bool hasHullList = false; if (shape.Type == PrimitiveShapeType.Sculpt && shape.SculptType == PrimitiveSculptType.Mesh) { var m = new LLMesh(m_AssetService[shape.SculptMap]); if (physicsShape == PrimitivePhysicsShapeType.Convex) { #if DEBUG m_Log.DebugFormat("Selected convex of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif if (m.HasConvexPhysics()) { try { #if DEBUG m_Log.DebugFormat("Using convex of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif convexShape = m.GetConvexPhysics(false); hasHullList = convexShape.HasHullList; return(convexShape); } catch (NoSuchMeshDataException) { /* no shape */ #if DEBUG m_Log.DebugFormat("No convex in asset of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif } catch (Exception e) { m_Log.Warn($"Failed to get convex data of {shape.SculptType} {shape.SculptMap}", e); } } #if DEBUG else { m_Log.DebugFormat("No convex shape in {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); } #endif if (convexShape == null) { #if DEBUG m_Log.DebugFormat("Using decompose to single convex for {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif MeshLOD lod = m.GetLOD(LLMesh.LodLevel.LOD3); lod.Optimize(); convexShape = DecomposeConvex(lod, true); } } else { #if DEBUG m_Log.DebugFormat("Selected detailed physics of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif if (m.HasLOD(LLMesh.LodLevel.Physics)) { /* check for physics mesh before giving out the single hull */ #if DEBUG m_Log.DebugFormat("Using detailed physics of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif MeshLOD lod = m.GetLOD(LLMesh.LodLevel.Physics); lod.Optimize(); convexShape = DecomposeConvex(lod); } else if (m.HasConvexPhysics()) { #if DEBUG m_Log.DebugFormat("Using convex of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif try { convexShape = m.GetConvexPhysics(true); hasHullList = convexShape.HasHullList; } catch (NoSuchMeshDataException) { /* no shape */ #if DEBUG m_Log.DebugFormat("No suitable convex in asset of {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif } catch (Exception e) { m_Log.Warn($"Failed to get convex data of {shape.Type}/{shape.SculptType}/{shape.SculptMap}", e); } if (convexShape == null) { /* this way we keep convex hull type functional by having it only get active on PrimitivePhysicsShapeType.Prim */ #if DEBUG m_Log.DebugFormat("Using decompose to convex for {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif MeshLOD lod = m.GetLOD(LLMesh.LodLevel.LOD3); lod.Optimize(); convexShape = DecomposeConvex(lod); } } } } else { #if DEBUG m_Log.DebugFormat("Using decompose to convex for {0}/{1}/{2}", shape.Type, shape.SculptType, shape.SculptMap); #endif MeshLOD m = shape.ToMesh(m_AssetService); m.Optimize(); convexShape = DecomposeConvex(m, physicsShape == PrimitivePhysicsShapeType.Convex); } return(convexShape); }
internal void DecrementUseCount(UUID id, PrimitivePhysicsShapeType physicsShape, PhysicsConvexShape shape) { if (0 == Interlocked.Decrement(ref shape.UseCount)) { m_Lock.AcquireWriterLock(() => m_ConvexShapesBySculptMesh.RemoveIf(GetMeshKey(id, physicsShape), (PhysicsConvexShape s) => s.UseCount == 0)); } }
public PhysicsConvexShape Compute(MeshLOD m, bool useSingleConvex = false) { if (m_VHacd == IntPtr.Zero) { throw new ObjectDisposedException("VHACD"); } var points = new double[m.Vertices.Count * 3]; var tris = new int[m.Triangles.Count * 3]; int idx = 0; int vertices = m.Vertices.Count; foreach (Vector3 v in m.Vertices) { points[idx++] = v.X; points[idx++] = v.Y; points[idx++] = v.Z; } idx = 0; foreach (Triangle t in m.Triangles) { tris[idx++] = t.Vertex1; tris[idx++] = t.Vertex2; tris[idx++] = t.Vertex3; if (t.Vertex1 >= vertices || t.Vertex2 >= vertices || t.Vertex3 >= vertices) { throw new InputDataException("Invalid triangle found"); } } if (idx == 0) { throw new InputDataException("No triangles found"); } Parameters p = Parameters.Defaults; if (useSingleConvex) { p.Depth = 0; } if (!VHacd_Compute(m_VHacd, points, 3, (uint)m.Vertices.Count, tris, 3, (uint)m.Triangles.Count, ref p)) { throw new InputDataException("Decompose failed"); } var shape = new PhysicsConvexShape(); int numhulls = VHacd_GetNConvexHulls(m_VHacd); for (uint hullidx = 0; hullidx < numhulls; ++hullidx) { var hull = new ConvexHull(); VHacd_GetConvexHull(m_VHacd, hullidx, ref hull); var resPoints = new double[hull.NumPoints * 3]; Marshal.Copy(hull.Points, resPoints, 0, hull.NumPoints * 3); var resTris = new int[hull.NumTriangles * 3]; Marshal.Copy(hull.Triangles, resTris, 0, hull.NumTriangles * 3); var cHull = new PhysicsConvexShape.ConvexHull(); for (int vertidx = 0; vertidx < hull.NumPoints * 3; vertidx += 3) { cHull.Vertices.Add(new Vector3( resPoints[vertidx + 0], resPoints[vertidx + 1], resPoints[vertidx + 2])); } int vCount = cHull.Vertices.Count; for (int triidx = 0; triidx < hull.NumTriangles * 3; ++triidx) { int tri = resTris[triidx]; if (tri >= vCount || tri < 0) { m_Log.ErrorFormat("Tri Index out of range"); throw new InputDataException("Tri index out of range"); } cHull.Triangles.Add(resTris[triidx]); } shape.Hulls.Add(cHull); } return(shape); }
protected PhysicsShapeReference(PhysicsShapeManager manager, PhysicsConvexShape shape) { m_PhysicsManager = manager; m_ConvexShape = shape; Interlocked.Increment(ref m_ConvexShape.UseCount); }
bool ISimulationDataPhysicsConvexStorageInterface.TryGetValue(ObjectPart.PrimitiveShape primShape, out PhysicsConvexShape shape) => m_ConvexShapesByPrimShape.TryGetValue(primShape, out shape);
bool ISimulationDataPhysicsConvexStorageInterface.TryGetValue(UUID meshid, PrimitivePhysicsShapeType physicsShape, out PhysicsConvexShape shape) => m_ConvexShapesByMesh.TryGetValue(GetMeshKey(meshid, physicsShape), out shape);