-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcore_boolean_fuzzy_cut_emmenthaler.py
More file actions
93 lines (74 loc) · 2.7 KB
/
core_boolean_fuzzy_cut_emmenthaler.py
File metadata and controls
93 lines (74 loc) · 2.7 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
#!/usr/bin/env python
##Copyright 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/>.
import random
import time
import sys
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox, BRepPrimAPI_MakeCylinder
from OCC.Core.gp import gp_Pnt, gp_Vec, gp_Ax2, gp_Dir
from OCC.Core.BRepAlgoAPI import BRepAlgoAPI_Cut
from OCC.Core.TopTools import TopTools_ListOfShape
from OCC.Display.SimpleGui import init_display
display, start_display, add_menu, add_function_to_menu = init_display()
def random_pnt():
x, y, z = [random.uniform(0, 1) for _ in range(3)]
return gp_Pnt(x, y, z)
def random_vec():
x, y, z = [random.uniform(-1, 1) for _ in range(3)]
return gp_Vec(x, y, z)
def fuzzy_cut(shape_A, shape_B, tol=5e-5, parallel=False):
"""returns shape_A - shape_B"""
cut = BRepAlgoAPI_Cut()
L1 = TopTools_ListOfShape()
L1.Append(shape_A)
L2 = TopTools_ListOfShape()
L2.Append(shape_B)
cut.SetArguments(L1)
cut.SetTools(L2)
cut.SetFuzzyValue(tol)
cut.SetRunParallel(parallel)
cut.Build()
return cut.Shape()
def emmenthaler(event=None):
init_time = time.time()
scope = 200.0
nb_iter = 40
box = BRepPrimAPI_MakeBox(scope, scope, scope).Shape()
def do_cyl():
axe = gp_Ax2()
axe.SetLocation(gp_Pnt((random_vec() * scope).XYZ()))
axe.SetDirection(gp_Dir(random_vec()))
cyl = BRepPrimAPI_MakeCylinder(axe, random.uniform(8, 36), 5000.0)
return cyl.Shape()
# perform a recursive fusszy cut
# initialize the loop with the box shape
shp = box
for i in range(nb_iter):
cyl = do_cyl()
tA = time.time()
shp = fuzzy_cut(shp, cyl, 1e-4)
print("boolean cylinder:", i, "took", time.time() - tA)
total_time = time.time() - init_time
print("Total time : %fs" % total_time)
display.DisplayShape(shp, update=True)
start_display()
def exit(event=None):
sys.exit()
if __name__ == "__main__":
add_menu("fuzzy boolean operations")
add_function_to_menu("fuzzy boolean operations", emmenthaler)
start_display()