/
face.py
291 lines (243 loc) · 10.3 KB
/
face.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import Rhino # type: ignore
from Rhino.Geometry import Interval # type: ignore
from Rhino.Geometry import RevSurface # type: ignore
from compas.geometry import Brep
from compas.geometry import BrepFace
from compas.geometry import Sphere
from compas.geometry import Cylinder
from compas.geometry import Frame
from compas.geometry import SurfaceType
from compas_rhino.geometry import RhinoNurbsSurface
from compas_rhino.geometry.surfaces import RhinoSurface
from compas_rhino.conversions import plane_to_compas_frame
from compas_rhino.conversions import sphere_to_compas
from compas_rhino.conversions import cylinder_to_compas
from compas_rhino.conversions import cylinder_to_rhino
from compas_rhino.conversions import sphere_to_rhino
from compas_rhino.conversions import frame_to_rhino_plane
from .loop import RhinoBrepLoop
from .edge import RhinoBrepEdge
class RhinoBrepFace(BrepFace):
"""A wrapper for Rhino.Geometry.BrepFace
Attributes
----------
native_surface : :class:`Rhino.Geometry.Surface`
The rhino native underlying geometry of this face.
loops : list[:class:`compas_rhino.geometry.RhinoBrepLoop`], read-only
The list of loops which comprise this face.
surface : :class:`compas_rhino.geometry.RhinoNurbsSurface`
The compas_rhino wrapper of the underlying geometry of this face.
boundary : :class:`compas_rhino.geometry.RhinoBrepLoop`, read-only
The loop which defines the outer boundary of this face.
holes : list[:class:`compas_rhino.geometry.RhinoBrepLoop`], read-only
The list of loops which comprise the holes of this brep, if any.
is_plane : float, read-only
True if the geometry of this face is a plane, False otherwise.
native_face : :class:`Rhino.Geometry.BrepFace`
The underlying BrepFace object.
"""
def __init__(self, rhino_face=None):
super(RhinoBrepFace, self).__init__()
self._loops = None
self._surface = None
self._face = None
if rhino_face:
self.native_face = rhino_face
# ==============================================================================
# Data
# ==============================================================================
@property
def data(self):
surface_type, surface, uv_domain, plane = self._get_surface_geometry(self._face.UnderlyingSurface())
return {
"surface_type": surface_type,
"surface": surface.data,
"uv_domain": uv_domain,
"frame": plane_to_compas_frame(plane).data, # until all shapes have a frame
"loops": [loop.data for loop in self._loops],
}
@classmethod
def from_data(cls, data, builder):
"""Construct an object of this type from the provided data.
Parameters
----------
data : dict
The data dictionary.
builder : :class:`compas_rhino.geometry.BrepBuilder`
The object reconstructing the current Brep.
Returns
-------
:class:`compas.data.Data`
An instance of this object type if the data contained in the dict has the correct schema.
"""
instance = cls()
instance._surface = instance._make_surface_from_data(
data["surface_type"], data["surface"], data["uv_domain"], data["frame"]
)
face_builder = builder.add_face(instance._surface)
for loop_data in data["loops"]:
RhinoBrepLoop.from_data(loop_data, face_builder)
instance.native_face = face_builder.result
return instance
# ==============================================================================
# Properties
# ==============================================================================
@property
def native_surface(self):
return self._surface
@property
def area(self):
return self._mass_props.Area
@property
def centroid(self):
return self._mass_props.Centroid
@property
def edges(self):
brep = self._face.Brep
edge_indices = self._face.AdjacentEdges()
return [RhinoBrepEdge(brep.Edges[index]) for index in edge_indices]
@property
def loops(self):
return self._loops
@property
def surface(self):
return self._surface
@property
def boundary(self):
return self._loops[0]
@property
def holes(self):
return self._loops[1:]
@property
def is_plane(self):
return self._face.UnderlyingSurface().IsPlanar()
@property
def is_cone(self):
return self._face.UnderlyingSurface().IsCone()
@property
def is_cylinder(self):
return self._face.UnderlyingSurface().IsCylinder()
@property
def is_sphere(self):
return self._face.UnderlyingSurface().IsSphere()
@property
def is_torus(self):
return self._face.UnderlyingSurface().IsTorus()
@property
def native_face(self):
return self._face
@native_face.setter
def native_face(self, rhino_face):
self._face = rhino_face
self._mass_props = Rhino.Geometry.AreaMassProperties.Compute(rhino_face.ToBrep())
self._loops = [RhinoBrepLoop(loop) for loop in rhino_face.Loops]
self._surface = RhinoNurbsSurface.from_rhino(self._face.UnderlyingSurface().ToNurbsSurface())
@property
def nurbssurface(self):
return self._surface
@property
def vertices(self):
vertices = []
for edge in self.edges:
vertices.extend(edge.vertices)
return vertices
@property
def type(self):
if self.is_cone:
return SurfaceType.CONE
elif self.is_cylinder:
return SurfaceType.CYLINDER
elif self.is_sphere:
return SurfaceType.SPHERE
elif self.is_torus:
return SurfaceType.TORUS
elif self.is_plane:
return SurfaceType.PLANE
else:
return SurfaceType.OTHER_SURFACE
# ==============================================================================
# Methods
# ==============================================================================
@staticmethod
def _get_surface_geometry(surface):
uv_domain = [[surface.Domain(0)[0], surface.Domain(0)[1]], [surface.Domain(1)[0], surface.Domain(1)[1]]]
if isinstance(surface, Rhino.Geometry.PlaneSurface):
_, plane = surface.FrameAt(0.0, 0.0)
return "plane", plane_to_compas_frame(plane), uv_domain, plane
if isinstance(surface, Rhino.Geometry.NurbsSurface):
_, plane = surface.FrameAt(0.0, 0.0)
return "nurbs", RhinoNurbsSurface.from_rhino(surface), uv_domain, plane
if isinstance(surface, Rhino.Geometry.RevSurface):
success, cast_surface = surface.TryGetSphere()
if success:
return "sphere", sphere_to_compas(cast_surface), uv_domain, cast_surface.EquatorialPlane
success, cast_surface = surface.TryGetCylinder()
if success:
return "cylinder", cylinder_to_compas(cast_surface), uv_domain, cast_surface.BasePlane
success, cast_surface = surface.TryGetTorus()
raise NotImplementedError(
"Support for surface type: {} is not yet implemented.".format(surface.__class__.__name__)
)
def _make_surface_from_data(self, surface_type, surface_data, uv_domain, frame_data):
u_domain, v_domain = uv_domain
frame = Frame.from_data(frame_data) # workaround until all shapes have a frame
if surface_type == "plane":
frame = Frame.from_data(surface_data) # redundancy in shapes which already have a frame
surface = RhinoSurface.from_frame(frame, u_domain, v_domain)
elif surface_type == "sphere":
sphere = self._make_sphere_surface(surface_data, u_domain, v_domain, frame)
surface = RhinoSurface.from_rhino(sphere)
elif surface_type == "cylinder":
cylinder = self._make_cylinder_surface(surface_data, u_domain, v_domain, frame)
surface = RhinoSurface.from_rhino(cylinder)
elif surface_type == "nurbs":
surface = RhinoNurbsSurface.from_data(surface_data)
elif surface_type == "torus":
raise NotImplementedError("Support for torus surface is not yet implemented!")
surface.rhino_surface.SetDomain(0, Interval(*u_domain))
surface.rhino_surface.SetDomain(1, Interval(*v_domain))
return surface
@staticmethod
def _make_cylinder_surface(surface_data, u_domain, v_domain, frame):
cylinder = Cylinder.from_data(surface_data)
cylinder = cylinder_to_rhino(cylinder)
cylinder.BasePlane = frame_to_rhino_plane(frame)
surface = RevSurface.CreateFromCylinder(cylinder)
surface.SetDomain(0, Interval(*u_domain))
surface.SetDomain(1, Interval(*v_domain))
return surface
@staticmethod
def _make_sphere_surface(surface_data, u_domain, v_domain, frame):
sphere = Sphere.from_data(surface_data)
sphere = sphere_to_rhino(sphere)
# seems Sphere => RevSurface conversion modifies the orientation of the sphere
# setting the plane here is overriden by this modification and surface ends up oriented differntly than
# original.
# sphere.EquatorialPlane = frame_to_rhino_plane(frame)
surface = RevSurface.CreateFromSphere(sphere)
surface.SetDomain(0, Interval(*u_domain))
surface.SetDomain(1, Interval(*v_domain))
return surface
# ==============================================================================
# Methods
# ==============================================================================
def adjacent_faces(self):
"""Returns a list of the faces adjacent to this face.
Returns
-------
list[:class:`compas_rhino.geometry.RhinoBrepFace`]
The list of adjacent faces.
"""
face_indices = self._face.AdjacentFaces()
brep = self._face.Brep
return [RhinoBrepFace(brep.Faces[index]) for index in face_indices]
def as_brep(self):
"""Returns a Brep representation of this face.
Returns
-------
:class:`~compas_rhino.geometry.RhinoBrep`
"""
return Brep.from_native(self._face.ToBrep())