-
Notifications
You must be signed in to change notification settings - Fork 15
/
Normal.cs
360 lines (304 loc) · 15.5 KB
/
Normal.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
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
using BH.oM.Geometry;
using BH.oM.Base.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
namespace BH.Engine.Geometry
{
public static partial class Query
{
/***************************************************/
/**** Public Methods - Surfaces ****/
/***************************************************/
[Description("Returns a vector normal to a given face of the mesh.")]
[Input("face", "The Face to get the normal to.")]
[Input("mesh", "The Mesh containing the face to get the normal to.")]
[Output("normal", "Vector normal to the given mesh face.")]
public static Vector Normal(this Face face, Mesh mesh)
{
List<Point> vertices = mesh.Vertices;
Face meshFace = mesh.Faces[mesh.Faces.IndexOf(face)];
Point pA = vertices[meshFace.A];
Point pB = vertices[meshFace.B];
Point pC = vertices[meshFace.C];
Vector normal;
if (!meshFace.IsQuad())
normal = CrossProduct(pB - pA, pC - pB);
else
{
Point pD = vertices[(meshFace.D)];
normal = (CrossProduct(pA - pD, pB - pA)) + (CrossProduct(pC - pB, pD - pC));
}
return normal.Normalise();
}
/***************************************************/
[Description("Returns a list of vectors normal to each face of the mesh.")]
[Input("mesh", "The Mesh to get the normals to.")]
[Output("normals", "List of vectors normal to the faces of a given mesh.")]
public static List<Vector> Normals(this Mesh mesh)
{
List<Vector> normals = new List<Vector>(mesh.Faces.Count);
List<Point> vertices = mesh.Vertices;
List<Face> faces = mesh.Faces;
for (int i = 0; i < mesh.Faces.Count; i++)
{
Point pA = vertices[(faces[i].A)];
Point pB = vertices[(faces[i].B)];
Point pC = vertices[(faces[i].C)];
if (!faces[i].IsQuad())
{
Vector normal = CrossProduct(pB - pA, pC - pB);
normals.Add(normal.Normalise());
}
else
{
Point pD = vertices[(faces[i].D)];
Vector normal = (CrossProduct(pA - pD, pB - pA)) + (CrossProduct(pC - pB, pD - pC));
normals.Add(normal.Normalise());
}
}
return normals.ToList();
}
/***************************************************/
[Description("Returns a list of vectors normal to each face of the mesh.")]
[Input("mesh", "The Mesh3D to get the normals to.")]
[Output("normals", "List of vectors normal to the faces of a given mesh.")]
public static List<Vector> Normals(this Mesh3D mesh)
{
return mesh.ToMesh().Normals();
}
/***************************************************/
/**** Public Methods - Curves ****/
/***************************************************/
[Description("Returns a vector normal to the plane of a given curve, oriented according to the right hand rule. Works only for closed, planar curves.")]
[Input("curve", "The Polyline to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector Normal(this IPolyline curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
if (!curve.IIsPlanar(tolerance))
{
Base.Compute.RecordError("A single normal vector is not unambiguously definable for non-planar curves.");
return null;
}
else if (!curve.IIsClosed(tolerance))
{
Base.Compute.RecordError("A single normal vector is not unambiguously definable for open curves.");
return null;
}
//Turning of check as shown to be an extreme performance burden. To be handled more long term in https://github.com/BHoM/BHoM_Engine/issues/2803
//else if (curve.IsSelfIntersecting(tolerance))
// Base.Compute.RecordWarning("Input curve is self-intersecting. Resulting normal vector might be flipped.");
List<Point> ctrlPts = curve.IControlPoints();
Point avg = ctrlPts.Average();
Vector normal = new Vector();
//Get out normal, from cross products between vectors from the average point to adjacent control points on the curve
for (int i = 0; i < ctrlPts.Count - 1; i++)
normal += (ctrlPts[i] - avg).CrossProduct(ctrlPts[i + 1] - avg);
if (normal.Length() < tolerance)
{
Base.Compute.RecordError("Couldn't calculate a normal vector of the given curve.");
return null;
}
normal = normal.Normalise();
//Check if normal needs to be flipped from the right hand rule
if (!curve.IsClockwise(normal, tolerance))
normal = -normal;
return normal;
}
/***************************************************/
[Description("Returns a vector normal to the plane of a given curve, oriented according to the right hand rule. Works only for closed and planar curves.")]
[Input("curve", "The PolyCurve to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector Normal(this IPolyCurve curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
List<ICurve> crvs = new List<ICurve>(curve.ISubParts());
if (crvs.Any(x => x is NurbsCurve))
{
Base.Compute.RecordError("Querying normal from PolyCurves with segments of type NurbsCurve is not supported.");
return null;
}
if (!curve.IIsPlanar(tolerance))
{
Base.Compute.RecordError("A single normal vector is not unambiguously definable for non-planar curves.");
return null;
}
else if (!curve.IIsClosed(tolerance))
{
Base.Compute.RecordError("A single normal vector is not unambiguously definable for open curves.");
return null;
}
//Turning of check as shown to be an extreme performance burden. To be handled more long term in https://github.com/BHoM/BHoM_Engine/issues/2803
//else if (curve.IsSelfIntersecting(tolerance))
// Base.Compute.RecordWarning("Input curve is self-intersecting. Resulting normal vector might be flipped.");
if (crvs.Count() == 0)
return null;
else if (crvs.Count() == 1)
return crvs[0].INormal();
else
{
List<Point> points = new List<Point>();
foreach (ICurve crv in crvs)
{
if (crv is Line)
points.Add((crv as Line).End);
else if (crv is Arc)
{
for (int j = 1; j < 5; j++)
{
points.Add((crv as Arc).PointAtParameter(j * 0.25));
}
}
}
Point avg = points.Average();
Vector normal = new Vector();
//Get out normal, from cross products between vectors from the average point to adjecent controlpoints on the curve
for (int i = 0; i < points.Count - 1; i++)
normal += (points[i] - avg).CrossProduct(points[i + 1] - avg);
if (normal.Length() < tolerance)
{
Base.Compute.RecordError("Couldn't calculate a normal vector of the given curve.");
return null;
}
normal = normal.Normalise();
//Check if normal needs to be flipped from the right hand rule
if (!curve.IsClockwise(normal, tolerance))
normal = -normal;
return normal;
}
}
/***************************************************/
[Description("Returns a vector normal to the plane of a circle.")]
[Input("curve", "The Circle to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector Normal(this Circle curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
return curve.Normal;
}
/***************************************************/
[Description("Returns a vector normal to the plane of an ellipse.")]
[Input("curve", "The Ellipse to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector Normal(this Ellipse curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
Vector normal = (curve.Axis1).CrossProduct(curve.Axis2);
return normal;
}
/***************************************************/
[Description("Returns a vector normal to the plane of an arc.")]
[Input("curve", "The Arc to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector Normal(this Arc curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
if (curve.Angle() > 0)
return curve.CoordinateSystem.Z;
else
return curve.CoordinateSystem.Z.Reverse();
}
/***************************************************/
/**** Public Methods - Surfaces ****/
/***************************************************/
[Description("Returns a vector normal to the planar surface.")]
[Input("surface", "The PlanarSurface to get the normal to.")]
[Output("normal", "Vector normal to the surface.")]
public static Vector Normal(this PlanarSurface surface)
{
if (surface == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null surface.");
return null;
}
return surface.ExternalBoundary.INormal();
}
/***************************************************/
/**** Public Methods - Interfaces ****/
/***************************************************/
[Description("Interface method that returns a vector normal to the plane of any ICurve and oriented according to the right hand rule. Works only for closed and planar curves with an exception for single Arcs.")]
[Input("curve", "The ICurve to get the normal to.")]
[Input("tolerance", "Distance tolerance used in geometry processing, default set to BH.oM.Geometry.Tolerance.Distance.")]
[Output("normal", "Vector normal to the plane of a curve.")]
public static Vector INormal(this ICurve curve, double tolerance = Tolerance.Distance)
{
if (curve == null)
{
Engine.Base.Compute.RecordError("Cannot compute the normal of a null curve.");
return null;
}
return Normal(curve as dynamic, tolerance);
}
/***************************************************/
[Description("Interface method that returns the list of vectors normal to any IGeometry.")]
[Input("geometry", "The IGeometry to get the normal to.")]
[Output("normals", "List of vectors normal to the given geometry.")]
public static List<Vector> INormals(this IGeometry geometry)
{
return Normals(geometry as dynamic);
}
/***************************************************/
/**** Private Fallback Methods ****/
/***************************************************/
private static Vector Normal(this ICurve curve, double tolerance = Tolerance.Distance)
{
Base.Compute.RecordError($"Normal is not implemented for ICurves of type: {curve.GetType().Name}.");
return null;
}
/***************************************************/
private static List<Vector> Normals(this IGeometry geometry)
{
Base.Compute.RecordError($"Normals is not implemented for IGeometry of type: {geometry.GetType().Name}.");
return null;
}
/***************************************************/
}
}