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);
        }
Esempio n. 2
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);
        }