Example #1
0
        public static CSG Create(Vector center = default(Vector), double radius = 1)
        {
            var c = center;
            var r = new double[] { radius, radius, radius };

            var polygons = new List <Polygon>()
            {
                { CreatePolygon(new List <short> {
                        0, 4, 6, 2
                    }, new Vector(-1, 0, 0), c, r) },
                { CreatePolygon(new List <short> {
                        1, 3, 7, 5
                    }, new Vector(+1, 0, 0), c, r) },
                { CreatePolygon(new List <short> {
                        0, 1, 5, 4
                    }, new Vector(0, -1, 0), c, r) },
                { CreatePolygon(new List <short> {
                        2, 6, 7, 3
                    }, new Vector(0, +1, 0), c, r) },
                { CreatePolygon(new List <short> {
                        0, 2, 3, 1
                    }, new Vector(0, 0, -1), c, r) },
                { CreatePolygon(new List <short> {
                        4, 5, 7, 6
                    }, new Vector(0, 0, +1), c, r) },
            };

            return(CSG.FromPolygons(polygons.ToArray()));
        }
Example #2
0
        public static CSG Create(Vector startV = default(Vector), Vector endV = default(Vector), double radius = 1, double slices = 16)
        {
            var s     = new Vector(startV == Vector.Zero ? Vector.Down : startV);
            var e     = new Vector(endV == Vector.Zero ? Vector.Up : endV);
            var ray   = e.Minus(s);
            var r     = radius;
            var axisZ = ray.Unit();
            var isY   = (Math.Abs(axisZ.y) > 0.5);

            var axisX = new Vector((isY ? 1 : 0), (!isY ? 0 : 1), 0).Cross(axisZ).Unit();
            var axisY = axisX.Cross(axisZ).Unit();
            var start = new Vertex(s, axisZ.Negated());
            var end   = new Vertex(e, axisZ.Unit());

            var polygons = new List <Polygon>();

            for (var i = 0; i < slices; i++)
            {
                var t0 = i / slices;
                var t1 = (i + 1) / slices;
                polygons.Add(new Polygon(new Vertex[] { start, point(axisX, axisY, axisZ, s, ray, r, 0, t0, -1), point(axisX, axisY, axisZ, s, ray, r, 0, t1, -1) }));
                polygons.Add(new Polygon(new Vertex[] { point(axisX, axisY, axisZ, s, ray, r, 0, t1, 0), point(axisX, axisY, axisZ, s, ray, r, 0, t0, 0), point(axisX, axisY, axisZ, s, ray, r, 1, t0, 0), point(axisX, axisY, axisZ, s, ray, r, 1, t1, 0) }));
                polygons.Add(new Polygon(new Vertex[] { end, point(axisX, axisY, axisZ, s, ray, r, 1, t1, 1), point(axisX, axisY, axisZ, s, ray, r, 1, t0, 1) }));
            }
            return(CSG.FromPolygons(polygons.ToArray()));
        }
Example #3
0
        public CSG Clone()
        {
            var csg = new CSG();

            csg.polygons = polygons.Select(p => p.Clone()).ToArray();
            return(csg);
        }
Example #4
0
        // Construct a CSG solid from a list of `CSG.Polygon` instances.
        public static CSG FromPolygons(Polygon[] polygons)
        {
            var csg = new CSG();

            csg.polygons = polygons;
            return(csg);
        }
Example #5
0
        public static CSG Create(Vector center = default(Vector), double radius = 1, double slices = 16, double stacks = 8)
        {
            var c        = new Vector(center);
            var r        = radius;
            var polygons = new List <Polygon>();
            var vertices = new List <Vertex>();

            for (var i = 0; i < slices; i++)
            {
                for (var j = 0; j < stacks; j++)
                {
                    vertices.Clear();
                    Vertex(vertices, c, r, i / slices, j / stacks);
                    if (j > 0)
                    {
                        Vertex(vertices, c, r, (i + 1) / slices, j / stacks);
                    }
                    if (j < stacks - 1)
                    {
                        Vertex(vertices, c, r, (i + 1) / slices, (j + 1) / stacks);
                    }
                    Vertex(vertices, c, r, i / slices, (j + 1) / stacks);
                    polygons.Add(new Polygon(vertices.ToArray()));
                }
            }
            return(CSG.FromPolygons(polygons.ToArray()));
        }
Example #6
0
        // Return a new CSG solid representing space in either this solid or in the
        // solid `csg`. Neither this solid nor the solid `csg` are modified.
        //
        //     A.union(B)
        //
        //     +-------+            +-------+
        //     |       |            |       |
        //     |   A   |            |       |
        //     |    +--+----+   =   |       +----+
        //     +----+--+    |       +----+       |
        //          |   B   |            |       |
        //          |       |            |       |
        //          +-------+            +-------+
        //
        public CSG Union(CSG csg)
        {
            var a = new Node(Clone().polygons);
            var b = new Node(csg.Clone().polygons);

            a.ClipTo(b);
            b.ClipTo(a);
            b.Invert();
            b.ClipTo(a);
            b.Invert();
            a.Build(b.AllPolygons());
            return(CSG.FromPolygons(a.AllPolygons()));
        }