/// <summary> /// Find and remove any junction (ie valence>2) vertices of the graph. /// At a junction, the pair of best-aligned (ie straightest) edges are left /// connected, and all the other edges are disconnected /// /// [TODO] currently there is no DGraph2.SetEdge(), so the 'other' edges /// are deleted and new edges inserted. Hence, edge IDs are not preserved. /// </summary> public static int DisconnectJunctions(DGraph2 graph) { var junctions = new List <int>(); // find all junctions foreach (int vid in graph.VertexIndices()) { if (graph.IsJunctionVertex(vid)) { junctions.Add(vid); } } foreach (int vid in junctions) { Vector2d v = graph.GetVertex(vid); int[] nbr_verts = graph.VtxVerticesItr(vid).ToArray(); // find best-aligned pair of edges connected to vid Index2i best_aligned = Index2i.Max; double max_angle = 0; for (int i = 0; i < nbr_verts.Length; ++i) { for (int j = i + 1; j < nbr_verts.Length; ++j) { double angle = Vector2d.AngleD( (graph.GetVertex(nbr_verts[i]) - v).Normalized, (graph.GetVertex(nbr_verts[j]) - v).Normalized); angle = Math.Abs(angle); if (angle > max_angle) { max_angle = angle; best_aligned = new Index2i(nbr_verts[i], nbr_verts[j]); } } } // for nbr verts that are not part of the best_aligned edges, // we remove those edges and add a new one connected to a new vertex for (int k = 0; k < nbr_verts.Length; ++k) { if (nbr_verts[k] == best_aligned.a || nbr_verts[k] == best_aligned.b) { continue; } int eid = graph.FindEdge(vid, nbr_verts[k]); graph.RemoveEdge(eid, true); if (graph.IsVertex(nbr_verts[k])) { var newpos = Vector2d.Lerp(graph.GetVertex(nbr_verts[k]), v, 0.99); int newv = graph.AppendVertex(newpos); graph.AppendEdge(nbr_verts[k], newv); } } } return(junctions.Count); }
/// <summary> /// compute length of path through graph /// </summary> public static double PathLength(DGraph2 graph, IList <int> pathVertices) { double len = 0; int N = pathVertices.Count; Vector2d prev = graph.GetVertex(pathVertices[0]), next = Vector2d.Zero; for (int i = 1; i < N; ++i) { next = graph.GetVertex(pathVertices[i]); len += prev.Distance(next); prev = next; } return(len); }
/// <summary> /// foreach edge [vid,b] connected to junction vertex vid, remove, add new vertex c, /// and then add new edge [b,c]. Optionally move c a bit back along edge from vid. /// </summary> public static void DisconnectJunction(DGraph2 graph, int vid, double shrinkFactor = 1.0) { Vector2d v = graph.GetVertex(vid); int[] nbr_verts = graph.VtxVerticesItr(vid).ToArray(); for (int k = 0; k < nbr_verts.Length; ++k) { int eid = graph.FindEdge(vid, nbr_verts[k]); graph.RemoveEdge(eid, true); if (graph.IsVertex(nbr_verts[k])) { Vector2d newpos = Vector2d.Lerp(graph.GetVertex(nbr_verts[k]), v, shrinkFactor); int newv = graph.AppendVertex(newpos); graph.AppendEdge(nbr_verts[k], newv); } } }
/// <summary> /// If vid has two or more neighbours, returns uniform laplacian, otherwise returns vid position /// </summary> public static Vector2d VertexLaplacian(DGraph2 graph, int vid, out bool isValid) { Vector2d v = graph.GetVertex(vid); Vector2d centroid = Vector2d.Zero; int n = 0; foreach (int vnbr in graph.VtxVerticesItr(vid)) { centroid += graph.GetVertex(vnbr); n++; } if (n == 2) { centroid /= n; isValid = true; return(centroid - v); } isValid = false; return(v); }
public void SplitToMaxEdgeLength(double fMaxLen) { var queue = new List <int>(); int NE = Graph.MaxEdgeID; for (int eid = 0; eid < NE; ++eid) { if (!Graph.IsEdge(eid)) { continue; } if (FixedEdgeFilterF(eid)) { continue; } Index2i ev = Graph.GetEdgeV(eid); double dist = Graph.GetVertex(ev.a).Distance(Graph.GetVertex(ev.b)); if (dist > fMaxLen) { DGraph2.EdgeSplitInfo splitInfo; if (Graph.SplitEdge(eid, out splitInfo) == MeshResult.Ok && dist > 2 * fMaxLen) { queue.Add(eid); queue.Add(splitInfo.eNewBN); } } } while (queue.Count > 0) { int eid = queue[queue.Count - 1]; queue.RemoveAt(queue.Count - 1); if (!Graph.IsEdge(eid)) { continue; } Index2i ev = Graph.GetEdgeV(eid); double dist = Graph.GetVertex(ev.a).Distance(Graph.GetVertex(ev.b)); if (dist > fMaxLen) { DGraph2.EdgeSplitInfo splitInfo; if (Graph.SplitEdge(eid, out splitInfo) == MeshResult.Ok && dist > 2 * fMaxLen) { queue.Add(eid); queue.Add(splitInfo.eNewBN); } } } }
private static int MergeAppendVertex(DGraph2 graph, Vector2d vertex, double mergeThreshold) { for (int i = 0; i < graph.VertexCount; i++) { double d = vertex.Distance(graph.GetVertex(i)); if (d < mergeThreshold) { return(i); } } return(graph.AppendVertex(vertex)); }
public void AppendGraph(DGraph2 graph, int gid = -1) { int[] mapV = new int[graph.MaxVertexID]; foreach (int vid in graph.VertexIndices()) { mapV[vid] = this.AppendVertex(graph.GetVertex(vid)); } foreach (int eid in graph.EdgeIndices()) { Index2i ev = graph.GetEdgeV(eid); int use_gid = (gid == -1) ? graph.GetEdgeGroup(eid) : gid; this.AppendEdge(mapV[ev.a], mapV[ev.b], use_gid); } }
/* * Graph improvement */ /// <summary> /// connect open boundary vertices within distThresh, by inserting new segments /// </summary> public void ConnectOpenBoundaries(double distThresh) { int max_vid = Graph.MaxVertexID; for (int vid = 0; vid < max_vid; ++vid) { if (Graph.IsBoundaryVertex(vid) == false) { continue; } Vector2d v = Graph.GetVertex(vid); int snap_with = find_nearest_boundary_vertex(v, distThresh, vid); if (snap_with != -1) { Vector2d v2 = Graph.GetVertex(snap_with); Insert(v, v2); } } }
/// <summary> /// Convert cells to polygons, with optional filter. /// If filter returns false, polygon is not included in output /// </summary> public List <Polygon2d> CellsToPolygons(Func <Polygon2d, bool> FilterF = null) { var result = new List <Polygon2d>(); for (int i = 0; i < CellLoops.Count; ++i) { int[] loop = CellLoops[i]; var poly = new Polygon2d(); for (int k = 0; k < loop.Length; ++k) { poly.AppendVertex(Graph.GetVertex(loop[k])); } if (FilterF != null && FilterF(poly) == false) { continue; } result.Add(poly); } return(result); }
/// <summary> /// Decompose graph into simple polylines and polygons. /// </summary> public static Curves ExtractCurves(DGraph2 graph) { Curves c = new Curves(); c.Loops = new List <Polygon2d>(); c.Paths = new List <PolyLine2d>(); HashSet <int> used = new HashSet <int>(); // find boundary and junction vertices HashSet <int> boundaries = new HashSet <int>(); HashSet <int> junctions = new HashSet <int>(); foreach (int vid in graph.VertexIndices()) { if (graph.IsBoundaryVertex(vid)) { boundaries.Add(vid); } if (graph.IsJunctionVertex(vid)) { junctions.Add(vid); } } // walk paths from boundary vertices foreach (int start_vid in boundaries) { int vid = start_vid; int eid = graph.GetVtxEdges(vid)[0]; if (used.Contains(eid)) { continue; } PolyLine2d path = new PolyLine2d(); path.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; path.AppendVertex(graph.GetVertex(vid)); if (boundaries.Contains(vid) || junctions.Contains(vid)) { break; // done! } } c.Paths.Add(path); } // ok we should be done w/ boundary verts now... boundaries.Clear(); foreach (int start_vid in junctions) { foreach (int outgoing_eid in graph.VtxEdgesItr(start_vid)) { if (used.Contains(outgoing_eid)) { continue; } int vid = start_vid; int eid = outgoing_eid; PolyLine2d path = new PolyLine2d(); path.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; path.AppendVertex(graph.GetVertex(vid)); if (eid == int.MaxValue || junctions.Contains(vid)) { break; // done! } } c.Paths.Add(path); } } // all that should be left are continuous loops... foreach (int start_eid in graph.EdgeIndices()) { if (used.Contains(start_eid)) { continue; } int eid = start_eid; Index2i ev = graph.GetEdgeV(eid); int vid = ev.a; Polygon2d poly = new Polygon2d(); poly.AppendVertex(graph.GetVertex(vid)); while (true) { used.Add(eid); Index2i next = NextEdgeAndVtx(eid, vid, graph); eid = next.a; vid = next.b; poly.AppendVertex(graph.GetVertex(vid)); if (eid == int.MaxValue || junctions.Contains(vid)) { throw new Exception("how did this happen??"); } if (used.Contains(eid)) { break; } } poly.RemoveVertex(poly.VertexCount - 1); c.Loops.Add(poly); } return(c); }
protected virtual void do_split(Line2d line, bool insert_edges, int insert_gid) { if (EdgeSigns.Length < Graph.MaxVertexID) { EdgeSigns.resize(Graph.MaxVertexID); } foreach (int vid in Graph.VertexIndices()) { EdgeSigns[vid] = line.WhichSide(Graph.GetVertex(vid), OnVertexTol); } hits.Clear(); foreach (int eid in Graph.EdgeIndices()) { Index2i ev = Graph.GetEdgeV(eid); var signs = new Index2i(EdgeSigns[ev.a], EdgeSigns[ev.b]); if (signs.a * signs.b > 0) { continue; // both positive or negative, ignore } var hit = new edge_hit() { hit_eid = eid, vtx_signs = signs, hit_vid = -1 }; Vector2d a = Graph.GetVertex(ev.a); Vector2d b = Graph.GetVertex(ev.b); // parallel-edge case (both are zero) if (signs.a == signs.b) { if (a.DistanceSquared(b) > MathUtil.Epsilon) { // we need to somehow not insert a new segment for this span below. // so, insert two hit points for the ray-interval, with same eid. // This will result in this span being skipped by the same-eid test below // *however*, if other edges self-intersect w/ this segment, this will *not work* // and duplicate edges will be inserted hit.hit_vid = ev.a; hit.line_t = line.Project(a); hits.Add(hit); hit.hit_vid = ev.b; hit.line_t = line.Project(b); hits.Add(hit); } else { // degenerate edge - fall through to a == 0 case below signs.b = 1; } } if (signs.a == 0) { hit.hit_pos = a; hit.hit_vid = ev.a; hit.line_t = line.Project(a); } else if (signs.b == 0) { hit.hit_pos = b; hit.hit_vid = ev.b; hit.line_t = line.Project(b); } else { var intr = new IntrLine2Segment2(line, new Segment2d(a, b)); if (intr.Find() == false) { throw new Exception("GraphSplitter2d.Split: signs are different but ray did not it?"); } if (intr.IsSimpleIntersection) { hit.hit_pos = intr.Point; hit.line_t = intr.Parameter; } else { throw new Exception("GraphSplitter2d.Split: got parallel edge case!"); } } hits.Add(hit); } // sort by increasing ray-t hits.Sort((hit0, hit1) => { return(hit0.line_t.CompareTo(hit1.line_t)); }); // insert segments between successive intersection points int N = hits.Count; for (int i = 0; i < N - 1; ++i) { int j = i + 1; // note: skipping parallel segments depends on this eid == eid test (see above) if (hits[i].line_t == hits[j].line_t || hits[i].hit_eid == hits[j].hit_eid) { continue; } int vi = hits[i].hit_vid; int vj = hits[j].hit_vid; if (vi == vj && vi >= 0) { continue; } if (vi >= 0 && vj >= 0) { int existing = Graph.FindEdge(vi, vj); if (existing >= 0) { continue; } } if (vi == -1) { DGraph2.EdgeSplitInfo split; var result = Graph.SplitEdge(hits[i].hit_eid, out split); if (result != MeshResult.Ok) { throw new Exception("GraphSplitter2d.Split: first edge split failed!"); } vi = split.vNew; Graph.SetVertex(vi, hits[i].hit_pos); edge_hit tmp = hits[i]; tmp.hit_vid = vi; hits[i] = tmp; } if (vj == -1) { DGraph2.EdgeSplitInfo split; var result = Graph.SplitEdge(hits[j].hit_eid, out split); if (result != MeshResult.Ok) { throw new Exception("GraphSplitter2d.Split: second edge split failed!"); } vj = split.vNew; Graph.SetVertex(vj, hits[j].hit_pos); edge_hit tmp = hits[j]; tmp.hit_vid = vj; hits[j] = tmp; } // check if we actually want to add this segment if (InsideTestF != null) { Vector2d midpoint = 0.5 * (Graph.GetVertex(vi) + Graph.GetVertex(vj)); if (InsideTestF(midpoint) == false) { continue; } } if (insert_edges) { Graph.AppendEdge(vi, vj, insert_gid); } } }