module Data.BoundingBox.B4 where
import Data.Vector.Class
import Data.Vector.V4
import Data.BoundingBox.Range as R
data BBox4 = BBox4 {minX, minY, minZ, minW, maxX, maxY, maxZ, maxW :: !Scalar} deriving (Eq, Show)
rangeX :: BBox4 -> Range
rangeX b = Range (minX b) (maxX b)
rangeY :: BBox4 -> Range
rangeY b = Range (minY b) (maxY b)
rangeZ :: BBox4 -> Range
rangeZ b = Range (minZ b) (maxZ b)
rangeW :: BBox4 -> Range
rangeW b = Range (minW b) (maxW b)
rangeXYZW :: Range -> Range -> Range -> Range -> BBox4
rangeXYZW (Range x0 x1) (Range y0 y1) (Range z0 z1) (Range w0 w1) = BBox4 x0 y0 z0 w0 x1 y1 z1 w1
bounds :: Vector4 -> Vector4 -> BBox4
bounds (Vector4 xa ya za wa) (Vector4 xb yb zb wb) =
BBox4 (min xa xb) (min ya yb) (min za zb) (min wa wb) (max xa xb) (max ya yb) (max za zb) (max wa wb)
within_bounds :: Vector4 -> BBox4 -> Bool
within_bounds (Vector4 x y z w) b =
x `R.within_bounds` (rangeX b) &&
y `R.within_bounds` (rangeY b) &&
z `R.within_bounds` (rangeZ b) &&
w `R.within_bounds` (rangeW b)
min_bound :: BBox4 -> Vector4
min_bound (BBox4 x0 y0 z0 w0 x1 y1 z1 w1) = Vector4 x0 y0 z0 w0
max_bound :: BBox4 -> Vector4
max_bound (BBox4 x0 y0 z0 w0 x1 y1 z1 w1) = Vector4 x1 y1 z1 w1
union :: BBox4 -> BBox4 -> BBox4
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)
rw = (rangeW b0) `R.union` (rangeW b1)
in rangeXYZW rx ry rz rw
isect :: BBox4 -> BBox4 -> Maybe BBox4
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)
rw <- (rangeW b0) `R.isect` (rangeW b1)
return (rangeXYZW rx ry rz rw)