packages feed

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

{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
module Waterfall.Internal.Diagram
( buildSolidDiagramFills
, silhouetteShape
) where
import Waterfall.Internal.Solid (PaintMap(..), materialiseFacePaints, paintMapIsEmpty)
import Waterfall.Paint (Paint)
import Waterfall.TwoD.Internal.Shape (Shape (..))
import Data.Acquire (Acquire)
import Foreign (Ptr)
import qualified OpenCascade.HLRBRep.Types as HLRBRep
import qualified OpenCascade.HLRBRep.TypeOfResultingEdge as HLRBRep
import qualified OpenCascade.TopoDS as TopoDS
import Waterfall.Internal.Edges (allEdges, buildEdgeCurve3D)
import Data.Traversable (forM)
import qualified OpenCascade.HLRBRep.HLRToShape as HLRBRep.HLRToShape
import qualified OpenCascade.Bnd.Box as Bnd.Box
import Data.Foldable (forM_, maximumBy)
import Control.Monad.IO.Class (liftIO)
import qualified OpenCascade.BRepBndLib as BRepBndLib
import qualified OpenCascade.BOPAlgo.Builder as BOPAlgo.Builder
import qualified OpenCascade.TopoDS.Builder as TopoDS.Builder
import qualified OpenCascade.TopoDS.Compound as TopoDS.Compound
import OpenCascade.Inheritance (upcast)
import Waterfall.Internal.FromOpenCascade (gpPntToV3, subShapes, subShapesOf)
import Control.Lens ((^.), (&), (.~), (<&>))
import Linear (V3 (..), _x, _y, _z, norm, unit, cross)
import qualified Data.IntMap as IntMap
import Data.Maybe (fromMaybe, catMaybes)
import qualified Waterfall.Internal.ShapeMap as ShapeMap
import qualified OpenCascade.BOPAlgo.Tools as BOPAlgo.Tools
import qualified OpenCascade.HLRAlgo as HLRAlgo
import qualified OpenCascade.HLRAlgo.Projector as Projector
import qualified OpenCascade.IntCurvesFace.ShapeIntersector as ShapeIntersector
import Control.Arrow (first)
import qualified OpenCascade.BRepMesh.IncrementalMesh as IncrementalMesh
import qualified OpenCascade.TopLoc.Location as TopLoc.Location
import qualified OpenCascade.BRep.Tool as BRep.Tool
import qualified OpenCascade.BRepGProp as BRepGProp
import qualified OpenCascade.GProp.GProps as GProps
import qualified OpenCascade.Poly.Triangulation as Poly.Triangulation
import qualified OpenCascade.Poly.Triangle as Poly.Triangle
import Control.Monad ((<=<), filterM, join)
import Waterfall.Internal.ToOpenCascade (v3ToPnt, v3ToDir)
import qualified OpenCascade.GP.Pnt as GP.Pnt
import qualified OpenCascade.GP.Lin as GP.Lin
import Waterfall.Internal.Finalizers (fromAcquirePtr, fromAcquirePtrT)
import qualified OpenCascade.TopoDS.Shape as TopoDS.Shape
import qualified Data.Map as M
import qualified OpenCascade.ShapeUpgrade.UnifySameDomain as UnifySameDomain
import qualified OpenCascade.HLRBRep.Algo as HLRBRep.Algo
import qualified OpenCascade.GP as GP
import qualified OpenCascade.HLRAlgo.Projector as HLRAlgo.Projector
import qualified OpenCascade.GP.Ax2 as GP.Ax2
import Data.Function (on)
import qualified Data.List.NonEmpty as NE
import Data.Proxy (Proxy(..))

boundaryEdges :: Ptr HLRBRep.HLRToShape -> Acquire [Ptr TopoDS.Edge]
boundaryEdges extractor = 
    fmap concat $ forM [HLRBRep.OutLine, HLRBRep.Sharp, HLRBRep.Rg1Line] $ \lt -> do
        compoundOfEdges <- HLRBRep.HLRToShape.compoundOfEdges 
            extractor lt True False
        traverse buildEdgeCurve3D =<< allEdges compoundOfEdges

calcFuzzyTolerance :: [Ptr TopoDS.Edge] -> Acquire Double
calcFuzzyTolerance edges = do
    theBox <- Bnd.Box.new
    forM_ edges $ \e -> do
        liftIO $ BRepBndLib.addOptimal (upcast e) theBox True False

    lo <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMin theBox
    hi <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMax theBox
    let d = hi - lo
    let maxDim = max (d ^. _x) (d ^. _y)
    return $ max 1e-6 (maxDim / 10000)

calcRayBound :: Ptr TopoDS.Shape -> Acquire Double
calcRayBound s = do
    theBox <- Bnd.Box.new
    liftIO $ BRepBndLib.addOptimal s theBox True False

    lo <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMin theBox
    hi <- liftIO . gpPntToV3 =<< Bnd.Box.cornerMax theBox
    return ( 2 * (norm lo + norm hi))

-- recursively drop edges with a free end
pruneDangling :: [Ptr TopoDS.Edge] -> Acquire [Ptr TopoDS.Edge]
pruneDangling edges = do
    withVertices <- forM edges $ \e -> do
        (e, ) <$> subShapesOf (Proxy @TopoDS.Vertex) (upcast e)
    let loop current = do
            vertexIndex <- liftIO $ ShapeMap.fromList (zip (concatMap snd current) [0 :: Int ..])
            labelled <- forM current $ \(e, vs) ->
                fmap (e, vs,) . liftIO $ traverse (fmap (fromMaybe (-1)) . ShapeMap.lookup vertexIndex) vs
            let weight labels = if length labels == 1 then 2 else 1 :: Int
                counts = IntMap.fromListWith (+) [(l, weight labels) | (_, _, labels) <- labelled, l <- labels]
                keep = [ (e, vs) | (e, vs, labels) <- labelled, all (\l -> IntMap.findWithDefault 0 l counts >= 2) labels ]
            if length keep == length current then pure (fst <$> keep) else loop keep
    loop withVertices

mergeRegions :: [Ptr TopoDS.Face] -> Acquire [Shape]
mergeRegions [] = pure []
mergeRegions [s] = liftIO $ pure . Shape <$> fromAcquirePtr (TopoDS.Shape.copy . upcast $ s)
mergeRegions regions = do
    builder <- TopoDS.Builder.new
    compound <- TopoDS.Compound.new
    liftIO $ TopoDS.Builder.makeCompound builder compound
    forM_ regions $ liftIO . TopoDS.Builder.add builder (upcast compound) . upcast
    unifier <- UnifySameDomain.fromShape (upcast compound) False True False
    liftIO $ UnifySameDomain.build unifier
    sameDomain <- UnifySameDomain.shape unifier
    liftIO . fmap (fmap Shape) . fromAcquirePtrT $ subShapesOf (Proxy @TopoDS.Face) sameDomain

faceArea :: Ptr TopoDS.Face -> Acquire Double
faceArea face = do
    props <- GProps.new
    liftIO $ BRepGProp.surfaceProperties (upcast face) props False False
    liftIO $ GProps.mass props

projectedRegions
    :: Ptr TopoDS.Shape 
    -> V3 Double 
    -> Ptr HLRAlgo.Projector 
    -> Ptr HLRBRep.HLRToShape
    -> Acquire [(Ptr TopoDS.Face, Maybe (Ptr TopoDS.Face))]
projectedRegions s projectionDir projector extractor = do
    boundary <- boundaryEdges extractor
    if null boundary 
        then return []
        else do
            tolerance <- calcFuzzyTolerance boundary 
            rayBound <- calcRayBound s
            let angularTolerance = 1e-8
            keptEdges <- case boundary of 
                [oneEdge] -> pure [oneEdge]
                _ -> do
                    fuser <- BOPAlgo.Builder.new 
                    liftIO $ BOPAlgo.Builder.setNonDestructive fuser True
                    liftIO $ BOPAlgo.Builder.setFuzzyValue fuser tolerance
                    forM_ boundary (liftIO . BOPAlgo.Builder.addArgument fuser . upcast)
                    liftIO $ BOPAlgo.Builder.perform fuser
                    pruneDangling 
                        =<< subShapes
                        =<< BOPAlgo.Builder.shape fuser

            builder <- TopoDS.Builder.new
            compound <- TopoDS.Compound.new
            liftIO $ TopoDS.Builder.makeCompound builder compound
            forM_ keptEdges $ liftIO . TopoDS.Builder.add builder (upcast compound) . upcast

            wires <- TopoDS.Shape.new
            status <- liftIO $ BOPAlgo.Tools.edgesToWires (upcast compound) wires True angularTolerance
            if status /= 0
                then pure [] 
                else do
                    faces <- TopoDS.Shape.new
                    ok <- liftIO $ BOPAlgo.Tools.wiresToFaces wires faces angularTolerance
                    if not ok
                        then return [] 
                        else do
                            regions <- subShapes faces

                            inv <- Projector.invertedTransformation projector
                            intersector <- ShapeIntersector.new
                            liftIO $ ShapeIntersector.load intersector s (tolerance/1000)

                            d <- v3ToDir projectionDir

                            forwardFacingRegions <- filterM (fmap (> 0) . faceArea) regions
                            forM forwardFacingRegions $ \region -> do
                                mesh <- IncrementalMesh.fromShapeAndLinDeflection (upcast region) tolerance
                                liftIO $ IncrementalMesh.perform mesh
                                loc <- TopLoc.Location.new
                                tri <- BRep.Tool.triangulation region loc
                                nullTriangulation <- liftIO $ Poly.Triangulation.isNull tri
                                nTri <- if nullTriangulation then pure 0 else liftIO $ Poly.Triangulation.nbTriangles tri

                                triangles <- forM [1..nTri] $ \i -> do 
                                    t <- Poly.Triangulation.triangle tri i
                                    indices <- traverse (liftIO . Poly.Triangle.value t) (V3 1 2 3)
                                    traverse (liftIO . gpPntToV3 <=< Poly.Triangulation.node tri) indices
                                let triangleArea (V3 a b c) = norm ((b - a) `cross` (c - a)) / 2
                                    triangleCenter corners = sum corners / 3
                                    candidatePoint = triangles
                                        & NE.nonEmpty
                                        & fmap (triangleCenter 
                                            . (maximumBy (compare `on` triangleArea)))
                                        & traverse . _z .~ 0

                                let lookupCandidateFace point = do
                                        pin <- v3ToPnt point
                                        p <- GP.Pnt.transformed pin inv
                                        line <- GP.Lin.new p d
                                        liftIO $ ShapeIntersector.perform intersector line (-rayBound) rayBound
                                        
                                        n <- liftIO $ ShapeIntersector.nbPnt intersector
                                        hits <- forM [1 .. n] $ \i -> (, i) <$> liftIO (ShapeIntersector.wParameter intersector i)
                                        forM (NE.nonEmpty hits) $ \h -> do
                                            let (_, nearest) = maximum h
                                            ShapeIntersector.face intersector nearest

                                faceMaybe <- fmap join . traverse lookupCandidateFace $ candidatePoint
                                return $ (region, faceMaybe)

silhouetteShape 
    :: Ptr TopoDS.Shape 
    -> Acquire [Shape]
silhouetteShape s = do
    algo <- HLRBRep.Algo.new
    liftIO $ HLRBRep.Algo.add algo s
    o <- GP.origin
    let projectionDir = unit _z
    projector <- HLRAlgo.Projector.fromAx2 =<< GP.Ax2.newAutoX o =<< v3ToDir projectionDir
    liftIO $ do 
        HLRBRep.Algo.projector algo projector
        HLRBRep.Algo.update algo
        HLRBRep.Algo.hide algo
    extractor <- HLRBRep.HLRToShape.fromAlgo algo
    regions <- projectedRegions s projectionDir projector extractor
    let hitRegions = fmap fst . catMaybes . fmap sequence $ regions
    mergeRegions hitRegions

buildSolidDiagramFills' 
    :: Ptr TopoDS.Shape
    -> V3 Double 
    -> Ptr HLRAlgo.Projector
    -> Ptr HLRBRep.HLRToShape
    -> [(Ptr TopoDS.Face, Paint)]
    -> Acquire [(Paint, Shape)]
buildSolidDiagramFills' s projectionDir projector extractor paints = do
    paintMap <- liftIO $ ShapeMap.fromList (first upcast <$> paints)
    regions <- projectedRegions s projectionDir projector extractor
    let hitRegions = catMaybes . fmap sequence $ regions
    paintRegionMap <- fmap (M.fromListWith (<>) . catMaybes)
        $ forM hitRegions $ \(region, hit) -> do
            paint <- liftIO $ ShapeMap.lookup paintMap (upcast hit)
            return $ (, [region]) <$> paint

    traverse mergeRegions paintRegionMap
        <&> foldMap sequence . M.toList

buildSolidDiagramFills
    :: PaintMap 
    -> Ptr TopoDS.Shape 
    -> V3 Double
    -> Ptr HLRAlgo.Projector
    -> Ptr HLRBRep.HLRToShape
    -> Acquire [(Paint, Shape)]
buildSolidDiagramFills paints s d projector extractor
    | paintMapIsEmpty paints = pure []
    | otherwise =    
        buildSolidDiagramFills' s d projector extractor
        =<< materialiseFacePaints s paints