waterfall-cad-0.9.0.0: src/Waterfall/TwoD/BoundingBox/AxisAligned.hs
module Waterfall.TwoD.BoundingBox.AxisAligned
( HasAxisAlignedBoundingBox2D ()
, axisAlignedBoundingBox2D
, aabbToShape
) where
import Waterfall.TwoD.Shape (Shape, unitSquare, area)
import Waterfall.TwoD.Internal.Shape (acquireShape)
import Waterfall.Internal.Finalizers (unsafeFromAcquire, acquireForeignPtr)
import Waterfall.Internal.FromOpenCascade (gpPntToV3)
import Linear (V2 (..), (^-^), _xy)
import qualified OpenCascade.Bnd.Box as Bnd.Box
import qualified OpenCascade.BRepBndLib as BRepBndLib
import Control.Monad.IO.Class (liftIO)
import Data.Acquire (Acquire)
import Foreign (Ptr)
import qualified OpenCascade.TopoDS as TopoDS
import Waterfall.TwoD.Internal.Path2D (Path2D (..))
import Waterfall.Internal.Path.Common (RawPath(..))
import OpenCascade.Inheritance (upcast)
import Waterfall.Internal.ToOpenCascade (v3ToVertex)
import Control.Monad (forM_)
import Waterfall.TwoD.Transforms (scale2D, translate2D)
import Control.Lens ((^.))
import Waterfall.Diagram (Diagram (getDiagramLines, getDiagramFills))
import Data.Functor.Compose (Compose(..))
import Data.Monoid (Ap(..))
-- | Class for things that have a Bounding Box
--
-- Has instances for `Shape`, `Path2D` and `Diagram`
class HasAxisAlignedBoundingBox2D a where
_isEmpty :: a -> Bool
_getShapes :: a -> Acquire [Ptr TopoDS.Shape]
instance HasAxisAlignedBoundingBox2D Shape where
_isEmpty s = area s == 0
_getShapes = fmap pure . acquireShape
instance HasAxisAlignedBoundingBox2D Path2D where
_isEmpty (Path2D EmptyRawPath) = True
_isEmpty _ = False
_getShapes (Path2D (ComplexRawPath p)) = fmap (pure . upcast) . acquireForeignPtr $ p
_getShapes (Path2D (SinglePointRawPath p)) = fmap (pure . upcast) . v3ToVertex $ p
_getShapes (Path2D EmptyRawPath) = pure []
instance HasAxisAlignedBoundingBox2D Diagram where
_isEmpty diagram =
all (all _isEmpty) (getDiagramLines diagram)
&& all (_isEmpty . snd) (getDiagramFills diagram)
_getShapes diagram =
getAp
((foldMap (Ap . _getShapes) . Compose) (getDiagramLines diagram)
<> (foldMap (Ap . _getShapes) . Compose) (getDiagramFills diagram)
)
-- | Return the smallest Axis Aligned Bounding Box (AABB) that contains the `Shape`, `Path2D` or `Diagram`.
--
-- If computable, the AABB is returned in the form @(lo, hi)@,
-- where @lo@ is the vertex of the box with the lowest individual values,
-- and @hi@ is the vertex with the highest values.
axisAlignedBoundingBox2D :: HasAxisAlignedBoundingBox2D a => a -> Maybe (V2 Double, V2 Double)
axisAlignedBoundingBox2D s =
if _isEmpty s
then Nothing
else Just . unsafeFromAcquire $ do
shapes <- _getShapes s
theBox <- Bnd.Box.new
liftIO $ forM_ shapes $ \shape -> BRepBndLib.addOptimal shape theBox False False
p1 <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMin theBox
p2 <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMax theBox
return (p1 ^. _xy, p2 ^. _xy)
-- | A rectangle, specified by two diagonal vertices.
--
-- This can be used to make a shape from the output of `axisAlignedBoundingBox2D`
aabbToShape :: (V2 Double, V2 Double) -- ^ if this argument input is @(lo, hi)@, one vertex of the rectangle is placed at @lo@, the opposite vertex is at @hi@
-> Shape
aabbToShape (lo, hi) = translate2D lo . scale2D (hi ^-^ lo) $ unitSquare