Skip to content

Commit b9638cf

Browse files
committed
clean up core_geometry_oriented_bounding_box
1 parent 207d4ad commit b9638cf

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

examples/core_geometry_oriented_bounding_box.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,64 @@
1414
##
1515
##You should have received a copy of the GNU Lesser General Public License
1616
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17-
1817
import random
1918

2019
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
2120
from OCC.Core.gp import gp_Pnt, gp_Ax2, gp_Dir, gp_XYZ
22-
from OCC.Core.BRepBndLib import brepbndlib_AddOBB
21+
from OCC.Core.BRepBndLib import brepbndlib
2322
from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex
2423
from OCC.Core.Bnd import Bnd_OBB
24+
from OCC.Core.BRepTools import breptools_Read
25+
from OCC.Core.TopoDS import TopoDS_Shape
26+
from OCC.Core.BRep import BRep_Builder
2527

2628
from OCC.Display.SimpleGui import init_display
2729

2830
display, start_display, add_menu, add_function_to_menu = init_display()
2931

3032

31-
def ConvertBndToShape(theBox):
32-
aBaryCenter = theBox.Center()
33-
aXDir = theBox.XDirection()
34-
aYDir = theBox.YDirection()
35-
aZDir = theBox.ZDirection()
36-
aHalfX = theBox.XHSize()
37-
aHalfY = theBox.YHSize()
38-
aHalfZ = theBox.ZHSize()
33+
def convert_bnd_to_shape(the_box):
34+
"""Converts a bounding box to a box shape."""
35+
barycenter = the_box.Center()
36+
x_dir = the_box.XDirection()
37+
y_dir = the_box.YDirection()
38+
z_dir = the_box.ZDirection()
39+
half_x = the_box.XHSize()
40+
half_y = the_box.YHSize()
41+
half_z = the_box.ZHSize()
3942

40-
ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z())
41-
ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z())
42-
az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z())
43-
p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z())
44-
anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir))
45-
anAxes.SetLocation(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ))
46-
aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape()
47-
return aBox
43+
x_vec = gp_XYZ(x_dir.X(), x_dir.Y(), x_dir.Z())
44+
y_vec = gp_XYZ(y_dir.X(), y_dir.Y(), y_dir.Z())
45+
z_vec = gp_XYZ(z_dir.X(), z_dir.Y(), z_dir.Z())
46+
point = gp_Pnt(barycenter.X(), barycenter.Y(), barycenter.Z())
47+
axes = gp_Ax2(point, gp_Dir(z_dir), gp_Dir(x_dir))
48+
axes.SetLocation(
49+
gp_Pnt(point.XYZ() - x_vec * half_x - y_vec * half_y - z_vec * half_z)
50+
)
51+
box = BRepPrimAPI_MakeBox(axes, 2.0 * half_x, 2.0 * half_y, 2.0 * half_z).Shape()
52+
return box
4853

4954

5055
# compute the oriented bounding box of a point cloud
5156
obb1 = Bnd_OBB()
52-
n = 10
53-
for _ in range(n):
57+
num_points = 10
58+
for _ in range(num_points):
5459
x = random.uniform(100, 500)
5560
y = random.uniform(100, 500)
5661
z = random.uniform(100, 500)
5762
p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape()
5863
display.DisplayShape(p)
59-
brepbndlib_AddOBB(p, obb1)
60-
obb_shape1 = ConvertBndToShape(obb1)
64+
brepbndlib.AddOBB(p, obb1)
65+
obb_shape1 = convert_bnd_to_shape(obb1)
6166
display.DisplayShape(obb_shape1, transparency=0.5)
6267

6368
# then loads a brep file and computes the optimal bounding box
64-
from OCC.Core.BRepTools import breptools_Read
65-
from OCC.Core.TopoDS import TopoDS_Shape
66-
from OCC.Core.BRep import BRep_Builder
67-
6869
cylinder_head = TopoDS_Shape()
6970
builder = BRep_Builder()
7071
breptools_Read(cylinder_head, "../assets/models/cylinder_head.brep", builder)
7172
obb2 = Bnd_OBB()
72-
brepbndlib_AddOBB(cylinder_head, obb2, True, True, True)
73-
obb_shape2 = ConvertBndToShape(obb2)
73+
brepbndlib.AddOBB(cylinder_head, obb2, True, True, True)
74+
obb_shape2 = convert_bnd_to_shape(obb2)
7475
display.DisplayShape(cylinder_head)
7576
display.DisplayShape(obb_shape2, transparency=0.5, update=True)
7677

0 commit comments

Comments
 (0)