def GetOrientedBoundingBoxShapeCompound(shapes_compound, optimal_OBB=False): obb = Bnd_OBB() if optimal_OBB: is_triangulationUsed = True is_optimal = True is_shapeToleranceUsed = False brepbndlib_AddOBB(shapes_compound, obb, is_triangulationUsed, is_optimal, is_shapeToleranceUsed) else: brepbndlib_AddOBB(shapes_compound, obb) aBaryCenter = obb.Center() aXDir = obb.XDirection() aYDir = obb.YDirection() aZDir = obb.ZDirection() aHalfX = obb.XHSize() aHalfY = obb.YHSize() aHalfZ = obb.ZHSize() ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) pt=[] pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY + az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY + az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY + az * aHalfZ)) pt.append(gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY + az * aHalfZ)) return pt
def get_oriented_boundingbox_ratio(shape, optimal_OBB=True, ratio=1.0): """ return the oriented bounding box of the TopoDS_Shape `shape` Parameters ---------- shape : TopoDS_Shape or a subclass such as TopoDS_Face the shape to compute the bounding box from optimal_OBB : bool, True by default. If set to True, compute the optimal (i.e. the smallest oriented bounding box). Optimal OBB is a bit longer. ratio : float, 1.0 by default. Returns ------- a list with center, x, y and z sizes a shape """ obb = Bnd_OBB() if optimal_OBB: is_triangulationUsed = True is_optimal = True is_shapeToleranceUsed = False brepbndlib_AddOBB(shape, obb, is_triangulationUsed, is_optimal, is_shapeToleranceUsed) else: brepbndlib_AddOBB(shape, obb) # converts the bounding box to a shape aBaryCenter = obb.Center() aXDir = obb.XDirection() aYDir = obb.YDirection() aZDir = obb.ZDirection() aHalfX = obb.XHSize() aHalfY = obb.YHSize() aHalfZ = obb.ZHSize() dx = aHalfX * ratio dy = aHalfY * ratio dz = aHalfZ * ratio ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir)) anAxes.SetLocation(gp_Pnt(p.XYZ() - ax * dx - ay * dy - az * dz)) aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * dx, 2.0 * dy, 2.0 * dz).Shape() return aBaryCenter, [dx, dy, dz], aBox
def get_oriented_boundingbox_coor(shape, optimal_OBB=False): '''return the oriented bounding box of the TopoDS_Shape `shape`''' obb = Bnd_OBB() if optimal_OBB: is_triangulationUsed = True is_optimal = True is_shapeToleranceUsed = False brepbndlib_AddOBB(shape, obb, is_triangulationUsed, is_optimal, is_shapeToleranceUsed) else: brepbndlib_AddOBB(shape, obb) # converts the bounding box to a shape aBaryCenter = obb.Center() aXDir = obb.XDirection() aYDir = obb.YDirection() aZDir = obb.ZDirection() aHalfX = obb.XHSize() aHalfY = obb.YHSize() aHalfZ = obb.ZHSize() ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir)) anAxes.SetLocation( gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ)) #corner points on the ground face of the box new_origin = gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ) # left_down right_up = gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY - az * aHalfZ) right_down = gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY - az * aHalfZ) left_up = gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY - az * aHalfZ) corners_bot = [new_origin, left_up, right_up, right_down] new_origin_top = gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY + az * aHalfZ) right_up_top = gp_Pnt(p.XYZ() + ax * aHalfX + ay * aHalfY + az * aHalfZ) right_down_top = gp_Pnt(p.XYZ() + ax * aHalfX - ay * aHalfY + az * aHalfZ) left_up_top = gp_Pnt(p.XYZ() - ax * aHalfX + ay * aHalfY + az * aHalfZ) corners_top = [new_origin_top, left_up_top, right_up_top, right_down_top] if corners_top[0].Z() >= corners_bot[0].Z(): #always return bot , top return corners_bot, corners_top else: return corners_top, corners_bot
def get_oriented_boundingbox(shape, optimal_OBB=True): """return the oriented bounding box of the TopoDS_Shape `shape` Parameters ---------- shape : TopoDS_Shape or a subclass such as TopoDS_Face the shape to compute the bounding box from optimal_OBB : bool, True by default. If set to True, compute the optimal (i.e. the smallest oriented bounding box). Optimal OBB is a bit longer. Returns ------- a list with center, x, y and z sizes a shape """ obb = Bnd_OBB() if optimal_OBB: is_triangulation_used = True is_optimal = True is_shape_tolerance_used = False brepbndlib_AddOBB(shape, obb, is_triangulation_used, is_optimal, is_shape_tolerance_used) else: brepbndlib_AddOBB(shape, obb) # converts the bounding box to a shape bary_center = obb.Center() x_direction = obb.XDirection() y_direction = obb.YDirection() z_direction = obb.ZDirection() a_half_x = obb.XHSize() a_half_y = obb.YHSize() a_half_z = obb.ZHSize() ax = gp_XYZ(x_direction.X(), x_direction.Y(), x_direction.Z()) ay = gp_XYZ(y_direction.X(), y_direction.Y(), y_direction.Z()) az = gp_XYZ(z_direction.X(), z_direction.Y(), z_direction.Z()) p = gp_Pnt(bary_center.X(), bary_center.Y(), bary_center.Z()) an_axe = gp_Ax2(p, gp_Dir(z_direction), gp_Dir(x_direction)) an_axe.SetLocation( gp_Pnt(p.XYZ() - ax * a_half_x - ay * a_half_y - az * a_half_z)) a_box = BRepPrimAPI_MakeBox(an_axe, 2.0 * a_half_x, 2.0 * a_half_y, 2.0 * a_half_z).Shape() return bary_center, [a_half_x, a_half_y, a_half_z], a_box
def get_oriented_bounding_box( shape, offset=0.0, xWidht=None, yWidht=None, zWidht=None, xxWidht=None, yyWidht=None, zzWidht=None, ): bbox = Bnd_OBB() brepbndlib_AddOBB(shape, bbox, True, True, True) # bbox.Enlarge(0.5) obb_shape1 = ConvertBndToShape(bbox, offset, xWidht, yWidht, zWidht, zzWidht, xxWidht, yyWidht) return obb_shape1
def get_bounding_box_alt(geom): from OCC.Core.Bnd import Bnd_OBB from OCC.Core.BRepBndLib import brepbndlib_AddOBB obb = Bnd_OBB() brepbndlib_AddOBB(geom, obb) # converts the bounding box to a shape # aBaryCenter = obb.Center() # aXDir = obb.XDirection() # aYDir = obb.YDirection() # aZDir = obb.ZDirection() # aHalfX = obb.XHSize() # aHalfY = obb.YHSize() # aHalfZ = obb.ZHSize() return obb
def obb(self, precision=0.0): """Compute the oriented bounding box of the surface. Parameters ---------- precision : float, optional Returns ------- :class:`~compas.geometry.Box` """ box = Bnd_OBB() brepbndlib_AddOBB(self.occ_shape, box, True, True, True) return Box(Frame.from_occ(box.Position()), box.XHSize(), box.YHSize(), box.ZHSize())
aHalfY = theBox.YHSize() aHalfZ = theBox.ZHSize() ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir)) anAxes.SetLocation( gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ)) aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape() return aBox obb = Bnd_OBB() # choose n random vertices n = 10 for _ in range(n): x = random.uniform(100, 1000) y = random.uniform(100, 1000) z = random.uniform(100, 1000) p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape() display.DisplayShape(p) brepbndlib_AddOBB(p, obb) obb_shape = ConvertBndToShape(obb) display.DisplayShape(obb_shape) # a ref box b = BRepPrimAPI_MakeBox(10, 10, 10).Shape()
def get_oriented_bounding_box_bnd(shape): bbox = Bnd_OBB() brepbndlib_AddOBB(shape, bbox, True, True, True) return bbox
aHalfZ = theBox.ZHSize() ax = gp_XYZ(aXDir.X(), aXDir.Y(), aXDir.Z()) ay = gp_XYZ(aYDir.X(), aYDir.Y(), aYDir.Z()) az = gp_XYZ(aZDir.X(), aZDir.Y(), aZDir.Z()) p = gp_Pnt(aBaryCenter.X(), aBaryCenter.Y(), aBaryCenter.Z()) anAxes = gp_Ax2(p, gp_Dir(aZDir), gp_Dir(aXDir)) anAxes.SetLocation( gp_Pnt(p.XYZ() - ax * aHalfX - ay * aHalfY - az * aHalfZ)) aBox = BRepPrimAPI_MakeBox(anAxes, 2.0 * aHalfX, 2.0 * aHalfY, 2.0 * aHalfZ).Shape() return aBox # compute the oriented bounding box of a point cloud obb1 = Bnd_OBB() n = 10 for _ in range(n): x = random.uniform(100, 500) y = random.uniform(100, 500) z = random.uniform(100, 500) p = BRepBuilderAPI_MakeVertex(gp_Pnt(x, y, z)).Shape() display.DisplayShape(p) brepbndlib_AddOBB(p, obb1) obb_shape1 = ConvertBndToShape(obb1) display.DisplayShape(obb_shape1, transparency=0.5) # then loads a brep file and computes the optimal bounding box from OCC.Core.BRepTools import breptools_Read from OCC.Core.TopoDS import TopoDS_Shape from OCC.Core.BRep import BRep_Builder