public static CSG sphere(Vector3 center, double radius = 1, double slices = 16, double stacks = 8f) { double r = radius; List <CsgPolygon> polygons = new List <CsgPolygon>(); List <IVertex> vertices; for (int i = 0; i < slices; i++) { for (int j = 0; j < stacks; j++) { vertices = new List <IVertex>(); makeSphereVertex(ref vertices, center, r, i / slices, j / stacks); if (j > 0) { makeSphereVertex(ref vertices, center, r, (i + 1) / slices, j / stacks); } if (j < stacks - 1) { makeSphereVertex(ref vertices, center, r, (i + 1) / slices, (j + 1) / stacks); } makeSphereVertex(ref vertices, center, r, i / slices, (j + 1) / stacks); polygons.Add(new CsgPolygon(vertices)); } } return(CSG.fromPolygons(polygons)); }
/// <summary> /// Cube function, Untested but compiles /// </summary> /// <param name="center">world space center of the cube</param> /// <param name="radius">size of the cube created at center</param> /// <returns></returns> public static CSG cube(Vector3?center, Vector3?radius) { Vector3 c = center.GetValueOrDefault(Vector3.Zero); Vector3 r = radius.GetValueOrDefault(Vector3.One); //TODO: Test if this works CsgPolygon[] polygons = new CsgPolygon[6]; int[][][] data = new int[][][] { new int[][] { new int[] { 0, 4, 6, 2 }, new int[] { -1, 0, 0 } }, new int[][] { new int[] { 1, 3, 7, 5 }, new int[] { 1, 0, 0 } }, new int[][] { new int[] { 0, 1, 5, 4 }, new int[] { 0, -1, 0 } }, new int[][] { new int[] { 2, 6, 7, 3 }, new int[] { 0, 1, 0 } }, new int[][] { new int[] { 0, 2, 3, 1 }, new int[] { 0, 0, -1 } }, new int[][] { new int[] { 4, 5, 7, 6 }, new int[] { 0, 0, 1 } } }; for (int x = 0; x < 6; x++) { int[][] v = data[x]; Vector3 normal = new Vector3(v[1][0], v[1][1], v[1][2]); IVertex[] verts = new IVertex[4]; for (int i = 0; i < 4; i++) { verts[i] = new Vertex( new Vector3( c.X + (r.X * (2 * (((v[0][i] & 1) > 0) ? 1 : 0) - 1)), c.Y + (r.Y * (2 * (((v[0][i] & 2) > 0) ? 1 : 0) - 1)), c.Z + (r.Z * (2 * (((v[0][i] & 4) > 0) ? 1 : 0) - 1))), normal ); } polygons[x] = new CsgPolygon(verts); } return(CSG.fromPolygons(polygons)); }
/// <summary> /// Return a new CSG solid representing space both this solid and in the /// solid `csg`. Neither this solid nor the solid `csg` are modified. /// </summary> /// <remarks> /// A.intersect(B) /// /// +-------+ /// | | /// | A | /// | +--+----+ = +--+ /// +----+--+ | +--+ /// | B | /// | | /// +-------+ /// </remarks> /// <param name="csg"></param> /// <returns>CSG of the intersection</returns> public CSG intersect(CSG csg) { Node a = new Node(this.polygons); Node b = new Node(csg.polygons); a.invert(); b.invert(); a.clipTo(b); b.clipTo(a); a.build(b.allPolygons()); return(CSG.fromPolygons(a.allPolygons()).inverse()); }
// public static CSG fromMesh(Mesh m, Transform tf) public static CSG fromMesh(Mesh m) { List <CsgPolygon> triangles = new List <CsgPolygon>(); int[] tris = m.Faces; for (int t = 0; t < tris.Length; t += 3) { Vertex[] vs = new Vertex[3]; /* * vs[0] = TranslateVertex(m, tf, tris[t]); * vs[1] = TranslateVertex(m, tf, tris[t + 1]); * vs[2] = TranslateVertex(m, tf, tris[t + 2]); */ vs[0] = new Vertex(m.Vertices[tris[t]], m.Normals[tris[t]]); vs[1] = new Vertex(m.Vertices[tris[t + 1]], m.Normals[tris[t + 1]]); vs[2] = new Vertex(m.Vertices[tris[t + 2]], m.Normals[tris[t + 2]]); triangles.Add(new CsgPolygon(vs)); } return(CSG.fromPolygons(triangles)); }