/
nurbs.py
412 lines (329 loc) · 11 KB
/
nurbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from math import sqrt
from compas.plugins import pluggable
from compas.plugins import PluginNotInstalledError
from compas.geometry import Point
from compas.geometry import Frame
from .curve import Curve
@pluggable(category="factories")
def new_nurbscurve(cls, *args, **kwargs):
curve = object.__new__(NurbsCurve)
curve.__init__(*args, **kwargs)
return curve
@pluggable(category="factories")
def new_nurbscurve_from_native(cls, *args, **kwargs):
raise PluginNotInstalledError
@pluggable(category="factories")
def new_nurbscurve_from_parameters(cls, *args, **kwargs):
raise PluginNotInstalledError
@pluggable(category="factories")
def new_nurbscurve_from_points(cls, *args, **kwargs):
raise PluginNotInstalledError
@pluggable(category="factories")
def new_nurbscurve_from_interpolation(cls, *args, **kwargs):
raise PluginNotInstalledError
@pluggable(category="factories")
def new_nurbscurve_from_step(cls, *args, **kwargs):
raise PluginNotInstalledError
class NurbsCurve(Curve):
"""A NURBS curve is defined by control points, weights, knots, and a degree.
Parameters
----------
name : str, optional
The name of the curve.
Attributes
----------
points : list[:class:`compas.geometry.Point`], read-only
The control points.
weights : list[float], read-only
The weights of the control points.
knots : list[float], read-only
The knots, without multiplicity.
knotsequence : list[float], read-only
The complete knot vector.
multiplicity : list[int], read-only
The multiplicities of the knots.
continuity : int, read-only
The degree of continuity of the curve.
degree : int, read-only
The degree of the curve.
order : int, read-only
The order of the curve (degree + 1).
"""
def __new__(cls, *args, **kwargs):
return new_nurbscurve(cls, *args, **kwargs)
DATASCHEMA = {
"type": "object",
"properties": {
"points": {"type": "array", "minItems": 2, "items": Point.DATASCHEMA},
"weights": {"type": "array", "items": {"type": "number"}},
"knots": {"type": "array", "items": {"type": "number"}},
"multiplicities": {"type": "array", "items": {"type": "integer"}},
"degree": {"type": "integer", "exclusiveMinimum": 0},
"is_periodic": {"type": "boolean"},
},
"additionalProperties": False,
"minProperties": 6,
}
@property
def __dtype__(self):
return "compas.geometry/NurbsCurve"
@property
def __data__(self):
return {
"points": [point.__data__ for point in self.points],
"weights": self.weights,
"knots": self.knots,
"multiplicities": self.multiplicities,
"degree": self.degree,
"is_periodic": self.is_periodic,
}
@classmethod
def __from_data__(cls, data):
return cls.from_parameters(
data["points"],
data["weights"],
data["knots"],
data["multiplicities"],
data["degree"],
data["is_periodic"],
)
def __init__(self, name=None):
super(NurbsCurve, self).__init__(name=name)
def __repr__(self):
return "{0}(points={1!r}, weigths={2}, knots={3}, multiplicities={4}, degree={5}, is_periodic={6})".format(
type(self).__name__,
self.points,
self.weights,
self.knots,
self.multiplicities,
self.degree,
self.is_periodic,
)
# ==============================================================================
# Properties
# ==============================================================================
@property
def points(self):
raise NotImplementedError
@property
def weights(self):
raise NotImplementedError
@property
def knots(self):
raise NotImplementedError
@property
def multiplicities(self):
raise NotImplementedError
@property
def knotvector(self):
raise NotImplementedError
@property
def continuity(self):
raise NotImplementedError
@property
def degree(self):
raise NotImplementedError
@property
def order(self):
return self.degree + 1
@property
def is_rational(self):
raise NotImplementedError
# ==============================================================================
# Constructors
# ==============================================================================
@classmethod
def from_native(cls, curve):
"""Construct a NURBS curve from a curve object.
Parameters
----------
curve : :class:`Rhino.Geometry.NurbsCurve`
A *Rhino* curve object.
Returns
-------
:class:`compas.geometry.NurbsCurve`
A COMPAS NURBS curve.
"""
return new_nurbscurve_from_native(cls, curve)
@classmethod
def from_step(cls, filepath):
"""Load a NURBS curve from an STP file.
Parameters
----------
filepath : str
The path to the file.
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return new_nurbscurve_from_step(cls, filepath)
@classmethod
def from_parameters(cls, points, weights, knots, multiplicities, degree, is_periodic=False):
"""Construct a NURBS curve from explicit curve parameters.
Parameters
----------
points : list[[float, float, float] | :class:`compas.geometry.Point`]
The control points.
weights : list[float]
The weights of the control points.
knots : list[float]
The curve knots, without multiplicity.
multiplicities : list[int]
Multiplicity of the knots.
degree : int
Degree of the curve.
is_periodic : bool, optional
Flag indicating that the curve is periodic.
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return new_nurbscurve_from_parameters(cls, points, weights, knots, multiplicities, degree, is_periodic=False)
@classmethod
def from_points(cls, points, degree=3):
"""Construct a NURBS curve from control points.
Parameters
----------
points : list[[float, float, float] | :class:`compas.geometry.Point`]
The control points.
degree : int, optional
The degree of the curve.
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return new_nurbscurve_from_points(cls, points, degree=degree)
@classmethod
def from_interpolation(cls, points, precision=1e-3):
"""Construct a NURBS curve by interpolating a set of points.
Parameters
----------
points : list[[float, float, float] | :class:`compas.geometry.Point`]
A list of interpolation points.
precision : int, optional
The desired precision of the interpolation.
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return new_nurbscurve_from_interpolation(cls, points, precision=1e-3)
@classmethod
def from_arc(cls, arc):
"""Construct a NURBS curve from an arc.
Parameters
----------
arc : :class:`compas.geometry.Arc`
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
raise NotImplementedError
@classmethod
def from_circle(cls, circle):
"""Construct a NURBS curve from a circle.
Parameters
----------
circle : :class:`compas.geometry.Circle`
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
frame = Frame.from_plane(circle.plane)
w = 0.5 * sqrt(2)
dx = frame.xaxis * circle.radius
dy = frame.yaxis * circle.radius
points = [
frame.point - dy,
frame.point - dy - dx,
frame.point - dx,
frame.point + dy - dx,
frame.point + dy,
frame.point + dy + dx,
frame.point + dx,
frame.point - dy + dx,
frame.point - dy,
]
knots = [0, 1 / 4, 1 / 2, 3 / 4, 1]
mults = [3, 2, 2, 2, 3]
weights = [1, w, 1, w, 1, w, 1, w, 1]
return cls.from_parameters(points=points, weights=weights, knots=knots, multiplicities=mults, degree=2)
@classmethod
def from_ellipse(cls, ellipse):
"""Construct a NURBS curve from an ellipse.
Parameters
----------
ellipse : :class:`compas.geometry.Ellipse`
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
frame = Frame.from_plane(ellipse.plane)
frame = Frame.worldXY()
w = 0.5 * sqrt(2)
dx = frame.xaxis * ellipse.major
dy = frame.yaxis * ellipse.minor
points = [
frame.point - dy,
frame.point - dy - dx,
frame.point - dx,
frame.point + dy - dx,
frame.point + dy,
frame.point + dy + dx,
frame.point + dx,
frame.point - dy + dx,
frame.point - dy,
]
knots = [0, 1 / 4, 1 / 2, 3 / 4, 1]
mults = [3, 2, 2, 2, 3]
weights = [1, w, 1, w, 1, w, 1, w, 1]
return cls.from_parameters(points=points, weights=weights, knots=knots, multiplicities=mults, degree=2)
@classmethod
def from_line(cls, line):
"""Construct a NURBS curve from a line.
Parameters
----------
line : :class:`compas.geometry.Line`
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return cls.from_parameters(
points=[line.start, line.end],
weights=[1.0, 1.0],
knots=[0.0, 1.0],
multiplicities=[2, 2],
degree=1,
)
# ==============================================================================
# Conversions
# ==============================================================================
# ==============================================================================
# Methods
# ==============================================================================
def copy(self):
"""Make an independent copy of the current curve.
Returns
-------
:class:`compas.geometry.NurbsCurve`
"""
return NurbsCurve.from_parameters(
self.points,
self.weights,
self.knots,
self.multiplicities,
self.degree,
self.is_periodic,
)
def insert_knot(self):
pass
def refine_knot(self):
pass
def remove_knot(self):
pass
def elevate_degree(self):
pass
def reduce_degree(self):
pass