packages feed

waterfall-cad-0.9.0.0: src/Waterfall/Internal/Solid.hs

{-# OPTIONS_HADDOCK not-home #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TypeApplications #-}
module Waterfall.Internal.Solid 
( Solid (..)
, PaintMap (..)
, History (..)
, emptyPaintMap
, paintMapIsEmpty
, acquireSolid
, fromAcquirePaints
, acquireFacePaints
, solidFromAcquire
, solidFromAcquireWithCatch
, solidFromAcquireTWithCatch
, solidFromAcquireMappingPaintMap
, solidFromAcquireWithPaintMap
, solidFromAcquireTWithPaintMap
, solidFromAcquireWithPaintMapWithCatch
, solidFromAcquireTWithPaintMapWithCatch
, solidFromAcquireMappingPaintMapWithCatch
, remapPaints
, mapPaintMap 
, materialiseFacePaints
, makeShapeHistory
, bopBuilderHistory
, shapeDivideHistory
, union3D
, difference3D
, intersection3D
, unions3D
, intersections3D
, emptySolid
, complement
, debug
) where

import Data.Acquire
import Foreign.Ptr
import Algebra.Lattice
import Control.Monad.IO.Class (liftIO)
import qualified OpenCascade.TopoDS as TopoDS
import qualified OpenCascade.TopoDS.Shape as TopoDS.Shape
import qualified OpenCascade.BRepAlgoAPI.Fuse as Fuse
import qualified OpenCascade.BRepAlgoAPI.Cut as Cut
import qualified OpenCascade.BRepAlgoAPI.Common as Common
import OpenCascade.BRepAlgoAPI (BuilderAlgo, BooleanOperation)
import qualified OpenCascade.BRepAlgoAPI.BuilderAlgo as BuilderAlgo
import qualified OpenCascade.BRepAlgoAPI.BooleanOperation as BooleanOperation
import qualified OpenCascade.BRepBuilderAPI.MakeSolid as MakeSolid
import OpenCascade.BRepBuilderAPI (MakeShape)
import qualified OpenCascade.BRepBuilderAPI.MakeShape as MakeShape
import qualified OpenCascade.BOPAlgo.Operation as BOPAlgo.Operation
import qualified OpenCascade.BOPAlgo.BOP as BOPAlgo.BOP
import qualified OpenCascade.BOPAlgo.CellsBuilder as CellsBuilder
import qualified OpenCascade.BOPAlgo.Builder as BOPAlgo.Builder
import qualified OpenCascade.TopAbs.ShapeEnum as ShapeEnum
import OpenCascade.Inheritance (SubTypeOf(..), upcast, unsafeDowncast)
import Waterfall.Internal.Finalizers (acquireForeignPtr, fromAcquirePtrT, unsafeFromAcquire, unsafeFromAcquirePtr, unsafeFromAcquirePtrTWithCatch, unsafeFromAcquirePtrWithCatch, unsafeFromAcquirePtrT)
import Waterfall.Internal.ToOpenCascade (shapeSingletonList)
import Data.Foldable (traverse_)
import Data.Bifunctor.Flip (Flip (..))
import Waterfall.Error (WaterfallError)
import Waterfall.Paint (Paint, paintIsDefined)
import Data.Functor.Compose (Compose(..))
import Control.Monad (filterM, forM_, (<=<))
import Waterfall.Internal.FromOpenCascade (shapesFromListOfShape, deduplicateTopoDSShapes, containsShapeKind, subShapes, subShapesOf)
import Foreign.ForeignPtr (ForeignPtr)
import Control.DeepSeq (NFData (rnf), liftRnf, liftRnf2, rwhnf)
import qualified OpenCascade.NCollection.List as NCollection.List
import qualified OpenCascade.ShapeUpgrade.Types as ShapeUpgrade
import qualified OpenCascade.ShapeBuild.ReShape as ShapeBuild.ReShape
import qualified OpenCascade.ShapeUpgrade.ShapeDivide as ShapeBuild.ShapeDivide
import Data.Proxy (Proxy(..))

data PaintMap 
    = UniformPaint Paint
    | FacePaints [(ForeignPtr TopoDS.Face, Paint)]

instance NFData PaintMap where 
    rnf (UniformPaint p) = rnf p
    rnf (FacePaints fs) = liftRnf (liftRnf2 rwhnf rnf) fs

emptyPaintMap :: PaintMap
emptyPaintMap = UniformPaint mempty

paintMapIsEmpty :: PaintMap -> Bool
paintMapIsEmpty (UniformPaint p) = not $ paintIsDefined p
paintMapIsEmpty _ = False
 
-- | The Boundary Representation of a solid object.
--
-- Alternatively, a region of 3d Space.
--
-- Under the hood, this is represented by an OpenCascade `TopoDS.Shape`.
-- The underlying shape should either be a Solid, or a Compound of Solids
-- 
-- While you shouldn't need to know what this means to use the library,
-- please feel free to report a bug if you're able to construct a `Solid`
-- where this isn't the case (without using internal functions).
data Solid = Solid 
    { rawSolid :: ForeignPtr TopoDS.Shape.Shape 
    , solidPaintMap :: PaintMap
    }

instance NFData Solid where
    rnf (Solid s paints) = rwhnf s `seq` rnf paints

acquireSolid :: Solid -> Acquire (Ptr TopoDS.Shape.Shape)
acquireSolid (Solid ptr _) = acquireForeignPtr ptr

solidFromAcquire :: PaintMap -> Acquire (Ptr TopoDS.Shape.Shape) -> Solid
solidFromAcquire paintMap = (`Solid` paintMap) . unsafeFromAcquirePtr

solidFromAcquireWithPaintMap ::  Acquire (PaintMap, Ptr TopoDS.Shape.Shape) -> Solid
solidFromAcquireWithPaintMap f = 
    let (paintMap, ptr) = unsafeFromAcquirePtrT f 
    in Solid ptr paintMap

solidFromAcquireTWithPaintMap :: Traversable t => Acquire (t (PaintMap, Ptr TopoDS.Shape.Shape)) -> t Solid
solidFromAcquireTWithPaintMap = 
    fmap (uncurry (flip Solid)) . getCompose . unsafeFromAcquirePtrT . fmap Compose

solidFromAcquireWithPaintMapWithCatch ::  Acquire (PaintMap, Ptr TopoDS.Shape.Shape) -> Either WaterfallError Solid
solidFromAcquireWithPaintMapWithCatch f = do
    (paintMap, ptr) <- unsafeFromAcquirePtrTWithCatch f 
    return $ Solid ptr paintMap
    
solidFromAcquireTWithPaintMapWithCatch :: Traversable t => Acquire (t (PaintMap, Ptr TopoDS.Shape.Shape)) -> Either WaterfallError (t Solid)
solidFromAcquireTWithPaintMapWithCatch = 
    fmap (fmap (uncurry (flip Solid)) . getCompose) . unsafeFromAcquirePtrTWithCatch . fmap Compose

solidFromAcquireWithCatch :: PaintMap -> Acquire (Ptr TopoDS.Shape.Shape) -> Either WaterfallError Solid
solidFromAcquireWithCatch paintMap = fmap (`Solid` paintMap) . unsafeFromAcquirePtrWithCatch

solidFromAcquireTWithCatch :: Traversable t => PaintMap -> Acquire (t (Ptr TopoDS.Shape.Shape)) -> Either WaterfallError (t Solid)
solidFromAcquireTWithCatch paintMap  = fmap (fmap (`Solid` paintMap)) . unsafeFromAcquirePtrTWithCatch

-- | print debug information about a `Solid` when it's evaluated 
-- exposes the properties of the underlying `OpenCascade.TopoDS.Shape`
debug :: Solid -> String
debug (Solid ptr _) = 
    let 
        fshow :: Show a => IO a -> IO String 
        fshow = fmap show
        actions = 
            [ ("type", fshow . TopoDS.Shape.shapeType)
            , ("closed", fshow . TopoDS.Shape.closed)
            , ("infinite", fshow . TopoDS.Shape.infinite)
            , ("orientable", fshow . TopoDS.Shape.orientable)
            , ("orientation", fshow . TopoDS.Shape.orientation)
            , ("null", fshow . TopoDS.Shape.isNull)
            , ("free", fshow . TopoDS.Shape.free)
            , ("locked", fshow . TopoDS.Shape.locked)
            , ("modified", fshow . TopoDS.Shape.modified)
            , ("checked",  fshow . TopoDS.Shape.checked)
            , ("convex", fshow . TopoDS.Shape.convex)
            , ("nbChildren", fshow . TopoDS.Shape.nbChildren)
            ]
    in unsafeFromAcquire $ do
        s <- acquireForeignPtr ptr
        liftIO $ (`foldMap` actions) $ \(actionName, value) -> 
                (return $ "\t" <> actionName <> "\t\t") <> value s <> (return "\n")

{--
-- TODO: this does not work, need to fix
everywhere :: Solid
everywhere = complement $ emptySolid
--}

-- | Invert a Solid, equivalent to `not` in boolean algebra.
--
-- The complement of a solid represents the solid with the same surface,
-- but where the opposite side of that surface is the \"inside\" of the solid.
--
-- Be warned that @complement emptySolid@ does not appear to work correctly.
complement :: Solid -> Solid
complement (Solid ptr paintMap) = (`Solid` paintMap) . unsafeFromAcquirePtr $ TopoDS.Shape.complemented =<< acquireForeignPtr ptr

-- | An empty solid
--
-- Be warned that @complement emptySolid@ does not appear to work correctly.
emptySolid :: Solid 
emptySolid =  (`Solid` emptyPaintMap) . unsafeFromAcquirePtr $ upcast <$> (MakeSolid.solid =<< MakeSolid.new)

-- defining the boolean CSG operators here, rather than in Waterfall.Booleans 
-- means that we can use them in typeclass instances without resorting to orphans


-- | this is used to abstract between `MakeShape` and `BOPAlgo.Builder`
data History = History 
    { historyModified :: Ptr TopoDS.Shape -> Acquire [Ptr TopoDS.Shape]
    , historyGenerated :: Ptr TopoDS.Shape -> Acquire [Ptr TopoDS.Shape]
    , historyIsDeleted :: Ptr TopoDS.Shape -> IO Bool
    , historyResult :: Acquire (Ptr TopoDS.Shape)
    }

makeShapeHistory :: Ptr MakeShape -> History
makeShapeHistory builder =
    History 
        (shapesFromListOfShape <=< MakeShape.modified builder)
        (shapesFromListOfShape <=< MakeShape.generated builder)
        (MakeShape.isDeleted builder)
        (MakeShape.shape builder)

bopBuilderHistory :: Ptr BOPAlgo.Builder.Builder -> History
bopBuilderHistory builder = 
    History 
        (shapesFromListOfShape <=< BOPAlgo.Builder.modified builder)
        (shapesFromListOfShape <=< BOPAlgo.Builder.generated builder)
        (BOPAlgo.Builder.isDeleted builder)
        (BOPAlgo.Builder.shape builder)

shapeDivideHistory :: Ptr ShapeUpgrade.ShapeDivide -> History
shapeDivideHistory shapeDivide = History 
    (\shape -> do 
        reshape <- ShapeBuild.ShapeDivide.getContext shapeDivide
        recorded <- liftIO $ ShapeBuild.ReShape.isRecorded reshape shape
        if not recorded 
            then return []
            else subShapesOf (Proxy @TopoDS.Face)
                =<< ShapeBuild.ReShape.apply reshape shape ShapeEnum.Face
    )
    (const . pure $ mempty)
    (const . pure $ False)
    (ShapeBuild.ShapeDivide.result shapeDivide)


-- | for a builder, and a shape, return:
--
-- * if the shape was modified by the builder, or there were generated shapes of the same kind, return the modified/generated shapes
-- * if the shape was deleted by the builder, return nothing
-- * otherwise, return the shape
remap :: History -> Ptr TopoDS.Shape -> Acquire [Ptr TopoDS.Shape]
remap history s = do
        modified <- historyModified history s
        generated <- historyGenerated history s
        inputShapeType <- liftIO $ TopoDS.Shape.shapeType s
        sameTypeGenerated <- liftIO $ 
            filterM (fmap (== inputShapeType) . TopoDS.Shape.shapeType) generated
        let candidates = modified <> sameTypeGenerated
        if not . null $ candidates
                then pure candidates
                else do
                    isDeleted <- liftIO $ historyIsDeleted history s
                    pure [s | not isDeleted]

fromAcquirePaints :: Acquire [(Ptr TopoDS.Face, Paint)] -> IO [(ForeignPtr TopoDS.Face, Paint)]
fromAcquirePaints paints = 
    let wrap = fmap (Compose . fmap Flip)
        unwrap = fmap runFlip . getCompose
    in fmap unwrap . fromAcquirePtrT . wrap  $ paints

remapPaints :: History -> [(Ptr TopoDS.Face, Paint)] -> IO [(ForeignPtr TopoDS.Face, Paint)]
remapPaints history facePaints = 
    let remapPaint (f, p) = fmap (,p) <$> (liftIO . traverse unsafeDowncast =<< remap history (upcast f))
        remappedPaints = concat <$> traverse remapPaint facePaints
    in fromAcquirePaints remappedPaints 

acquireFacePaints :: [(ForeignPtr TopoDS.Face, Paint)] -> Acquire [(Ptr TopoDS.Face, Paint)]
acquireFacePaints = 
    let wrap = Compose . fmap Flip
        unwrap = fmap runFlip . getCompose
    in fmap unwrap . traverse acquireForeignPtr . wrap

materialiseFacePaints :: Ptr TopoDS.Shape -> PaintMap -> Acquire [(Ptr TopoDS.Face, Paint)]
materialiseFacePaints _ (FacePaints facePaints) = acquireFacePaints facePaints 
materialiseFacePaints solid (UniformPaint paint) = do
    faces <- subShapes solid
    return $ fmap (, paint) faces

materialiseSolidFacePaints :: Solid -> Acquire [(Ptr TopoDS.Face, Paint)]
materialiseSolidFacePaints solid = do
    s <- acquireSolid solid
    materialiseFacePaints s (solidPaintMap solid)

mapPaintMap :: (PaintMap, History) -> Acquire (PaintMap, Ptr TopoDS.Shape.Shape)
mapPaintMap (paintMap, history) = do
    shape <- historyResult history

    remappedPaintMap <- case paintMap of
        UniformPaint _ -> pure paintMap
        FacePaints oldFacePaints -> 
            FacePaints <$> (liftIO . remapPaints history =<< acquireFacePaints oldFacePaints)
            
    return (remappedPaintMap, shape)


solidFromAcquireMappingPaintMap :: Acquire (PaintMap, History) -> Solid
solidFromAcquireMappingPaintMap f = 
    solidFromAcquireWithPaintMap (mapPaintMap =<< f)

solidFromAcquireMappingPaintMapWithCatch :: Acquire (PaintMap, History) -> Either WaterfallError Solid
solidFromAcquireMappingPaintMapWithCatch f = 
    solidFromAcquireWithPaintMapWithCatch (mapPaintMap =<< f)

toBoolean 
    :: (SubTypeOf MakeShape a
    , SubTypeOf BooleanOperation a
    , SubTypeOf BuilderAlgo a)
    => (Acquire (Ptr a)) -> Solid -> Solid -> Solid
toBoolean f (Solid ptrA paintMapA) (Solid ptrB paintMapB) = solidFromAcquireMappingPaintMap $ do
    a <- acquireForeignPtr ptrA
    b <- acquireForeignPtr ptrB
    builder <- f
    liftIO $ BuilderAlgo.setNonDestructive (upcast builder) True
    liftIO . BuilderAlgo.setArguments (upcast builder) =<< shapeSingletonList a
    liftIO . BooleanOperation.setTools (upcast builder) =<< shapeSingletonList b
    liftIO $ MakeShape.build (upcast builder)

    materialisedPaintMap <- case (paintMapA, paintMapB) of
        (UniformPaint paintA, UniformPaint paintB) | paintA == paintB -> pure paintMapA
        _ -> liftIO $ FacePaints <$> fromAcquirePaints (
                (<>) <$> materialiseFacePaints a paintMapA <*> materialiseFacePaints b paintMapB
            )

    return (materialisedPaintMap, makeShapeHistory . upcast $ builder)

-- | Take the sum of two solids
--
-- The region occupied by either one of them.
union3D :: Solid -> Solid -> Solid
union3D = toBoolean Fuse.new

matchesUniformPaint :: Paint -> PaintMap -> Bool
matchesUniformPaint pa (UniformPaint pb) = pa == pb
matchesUniformPaint _ _ = False

toBooleans :: BOPAlgo.Operation.Operation -> [Solid] -> Solid
toBooleans _ [] = emptySolid
toBooleans _ [x] = x
toBooleans op solids@(h:t) = solidFromAcquireMappingPaintMap $ do
    firstPtr <- acquireSolid $ h
    ptrs <- traverse acquireSolid t
    bop <- BOPAlgo.BOP.new
    let builder = upcast bop
    liftIO $ do
        BOPAlgo.Builder.setNonDestructive builder True
        BOPAlgo.BOP.setOperation bop op
        BOPAlgo.Builder.addArgument builder firstPtr
        traverse_ (BOPAlgo.BOP.addTool bop) ptrs
        BOPAlgo.Builder.setRunParallel builder True
        BOPAlgo.Builder.perform builder


    combinedPaintMap <- 
        case solidPaintMap h of
            UniformPaint p | all (matchesUniformPaint p . solidPaintMap) t -> 
                pure $ UniformPaint p
            _ -> liftIO $ FacePaints <$> fromAcquirePaints (concat <$> traverse materialiseSolidFacePaints solids)
            
    return  (combinedPaintMap, bopBuilderHistory builder)

-- | Take the sum of a list of solids 
-- 
-- May be more performant than chaining multiple applications of `union3D`
unions3D :: [Solid] -> Solid
unions3D = toBooleans BOPAlgo.Operation.Fuse 
    . filter (not . isEmptySolid)
    . deduplicateSolids

-- | Take the difference of two solids
-- 
-- The region occupied by the first, but not the second.
difference3D :: Solid -> Solid -> Solid
difference3D = toBoolean Cut.new

-- | Take the intersection of two solids 
--
-- The region occupied by both of them.
intersection3D :: Solid -> Solid -> Solid
intersection3D = toBoolean Common.new

deduplicateSolids :: [Solid] -> [Solid]
deduplicateSolids = deduplicateTopoDSShapes acquireSolid

isEmptySolid :: Solid -> Bool
isEmptySolid  = 
    not . unsafeFromAcquire . (containsShapeKind ShapeEnum.Face <=< acquireSolid)

intersections3D' :: [Solid] -> Solid
intersections3D' [] = emptySolid
intersections3D' [x] = x
intersections3D' solids@(h:t) 
    | any isEmptySolid solids = emptySolid
    | otherwise  = solidFromAcquireMappingPaintMap $ do
    builder <- CellsBuilder.new 
    liftIO $ BOPAlgo.Builder.setRunParallel (upcast builder) True
    liftIO $ BOPAlgo.Builder.setNonDestructive (upcast builder) True
    listToTake <- NCollection.List.newListOfShape
    shapes <- traverse acquireSolid solids
    forM_ shapes (liftIO . NCollection.List.appendListOfShape listToTake)
    listToAvoid <- NCollection.List.newListOfShape
    liftIO $ forM_ shapes (BOPAlgo.Builder.addArgument (upcast builder))
    liftIO (BOPAlgo.Builder.perform (upcast builder))
    liftIO $ CellsBuilder.addToResult builder listToTake listToAvoid 1 False
    liftIO $ CellsBuilder.removeInternalBoundaries builder

    combinedPaintMap <- 
        case solidPaintMap h of
            UniformPaint p | all (matchesUniformPaint p . solidPaintMap) t -> 
                pure $ UniformPaint p
            _ -> liftIO $ FacePaints <$> fromAcquirePaints (concat <$> traverse materialiseSolidFacePaints solids)
            
    return  (combinedPaintMap, bopBuilderHistory . upcast $ builder)

-- | Take the intersection of a list of solids 
-- 
-- May be more performant than chaining multiple applications of `intersection3D`
intersections3D :: [Solid] -> Solid
intersections3D = intersections3D' . deduplicateSolids

-- | While `Solid` could form a Semigroup via either `union3D` or `intersection3D`.
-- the default Semigroup is from `union3D`.
--
-- The Semigroup from `intersection3D` can be obtained using `Meet` from the lattices package.
instance Semigroup Solid where
    (<>) :: Solid -> Solid -> Solid
    (<>) = union3D

instance Monoid Solid where
    mempty = emptySolid
    mconcat = unions3D

instance Lattice Solid where 
    (/\) = intersection3D
    (\/) = union3D

instance BoundedJoinSemiLattice Solid where
    bottom = emptySolid

{--
-- TODO: because everywhere doesn't work correctly
-- using the BoundedMeetSemiLattice instance
-- and by extension, the Heyting instance
-- is liable to produce invalid shapes
instance BoundedMeetSemiLattice Solid where
    top = everywhere

-- every boolean algebra is a Heyting algebra with
--  a → b defined as ¬a ∨ b
instance Heyting Solid where
    neg = complement
    a ==> b = neg a \/ b
--}