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);

            Vertices = convexHull.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.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, cvxFace.Normal, false));
            }
            Faces = convexHullFaceList.ToArray();
            Edges = MakeEdges(Faces, Vertices);
            TessellatedSolid.DefineCenterVolumeAndSurfaceArea(Faces, out Center, out Volume, out SurfaceArea);
        }
Ejemplo n.º 2
0
        internal TVGLConvexHull(IList <Vertex> allVertices, IList <Vertex> convexHullPoints,
                                IList <int> convexHullFaceIndices, double[] center = null, double volume = double.NaN,
                                double surfaceArea = double.NaN)
        {
            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);
            if (center == null || double.IsNaN(volume) || double.IsNaN(surfaceArea))
            {
                TessellatedSolid.DefineCenterVolumeAndSurfaceArea(Faces, out Center, out Volume, out SurfaceArea);
            }
            else
            {
                Center      = center;
                Volume      = volume;
                SurfaceArea = surfaceArea;
            }
        }