Ejemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="TVGLConvexHull" /> class.
        /// </summary>
        /// <param name="ts">The tessellated solid that the convex hull is made from.</param>
        public TVGLConvexHull(IList <Vertex> vertices, double tolerance)
        {
            var convexHull = ConvexHull.Create(vertices, tolerance);

            if (convexHull.Result == null)
            {
                return;
            }
            Vertices = convexHull.Result.Points.ToArray();
            var convexHullFaceList  = new List <PolygonalFace>();
            var checkSumMultipliers = new long[3];

            for (var i = 0; i < 3; i++)
            {
                checkSumMultipliers[i] = (long)Math.Pow(Constants.CubeRootOfLongMaxValue, i);
            }
            var alreadyCreatedFaces = new HashSet <long>();

            foreach (var cvxFace in convexHull.Result.Faces)
            {
                var faceVertices   = cvxFace.Vertices;
                var orderedIndices = faceVertices.Select(v => v.IndexInList).ToList();
                orderedIndices.Sort();
                var checksum = orderedIndices.Select((t, j) => t * checkSumMultipliers[j]).Sum();
                if (alreadyCreatedFaces.Contains(checksum))
                {
                    continue;
                }
                alreadyCreatedFaces.Add(checksum);
                convexHullFaceList.Add(new PolygonalFace(faceVertices, new Vector3(cvxFace.Normal), false));
            }
            Faces = convexHullFaceList.ToArray();
            Edges = MakeEdges(Faces, Vertices);
            TessellatedSolid.CalculateVolumeAndCenter(Faces, tolerance, out Volume, out Center);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Gets the vertices belonging to this solid
 /// </summary>
 /// <returns></returns>
 public double Volume(double tolerance)
 {
     if (_volume > 0)
     {
         return(_volume);
     }
     TessellatedSolid.CalculateVolumeAndCenter(AllFaces, tolerance, out _volume, out _);
     return(_volume);
 }
Ejemplo n.º 3
0
        internal TVGLConvexHull(IList <Vertex> allVertices, IList <Vertex> convexHullPoints,
                                IList <int> convexHullFaceIndices, double tolerance)
        {
            Vertices = convexHullPoints.ToArray();
            var numCvxHullFaces = convexHullFaceIndices.Count / 3;

            Faces = new PolygonalFace[numCvxHullFaces];
            var checkSumMultipliers = new long[3];

            for (var i = 0; i < 3; i++)
            {
                checkSumMultipliers[i] = (long)Math.Pow(Constants.CubeRootOfLongMaxValue, i);
            }
            var alreadyCreatedFaces = new HashSet <long>();

            for (int i = 0; i < numCvxHullFaces; i++)
            {
                var orderedIndices = new List <int>
                {
                    convexHullFaceIndices[3 * i], convexHullFaceIndices[3 * i + 1], convexHullFaceIndices[3 * i + 2]
                };
                orderedIndices.Sort();
                var checksum = orderedIndices.Select((t, j) => t * checkSumMultipliers[j]).Sum();
                if (alreadyCreatedFaces.Contains(checksum))
                {
                    continue;
                }
                alreadyCreatedFaces.Add(checksum);
                var faceVertices = new[]
                {
                    allVertices[convexHullFaceIndices[3 * i]],
                    allVertices[convexHullFaceIndices[3 * i + 1]],
                    allVertices[convexHullFaceIndices[3 * i + 2]],
                };
                Faces[i] = new PolygonalFace(faceVertices, false);
            }
            Edges       = MakeEdges(Faces, Vertices);
            SurfaceArea = Faces.Sum(face => face.Area);
            TessellatedSolid.CalculateVolumeAndCenter(Faces, tolerance, out Volume, out Center);
        }