/
nurbs.py
299 lines (245 loc) · 9.69 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
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from itertools import groupby
import Rhino.Geometry # type: ignore
from compas.geometry import Point
from compas.geometry import NurbsCurve
from compas_rhino.conversions import point_to_rhino
from compas_rhino.conversions import point_to_compas
from compas_rhino.conversions import line_to_rhino
from .curve import RhinoCurve
def rhino_curve_from_parameters(points, weights, knots, multiplicities, degree):
rhino_curve = Rhino.Geometry.NurbsCurve(3, True, degree + 1, len(points))
for index, (point, weight) in enumerate(zip(points, weights)):
rhino_curve.Points.SetPoint(index, point_to_rhino(point), weight)
knotvector = [knot for knot, mult in zip(knots, multiplicities) for _ in range(mult)]
# account for superfluous knots
# https://developer.rhino3d.com/guides/opennurbs/superfluous-knots/
p = len(points)
o = degree + 1
k = p + o
if len(knotvector) == k:
knotvector[:] = knotvector[1:-1]
for index, knot in enumerate(knotvector):
rhino_curve.Knots[index] = knot
return rhino_curve
class RhinoNurbsCurve(NurbsCurve, RhinoCurve):
"""Class representing a NURBS curve based on the NurbsCurve of Rhino.Geometry.
Parameters
----------
name : str, optional
Name of the curve.
Attributes
----------
points : list[:class:`compas.geometry.Point`], read-only
The control points of the curve.
weights : list[float], read-only
The weights of the control points.
knots : list[float], read-only
The knot vector, without duplicates.
multiplicities : list[int], read-only
The multiplicities of the knots in the knot vector.
knotsequence : list[float], read-only
The knot vector, with repeating values according to the multiplicities.
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).
is_rational : bool, read-only
True is the curve is rational.
References
----------
* https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_NurbsCurve.htm
* https://en.wikipedia.org/wiki/Non-uniform_rational_B-spline
* https://developer.rhino3d.com/guides/opennurbs/nurbs-geometry-overview/
"""
# def __init__(self, name=None):
# super(RhinoNurbsCurve, self).__init__(name=name)
# self.rhino_curve = None
# ==============================================================================
# Data
# ==============================================================================
@property
def __data__(self):
# add superfluous knots
# for compatibility with all/most other NURBS implementations
# https://developer.rhino3d.com/guides/opennurbs/superfluous-knots/
multiplicities = self.multiplicities[:] # type: ignore
multiplicities[0] += 1
multiplicities[-1] += 1
return {
"points": [point.__data__ for point in self.points], # type: ignore
"weights": self.weights,
"knots": self.knots,
"multiplicities": multiplicities,
"degree": self.degree,
"is_periodic": self.is_periodic,
}
@classmethod
def __from_data__(cls, data):
points = [Point.__from_data__(point) for point in data["points"]]
weights = data["weights"]
knots = data["knots"]
multiplicities = data["multiplicities"]
degree = data["degree"]
# is_periodic = data['is_periodic']
# have not found a way to actually set this
# not sure if that is actually possible...
curve = cls()
curve.rhino_curve = rhino_curve_from_parameters(points, weights, knots, multiplicities, degree)
return curve
# ==============================================================================
# Rhino Properties
# ==============================================================================
# ==============================================================================
# Properties
# ==============================================================================
@property
def points(self):
if self.rhino_curve:
return [point_to_compas(point.Location) for point in self.rhino_curve.Points]
@property
def weights(self):
if self.rhino_curve:
return [point.Weight for point in self.rhino_curve.Points]
@property
def knots(self):
if self.rhino_curve:
return [key for key, _ in groupby(self.rhino_curve.Knots)]
@property
def knotsequence(self):
if self.rhino_curve:
return list(self.rhino_curve.Knots)
@property
def multiplicities(self):
if self.rhino_curve:
return [len(list(group)) for _, group in groupby(self.rhino_curve.Knots)]
@property
def degree(self):
if self.rhino_curve:
return self.rhino_curve.Degree
@property
def order(self):
if self.rhino_curve:
return self.rhino_curve.Order
@property
def is_rational(self):
if self.rhino_curve:
return self.rhino_curve.IsRational
# ==============================================================================
# Constructors
# ==============================================================================
@classmethod
def from_parameters(cls, points, weights, knots, multiplicities, degree, is_periodic=False):
"""Construct a NURBS curve from explicit curve parameters.
Parameters
----------
points : list[:class:`compas.geometry.Point`]
The control points.
weights : list[float]
The control point weights.
knots : list[float]
The curve knots, without duplicates.
multiplicities : list[int]
The multiplicities of the knots.
degree : int
The degree of the curve.
is_periodic : bool, optional
Flag indicating whether the curve is periodic or not.
Note that this parameters is currently not supported.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsCurve`
"""
curve = cls()
degree = min(degree, len(points) - 1)
curve.rhino_curve = rhino_curve_from_parameters(points, weights, knots, multiplicities, degree)
return curve
@classmethod
def from_points(cls, points, degree=3, is_periodic=False):
"""Construct a NURBS curve from control points.
Parameters
----------
points : list[:class:`compas.geometry.Point`]
The control points.
degree : int, optional
The degree of the curve.
is_periodic : bool, optional
Flag indicating whether the curve is periodic or not.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsCurve`
"""
curve = cls()
degree = min(degree, len(points) - 1)
rhino_curve = Rhino.Geometry.NurbsCurve.Create(is_periodic, degree, [point_to_rhino(point) for point in points])
curve.rhino_curve = rhino_curve
return curve
@classmethod
def from_interpolation(cls, points, precision=1e-3):
"""Construct a NURBS curve by interpolating a set of points.
Parameters
----------
points : list[:class:`compas.geometry.Point`]
The control points.
precision : float, optional
The required precision of the interpolation.
This parameter is currently not supported.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsCurve`
"""
curve = cls()
curve.rhino_curve = Rhino.Geometry.NurbsCurve.CreateHSpline([point_to_rhino(point) for point in points])
return curve
# @classmethod
# def from_circle(cls, circle):
# """Construct a NURBS curve from a circle.
# Parameters
# ----------
# circle : :class:`compas.geometry.Circle`
# A circle geometry.
# Returns
# -------
# :class:`compas_rhino.geometry.RhinoNurbsCurve`
# """
# curve = cls()
# curve.rhino_curve = Rhino.Geometry.NurbsCurve.CreateFromCircle(circle_to_rhino(circle))
# return curve
# @classmethod
# def from_ellipse(cls, ellipse):
# """Construct a NURBS curve from an ellipse.
# Parameters
# ----------
# ellipse : :class:`compas.geometry.Ellipse`
# An ellipse geometry.
# Returns
# -------
# :class:`compas_rhino.geometry.RhinoNurbsCurve`
# """
# curve = cls()
# curve.rhino_curve = Rhino.Geometry.NurbsCurve.CreateFromEllipse(ellipse_to_rhino(ellipse))
# return curve
@classmethod
def from_line(cls, line):
"""Construct a NURBS curve from a line.
Parameters
----------
line : :class:`compas.geometry.Line`
A line geometry.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsCurve`
"""
curve = cls()
curve.rhino_curve = Rhino.Geometry.NurbsCurve.CreateFromLine(line_to_rhino(line))
return curve
# ==============================================================================
# Conversions
# ==============================================================================
# ==============================================================================
# Methods
# ==============================================================================