-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspline.go
More file actions
282 lines (255 loc) · 9.87 KB
/
Copy pathspline.go
File metadata and controls
282 lines (255 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package sketch
import (
"fmt"
"github.com/lestrrat-3d/sketch/geom"
)
// Spline is an open clamped cubic B-spline whose control points are ordinary
// sketch points. The solver reshapes the curve by moving control points — every
// point-based constraint, dimension and goal applies to them directly; the
// spline itself carries no extra unknowns and no internal constraints. The curve
// starts at the first control point and ends at the last (clamped), with end
// tangents along the first and last control-polygon legs.
type Spline struct {
s *Sketch
Control []*Point
id int
construction bool
named // optional label
refState // reference splines are a follow-up; stale derived from control points
}
func (sp *Spline) entity() {}
func (sp *Spline) isNil() bool { return sp == nil }
func (sp *Spline) entID() int { return sp.id }
func (sp *Spline) IsConstruction() bool { return sp.construction }
func (sp *Spline) SetConstruction(v bool) {
if !sp.reference {
sp.construction = v
}
}
// IsStale reports whether any control point is stale (derived; reference splines
// are not yet authorable).
func (sp *Spline) IsStale() bool {
for _, p := range sp.Control {
if p.IsStale() {
return true
}
}
return false
}
// Geometry returns a fresh [geom.Spline] snapshot over the spline's current
// control-point coordinates.
func (sp *Spline) Geometry() *geom.Spline {
ctrl := make([]*geom.Point, len(sp.Control))
for i, p := range sp.Control {
ctrl[i] = p.Geometry()
}
// The control points are already validated (CreateSpline requires >= 4), so
// build the snapshot directly rather than re-validating through NewSpline.
return &geom.Spline{Control: ctrl}
}
// Eval returns the curve point at parameter t ∈ [0, 1] (clamped), using the
// solved control-point coordinates.
func (sp *Spline) Eval(t float64) (float64, float64) {
// control-point count is guaranteed >=4 by the Spline constructor (CreateSpline).
x, y, _ := geom.EvalCubicBSpline(sp.controlCoords(), t)
return x, y
}
// Polyline samples the solved curve at segments+1 evenly spaced parameters.
func (sp *Spline) Polyline(segments int) [][2]float64 {
pts, _ := geom.SampleCubicBSpline(sp.controlCoords(), segments)
return pts
}
func (sp *Spline) controlCoords() [][2]float64 {
pts := make([][2]float64, len(sp.Control))
for i, p := range sp.Control {
pts[i] = [2]float64{p.x(), p.y()}
}
return pts
}
// CreateSpline adds a cubic B-spline over the given control points and returns its
// handle. Share control points with other geometry to relate them. It returns
// [ErrInvalidShape] with fewer than 4 control points.
func (s *Sketch) CreateSpline(control ...*Point) (*Spline, error) {
if len(control) < 4 {
return nil, fmt.Errorf("%w: CreateSpline requires at least 4 control points, got %d", ErrInvalidShape, len(control))
}
for i, p := range control {
if p == nil {
return nil, fmt.Errorf("%w: CreateSpline control point %d is nil", ErrInvalidShape, i)
}
}
sp := &Spline{s: s, Control: append([]*Point(nil), control...), id: len(s.ents)}
s.addEntity(sp)
return sp, nil
}
// ClosedSpline is a closed (periodic) uniform cubic B-spline whose control points
// are ordinary sketch points. It is a smooth closed loop (C2 across the seam) and
// bounds a region on its own, so — unlike the open [Spline] — it has no
// endpoints. The solver reshapes it by moving control points; it carries no extra
// unknowns and no internal constraints.
type ClosedSpline struct {
s *Sketch
Control []*Point
id int
construction bool
named // optional label
refState // reference closed splines are a follow-up; stale derived from control points
}
func (sp *ClosedSpline) entity() {}
func (sp *ClosedSpline) isNil() bool { return sp == nil }
func (sp *ClosedSpline) entID() int { return sp.id }
func (sp *ClosedSpline) IsConstruction() bool { return sp.construction }
func (sp *ClosedSpline) SetConstruction(v bool) {
if !sp.reference {
sp.construction = v
}
}
// IsStale reports whether any control point is stale (derived; reference closed
// splines are not yet authorable).
func (sp *ClosedSpline) IsStale() bool {
for _, p := range sp.Control {
if p.IsStale() {
return true
}
}
return false
}
// Geometry returns a fresh [geom.ClosedSpline] snapshot over the closed spline's
// current control-point coordinates.
func (sp *ClosedSpline) Geometry() *geom.ClosedSpline {
ctrl := make([]*geom.Point, len(sp.Control))
for i, p := range sp.Control {
ctrl[i] = p.Geometry()
}
return &geom.ClosedSpline{Control: ctrl}
}
// Eval returns the curve point at parameter t (reduced modulo 1 into the periodic
// domain), using the solved control-point coordinates. A parameter the reduction
// cannot place — a NaN, or an infinity, whose reduction is a NaN — evaluates to
// NaN.
func (sp *ClosedSpline) Eval(t float64) (float64, float64) {
x, y, _ := geom.EvalPeriodicCubicBSpline(sp.controlCoords(), t)
return x, y
}
// Polyline samples the solved closed curve at segments+1 evenly spaced
// parameters; the last point equals the first, closing the ring.
func (sp *ClosedSpline) Polyline(segments int) [][2]float64 {
pts, _ := geom.SamplePeriodicCubicBSpline(sp.controlCoords(), segments)
return pts
}
func (sp *ClosedSpline) controlCoords() [][2]float64 {
pts := make([][2]float64, len(sp.Control))
for i, p := range sp.Control {
pts[i] = [2]float64{p.x(), p.y()}
}
return pts
}
// CreateClosedSpline adds a closed (periodic) cubic B-spline over the given control
// points and returns its handle. Share control points with other geometry to
// relate them. It returns [ErrInvalidShape] with fewer than 3 control points or a
// nil control point.
func (s *Sketch) CreateClosedSpline(control ...*Point) (*ClosedSpline, error) {
if len(control) < 3 {
return nil, fmt.Errorf("%w: CreateClosedSpline requires at least 3 control points, got %d", ErrInvalidShape, len(control))
}
for i, p := range control {
if p == nil {
return nil, fmt.Errorf("%w: CreateClosedSpline control point %d is nil", ErrInvalidShape, i)
}
}
sp := &ClosedSpline{s: s, Control: append([]*Point(nil), control...), id: len(s.ents)}
s.addEntity(sp)
return sp, nil
}
// FitSpline is an open spline that INTERPOLATES its fit points: the curve passes
// through every fit point. The fit points are ordinary sketch points and the
// durable handles — the interpolating natural cubic is recomputed from their
// current coordinates on every evaluation, so the curve keeps passing through them
// as the solver moves them. It carries no extra unknowns and no internal
// constraints.
type FitSpline struct {
s *Sketch
Fit []*Point
id int
construction bool
named // optional label
refState // reference fit splines are a follow-up; stale derived from fit points
}
func (sp *FitSpline) entity() {}
func (sp *FitSpline) isNil() bool { return sp == nil }
func (sp *FitSpline) entID() int { return sp.id }
func (sp *FitSpline) IsConstruction() bool { return sp.construction }
func (sp *FitSpline) SetConstruction(v bool) {
if !sp.reference {
sp.construction = v
}
}
// IsStale reports whether any fit point is stale (derived; reference fit splines
// are not yet authorable).
func (sp *FitSpline) IsStale() bool {
for _, p := range sp.Fit {
if p.IsStale() {
return true
}
}
return false
}
// Geometry returns a fresh [geom.FitSpline] snapshot over the fit spline's current
// fit-point coordinates.
func (sp *FitSpline) Geometry() *geom.FitSpline {
fit := make([]*geom.Point, len(sp.Fit))
for i, p := range sp.Fit {
fit[i] = p.Geometry()
}
return &geom.FitSpline{Fit: fit}
}
// Eval returns the interpolated curve point at parameter t ∈ [0, 1] (normalized
// chord length), using the solved fit-point coordinates.
func (sp *FitSpline) Eval(t float64) (float64, float64) {
x, y, _ := geom.EvalFitSpline(sp.fitCoords(), t)
return x, y
}
// Interpolant returns the natural-cubic interpolant [FitSpline.Eval] evaluates at
// the solved fit-point coordinates — the curve's defining data rather than points
// on it, for a consumer that has to integrate or record the exact curve. It is a
// snapshot like [FitSpline.Geometry]: it does not follow later solves.
//
// It returns [geom.ErrNonFiniteFitInterpolant] when the solved coordinates — finite
// though they are — put the cumulative chord length or a span coefficient beyond
// floating point, since the interpolant would then describe less than the curve
// [FitSpline.Eval] evaluates.
func (sp *FitSpline) Interpolant() (*geom.FitInterpolant, error) {
// fit-point count is guaranteed >=2 by CreateFitSpline, so ErrTooFewFitPoints
// is unreachable here.
return geom.NewFitInterpolant(sp.fitCoords())
}
// Polyline samples the solved interpolating curve at segments+1 evenly spaced
// (in chord length) parameters.
func (sp *FitSpline) Polyline(segments int) [][2]float64 {
pts, _ := geom.SampleFitSpline(sp.fitCoords(), segments)
return pts
}
func (sp *FitSpline) fitCoords() [][2]float64 {
pts := make([][2]float64, len(sp.Fit))
for i, p := range sp.Fit {
pts[i] = [2]float64{p.x(), p.y()}
}
return pts
}
// CreateFitSpline adds a fit-point (interpolating) cubic spline through the given fit
// points and returns its handle. The curve passes through every fit point; share
// fit points with other geometry to relate them. It returns [ErrInvalidShape] with
// fewer than 2 fit points or a nil fit point.
func (s *Sketch) CreateFitSpline(fit ...*Point) (*FitSpline, error) {
if len(fit) < 2 {
return nil, fmt.Errorf("%w: CreateFitSpline requires at least 2 fit points, got %d", ErrInvalidShape, len(fit))
}
for i, p := range fit {
if p == nil {
return nil, fmt.Errorf("%w: CreateFitSpline fit point %d is nil", ErrInvalidShape, i)
}
}
sp := &FitSpline{s: s, Fit: append([]*Point(nil), fit...), id: len(s.ents)}
s.addEntity(sp)
return sp, nil
}