public override bool Equals(object obj) { WorkingVertex v = obj as WorkingVertex; if (v == null) { return(false); } Vec3 dif = v.position.xyz - position.xyz; return(dif.ComputeMagnitudeSquared() < 0.000000001); }
// adds the specified vert if it is not yet present, and returns the new vert's index // otherwise returns the index of the existing vert it's a duplicate of public int AddVertex(WorkingVertex vert) { int index = allVerts.FindIndex((v) => (v.position.xyz - vert.position.xyz).ComputeMagnitudeSquared() < 0.0000000001); if (index == -1) { allVerts.Add(vert); return(allVerts.Count - 1); } else { return(index); } }
public CutTriangle(WorkingTriangle basis) { objID = basis.objID; List <int> otherTriangles = new List <int>(); List <List <int> > vertsInOtherTriangles = new List <List <int> >(); // copying original triangle's 3 verts for (int i = 0; i < 3; i++) { originalVerts[i] = AddVertex(basis.verts[i]); } // finding verts formed by cutting original triangle's 3 edges for (int i = 0; i < 3; i++) { WorkingEdge edge = basis.edges[i]; Vec3 direction = basis.verts[(i + 1) % 3].position.xyz - basis.verts[i].position.xyz; List <int> verts = new List <int>(); List <double> dots = new List <double>(); List <int> indices = new List <int>(); verts.Add(originalVerts[i]); dots.Add(Vec3.Dot(direction, basis.verts[i].position.xyz)); indices.Add(indices.Count); foreach (EdgeIntersection x in edge.intersections) { WorkingVertex v = new WorkingVertex(); v.position = x.position; v.vinfo = basis.InterpolateVInfos(x.position.xyz); int v_index = AddVertex(v); v = allVerts[v_index]; edgeCutVerts[i].Add(v_index); verts.Add(v_index); dots.Add(Vec3.Dot(direction, v.position.xyz)); indices.Add(indices.Count); int index = otherTriangles.IndexOf(x.triangle.triID); if (index == -1) { otherTriangles.Add(x.triangle.triID); index = vertsInOtherTriangles.Count; vertsInOtherTriangles.Add(new List <int>()); } vertsInOtherTriangles[index].Add(v_index); } verts.Add(originalVerts[(i + 1) % 3]); dots.Add(Vec3.Dot(direction, basis.verts[(i + 1) % 3].position.xyz)); indices.Add(indices.Count); // sorting the cut edges indices.Sort((a, b) => (int)Math.Sign(dots[a] - dots[b])); for (int j = 1; j < indices.Count; j++) { cutEdges[i].Add(new int[] { verts[indices[j - 1]], verts[indices[j]] }); } } foreach (EdgeIntersection x in basis.otherObjectEdgeIntersections) { WorkingVertex v = new WorkingVertex(); v.position = x.position; v.vinfo = basis.InterpolateVInfos(x.position.xyz); int v_index = AddVertex(v); v = allVerts[v_index]; centerVerts.Add(v_index); foreach (WorkingTriangle tri in x.edge.triangles) { if (tri == null) { continue; } int index = otherTriangles.IndexOf(tri.triID); if (index == -1) { otherTriangles.Add(tri.triID); index = vertsInOtherTriangles.Count; vertsInOtherTriangles.Add(new List <int>()); } else { } vertsInOtherTriangles[index].Add(v_index); } } foreach (List <int> edgeVerts in vertsInOtherTriangles) { if (edgeVerts.Count == 2) { slicedEdges.Add(new int[] { edgeVerts[0], edgeVerts[1] }); } } }
public WorkingModel ToWorkingModel(int id, List<int> keptTriangles) { WorkingModel model = new WorkingModel(); model.objID = id; List<int> vertexIndices = new List<int>(); List<int> useVerts = new List<int>(); // get all the triangles and their vertices foreach (int triangleIndex in keptTriangles) { WorkingTriangle triangle = new WorkingTriangle(); triangle.objID = id; triangle.triID = triangleIndex; for (int i = 0; i < 3; i++) { int vi = t_v[triangleIndex, i]; int useIndex = vertexIndices.IndexOf(vi); VertexPosition vpos; if (useIndex == -1) { useVerts.Add(vertexIndices.Count); vertexIndices.Add(vi); Vec3 pos = verts[vi]; vpos = new VertexPosition { xyz = new Vec3 { x = pos.x, y = pos.y, z = pos.z } }; model.vertexPositions.Add(vpos); } else { useVerts.Add(useIndex); Vec3 pos = verts[vi]; vpos = model.vertexPositions[useIndex]; } WorkingVertex wvert = new WorkingVertex(); wvert.position = vpos; wvert.vinfo[0] = new VInfoReference { objID = id, index = triangleIndex * 3 + i, weight = 1.0 }; wvert.vinfo[1] = new VInfoReference { objID = id, index = -1, weight = 0.0 }; wvert.vinfo[2] = new VInfoReference { objID = id, index = -1, weight = 0.0 }; triangle.verts[i] = wvert; } model.triangles.Add(triangle); model.originalTriangleIndices.Add(triangleIndex); } // now get all the edges between them foreach (WorkingTriangle tri in model.triangles) { for (int i = 0; i < 3; i++) { // if a triangle hasn't got an edge, give it one, and find the matching triangle if (tri.edges[i] == null) { WorkingEdge edge = tri.edges[i] = new WorkingEdge(); edge.verts[0] = tri.verts[i]; edge.verts[1] = tri.verts[(i + 1) % 3]; edge.triangles[0] = tri; foreach(WorkingTriangle other in model.triangles) { if(other != tri) { for (int j = 0; j < 3; j++) { // if another triangle has an edge, it's got a matching triangle, and our triangle can't be their match if (other.edges[j] != null) continue; if ((other.verts[j] == edge.verts[0] && other.verts[(j + 1) % 3] == edge.verts[1]) || (other.verts[j] == edge.verts[1] && other.verts[(j + 1) % 3] == edge.verts[0])) { edge.triangles[1] = other; other.edges[j] = edge; break; } } } } } } } return model; }