A BoundingBox3d is a rectangular box in 3D defined by its minimum and maximum
X, Y and Z values. It is possible to generate bounding boxes for most geometric
objects; for example, Triangle3d.boundingBox
takes a Triangle3d and returns a BoundingBox3d that contains that triangle.
There are several use cases where it is more efficient to deal with the bounding
box of an object than the object itself, such as:
Bounding boxes can be constructed by passing a record with minX, maxX,
minY, maxY, minZ and maxZ fields to the BoundingBox3d constructor, for
example
exampleBox =
BoundingBox3d
{ minX = -2
, maxX = 2
, minY = 2
, maxY = 5
, minZ = 3
, maxZ = 4
}If you construct a BoundingBox3d this way, you must ensure that the given
values are properly ordered: minX <= maxX,
minY <= maxY, minZ <= maxZ.
Alternately, you can construct bounding boxes using functions such as
Point3d.hull where the input order does not matter.
Construct a bounding box containing all points in the given list. If the
list is empty, returns Nothing.
points =
[ Point3d ( 2, 1, 3 )
, Point3d ( -1, 5, -2 )
, Point3d ( 6, 4, 2 )
]
BoundingBox3d.containing points
--> Just
--> (BoundingBox3d
--> { minX = -1
--> , maxX = 6
--> , minY = 1
--> , maxY = 5
--> , minZ = -2
--> , maxZ = 3
--> }
--> )
BoundingBox3d.containing []
--> NothingIf you have exactly two points, you can use Point3d.hull
instead (which returns a BoundingBox3d instead of a Maybe BoundingBox3d).
Get the minimum and maximum X, Y and Z values of a bounding box in a single record.
BoundingBox3d.extrema exampleBox
--> { minX = -2
--> , maxX = 2
--> , minY = 2
--> , maxY = 5
--> , minZ = 3
--> , maxZ = 4
--> }Can be useful when combined with record destructuring, for example
{ minX, maxX, minY, maxY, minZ, maxZ } =
BoundingBox3d.extrema exampleBox
--> minX = -2
--> maxX = 2
--> minY = 2
--> maxY = 5
--> minZ = 3
--> maxZ = 4Get the minimum X value of a bounding box.
BoundingBox3d.minX exampleBox
--> -2Get the maximum X value of a bounding box.
BoundingBox3d.maxX exampleBox
--> 2Get the minimum Y value of a bounding box.
BoundingBox3d.minY exampleBox
--> 2Get the maximum Y value of a bounding box.
BoundingBox3d.maxY exampleBox
--> 5Get the minimum Z value of a bounding box.
BoundingBox3d.minZ exampleBox
--> 3Get the maximum Z value of a bounding box.
BoundingBox3d.maxZ exampleBox
--> 4Get the X, Y and Z dimensions (widths) of a bounding box.
BoundingBox3d.dimensions exampleBox
--> ( 4, 3, 1 )Get the median X value of a bounding box.
BoundingBox3d.midX exampleBox
--> 0Get the median Y value of a bounding box.
BoundingBox3d.midY exampleBox
--> 3.5Get the median Z value of a bounding box.
BoundingBox3d.midZ exampleBox
--> 3.5Get the point at the center of a bounding box.
BoundingBox3d.centroid exampleBox
--> Point3d ( 0, 3.5, 3.5 )Check if a bounding box contains a particular point.
firstPoint =
Point3d ( 1, 4, 3 )
secondPoint =
Point3d ( 3, 4, 5 )
BoundingBox3d.contains firstPoint exampleBox
--> True
BoundingBox3d.contains secondPoint exampleBox
--> FalseTest if one bounding box overlaps (touches) another.
firstBox =
BoundingBox3d
{ minX = 0
, maxX = 3
, minY = 0
, maxY = 2
, minZ = 0
, maxZ = 1
}
secondBox =
BoundingBox3d
{ minX = 0
, maxX = 3
, minY = 1
, maxY = 4
, minZ = -1
, maxZ = 2
}
thirdBox =
BoundingBox3d
{ minX = 0
, maxX = 3
, minY = 4
, maxY = 5
, minZ = -1
, maxZ = 2
}
BoundingBox3d.overlaps firstBox secondBox
--> True
BoundingBox3d.overlaps firstBox thirdBox
--> FalseTest if the second given bounding box is fully contained within the first (is a subset of it).
outerBox =
BoundingBox3d
{ minX = 0
, maxX = 10
, minY = 0
, maxY = 10
, minZ = 0
, maxZ = 10
}
innerBox =
BoundingBox3d
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 9
, minZ = 7
, maxZ = 8
}
overlappingBox =
BoundingBox3d
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 12
, minZ = 7
, maxZ = 8
}
BoundingBox3d.isContainedIn outerBox innerBox
--> True
BoundingBox3d.isContainedIn outerBox overlappingBox
--> FalseBuild a bounding box that contains both given bounding boxes.
firstBox =
BoundingBox3d
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
, minZ = 0
, maxZ = 5
}
secondBox =
BoundingBox3d
{ minX = -2
, maxX = 2
, minY = 4
, maxY = 5
, minZ = -1
, maxZ = 0
}
BoundingBox3d.hull firstBox secondBox
--> BoundingBox3d
--> { minX = -2
--> , maxX = 4
--> , minY = 2
--> , maxY = 5
--> , minZ = -1
--> , maxZ = 5
--> }Attempt to build a bounding box that contains all points common to both
given bounding boxes. If the given boxes do not overlap, returns Nothing.
firstBox =
BoundingBox3d
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
, minZ = 5
, maxZ = 8
}
secondBox =
BoundingBox3d
{ minX = 2
, maxX = 5
, minY = 1
, maxY = 4
, minZ = 6
, maxZ = 7
}
thirdBox =
BoundingBox3d
{ minX = 1
, maxX = 4
, minY = 4
, maxY = 5
, minZ = 5
, maxZ = 8
}
BoundingBox3d.intersection firstBox secondBox
--> Just
--> (BoundingBox3d
--> { minX = 2
--> , maxX = 4
--> , minY = 2
--> , maxY = 3
--> , minZ = 6
--> , maxZ = 7
--> }
--> )
BoundingBox3d.intersection firstBox thirdBox
--> Nothing
Construct a zero-width bounding box containing a single point.