forked from tpaviot/pythonocc-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface.py
More file actions
467 lines (387 loc) · 14.4 KB
/
face.py
File metadata and controls
467 lines (387 loc) · 14.4 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
##Copyright 2008-2013 Jelle Feringa (jelleferinga@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>
from OCC.BRep import BRep_Tool_Surface, BRep_Tool
from OCC.BRepIntCurveSurface import BRepIntCurveSurface_Inter
from OCC.BRepTopAdaptor import BRepTopAdaptor_FClass2d
from OCC.Geom import Geom_Curve
from OCC.GeomAPI import GeomAPI_ProjectPointOnSurf
from OCC.GeomLib import GeomLib_IsPlanarSurface
from OCC.TopAbs import TopAbs_IN
from OCC.TopExp import topexp
from OCC.TopoDS import *
from OCC.GeomLProp import GeomLProp_SLProps
from OCC.BRepCheck import BRepCheck_Face
from OCC.BRepTools import breptools_UVBounds
from OCC.BRepAdaptor import BRepAdaptor_Surface, BRepAdaptor_HSurface
from OCC.ShapeAnalysis import ShapeAnalysis_Surface
from OCC.IntTools import IntTools_FaceFace
from OCC.ShapeAnalysis import ShapeAnalysis_Surface
from OCC.GeomProjLib import geomprojlib
from OCC.Adaptor3d import Adaptor3d_IsoCurve
from base import Display, KbeObject, GlobalProperties
from edge import Edge
from Construct import *
from Topology import Topo, WireExplorer
'''
TODO:
use IntTools_FaceFace to compute intersection between 2 faces
also useful to test if 2 faces are tangent
inflection point -> scipy.fsolve
radius / centre of circle
divide curve by circles
frenet frame
'''
class DiffGeomSurface(object):
def __init__(self, instance):
self.instance = instance
self._curvature = None
self._curvature_initiated = False
def curvature(self, u, v):
'''returns the curvature at the u parameter
the curvature object can be returned too using
curvatureType == curvatureType
curvatureTypes are:
gaussian
minimum
maximum
mean
curvatureType
'''
if not self._curvature_initiated:
self._curvature = GeomLProp_SLProps(self.instance.surface_handle, u, v, 1, 1e-6)
_domain = self.instance.domain()
if u in _domain or v in _domain:
print('<<<CORRECTING DOMAIN...>>>')
div = 1000
delta_u, delta_v = (_domain[0] - _domain[1])/div, (_domain[2] - _domain[3])/div
if u in _domain:
low, hi = u-_domain[0], u-_domain[1]
if low < hi:
u = u - delta_u
else:
u = u + delta_u
if v in _domain:
low, hi = v-_domain[2], v-_domain[3]
if low < hi:
v = v - delta_v
else:
v = v + delta_v
self._curvature.SetParameters(u, v)
self._curvature_initiated = True
return self._curvature
def gaussian_curvature(self, u, v):
return self.curvature(u, v).GaussianCurvature()
def min_curvature(self, u, v):
return self.curvature(u, v).MinCurvature()
def mean_curvature(self, u, v):
return self.curvature(u, v).MeanCurvature()
def max_curvature(self, u, v):
return self.curvature(u, v).MaxCurvature()
def normal(self, u, v):
# TODO: should make this return a gp_Vec
curv = self.curvature(u, v)
if curv.IsNormalDefined():
return curv.Normal()
else:
raise ValueError('normal is not defined at u,v: {0}, {1}'.format(u, v))
def tangent(self, u, v):
dU, dV = gp_Dir(), gp_Dir()
curv = self.curvature(u, v)
if curv.IsTangentUDefined() and curv.IsTangentVDefined():
curv.TangentU(dU), curv.TangentV(dV)
return dU, dV
else:
return None, None
def radius(self, u, v):
'''returns the radius at u
'''
# TODO: SHOULD WE RETURN A SIGNED RADIUS? ( get rid of abs() )?
try:
_crv_min = 1./self.min_curvature(u, v)
except ZeroDivisionError:
_crv_min = 0.
try:
_crv_max = 1./self.max_curvature(u, v)
except ZeroDivisionError:
_crv_max = 0.
return abs((_crv_min+_crv_max)/2.)
def frenet_frame(self, u, v):
'''returns the frenet frame ( the 2 tangency directions + normal )
syntax sugar
'''
raise NotImplementedError
def derivative_u(self, u, n):
'''return n derivatives of u
'''
raise NotImplementedError
def derivative_v(self, v, n):
'''return n derivatives of v
'''
raise NotImplementedError
def torsion(self, u, v):
'''returns the torsion at the parameter
http://en.wikipedia.org/wiki/Frenet-Serret_formulas
'''
raise NotImplementedError
def continuity(self, face):
'''returns continuity between self and another surface
'''
# add dictionary mapping which G / C continuity it is...
raise NotImplementedError
def inflection_parameters(self):
"""
:return: a list of tuples (u,v) of parameters
where there are inflection points on the edge
returns None if no inflection parameters are found
"""
raise NotImplementedError
class Face(TopoDS_Face, KbeObject):
"""high level surface API
object is a Face if part of a Solid
otherwise the same methods do apply, apart from the topology obviously
"""
def __init__(self, face):
'''
'''
assert isinstance(face, TopoDS_Face), 'need a TopoDS_Face, got a %s' % edge.__class__
assert not face.IsNull()
super(Face, self).__init__()
KbeObject.__init__(self, 'face')
# we need to copy the base shape using the following three
# lines
assert self.IsNull()
self.TShape(face.TShape())
self.Location(face.Location())
self.Orientation(face.Orientation())
assert not self.IsNull()
# cooperative classes
self.DiffGeom = DiffGeomSurface(self)
# STATE; whether cooperative classes are yet initialized
self._curvature_initiated = False
self._geometry_lookup_init = False
#===================================================================
# properties
#===================================================================
self._h_srf = None
self._srf = None
self._adaptor = None
self._adaptor_handle = None
self._classify_uv = None # cache the u,v classifier, no need to rebuild for every sample
self._topo = None
# aliasing of useful methods
def is_u_periodic(self):
return self.adaptor.IsUPeriodic()
def is_v_periodic(self):
return self.adaptor.IsVPeriodic()
def is_u_closed(self):
return self.adaptor.IsUClosed()
def is_v_closed(self):
return self.adaptor.IsVClosed()
def is_u_rational(self):
return self.adaptor.IsURational()
def is_v_rational(self):
return self.adaptor.IsVRational()
def u_degree(self):
return self.adaptor.UDegree()
def v_degree(self):
return self.adaptor.VDegree()
def u_continuity(self):
return self.adaptor.UContinuity()
def v_continuity(self):
return self.adaptor.VContinuity()
def domain(self):
'''the u,v domain of the curve
:return: UMin, UMax, VMin, VMax
'''
return breptools_UVBounds(self)
def mid_point(self):
"""
:return: the parameter at the mid point of the face,
and its corresponding gp_Pnt
"""
u_min, u_max, v_min, v_max = self.domain()
u_mid = (u_min + u_max) / 2.
v_mid = (v_min + v_max) / 2.
pnt = self.parameter_to_point(u_mid, v_mid)
return ((u_mid, v_mid), self.adaptor.Value(u_mid, v_mid))
@property
def topo(self):
if self._topo is not None:
return self._topo
else:
self._topo = Topo(self)
return self._topo
@property
def surface(self):
if self._srf is not None and not self.is_dirty:
pass
else:
self._h_srf = BRep_Tool_Surface(self)
self._srf = self._h_srf.GetObject()
return self._srf
@property
def surface_handle(self):
if self._h_srf is not None and not self.is_dirty:
pass
else:
self.surface
return self._h_srf
@property
def adaptor(self):
if self._adaptor is not None and not self.is_dirty:
pass
else:
self._adaptor = BRepAdaptor_Surface(self)
self._adaptor_handle = BRepAdaptor_HSurface()
self._adaptor_handle.Set(self._adaptor)
return self._adaptor
@property
def adaptor_handle(self):
if self._adaptor_handle is not None and not self.is_dirty:
pass
else:
self.adaptor
return self._adaptor_handle
def weight(self, indx):
'''sets or gets the weight of a control point at the index
'''
# TODO: somehow its hard to get a Geom_SplineSurface object from a face
# nessecary to get control points and weights
raise NotImplementedError
def close(self):
'''if possible, close self'''
raise NotImplementedError
def is_closed(self):
sa = ShapeAnalysis_Surface(self.surface_handle)
# sa.GetBoxUF()
return sa.IsUClosed(), sa.IsVClosed()
def is_planar(self, tol=TOLERANCE):
'''checks if the surface is planar within a tolerance
:return: bool, gp_Pln
'''
aaa = GeomLib_IsPlanarSurface(self.surface_handle, tol)
return aaa.IsPlanar()
def is_trimmed(self):
"""
:return: True if the Wire delimiting the Face lies on the bounds
of the surface
if this is not the case, the wire represents a contour that delimits
the face [ think cookie cutter ]
and implies that the surface is trimmed
"""
_round = lambda x: round(x, 3)
a = map(_round, breptools_UVBounds(self))
b = map(_round, self.adaptor.Surface().Surface().GetObject().Bounds())
if a != b:
print('a,b', a, b)
return True
return False
def is_overlapping(self, other):
overlap = IntTools_FaceFace()
def on_trimmed(self, u, v):
'''tests whether the surface at the u,v parameter has been trimmed
'''
if self._classify_uv is None:
self._classify_uv = BRepTopAdaptor_FClass2d(self, 1e-9)
uv = gp_Pnt2d(u, v)
if self._classify_uv.Perform(uv) == TopAbs_IN:
return True
else:
return False
def parameter_to_point(self, u, v):
'''returns the coordinate at u,v
'''
return self.surface.Value(u, v)
def point_to_parameter(self, pt):
'''
returns the uv value of a point on a surface
@param pt:
'''
sas = ShapeAnalysis_Surface(self.surface_handle)
uv = sas.ValueOfUV(pt, self.tolerance)
return uv.Coord()
def transform(self, transform):
'''affine transform
'''
raise NotImplementedError
def continuity_edge_face(self, edge, face):
"""
compute the continuity between two faces at :edge:
:param edge: an Edge or TopoDS_Edge from :face:
:param face: a Face or TopoDS_Face
:return: bool, GeomAbs_Shape if it has continuity, otherwise
False, None
"""
bt = BRep_Tool()
if bt.HasContinuity(edge, self, face):
continuity = bt.Continuity(edge, self, face)
return True, continuity
else:
return False, None
#===========================================================================
# Surface.project
# project curve, point on face
#===========================================================================
def project_vertex(self, pnt, tol=TOLERANCE):
'''projects self with a point, curve, edge, face, solid
method wraps dealing with the various topologies
if other is a point:
returns uv, point
'''
if isinstance(pnt, TopoDS_Vertex):
pnt = BRep_Tool.Pnt(pnt)
proj = GeomAPI_ProjectPointOnSurf(pnt, self.surface_handle, tol)
uv = proj.LowerDistanceParameters()
proj_pnt = proj.NearestPoint()
return uv, proj_pnt
def project_curve(self, other):
# this way Geom_Circle and alike are valid too
if (isinstance(other, TopoDS_Edge) or
isinstance(other, Geom_Curve) or
issubclass(other, Geom_Curve)):
# convert edge to curve
first, last = topexp.FirstVertex(other), topexp.LastVertex(other)
lbound, ubound = BRep_Tool().Parameter(first, other), BRep_Tool().Parameter(last, other)
other = BRep_Tool.Curve(other, lbound, ubound).GetObject()
return geomprojlib.Project(other, self.surface_handle)
def project_edge(self, edg):
if hasattr(edg, 'adaptor'):
return self.project_curve(self, self.adaptor)
return self.project_curve(self, to_adaptor_3d(edg))
def iso_curve(self, u_or_v, param):
"""
get the iso curve from a u,v + parameter
:param u_or_v:
:param param:
:return:
"""
uv = 0 if u_or_v == 'u' else 1
# TODO: REFACTOR, part of the Face class now...
iso = Adaptor3d_IsoCurve(self.adaptor_handle.GetHandle(), uv, param)
return iso
def Edges(self):
return [Edge(i) for i in WireExplorer(self.topo.wires().next()).ordered_edges()]
def __repr__(self):
return self.name
def __str__(self):
return self.__repr__()
if __name__ == "__main__":
from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere
sph = BRepPrimAPI_MakeSphere(1, 1).Face()
fc = Face(sph)
print(fc.is_trimmed())
print(fc.is_planar())