|
14 | 14 | ## |
15 | 15 | ##You should have received a copy of the GNU Lesser General Public License |
16 | 16 | ##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. |
17 | | - |
18 | 17 | import random |
19 | 18 |
|
20 | 19 | from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox |
21 | 20 | 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 |
23 | 22 | from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_MakeVertex |
24 | 23 | 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 |
25 | 27 |
|
26 | 28 | from OCC.Display.SimpleGui import init_display |
27 | 29 |
|
28 | 30 | display, start_display, add_menu, add_function_to_menu = init_display() |
29 | 31 |
|
30 | 32 |
|
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() |
39 | 42 |
|
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 |
48 | 53 |
|
49 | 54 |
|
50 | 55 | # compute the oriented bounding box of a point cloud |
51 | 56 | obb1 = Bnd_OBB() |
52 | | -n = 10 |
53 | | -for _ in range(n): |
| 57 | +num_points = 10 |
| 58 | +for _ in range(num_points): |
54 | 59 | x = random.uniform(100, 500) |
55 | 60 | y = random.uniform(100, 500) |
56 | 61 | z = random.uniform(100, 500) |
57 | 62 | p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape() |
58 | 63 | 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) |
61 | 66 | display.DisplayShape(obb_shape1, transparency=0.5) |
62 | 67 |
|
63 | 68 | # 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 | | - |
68 | 69 | cylinder_head = TopoDS_Shape() |
69 | 70 | builder = BRep_Builder() |
70 | 71 | breptools_Read(cylinder_head, "../assets/models/cylinder_head.brep", builder) |
71 | 72 | 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) |
74 | 75 | display.DisplayShape(cylinder_head) |
75 | 76 | display.DisplayShape(obb_shape2, transparency=0.5, update=True) |
76 | 77 |
|
|
0 commit comments