-
Notifications
You must be signed in to change notification settings - Fork 1
/
CerverFunctions.cs
358 lines (275 loc) · 11.2 KB
/
CerverFunctions.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bentley.GenerativeComponents.MicroStation;
using Bentley.GenerativeComponents.GeneralPurpose;
using Bentley.GenerativeComponents.GCScript;
using Bentley.GenerativeComponents.GCScript.GCTypes;
using Bentley.GenerativeComponents.GCScript.NameScopes;
using Bentley.GenerativeComponents.Features;
using Bentley.GenerativeComponents.Features.Specific;
using Bentley.Geometry;
using Bentley.Interop.MicroStationDGN;
using Cerver.Functions;
namespace Cerver.Functions
{
public class CerverFunctions
{
public static double Sqr(double num)
{
return num * num;
}
private static double SquaredDistance(DPoint3d pt0, DPoint3d pt1)
{
double dx = pt0.X - pt1.X;
double dy = pt0.Y - pt1.Y;
double dz = pt0.Z - pt1.Z;
return dx * dx + dy * dy + dz * dz;
}
static public double ListMin(double[] list)
{
double min = list[0];
foreach (double d in list)
{
if (d < min) min = d;
}
return min;
}
static public double ListMax(double[] list)
{
double max = list[0];
foreach (double d in list)
{
if (d > max) max = d;
}
return max;
}
public static double DPDistance(DPoint3d ptA, DPoint3d ptB)
{
double dx = Sqr((ptA.X - ptB.X));
double dy = Sqr((ptA.Y - ptB.Y));
double dz = Sqr((ptA.Z - ptB.Z));
double dist = Math.Sqrt((dx + dy + dz));
return dist;
}
public static double Distance(IPoint ptA, IPoint ptB)
{
return DPDistance(ptA.DPoint3d, ptB.DPoint3d);
}
public static DPoint3d closestPointOnSurf(DPoint3d point, ISurface surf, double tol, out double Dist, out Point2d UV, out DVector3d normal)
{
Bentley.Interop.MicroStationDGN.Point3d cpRef = new Bentley.Interop.MicroStationDGN.Point3d();
cpRef.X = point.X;
cpRef.Y = point.Y;
cpRef.Z = point.Z;
Bentley.Interop.MicroStationDGN.Point3d cp = new Bentley.Interop.MicroStationDGN.Point3d();
Bentley.Interop.MicroStationDGN.Point2d cp2d = new Bentley.Interop.MicroStationDGN.Point2d();
Dist = GeometryTools.BSplineSurfaceComputeMinimumDistance(ref cp, ref cp2d, ref cpRef, tol, surf.com_bsplineSurface);
UV = new Point2d();
UV.X = cp2d.X;
UV.Y = cp2d.Y;
normal = NormalAtUVParameterOnSurface(surf.com_bsplineSurface, UV.X, UV.Y);
return new DPoint3d(cp.X, cp.Y, cp.Z);
}
public static DPoint3d closestPointOnSurf(DPoint3d point, ISurface surf, double tol)
{
Bentley.Interop.MicroStationDGN.Point3d cpRef = new Bentley.Interop.MicroStationDGN.Point3d();
cpRef.X = point.X;
cpRef.Y = point.Y;
cpRef.Z = point.Z;
Bentley.Interop.MicroStationDGN.Point3d cp = new Bentley.Interop.MicroStationDGN.Point3d();
Bentley.Interop.MicroStationDGN.Point2d cp2d = new Bentley.Interop.MicroStationDGN.Point2d();
GeometryTools.BSplineSurfaceComputeMinimumDistance(ref cp, ref cp2d, ref cpRef, tol, surf.com_bsplineSurface);
return new DPoint3d(cp.X, cp.Y, cp.Z);
}
public static bool isPointInside(ISurface bs, DPoint3d testPt, double tol, out DVector3d normal, out DPoint3d ClosestPoint)
{
Point2d uvPoint = new Point2d();
DVector3d norm;
double dist = 0;
DPoint3d cp = closestPointOnSurf(testPt, bs, tol, out dist, out uvPoint, out norm);
normal = norm;
var testVec = new DVector3d(ref cp, ref testPt);
ClosestPoint = cp;
if (norm.AngleTo(ref testVec).Degrees <= 90)
{
return false;
}
else return true;
}
public static bool isPointInside(ISurface bs, DPoint3d testPt, double tol)
{
DPoint3d cp;
DVector3d normal;
return isPointInside(bs, testPt, tol, out normal, out cp);
}
public static DPoint3d MeshCP(Mesh m, DPoint3d p)
{
double cdist = m.Vertices[0].DPoint3d.Distance(ref p);
double dist;
DPoint3d cp = m.Vertices[0].DPoint3d;
foreach (Point mp in m.Vertices)
{
dist = mp.DPoint3d.Distance(ref p);
if (dist < cdist)
{
cp = mp.DPoint3d;
}
}
return cp;
}
public static DSegment3d[] GetMeshEdges(Mesh m, out int[] startVtx, out int[] endVtx)
{
Dictionary<string, DSegment3d> edgeDic = new Dictionary<string, DSegment3d>(m.Vertices.Length);
string key, keyr;
DPoint3d p0, p1;
List<int> svtx = new List<int>(m.Vertices.Length * 2);
List<int> evtx = new List<int>(m.Vertices.Length * 2);
string hash1, hash2;
foreach (var f in m.Indices)
{
for (int i = 0; i < f.Length; i++)
{
hash2 = m.Vertices[f[i] - 1].GetHashCode().ToString();
if (i < f.Length - 1)
{
hash1 = m.Vertices[f[i + 1] - 1].GetHashCode().ToString();
key = hash2 + "|" + hash1;
keyr = hash1 + "|" + hash2;
}
else
{
hash1 = m.Vertices[f[0] - 1].GetHashCode().ToString();
key = hash2 + "|" + hash1;
keyr = hash1 + "|" + hash2;
}
if (!edgeDic.ContainsKey(key) && !edgeDic.ContainsKey(keyr))
{
if (i < f.Length - 1)
{
p0 = m.Vertices[f[i] - 1].DPoint3d;
p1 = m.Vertices[f[i + 1] - 1].DPoint3d;
svtx.Add(f[i] - 1);
evtx.Add(f[i + 1] - 1);
}
else
{
p0 = m.Vertices[f[i] - 1].DPoint3d;
p1 = m.Vertices[f[0] - 1].DPoint3d;
svtx.Add(f[i] - 1);
evtx.Add(f[0] - 1);
}
try
{
edgeDic.Add(key, new DSegment3d(ref p0, ref p1));
}catch (ArgumentException)
{
Feature.Print("key exits");
}
}
}
}
startVtx = svtx.ToArray();
endVtx = evtx.ToArray();
return edgeDic.Values.ToArray();
}
public static int[] GetConnectedPointID(Mesh m, DPoint3d centerPoint)
{
int ptid = -1;
for (int i = 0; i < m.Vertices.Length; i++)
{
if (m.Vertices[i].X == centerPoint.X && m.Vertices[i].Y == centerPoint.Y && m.Vertices[i].Z == centerPoint.Z)
{
ptid = i;
break;
}
}
return GetConnectedPointID(m, ptid);
}
public static int[] GetConnectedPointID(Mesh m, int vtxId)
{
int ptid = vtxId;
int[] svtx, evtx;
DSegment3d[] edges = GetMeshEdges(m, out svtx, out evtx);
Dictionary<int, int> connectedID = new Dictionary<int, int>(6);
for (int i = 0; i < svtx.Length; i++)
{
if (svtx[i] == ptid) connectedID.Add(evtx[i], evtx[i]);
if (evtx[i] == ptid) connectedID.Add(svtx[i], svtx[i]);
}
return connectedID.Values.ToArray();
}
public static DVector3d NormalAtUVParameterOnSurface(BsplineSurface com_bsplineSurface, double U, double V)
{
if (com_bsplineSurface != null)
{
int arg_0C_0 = com_bsplineSurface.UPolesCount;
int arg_13_0 = com_bsplineSurface.VPolesCount;
if (U >= 1.0) U = 0.99999;
if (U <= 0.0) U = 1E-05;
if (V >= 1.0) V = 0.99999;
if (V <= 0.0) V = 1E-05;
if (com_bsplineSurface.BoundsCount > 0 && !Translation.IsPointUVparametersWithinSurfaceBounds(com_bsplineSurface, U, V))
{
return DVector3d.Zero;
}
Point2d point2d = DgnTools.ToPoint2d(new DPoint2d(U, V));
FirstPartials3d firstPartials3d = default(FirstPartials3d);
Point3d source = default(Point3d);
Point3d source2 = DgnTools.ToPoint3d(DPoint3d.Zero);
try
{
source2 = com_bsplineSurface.EvaluatePointDerivatives1(ref firstPartials3d, ref source, ref point2d);
}
catch
{
}
DPoint3d origin = DgnTools.ToDPoint3d(source2);
DVector3d dVector3d = DgnTools.ToDVector3d(source);
return dVector3d;
}
return DVector3d.Zero;
}
}
}
namespace Cerver.GCExtensionMethods
{
public static class MeshExtensions
{
public static DSegment3d[] Edges(this Mesh m)
{
int[] svtx, evtx;
return CerverFunctions.GetMeshEdges(m, out svtx, out evtx);
}
public static int[] ConnectedVtxID(this Mesh m, DPoint3d searchPoint)
{
return CerverFunctions.GetConnectedPointID(m, searchPoint);
}
public static int[] ConnectedVtxID(this Mesh m, int vtxIndex)
{
return CerverFunctions.GetConnectedPointID(m,vtxIndex );
}
public static DPoint3d[] ConnectedVtx(this Mesh m, DPoint3d searchPoint)
{
int[] conectedVtxID = CerverFunctions.GetConnectedPointID(m, searchPoint);
List<DPoint3d> connectedVtx = new List<DPoint3d>(conectedVtxID.Length);
foreach (var id in conectedVtxID)
{
connectedVtx.Add(m.Vertices[id].DPoint3d);
}
return connectedVtx.ToArray();
}
public static DPoint3d[] ConnectedVtx(this Mesh m, DPoint3d searchPoint, out int[] conectedVtxIDs)
{
int[] conectedVtxID = CerverFunctions.GetConnectedPointID(m, searchPoint);
conectedVtxIDs = conectedVtxID;
List<DPoint3d> connectedVtx = new List<DPoint3d>(conectedVtxID.Length);
foreach (var id in conectedVtxID)
{
connectedVtx.Add(m.Vertices[id].DPoint3d);
}
return connectedVtx.ToArray();
}
}
}