|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace g3 |
| 7 | +{ |
| 8 | + public class GraphSplitter2d |
| 9 | + { |
| 10 | + public DGraph2 Graph; |
| 11 | + |
| 12 | + public double OnVertexTol = MathUtil.Epsilonf; |
| 13 | + public int InsertedEdgesID = 1; |
| 14 | + |
| 15 | + public Func<Vector2d, bool> InsideTestF = null; |
| 16 | + |
| 17 | + public GraphSplitter2d(DGraph2 graph) |
| 18 | + { |
| 19 | + Graph = graph; |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | + DVector<int> EdgeSigns = new DVector<int>(); |
| 24 | + |
| 25 | + |
| 26 | + struct edge_hit |
| 27 | + { |
| 28 | + public int hit_eid; |
| 29 | + public Index2i vtx_signs; |
| 30 | + public int hit_vid; |
| 31 | + public Vector2d hit_pos; |
| 32 | + public double line_t; |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + List<edge_hit> hits = new List<edge_hit>(); |
| 37 | + |
| 38 | + public void InsertCutEdges(Line2d line) |
| 39 | + { |
| 40 | + if (EdgeSigns.Length < Graph.MaxVertexID) |
| 41 | + EdgeSigns.resize(Graph.MaxVertexID); |
| 42 | + foreach ( int vid in Graph.VertexIndices()) { |
| 43 | + EdgeSigns[vid] = line.WhichSide(Graph.GetVertex(vid), OnVertexTol); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + hits.Clear(); |
| 48 | + foreach ( int eid in Graph.EdgeIndices() ) { |
| 49 | + Index2i ev = Graph.GetEdgeV(eid); |
| 50 | + Index2i signs = new Index2i(EdgeSigns[ev.a], EdgeSigns[ev.b]); |
| 51 | + if (signs.a * signs.b > 0) |
| 52 | + continue; // both positive or negative, ignore |
| 53 | + |
| 54 | + edge_hit hit = new edge_hit() { hit_eid = eid, vtx_signs = signs, hit_vid = -1 }; |
| 55 | + Vector2d a = Graph.GetVertex(ev.a); |
| 56 | + Vector2d b = Graph.GetVertex(ev.b); |
| 57 | + |
| 58 | + // parallel-edge case (both are zero) |
| 59 | + if (signs.a == signs.b) { |
| 60 | + if ( a.DistanceSquared(b) > MathUtil.Epsilon ) { |
| 61 | + // we need to somehow not insert a new segment for this span below. |
| 62 | + // so, insert two hit points for the ray-interval, with same eid. |
| 63 | + // This will result in this span being skipped by the same-eid test below |
| 64 | + // *however*, if other edges self-intersect w/ this segment, this will *not work* |
| 65 | + // and duplicate edges will be inserted |
| 66 | + hit.hit_vid = ev.a; |
| 67 | + hit.line_t = line.Project(a); |
| 68 | + hits.Add(hit); |
| 69 | + edge_hit hit2 = hit; |
| 70 | + hit.hit_vid = ev.b; |
| 71 | + hit.line_t = line.Project(b); |
| 72 | + hits.Add(hit); |
| 73 | + } else { |
| 74 | + // degenerate edge - fall through to a == 0 case below |
| 75 | + signs.b = 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if ( signs.a == 0 ) { |
| 80 | + hit.hit_pos = a; |
| 81 | + hit.hit_vid = ev.a; |
| 82 | + hit.line_t = line.Project(a); |
| 83 | + } else if (signs.b == 0 ) { |
| 84 | + hit.hit_pos = b; |
| 85 | + hit.hit_vid = ev.b; |
| 86 | + hit.line_t = line.Project(b); |
| 87 | + } else { |
| 88 | + IntrLine2Segment2 intr = new IntrLine2Segment2(line, new Segment2d(a, b)); |
| 89 | + if (intr.Find() == false) |
| 90 | + throw new Exception("GraphSplitter2d.InsertCutEdges: signs are different but ray did not it?"); |
| 91 | + if ( intr.IsSimpleIntersection ) { |
| 92 | + hit.hit_pos = intr.Point; |
| 93 | + hit.line_t = intr.Parameter; |
| 94 | + } else { |
| 95 | + throw new Exception("GraphSplitter2d.InsertCutEdges: got parallel edge case!"); |
| 96 | + } |
| 97 | + } |
| 98 | + hits.Add(hit); |
| 99 | + } |
| 100 | + |
| 101 | + // sort by increasing ray-t |
| 102 | + hits.Sort((hit0, hit1) => { return hit0.line_t.CompareTo(hit1.line_t); }); |
| 103 | + |
| 104 | + // insert segments between successive intersection points |
| 105 | + int N = hits.Count; |
| 106 | + for ( int i = 0; i < N-1; ++i ) { |
| 107 | + int j = i + 1; |
| 108 | + // note: skipping parallel segments depends on this eid == eid test (see above) |
| 109 | + if (hits[i].line_t == hits[j].line_t || hits[i].hit_eid == hits[j].hit_eid) |
| 110 | + continue; |
| 111 | + |
| 112 | + int vi = hits[i].hit_vid; |
| 113 | + if (vi == -1) { |
| 114 | + DGraph2.EdgeSplitInfo split; |
| 115 | + var result = Graph.SplitEdge(hits[i].hit_eid, out split); |
| 116 | + if (result != MeshResult.Ok) |
| 117 | + throw new Exception("GraphSplitter2d.InsertCutEdges: first edge split failed!"); |
| 118 | + vi = split.vNew; |
| 119 | + Graph.SetVertex(vi, hits[i].hit_pos); |
| 120 | + } |
| 121 | + |
| 122 | + int vj = hits[j].hit_vid; |
| 123 | + if ( vj == -1) { |
| 124 | + DGraph2.EdgeSplitInfo split; |
| 125 | + var result = Graph.SplitEdge(hits[j].hit_eid, out split); |
| 126 | + if (result != MeshResult.Ok) |
| 127 | + throw new Exception("GraphSplitter2d.InsertCutEdges: second edge split failed!"); |
| 128 | + vj = split.vNew; |
| 129 | + Graph.SetVertex(vj, hits[j].hit_pos); |
| 130 | + } |
| 131 | + |
| 132 | + // check if we actually want to add this segment |
| 133 | + if ( InsideTestF != null ) { |
| 134 | + Vector2d midpoint = 0.5 * (Graph.GetVertex(vi) + Graph.GetVertex(vj)); |
| 135 | + if (InsideTestF(midpoint) == false) |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + Graph.AppendEdge(vi, vj, InsertedEdgesID); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + } |
| 148 | +} |
0 commit comments