public static Mesh CarveToMesh(this CarveSharp.CarveMesh cm) { Mesh m = new Mesh(); for (int i = 0; i < cm.Vertices.Length; i += 3) { m.Vertices.Add(new Rhino.Geometry.Point3d( cm.Vertices[i], cm.Vertices[i + 1], cm.Vertices[i + 2])); } int j = 0; for (int i = 0; i < cm.FaceSizes.Length; ++i) { if (cm.FaceSizes[i] == 3) { m.Faces.AddFace( cm.FaceIndices[j], cm.FaceIndices[j + 1], cm.FaceIndices[j + 2]); } else if (cm.FaceSizes[i] == 4) { m.Faces.AddFace( cm.FaceIndices[j], cm.FaceIndices[j + 1], cm.FaceIndices[j + 2], cm.FaceIndices[j + 3]); } j += cm.FaceSizes[i]; } return(m); }
/// <summary> /// Duplicate CarveMesh. /// </summary> /// <returns></returns> public CarveMesh Duplicate() { CarveMesh cm = new CarveMesh(); cm.Vertices = new double[Vertices.Length]; cm.FaceIndices = new int[FaceIndices.Length]; cm.FaceSizes = new int[FaceSizes.Length]; Vertices.CopyTo(cm.Vertices, 0); FaceIndices.CopyTo(cm.FaceIndices, 0); FaceSizes.CopyTo(cm.FaceSizes, 0); return(cm); }
public static CarveSharp.CarveMesh MeshToCarve(this Mesh m) { double[] verts = new double[m.Vertices.Count * 3]; int[] facesizes = new int[m.Faces.Count]; List <int> faces = new List <int>(); int j = 0; for (int i = 0; i < m.Vertices.Count; ++i) { verts[j] = m.Vertices[i].X; ++j; verts[j] = m.Vertices[i].Y; ++j; verts[j] = m.Vertices[i].Z; ++j; } for (int i = 0; i < m.Faces.Count; ++i) { faces.Add(m.Faces[i].A); faces.Add(m.Faces[i].B); faces.Add(m.Faces[i].C); if (m.Faces[i].IsQuad) { facesizes[i] = 4; faces.Add(m.Faces[i].D); } else { facesizes[i] = 3; } } CarveSharp.CarveMesh cm = new CarveSharp.CarveMesh(); cm.Vertices = verts; cm.FaceIndices = faces.ToArray(); cm.FaceSizes = facesizes; return(cm); }
public static CarveMesh PerformCSG(CarveMesh MeshA, CarveMesh MeshB, CSGOperations operation) { return(PerformCSG(MeshA.Vertices, MeshA.FaceIndices, MeshA.FaceSizes, MeshB.Vertices, MeshB.FaceIndices, MeshB.FaceSizes, operation)); }
public static CarveMesh PerformCSG(double[] firstVerts, int[] firstFaceIndices, int[] firstFaceSizes, double[] secondVerts, int[] secondFaceIndices, int[] secondFaceSizes, CSGOperations operation) { CarveMesh finalResult = null; unsafe { InteropMesh a = new InteropMesh(); InteropMesh b = new InteropMesh(); InteropMesh *result; fixed(double *aVerts = &firstVerts[0], bVerts = &secondVerts[0]) { fixed(int *aFaces = &firstFaceIndices[0], bFaces = &secondFaceIndices[0]) { fixed(int *aFSizes = &firstFaceSizes[0], bFSizes = &secondFaceSizes[0]) { a.numVertices = firstVerts.Length; a.numFaceIndices = firstFaceIndices.Length; a.vertices = aVerts; a.faceIndices = aFaces; a.faceSizes = aFSizes; a.numFaces = firstFaceSizes.Length; b.numVertices = secondVerts.Length; b.numFaceIndices = secondFaceIndices.Length; b.vertices = bVerts; b.faceIndices = bFaces; b.faceSizes = bFSizes; b.numFaces = secondFaceSizes.Length; try { result = performCSG(&a, &b, operation); } catch (SEHException ex) { ArgumentException e = new ArgumentException("Carve has thrown an error. Possible reason is corrupt or self-intersecting meshes", ex); throw e; } } } } if (result->numVertices == 0) { freeMesh(result); return(null); } finalResult = new CarveMesh(); finalResult.Vertices = new double[result->numVertices]; finalResult.FaceIndices = new int[result->numFaceIndices]; finalResult.FaceSizes = new int[result->numFaces]; if (result->numVertices > 0) { Parallel.For(0, finalResult.Vertices.Length, i => { finalResult.Vertices[i] = result->vertices[i]; }); } if (result->numFaceIndices > 0) { Parallel.For(0, finalResult.FaceIndices.Length, i => { finalResult.FaceIndices[i] = result->faceIndices[i]; }); } if (result->numFaces > 0) { Parallel.For(0, finalResult.FaceSizes.Length, i => { finalResult.FaceSizes[i] = result->faceSizes[i]; }); } freeMesh(result); } // end-unsafe return(finalResult); }