Example #1
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));
        }
Example #2
0
 public CSGVector cross(CSGVector a)
 {
     return(new CSGVector(
                this.y * a.z - this.z * a.y,
                this.z * a.x - this.x * a.z,
                this.x * a.y - this.y * a.x));
 }
Example #3
0
        private static CSGVertex point(double stack, double slice, int normalblend)
        {
            double    angle  = slice * Math.PI * 2;
            CSGVector _out   = axisX.times((float)Math.Cos(angle)).plus(axisY.times((float)Math.Sin(angle)));
            CSGVector pos    = CylinderBottom.plus(ray.times((float)stack)).plus(_out.times(CylinderRadius));
            CSGVector normal = _out.times(1 - Math.Abs(normalblend)).plus(axisZ.times(normalblend));

            return(new CSGVertex(pos, normal));
        }
Example #4
0
        private static CSGVertex AddVertex(double theta, double phi)
        {
            theta *= Math.PI * 2;
            phi   *= Math.PI;

            CSGVector dir = new CSGVector(
                (float)(Math.Cos(theta) * Math.Sin(phi)),
                (float)Math.Cos(phi),
                (float)(Math.Sin(theta) * Math.Sin(phi)));

            return(new CSGVertex(center.plus(dir.times(SphereRadius)), dir));
        }
Example #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));
        }
Example #6
0
 public static CSG Cube(CSGVector center, int size)
 {
     return(CSG.Cube(center, size, size, size));
 }
Example #7
0
 public static CSG Cube(CSGVector center)
 {
     return(CSG.Cube(center, 20));
 }
Example #8
0
        public static CSGPlane fromPoints(CSGVector a, CSGVector b, CSGVector c)
        {
            CSGVector n = b.minus(a).cross(c.minus(a)).unit();

            return(new CSGPlane(n, n.dot(a)));
        }
Example #9
0
 public CSGPlane(CSGVector normal, float w)
 {
     this.normal = normal;
     this.w      = w;
 }
Example #10
0
 public void flip()
 {
     this.normal = this.normal.negated();
 }
Example #11
0
 public CSGVertex(float[] pos, float[] normal)
 {
     this.pos    = new CSGVector(pos);
     this.normal = new CSGVector(normal);
 }
Example #12
0
 public CSGVertex(CSGVector pos, CSGVector normal)
 {
     this.pos    = pos;
     this.normal = normal;
 }
Example #13
0
 public CSGVector lerp(CSGVector a, float t)
 {
     return(this.plus(a.minus(this).times(t)));
 }
Example #14
0
 public float dot(CSGVector a)
 {
     return(this.x * a.x + this.y * a.y + this.z * a.z);
 }
Example #15
0
 public CSGVector minus(CSGVector a)
 {
     return(new CSGVector(this.x - a.x, this.y - a.y, this.z - a.z));
 }
Example #16
0
 public CSGVector plus(CSGVector a)
 {
     return(new CSGVector(this.x + a.x, this.y + a.y, this.z + a.z));
 }