Example #1
0
 /// <summary>
 /// Creates a polyhedron by intersecting a set of at least 4 planes.
 /// This constructor uses high precision operations for plane intersections.
 /// </summary>
 public Polyhedron(IEnumerable <Plane> planes)
 {
     Polygons = new Precision.Polyhedron(planes.Select(x => x.ToPrecisionPlane()))
                .Polygons
                .Select(x => x.ToStandardPolygon())
                .ToList();
 }
Example #2
0
        /// <summary>
        /// Splits this polyhedron into two polyhedron by intersecting against a plane.
        /// </summary>
        /// <param name="plane">The splitting plane</param>
        /// <param name="back">The back side of the polyhedron</param>
        /// <param name="front">The front side of the polyhedron</param>
        /// <returns>True if the plane splits the polyhedron, false if the plane doesn't intersect</returns>
        public bool Split(Plane plane, out Polyhedron back, out Polyhedron front)
        {
            back = front = null;

            // Check that this solid actually spans the plane
            var classify = Polygons.Select(x => x.ClassifyAgainstPlane(plane)).Distinct().ToList();

            if (classify.All(x => x != PlaneClassification.Spanning))
            {
                if (classify.Any(x => x == PlaneClassification.Back))
                {
                    back = this;
                }
                else if (classify.Any(x => x == PlaneClassification.Front))
                {
                    front = this;
                }
                return(false);
            }

            var backPlanes = new List <Plane> {
                plane
            };
            var frontPlanes = new List <Plane> {
                new Plane(-plane.Normal, -plane.DistanceFromOrigin)
            };

            foreach (var face in Polygons)
            {
                var classification = face.ClassifyAgainstPlane(plane);
                if (classification != PlaneClassification.Back)
                {
                    frontPlanes.Add(face.Plane);
                }
                if (classification != PlaneClassification.Front)
                {
                    backPlanes.Add(face.Plane);
                }
            }

            back  = new Polyhedron(backPlanes);
            front = new Polyhedron(frontPlanes);

            return(true);
        }