public void Intersect(WorkingTriangle triangle) { Vec3 original = verts[0].position.xyz; Vec3 terminal = verts[1].position.xyz; Vec3 triVertexA = triangle.verts[0].position.xyz; Vec3 triVertexB = triangle.verts[1].position.xyz; Vec3 triVertexC = triangle.verts[2].position.xyz; double timeToImpact; Vec3 impactPosition; double u, v; if (Util.RayTriangleIntersect(original, terminal - original, triVertexA, triVertexB, triVertexC, out timeToImpact, out impactPosition, out u, out v)) { // make sure it's not beyond the length of the edge! if (timeToImpact >= 0.0 && timeToImpact <= 1.0) { EdgeIntersection intersection = new EdgeIntersection(); intersection.edge = this; intersection.position = new VertexPosition { xyz = new Vec3 { x = impactPosition.x, y = impactPosition.y, z = impactPosition.z } }; intersection.triangle = triangle; intersections.Add(intersection); triangle.otherObjectEdgeIntersections.Add(intersection); } } }
public WorkingVertex[] verts = new WorkingVertex[2]; // the two endpoints of this edge #endregion Fields #region Methods public WorkingTriangle GetOtherTriangle(WorkingTriangle notThisOne) { if (notThisOne == triangles[0]) return triangles[1]; else return triangles[0]; }
public WorkingTriangle GetOtherTriangle(WorkingTriangle notThisOne) { if (notThisOne == triangles[0]) { return(triangles[1]); } else { return(triangles[0]); } }
// my edges versus the flat of other model's triangles public void Intersect(WorkingModel other, List <TrianglePair> pairs, bool reversed) { List <WorkingEdge> edges = new List <WorkingEdge>(); List <int> triangleToEdgeIndex = new List <int>(); foreach (WorkingTriangle tri in triangles) { for (int i = 0; i < 3; i++) { int index = edges.FindIndex((edge) => edge.Equals(tri.edges[i])); if (index == -1) { triangleToEdgeIndex.Add(edges.Count); edges.Add(tri.edges[i]); } else { triangleToEdgeIndex.Add(index); } } } List <int[]> pairsAlreadyHandled = new List <int[]>(); foreach (TrianglePair pair in pairs) { int a = originalTriangleIndices.IndexOf(reversed ? pair.b : pair.a); int b = other.originalTriangleIndices.IndexOf(reversed ? pair.a : pair.b); WorkingTriangle aTri = triangles[a]; WorkingTriangle bTri = other.triangles[b]; for (int i = 0; i < 3; i++) { int edgeIndex = a * 3 + i; int[] matchup = new int[] { edgeIndex, b }; if (!pairsAlreadyHandled.Exists((item) => item[0] == matchup[0] && item[1] == matchup[1])) { aTri.edges[i].Intersect(bTri); pairsAlreadyHandled.Add(matchup); } } } }
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; }