// vertices grouped into 3's (3 verts per triangle) public CSGModel(List<Vec3> modelVerts, List<VInfo> matchingInfos) : this() { List<CSGVertex> vertexMapping = new List<CSGVertex>(); for (int i = 0; i < modelVerts.Count; i++) vertexMapping.Add(GetVertex(modelVerts[i], true)); for (int i = 0; i < modelVerts.Count; i += 3) { CSGVertex[] tri = new CSGVertex[3]; VInfo[] vinfos = new VInfo[3]; bool acceptable = true; for (int j = 0; j < 3; j++) { tri[j] = vertexMapping[i + j]; for (int k = 0; k < j; k++) if (tri[j] == tri[k]) acceptable = false; vinfos[j] = matchingInfos[i + j]; if (!acceptable) break; } if (acceptable) { CSGSourceTriangle triangle = new CSGSourceTriangle(this, tri, vinfos); foreach (CSGShape shape in triangle.divisions) shapes.Add(shape); sourceTriangles.Add(triangle); } } }
public CSGEdge(CSGVertex[] endpoints) : this() { for (int i = 0; i < 2; i++) { this.endpoints[i] = endpoints[i]; endpoints[i].SetAsNeighbor(this); } }
public CSGSourceTriangle(CSGModel model, CSGVertex[] verts, VInfo[] vinfos) : this() { this.model = model; for(int i = 0; i< 3;i++) { sourceVerts[i] = verts[i]; vertexVInfos[verts[i].id] = vinfos[i]; } ComputePlane(); divisions.Add(new CSGShape(model, this)); }
public CSGModel(List <Vec3> modelVerts, List <VInfo> matchingInfos) // vertices grouped into 3's (3 verts per triangle) : this() { List <CSGVertex> vertexMapping = new List <CSGVertex>(); for (int i = 0; i < modelVerts.Count; i++) { vertexMapping.Add(GetVertex(modelVerts[i], true)); } for (int i = 0; i < modelVerts.Count; i += 3) { CSGVertex[] tri = new CSGVertex[3]; VInfo[] vinfos = new VInfo[3]; bool acceptable = true; for (int j = 0; j < 3; j++) { tri[j] = vertexMapping[i + j]; for (int k = 0; k < j; k++) { if (tri[j] == tri[k]) { acceptable = false; } } vinfos[j] = matchingInfos[i + j]; if (!acceptable) { break; } } if (acceptable) { CSGSourceTriangle triangle = new CSGSourceTriangle(this, tri, vinfos); foreach (CSGShape shape in triangle.divisions) { shapes.Add(shape); } sourceTriangles.Add(triangle); } } }
public CSGEdge GetEdge(CSGVertex a, CSGVertex b, bool create) { int index = edges.FindIndex((e) => e.endpoints[0].id == a.id && e.endpoints[1].id == b.id || e.endpoints[0].id == b.id && e.endpoints[1].id == a.id); if (index == -1) { if (!create) { return(null); } else { edges.Add(new CSGEdge(new CSGVertex[] { a, b })); return(edges[edges.Count - 1]); } } else { return(edges[index]); } }
// clone a model and transform its verts and vinfos (scraps any WIP intersection info) public CSGModel CloneAndTransform(ModelUtil.CopyAndTransformVert vertCopier, ModelUtil.CopyAndTransformVInfo vinfoCopier) { CSGModel result = new CSGModel(); Dictionary<long, CSGVertex> vertexMapping = new Dictionary<long, CSGVertex>(); Dictionary<long, CSGEdge> edgeMapping = new Dictionary<long, CSGEdge>(); Dictionary<long, CSGShape> shapeMapping = new Dictionary<long, CSGShape>(); Dictionary<long, CSGSourceTriangle> triangleMapping = new Dictionary<long, CSGSourceTriangle>(); foreach (CSGVertex vert in vertices) { CSGVertex v = new CSGVertex(); vertexMapping[vert.id] = v; result.vertices.Add(v); } foreach (CSGEdge edge in edges) { CSGEdge e = new CSGEdge(); edgeMapping[edge.id] = e; result.edges.Add(e); } foreach (CSGShape shape in shapes) { CSGShape s = new CSGShape(); shapeMapping[shape.id] = s; result.shapes.Add(s); } foreach (CSGSourceTriangle triangle in sourceTriangles) { CSGSourceTriangle t = new CSGSourceTriangle(); triangleMapping[triangle.id] = t; result.sourceTriangles.Add(t); } foreach (CSGVertex iVertex in vertices) { CSGVertex rVertex = vertexMapping[iVertex.id]; if(iVertex.parentTriangle != null) rVertex.parentTriangle = triangleMapping[iVertex.parentTriangle.id]; vertCopier(iVertex.position, out rVertex.position); foreach(CSGEdge edge in iVertex.neighbors) rVertex.neighbors.Add(edgeMapping[edge.id]); } foreach (CSGEdge iEdge in edges) { CSGEdge rEdge = edgeMapping[iEdge.id]; if(iEdge.parentTriangle != null) rEdge.parentTriangle = triangleMapping[iEdge.parentTriangle.id]; for (int i = 0; i < 2; i++) { rEdge.endpoints[i] = vertexMapping[iEdge.endpoints[i].id]; if (iEdge.separatedShapes[i] != null) rEdge.separatedShapes[i] = shapeMapping[iEdge.separatedShapes[i].id]; } } foreach (CSGSourceTriangle iTriangle in sourceTriangles) { CSGSourceTriangle rTriangle = triangleMapping[iTriangle.id]; for (int i = 0; i < 3; i++) rTriangle.sourceVerts[i] = vertexMapping[iTriangle.sourceVerts[i].id]; foreach (long key in iTriangle.vertexVInfos.Keys) { VInfo vinfo; vinfoCopier(iTriangle.vertexVInfos[key], out vinfo); rTriangle.vertexVInfos[key] = vinfo; } } foreach (CSGShape iShape in shapes) { CSGShape rShape = shapeMapping[iShape.id]; rShape.parentTriangle = triangleMapping[iShape.parentTriangle.id]; foreach (CSGVertex vert in iShape.boundary) rShape.boundary.Add(vertexMapping[vert.id]); foreach (List<CSGVertex> iHole in iShape.holes) { List<CSGVertex> rHole = new List<CSGVertex>(); foreach (CSGVertex vert in iHole) rHole.Add(vertexMapping[vert.id]); rShape.holes.Add(rHole); } foreach (CSGEdge iEdge in iShape.edges) rShape.edges.Add(edgeMapping[iEdge.id]); } return result; }
public CSGEdge GetEdge(CSGVertex a, CSGVertex b, bool create) { int index = edges.FindIndex((e) => e.endpoints[0].id == a.id && e.endpoints[1].id == b.id || e.endpoints[0].id == b.id && e.endpoints[1].id == a.id); if (index == -1) if (!create) return null; else { edges.Add(new CSGEdge(new CSGVertex[] { a, b })); return edges[edges.Count - 1]; } else return edges[index]; }
// clone a model and transform its verts and vinfos (scraps any WIP intersection info) public CSGModel CloneAndTransform(ModelUtil.CopyAndTransformVert vertCopier, ModelUtil.CopyAndTransformVInfo vinfoCopier) { CSGModel result = new CSGModel(); Dictionary <long, CSGVertex> vertexMapping = new Dictionary <long, CSGVertex>(); Dictionary <long, CSGEdge> edgeMapping = new Dictionary <long, CSGEdge>(); Dictionary <long, CSGShape> shapeMapping = new Dictionary <long, CSGShape>(); Dictionary <long, CSGSourceTriangle> triangleMapping = new Dictionary <long, CSGSourceTriangle>(); foreach (CSGVertex vert in vertices) { CSGVertex v = new CSGVertex(); vertexMapping[vert.id] = v; result.vertices.Add(v); } foreach (CSGEdge edge in edges) { CSGEdge e = new CSGEdge(); edgeMapping[edge.id] = e; result.edges.Add(e); } foreach (CSGShape shape in shapes) { CSGShape s = new CSGShape(); shapeMapping[shape.id] = s; result.shapes.Add(s); } foreach (CSGSourceTriangle triangle in sourceTriangles) { CSGSourceTriangle t = new CSGSourceTriangle(); triangleMapping[triangle.id] = t; result.sourceTriangles.Add(t); } foreach (CSGVertex iVertex in vertices) { CSGVertex rVertex = vertexMapping[iVertex.id]; if (iVertex.parentTriangle != null) { rVertex.parentTriangle = triangleMapping[iVertex.parentTriangle.id]; } vertCopier(iVertex.position, out rVertex.position); foreach (CSGEdge edge in iVertex.neighbors) { rVertex.neighbors.Add(edgeMapping[edge.id]); } } foreach (CSGEdge iEdge in edges) { CSGEdge rEdge = edgeMapping[iEdge.id]; if (iEdge.parentTriangle != null) { rEdge.parentTriangle = triangleMapping[iEdge.parentTriangle.id]; } for (int i = 0; i < 2; i++) { rEdge.endpoints[i] = vertexMapping[iEdge.endpoints[i].id]; if (iEdge.separatedShapes[i] != null) { rEdge.separatedShapes[i] = shapeMapping[iEdge.separatedShapes[i].id]; } } } foreach (CSGSourceTriangle iTriangle in sourceTriangles) { CSGSourceTriangle rTriangle = triangleMapping[iTriangle.id]; for (int i = 0; i < 3; i++) { rTriangle.sourceVerts[i] = vertexMapping[iTriangle.sourceVerts[i].id]; } foreach (long key in iTriangle.vertexVInfos.Keys) { VInfo vinfo; vinfoCopier(iTriangle.vertexVInfos[key], out vinfo); rTriangle.vertexVInfos[key] = vinfo; } } foreach (CSGShape iShape in shapes) { CSGShape rShape = shapeMapping[iShape.id]; rShape.parentTriangle = triangleMapping[iShape.parentTriangle.id]; foreach (CSGVertex vert in iShape.boundary) { rShape.boundary.Add(vertexMapping[vert.id]); } foreach (List <CSGVertex> iHole in iShape.holes) { List <CSGVertex> rHole = new List <CSGVertex>(); foreach (CSGVertex vert in iHole) { rHole.Add(vertexMapping[vert.id]); } rShape.holes.Add(rHole); } foreach (CSGEdge iEdge in iShape.edges) { rShape.edges.Add(edgeMapping[iEdge.id]); } } return(result); }