Exemple #1
0
        public static CSG Sphere()
        {
            int slices = 16;
            int stacks = 8;

            List <CSGPolygon> polygons = new List <CSGPolygon>();

            for (int i = 0; i < slices; i++)
            {
                for (int j = 0; j < stacks; j++)
                {
                    List <CSGVertex> vertexList = new List <CSGVertex>();
                    vertexList.Add(AddVertex((double)i / (double)slices, (double)j / (double)stacks));
                    if (j > 0)
                    {
                        vertexList.Add(AddVertex((double)(i + 1) / (double)slices, (double)j / (double)stacks));
                    }
                    if (j < stacks - 1)
                    {
                        vertexList.Add(AddVertex((double)(i + 1) / (double)slices, (double)(j + 1) / (double)stacks));
                    }
                    vertexList.Add(AddVertex((double)i / (double)slices, (double)(j + 1) / (double)stacks));
                    polygons.Add(new CSGPolygon(vertexList, 0));
                }
            }

            return(CSG.fromPolygons(polygons));
        }
Exemple #2
0
        public static CSG Cylinder(CSGVector bottom, CSGVector top, int radius)
        {
            CylinderBottom = bottom;
            CylinderTop    = top;
            CylinderRadius = radius;

            ray = CylinderTop.minus(CylinderBottom);

            int slices = 16;

            axisZ = ray.unit();
            int isY = Math.Abs(axisZ.y) > 0.5 ? 1 : 0;

            axisX = new CSGVector(isY, 1 - isY, 0).cross(axisZ).unit();
            axisY = axisX.cross(axisZ).unit();
            CSGVertex         start    = new CSGVertex(CylinderBottom, axisZ.negated());
            CSGVertex         end      = new CSGVertex(CylinderTop, axisZ.unit());
            List <CSGPolygon> polygons = new List <CSGPolygon>();

            for (int i = 0; i < slices; i++)
            {
                double t0 = (double)i / (double)slices;
                double t1 = (double)(i + 1) / (double)slices;
                polygons.Add(new CSGPolygon(start, point(0, t0, -1), point(0, t1, -1)));
                polygons.Add(new CSGPolygon(point(0, t1, 0), point(0, t0, 0), point(1, t0, 0), point(1, t1, 0)));
                polygons.Add(new CSGPolygon(end, point(1, t1, 1), point(1, t0, 1)));
            }

            return(CSG.fromPolygons(polygons));
        }
Exemple #3
0
        public CSG clone()
        {
            CSG csg = new CSG();

            foreach (CSGPolygon p in polygons)
            {
                csg.polygons.Add(p.clone());
            }

            return(csg);
        }
Exemple #4
0
        public CSG union(CSG csg)
        {
            CSGNode a = new CSGNode(this.clone().polygons);
            CSGNode b = new CSGNode(csg.clone().polygons);

            a.clipTo(b);
            b.clipTo(a);
            b.invert();
            b.clipTo(a);
            b.invert();
            a.build(b.allPolygons());
            return(CSG.fromTriangles(a.allPolygons()));
        }
Exemple #5
0
        public static CSG Cube(CSGVector center, int r1, int r2, int r3)
        {
            CubeCenter = center;

            CubeDimension[0] = r1;
            CubeDimension[1] = r2;
            CubeDimension[2] = r3;

            List <CubeMaker> cubemakerList = new List <CubeMaker>();

            cubemakerList.Add(new CubeMaker(0, 4, 6, 2, -1, 0, 0));
            cubemakerList.Add(new CubeMaker(1, 3, 7, 5, +1, 0, 0));
            cubemakerList.Add(new CubeMaker(0, 1, 5, 4, 0, -1, 0));
            cubemakerList.Add(new CubeMaker(2, 6, 7, 3, 0, +1, 0));
            cubemakerList.Add(new CubeMaker(0, 2, 3, 1, 0, 0, -1));
            cubemakerList.Add(new CubeMaker(4, 5, 7, 6, 0, 0, +1));

            List <CSGPolygon> polygonList = new List <CSGPolygon>();

            foreach (CubeMaker cm in cubemakerList)
            {
                List <CSGVertex> vertexList = new List <CSGVertex>();

                for (int i = 0; i < 4; i++)
                {
                    float x = CubeCenter.x + CubeDimension[0] * (2 * ((cm.param[i] & 1) != 0 ? 1 : 0) - 1);
                    float y = CubeCenter.y + CubeDimension[1] * (2 * ((cm.param[i] & 2) != 0 ? 1 : 0) - 1);
                    float z = CubeCenter.z + CubeDimension[2] * (2 * ((cm.param[i] & 4) != 0 ? 1 : 0) - 1);

                    CSGVector pos = new CSGVector(x, y, z);
                    CSGVector n   = new CSGVector(cm.nx, cm.ny, cm.nz);

                    vertexList.Add(new CSGVertex(pos, n));
                }

                polygonList.Add(new CSGPolygon(vertexList, 0));
            }

            return(CSG.fromPolygons(polygonList));
        }
Exemple #6
0
 public static CSG Cube(CSGVector center, int size)
 {
     return(CSG.Cube(center, size, size, size));
 }
Exemple #7
0
 public static CSG Cube(CSGVector center)
 {
     return(CSG.Cube(center, 20));
 }
Exemple #8
0
 public static CSG Cube(int size)
 {
     return(CSG.Cube(new CSGVector(0, 0, 0), size));
 }
Exemple #9
0
 public static CSG Cube()
 {
     return(CSG.Cube(new CSGVector(0, 0, 0)));
 }
Exemple #10
0
 public static CSG Cylinder(int height, int radius)
 {
     return(CSG.Cylinder(new CSGVector(0, 0, 0), new CSGVector(0, height, 0), radius));
 }
Exemple #11
0
 public static CSG Cylinder()
 {
     return(CSG.Cylinder(30, 20));
 }