/
surface.py
407 lines (320 loc) · 12.4 KB
/
surface.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import Rhino.Geometry # type: ignore
from compas.geometry import Surface
from compas_rhino.conversions import box_to_compas
from compas_rhino.conversions import cylinder_to_rhino
from compas_rhino.conversions import frame_to_rhino_plane
from compas_rhino.conversions import plane_to_compas_frame
from compas_rhino.conversions import plane_to_rhino
from compas_rhino.conversions import point_to_compas
from compas_rhino.conversions import point_to_rhino
from compas_rhino.conversions import sphere_to_rhino
from compas_rhino.conversions import transformation_to_rhino
from compas_rhino.conversions import vector_to_compas
from compas_rhino.geometry.curves import RhinoCurve
class RhinoSurface(Surface):
"""Class representing a general surface object.
Attributes
----------
domain_u: tuple[float, float]
The parameter domain in the U direction.
domain_v: tuple[float, float]
The parameter domain in the V direction.
is_periodic_u: bool
True if the surface is periodic in the U direction.
is_periodic_v: bool
True if the surface is periodic in the V direction.
"""
def __init__(self, name=None):
super(RhinoSurface, self).__init__(name=name)
self._rhino_surface = None
@property
def rhino_surface(self):
return self._rhino_surface
@rhino_surface.setter
def rhino_surface(self, surface):
self._rhino_surface = surface
# ==============================================================================
# Data
# ==============================================================================
# ==============================================================================
# Properties
# ==============================================================================
@property
def domain_u(self):
if self.rhino_surface:
return self.rhino_surface.Domain(0)
@property
def domain_v(self):
if self.rhino_surface:
return self.rhino_surface.Domain(1)
@property
def is_periodic_u(self):
if self.rhino_surface:
return self.rhino_surface.IsPeriodic(0)
@property
def is_periodic_v(self):
if self.rhino_surface:
return self.rhino_surface.IsPeriodic(1)
# ==============================================================================
# Constructors
# ==============================================================================
@classmethod
def from_corners(cls, corners):
"""Creates a NURBS surface using the given 4 corners.
The order of the given points determins the normal direction of the generated surface.
Parameters
----------
corners : list(:class:`compas.geometry.Point`)
4 points in 3d space to represent the corners of the planar surface.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsSurface`
"""
rhino_points = [Rhino.Geometry.Point3d(corner.x, corner.y, corner.z) for corner in corners]
return cls.from_rhino(Rhino.Geometry.NurbsSurface.CreateFromCorners(*rhino_points))
@classmethod
def from_sphere(cls, sphere):
"""Creates a NURBS surface from a sphere.
Parameters
----------
sphere : :class:`compas.geometry.Sphere`
The surface's geometry.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsSurface`
"""
sphere = sphere_to_rhino(sphere)
surface = Rhino.Geometry.NurbsSurface.CreateFromSphere(sphere)
return cls.from_rhino(surface)
@classmethod
def from_cylinder(cls, cylinder):
"""Create a NURBS surface from a cylinder.
Parameters
----------
cylinder : :class:`compas.geometry.Cylinder`
The surface's geometry.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsSurface`
"""
cylinder = cylinder_to_rhino(cylinder)
surface = Rhino.Geometry.NurbsSurface.CreateFromCylinder(cylinder)
return cls.from_rhino(surface)
@classmethod
def from_torus(cls, torus):
"""Create a NURBS surface from a torus.
Parameters
----------
torus : :class:`compas.geometry.Torus`
The surface's geometry.
Returns
-------
:class:`compas_rhino.geometry.RhinoNurbsSurface`
"""
raise NotImplementedError
@classmethod
def from_rhino(cls, rhino_surface):
"""Construct a NURBS surface from an existing Rhino surface.
Parameters
----------
rhino_surface : :rhino:`Rhino.Geometry.Surface`
A Rhino surface.
Returns
-------
:class:`compas_rhino.geometry.RhinoSurface`
"""
curve = cls()
curve.rhino_surface = rhino_surface
return curve
@classmethod
def from_plane(cls, plane, box):
"""Construct a surface from a plane.
Parameters
----------
plane : :class:`compas.geometry.Plane`
The plane.
Returns
-------
:class:`compas_rhino.geometry.RhinoSurface`
"""
plane = plane_to_rhino(plane)
box = Rhino.Geometry.BoundingBox(box.xmin, box.ymin, box.zmin, box.xmax, box.ymax, box.zmax)
rhino_surface = Rhino.Geometry.PlaneSurface.CreateThroughBox(plane, box)
return cls.from_rhino(rhino_surface)
@classmethod
def from_frame(cls, frame, u_interval, v_interval):
"""Creates a planar surface from a frame and parametric domain information.
Parameters
----------
frame : :class:`compas.geometry.Frame`
A frame with point at the center of the wanted plannar surface and
x and y axes the direction of u and v respectively.
u_interval : tuple(float, float)
The parametric domain of the U parameter. u_interval[0] => u_interval[1].
v_interval : tuple(float, float)
The parametric domain of the V parameter. v_interval[0] => v_interval[1].
Returns
-------
:class:`compas_rhino.geometry.surface.RhinoSurface`
"""
surface = Rhino.Geometry.PlaneSurface(
frame_to_rhino_plane(frame),
Rhino.Geometry.Interval(*u_interval),
Rhino.Geometry.Interval(*v_interval),
)
if not surface:
msg = "Failed creating PlaneSurface from frame:{} u_interval:{} v_interval:{}"
raise ValueError(msg.format(frame, u_interval, v_interval))
return cls.from_rhino(surface)
# ==============================================================================
# Conversions
# ==============================================================================
# ==============================================================================
# Methods
# ==============================================================================
def copy(self):
"""Make an independent copy of the current surface.
Returns
-------
:class:`compas_rhino.geometry.RhinoSurface`
"""
cls = type(self)
surface = cls()
surface.rhino_surface = self.rhino_surface.Duplicate() # type: ignore
return surface
def transform(self, T):
"""Transform this surface.
Parameters
----------
T : :class:`compas.geometry.Transformation`
A COMPAS transformation.
Returns
-------
None
"""
self.rhino_surface.Transform(transformation_to_rhino(T)) # type: ignore
def u_isocurve(self, u):
"""Compute the isoparametric curve at parameter u.
Parameters
----------
u : float
Returns
-------
:class:`compas_rhino.geometry.RhinoCurve`
"""
curve = self.rhino_surface.IsoCurve(1, u) # type: ignore
return RhinoCurve.from_rhino(curve)
def v_isocurve(self, v):
"""Compute the isoparametric curve at parameter v.
Parameters
----------
v : float
Returns
-------
:class:`compas_rhino.geometry.RhinoCurve`
"""
curve = self.rhino_surface.IsoCurve(0, v) # type: ignore
return RhinoCurve.from_rhino(curve)
def point_at(self, u, v):
"""Compute a point on the surface.
Parameters
----------
u : float
v : float
Returns
-------
:class:`compas.geometry.Point`
"""
point = self.rhino_surface.PointAt(u, v) # type: ignore
return point_to_compas(point)
def curvature_at(self, u, v):
"""Compute the curvature at a point on the surface.
Parameters
----------
u : float
v : float
Returns
-------
tuple[[float, float, float], [float, float, float], float, [float, float, float], float, [float, float, float], float, float] | None
A tuple containing the point, normal vector, maximum principal curvature value, maximum principal curvature direction,
minimun principal curvature value, minimun principal curvature direction, gaussian curvature value and mean curvature
value for the point at UV. None at failure.
"""
surface_curvature = self.rhino_surface.CurvatureAt(u, v) # type: ignore
if surface_curvature:
point, normal, kappa_u, direction_u, kappa_v, direction_v, gaussian, mean = surface_curvature
cpoint = point_to_compas(point)
cnormal = vector_to_compas(normal)
cdirection_u = vector_to_compas(direction_u)
cdirection_v = vector_to_compas(direction_v)
return (cpoint, cnormal, kappa_u, cdirection_u, kappa_v, cdirection_v, gaussian, mean)
def frame_at(self, u, v):
"""Compute the local frame at a point on the curve.
Parameters
----------
u : float
v : float
Returns
-------
:class:`compas.geometry.Frame`
"""
result, plane = self.rhino_surface.FrameAt(u, v) # type: ignore
if result:
return plane_to_compas_frame(plane)
# ==============================================================================
# Methods continued
# ==============================================================================
def closest_point(self, point, return_parameters=False):
"""Compute the closest point on the curve to a given point.
Parameters
----------
point : :class:`compas.geometry.Point`
The test point.
return_parameters : bool, optional
If True, return the UV parameters of the closest point as tuple in addition to the point location.
Returns
-------
:class:`compas.geometry.Point`
If `return_parameters` is False.
:class:`compas.geometry.Point`, (float, float)
If `return_parameters` is True.
"""
result, u, v = self.rhino_surface.ClosestPoint(point_to_rhino(point)) # type: ignore
if not result:
return
point = self.point_at(u, v)
if return_parameters:
return point, (u, v)
return point
def aabb(self, precision=0.0, optimal=False):
"""Compute the axis aligned bounding box of the surface.
Parameters
----------
precision : float, optional
optimal : float, optional
Flag indicating that the box should be precise.
Returns
-------
:class:`compas.geometry.Box`
"""
box = self.rhino_surface.GetBoundingBox(optimal) # type: ignore
return box_to_compas(Rhino.Geometry.Box(box))
def intersections_with_curve(self, curve, tolerance=1e-3, overlap=1e-3):
"""Compute the intersections with a curve.
Parameters
----------
line : :class:`compas.geometry.Curve`
Returns
-------
list[:class:`compas.geometry.Point`]
"""
intersections = Rhino.Geometry.Intersect.Intersection.CurveSurface(curve.rhino_curve, self.rhino_surface, tolerance, overlap)
points = []
for event in intersections:
if event.IsPoint:
point = point_to_compas(event.PointA)
points.append(point)
return points