/// <summary> /// Find the triangle(s) containing the given point. The search starts from an arbitrary /// triangle and homes in on the vertex by visiting triangles closest to the vertex. /// If the vertex is on and edge seperating two triangles, both are returned. /// </summary> /// <param name="v">The vertex to search for</param> /// <returns>The triangles(s) containing the vertex</returns> internal FindTriangle[] FindContaining(Vertex v) { Triangle current = root; while (true) { PointOnTriangle loc = PointOnTriangle.None; Halfedge closestEdge = null; PointShapeRelation rel = current.Contains(v, out loc, out closestEdge); if (rel == PointShapeRelation.Inside) { // Inside triangle, return. return(new FindTriangle[] { new FindTriangle(current, rel, loc) }); } else if (rel == PointShapeRelation.Outside) { // Outside triangle, continue searching from the neighbour closest to the point Halfedge opp = closestEdge.Opposite; if (opp == null) { throw new InvalidOperationException("No triangle contains this vertex."); } current = opp.Parent; } else { // On an edge, return the triangle and its neighbour (if there is one) Halfedge opp = closestEdge.Opposite; if (opp == null) { return(new FindTriangle[] { new FindTriangle(current, rel, loc) }); } else { Triangle other = opp.Parent; PointOnTriangle otherLoc = PointOnTriangle.None; PointShapeRelation otherRel = other.Contains(v, out otherLoc); return(new FindTriangle[] { new FindTriangle(current, rel, loc), new FindTriangle(other, otherRel, otherLoc) }); } } } }
public FindTriangle(Triangle tri, PointShapeRelation rel, PointOnTriangle loc) { Triangle = tri; Relation = rel; Location = loc; }