@itwin/core-geometry 4.0.0-dev.22 → 4.0.0-dev.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/Geometry.d.ts +23 -0
- package/lib/cjs/Geometry.d.ts.map +1 -1
- package/lib/cjs/Geometry.js +25 -1
- package/lib/cjs/Geometry.js.map +1 -1
- package/lib/cjs/geometry3d/BarycentricTriangle.d.ts +195 -8
- package/lib/cjs/geometry3d/BarycentricTriangle.d.ts.map +1 -1
- package/lib/cjs/geometry3d/BarycentricTriangle.js +459 -11
- package/lib/cjs/geometry3d/BarycentricTriangle.js.map +1 -1
- package/lib/cjs/geometry3d/GrowableXYArray.d.ts +1 -1
- package/lib/cjs/geometry3d/GrowableXYArray.d.ts.map +1 -1
- package/lib/cjs/geometry3d/GrowableXYArray.js +1 -1
- package/lib/cjs/geometry3d/GrowableXYArray.js.map +1 -1
- package/lib/cjs/geometry3d/IndexedXYCollection.d.ts +22 -7
- package/lib/cjs/geometry3d/IndexedXYCollection.d.ts.map +1 -1
- package/lib/cjs/geometry3d/IndexedXYCollection.js +41 -5
- package/lib/cjs/geometry3d/IndexedXYCollection.js.map +1 -1
- package/lib/cjs/geometry3d/IndexedXYZCollection.d.ts +58 -4
- package/lib/cjs/geometry3d/IndexedXYZCollection.d.ts.map +1 -1
- package/lib/cjs/geometry3d/IndexedXYZCollection.js +102 -4
- package/lib/cjs/geometry3d/IndexedXYZCollection.js.map +1 -1
- package/lib/cjs/geometry3d/Point2dArrayCarrier.d.ts +10 -0
- package/lib/cjs/geometry3d/Point2dArrayCarrier.d.ts.map +1 -1
- package/lib/cjs/geometry3d/Point2dArrayCarrier.js +14 -0
- package/lib/cjs/geometry3d/Point2dArrayCarrier.js.map +1 -1
- package/lib/cjs/geometry3d/Point3dArrayCarrier.d.ts +0 -6
- package/lib/cjs/geometry3d/Point3dArrayCarrier.d.ts.map +1 -1
- package/lib/cjs/geometry3d/Point3dArrayCarrier.js +0 -6
- package/lib/cjs/geometry3d/Point3dArrayCarrier.js.map +1 -1
- package/lib/cjs/geometry3d/Point3dVector3d.d.ts +1 -1
- package/lib/cjs/geometry3d/Point3dVector3d.js +2 -2
- package/lib/cjs/geometry3d/Point3dVector3d.js.map +1 -1
- package/lib/cjs/geometry3d/PointHelpers.d.ts +14 -1
- package/lib/cjs/geometry3d/PointHelpers.d.ts.map +1 -1
- package/lib/cjs/geometry3d/PointHelpers.js +33 -1
- package/lib/cjs/geometry3d/PointHelpers.js.map +1 -1
- package/lib/cjs/geometry3d/PolygonOps.d.ts +120 -10
- package/lib/cjs/geometry3d/PolygonOps.d.ts.map +1 -1
- package/lib/cjs/geometry3d/PolygonOps.js +412 -12
- package/lib/cjs/geometry3d/PolygonOps.js.map +1 -1
- package/lib/cjs/polyface/FacetLocationDetail.d.ts +264 -0
- package/lib/cjs/polyface/FacetLocationDetail.d.ts.map +1 -0
- package/lib/cjs/polyface/FacetLocationDetail.js +378 -0
- package/lib/cjs/polyface/FacetLocationDetail.js.map +1 -0
- package/lib/cjs/polyface/IndexedPolyfaceVisitor.d.ts +2 -5
- package/lib/cjs/polyface/IndexedPolyfaceVisitor.d.ts.map +1 -1
- package/lib/cjs/polyface/IndexedPolyfaceVisitor.js +5 -2
- package/lib/cjs/polyface/IndexedPolyfaceVisitor.js.map +1 -1
- package/lib/cjs/polyface/PolyfaceBuilder.d.ts +20 -14
- package/lib/cjs/polyface/PolyfaceBuilder.d.ts.map +1 -1
- package/lib/cjs/polyface/PolyfaceBuilder.js +21 -17
- package/lib/cjs/polyface/PolyfaceBuilder.js.map +1 -1
- package/lib/cjs/polyface/PolyfaceData.d.ts +1 -1
- package/lib/cjs/polyface/PolyfaceData.d.ts.map +1 -1
- package/lib/cjs/polyface/PolyfaceData.js.map +1 -1
- package/lib/cjs/polyface/PolyfaceQuery.d.ts +22 -1
- package/lib/cjs/polyface/PolyfaceQuery.d.ts.map +1 -1
- package/lib/cjs/polyface/PolyfaceQuery.js +52 -2
- package/lib/cjs/polyface/PolyfaceQuery.js.map +1 -1
- package/lib/cjs/solid/Sphere.d.ts +5 -5
- package/lib/cjs/solid/Sphere.js +5 -5
- package/lib/cjs/solid/Sphere.js.map +1 -1
- package/lib/esm/Geometry.d.ts +23 -0
- package/lib/esm/Geometry.d.ts.map +1 -1
- package/lib/esm/Geometry.js +24 -0
- package/lib/esm/Geometry.js.map +1 -1
- package/lib/esm/geometry3d/BarycentricTriangle.d.ts +195 -8
- package/lib/esm/geometry3d/BarycentricTriangle.d.ts.map +1 -1
- package/lib/esm/geometry3d/BarycentricTriangle.js +459 -12
- package/lib/esm/geometry3d/BarycentricTriangle.js.map +1 -1
- package/lib/esm/geometry3d/GrowableXYArray.d.ts +1 -1
- package/lib/esm/geometry3d/GrowableXYArray.d.ts.map +1 -1
- package/lib/esm/geometry3d/GrowableXYArray.js +1 -1
- package/lib/esm/geometry3d/GrowableXYArray.js.map +1 -1
- package/lib/esm/geometry3d/IndexedXYCollection.d.ts +22 -7
- package/lib/esm/geometry3d/IndexedXYCollection.d.ts.map +1 -1
- package/lib/esm/geometry3d/IndexedXYCollection.js +41 -5
- package/lib/esm/geometry3d/IndexedXYCollection.js.map +1 -1
- package/lib/esm/geometry3d/IndexedXYZCollection.d.ts +58 -4
- package/lib/esm/geometry3d/IndexedXYZCollection.d.ts.map +1 -1
- package/lib/esm/geometry3d/IndexedXYZCollection.js +103 -5
- package/lib/esm/geometry3d/IndexedXYZCollection.js.map +1 -1
- package/lib/esm/geometry3d/Point2dArrayCarrier.d.ts +10 -0
- package/lib/esm/geometry3d/Point2dArrayCarrier.d.ts.map +1 -1
- package/lib/esm/geometry3d/Point2dArrayCarrier.js +14 -0
- package/lib/esm/geometry3d/Point2dArrayCarrier.js.map +1 -1
- package/lib/esm/geometry3d/Point3dArrayCarrier.d.ts +0 -6
- package/lib/esm/geometry3d/Point3dArrayCarrier.d.ts.map +1 -1
- package/lib/esm/geometry3d/Point3dArrayCarrier.js +0 -6
- package/lib/esm/geometry3d/Point3dArrayCarrier.js.map +1 -1
- package/lib/esm/geometry3d/Point3dVector3d.d.ts +1 -1
- package/lib/esm/geometry3d/Point3dVector3d.js +2 -2
- package/lib/esm/geometry3d/Point3dVector3d.js.map +1 -1
- package/lib/esm/geometry3d/PointHelpers.d.ts +14 -1
- package/lib/esm/geometry3d/PointHelpers.d.ts.map +1 -1
- package/lib/esm/geometry3d/PointHelpers.js +33 -1
- package/lib/esm/geometry3d/PointHelpers.js.map +1 -1
- package/lib/esm/geometry3d/PolygonOps.d.ts +120 -10
- package/lib/esm/geometry3d/PolygonOps.d.ts.map +1 -1
- package/lib/esm/geometry3d/PolygonOps.js +411 -12
- package/lib/esm/geometry3d/PolygonOps.js.map +1 -1
- package/lib/esm/polyface/FacetLocationDetail.d.ts +264 -0
- package/lib/esm/polyface/FacetLocationDetail.d.ts.map +1 -0
- package/lib/esm/polyface/FacetLocationDetail.js +371 -0
- package/lib/esm/polyface/FacetLocationDetail.js.map +1 -0
- package/lib/esm/polyface/IndexedPolyfaceVisitor.d.ts +2 -5
- package/lib/esm/polyface/IndexedPolyfaceVisitor.d.ts.map +1 -1
- package/lib/esm/polyface/IndexedPolyfaceVisitor.js +5 -2
- package/lib/esm/polyface/IndexedPolyfaceVisitor.js.map +1 -1
- package/lib/esm/polyface/PolyfaceBuilder.d.ts +20 -14
- package/lib/esm/polyface/PolyfaceBuilder.d.ts.map +1 -1
- package/lib/esm/polyface/PolyfaceBuilder.js +21 -17
- package/lib/esm/polyface/PolyfaceBuilder.js.map +1 -1
- package/lib/esm/polyface/PolyfaceData.d.ts +1 -1
- package/lib/esm/polyface/PolyfaceData.d.ts.map +1 -1
- package/lib/esm/polyface/PolyfaceData.js.map +1 -1
- package/lib/esm/polyface/PolyfaceQuery.d.ts +22 -1
- package/lib/esm/polyface/PolyfaceQuery.d.ts.map +1 -1
- package/lib/esm/polyface/PolyfaceQuery.js +52 -2
- package/lib/esm/polyface/PolyfaceQuery.js.map +1 -1
- package/lib/esm/solid/Sphere.d.ts +5 -5
- package/lib/esm/solid/Sphere.js +5 -5
- package/lib/esm/solid/Sphere.js.map +1 -1
- package/package.json +4 -4
|
@@ -7,11 +7,100 @@
|
|
|
7
7
|
* @module CartesianGeometry
|
|
8
8
|
*/
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.BarycentricTriangle = void 0;
|
|
10
|
+
exports.BarycentricTriangle = exports.TriangleLocationDetail = void 0;
|
|
11
|
+
const core_bentley_1 = require("@itwin/core-bentley");
|
|
11
12
|
const Geometry_1 = require("../Geometry");
|
|
13
|
+
const Matrix3d_1 = require("./Matrix3d");
|
|
12
14
|
const Point3dVector3d_1 = require("./Point3dVector3d");
|
|
15
|
+
const Ray3d_1 = require("./Ray3d");
|
|
13
16
|
/**
|
|
14
|
-
*
|
|
17
|
+
* Carries data about a location in the plane of a triangle.
|
|
18
|
+
* * Each instance carries both world and barycentric coordinates for the point, and provides query services on the latter.
|
|
19
|
+
* * No tolerance is used when querying barycentric coordinates (e.g., `isInsideOrOn`, `classify`). Use `snapLocalToEdge`
|
|
20
|
+
* to adjust the barycentric coordinates to a triangle edge if they lie within a parametric tolerance.
|
|
21
|
+
*
|
|
22
|
+
* Properties of the barycentric coordinates (b0, b1, b2) of a point p in the plane of a triangle T with vertices p0, p1, p2:
|
|
23
|
+
* * 1 = b0 + b1 + b2
|
|
24
|
+
* * p = b0 * p0 + b1 * p1 + b2 * p2
|
|
25
|
+
* * If T is spanned by the vectors U=p1-p0 and V=p2-p0, then the vector P=p-p0 can be written P = b1 * U + b2 * V.
|
|
26
|
+
* * The coordinates are all nonnegative if and only if p is inside or on T.
|
|
27
|
+
* * Exactly one coordinate is zero if and only if p lies on an (infinitely extended) edge of T.
|
|
28
|
+
* * Exactly two coordinates are zero if and only if p coincides with a vertex of T.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
class TriangleLocationDetail {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.world = new Point3dVector3d_1.Point3d();
|
|
34
|
+
this.local = new Point3dVector3d_1.Point3d();
|
|
35
|
+
this.a = 0.0;
|
|
36
|
+
this.closestEdgeIndex = 0;
|
|
37
|
+
this.closestEdgeParam = 0.0;
|
|
38
|
+
}
|
|
39
|
+
/** Invalidate this detail. */
|
|
40
|
+
invalidate() {
|
|
41
|
+
this.world.setZero();
|
|
42
|
+
this.local.setZero();
|
|
43
|
+
this.a = 0.0;
|
|
44
|
+
this.closestEdgeIndex = 0;
|
|
45
|
+
this.closestEdgeParam = 0.0;
|
|
46
|
+
}
|
|
47
|
+
/** Create an invalid detail.
|
|
48
|
+
* @param result optional pre-allocated object to fill and return
|
|
49
|
+
*/
|
|
50
|
+
static create(result) {
|
|
51
|
+
if (undefined === result)
|
|
52
|
+
result = new TriangleLocationDetail();
|
|
53
|
+
else
|
|
54
|
+
result.invalidate();
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
/** Set the instance contents from the other detail.
|
|
58
|
+
* @param other detail to clone
|
|
59
|
+
*/
|
|
60
|
+
copyContentsFrom(other) {
|
|
61
|
+
this.world.setFrom(other.world);
|
|
62
|
+
this.local.setFrom(other.local);
|
|
63
|
+
this.a = other.a;
|
|
64
|
+
this.closestEdgeIndex = other.closestEdgeIndex;
|
|
65
|
+
this.closestEdgeParam = other.closestEdgeParam;
|
|
66
|
+
}
|
|
67
|
+
/** Whether this detail is invalid. */
|
|
68
|
+
get isValid() {
|
|
69
|
+
return !this.local.isZero;
|
|
70
|
+
}
|
|
71
|
+
/** Queries the barycentric coordinates to determine whether this instance specifies a location inside or on the triangle.
|
|
72
|
+
* @see classify
|
|
73
|
+
*/
|
|
74
|
+
get isInsideOrOn() {
|
|
75
|
+
return this.isValid && this.local.x >= 0.0 && this.local.y >= 0.0 && this.local.z >= 0.0;
|
|
76
|
+
}
|
|
77
|
+
/** Queries this detail to classify the location of this instance with respect to the triangle.
|
|
78
|
+
* @returns location code
|
|
79
|
+
* @see isInsideOrOn
|
|
80
|
+
*/
|
|
81
|
+
get classify() {
|
|
82
|
+
if (!this.isValid)
|
|
83
|
+
return Geometry_1.PolygonLocation.Unknown;
|
|
84
|
+
if (this.isInsideOrOn) {
|
|
85
|
+
let numZero = 0;
|
|
86
|
+
if (Math.abs(this.local.x) === 0.0)
|
|
87
|
+
++numZero;
|
|
88
|
+
if (Math.abs(this.local.y) === 0.0)
|
|
89
|
+
++numZero;
|
|
90
|
+
if (Math.abs(this.local.z) === 0.0)
|
|
91
|
+
++numZero;
|
|
92
|
+
if (2 === numZero)
|
|
93
|
+
return Geometry_1.PolygonLocation.OnPolygonVertex;
|
|
94
|
+
if (1 === numZero)
|
|
95
|
+
return Geometry_1.PolygonLocation.OnPolygonEdgeInterior;
|
|
96
|
+
return Geometry_1.PolygonLocation.InsidePolygonProjectsToEdgeInterior;
|
|
97
|
+
}
|
|
98
|
+
return (this.closestEdgeParam === 0.0) ? Geometry_1.PolygonLocation.OutsidePolygonProjectsToVertex : Geometry_1.PolygonLocation.OutsidePolygonProjectsToEdgeInterior;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.TriangleLocationDetail = TriangleLocationDetail;
|
|
102
|
+
/**
|
|
103
|
+
* 3 points defining a triangle to be evaluated with barycentric coordinates.
|
|
15
104
|
* @public
|
|
16
105
|
*/
|
|
17
106
|
class BarycentricTriangle {
|
|
@@ -23,6 +112,10 @@ class BarycentricTriangle {
|
|
|
23
112
|
this.points.push(point0);
|
|
24
113
|
this.points.push(point1);
|
|
25
114
|
this.points.push(point2);
|
|
115
|
+
this.edgeLength2 = [];
|
|
116
|
+
this.edgeLength2.push(point1.distanceSquared(point2));
|
|
117
|
+
this.edgeLength2.push(point0.distanceSquared(point2));
|
|
118
|
+
this.edgeLength2.push(point0.distanceSquared(point1));
|
|
26
119
|
}
|
|
27
120
|
/**
|
|
28
121
|
* Return a `BarycentricTriangle` with coordinates given by enumerated x,y,z of the 3 points.
|
|
@@ -49,31 +142,331 @@ class BarycentricTriangle {
|
|
|
49
142
|
}
|
|
50
143
|
/** Return area divided by sum of squared lengths. */
|
|
51
144
|
get aspectRatio() {
|
|
52
|
-
return Geometry_1.Geometry.safeDivideFraction(
|
|
145
|
+
return Geometry_1.Geometry.safeDivideFraction(this.area, this.edgeLengthSquared(0) + this.edgeLengthSquared(1) + this.edgeLengthSquared(2), 0);
|
|
53
146
|
}
|
|
54
147
|
/** Return the area of the triangle. */
|
|
55
148
|
get area() {
|
|
56
149
|
return 0.5 * this.points[0].crossProductToPointsMagnitude(this.points[1], this.points[2]);
|
|
57
150
|
}
|
|
151
|
+
/** Return the perimeter of the triangle */
|
|
152
|
+
get perimeter() {
|
|
153
|
+
return this.edgeLength(0) + this.edgeLength(1) + this.edgeLength(2);
|
|
154
|
+
}
|
|
58
155
|
/** Sum the points with given scales.
|
|
59
|
-
* *
|
|
60
|
-
* * If scales do not
|
|
156
|
+
* * If the scales sum to 1, they are barycentric coordinates, and hence the result point is in the plane of the triangle.
|
|
157
|
+
* * If the scales do not sum to 1, the point is in the triangle scaled (by the scale sum) from the origin.
|
|
158
|
+
* @param b0 scale to apply to vertex 0
|
|
159
|
+
* @param b1 scale to apply to vertex 1
|
|
160
|
+
* @param b2 scale to apply to vertex 2
|
|
161
|
+
* @param result optional pre-allocated point to fill and return
|
|
162
|
+
* @return linear combination of the vertices of this triangle
|
|
163
|
+
* @see pointToFraction
|
|
164
|
+
*/
|
|
165
|
+
fractionToPoint(b0, b1, b2, result) {
|
|
166
|
+
return Point3dVector3d_1.Point3d.createAdd3Scaled(this.points[0], b0, this.points[1], b1, this.points[2], b2, result);
|
|
167
|
+
}
|
|
168
|
+
/** Compute the projection of the given point onto the plane of this triangle.
|
|
169
|
+
* @param point point p to project
|
|
170
|
+
* @param result optional pre-allocated object to fill and return
|
|
171
|
+
* @returns details d of the projection point P = `d.point`:
|
|
172
|
+
* * `d.isValid` returns true if and only if `this.normal()` is defined.
|
|
173
|
+
* * `d.classify` can be used to determine where P lies with respect to the triangle.
|
|
174
|
+
* * `d.a` is the signed projection distance: P = p + a * `this.normal()`.
|
|
175
|
+
* @see fractionToPoint
|
|
176
|
+
*/
|
|
177
|
+
pointToFraction(point, result) {
|
|
178
|
+
const normal = BarycentricTriangle._workVector0 = this.normal(BarycentricTriangle._workVector0);
|
|
179
|
+
if (undefined === normal)
|
|
180
|
+
return TriangleLocationDetail.create(result);
|
|
181
|
+
const ray = BarycentricTriangle._workRay = Ray3d_1.Ray3d.create(point, normal, BarycentricTriangle._workRay);
|
|
182
|
+
return this.intersectRay3d(ray, result); // is free to use workVector0
|
|
183
|
+
}
|
|
184
|
+
/** Convert from opposite-vertex to start-vertex edge indexing. */
|
|
185
|
+
static edgeOppositeVertexIndexToStartVertexIndex(edgeIndex) {
|
|
186
|
+
return Geometry_1.Geometry.cyclic3dAxis(edgeIndex + 1);
|
|
187
|
+
}
|
|
188
|
+
/** Convert from start-vertex to opposite-vertex edge indexing. */
|
|
189
|
+
static edgeStartVertexIndexToOppositeVertexIndex(startVertexIndex) {
|
|
190
|
+
return Geometry_1.Geometry.cyclic3dAxis(startVertexIndex - 1);
|
|
191
|
+
}
|
|
192
|
+
/** Examine a point's barycentric coordinates to determine if it lies inside the triangle but not on an edge/vertex.
|
|
193
|
+
* * No parametric tolerance is used.
|
|
194
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
195
|
+
* @returns whether the point with barycentric coordinates is strictly inside the triangle.
|
|
196
|
+
*/
|
|
197
|
+
static isInsideTriangle(b0, b1, b2) {
|
|
198
|
+
return b0 > 0 && b1 > 0 && b2 > 0;
|
|
199
|
+
}
|
|
200
|
+
/** Examine a point's barycentric coordinates to determine if it lies inside the triangle or on an edge/vertex.
|
|
201
|
+
* * No parametric tolerance is used.
|
|
202
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
203
|
+
* @returns whether the point with barycentric coordinates is inside or on the triangle.
|
|
204
|
+
*/
|
|
205
|
+
static isInsideOrOnTriangle(b0, b1, b2) {
|
|
206
|
+
return b0 >= 0 && b1 >= 0 && b2 >= 0;
|
|
207
|
+
}
|
|
208
|
+
/** Examine a point's barycentric coordinates to determine if it lies "outside" an edge of the triangle.
|
|
209
|
+
* * No parametric tolerance is used.
|
|
210
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
211
|
+
* @returns index of vertex/edge i for which b_i < 0 and b_j >= 0 and b_k >= 0, or -1
|
|
61
212
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
213
|
+
static isInRegionBeyondEdge(b0, b1, b2) {
|
|
214
|
+
if (b0 < 0 && b1 >= 0 && b2 >= 0)
|
|
215
|
+
return 0;
|
|
216
|
+
if (b0 >= 0 && b1 < 0 && b2 >= 0)
|
|
217
|
+
return 1;
|
|
218
|
+
if (b0 >= 0 && b1 >= 0 && b2 < 0)
|
|
219
|
+
return 2;
|
|
220
|
+
return -1;
|
|
221
|
+
}
|
|
222
|
+
/** Examine a point's barycentric coordinates to determine if it lies "outside" a vertex of the triangle.
|
|
223
|
+
* * No parametric tolerance is used.
|
|
224
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
225
|
+
* @returns index of vertex i for which and b_j < 0 and b_k < 0, or -1
|
|
226
|
+
*/
|
|
227
|
+
static isInRegionBeyondVertex(b0, b1, b2) {
|
|
228
|
+
if (b1 < 0 && b2 < 0)
|
|
229
|
+
return 0;
|
|
230
|
+
if (b0 < 0 && b2 < 0)
|
|
231
|
+
return 1;
|
|
232
|
+
if (b0 < 0 && b1 < 0)
|
|
233
|
+
return 2;
|
|
234
|
+
return -1;
|
|
235
|
+
}
|
|
236
|
+
/** Examine a point's barycentric coordinates to determine if it lies on a vertex of the triangle.
|
|
237
|
+
* * No parametric tolerance is used.
|
|
238
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
239
|
+
* @returns index of vertex i for which b_i = 1 and b_j = b_k = 0, or -1
|
|
240
|
+
*/
|
|
241
|
+
static isOnVertex(b0, b1, b2) {
|
|
242
|
+
if (b0 === 1 && b1 === 0 && b2 === 0)
|
|
243
|
+
return 0;
|
|
244
|
+
if (b0 === 0 && b1 === 1 && b2 === 0)
|
|
245
|
+
return 1;
|
|
246
|
+
if (b0 === 0 && b1 === 0 && b2 === 1)
|
|
247
|
+
return 2;
|
|
248
|
+
return -1;
|
|
249
|
+
}
|
|
250
|
+
/** Examine a point's barycentric coordinates to determine if it lies on a bounded edge of the triangle.
|
|
251
|
+
* * No parametric tolerance is used.
|
|
252
|
+
* * It is assumed b0 + b1 + b2 = 1.
|
|
253
|
+
* @returns index of vertex/edge i for which b_i = 0 and b_j > 0 and b_k > 0, or -1
|
|
254
|
+
*/
|
|
255
|
+
static isOnBoundedEdge(b0, b1, b2) {
|
|
256
|
+
if (b0 === 0 && b1 > 0 && b2 > 0)
|
|
257
|
+
return 0;
|
|
258
|
+
if (b0 > 0 && b1 === 0 && b2 > 0)
|
|
259
|
+
return 1;
|
|
260
|
+
if (b0 > 0 && b1 > 0 && b2 === 0)
|
|
261
|
+
return 2;
|
|
262
|
+
return -1;
|
|
263
|
+
}
|
|
264
|
+
/** @returns edge/vertex index (0,1,2) for which the function has a minimum value */
|
|
265
|
+
static indexOfMinimum(fn) {
|
|
266
|
+
let i = 0;
|
|
267
|
+
let min = fn(0);
|
|
268
|
+
const val = fn(1);
|
|
269
|
+
if (min > val) {
|
|
270
|
+
i = 1;
|
|
271
|
+
min = val;
|
|
272
|
+
}
|
|
273
|
+
if (min > fn(2))
|
|
274
|
+
i = 2;
|
|
275
|
+
return i;
|
|
276
|
+
}
|
|
277
|
+
/** Return the index of the closest triangle vertex to the point given by its barycentric coordinates. */
|
|
278
|
+
closestVertexIndex(b0, b1, b2) {
|
|
279
|
+
return BarycentricTriangle.indexOfMinimum((i) => {
|
|
280
|
+
const a = BarycentricTriangle._workPoint = Point3dVector3d_1.Point3d.createZero(BarycentricTriangle._workPoint);
|
|
281
|
+
a.setAt(i, 1.0);
|
|
282
|
+
return this.distanceSquared(a.x, a.y, a.z, b0, b1, b2);
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
/** Compute the projection of barycentric point p onto the (unbounded) edge e_k(v_i,v_j) of the triangle T(v_i,v_j,v_k).
|
|
286
|
+
* @param k vertex v_k is opposite the edge e_k
|
|
287
|
+
* @param b barycentric coordinates of point to project
|
|
288
|
+
* @returns parameter s along e_k, such that:
|
|
289
|
+
* * the projection point is q = v_i + s * (v_j - v_i)
|
|
290
|
+
* * the barycentric coords of the projection are q_ijk = (1 - s, s, 0)
|
|
291
|
+
*/
|
|
292
|
+
computeProjectionToEdge(k, b) {
|
|
293
|
+
// Let U=v_j-v_i and V=v_k-v_i. Then
|
|
294
|
+
// 0 = (p-q).(v_j-v_i) = ((p-v_i)-(q-v_i)).(v_j-v_i) = (b[j]U + b[k]V - sU).U = b[j]U.U + b[k]U.V - sU.U
|
|
295
|
+
// Thus s = b[j] + b[k]U.V/U.U
|
|
296
|
+
k = Geometry_1.Geometry.cyclic3dAxis(k);
|
|
297
|
+
const i = Geometry_1.Geometry.cyclic3dAxis(k + 1);
|
|
298
|
+
const j = Geometry_1.Geometry.cyclic3dAxis(i + 1);
|
|
299
|
+
return b[j] + b[k] * this.dotProductOfEdgeVectorsAtVertex(i) / this.edgeLengthSquared(k);
|
|
300
|
+
}
|
|
301
|
+
/** Compute the projection of a barycentric point p to the triangle T(v_0,v_1,v_2).
|
|
302
|
+
* @param b0 barycentric coordinate of p corresponding to v_0
|
|
303
|
+
* @param b1 barycentric coordinate of p corresponding to v_1
|
|
304
|
+
* @param b2 barycentric coordinate of p corresponding to v_2
|
|
305
|
+
* @returns closest edge start vertex index i and projection parameter u such that the projection q = v_i + u * (v_j - v_i).
|
|
306
|
+
*/
|
|
307
|
+
closestPoint(b0, b1, b2) {
|
|
308
|
+
const b = [b0, b1, b2];
|
|
309
|
+
let edgeIndex = -1; // opposite-vertex index
|
|
310
|
+
let edgeParam = 0.0;
|
|
311
|
+
if (BarycentricTriangle.isInsideTriangle(b0, b1, b2)) { // projects to any edge
|
|
312
|
+
edgeIndex = BarycentricTriangle.indexOfMinimum((i) => {
|
|
313
|
+
// We want smallest projection distance d_i of p to e_i.
|
|
314
|
+
// Since b[i]=d_i|e_i|/2A we can compare quantities b[i]/|e_i|.
|
|
315
|
+
return b[i] * b[i] / this.edgeLengthSquared(i); // avoid sqrt
|
|
316
|
+
});
|
|
317
|
+
edgeParam = this.computeProjectionToEdge(edgeIndex, b);
|
|
318
|
+
}
|
|
319
|
+
else if ((edgeIndex = BarycentricTriangle.isInRegionBeyondVertex(b0, b1, b2)) >= 0) { // projects to other edges, or any vertex
|
|
320
|
+
edgeIndex = Geometry_1.Geometry.cyclic3dAxis(edgeIndex + 1);
|
|
321
|
+
edgeParam = this.computeProjectionToEdge(edgeIndex, b);
|
|
322
|
+
if (edgeParam < 0 || edgeParam > 1) {
|
|
323
|
+
edgeIndex = Geometry_1.Geometry.cyclic3dAxis(edgeIndex + 1);
|
|
324
|
+
edgeParam = this.computeProjectionToEdge(edgeIndex, b);
|
|
325
|
+
if (edgeParam < 0 || edgeParam > 1) {
|
|
326
|
+
edgeParam = 0.0;
|
|
327
|
+
edgeIndex = BarycentricTriangle.edgeStartVertexIndexToOppositeVertexIndex(this.closestVertexIndex(b0, b1, b2));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
else if ((edgeIndex = BarycentricTriangle.isInRegionBeyondEdge(b0, b1, b2)) >= 0) { // projects to the edge or its vertices
|
|
332
|
+
edgeParam = this.computeProjectionToEdge(edgeIndex, b);
|
|
333
|
+
if (edgeParam < 0) {
|
|
334
|
+
edgeParam = 0.0; // start of this edge
|
|
335
|
+
}
|
|
336
|
+
else if (edgeParam > 1) {
|
|
337
|
+
edgeParam = 0.0;
|
|
338
|
+
edgeIndex = Geometry_1.Geometry.cyclic3dAxis(edgeIndex + 1); // end of this edge = start of next edge
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
else if ((edgeIndex = BarycentricTriangle.isOnBoundedEdge(b0, b1, b2)) >= 0) {
|
|
342
|
+
edgeParam = 1 - b[BarycentricTriangle.edgeOppositeVertexIndexToStartVertexIndex(edgeIndex)];
|
|
343
|
+
}
|
|
344
|
+
else if ((edgeIndex = BarycentricTriangle.isOnVertex(b0, b1, b2)) >= 0) {
|
|
345
|
+
edgeParam = 0.0;
|
|
346
|
+
edgeIndex = BarycentricTriangle.edgeStartVertexIndexToOppositeVertexIndex(edgeIndex);
|
|
347
|
+
}
|
|
348
|
+
// invalid edgeIndex shouldn't happen, but propagate it anyway
|
|
349
|
+
(0, core_bentley_1.assert)(edgeIndex === 0 || edgeIndex === 1 || edgeIndex === 2);
|
|
350
|
+
return {
|
|
351
|
+
closestEdgeIndex: (edgeIndex < 0) ? -1 : BarycentricTriangle.edgeOppositeVertexIndexToStartVertexIndex(edgeIndex),
|
|
352
|
+
closestEdgeParam: edgeParam,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/** Compute the intersection of a line (parameterized as a ray) with the plane of this triangle.
|
|
356
|
+
* @param ray infinite line to intersect, as a ray
|
|
357
|
+
* @param result optional pre-allocated object to fill and return
|
|
358
|
+
* @returns details d of the line-plane intersection `d.point`:
|
|
359
|
+
* * `d.isValid` returns true if and only if the line intersects the plane.
|
|
360
|
+
* * `d.classify` can be used to determine where the intersection lies with respect to the triangle.
|
|
361
|
+
* * `d.a` is the intersection parameter. If `d.a` >= 0, the ray intersects the plane of the triangle.
|
|
362
|
+
* @see pointToFraction
|
|
363
|
+
*/
|
|
364
|
+
intersectRay3d(ray, result) {
|
|
365
|
+
result = TriangleLocationDetail.create(result);
|
|
366
|
+
// Let r0 = ray.origin, d = ray.direction. Write intersection point p two ways for unknown scalars s,b0,b1,b2:
|
|
367
|
+
// r0 + s*d = p = b0*p0 + b1*p1 + b2*p2
|
|
368
|
+
// Subtract p0 from both ends, let u=p1-p0, v=p2-p0, c=r0-p0, and enforce b0+b1+b2=1:
|
|
369
|
+
// b1*u + b2*v - s*d = c
|
|
370
|
+
// This is a linear system Mx=c where M has columns u,v,d and solution x=(b1,b2,-s).
|
|
371
|
+
const r0 = ray.origin;
|
|
372
|
+
const d = ray.direction;
|
|
373
|
+
const u = BarycentricTriangle._workVector0 = Point3dVector3d_1.Vector3d.createStartEnd(this.points[0], this.points[1], BarycentricTriangle._workVector0);
|
|
374
|
+
const v = BarycentricTriangle._workVector1 = Point3dVector3d_1.Vector3d.createStartEnd(this.points[0], this.points[2], BarycentricTriangle._workVector1);
|
|
375
|
+
const M = BarycentricTriangle._workMatrix = Matrix3d_1.Matrix3d.createColumns(u, v, d, BarycentricTriangle._workMatrix);
|
|
376
|
+
const c = Point3dVector3d_1.Vector3d.createStartEnd(this.points[0], r0, BarycentricTriangle._workVector0); // reuse workVector0
|
|
377
|
+
const solution = BarycentricTriangle._workVector1; // reuse workVector1
|
|
378
|
+
if (undefined === M.multiplyInverse(c, solution))
|
|
379
|
+
return result; // invalid
|
|
380
|
+
result.a = -solution.z;
|
|
381
|
+
ray.fractionToPoint(result.a, result.world);
|
|
382
|
+
result.local.set(1.0 - solution.x - solution.y, solution.x, solution.y);
|
|
383
|
+
const proj = this.closestPoint(result.local.x, result.local.y, result.local.z);
|
|
384
|
+
result.closestEdgeIndex = proj.closestEdgeIndex;
|
|
385
|
+
result.closestEdgeParam = proj.closestEdgeParam;
|
|
386
|
+
return result;
|
|
387
|
+
}
|
|
388
|
+
/** Compute the intersection of a line (parameterized as a line segment) with the plane of this triangle.
|
|
389
|
+
* @param point0 start point of segment on line to intersect
|
|
390
|
+
* @param point1 end point of segment on line to intersect
|
|
391
|
+
* @param result optional pre-allocated object to fill and return
|
|
392
|
+
* @returns details d of the line-plane intersection `d.point`:
|
|
393
|
+
* * `d.isValid` returns true if and only if the line intersects the plane.
|
|
394
|
+
* * `d.classify` can be used to determine where the intersection lies with respect to the triangle.
|
|
395
|
+
* * `d.a` is the intersection parameter. If `d.a` is in [0,1], the segment intersects the plane of the triangle.
|
|
396
|
+
* @see intersectRay3d
|
|
397
|
+
*/
|
|
398
|
+
intersectSegment(point0, point1, result) {
|
|
399
|
+
BarycentricTriangle._workRay = Ray3d_1.Ray3d.createStartEnd(point0, point1, BarycentricTriangle._workRay);
|
|
400
|
+
return this.intersectRay3d(BarycentricTriangle._workRay, result);
|
|
401
|
+
}
|
|
402
|
+
/** Adjust the location to an edge of the triangle if within either given tolerance.
|
|
403
|
+
* @param location details of a point in the plane of the triangle, `location.local` and `location.world` possibly updated to lie on a triangle edge
|
|
404
|
+
* @param distanceTolerance absolute distance tolerance (or zero to ignore)
|
|
405
|
+
* @param parameterTolerance barycentric coordinate fractional tolerance (or zero to ignore)
|
|
406
|
+
* @return whether the location was adjusted
|
|
407
|
+
*/
|
|
408
|
+
snapLocationToEdge(location, distanceTolerance = Geometry_1.Geometry.smallMetricDistance, parameterTolerance = Geometry_1.Geometry.smallFraction) {
|
|
409
|
+
if (!location.isValid)
|
|
410
|
+
return false;
|
|
411
|
+
// first try parametric tol to zero barycentric coordinate (no vertices or world distances used!)
|
|
412
|
+
if (parameterTolerance > 0.0) {
|
|
413
|
+
let numSnapped = 0;
|
|
414
|
+
let newSum = 0.0;
|
|
415
|
+
for (let i = 0; i < 3; i++) {
|
|
416
|
+
const barycentricDist = Math.abs(location.local.at(i));
|
|
417
|
+
if (barycentricDist > 0.0 && barycentricDist < parameterTolerance) {
|
|
418
|
+
location.local.setAt(i, 0.0);
|
|
419
|
+
numSnapped++;
|
|
420
|
+
}
|
|
421
|
+
newSum += location.local.at(i);
|
|
422
|
+
}
|
|
423
|
+
if (numSnapped > 0 && newSum > 0.0) {
|
|
424
|
+
location.local.scaleInPlace(1.0 / newSum);
|
|
425
|
+
if (1 === numSnapped) {
|
|
426
|
+
location.closestEdgeIndex = BarycentricTriangle.edgeOppositeVertexIndexToStartVertexIndex(BarycentricTriangle.isOnBoundedEdge(location.local.x, location.local.y, location.local.z));
|
|
427
|
+
location.closestEdgeParam = 1.0 - location.local.at(location.closestEdgeIndex);
|
|
428
|
+
}
|
|
429
|
+
else { // 2 snapped, at vertex
|
|
430
|
+
location.closestEdgeIndex = BarycentricTriangle.isOnVertex(location.local.x, location.local.y, location.local.z);
|
|
431
|
+
location.closestEdgeParam = 0.0;
|
|
432
|
+
}
|
|
433
|
+
this.fractionToPoint(location.local.x, location.local.y, location.local.z, location.world);
|
|
434
|
+
return true;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
// failing that, try distance tol to closest edge projection
|
|
438
|
+
if (distanceTolerance > 0.0) {
|
|
439
|
+
const i = location.closestEdgeIndex;
|
|
440
|
+
const j = (i + 1) % 3;
|
|
441
|
+
const k = (j + 1) % 3;
|
|
442
|
+
const edgeProjection = BarycentricTriangle._workPoint = this.points[i].interpolate(location.closestEdgeParam, this.points[j], BarycentricTriangle._workPoint);
|
|
443
|
+
const dist = location.world.distance(edgeProjection);
|
|
444
|
+
if (dist > 0.0 && dist < distanceTolerance) {
|
|
445
|
+
location.local.setAt(i, 1.0 - location.closestEdgeParam);
|
|
446
|
+
location.local.setAt(j, location.closestEdgeParam);
|
|
447
|
+
location.local.setAt(k, 0.0);
|
|
448
|
+
location.world.setFrom(edgeProjection);
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return false;
|
|
64
453
|
}
|
|
65
454
|
/** Copy all values from `other`
|
|
66
455
|
*/
|
|
67
456
|
setFrom(other) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
457
|
+
for (let i = 0; i < 3; ++i) {
|
|
458
|
+
this.points[i].setFromPoint3d(other.points[i]);
|
|
459
|
+
this.edgeLength2[i] = other.edgeLength2[i];
|
|
460
|
+
}
|
|
71
461
|
}
|
|
72
|
-
/**
|
|
462
|
+
/** Copy contents of (not pointers to) the given points. A vertex is zeroed if its corresponding input point is undefined. */
|
|
73
463
|
set(point0, point1, point2) {
|
|
74
464
|
this.points[0].setFromPoint3d(point0);
|
|
75
465
|
this.points[1].setFromPoint3d(point1);
|
|
76
466
|
this.points[2].setFromPoint3d(point2);
|
|
467
|
+
this.edgeLength2[0] = this.points[1].distanceSquared(this.points[2]);
|
|
468
|
+
this.edgeLength2[1] = this.points[0].distanceSquared(this.points[2]);
|
|
469
|
+
this.edgeLength2[2] = this.points[0].distanceSquared(this.points[1]);
|
|
77
470
|
}
|
|
78
471
|
/**
|
|
79
472
|
* * For `this` and `other` BarycentricTriangles, compute cross products of vectors from point0 to point1 and from point0 to point2.
|
|
@@ -90,12 +483,67 @@ class BarycentricTriangle {
|
|
|
90
483
|
// Do the scale as true division (rather than multiply by precomputed 1/3). This might protect one bit of result.
|
|
91
484
|
return Point3dVector3d_1.Point3d.create((this.points[0].x + this.points[1].x + this.points[2].x) / 3.0, (this.points[0].y + this.points[1].y + this.points[2].y) / 3.0, (this.points[0].z + this.points[1].z + this.points[2].z) / 3.0, result);
|
|
92
485
|
}
|
|
486
|
+
/** Return the incenter of the triangle. */
|
|
487
|
+
incenter(result) {
|
|
488
|
+
const a = this.edgeLength(0);
|
|
489
|
+
const b = this.edgeLength(1);
|
|
490
|
+
const c = this.edgeLength(2);
|
|
491
|
+
const scale = Geometry_1.Geometry.safeDivideFraction(1.0, a + b + c, 0.0);
|
|
492
|
+
return this.fractionToPoint(scale * a, scale * b, scale * c, result);
|
|
493
|
+
}
|
|
494
|
+
/** Return the circumcenter of the triangle. */
|
|
495
|
+
circumcenter(result) {
|
|
496
|
+
const a2 = this.edgeLengthSquared(0);
|
|
497
|
+
const b2 = this.edgeLengthSquared(1);
|
|
498
|
+
const c2 = this.edgeLengthSquared(2);
|
|
499
|
+
const x = a2 * (b2 + c2 - a2);
|
|
500
|
+
const y = b2 * (c2 + a2 - b2);
|
|
501
|
+
const z = c2 * (a2 + b2 - c2);
|
|
502
|
+
const scale = Geometry_1.Geometry.safeDivideFraction(1.0, x + y + z, 0.0);
|
|
503
|
+
return this.fractionToPoint(scale * x, scale * y, scale * z, result);
|
|
504
|
+
}
|
|
505
|
+
/** Return the unit normal of the triangle.
|
|
506
|
+
* @param result optional pre-allocated vector to fill and return.
|
|
507
|
+
* @returns unit normal, or undefined if cross product length is too small.
|
|
508
|
+
*/
|
|
509
|
+
normal(result) {
|
|
510
|
+
const cross = this.points[0].crossProductToPoints(this.points[1], this.points[2], result);
|
|
511
|
+
if (cross.tryNormalizeInPlace())
|
|
512
|
+
return cross;
|
|
513
|
+
return undefined;
|
|
514
|
+
}
|
|
93
515
|
/** test for point-by-point `isAlmostEqual` relationship. */
|
|
94
516
|
isAlmostEqual(other) {
|
|
95
517
|
return this.points[0].isAlmostEqual(other.points[0])
|
|
96
518
|
&& this.points[1].isAlmostEqual(other.points[1])
|
|
97
519
|
&& this.points[2].isAlmostEqual(other.points[2]);
|
|
98
520
|
}
|
|
521
|
+
/** Compute length of the triangle edge opposite the vertex with the given index.
|
|
522
|
+
* @see edgeStartVertexIndexToOppositeVertexIndex
|
|
523
|
+
*/
|
|
524
|
+
edgeLength(oppositeVertexIndex) {
|
|
525
|
+
return Math.sqrt(this.edgeLengthSquared(oppositeVertexIndex));
|
|
526
|
+
}
|
|
527
|
+
/** Compute squared length of the triangle edge opposite the vertex with the given index.
|
|
528
|
+
* @see edgeStartVertexIndexToOppositeVertexIndex
|
|
529
|
+
*/
|
|
530
|
+
edgeLengthSquared(oppositeVertexIndex) {
|
|
531
|
+
return this.edgeLength2[Geometry_1.Geometry.cyclic3dAxis(oppositeVertexIndex)];
|
|
532
|
+
}
|
|
533
|
+
/** Compute dot product of the edge vectors based at the vertex with the given index. */
|
|
534
|
+
dotProductOfEdgeVectorsAtVertex(baseVertexIndex) {
|
|
535
|
+
const i = Geometry_1.Geometry.cyclic3dAxis(baseVertexIndex);
|
|
536
|
+
const j = Geometry_1.Geometry.cyclic3dAxis(i + 1);
|
|
537
|
+
const k = Geometry_1.Geometry.cyclic3dAxis(j + 1);
|
|
538
|
+
return Geometry_1.Geometry.dotProductXYZXYZ(this.points[j].x - this.points[i].x, this.points[j].y - this.points[i].y, this.points[j].z - this.points[i].z, this.points[k].x - this.points[i].x, this.points[k].y - this.points[i].y, this.points[k].z - this.points[i].z);
|
|
539
|
+
}
|
|
540
|
+
/** Compute the squared distance between two points given by their barycentric coordinates.
|
|
541
|
+
* * It is assumed that a0 + a1 + a2 = b0 + b1 + b2 = 1.
|
|
542
|
+
*/
|
|
543
|
+
distanceSquared(a0, a1, a2, b0, b1, b2) {
|
|
544
|
+
// the barycentric displacement vector distance formula
|
|
545
|
+
return -this.edgeLengthSquared(0) * (b1 - a1) * (b2 - a2) - this.edgeLengthSquared(1) * (b2 - a2) * (b0 - a0) - this.edgeLengthSquared(2) * (b0 - a0) * (b1 - a1);
|
|
546
|
+
}
|
|
99
547
|
}
|
|
100
548
|
exports.BarycentricTriangle = BarycentricTriangle;
|
|
101
549
|
//# sourceMappingURL=BarycentricTriangle.js.map
|