@margoengine/splines 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +118 -3
- package/dist/closest.d.ts +36 -0
- package/dist/closest.js +101 -0
- package/dist/fit.d.ts +29 -0
- package/dist/fit.js +175 -0
- package/dist/frames.d.ts +48 -0
- package/dist/frames.js +113 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +86 -0
- package/dist/kinds.d.ts +27 -0
- package/dist/kinds.js +76 -0
- package/dist/knots.d.ts +81 -0
- package/dist/knots.js +196 -0
- package/dist/mesh.d.ts +72 -0
- package/dist/mesh.js +106 -0
- package/dist/offset.d.ts +33 -0
- package/dist/offset.js +176 -0
- package/dist/polynomial-we3se7Ae.js +40 -0
- package/dist/polynomial.d.ts +6 -0
- package/dist/query.d.ts +50 -0
- package/dist/query.js +394 -0
- package/package.json +78 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Scott Campbell
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @margoengine/splines
|
|
2
|
+
|
|
3
|
+
Import `margoengine/splines` from the `margoengine` package, or `@margoengine/splines` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
|
|
4
|
+
|
|
5
|
+
Piecewise 3D Catmull-Rom, cubic Bezier, cubic Hermite, uniform B-spline and Kochanek-Bartels curves, with a cached arc-length lookup, closest point, stable frames, knot editing, mesh placement and bending, and curve fitting.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import {Spline} from 'margoengine/splines';
|
|
9
|
+
const curve = new Spline([[0,0,0], [5,1,5], [10,0,10]]);
|
|
10
|
+
const point = new Float64Array(3);
|
|
11
|
+
curve.atDistance(curve.length / 2, point);
|
|
12
|
+
curve.tangent(0.5, point);
|
|
13
|
+
const sample = curve.at(0.5); // Always a typed array, including when output is omitted.
|
|
14
|
+
console.log(sample[0]);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`Spline` and `CubicSpline` can be passed directly to `PathControls` from
|
|
18
|
+
`@margoengine/controls/presentation`; their sample methods always return an array.
|
|
19
|
+
|
|
20
|
+
Bezier curves require 3n+1 points and are open. Hermite requires one tangent per point; Catmull-Rom and Hermite can be closed. at uses t in 0..1; atDistance uses world units. Returned default output is reused, so provide an output buffer to retain multiple results. After editing points, call updateArcLengths. Arc lengths are numerical approximations controlled by divisions. Tangents use finite differences that wrap across a closed seam; the two endpoint tangents are identical. Closed-loop frames join their reference normals after twist correction, so mesh placement and deformation use the same endpoint frame.
|
|
21
|
+
|
|
22
|
+
## More curve types
|
|
23
|
+
|
|
24
|
+
`kind: 'bspline'` is the uniform cubic B-spline: smoother than Catmull-Rom (continuous curvature) but it only pulls toward interior points, so it does not pass through them; open curves start and end exactly at the first and last point. `kind: 'kochanek-bartels'` passes through every point and adds `tension`, `continuity` and `bias`, each -1 to 1 and 0 by default (all zero is Catmull-Rom).
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
const waypoints: [number, number, number][] = [[0, 2, 0], [5, 3, 5], [10, 2, 10], [15, 4, 5]];
|
|
28
|
+
const camera = new Spline(waypoints, {kind: 'kochanek-bartels', tension: 0.3, bias: -0.2});
|
|
29
|
+
const smooth = new Spline(waypoints, {kind: 'bspline', closed: true});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Closest point (`@margoengine/splines/closest`)
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import {Spline} from 'margoengine/splines';
|
|
36
|
+
import {closestPointOnSpline} from 'margoengine/splines/closest';
|
|
37
|
+
const curve = new Spline([[0, 0, 0], [5, 1, 5], [10, 0, 10]]);
|
|
38
|
+
const hit = closestPointOnSpline(curve, [3, 1, 4]);
|
|
39
|
+
// hit.t (0..1), hit.arc (world units along the curve), hit.point, hit.distance
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The answer is exact for every curve kind, however many pieces the curve has and however close it passes to itself: each cubic piece whose control-point box could hold a nearer point has the stationary points of its squared distance (the roots of a quintic) isolated on its Bernstein form and refined by bisection (`iterations`, default 52). `samples` (default the spline's `divisions`) only seeds the search. It used to be a sample-then-bisect search that could miss the nearest pass when a curve had about as many pieces as samples. `distanceAt(spline, t)` and `parameterAt(spline, distance)` convert between `t` and arc length.
|
|
43
|
+
|
|
44
|
+
## Stable frames and roll (`@margoengine/splines/frames`)
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import {Spline} from 'margoengine/splines';
|
|
48
|
+
import {SplineFrames} from 'margoengine/splines/frames';
|
|
49
|
+
const curve = new Spline([[0, 0, 0], [5, 1, 5], [10, 0, 10]]);
|
|
50
|
+
const frames = new SplineFrames(curve, {up: [0, 1, 0], roll: (t) => t * 0.5});
|
|
51
|
+
const frame = frames.frameAt(curve.length * 0.25);
|
|
52
|
+
// frame.position, frame.tangent, frame.normal, frame.binormal (binormal = tangent x normal)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Frames are carried along the curve by double reflection (rotation-minimising), so they do not flip where a fixed up vector would (a curve passing through the up direction). `roll` is radians about the tangent: a function of `t`, or one value per knot (`knotCount(curve)`, blended linearly). A closed curve's twist is spread over its length so the last frame meets the first (`closeLoop: false` turns that off). Frames are sampled at `samples` (default 256) steps and blended in between.
|
|
56
|
+
|
|
57
|
+
## Knot editing (`@margoengine/splines/knots`)
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import {KnotPath} from 'margoengine/splines/knots';
|
|
61
|
+
const path = new KnotPath([
|
|
62
|
+
{position: [0, 0, 0], data: [1]},
|
|
63
|
+
{position: [4, 3, 0], data: [2]},
|
|
64
|
+
{position: [8, 0, 0], mode: 'linear', data: [4]},
|
|
65
|
+
]);
|
|
66
|
+
path.insertAt(0, 0.5); // split the first segment; the curve does not move
|
|
67
|
+
path.setMode(1, 'broken', {tangentOut: [0, 2, 0]});
|
|
68
|
+
const spline = path.toSpline(); // a Bezier Spline
|
|
69
|
+
const width = path.dataAt(0.7)[0]; // per-knot numbers blended along the curve
|
|
70
|
+
const [a, b] = path.split(2); // two paths sharing knot 2
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Modes: `auto` (smooth through the knot), `linear` (handles point at the neighbors; a segment between two linear knots is straight) and `broken` (the stored `tangentIn` and `tangentOut`, so a corner can point any way). `insertAt` keeps the shape by turning the new knot and its two neighbors `broken`; pass `false` as the third argument to insert an `auto` knot and let the curve reshape. A closed path builds an open Bezier whose last point is its first.
|
|
74
|
+
|
|
75
|
+
## Place and deform along a spline (`@margoengine/splines/mesh`)
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
79
|
+
import {Spline} from 'margoengine/splines';
|
|
80
|
+
import {placeAlongSpline, deformAlongSpline} from 'margoengine/splines/mesh';
|
|
81
|
+
const curve = new Spline([[0, 0, 0], [5, 1, 5], [10, 0, 10]]);
|
|
82
|
+
const posts = placeAlongSpline(curve, {spacing: 2.5, fit: true, forward: 'z'});
|
|
83
|
+
// posts.matrices: count column-major 4x4 matrices, ready for an instance buffer
|
|
84
|
+
const {positions, normals} = boxGeometry(0.2, 0.2, 1, 1, 1, 16);
|
|
85
|
+
const pipe = deformAlongSpline(curve, positions, {axis: 'z', normals, fit: 'stretch'});
|
|
86
|
+
// pipe.positions, pipe.normals
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`frameBasis(frame, forward)` gives that same turn for one frame. `placeAlongSpline` puts the mesh's `forward` axis along the tangent with its up axis on the frame normal (`frames` takes `SplineFrames` options or a ready instance; `align: false` keeps every instance upright). `deformAlongSpline` turns each vertex's coordinate along `axis` into a distance along the curve and its other two coordinates into offsets in the frame there; `fit: 'stretch'` scales the mesh to the curve's length and `overflow` chooses between continuing straight past the ends or holding the end frame. A mesh bends in straight chords between its vertices, so subdivide long pieces first. Normals are rotated with the frame, not re-derived.
|
|
90
|
+
|
|
91
|
+
## Fit a spline to points (`@margoengine/splines/fit`)
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import {fitSpline} from 'margoengine/splines/fit';
|
|
95
|
+
const gpsTrack: [number, number, number][] = [[0, 0, 0], [3, 0, 1], [6, 0, 1.5], [9, 0, 1], [12, 0, 0]];
|
|
96
|
+
const {spline, points, pieces, maxError} = fitSpline(gpsTrack, {tolerance: 0.5});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Every input point ends up within `tolerance` of the returned cubic Bezier curve (`maxError` is the measured worst, at most `tolerance`). Pieces are cut at the worst point where one cubic misses; repeated points are dropped. The result is a `kind: 'bezier'` spline of `3 * pieces + 1` control points.
|
|
100
|
+
|
|
101
|
+
## Bounds and intersections (`@margoengine/splines/query`)
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import {Spline} from '@margoengine/splines';
|
|
105
|
+
import {intersectSplines, splineBounds} from '@margoengine/splines/query';
|
|
106
|
+
const road = new Spline([[0, 0, 0], [1, 0.3, 0], [2, -0.2, 0], [3, 0.1, 0]]);
|
|
107
|
+
const rail = new Spline([[0, 0.2, 0], [1, -0.3, 0], [2, 0.3, 0], [3, -0.1, 0]]);
|
|
108
|
+
const crossings = intersectSplines(road, rail);
|
|
109
|
+
console.log(crossings.points.length, crossings.overlaps.length); // 3 0
|
|
110
|
+
console.log(splineBounds(road).max[1] > 0.3); // true: the analytic peak lies between samples
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`splineBounds` and `splineExtrema` solve each piece's derivative exactly. `intersectSplinePlane` and `intersectSplineRay` find roots per piece; `intersectSplines` subdivides piece pairs by their control boxes and polishes each hit with Newton steps. Identical or reversed pieces come back as `overlaps`, not as endless points. Each curve's pieces are rebuilt as exact cubics once per call (four `at` evaluations per piece for non-Bezier kinds), not once per pair. `maxWork` (default 200000) bounds the subdivision nodes.
|
|
114
|
+
|
|
115
|
+
Apache-2.0. Optional, tree-shakable package; core peer: ^0.1.0 || ^1.0.0.
|
|
116
|
+
|
|
117
|
+
Build and unit tests: npm test --workspace @margoengine/splines
|
|
118
|
+
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Spline } from './index.js';
|
|
2
|
+
/** Where a point lands on a spline: see {@link closestPointOnSpline}. */
|
|
3
|
+
export interface SplineClosestPoint {
|
|
4
|
+
/** Curve parameter, 0..1, the value `spline.at` takes. */
|
|
5
|
+
t: number;
|
|
6
|
+
/** Arc-length position in world units, the value `spline.atDistance` takes. */
|
|
7
|
+
arc: number;
|
|
8
|
+
/** The nearest point on the curve. */
|
|
9
|
+
point: Float64Array;
|
|
10
|
+
/** Distance from the query point to `point`. */
|
|
11
|
+
distance: number;
|
|
12
|
+
}
|
|
13
|
+
/** Options for {@link closestPointOnSpline}. */
|
|
14
|
+
export interface SplineClosestOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Evenly spaced samples that seed the exact search (a closer seed skips more pieces; the answer does not depend on
|
|
17
|
+
* it), an integer 2..1,000,000. Default: the spline's own `divisions`.
|
|
18
|
+
*/
|
|
19
|
+
samples?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Bisection steps that refine each minimum of the distance, an integer 0..200. Default 52 (to full double precision).
|
|
22
|
+
* 0 skips the exact search and returns the nearest sample.
|
|
23
|
+
*/
|
|
24
|
+
iterations?: number;
|
|
25
|
+
}
|
|
26
|
+
/** Arc-length position of curve parameter `t`, read from the spline's cached lookup (the inverse of `atDistance`). */
|
|
27
|
+
export declare function distanceAt(spline: Spline, t: number): number;
|
|
28
|
+
/**
|
|
29
|
+
* Nearest position on a spline to a 3D point, for snapping, rails, cameras on tracks and AI paths. Exact for every
|
|
30
|
+
* curve kind: each cubic piece whose control-point box could hold a closer point has the stationary points of its
|
|
31
|
+
* squared distance, the roots of a quintic, isolated by subdividing its Bernstein form (the variation-diminishing
|
|
32
|
+
* property bounds the roots by the sign changes of the coefficients) and refined by bisection; the piece ends are
|
|
33
|
+
* candidates too. `samples` only seeds the search, so close passes of a tight spiral need no extra samples. Ties go
|
|
34
|
+
* to the smaller parameter.
|
|
35
|
+
*/
|
|
36
|
+
export declare function closestPointOnSpline(spline: Spline, query: ArrayLike<number>, { samples, iterations }?: SplineClosestOptions, output?: SplineClosestPoint): SplineClosestPoint;
|
package/dist/closest.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { a as e, i as t, n } from "./polynomial-we3se7Ae.js";
|
|
2
|
+
//#region src/closest.ts
|
|
3
|
+
function r(e, t) {
|
|
4
|
+
if (!Number.isFinite(t) || t < 0 || t > 1) throw RangeError("Curve parameter must be 0..1");
|
|
5
|
+
let n = e.arcLengths, r = t * (n.length - 1), i = Math.min(n.length - 2, Math.floor(r));
|
|
6
|
+
return n[i] + (n[i + 1] - n[i]) * (r - i);
|
|
7
|
+
}
|
|
8
|
+
var i = (e, t, n, r, i, a) => {
|
|
9
|
+
e.at(t, n);
|
|
10
|
+
let o = n[0] - r, s = n[1] - i, c = n[2] - a;
|
|
11
|
+
return o * o + s * s + c * c;
|
|
12
|
+
}, a = [
|
|
13
|
+
1,
|
|
14
|
+
3,
|
|
15
|
+
3,
|
|
16
|
+
1
|
|
17
|
+
], o = [
|
|
18
|
+
1,
|
|
19
|
+
2,
|
|
20
|
+
1
|
|
21
|
+
], s = [
|
|
22
|
+
1,
|
|
23
|
+
5,
|
|
24
|
+
10,
|
|
25
|
+
10,
|
|
26
|
+
5,
|
|
27
|
+
1
|
|
28
|
+
];
|
|
29
|
+
function c(e, t, n, r, i) {
|
|
30
|
+
i.fill(0);
|
|
31
|
+
for (let c = 0; c < 4; c++) {
|
|
32
|
+
let l = e[c * 3] - t, u = e[c * 3 + 1] - n, d = e[c * 3 + 2] - r;
|
|
33
|
+
for (let t = 0; t < 3; t++) {
|
|
34
|
+
let n = t * 3, r = 3 * (l * (e[n + 3] - e[n]) + u * (e[n + 4] - e[n + 1]) + d * (e[n + 5] - e[n + 2]));
|
|
35
|
+
i[c + t] += a[c] * o[t] / s[c + t] * r;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
var l = (e, n, r, i, a, o, s) => (t(e, n, 0, o), t(e, n, 1, s), (o[0] - r) * s[0] + (o[1] - i) * s[1] + (o[2] - a) * s[2]), u = (e) => {
|
|
40
|
+
let t = 0, n = 0;
|
|
41
|
+
for (let r = 0; r < e.length; r++) e[r] !== 0 && (n && Math.sign(e[r]) !== n && t++, n = Math.sign(e[r]));
|
|
42
|
+
return t;
|
|
43
|
+
};
|
|
44
|
+
function d(t, a, { samples: o = t.arcLengths.length - 1, iterations: s = 52 } = {}, d = {
|
|
45
|
+
t: 0,
|
|
46
|
+
arc: 0,
|
|
47
|
+
point: /* @__PURE__ */ new Float64Array(3),
|
|
48
|
+
distance: 0
|
|
49
|
+
}) {
|
|
50
|
+
if (a.length !== 3 || !(Number.isFinite(a[0]) && Number.isFinite(a[1]) && Number.isFinite(a[2]))) throw TypeError("Query must be a finite 3D point");
|
|
51
|
+
if (!Number.isInteger(o) || o < 2 || o > 1e6) throw RangeError("samples must be an integer from 2 to 1,000,000");
|
|
52
|
+
if (!Number.isInteger(s) || s < 0 || s > 200) throw RangeError("iterations must be an integer from 0 to 200");
|
|
53
|
+
let f = a[0], p = a[1], m = a[2], h = /* @__PURE__ */ new Float64Array(3), g = 0, _ = Infinity;
|
|
54
|
+
for (let e = 0; e <= o; e++) {
|
|
55
|
+
let n = i(t, e / o, h, f, p, m);
|
|
56
|
+
n < _ && (_ = n, g = e / o);
|
|
57
|
+
}
|
|
58
|
+
if (s > 0) {
|
|
59
|
+
let r = e(t), o = /* @__PURE__ */ new Float64Array(3), d = /* @__PURE__ */ new Float64Array(3), v = [], y = (e, t, n, r, i) => {
|
|
60
|
+
let a = u(t);
|
|
61
|
+
if (!a) return;
|
|
62
|
+
if (a === 1 || i >= 48) {
|
|
63
|
+
let t = n, i = r;
|
|
64
|
+
if (!(l(e, t, f, p, m, o, d) < 0 && l(e, i, f, p, m, o, d) > 0)) {
|
|
65
|
+
a > 1 && v.push((n + r) / 2);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
for (let n = 0; n < s && t < i; n++) {
|
|
69
|
+
let n = (t + i) / 2;
|
|
70
|
+
l(e, n, f, p, m, o, d) < 0 ? t = n : i = n;
|
|
71
|
+
}
|
|
72
|
+
v.push((t + i) / 2);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
let c = /* @__PURE__ */ new Float64Array(6), h = /* @__PURE__ */ new Float64Array(6), g = Float64Array.from(t);
|
|
76
|
+
for (let e = 0; e < 6; e++) {
|
|
77
|
+
c[e] = g[0], h[5 - e] = g[5 - e];
|
|
78
|
+
for (let t = 0; t < 5 - e; t++) g[t] = (g[t] + g[t + 1]) / 2;
|
|
79
|
+
}
|
|
80
|
+
let _ = (n + r) / 2;
|
|
81
|
+
y(e, c, n, _, i + 1), y(e, h, _, r, i + 1);
|
|
82
|
+
}, b = /* @__PURE__ */ new Float64Array(6);
|
|
83
|
+
for (let e = 0; e < r; e++) {
|
|
84
|
+
let o = n(t, e), s = 0;
|
|
85
|
+
for (let e = 0; e < 3; e++) {
|
|
86
|
+
let t = a[e], n = Math.min(o[e], o[e + 3], o[e + 6], o[e + 9]), r = Math.max(o[e], o[e + 3], o[e + 6], o[e + 9]);
|
|
87
|
+
s += t < n ? (n - t) ** 2 : t > r ? (t - r) ** 2 : 0;
|
|
88
|
+
}
|
|
89
|
+
if (!(s > _)) {
|
|
90
|
+
v.length = 0, v.push(0), c(o, f, p, m, b), y(o, b, 0, 1, 0), v.push(1);
|
|
91
|
+
for (let n of v) {
|
|
92
|
+
let a = Math.min(1, (e + n) / r), o = i(t, a, h, f, p, m);
|
|
93
|
+
(o < _ || o === _ && a < g) && (_ = o, g = a);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return t.at(g, d.point), d.t = g, d.arc = r(t, g), d.distance = Math.sqrt(_), d;
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
export { d as closestPointOnSpline, r as distanceAt };
|
package/dist/fit.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Spline } from './index.js';
|
|
2
|
+
/** Options for {@link fitSpline}. */
|
|
3
|
+
export interface SplineFitOptions {
|
|
4
|
+
/** Largest distance allowed between any input point and the fitted curve, above 0, in the points' own units. */
|
|
5
|
+
tolerance: number;
|
|
6
|
+
/** Newton refinements of each point's curve parameter before a piece is split, an integer 0..20. Default 4. */
|
|
7
|
+
refinements?: number;
|
|
8
|
+
/** Samples of the arc-length lookup of the returned spline. Default 256. */
|
|
9
|
+
divisions?: number;
|
|
10
|
+
}
|
|
11
|
+
/** Result of {@link fitSpline}. */
|
|
12
|
+
export interface SplineFit {
|
|
13
|
+
/** The cubic Bezier control points, 3 numbers each: `3 * pieces + 1` points. */
|
|
14
|
+
points: Float64Array;
|
|
15
|
+
/** The fitted curve as a `kind: 'bezier'` spline. */
|
|
16
|
+
spline: Spline;
|
|
17
|
+
/** Number of cubic pieces. */
|
|
18
|
+
pieces: number;
|
|
19
|
+
/** Largest distance from an input point to the curve, measured at that point's own parameter. At most `tolerance`. */
|
|
20
|
+
maxError: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Turn a dense run of 3D points (a GPS track, a freehand stroke) into a few cubic Bezier pieces that stay within
|
|
24
|
+
* `tolerance` of every input point. Each piece is a least-squares cubic with tangents taken from the points, refined
|
|
25
|
+
* by Newton steps on each point's parameter; a piece that misses the tolerance is cut at its worst point and both
|
|
26
|
+
* halves are fitted again. Repeated points are dropped. Pieces join with matching tangents except at cuts forced by
|
|
27
|
+
* sharp corners, where the cut tangent is shared and the curve stays smooth.
|
|
28
|
+
*/
|
|
29
|
+
export declare function fitSpline(points: readonly ArrayLike<number>[], { tolerance, refinements, divisions }: SplineFitOptions): SplineFit;
|
package/dist/fit.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Spline as e } from "./index.js";
|
|
2
|
+
//#region src/fit.ts
|
|
3
|
+
var t = (e, t, n, r) => [
|
|
4
|
+
e[t] - n[r],
|
|
5
|
+
e[t + 1] - n[r + 1],
|
|
6
|
+
e[t + 2] - n[r + 2]
|
|
7
|
+
], n = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = (e) => {
|
|
8
|
+
let t = Math.hypot(e[0], e[1], e[2]);
|
|
9
|
+
return t > 0 ? [
|
|
10
|
+
e[0] / t,
|
|
11
|
+
e[1] / t,
|
|
12
|
+
e[2] / t
|
|
13
|
+
] : [
|
|
14
|
+
0,
|
|
15
|
+
0,
|
|
16
|
+
0
|
|
17
|
+
];
|
|
18
|
+
}, i = (e, t, n) => {
|
|
19
|
+
let r = 1 - t, i = n === 0 ? [
|
|
20
|
+
r * r * r,
|
|
21
|
+
3 * r * r * t,
|
|
22
|
+
3 * r * t * t,
|
|
23
|
+
t * t * t
|
|
24
|
+
] : n === 1 ? [
|
|
25
|
+
-3 * r * r,
|
|
26
|
+
3 * r * r - 6 * r * t,
|
|
27
|
+
6 * r * t - 3 * t * t,
|
|
28
|
+
3 * t * t
|
|
29
|
+
] : [
|
|
30
|
+
6 * r,
|
|
31
|
+
-12 * r + 6 * t,
|
|
32
|
+
6 * r - 12 * t,
|
|
33
|
+
6 * t
|
|
34
|
+
], a = [
|
|
35
|
+
0,
|
|
36
|
+
0,
|
|
37
|
+
0
|
|
38
|
+
];
|
|
39
|
+
for (let t = 0; t < 3; t++) a[t] = i[0] * e[t] + i[1] * e[3 + t] + i[2] * e[6 + t] + i[3] * e[9 + t];
|
|
40
|
+
return a;
|
|
41
|
+
};
|
|
42
|
+
function a(e, t, r, i, a, o) {
|
|
43
|
+
let s = r - t + 1, c = /* @__PURE__ */ new Float64Array(12), l = t * 3, u = r * 3;
|
|
44
|
+
for (let t = 0; t < 3; t++) c[t] = e[l + t], c[9 + t] = e[u + t];
|
|
45
|
+
let d = 0, f = 0, p = 0, m = 0, h = 0;
|
|
46
|
+
for (let r = 0; r < s; r++) {
|
|
47
|
+
let s = i[r], l = 1 - s, u = l * l * l, g = 3 * l * l * s, _ = 3 * l * s * s, v = s * s * s, y = [
|
|
48
|
+
a[0] * g,
|
|
49
|
+
a[1] * g,
|
|
50
|
+
a[2] * g
|
|
51
|
+
], b = [
|
|
52
|
+
o[0] * _,
|
|
53
|
+
o[1] * _,
|
|
54
|
+
o[2] * _
|
|
55
|
+
], x = (t + r) * 3;
|
|
56
|
+
d += n(y, y), f += n(y, b), p += n(b, b);
|
|
57
|
+
let S = [
|
|
58
|
+
0,
|
|
59
|
+
0,
|
|
60
|
+
0
|
|
61
|
+
];
|
|
62
|
+
for (let t = 0; t < 3; t++) S[t] = e[x + t] - (c[t] * (u + g) + c[9 + t] * (_ + v));
|
|
63
|
+
m += n(y, S), h += n(b, S);
|
|
64
|
+
}
|
|
65
|
+
let g = d * p - f * f, _ = Math.hypot(e[u] - e[l], e[u + 1] - e[l + 1], e[u + 2] - e[l + 2]), v = g === 0 ? 0 : (m * p - h * f) / g, y = g === 0 ? 0 : (d * h - f * m) / g;
|
|
66
|
+
(!(v > 1e-6 * _) || !(y > 1e-6 * _)) && (v = y = _ / 3);
|
|
67
|
+
for (let e = 0; e < 3; e++) c[3 + e] = c[e] + a[e] * v, c[6 + e] = c[9 + e] + o[e] * y;
|
|
68
|
+
return c;
|
|
69
|
+
}
|
|
70
|
+
function o(e, t, n, r, a) {
|
|
71
|
+
let o = 0, s = Math.floor((t + n) / 2);
|
|
72
|
+
for (let c = t + 1; c < n; c++) {
|
|
73
|
+
let n = i(r, a[c - t], 0), l = Math.hypot(n[0] - e[c * 3], n[1] - e[c * 3 + 1], n[2] - e[c * 3 + 2]);
|
|
74
|
+
l > o && (o = l, s = c);
|
|
75
|
+
}
|
|
76
|
+
return [o, s];
|
|
77
|
+
}
|
|
78
|
+
function s(e, t, r, a, o) {
|
|
79
|
+
for (let s = t + 1; s < r; s++) {
|
|
80
|
+
let r = o[s - t], c = i(a, r, 0), l = i(a, r, 1), u = i(a, r, 2), d = [
|
|
81
|
+
c[0] - e[s * 3],
|
|
82
|
+
c[1] - e[s * 3 + 1],
|
|
83
|
+
c[2] - e[s * 3 + 2]
|
|
84
|
+
], f = n(d, l), p = n(l, l) + n(d, u);
|
|
85
|
+
Math.abs(p) > 1e-12 && (o[s - t] = Math.min(1, Math.max(0, r - f / p)));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function c(n, { tolerance: i, refinements: c = 4, divisions: l = 256 }) {
|
|
89
|
+
if (!(Number.isFinite(i) && i > 0)) throw RangeError("tolerance must be above 0");
|
|
90
|
+
if (!Number.isInteger(c) || c < 0 || c > 20) throw RangeError("refinements must be an integer from 0 to 20");
|
|
91
|
+
if (n.length < 2 || n.length > 1e7) throw RangeError("Give 2 to 10,000,000 points");
|
|
92
|
+
let u = [];
|
|
93
|
+
for (let e of n) {
|
|
94
|
+
if (e.length !== 3 || !(Number.isFinite(e[0]) && Number.isFinite(e[1]) && Number.isFinite(e[2]))) throw TypeError("Every point must be a finite 3D point");
|
|
95
|
+
let t = u.length;
|
|
96
|
+
(!t || Math.hypot(e[0] - u[t - 3], e[1] - u[t - 2], e[2] - u[t - 1]) > 1e-12) && u.push(e[0], e[1], e[2]);
|
|
97
|
+
}
|
|
98
|
+
let d = Float64Array.from(u), f = d.length / 3;
|
|
99
|
+
if (f < 2) throw RangeError("Need at least two distinct points");
|
|
100
|
+
let p = [...d.subarray(0, 3)], m = 0, h = (e, n) => {
|
|
101
|
+
let i = e + n, a = e + 2 * n;
|
|
102
|
+
if (a < 0 || a >= f) return r(t(d, i * 3, d, e * 3));
|
|
103
|
+
let o = Math.hypot(...t(d, i * 3, d, e * 3)), s = o + Math.hypot(...t(d, a * 3, d, i * 3)), c = -(o + s) / (o * s), l = s / (o * (s - o)), u = -o / (s * (s - o)), p = [
|
|
104
|
+
0,
|
|
105
|
+
0,
|
|
106
|
+
0
|
|
107
|
+
];
|
|
108
|
+
for (let t = 0; t < 3; t++) p[t] = c * d[e * 3 + t] + l * d[i * 3 + t] + u * d[a * 3 + t];
|
|
109
|
+
return r(p);
|
|
110
|
+
}, g = [[
|
|
111
|
+
0,
|
|
112
|
+
f - 1,
|
|
113
|
+
h(0, 1),
|
|
114
|
+
h(f - 1, -1)
|
|
115
|
+
]];
|
|
116
|
+
for (; g.length;) {
|
|
117
|
+
let [e, n, l, u] = g.pop(), f;
|
|
118
|
+
if (n - e === 1) {
|
|
119
|
+
let r = Math.hypot(...t(d, n * 3, d, e * 3)) / 3;
|
|
120
|
+
f = /* @__PURE__ */ new Float64Array(12);
|
|
121
|
+
for (let t = 0; t < 3; t++) f[t] = d[e * 3 + t], f[3 + t] = f[t] + l[t] * r, f[9 + t] = d[n * 3 + t], f[6 + t] = f[9 + t] + u[t] * r;
|
|
122
|
+
_(f, 0);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
let p = n - e + 1, m = new Float64Array(p);
|
|
126
|
+
for (let n = 1; n < p; n++) m[n] = m[n - 1] + Math.hypot(...t(d, (e + n) * 3, d, (e + n - 1) * 3));
|
|
127
|
+
let h = m[p - 1];
|
|
128
|
+
for (let e = 1; e < p; e++) m[e] /= h;
|
|
129
|
+
f = a(d, e, n, m, l, u);
|
|
130
|
+
let [v, y] = o(d, e, n, f, m);
|
|
131
|
+
for (let t = 0; t < c && v > i; t++) s(d, e, n, f, m), f = a(d, e, n, m, l, u), [v, y] = o(d, e, n, f, m);
|
|
132
|
+
if (v <= i) {
|
|
133
|
+
_(f, v);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
y = Math.min(n - 1, Math.max(e + 1, y));
|
|
137
|
+
let b = r(t(d, (y - 1) * 3, d, (y + 1) * 3));
|
|
138
|
+
g.push([
|
|
139
|
+
y,
|
|
140
|
+
n,
|
|
141
|
+
[
|
|
142
|
+
-b[0],
|
|
143
|
+
-b[1],
|
|
144
|
+
-b[2]
|
|
145
|
+
],
|
|
146
|
+
u
|
|
147
|
+
], [
|
|
148
|
+
e,
|
|
149
|
+
y,
|
|
150
|
+
l,
|
|
151
|
+
b
|
|
152
|
+
]);
|
|
153
|
+
}
|
|
154
|
+
function _(e, t) {
|
|
155
|
+
for (let t = 3; t < 12; t++) p.push(e[t]);
|
|
156
|
+
t > m && (m = t);
|
|
157
|
+
}
|
|
158
|
+
let v = Float64Array.from(p), y = (p.length / 3 - 1) / 3, b = [];
|
|
159
|
+
for (let e = 0; e < v.length; e += 3) b.push([
|
|
160
|
+
v[e],
|
|
161
|
+
v[e + 1],
|
|
162
|
+
v[e + 2]
|
|
163
|
+
]);
|
|
164
|
+
return {
|
|
165
|
+
points: v,
|
|
166
|
+
spline: new e(b, {
|
|
167
|
+
kind: "bezier",
|
|
168
|
+
divisions: l
|
|
169
|
+
}),
|
|
170
|
+
pieces: y,
|
|
171
|
+
maxError: m
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
//#endregion
|
|
175
|
+
export { c as fitSpline };
|