-
Notifications
You must be signed in to change notification settings - Fork 0
/
TINData.cs
268 lines (242 loc) · 8.92 KB
/
TINData.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GISDemo
{
public class TINtriangle
{
public int pa, pb, pc;
public int ntbc, ntca,ntab;
public double segbc, segca, segab; //内插等值点在三边的位置;
public bool valid;
public TINtriangle(int pa_,int pb_,int pc_,int ntbc_=-1,int ntca_=-1,int ntab_=-1)
{
pa = pa_;
pb = pb_;
pc = pc_;
ntab = ntab_;
ntbc = ntbc_;
ntca = ntca_;
valid = true;
segbc = segca = segab = -1;
}
public void Rotate()
{
// (a,b,c) -> (b,c,a);
int tp = pa, tn = ntbc;
double ts=segbc;
pa = pb;
pb = pc;
pc = tp;
ntbc = ntca;
ntca = ntab;
ntab = tn;
segbc = segca;
segca = segab;
segab = ts;
}
public void Swap()
{
// (a,b,c)->(a,c,b)
int tp = pb,tn = ntca;
double ts = segca;
pb = pc;
pc = tp;
ntca = ntab;
ntab = tn;
segca = 1 - segab;
segab = 1 - segca;
segbc = 1 - segbc;
}
public void UpdateNeighbor(int t_old,int t_new)
{
if (ntbc == t_old) ntbc = t_new;
else if (ntca == t_old) ntca = t_new;
else if (ntab == t_old) ntab = t_new;
}
}
public class TINData
{
public List<PointZ> points;
public List<TINtriangle> triangles;
private double M;
public double BiasX,BiasY;
public TINData(List<PointRecord> data,double XBias,double YBias)
{
points = new List<PointZ>();
M = 0;
for(int i=0;i<data.Count;i++)
{
points.Add(new PointZ(data[i].X - XBias, data[i].Y - YBias, data[i].Z));
if (Math.Abs(data[i].X - XBias) > M) M = Math.Abs(data[i].X - XBias);
if (Math.Abs(data[i].Y - YBias) > M) M = Math.Abs(data[i].Y - YBias);
}
triangles = new List<TINtriangle>();
BiasX = XBias;
BiasY = YBias;
}
public void Delaunay()
{
// 建立一个货真价实、如假包换、满足空圆性质的Delaunay三角网
List<TINtriangle> temptri = new List<TINtriangle>();
// 包围三角形 编号:N,N+1,N+2
PointZ pr = new PointZ(3 * M, 0), pu = new PointZ(0, 3 * M), pl = new PointZ(-3 * M, -3 * M);
int N = points.Count,tc;
TINtriangle T;
points.Add(pr);
points.Add(pu);
points.Add(pl);
temptri.Add(new TINtriangle(N, N+1, N+2));
int inside;
PointZ P;
for(int i=0;i<N;i++)
{
P = points[i];
//找到点所在的三角形
inside = -1;
for(int t=0;t< temptri.Count;t++)
{
if (!temptri[t].valid) continue;
PointZ p1, p2,p3;
p1 = points[temptri[t].pa];
p2 = points[temptri[t].pb];
p3 = points[temptri[t].pc];
if (GeomTools.Point_in_Triangle(P,p1,p2,p3))
{
inside = t;
break;
}
}
T = temptri[inside];
tc = temptri.Count;
//新加入三角形:id为tc,tc+1,tc+2
TINtriangle tab, tbc, tca;
tab = new TINtriangle(i,T.pa, T.pb, T.ntab,tc + 1,tc+2);
tbc = new TINtriangle(i,T.pb, T.pc, T.ntbc,tc + 2,tc);
tca = new TINtriangle(i,T.pc, T.pa, T.ntca,tc, tc+1);
if (T.ntab>0) temptri[T.ntab].UpdateNeighbor(inside, tc);
if (T.ntbc > 0) temptri[T.ntbc].UpdateNeighbor(inside, tc + 1);
if (T.ntca > 0) temptri[T.ntca].UpdateNeighbor(inside, tc + 2);
temptri.Add(tab);
temptri.Add(tbc);
temptri.Add(tca);
T.valid = false;
LegalizeEdge(points, temptri, tc, 0);
LegalizeEdge(points, temptri, tc+1, 0);
LegalizeEdge(points, temptri, tc+2, 0);
}
//后处理
for(int t=0;t<temptri.Count;t++)
{
if (!temptri[t].valid) continue;
if (temptri[t].pa >= N || temptri[t].pb >= N || temptri[t].pc >= N) continue;
T = temptri[t];
triangles.Add(new TINtriangle(T.pa, T.pb, T.pc));
}
points.RemoveRange(N, 3);
}
public void NeighborLinks()
{
//对于固定的三角网,建立三角形的邻接关系
int N = triangles.Count;
HashSet<int> tp;
TINtriangle T, NT;
for(int t=0;t<N;t++)
{
T = triangles[t];
for (int u = 0; u < N; u++)
{
if (u == t) continue;
NT = triangles[u];
tp = new HashSet<int>();
tp.Add(NT.pa);
tp.Add(NT.pb);
tp.Add(NT.pc);
if (tp.Contains(T.pa) && tp.Contains(T.pb)) T.ntab = u;
else if (tp.Contains(T.pa) && tp.Contains(T.pc)) T.ntca = u;
else if (tp.Contains(T.pc) && tp.Contains(T.pb)) T.ntbc = u;
}
}
return;
}
private void LegalizeEdge(List<PointZ> plist,List<TINtriangle> tlist,int tri,int side)
{
//边反转,检查tri号三角形的边(0-bc,1-ca,2-ab)
TINtriangle T1 = tlist[tri],T2;
int ntri;
//对齐两三角形位置,使得T1.pb==T2.pb;T1.pc==T2.pc;
switch (side)
{
case 0:
ntri = T1.ntbc;
break;
case 1:
ntri = T1.ntca;
T1.Rotate();
break;
case 2:
ntri = T1.ntab;
T1.Rotate();
T1.Rotate();
break;
default:
ntri = -1;
break;
}
if (ntri == -1) return;
T2 = tlist[ntri];
HashSet<int> com = new HashSet<int>();
com.Add(T1.pb);
com.Add(T1.pc);
while(com.Contains(T2.pa)) T2.Rotate();
if (T2.pb != T1.pb) T2.Swap();
if (GeomTools.Concave(plist[T1.pb], plist[T1.pc], plist[T1.pa], plist[T2.pa])) return;
int M = plist.Count - 3;
bool diaglegal = true;
if(T1.pb>=M || T1.pc>=M)
{
if (T1.pa >= M || T2.pa >= M)
{
diaglegal = Math.Max(T1.pb, T1.pc) < Math.Max(T1.pa, T2.pa);
}
else
{
diaglegal = false;
}
}
else if(T1.pa>=M || T2.pa>=M)
{
diaglegal = true;
}
else if (Illegal(plist[T1.pb], plist[T1.pc], plist[T1.pa], plist[T2.pa]))
{
diaglegal = false;
}
if (!diaglegal)
{
TINtriangle tab, tac; // 编号tc,tc+1
int tc = tlist.Count;
tab = new TINtriangle(T1.pa, T1.pb, T2.pa, T2.ntab, tc+1, T1.ntab);
tac = new TINtriangle(T1.pa, T1.pc, T2.pa, T2.ntca, tc, T1.ntca);
if (T1.ntab > 0) tlist[T1.ntab].UpdateNeighbor(tri, tc);
if (T1.ntca > 0) tlist[T1.ntca].UpdateNeighbor(tri, tc+1);
if (T2.ntab > 0) tlist[T2.ntab].UpdateNeighbor(ntri, tc);
if (T2.ntca > 0) tlist[T2.ntca].UpdateNeighbor(ntri, tc + 1);
T1.valid = false;
T2.valid = false;
tlist.Add(tab);
tlist.Add(tac);
LegalizeEdge(plist, tlist, tc, 0);
LegalizeEdge(plist, tlist, tc+1, 0);
}
}
private bool Illegal(PointZ a,PointZ b,PointZ c,PointZ d)
{
//对角线ab 是否在四边形acbd中合法
double angsum = GeomTools.Angle(a, c, b) + GeomTools.Angle(a, d, b);
return angsum > Math.PI;
}
}
}