forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphSplitter2d.cs
192 lines (159 loc) · 7.04 KB
/
GraphSplitter2d.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace g3
{
/// <summary>
/// This class is used to bisect an existing DGraph2 with infinite lines.
/// This is easier than inserting new segments, which can be done using Arrangement2d.
///
/// Computations are done in double precision. Use at your own risk.
///
/// [TODO]
/// - computation of signs for a split-line is currently O(N). If inserting many
/// parallel lines, can improve this using standard sorting.
/// </summary>
public class GraphSplitter2d
{
public DGraph2 Graph;
/// <summary>
/// tolerance for WhichSide(vtx) tests
/// </summary>
public double OnVertexTol = MathUtil.Epsilonf;
/// <summary>
/// default ID for new edges, can override in specific functions
/// </summary>
public int InsertedEdgesID = 1;
/// <summary>
/// when inserting new segments, we check if their midpoint passes this test
/// </summary>
public Func<Vector2d, bool> InsideTestF = null;
public GraphSplitter2d(DGraph2 graph)
{
Graph = graph;
}
/// <summary>
/// split all graph edges that intersect line, and insert segments
/// connecting these points
/// </summary>
public void InsertLine(Line2d line, int insert_edges_id = -1)
{
if (insert_edges_id == -1)
insert_edges_id = InsertedEdgesID;
do_split(line, true, insert_edges_id);
}
DVector<int> EdgeSigns = new DVector<int>();
struct edge_hit
{
public int hit_eid;
public Index2i vtx_signs;
public int hit_vid;
public Vector2d hit_pos;
public double line_t;
}
List<edge_hit> hits = new List<edge_hit>();
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);
Index2i signs = new Index2i(EdgeSigns[ev.a], EdgeSigns[ev.b]);
if (signs.a * signs.b > 0)
continue; // both positive or negative, ignore
edge_hit 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 {
IntrLine2Segment2 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);
}
}
}
}