@constructorfleet/sketchforge-engine 1.31.0 → 1.33.0
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/README.md +120 -1
- package/dist/apiErrors.d.ts +84 -0
- package/dist/apiErrors.d.ts.map +1 -0
- package/dist/apiErrors.js +292 -0
- package/dist/apiErrors.js.map +1 -0
- package/dist/apiParameters.d.ts +426 -0
- package/dist/apiParameters.d.ts.map +1 -0
- package/dist/apiParameters.js +144 -0
- package/dist/apiParameters.js.map +1 -0
- package/dist/apiShapeSummary.d.ts +128 -0
- package/dist/apiShapeSummary.d.ts.map +1 -0
- package/dist/apiShapeSummary.js +96 -0
- package/dist/apiShapeSummary.js.map +1 -0
- package/dist/apiSketchConstraints.d.ts +80 -0
- package/dist/apiSketchConstraints.d.ts.map +1 -0
- package/dist/apiSketchConstraints.js +59 -0
- package/dist/apiSketchConstraints.js.map +1 -0
- package/dist/domain/sketchforge.d.ts +31 -0
- package/dist/domain/sketchforge.d.ts.map +1 -1
- package/dist/editorHistory.d.ts +25 -4
- package/dist/editorHistory.d.ts.map +1 -1
- package/dist/editorHistory.js +75 -14
- package/dist/editorHistory.js.map +1 -1
- package/dist/engine.d.ts +105 -3
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +1281 -655
- package/dist/engine.js.map +1 -1
- package/dist/expressionBindings.d.ts +113 -0
- package/dist/expressionBindings.d.ts.map +1 -0
- package/dist/expressionBindings.js +353 -0
- package/dist/expressionBindings.js.map +1 -0
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -1
- package/dist/parameterBindingPlan.d.ts +64 -0
- package/dist/parameterBindingPlan.d.ts.map +1 -0
- package/dist/parameterBindingPlan.js +138 -0
- package/dist/parameterBindingPlan.js.map +1 -0
- package/dist/parameterExpressions.d.ts +128 -0
- package/dist/parameterExpressions.d.ts.map +1 -0
- package/dist/parameterExpressions.js +666 -0
- package/dist/parameterExpressions.js.map +1 -0
- package/dist/parametricRecompute.d.ts.map +1 -1
- package/dist/parametricRecompute.js +25 -2
- package/dist/parametricRecompute.js.map +1 -1
- package/dist/projectParameters.d.ts +126 -0
- package/dist/projectParameters.d.ts.map +1 -0
- package/dist/projectParameters.js +534 -0
- package/dist/projectParameters.js.map +1 -0
- package/dist/revolveRequests.d.ts +1 -0
- package/dist/revolveRequests.d.ts.map +1 -1
- package/dist/revolveRequests.js +1 -0
- package/dist/revolveRequests.js.map +1 -1
- package/dist/sceneExport.d.ts.map +1 -1
- package/dist/sceneExport.js +3 -0
- package/dist/sceneExport.js.map +1 -1
- package/dist/serverProjectDocument.d.ts +7 -2
- package/dist/serverProjectDocument.d.ts.map +1 -1
- package/dist/serverProjectDocument.js +16 -1
- package/dist/serverProjectDocument.js.map +1 -1
- package/dist/serverShapeGroups.d.ts +5 -0
- package/dist/serverShapeGroups.d.ts.map +1 -1
- package/dist/serverShapeGroups.js +45 -0
- package/dist/serverShapeGroups.js.map +1 -1
- package/dist/shapeRequestExpressions.d.ts +31 -0
- package/dist/shapeRequestExpressions.d.ts.map +1 -0
- package/dist/shapeRequestExpressions.js +64 -0
- package/dist/shapeRequestExpressions.js.map +1 -0
- package/dist/shapeRequests.d.ts +1 -1
- package/dist/shapeRequests.d.ts.map +1 -1
- package/dist/shapeRequests.js +3 -1
- package/dist/shapeRequests.js.map +1 -1
- package/dist/sketchConstraintBindings.d.ts +131 -0
- package/dist/sketchConstraintBindings.d.ts.map +1 -0
- package/dist/sketchConstraintBindings.js +353 -0
- package/dist/sketchConstraintBindings.js.map +1 -0
- package/dist/sketchConstraints.d.ts +34 -0
- package/dist/sketchConstraints.d.ts.map +1 -0
- package/dist/sketchConstraints.js +919 -0
- package/dist/sketchConstraints.js.map +1 -0
- package/dist/skfProject.d.ts +41 -4
- package/dist/skfProject.d.ts.map +1 -1
- package/dist/skfProject.js +154 -16
- package/dist/skfProject.js.map +1 -1
- package/dist/support/lib/operator-configuration.json +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,919 @@
|
|
|
1
|
+
const CONSTRAINT_TOLERANCE_MM = 1e-5;
|
|
2
|
+
const DEGENERATE_DISTANCE_DIRECTION = { x: 1, z: 0 };
|
|
3
|
+
const MAX_GLOBAL_ITERATIONS = 40;
|
|
4
|
+
const MAX_CG_ITERATIONS = 48;
|
|
5
|
+
const INITIAL_DAMPING = 1e-3;
|
|
6
|
+
const SOLVER_CONVERGENCE_TOLERANCE_MM = 1e-8;
|
|
7
|
+
export function addSketchConstraint(profile, constraint) {
|
|
8
|
+
if (profile.constraints?.some((existing) => existing.id === constraint.id))
|
|
9
|
+
return profile;
|
|
10
|
+
return { ...profile, constraints: [...(profile.constraints ?? []), constraint] };
|
|
11
|
+
}
|
|
12
|
+
export function removeSketchConstraint(profile, constraintId) {
|
|
13
|
+
const constraints = (profile.constraints ?? []).filter((constraint) => constraint.id !== constraintId);
|
|
14
|
+
return constraints.length === (profile.constraints?.length ?? 0) ? profile : { ...profile, constraints };
|
|
15
|
+
}
|
|
16
|
+
function movePoint(point, x, z) {
|
|
17
|
+
const dx = x - point.x;
|
|
18
|
+
const dz = z - point.z;
|
|
19
|
+
return {
|
|
20
|
+
...point,
|
|
21
|
+
x,
|
|
22
|
+
z,
|
|
23
|
+
...(point.handleIn ? { handleIn: { x: point.handleIn.x + dx, z: point.handleIn.z + dz } } : {}),
|
|
24
|
+
...(point.handleOut ? { handleOut: { x: point.handleOut.x + dx, z: point.handleOut.z + dz } } : {}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function distance(a, b) {
|
|
28
|
+
return Math.hypot(b.x - a.x, b.z - a.z);
|
|
29
|
+
}
|
|
30
|
+
function segmentPair(segmentIds, profile) {
|
|
31
|
+
const first = profile.segments.find(({ id }) => id === segmentIds[0]);
|
|
32
|
+
const second = profile.segments.find(({ id }) => id === segmentIds[1]);
|
|
33
|
+
return first && second ? [first, second] : null;
|
|
34
|
+
}
|
|
35
|
+
function tangentInfo(segmentIds, profile, points) {
|
|
36
|
+
const pair = segmentPair(segmentIds, profile);
|
|
37
|
+
if (!pair)
|
|
38
|
+
return null;
|
|
39
|
+
const isLine = (segment) => !segment.kind || segment.kind === "line";
|
|
40
|
+
const isBezier = (segment) => segment.kind === "bezier" || segment.kind === "smooth";
|
|
41
|
+
const line = isLine(pair[0]) && isBezier(pair[1]) ? pair[0] : isLine(pair[1]) && isBezier(pair[0]) ? pair[1] : null;
|
|
42
|
+
const curve = line === pair[0] ? pair[1] : line === pair[1] ? pair[0] : null;
|
|
43
|
+
if (!line || !curve)
|
|
44
|
+
return null;
|
|
45
|
+
const sharedIds = [line.startId, line.endId].filter((id) => id === curve.startId || id === curve.endId);
|
|
46
|
+
if (sharedIds.length !== 1)
|
|
47
|
+
return null;
|
|
48
|
+
const sharedId = sharedIds[0];
|
|
49
|
+
const shared = points.get(sharedId);
|
|
50
|
+
const lineStart = points.get(line.startId);
|
|
51
|
+
const lineEnd = points.get(line.endId);
|
|
52
|
+
if (!shared || !lineStart || !lineEnd)
|
|
53
|
+
return null;
|
|
54
|
+
const handle = curve.startId === sharedId ? shared.handleOut : shared.handleIn;
|
|
55
|
+
if (!handle)
|
|
56
|
+
return null;
|
|
57
|
+
const curveX = curve.startId === sharedId ? handle.x - shared.x : shared.x - handle.x;
|
|
58
|
+
const curveZ = curve.startId === sharedId ? handle.z - shared.z : shared.z - handle.z;
|
|
59
|
+
return { line, lineStart, lineEnd, curveX, curveZ };
|
|
60
|
+
}
|
|
61
|
+
export function isSketchTangentPair(segmentIds, profile) {
|
|
62
|
+
return Boolean(tangentInfo(segmentIds, profile, new Map(profile.points.map((point) => [point.id, point]))));
|
|
63
|
+
}
|
|
64
|
+
function equalLengthPair(segmentIds, profile) {
|
|
65
|
+
if (segmentIds[0] === segmentIds[1])
|
|
66
|
+
return null;
|
|
67
|
+
const pair = segmentPair(segmentIds, profile);
|
|
68
|
+
if (!pair)
|
|
69
|
+
return null;
|
|
70
|
+
const isLine = (segment) => !segment.kind || segment.kind === "line";
|
|
71
|
+
const points = new Map(profile.points.map((point) => [point.id, point]));
|
|
72
|
+
const isNonDegenerate = (segment) => {
|
|
73
|
+
const start = points.get(segment.startId), end = points.get(segment.endId);
|
|
74
|
+
return Boolean(start && end && distance(start, end) > CONSTRAINT_TOLERANCE_MM);
|
|
75
|
+
};
|
|
76
|
+
return isLine(pair[0]) && isLine(pair[1]) && isNonDegenerate(pair[0]) && isNonDegenerate(pair[1]) ? pair : null;
|
|
77
|
+
}
|
|
78
|
+
/** Whether the selected segments are a supported pair for an equal-length relation. */
|
|
79
|
+
export function isSketchEqualLengthPair(segmentIds, profile) {
|
|
80
|
+
return Boolean(equalLengthPair(segmentIds, profile));
|
|
81
|
+
}
|
|
82
|
+
function anglePair(segmentIds, profile) {
|
|
83
|
+
if (segmentIds[0] === segmentIds[1])
|
|
84
|
+
return null;
|
|
85
|
+
const pair = segmentPair(segmentIds, profile);
|
|
86
|
+
if (!pair)
|
|
87
|
+
return null;
|
|
88
|
+
const points = new Map(profile.points.map((point) => [point.id, point]));
|
|
89
|
+
const isNonDegenerate = (segment) => {
|
|
90
|
+
const start = points.get(segment.startId), end = points.get(segment.endId);
|
|
91
|
+
return Boolean(start && end && distance(start, end) > CONSTRAINT_TOLERANCE_MM);
|
|
92
|
+
};
|
|
93
|
+
return isNonDegenerate(pair[0]) && isNonDegenerate(pair[1]) ? pair : null;
|
|
94
|
+
}
|
|
95
|
+
/** Whether the selected segments are a supported pair for an angle relation. */
|
|
96
|
+
export function isSketchAnglePair(segmentIds, profile) {
|
|
97
|
+
return Boolean(anglePair(segmentIds, profile));
|
|
98
|
+
}
|
|
99
|
+
function symmetryInfo(pointIds, segmentId, profile, points = new Map(profile.points.map((point) => [point.id, point]))) {
|
|
100
|
+
if (pointIds[0] === pointIds[1])
|
|
101
|
+
return null;
|
|
102
|
+
const first = points.get(pointIds[0]);
|
|
103
|
+
const second = points.get(pointIds[1]);
|
|
104
|
+
const axis = profile.segments.find(({ id }) => id === segmentId);
|
|
105
|
+
if (!first || !second || !axis)
|
|
106
|
+
return null;
|
|
107
|
+
const start = points.get(axis.startId);
|
|
108
|
+
const end = points.get(axis.endId);
|
|
109
|
+
if (!start || !end)
|
|
110
|
+
return null;
|
|
111
|
+
const length = distance(start, end);
|
|
112
|
+
if (length <= CONSTRAINT_TOLERANCE_MM)
|
|
113
|
+
return null;
|
|
114
|
+
return { first, second, start, end, length, ux: (end.x - start.x) / length, uz: (end.z - start.z) / length };
|
|
115
|
+
}
|
|
116
|
+
/** Whether two distinct points and a non-degenerate segment can form a symmetry relation. */
|
|
117
|
+
export function isSketchSymmetrySetup(pointIds, segmentId, profile) {
|
|
118
|
+
return Boolean(symmetryInfo(pointIds, segmentId, profile));
|
|
119
|
+
}
|
|
120
|
+
function pointPair(constraint, points) {
|
|
121
|
+
const first = points.get(constraint.pointIds[0]);
|
|
122
|
+
const second = points.get(constraint.pointIds[1]);
|
|
123
|
+
return first && second ? [first, second] : null;
|
|
124
|
+
}
|
|
125
|
+
function residual(constraint, profile, points) {
|
|
126
|
+
if (constraint.kind === "fixed") {
|
|
127
|
+
const point = points.get(constraint.pointId);
|
|
128
|
+
return point ? Math.max(Math.abs(point.x - constraint.xMm), Math.abs(point.z - constraint.zMm)) : Number.POSITIVE_INFINITY;
|
|
129
|
+
}
|
|
130
|
+
if (constraint.kind === "perpendicular") {
|
|
131
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
132
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
133
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
134
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
135
|
+
return Number.POSITIVE_INFINITY;
|
|
136
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
137
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
138
|
+
const lengths = Math.hypot(ux, uz) * Math.hypot(vx, vz);
|
|
139
|
+
return lengths <= CONSTRAINT_TOLERANCE_MM ** 2 ? Number.POSITIVE_INFINITY : Math.abs((ux * vx + uz * vz) / lengths);
|
|
140
|
+
}
|
|
141
|
+
if (constraint.kind === "parallel") {
|
|
142
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
143
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
144
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
145
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
146
|
+
return Number.POSITIVE_INFINITY;
|
|
147
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
148
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
149
|
+
const lengths = Math.hypot(ux, uz) * Math.hypot(vx, vz);
|
|
150
|
+
return lengths <= CONSTRAINT_TOLERANCE_MM ** 2 ? Number.POSITIVE_INFINITY : Math.abs((ux * vz - uz * vx) / lengths);
|
|
151
|
+
}
|
|
152
|
+
if (constraint.kind === "tangent") {
|
|
153
|
+
const info = tangentInfo(constraint.segmentIds, profile, points);
|
|
154
|
+
if (!info)
|
|
155
|
+
return Number.POSITIVE_INFINITY;
|
|
156
|
+
const lineX = info.lineEnd.x - info.lineStart.x;
|
|
157
|
+
const lineZ = info.lineEnd.z - info.lineStart.z;
|
|
158
|
+
const lineLength = Math.hypot(lineX, lineZ);
|
|
159
|
+
const curveLength = Math.hypot(info.curveX, info.curveZ);
|
|
160
|
+
return lineLength <= CONSTRAINT_TOLERANCE_MM || curveLength <= CONSTRAINT_TOLERANCE_MM
|
|
161
|
+
? Number.POSITIVE_INFINITY
|
|
162
|
+
: Math.abs((lineX * info.curveZ - lineZ * info.curveX) / (lineLength * curveLength));
|
|
163
|
+
}
|
|
164
|
+
if (constraint.kind === "equalLength") {
|
|
165
|
+
const pair = equalLengthPair(constraint.segmentIds, profile);
|
|
166
|
+
const firstStart = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
167
|
+
const secondStart = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
168
|
+
if (!firstStart || !firstEnd || !secondStart || !secondEnd)
|
|
169
|
+
return Number.POSITIVE_INFINITY;
|
|
170
|
+
const firstLength = distance(firstStart, firstEnd), secondLength = distance(secondStart, secondEnd);
|
|
171
|
+
return firstLength <= CONSTRAINT_TOLERANCE_MM || secondLength <= CONSTRAINT_TOLERANCE_MM
|
|
172
|
+
? Number.POSITIVE_INFINITY
|
|
173
|
+
: Math.abs(firstLength - secondLength);
|
|
174
|
+
}
|
|
175
|
+
if (constraint.kind === "angle") {
|
|
176
|
+
const pair = anglePair(constraint.segmentIds, profile);
|
|
177
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
178
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
179
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
180
|
+
return Number.POSITIVE_INFINITY;
|
|
181
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
182
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
183
|
+
const lengths = Math.hypot(ux, uz) * Math.hypot(vx, vz);
|
|
184
|
+
if (lengths <= CONSTRAINT_TOLERANCE_MM ** 2)
|
|
185
|
+
return Number.POSITIVE_INFINITY;
|
|
186
|
+
const cosine = Math.max(-1, Math.min(1, (ux * vx + uz * vz) / lengths));
|
|
187
|
+
return Math.abs(Math.acos(cosine) - constraint.valueDeg * Math.PI / 180);
|
|
188
|
+
}
|
|
189
|
+
if (constraint.kind === "symmetry") {
|
|
190
|
+
const info = symmetryInfo(constraint.pointIds, constraint.segmentId, profile, points);
|
|
191
|
+
if (!info)
|
|
192
|
+
return Number.POSITIVE_INFINITY;
|
|
193
|
+
const middleX = (info.first.x + info.second.x) / 2;
|
|
194
|
+
const middleZ = (info.first.z + info.second.z) / 2;
|
|
195
|
+
const onAxis = (middleX - info.start.x) * info.uz - (middleZ - info.start.z) * info.ux;
|
|
196
|
+
const acrossAxis = (info.second.x - info.first.x) * info.ux + (info.second.z - info.first.z) * info.uz;
|
|
197
|
+
return Math.max(Math.abs(onAxis), Math.abs(acrossAxis));
|
|
198
|
+
}
|
|
199
|
+
if (constraint.kind === "horizontal" || constraint.kind === "vertical") {
|
|
200
|
+
const segment = profile.segments.find((candidate) => candidate.id === constraint.segmentId);
|
|
201
|
+
const first = segment && points.get(segment.startId);
|
|
202
|
+
const second = segment && points.get(segment.endId);
|
|
203
|
+
return first && second ? Math.abs(constraint.kind === "horizontal" ? first.z - second.z : first.x - second.x) : Number.POSITIVE_INFINITY;
|
|
204
|
+
}
|
|
205
|
+
const pair = pointPair(constraint, points);
|
|
206
|
+
if (!pair)
|
|
207
|
+
return Number.POSITIVE_INFINITY;
|
|
208
|
+
const [first, second] = pair;
|
|
209
|
+
if (constraint.kind === "coincident")
|
|
210
|
+
return distance(first, second);
|
|
211
|
+
const target = constraint.kind === "radius" ? constraint.valueMm * 2 : constraint.valueMm;
|
|
212
|
+
return Math.abs(distance(first, second) - target);
|
|
213
|
+
}
|
|
214
|
+
function equationRows(constraints, profile, points, variableIndex) {
|
|
215
|
+
const equations = [];
|
|
216
|
+
const add = (value, terms) => {
|
|
217
|
+
equations.push({
|
|
218
|
+
value,
|
|
219
|
+
terms: terms.flatMap(({ pointId, coordinate, coefficient }) => {
|
|
220
|
+
const base = variableIndex.get(pointId);
|
|
221
|
+
return base === undefined ? [] : [{ index: base + coordinate, coefficient }];
|
|
222
|
+
}),
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
for (const constraint of constraints) {
|
|
226
|
+
if (constraint.kind === "fixed")
|
|
227
|
+
continue;
|
|
228
|
+
if (constraint.kind === "perpendicular") {
|
|
229
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
230
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
231
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
232
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
233
|
+
continue;
|
|
234
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
235
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
236
|
+
const firstLength = Math.hypot(ux, uz), secondLength = Math.hypot(vx, vz);
|
|
237
|
+
if (firstLength <= CONSTRAINT_TOLERANCE_MM || secondLength <= CONSTRAINT_TOLERANCE_MM)
|
|
238
|
+
continue;
|
|
239
|
+
const cosine = (ux * vx + uz * vz) / (firstLength * secondLength);
|
|
240
|
+
const gux = vx / (firstLength * secondLength) - cosine * ux / (firstLength * firstLength);
|
|
241
|
+
const guz = vz / (firstLength * secondLength) - cosine * uz / (firstLength * firstLength);
|
|
242
|
+
const gvx = ux / (firstLength * secondLength) - cosine * vx / (secondLength * secondLength);
|
|
243
|
+
const gvz = uz / (firstLength * secondLength) - cosine * vz / (secondLength * secondLength);
|
|
244
|
+
add(cosine, [
|
|
245
|
+
{ pointId: first.id, coordinate: 0, coefficient: -gux }, { pointId: first.id, coordinate: 1, coefficient: -guz },
|
|
246
|
+
{ pointId: firstEnd.id, coordinate: 0, coefficient: gux }, { pointId: firstEnd.id, coordinate: 1, coefficient: guz },
|
|
247
|
+
{ pointId: second.id, coordinate: 0, coefficient: -gvx }, { pointId: second.id, coordinate: 1, coefficient: -gvz },
|
|
248
|
+
{ pointId: secondEnd.id, coordinate: 0, coefficient: gvx }, { pointId: secondEnd.id, coordinate: 1, coefficient: gvz },
|
|
249
|
+
]);
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (constraint.kind === "parallel") {
|
|
253
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
254
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
255
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
256
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
257
|
+
continue;
|
|
258
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
259
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
260
|
+
const firstLength = Math.hypot(ux, uz), secondLength = Math.hypot(vx, vz);
|
|
261
|
+
if (firstLength <= CONSTRAINT_TOLERANCE_MM || secondLength <= CONSTRAINT_TOLERANCE_MM)
|
|
262
|
+
continue;
|
|
263
|
+
const sine = (ux * vz - uz * vx) / (firstLength * secondLength);
|
|
264
|
+
const gux = vz / (firstLength * secondLength) - sine * ux / (firstLength * firstLength);
|
|
265
|
+
const guz = -vx / (firstLength * secondLength) - sine * uz / (firstLength * firstLength);
|
|
266
|
+
const gvx = -uz / (firstLength * secondLength) - sine * vx / (secondLength * secondLength);
|
|
267
|
+
const gvz = ux / (firstLength * secondLength) - sine * vz / (secondLength * secondLength);
|
|
268
|
+
add(sine, [
|
|
269
|
+
{ pointId: first.id, coordinate: 0, coefficient: -gux }, { pointId: first.id, coordinate: 1, coefficient: -guz },
|
|
270
|
+
{ pointId: firstEnd.id, coordinate: 0, coefficient: gux }, { pointId: firstEnd.id, coordinate: 1, coefficient: guz },
|
|
271
|
+
{ pointId: second.id, coordinate: 0, coefficient: -gvx }, { pointId: second.id, coordinate: 1, coefficient: -gvz },
|
|
272
|
+
{ pointId: secondEnd.id, coordinate: 0, coefficient: gvx }, { pointId: secondEnd.id, coordinate: 1, coefficient: gvz },
|
|
273
|
+
]);
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (constraint.kind === "tangent") {
|
|
277
|
+
const info = tangentInfo(constraint.segmentIds, profile, points);
|
|
278
|
+
if (!info)
|
|
279
|
+
continue;
|
|
280
|
+
const ux = info.lineEnd.x - info.lineStart.x;
|
|
281
|
+
const uz = info.lineEnd.z - info.lineStart.z;
|
|
282
|
+
const vx = info.curveX;
|
|
283
|
+
const vz = info.curveZ;
|
|
284
|
+
const lineLength = Math.hypot(ux, uz);
|
|
285
|
+
const curveLength = Math.hypot(vx, vz);
|
|
286
|
+
if (lineLength <= CONSTRAINT_TOLERANCE_MM || curveLength <= CONSTRAINT_TOLERANCE_MM)
|
|
287
|
+
continue;
|
|
288
|
+
const sine = (ux * vz - uz * vx) / (lineLength * curveLength);
|
|
289
|
+
const gux = vz / (lineLength * curveLength) - sine * ux / (lineLength * lineLength);
|
|
290
|
+
const guz = -vx / (lineLength * curveLength) - sine * uz / (lineLength * lineLength);
|
|
291
|
+
add(sine, [
|
|
292
|
+
{ pointId: info.lineStart.id, coordinate: 0, coefficient: -gux },
|
|
293
|
+
{ pointId: info.lineStart.id, coordinate: 1, coefficient: -guz },
|
|
294
|
+
{ pointId: info.lineEnd.id, coordinate: 0, coefficient: gux },
|
|
295
|
+
{ pointId: info.lineEnd.id, coordinate: 1, coefficient: guz },
|
|
296
|
+
]);
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
if (constraint.kind === "equalLength") {
|
|
300
|
+
const pair = equalLengthPair(constraint.segmentIds, profile);
|
|
301
|
+
const firstStart = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
302
|
+
const secondStart = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
303
|
+
if (!firstStart || !firstEnd || !secondStart || !secondEnd)
|
|
304
|
+
continue;
|
|
305
|
+
const firstDx = firstEnd.x - firstStart.x, firstDz = firstEnd.z - firstStart.z;
|
|
306
|
+
const secondDx = secondEnd.x - secondStart.x, secondDz = secondEnd.z - secondStart.z;
|
|
307
|
+
const firstLength = Math.hypot(firstDx, firstDz), secondLength = Math.hypot(secondDx, secondDz);
|
|
308
|
+
if (firstLength <= CONSTRAINT_TOLERANCE_MM || secondLength <= CONSTRAINT_TOLERANCE_MM)
|
|
309
|
+
continue;
|
|
310
|
+
const firstX = firstDx / firstLength, firstZ = firstDz / firstLength;
|
|
311
|
+
const secondX = secondDx / secondLength, secondZ = secondDz / secondLength;
|
|
312
|
+
add(firstLength - secondLength, [
|
|
313
|
+
{ pointId: firstStart.id, coordinate: 0, coefficient: -firstX },
|
|
314
|
+
{ pointId: firstStart.id, coordinate: 1, coefficient: -firstZ },
|
|
315
|
+
{ pointId: firstEnd.id, coordinate: 0, coefficient: firstX },
|
|
316
|
+
{ pointId: firstEnd.id, coordinate: 1, coefficient: firstZ },
|
|
317
|
+
{ pointId: secondStart.id, coordinate: 0, coefficient: secondX },
|
|
318
|
+
{ pointId: secondStart.id, coordinate: 1, coefficient: secondZ },
|
|
319
|
+
{ pointId: secondEnd.id, coordinate: 0, coefficient: -secondX },
|
|
320
|
+
{ pointId: secondEnd.id, coordinate: 1, coefficient: -secondZ },
|
|
321
|
+
]);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (constraint.kind === "angle") {
|
|
325
|
+
const pair = anglePair(constraint.segmentIds, profile);
|
|
326
|
+
const first = pair && points.get(pair[0].startId), firstEnd = pair && points.get(pair[0].endId);
|
|
327
|
+
const second = pair && points.get(pair[1].startId), secondEnd = pair && points.get(pair[1].endId);
|
|
328
|
+
if (!first || !firstEnd || !second || !secondEnd)
|
|
329
|
+
continue;
|
|
330
|
+
const ux = firstEnd.x - first.x, uz = firstEnd.z - first.z;
|
|
331
|
+
const vx = secondEnd.x - second.x, vz = secondEnd.z - second.z;
|
|
332
|
+
const firstLength = Math.hypot(ux, uz), secondLength = Math.hypot(vx, vz);
|
|
333
|
+
if (firstLength <= CONSTRAINT_TOLERANCE_MM || secondLength <= CONSTRAINT_TOLERANCE_MM)
|
|
334
|
+
continue;
|
|
335
|
+
const cosine = (ux * vx + uz * vz) / (firstLength * secondLength);
|
|
336
|
+
const sine = Math.sqrt(Math.max(0, 1 - cosine * cosine));
|
|
337
|
+
const scale = -1 / Math.max(sine, 1e-6);
|
|
338
|
+
const gux = (vx / (firstLength * secondLength) - cosine * ux / (firstLength * firstLength)) * scale;
|
|
339
|
+
const guz = (vz / (firstLength * secondLength) - cosine * uz / (firstLength * firstLength)) * scale;
|
|
340
|
+
const gvx = (ux / (firstLength * secondLength) - cosine * vx / (secondLength * secondLength)) * scale;
|
|
341
|
+
const gvz = (uz / (firstLength * secondLength) - cosine * vz / (secondLength * secondLength)) * scale;
|
|
342
|
+
add(Math.acos(Math.max(-1, Math.min(1, cosine))) - constraint.valueDeg * Math.PI / 180, [
|
|
343
|
+
{ pointId: first.id, coordinate: 0, coefficient: -gux }, { pointId: first.id, coordinate: 1, coefficient: -guz },
|
|
344
|
+
{ pointId: firstEnd.id, coordinate: 0, coefficient: gux }, { pointId: firstEnd.id, coordinate: 1, coefficient: guz },
|
|
345
|
+
{ pointId: second.id, coordinate: 0, coefficient: -gvx }, { pointId: second.id, coordinate: 1, coefficient: -gvz },
|
|
346
|
+
{ pointId: secondEnd.id, coordinate: 0, coefficient: gvx }, { pointId: secondEnd.id, coordinate: 1, coefficient: gvz },
|
|
347
|
+
]);
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (constraint.kind === "symmetry") {
|
|
351
|
+
const info = symmetryInfo(constraint.pointIds, constraint.segmentId, profile, points);
|
|
352
|
+
if (!info)
|
|
353
|
+
continue;
|
|
354
|
+
const { first, second, start, end, length, ux, uz } = info;
|
|
355
|
+
const middleX = (first.x + second.x) / 2 - start.x;
|
|
356
|
+
const middleZ = (first.z + second.z) / 2 - start.z;
|
|
357
|
+
const acrossX = second.x - first.x;
|
|
358
|
+
const acrossZ = second.z - first.z;
|
|
359
|
+
const onAxis = middleX * uz - middleZ * ux;
|
|
360
|
+
const acrossAxis = acrossX * ux + acrossZ * uz;
|
|
361
|
+
const firstOnAxisX = -uz + (middleX * ux * uz + middleZ * (1 - ux * ux)) / length;
|
|
362
|
+
const firstOnAxisZ = ux - (middleX * (1 - uz * uz) + middleZ * ux * uz) / length;
|
|
363
|
+
const secondOnAxisX = -(middleX * ux * uz + middleZ * (1 - ux * ux)) / length;
|
|
364
|
+
const secondOnAxisZ = (middleX * (1 - uz * uz) + middleZ * ux * uz) / length;
|
|
365
|
+
const firstAcrossAxisX = (-acrossX * (1 - ux * ux) + acrossZ * ux * uz) / length;
|
|
366
|
+
const firstAcrossAxisZ = (acrossX * ux * uz - acrossZ * (1 - uz * uz)) / length;
|
|
367
|
+
const secondAcrossAxisX = (acrossX * (1 - ux * ux) - acrossZ * ux * uz) / length;
|
|
368
|
+
const secondAcrossAxisZ = (-acrossX * ux * uz + acrossZ * (1 - uz * uz)) / length;
|
|
369
|
+
add(onAxis, [
|
|
370
|
+
{ pointId: first.id, coordinate: 0, coefficient: 0.5 * uz }, { pointId: first.id, coordinate: 1, coefficient: -0.5 * ux },
|
|
371
|
+
{ pointId: second.id, coordinate: 0, coefficient: 0.5 * uz }, { pointId: second.id, coordinate: 1, coefficient: -0.5 * ux },
|
|
372
|
+
{ pointId: start.id, coordinate: 0, coefficient: firstOnAxisX }, { pointId: start.id, coordinate: 1, coefficient: firstOnAxisZ },
|
|
373
|
+
{ pointId: end.id, coordinate: 0, coefficient: secondOnAxisX }, { pointId: end.id, coordinate: 1, coefficient: secondOnAxisZ },
|
|
374
|
+
]);
|
|
375
|
+
add(acrossAxis, [
|
|
376
|
+
{ pointId: first.id, coordinate: 0, coefficient: -ux }, { pointId: first.id, coordinate: 1, coefficient: -uz },
|
|
377
|
+
{ pointId: second.id, coordinate: 0, coefficient: ux }, { pointId: second.id, coordinate: 1, coefficient: uz },
|
|
378
|
+
{ pointId: start.id, coordinate: 0, coefficient: firstAcrossAxisX }, { pointId: start.id, coordinate: 1, coefficient: firstAcrossAxisZ },
|
|
379
|
+
{ pointId: end.id, coordinate: 0, coefficient: secondAcrossAxisX }, { pointId: end.id, coordinate: 1, coefficient: secondAcrossAxisZ },
|
|
380
|
+
]);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (constraint.kind === "horizontal" || constraint.kind === "vertical") {
|
|
384
|
+
const segment = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
385
|
+
const first = segment && points.get(segment.startId);
|
|
386
|
+
const second = segment && points.get(segment.endId);
|
|
387
|
+
if (!first || !second)
|
|
388
|
+
continue;
|
|
389
|
+
const coordinate = constraint.kind === "horizontal" ? 1 : 0;
|
|
390
|
+
add(first[coordinate === 1 ? "z" : "x"] - second[coordinate === 1 ? "z" : "x"], [
|
|
391
|
+
{ pointId: first.id, coordinate, coefficient: 1 },
|
|
392
|
+
{ pointId: second.id, coordinate, coefficient: -1 },
|
|
393
|
+
]);
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const pair = pointPair(constraint, points);
|
|
397
|
+
if (!pair)
|
|
398
|
+
continue;
|
|
399
|
+
const [first, second] = pair;
|
|
400
|
+
const dx = second.x - first.x;
|
|
401
|
+
const dz = second.z - first.z;
|
|
402
|
+
if (constraint.kind === "coincident") {
|
|
403
|
+
add(first.x - second.x, [
|
|
404
|
+
{ pointId: first.id, coordinate: 0, coefficient: 1 },
|
|
405
|
+
{ pointId: second.id, coordinate: 0, coefficient: -1 },
|
|
406
|
+
]);
|
|
407
|
+
add(first.z - second.z, [
|
|
408
|
+
{ pointId: first.id, coordinate: 1, coefficient: 1 },
|
|
409
|
+
{ pointId: second.id, coordinate: 1, coefficient: -1 },
|
|
410
|
+
]);
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const length = Math.hypot(dx, dz);
|
|
414
|
+
const unitX = length <= CONSTRAINT_TOLERANCE_MM ? DEGENERATE_DISTANCE_DIRECTION.x : dx / length;
|
|
415
|
+
const unitZ = length <= CONSTRAINT_TOLERANCE_MM ? DEGENERATE_DISTANCE_DIRECTION.z : dz / length;
|
|
416
|
+
const target = constraint.kind === "radius" ? constraint.valueMm * 2 : constraint.valueMm;
|
|
417
|
+
add(length - target, [
|
|
418
|
+
{ pointId: first.id, coordinate: 0, coefficient: -unitX },
|
|
419
|
+
{ pointId: first.id, coordinate: 1, coefficient: -unitZ },
|
|
420
|
+
{ pointId: second.id, coordinate: 0, coefficient: unitX },
|
|
421
|
+
{ pointId: second.id, coordinate: 1, coefficient: unitZ },
|
|
422
|
+
]);
|
|
423
|
+
}
|
|
424
|
+
return equations;
|
|
425
|
+
}
|
|
426
|
+
function objective(equations) {
|
|
427
|
+
return equations.reduce((sum, equation) => sum + equation.value * equation.value, 0);
|
|
428
|
+
}
|
|
429
|
+
function multiplyNormalMatrix(equations, vector, damping) {
|
|
430
|
+
const output = vector.map((value) => damping * value);
|
|
431
|
+
for (const equation of equations) {
|
|
432
|
+
const projection = equation.terms.reduce((sum, term) => sum + term.coefficient * vector[term.index], 0);
|
|
433
|
+
for (const term of equation.terms)
|
|
434
|
+
output[term.index] += term.coefficient * projection;
|
|
435
|
+
}
|
|
436
|
+
return output;
|
|
437
|
+
}
|
|
438
|
+
function solveNormalSystem(equations, variableCount, damping) {
|
|
439
|
+
const right = Array(variableCount).fill(0);
|
|
440
|
+
for (const equation of equations) {
|
|
441
|
+
for (const term of equation.terms)
|
|
442
|
+
right[term.index] -= term.coefficient * equation.value;
|
|
443
|
+
}
|
|
444
|
+
const solution = Array(variableCount).fill(0);
|
|
445
|
+
const residualVector = [...right];
|
|
446
|
+
const direction = [...right];
|
|
447
|
+
let residualSquared = residualVector.reduce((sum, value) => sum + value * value, 0);
|
|
448
|
+
const target = Math.max(1, Math.sqrt(residualSquared)) * 1e-8;
|
|
449
|
+
for (let iteration = 0; iteration < Math.min(MAX_CG_ITERATIONS, Math.max(16, variableCount * 2)) && Math.sqrt(residualSquared) > target; iteration += 1) {
|
|
450
|
+
const product = multiplyNormalMatrix(equations, direction, damping);
|
|
451
|
+
const denominator = direction.reduce((sum, value, index) => sum + value * product[index], 0);
|
|
452
|
+
if (!Number.isFinite(denominator) || denominator <= 1e-20)
|
|
453
|
+
break;
|
|
454
|
+
const step = residualSquared / denominator;
|
|
455
|
+
for (let index = 0; index < variableCount; index += 1) {
|
|
456
|
+
solution[index] += step * direction[index];
|
|
457
|
+
residualVector[index] -= step * product[index];
|
|
458
|
+
}
|
|
459
|
+
const nextSquared = residualVector.reduce((sum, value) => sum + value * value, 0);
|
|
460
|
+
if (!Number.isFinite(nextSquared))
|
|
461
|
+
break;
|
|
462
|
+
const ratio = nextSquared / Math.max(residualSquared, 1e-30);
|
|
463
|
+
for (let index = 0; index < variableCount; index += 1) {
|
|
464
|
+
direction[index] = residualVector[index] + ratio * direction[index];
|
|
465
|
+
}
|
|
466
|
+
residualSquared = nextSquared;
|
|
467
|
+
}
|
|
468
|
+
return solution;
|
|
469
|
+
}
|
|
470
|
+
function applyStep(points, variableIndex, step) {
|
|
471
|
+
const result = new Map(points);
|
|
472
|
+
for (const [pointId, base] of variableIndex) {
|
|
473
|
+
const point = result.get(pointId);
|
|
474
|
+
if (!point)
|
|
475
|
+
continue;
|
|
476
|
+
const dx = Math.max(-1000, Math.min(1000, step[base]));
|
|
477
|
+
const dz = Math.max(-1000, Math.min(1000, step[base + 1]));
|
|
478
|
+
result.set(pointId, movePoint(point, point.x + dx, point.z + dz));
|
|
479
|
+
}
|
|
480
|
+
return result;
|
|
481
|
+
}
|
|
482
|
+
function isPointDistanceConstraint(constraint) {
|
|
483
|
+
return constraint.kind === "distance" || constraint.kind === "radius" || constraint.kind === "diameter";
|
|
484
|
+
}
|
|
485
|
+
function breakCollinearDistanceDegeneracy(constraints, profile, points, variableIndex) {
|
|
486
|
+
const unsatisfiedDistances = constraints
|
|
487
|
+
.filter(isPointDistanceConstraint)
|
|
488
|
+
.filter((constraint) => residual(constraint, profile, points) > CONSTRAINT_TOLERANCE_MM);
|
|
489
|
+
if (!unsatisfiedDistances.length)
|
|
490
|
+
return null;
|
|
491
|
+
const neighbors = new Map();
|
|
492
|
+
const connect = (firstId, secondId) => {
|
|
493
|
+
if (!neighbors.has(firstId))
|
|
494
|
+
neighbors.set(firstId, new Set());
|
|
495
|
+
if (!neighbors.has(secondId))
|
|
496
|
+
neighbors.set(secondId, new Set());
|
|
497
|
+
neighbors.get(firstId).add(secondId);
|
|
498
|
+
neighbors.get(secondId).add(firstId);
|
|
499
|
+
};
|
|
500
|
+
for (const constraint of constraints) {
|
|
501
|
+
if (constraint.kind === "perpendicular") {
|
|
502
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
503
|
+
if (pair) {
|
|
504
|
+
const first = profile.segments.find(({ id }) => id === pair[0].id);
|
|
505
|
+
const second = profile.segments.find(({ id }) => id === pair[1].id);
|
|
506
|
+
connect(first.startId, first.endId);
|
|
507
|
+
connect(second.startId, second.endId);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
else if (constraint.kind === "parallel") {
|
|
511
|
+
const pair = segmentPair(constraint.segmentIds, profile);
|
|
512
|
+
if (pair) {
|
|
513
|
+
connect(pair[0].startId, pair[0].endId);
|
|
514
|
+
connect(pair[1].startId, pair[1].endId);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
else if (constraint.kind === "tangent") {
|
|
518
|
+
const info = tangentInfo(constraint.segmentIds, profile, points);
|
|
519
|
+
if (info)
|
|
520
|
+
connect(info.line.startId, info.line.endId);
|
|
521
|
+
}
|
|
522
|
+
else if (constraint.kind === "equalLength" || constraint.kind === "angle") {
|
|
523
|
+
const pair = constraint.kind === "equalLength" ? equalLengthPair(constraint.segmentIds, profile) : anglePair(constraint.segmentIds, profile);
|
|
524
|
+
if (pair) {
|
|
525
|
+
connect(pair[0].startId, pair[0].endId);
|
|
526
|
+
connect(pair[1].startId, pair[1].endId);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
else if (constraint.kind === "horizontal" || constraint.kind === "vertical") {
|
|
530
|
+
const segment = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
531
|
+
if (segment)
|
|
532
|
+
connect(segment.startId, segment.endId);
|
|
533
|
+
}
|
|
534
|
+
else if (constraint.kind === "symmetry") {
|
|
535
|
+
const axis = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
536
|
+
if (axis)
|
|
537
|
+
connect(axis.startId, axis.endId);
|
|
538
|
+
}
|
|
539
|
+
else if (constraint.kind === "coincident" || isPointDistanceConstraint(constraint)) {
|
|
540
|
+
connect(constraint.pointIds[0], constraint.pointIds[1]);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const componentIds = new Set();
|
|
544
|
+
const pending = unsatisfiedDistances.flatMap((constraint) => [...constraint.pointIds]);
|
|
545
|
+
while (pending.length) {
|
|
546
|
+
const pointId = pending.pop();
|
|
547
|
+
if (componentIds.has(pointId))
|
|
548
|
+
continue;
|
|
549
|
+
componentIds.add(pointId);
|
|
550
|
+
pending.push(...(neighbors.get(pointId) ?? []));
|
|
551
|
+
}
|
|
552
|
+
const componentPoints = profile.points.filter(({ id }) => componentIds.has(id));
|
|
553
|
+
if (componentPoints.length < 3)
|
|
554
|
+
return null;
|
|
555
|
+
let farthestPair = null;
|
|
556
|
+
let largestDistance = 0;
|
|
557
|
+
for (let firstIndex = 0; firstIndex < componentPoints.length; firstIndex += 1) {
|
|
558
|
+
for (let secondIndex = firstIndex + 1; secondIndex < componentPoints.length; secondIndex += 1) {
|
|
559
|
+
const first = points.get(componentPoints[firstIndex].id);
|
|
560
|
+
const second = points.get(componentPoints[secondIndex].id);
|
|
561
|
+
const length = distance(first, second);
|
|
562
|
+
if (length > largestDistance) {
|
|
563
|
+
largestDistance = length;
|
|
564
|
+
farthestPair = [first, second];
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (!farthestPair)
|
|
569
|
+
return null;
|
|
570
|
+
const [axisStart, axisEnd] = farthestPair;
|
|
571
|
+
const axisX = (axisEnd.x - axisStart.x) / largestDistance;
|
|
572
|
+
const axisZ = (axisEnd.z - axisStart.z) / largestDistance;
|
|
573
|
+
const maxDeviation = componentPoints.reduce((max, point) => {
|
|
574
|
+
const current = points.get(point.id);
|
|
575
|
+
const cross = Math.abs((current.x - axisStart.x) * axisZ - (current.z - axisStart.z) * axisX);
|
|
576
|
+
return Math.max(max, cross);
|
|
577
|
+
}, 0);
|
|
578
|
+
if (maxDeviation > Math.max(1, largestDistance) * 1e-10)
|
|
579
|
+
return null;
|
|
580
|
+
const pointToPerturb = [...componentPoints].reverse().find((point) => variableIndex.has(point.id));
|
|
581
|
+
if (!pointToPerturb)
|
|
582
|
+
return null;
|
|
583
|
+
const current = points.get(pointToPerturb.id);
|
|
584
|
+
const epsilon = Math.max(1e-3, largestDistance * 1e-4);
|
|
585
|
+
const next = new Map(points);
|
|
586
|
+
next.set(pointToPerturb.id, movePoint(current, current.x - axisZ * epsilon, current.z + axisX * epsilon));
|
|
587
|
+
return next;
|
|
588
|
+
}
|
|
589
|
+
/** Solve supported positional constraints together with a bounded damped least-squares iteration. */
|
|
590
|
+
export function solveSketchConstraints(profile, anchoredPointIds = []) {
|
|
591
|
+
const constraints = profile.constraints ?? [];
|
|
592
|
+
if (!constraints.length)
|
|
593
|
+
return { profile, unsatisfiedConstraintIds: [] };
|
|
594
|
+
const fixedPointConstraints = constraints.filter((constraint) => constraint.kind === "fixed");
|
|
595
|
+
const anchors = new Set([...anchoredPointIds, ...fixedPointConstraints.map((constraint) => constraint.pointId)]);
|
|
596
|
+
const constrainedPointIds = new Set(constraints.flatMap((constraint) => {
|
|
597
|
+
if (constraint.kind === "fixed")
|
|
598
|
+
return [constraint.pointId];
|
|
599
|
+
if (constraint.kind === "perpendicular" || constraint.kind === "parallel" || constraint.kind === "equalLength" || constraint.kind === "angle")
|
|
600
|
+
return constraint.segmentIds.flatMap((id) => {
|
|
601
|
+
const segment = profile.segments.find(({ id: candidate }) => candidate === id);
|
|
602
|
+
return segment ? [segment.startId, segment.endId] : [];
|
|
603
|
+
});
|
|
604
|
+
if (constraint.kind === "tangent") {
|
|
605
|
+
const info = tangentInfo(constraint.segmentIds, profile, new Map(profile.points.map((point) => [point.id, point])));
|
|
606
|
+
return info ? [info.line.startId, info.line.endId] : [];
|
|
607
|
+
}
|
|
608
|
+
if (constraint.kind === "horizontal" || constraint.kind === "vertical") {
|
|
609
|
+
const segment = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
610
|
+
return segment ? [segment.startId, segment.endId] : [];
|
|
611
|
+
}
|
|
612
|
+
if (constraint.kind === "symmetry") {
|
|
613
|
+
const axis = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
614
|
+
return [...constraint.pointIds, ...(axis ? [axis.startId, axis.endId] : [])];
|
|
615
|
+
}
|
|
616
|
+
return constraint.pointIds;
|
|
617
|
+
}));
|
|
618
|
+
const variableIndex = new Map();
|
|
619
|
+
for (const point of profile.points) {
|
|
620
|
+
if (!anchors.has(point.id) && constrainedPointIds.has(point.id))
|
|
621
|
+
variableIndex.set(point.id, variableIndex.size * 2);
|
|
622
|
+
}
|
|
623
|
+
const variableCount = variableIndex.size * 2;
|
|
624
|
+
let points = new Map(profile.points.map((point) => [point.id, point]));
|
|
625
|
+
const initializedFixedPointIds = new Set();
|
|
626
|
+
for (const constraint of fixedPointConstraints) {
|
|
627
|
+
const point = points.get(constraint.pointId);
|
|
628
|
+
if (point && !initializedFixedPointIds.has(point.id)) {
|
|
629
|
+
points.set(point.id, movePoint(point, constraint.xMm, constraint.zMm));
|
|
630
|
+
initializedFixedPointIds.add(point.id);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
let damping = INITIAL_DAMPING;
|
|
634
|
+
let currentEquations = equationRows(constraints, profile, points, variableIndex);
|
|
635
|
+
let currentObjective = objective(currentEquations);
|
|
636
|
+
let bestPoints = points;
|
|
637
|
+
let bestObjective = currentObjective;
|
|
638
|
+
let seededCollinearGeometry = false;
|
|
639
|
+
for (let iteration = 0; iteration < MAX_GLOBAL_ITERATIONS && variableCount > 0; iteration += 1) {
|
|
640
|
+
if (currentEquations.every(({ value }) => Math.abs(value) <= SOLVER_CONVERGENCE_TOLERANCE_MM))
|
|
641
|
+
break;
|
|
642
|
+
const step = solveNormalSystem(currentEquations, variableCount, damping);
|
|
643
|
+
const maxStep = Math.max(0, ...step.map(Math.abs));
|
|
644
|
+
if (!Number.isFinite(maxStep) || maxStep < 1e-12)
|
|
645
|
+
break;
|
|
646
|
+
let accepted = null;
|
|
647
|
+
for (const scale of [1, 0.5, 0.25, 0.125, 0.0625]) {
|
|
648
|
+
const candidateStep = step.map((value) => value * scale);
|
|
649
|
+
const candidatePoints = applyStep(points, variableIndex, candidateStep);
|
|
650
|
+
const candidateEquations = equationRows(constraints, profile, candidatePoints, variableIndex);
|
|
651
|
+
const candidateObjective = objective(candidateEquations);
|
|
652
|
+
if (Number.isFinite(candidateObjective) && candidateObjective < currentObjective - Math.max(1e-16, currentObjective * 1e-12)) {
|
|
653
|
+
accepted = { points: candidatePoints, equations: candidateEquations, objective: candidateObjective };
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (!accepted) {
|
|
658
|
+
if (!seededCollinearGeometry) {
|
|
659
|
+
const seeded = breakCollinearDistanceDegeneracy(constraints, profile, points, variableIndex);
|
|
660
|
+
if (seeded) {
|
|
661
|
+
points = seeded;
|
|
662
|
+
currentEquations = equationRows(constraints, profile, points, variableIndex);
|
|
663
|
+
currentObjective = objective(currentEquations);
|
|
664
|
+
if (currentObjective < bestObjective) {
|
|
665
|
+
bestPoints = points;
|
|
666
|
+
bestObjective = currentObjective;
|
|
667
|
+
}
|
|
668
|
+
damping = INITIAL_DAMPING;
|
|
669
|
+
seededCollinearGeometry = true;
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
damping *= 10;
|
|
674
|
+
if (damping > 1e8)
|
|
675
|
+
break;
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
const previousObjective = currentObjective;
|
|
679
|
+
points = accepted.points;
|
|
680
|
+
currentEquations = accepted.equations;
|
|
681
|
+
currentObjective = accepted.objective;
|
|
682
|
+
if (currentObjective < bestObjective) {
|
|
683
|
+
bestPoints = points;
|
|
684
|
+
bestObjective = currentObjective;
|
|
685
|
+
}
|
|
686
|
+
damping = Math.max(1e-9, damping * 0.3);
|
|
687
|
+
const residualAboveTolerance = currentEquations.some(({ value }) => Math.abs(value) > CONSTRAINT_TOLERANCE_MM);
|
|
688
|
+
const relativeProgress = (previousObjective - currentObjective) / Math.max(previousObjective, 1e-30);
|
|
689
|
+
if (!seededCollinearGeometry && residualAboveTolerance && relativeProgress < 1e-5) {
|
|
690
|
+
const seeded = breakCollinearDistanceDegeneracy(constraints, profile, points, variableIndex);
|
|
691
|
+
if (seeded) {
|
|
692
|
+
points = seeded;
|
|
693
|
+
currentEquations = equationRows(constraints, profile, points, variableIndex);
|
|
694
|
+
currentObjective = objective(currentEquations);
|
|
695
|
+
if (currentObjective < bestObjective) {
|
|
696
|
+
bestPoints = points;
|
|
697
|
+
bestObjective = currentObjective;
|
|
698
|
+
}
|
|
699
|
+
damping = INITIAL_DAMPING;
|
|
700
|
+
seededCollinearGeometry = true;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
const solved = { ...profile, points: profile.points.map((point) => bestPoints.get(point.id) ?? point) };
|
|
705
|
+
const unsatisfiedConstraintIds = constraints
|
|
706
|
+
.filter((constraint) => {
|
|
707
|
+
const error = residual(constraint, solved, bestPoints);
|
|
708
|
+
return !Number.isFinite(error) || error > CONSTRAINT_TOLERANCE_MM;
|
|
709
|
+
})
|
|
710
|
+
.map((constraint) => constraint.id);
|
|
711
|
+
return { profile: solved, unsatisfiedConstraintIds };
|
|
712
|
+
}
|
|
713
|
+
export function sketchConstraintsForProfile(profile) {
|
|
714
|
+
const pointIds = new Set(profile.points.map((point) => point.id));
|
|
715
|
+
const segmentIds = new Set(profile.segments.map((segment) => segment.id));
|
|
716
|
+
return (profile.constraints ?? []).filter((constraint) => constraint.kind === "fixed" ? pointIds.has(constraint.pointId)
|
|
717
|
+
: constraint.kind === "horizontal" || constraint.kind === "vertical"
|
|
718
|
+
? segmentIds.has(constraint.segmentId)
|
|
719
|
+
: constraint.kind === "perpendicular" || constraint.kind === "parallel" ? constraint.segmentIds.every((id) => segmentIds.has(id))
|
|
720
|
+
: constraint.kind === "tangent" ? isSketchTangentPair(constraint.segmentIds, profile)
|
|
721
|
+
: constraint.kind === "equalLength" ? isSketchEqualLengthPair(constraint.segmentIds, profile)
|
|
722
|
+
: constraint.kind === "angle" ? isSketchAnglePair(constraint.segmentIds, profile)
|
|
723
|
+
: constraint.kind === "symmetry" ? isSketchSymmetrySetup(constraint.pointIds, constraint.segmentId, profile)
|
|
724
|
+
: pointIds.has(constraint.pointIds[0]) && pointIds.has(constraint.pointIds[1]));
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Estimates local parameter freedom from the rank of the supported constraint
|
|
728
|
+
* equations at the profile's current coordinates. This is not a solver or a
|
|
729
|
+
* proof of global constraint satisfaction. Bezier handles and sweep depths are
|
|
730
|
+
* counted as free parameters because the supported constraints do not constrain
|
|
731
|
+
* them.
|
|
732
|
+
*/
|
|
733
|
+
export function sketchConstraintDegreesOfFreedom(profile) {
|
|
734
|
+
const constraints = sketchConstraintsForProfile(profile);
|
|
735
|
+
const pointIndex = new Map(profile.points.map((point, index) => [point.id, index * 2]));
|
|
736
|
+
const parameterCount = profile.points.reduce((count, point) => count + 2
|
|
737
|
+
+ (point.handleIn ? 2 : 0) + (point.handleOut ? 2 : 0) + (point.sweepDepth !== undefined ? 1 : 0), 0);
|
|
738
|
+
const equationCount = constraints.reduce((count, constraint) => count
|
|
739
|
+
+ (constraint.kind === "fixed" || constraint.kind === "coincident" || constraint.kind === "symmetry" ? 2 : 1), 0);
|
|
740
|
+
const rows = [];
|
|
741
|
+
for (const constraint of constraints) {
|
|
742
|
+
if (constraint.kind === "fixed") {
|
|
743
|
+
const anchorIndex = pointIndex.get(constraint.pointId);
|
|
744
|
+
const xRow = Array(parameterCount).fill(0);
|
|
745
|
+
const zRow = Array(parameterCount).fill(0);
|
|
746
|
+
xRow[anchorIndex] = 1;
|
|
747
|
+
zRow[anchorIndex + 1] = 1;
|
|
748
|
+
rows.push(xRow, zRow);
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
if (constraint.kind === "perpendicular" || constraint.kind === "parallel" || constraint.kind === "angle") {
|
|
752
|
+
const firstSegment = profile.segments.find(({ id }) => id === constraint.segmentIds[0]);
|
|
753
|
+
const secondSegment = profile.segments.find(({ id }) => id === constraint.segmentIds[1]);
|
|
754
|
+
const ids = [firstSegment.startId, firstSegment.endId, secondSegment.startId, secondSegment.endId];
|
|
755
|
+
const points = ids.map((id) => profile.points.find((point) => point.id === id));
|
|
756
|
+
const [a, b, c, d] = points;
|
|
757
|
+
const ux = b.x - a.x, uz = b.z - a.z, vx = d.x - c.x, vz = d.z - c.z;
|
|
758
|
+
const lu = Math.hypot(ux, uz), lv = Math.hypot(vx, vz);
|
|
759
|
+
if (lu <= CONSTRAINT_TOLERANCE_MM || lv <= CONSTRAINT_TOLERANCE_MM)
|
|
760
|
+
return { parameterCount, equationCount, rank: null, degreesOfFreedom: null, status: "indeterminate", reason: "degenerate-segment" };
|
|
761
|
+
const usesSine = constraint.kind === "parallel";
|
|
762
|
+
const relation = usesSine ? (ux * vz - uz * vx) / (lu * lv) : (ux * vx + uz * vz) / (lu * lv);
|
|
763
|
+
const gux = (usesSine ? vz : vx) / (lu * lv) - relation * ux / (lu * lu);
|
|
764
|
+
const guz = (usesSine ? -vx : vz) / (lu * lv) - relation * uz / (lu * lu);
|
|
765
|
+
const gvx = (usesSine ? -uz : ux) / (lu * lv) - relation * vx / (lv * lv);
|
|
766
|
+
const gvz = (usesSine ? ux : uz) / (lu * lv) - relation * vz / (lv * lv);
|
|
767
|
+
const scale = constraint.kind === "angle" ? -1 / Math.max(Math.sqrt(Math.max(0, 1 - relation * relation)), 1e-6) : 1;
|
|
768
|
+
const row = Array(parameterCount).fill(0);
|
|
769
|
+
const coefficients = [-gux * scale, -guz * scale, gux * scale, guz * scale, -gvx * scale, -gvz * scale, gvx * scale, gvz * scale];
|
|
770
|
+
ids.forEach((id, index) => {
|
|
771
|
+
const base = pointIndex.get(id);
|
|
772
|
+
row[base] = (row[base] ?? 0) + coefficients[index * 2];
|
|
773
|
+
row[base + 1] = (row[base + 1] ?? 0) + coefficients[index * 2 + 1];
|
|
774
|
+
});
|
|
775
|
+
rows.push(row);
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
if (constraint.kind === "tangent") {
|
|
779
|
+
const info = tangentInfo(constraint.segmentIds, profile, new Map(profile.points.map((point) => [point.id, point])));
|
|
780
|
+
if (!info)
|
|
781
|
+
return { parameterCount, equationCount, rank: null, degreesOfFreedom: null, status: "indeterminate", reason: "degenerate-segment" };
|
|
782
|
+
const ux = info.lineEnd.x - info.lineStart.x;
|
|
783
|
+
const uz = info.lineEnd.z - info.lineStart.z;
|
|
784
|
+
const vx = info.curveX;
|
|
785
|
+
const vz = info.curveZ;
|
|
786
|
+
const lu = Math.hypot(ux, uz), lv = Math.hypot(vx, vz);
|
|
787
|
+
if (lu <= CONSTRAINT_TOLERANCE_MM || lv <= CONSTRAINT_TOLERANCE_MM)
|
|
788
|
+
return { parameterCount, equationCount, rank: null, degreesOfFreedom: null, status: "indeterminate", reason: "degenerate-segment" };
|
|
789
|
+
const sine = (ux * vz - uz * vx) / (lu * lv);
|
|
790
|
+
const gux = vz / (lu * lv) - sine * ux / (lu * lu);
|
|
791
|
+
const guz = -vx / (lu * lv) - sine * uz / (lu * lu);
|
|
792
|
+
const row = Array(parameterCount).fill(0);
|
|
793
|
+
const start = pointIndex.get(info.line.startId);
|
|
794
|
+
const end = pointIndex.get(info.line.endId);
|
|
795
|
+
row[start] = (row[start] ?? 0) - gux;
|
|
796
|
+
row[start + 1] = (row[start + 1] ?? 0) - guz;
|
|
797
|
+
row[end] = (row[end] ?? 0) + gux;
|
|
798
|
+
row[end + 1] = (row[end + 1] ?? 0) + guz;
|
|
799
|
+
rows.push(row);
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
if (constraint.kind === "equalLength") {
|
|
803
|
+
const pair = equalLengthPair(constraint.segmentIds, profile);
|
|
804
|
+
if (!pair)
|
|
805
|
+
continue;
|
|
806
|
+
const ids = pair.flatMap(({ startId, endId }) => [startId, endId]);
|
|
807
|
+
const coordinates = ids.map((id) => profile.points.find((point) => point.id === id));
|
|
808
|
+
const [a, b, c, d] = coordinates;
|
|
809
|
+
const ux = b.x - a.x, uz = b.z - a.z, vx = d.x - c.x, vz = d.z - c.z;
|
|
810
|
+
const lu = Math.hypot(ux, uz), lv = Math.hypot(vx, vz);
|
|
811
|
+
if (lu <= CONSTRAINT_TOLERANCE_MM || lv <= CONSTRAINT_TOLERANCE_MM)
|
|
812
|
+
return { parameterCount, equationCount, rank: null, degreesOfFreedom: null, status: "indeterminate", reason: "degenerate-segment" };
|
|
813
|
+
const coefficients = [-ux / lu, -uz / lu, ux / lu, uz / lu, vx / lv, vz / lv, -vx / lv, -vz / lv];
|
|
814
|
+
const row = Array(parameterCount).fill(0);
|
|
815
|
+
ids.forEach((id, index) => {
|
|
816
|
+
const base = pointIndex.get(id);
|
|
817
|
+
row[base] = (row[base] ?? 0) + coefficients[index * 2];
|
|
818
|
+
row[base + 1] = (row[base + 1] ?? 0) + coefficients[index * 2 + 1];
|
|
819
|
+
});
|
|
820
|
+
rows.push(row);
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
if (constraint.kind === "symmetry") {
|
|
824
|
+
const info = symmetryInfo(constraint.pointIds, constraint.segmentId, profile);
|
|
825
|
+
if (!info)
|
|
826
|
+
continue;
|
|
827
|
+
const { first, second, start, end, length, ux, uz } = info;
|
|
828
|
+
const middleX = (first.x + second.x) / 2 - start.x;
|
|
829
|
+
const middleZ = (first.z + second.z) / 2 - start.z;
|
|
830
|
+
const acrossX = second.x - first.x;
|
|
831
|
+
const acrossZ = second.z - first.z;
|
|
832
|
+
const firstOnAxisX = -uz + (middleX * ux * uz + middleZ * (1 - ux * ux)) / length;
|
|
833
|
+
const firstOnAxisZ = ux - (middleX * (1 - uz * uz) + middleZ * ux * uz) / length;
|
|
834
|
+
const secondOnAxisX = -(middleX * ux * uz + middleZ * (1 - ux * ux)) / length;
|
|
835
|
+
const secondOnAxisZ = (middleX * (1 - uz * uz) + middleZ * ux * uz) / length;
|
|
836
|
+
const firstAcrossAxisX = (-acrossX * (1 - ux * ux) + acrossZ * ux * uz) / length;
|
|
837
|
+
const firstAcrossAxisZ = (acrossX * ux * uz - acrossZ * (1 - uz * uz)) / length;
|
|
838
|
+
const secondAcrossAxisX = (acrossX * (1 - ux * ux) - acrossZ * ux * uz) / length;
|
|
839
|
+
const secondAcrossAxisZ = (-acrossX * ux * uz + acrossZ * (1 - uz * uz)) / length;
|
|
840
|
+
const onAxisRow = Array(parameterCount).fill(0);
|
|
841
|
+
const acrossAxisRow = Array(parameterCount).fill(0);
|
|
842
|
+
const bases = [first.id, second.id, start.id, end.id].map((id) => pointIndex.get(id));
|
|
843
|
+
const onAxisCoefficients = [0.5 * uz, -0.5 * ux, 0.5 * uz, -0.5 * ux, firstOnAxisX, firstOnAxisZ, secondOnAxisX, secondOnAxisZ];
|
|
844
|
+
const acrossAxisCoefficients = [-ux, -uz, ux, uz, firstAcrossAxisX, firstAcrossAxisZ, secondAcrossAxisX, secondAcrossAxisZ];
|
|
845
|
+
bases.forEach((base, index) => {
|
|
846
|
+
onAxisRow[base] = (onAxisRow[base] ?? 0) + onAxisCoefficients[index * 2];
|
|
847
|
+
onAxisRow[base + 1] = (onAxisRow[base + 1] ?? 0) + onAxisCoefficients[index * 2 + 1];
|
|
848
|
+
acrossAxisRow[base] = (acrossAxisRow[base] ?? 0) + acrossAxisCoefficients[index * 2];
|
|
849
|
+
acrossAxisRow[base + 1] = (acrossAxisRow[base + 1] ?? 0) + acrossAxisCoefficients[index * 2 + 1];
|
|
850
|
+
});
|
|
851
|
+
rows.push(onAxisRow, acrossAxisRow);
|
|
852
|
+
continue;
|
|
853
|
+
}
|
|
854
|
+
if (constraint.kind === "horizontal" || constraint.kind === "vertical") {
|
|
855
|
+
const segment = profile.segments.find(({ id }) => id === constraint.segmentId);
|
|
856
|
+
const start = pointIndex.get(segment.startId);
|
|
857
|
+
const end = pointIndex.get(segment.endId);
|
|
858
|
+
const row = Array(parameterCount).fill(0);
|
|
859
|
+
const coordinate = constraint.kind === "horizontal" ? 1 : 0;
|
|
860
|
+
row[start + coordinate] = (row[start + coordinate] ?? 0) + 1;
|
|
861
|
+
row[end + coordinate] = (row[end + coordinate] ?? 0) - 1;
|
|
862
|
+
rows.push(row);
|
|
863
|
+
continue;
|
|
864
|
+
}
|
|
865
|
+
const [firstId, secondId] = constraint.pointIds;
|
|
866
|
+
const firstIndex = pointIndex.get(firstId);
|
|
867
|
+
const secondIndex = pointIndex.get(secondId);
|
|
868
|
+
if (constraint.kind === "coincident") {
|
|
869
|
+
for (const coordinate of [0, 1]) {
|
|
870
|
+
const row = Array(parameterCount).fill(0);
|
|
871
|
+
row[firstIndex + coordinate] = (row[firstIndex + coordinate] ?? 0) + 1;
|
|
872
|
+
row[secondIndex + coordinate] = (row[secondIndex + coordinate] ?? 0) - 1;
|
|
873
|
+
rows.push(row);
|
|
874
|
+
}
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
const first = profile.points.find(({ id }) => id === firstId);
|
|
878
|
+
const second = profile.points.find(({ id }) => id === secondId);
|
|
879
|
+
const dx = second.x - first.x;
|
|
880
|
+
const dz = second.z - first.z;
|
|
881
|
+
const length = Math.hypot(dx, dz);
|
|
882
|
+
if (length <= CONSTRAINT_TOLERANCE_MM) {
|
|
883
|
+
return { parameterCount, equationCount, rank: null, degreesOfFreedom: null, status: "indeterminate", reason: "degenerate-distance" };
|
|
884
|
+
}
|
|
885
|
+
const row = Array(parameterCount).fill(0);
|
|
886
|
+
row[firstIndex] = -dx / length;
|
|
887
|
+
row[firstIndex + 1] = -dz / length;
|
|
888
|
+
row[secondIndex] = dx / length;
|
|
889
|
+
row[secondIndex + 1] = dz / length;
|
|
890
|
+
rows.push(row);
|
|
891
|
+
}
|
|
892
|
+
// Row reduction with partial pivoting gives the local constraint rank, so
|
|
893
|
+
// duplicate and dependent equations only remove freedom once.
|
|
894
|
+
let rank = 0;
|
|
895
|
+
const matrix = rows.map((row) => [...row]);
|
|
896
|
+
for (let column = 0; column < parameterCount && rank < matrix.length; column += 1) {
|
|
897
|
+
let pivot = rank;
|
|
898
|
+
for (let row = rank + 1; row < matrix.length; row += 1) {
|
|
899
|
+
if (Math.abs(matrix[row][column]) > Math.abs(matrix[pivot][column]))
|
|
900
|
+
pivot = row;
|
|
901
|
+
}
|
|
902
|
+
if (Math.abs(matrix[pivot]?.[column] ?? 0) < 1e-8)
|
|
903
|
+
continue;
|
|
904
|
+
[matrix[rank], matrix[pivot]] = [matrix[pivot], matrix[rank]];
|
|
905
|
+
const divisor = matrix[rank][column];
|
|
906
|
+
for (let index = column; index < parameterCount; index += 1)
|
|
907
|
+
matrix[rank][index] /= divisor;
|
|
908
|
+
for (let row = 0; row < matrix.length; row += 1) {
|
|
909
|
+
if (row === rank)
|
|
910
|
+
continue;
|
|
911
|
+
const factor = matrix[row][column];
|
|
912
|
+
for (let index = column; index < parameterCount; index += 1)
|
|
913
|
+
matrix[row][index] -= factor * matrix[rank][index];
|
|
914
|
+
}
|
|
915
|
+
rank += 1;
|
|
916
|
+
}
|
|
917
|
+
return { parameterCount, equationCount, rank, degreesOfFreedom: parameterCount - rank, status: "estimated" };
|
|
918
|
+
}
|
|
919
|
+
//# sourceMappingURL=sketchConstraints.js.map
|