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:
Construct a bounding box from its minimum and maximum X, Y and Z values:
exampleBox =
BoundingBox3d.with
{ minX = -2
, maxX = 2
, minY = 2
, maxY = 5
, minZ = 3
, maxZ = 4
}If the minimum and maximum values are provided in the wrong order (for example if <code>minX > maxX</code>), then they will be swapped so that the resulting bounding box is valid.
Construct a zero-width bounding box containing a single point.
point =
Point3d.fromCoordinates ( 2, 1, 3 )
BoundingBox3d.singleton point
--> BoundingBox3d.with
--> { minX = 2
--> , maxX = 2
--> , minY = 1
--> , maxY = 1
--> , minZ = 3
--> , maxZ = 3
--> }Build a bounding box that contains both given bounding boxes.
firstBox =
BoundingBox3d.with
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
, minZ = 0
, maxZ = 5
}
secondBox =
BoundingBox3d.with
{ minX = -2
, maxX = 2
, minY = 4
, maxY = 5
, minZ = -1
, maxZ = 0
}
BoundingBox3d.hull firstBox secondBox
--> BoundingBox3d.with
--> { 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.with
{ minX = 1
, maxX = 4
, minY = 2
, maxY = 3
, minZ = 5
, maxZ = 8
}
secondBox =
BoundingBox3d.with
{ minX = 2
, maxX = 5
, minY = 1
, maxY = 4
, minZ = 6
, maxZ = 7
}
thirdBox =
BoundingBox3d.with
{ minX = 1
, maxX = 4
, minY = 4
, maxY = 5
, minZ = 5
, maxZ = 8
}
BoundingBox3d.intersection firstBox secondBox
--> Just
--> (BoundingBox3d.with
--> { minX = 2
--> , maxX = 4
--> , minY = 2
--> , maxY = 3
--> , minZ = 6
--> , maxZ = 7
--> }
--> )
BoundingBox3d.intersection firstBox thirdBox
--> NothingConstruct a bounding box containing all bounding boxes in the given list. If
the list is empty, returns Nothing.
singletonBox =
BoundingBox3d.singleton
(Point3d.fromCoordinates ( 2, 1, 0 ))
BoundingBox3d.hullOf [ exampleBox, singletonBox ]
--> Just
--> (BoundingBox3d.with
--> { minX = -2,
--> , maxX = 2
--> , minY = 1
--> , maxY = 5
--> , minZ = 0
--> , maxZ = 4
--> }
--> )
BoundingBox3d.hullOf [ exampleBox ]
--> Just exampleBox
BoundingBox3d.hullOf []
--> NothingIf you have exactly two bounding boxes, you can use BoundingBox3d.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.fromCoordinates ( 0, 3.5, 3.5 )Check if a bounding box contains a particular point.
firstPoint =
Point3d.fromCoordinates ( 1, 4, 3 )
secondPoint =
Point3d.fromCoordinates ( 3, 4, 5 )
BoundingBox3d.contains firstPoint exampleBox
--> True
BoundingBox3d.contains secondPoint exampleBox
--> FalseTest if the second given bounding box is fully contained within the first (is a subset of it).
outerBox =
BoundingBox3d.with
{ minX = 0
, maxX = 10
, minY = 0
, maxY = 10
, minZ = 0
, maxZ = 10
}
innerBox =
BoundingBox3d.with
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 9
, minZ = 7
, maxZ = 8
}
overlappingBox =
BoundingBox3d.with
{ minX = 1
, maxX = 5
, minY = 3
, maxY = 12
, minZ = 7
, maxZ = 8
}
BoundingBox3d.isContainedIn outerBox innerBox
--> True
BoundingBox3d.isContainedIn outerBox overlappingBox
--> FalseTest if two boxes touch or overlap at all (have any points in common);
BoundingBox3d.intersects firstBox secondBoxis equivalent to
BoundingBox3d.intersection firstBox secondBox
/= Nothingbut is more efficient.
firstBox =
BoundingBox3d.with
{ minX = 0
, maxX = 3
, minY = 0
, maxY = 2
, minZ = 0
, maxZ = 1
}
secondBox =
BoundingBox3d.with
{ minX = 0
, maxX = 3
, minY = 1
, maxY = 4
, minZ = -1
, maxZ = 2
}
thirdBox =
BoundingBox3d.with
{ minX = 0
, maxX = 3
, minY = 4
, maxY = 5
, minZ = -1
, maxZ = 2
}
BoundingBox3d.intersects firstBox secondBox
--> True
BoundingBox3d.intersects firstBox thirdBox
--> FalseDEPRECATED: Alias for intersects, kept for compatibility. Use intersects
instead.
Check if one box overlaps another by less than, greater than or equal to a given amount. For example, you could perform a tolerant collision check (one that only returns true if the boxes overlap by at least some small finite amount, and ignores boxes that just barely touch each other) as
boxesCollide =
BoundingBox3d.overlappingBy GT 0.001 box1 box2This can be read as "box1 and box2 are overlapping by greater than 0.001
units". (The Order
type and its three values LT, GT and EQ are defined in Elm's Basics
module so are available by default in any Elm program.)
Overlap is defined as the minimum distance one box would have to move so that it did not touch the other, and is always positive for any two overlapping boxes.
Boxes that just touch are considered to have an overlap of zero, which is distinct from 'no overlap'. Boxes that do not touch or overlap at all are considered to have an overlap which is less than zero but not comparable to any negative number.
overlappingBy LT 1e-3 will return true if the two boxes overlap by less
than 0.001 units or if they do not overlap at all (false if they overlap by
more than 0.001 units).overlappingBy LT 0 will return true only if the two boxes don't touch or
overlap at all.overlappingBy LT -1e-3 will always return false! If you care about how
much two boxes are separated by, use separatedBy instead.overlappingBy GT 1e-3 will return true if the two boxes overlap by at
least 0.001 units (false if they overlap by less than that or do not overlap
at all).overlappingBy GT 0 will return true if the two boxes overlap by any
non-zero amount (false if they just touch or do not overlap at all).overlappingBy GT -1e-3 doesn't make a lot of sense but will return true if
the boxes touch or overlap at all (false if they don't overlap, regardless
of how close they are to overlapping). In this case, though, it would make
more sense to just user intersects instead.Checking whether two boxes overlap by exactly a given amount is pretty weird and vulnerable to floating-point roundoff, but is defined as follows:
overlappingBy EQ 1e-3 will return true if the two boxes overlap by exactly
0.001 units.overlappingBy EQ 0 will return true if and only if the boxes just touch
each other.overlappingBy EQ -1e-3 will always return false.Check if one box is separated from another by less than, greater than or
equal to a given amount. For example, to perform clash detection between some
objects, you could use separatedBy on those objects' bounding boxes as a quick
check to see if the objects had a gap of at least 1 cm between them:
safelySeparated =
BoundingBox3d.separatedBy GT 0.01 box1 box2This can be read as "box1 and box2 are separated by greater than 0.01
units". (The Order
type and its three values LT, GT and EQ are defined in Elm's Basics
module so are available by default in any Elm program.)
Separation is defined as the minimum distance one box would have to move so that it touched the other, and is always positive for any two boxes that do not touch.
Boxes that just touch are considered to have a separation of zero, which is distinct from 'no separation'. 'No separation' (overlap) is considered to be less than zero but not comparable to any negative number.
separatedBy LT 1e-3 will return true if the two boxes are separated by
less than 0.001 units or if they touch or overlap (false if they are
separated by at least 0.001 units).separatedBy LT 0 will return true only if the boxes overlap by some
non-zero amount.separatedBy LT -1e-3 will always return false! If you care about how
much two boxes overlap by, use overlappingBy instead.separatedBy GT 1e-3 will return true if the two boxes are separated by at
least 0.001 units (false if they are separated by less than that or if they
touch or overlap).separatedBy GT 0 will return true if the two boxes are separated by any
non-zero amount (false if they touch or overlap).separatedBy GT -1e-3 doesn't make a lot of sense but will return true if
the boxes just touch or are separated by any amount (false if they overlap
by any non-zero amount).Checking whether two boxes are separated by exactly a given amount is pretty weird and vulnerable to floating-point roundoff, but is defined as follows:
separatedBy EQ 1e-3 will return true if the two boxes are separated by
exactly 0.001 units.separatedBy EQ 0 will return true if and only if the boxes just touch each
other.separatedBy EQ -3 will always return false.Scale a bounding box about a given point by a given scale.
point =
Point3d.fromCoordinates ( 2, 2, 2 )
BoundingBox3d.scaleAbout point 2 exampleBox
--> BoundingBox3d.with
--> { minX = -6
--> , maxX = 2
--> , minY = 2
--> , maxY = 8
--> , minZ = 4
--> , maxZ = 6
--> }Translate a bounding box by a given displacement.
displacement =
Vector3d.fromComponents ( 2, -3, 1 )
BoundingBox3d.translateBy displacement exampleBox
--> BoundingBox3d.with
--> { minX = 0
--> , maxX = 4
--> , minY = -1
--> , maxY = 2
--> , minZ = 4
--> , maxZ = 5
--> }