packages feed

waterfall-cad-0.9.0.0: src/Waterfall/BoundingBox/Oriented.hs

{-# LANGUAGE InstanceSigs #-}
module Waterfall.BoundingBox.Oriented
( HasOrientedBoundingBox ()
, OrientedBoundingBox
, obbCenter
, obbSideX
, obbSideY
, obbSideZ
, orientedBoundingBox
, obbToSolid
) where

import Linear (V3 (..), normalize, (^*))
import Waterfall.Internal.Solid (Solid (..), acquireSolid, solidFromAcquire, emptyPaintMap)
import Waterfall.Internal.Finalizers (acquireForeignPtr, unsafeFromAcquirePtr, unsafeFromAcquire)
import Waterfall.Internal.FromOpenCascade (gpXYZToV3)
import Foreign.Ptr (Ptr)
import Waterfall.Solid (volume, box)
import Waterfall.Transforms (translate)
import qualified OpenCascade.Bnd.OBB as OBB
import OpenCascade.Bnd.OBB (OBB)
import OpenCascade.GP.Types (XYZ)
import qualified OpenCascade.GP as GP
import qualified OpenCascade.GP.Ax3 as Ax3
import qualified OpenCascade.GP.Trsf as Trsf
import qualified OpenCascade.BRepBndLib as BRepBndLib
import qualified OpenCascade.BRepBuilderAPI.Transform  as BRepBuilderAPI.Transform
import qualified OpenCascade.BRepBuilderAPI.MakeShape as BRepBuilderAPI.MakeShape
import OpenCascade.Inheritance (upcast)
import Data.Acquire (Acquire)
import Control.Monad.IO.Class (liftIO)
import Control.Monad ((<=<), forM_)
import Foreign.ForeignPtr (ForeignPtr)
import Control.DeepSeq (NFData (rnf), rwhnf)
import qualified OpenCascade.TopoDS as TopoDS
import Waterfall.Internal.Path (Path (..))
import Waterfall.Internal.Path.Common (RawPath(..))
import Waterfall.Internal.ToOpenCascade (v3ToVertex)

-- | Class for things that have a Oriented Bounding Box
-- 
-- Has instances for `Solid` and `Path`
class HasOrientedBoundingBox a where
    _isEmpty :: a -> Bool
    _getShapes :: a -> Acquire [Ptr TopoDS.Shape]

instance HasOrientedBoundingBox Solid where
    _isEmpty s = volume s == 0
    _getShapes :: Solid -> Acquire [Ptr TopoDS.Shape]
    _getShapes = fmap pure . acquireSolid

instance HasOrientedBoundingBox Path where
    _isEmpty (Path EmptyRawPath) = True
    _isEmpty _ = False

    _getShapes (Path (ComplexRawPath p)) = fmap (pure . upcast) . acquireForeignPtr $ p
    _getShapes (Path (SinglePointRawPath p)) = fmap (pure . upcast) . v3ToVertex $ p
    _getShapes (Path EmptyRawPath) = pure []

-- | An OrientedBoundingBox may be a tighter fit than an axis aligned bounding box would be
newtype OrientedBoundingBox = OrientedBoundingBox { rawOBB :: ForeignPtr OBB }

instance NFData OrientedBoundingBox where 
    rnf (OrientedBoundingBox obb) = rwhnf obb

-- | Compute an `OrientedBoundingBox` for a `Solid` or `Path`
orientedBoundingBox :: HasOrientedBoundingBox a => a -> Maybe OrientedBoundingBox
orientedBoundingBox s = 
    if _isEmpty s
        then Nothing 
        else Just . OrientedBoundingBox . unsafeFromAcquirePtr $ do
            obb <- OBB.new
            shapes <- _getShapes s
            liftIO $ forM_ shapes $ \shape -> BRepBndLib.addOBB shape obb False True True
            return obb

queryOBB :: (Ptr OBB -> Acquire (V3 Double)) -> OrientedBoundingBox -> V3 Double
queryOBB f = unsafeFromAcquire . ( f <=< acquireForeignPtr . rawOBB)

-- | The center point of an `OrientedBoundingBox`
obbCenter :: OrientedBoundingBox -> V3 Double
obbCenter = queryOBB (liftIO . gpXYZToV3 <=< OBB.center)

getSide :: (Ptr OBB -> Acquire (Ptr XYZ)) -> (Ptr OBB -> IO Double) -> OrientedBoundingBox -> V3 Double
getSide fxyz fLength = queryOBB $ \obb -> do
    side <- liftIO . gpXYZToV3 =<< fxyz obb
    len <- liftIO $ fLength obb
    return $ normalize side ^* len

-- | The "X" side of the oriented bounding box.
--
-- This is measured from the center to one Face.
-- So the length of this vector is /half/ of the side length of the bounding box.
obbSideX :: OrientedBoundingBox -> V3 Double 
obbSideX = getSide OBB.xDirection OBB.xHSize
    
-- | The "Y" side of the oriented bounding box.
--
-- This is measured from the center to one face.
-- So the length of this vector is /half/ of the side length of the bounding box.
obbSideY :: OrientedBoundingBox -> V3 Double 
obbSideY = getSide OBB.yDirection OBB.yHSize
    
-- | The "Z" side of the oriented bounding box
--
-- This is measured from the center to one face.
-- So the length of this vector is /half/ of the side length of the bounding box.
obbSideZ :: OrientedBoundingBox -> V3 Double
obbSideZ =  getSide OBB.zDirection OBB.zHSize

-- | Reify an `OrientedBoundingBox` as a `Solid`
obbToSolid :: OrientedBoundingBox -> Solid
obbToSolid obb = solidFromAcquire emptyPaintMap $ do
    obb' <- acquireForeignPtr . rawOBB $ obb
    x <- liftIO . OBB.xHSize $ obb'
    y <- liftIO . OBB.yHSize $ obb'
    z <- liftIO . OBB.zHSize $ obb'
    let halfBox = V3 x y z
    unpositioned <- acquireSolid . translate (negate halfBox) $ box (V3 x y z ^* 2)
    position <- OBB.position obb'
    o <- Ax3.fromAx2 =<< GP.xoy
    trsf <- Trsf.new
    liftIO $ Trsf.setDisplacement trsf o position
    BRepBuilderAPI.MakeShape.shape 
        =<< fmap upcast (BRepBuilderAPI.Transform.fromShapeTrsfAndCopy unpositioned trsf True)