module Data.BoundingBox.B3 where
import Data.Vector.Class
import Data.Vector.V3
import Data.BoundingBox.Range as R
data BBox3 = BBox3 {minX, minY, minZ, maxX, maxY, maxZ :: !Scalar} deriving (Eq, Show)
rangeX :: BBox3 -> Range
rangeX b = Range (minX b) (maxX b)
rangeY :: BBox3 -> Range
rangeY b = Range (minY b) (maxY b)
rangeZ :: BBox3 -> Range
rangeZ b = Range (minZ b) (maxZ b)
rangeXYZ :: Range -> Range -> Range -> BBox3
rangeXYZ (Range x0 x1) (Range y0 y1) (Range z0 z1) = BBox3 x0 y0 z0 x1 y1 z1
bounds :: Vector3 -> Vector3 -> BBox3
bounds (Vector3 xa ya za) (Vector3 xb yb zb) = BBox3 (min xa xb) (min ya yb) (min za zb) (max xa xb) (max ya yb) (max za zb)
points_bounds :: [Vector3] -> BBox3
points_bounds ps =
let
xs = map v3x ps
ys = map v3y ps
zs = map v3z ps
in BBox3 (minimum xs) (minimum ys) (minimum zs) (maximum xs) (maximum ys) (maximum zs)
within_bounds :: Vector3 -> BBox3 -> Bool
within_bounds (Vector3 x y z) b =
x `R.within_bounds` (rangeX b) &&
y `R.within_bounds` (rangeY b) &&
z `R.within_bounds` (rangeZ b)
min_bound :: BBox3 -> Vector3
min_bound (BBox3 x0 y0 z0 x1 y1 z1) = Vector3 x0 y0 z0
max_bound :: BBox3 -> Vector3
max_bound (BBox3 x0 y0 z0 x1 y1 z1) = Vector3 x1 y1 z1
union :: BBox3 -> BBox3 -> BBox3
union b0 b1 =
let
rx = (rangeX b0) `R.union` (rangeX b1)
ry = (rangeY b0) `R.union` (rangeY b1)
rz = (rangeZ b0) `R.union` (rangeZ b1)
in rangeXYZ rx ry rz
isect :: BBox3 -> BBox3 -> Maybe BBox3
isect b0 b1 = do
rx <- (rangeX b0) `R.isect` (rangeX b1)
ry <- (rangeY b0) `R.isect` (rangeY b1)
rz <- (rangeZ b0) `R.isect` (rangeZ b1)
return (rangeXYZ rx ry rz)