Skip to content

Commit

Permalink
add oriented bounding box support to Solid.from_bounding_box and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegenstein committed Feb 7, 2025
1 parent e7aee38 commit b64807f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/build123d/topology/three_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
BoundBox,
Color,
Location,
OrientedBoundBox,
Plane,
Vector,
VectorLike,
Expand Down Expand Up @@ -949,9 +950,13 @@ def extrude_until(
return result

@classmethod
def from_bounding_box(cls, bbox: BoundBox) -> Solid:
def from_bounding_box(cls, bbox: BoundBox | OrientedBoundBox) -> Solid:
"""A box of the same dimensions and location"""
return Solid.make_box(*bbox.size).locate(Location(bbox.min))
if isinstance(bbox, BoundBox):
return Solid.make_box(*bbox.size).locate(Location(bbox.min))
else:
moved_plane = Plane(Location(-bbox.size / 2)).move(bbox.location)
return Solid.make_box(*bbox.size, plane=moved_plane)

@classmethod
def make_box(
Expand Down
22 changes: 21 additions & 1 deletion tests/test_direct_api/test_solid.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@
import unittest

from build123d.build_enums import GeomType, Kind, Until
from build123d.geometry import Axis, Location, Plane, Pos, Vector
from build123d.geometry import (
Axis,
BoundBox,
Location,
OrientedBoundBox,
Plane,
Pos,
Vector,
)
from build123d.objects_curve import Spline
from build123d.objects_sketch import Circle, Rectangle
from build123d.topology import Compound, Edge, Face, Shell, Solid, Vertex, Wire
Expand Down Expand Up @@ -233,6 +241,18 @@ def test_revolve(self):
)
self.assertEqual(len(r.faces()), 6)

def test_from_bounding_box(self):
cyl = Solid.make_cylinder(0.001, 10).locate(Location(Plane.isometric))
cyl2 = Solid.make_cylinder(1, 10).locate(Location(Plane.isometric))

rbb = Solid.from_bounding_box(cyl.bounding_box())
obb = Solid.from_bounding_box(cyl.oriented_bounding_box())
obb2 = Solid.from_bounding_box(cyl2.oriented_bounding_box())

self.assertAlmostEqual(rbb.volume, (10**3) * (3**0.5) / 9, 0)
self.assertTrue(rbb.volume > obb.volume)
self.assertAlmostEqual(obb2.volume, 40, 4)


if __name__ == "__main__":
unittest.main()

0 comments on commit b64807f

Please sign in to comment.