packages feed

waterfall-cad-examples-0.9.0.0: test/DiagramTests.hs

module DiagramTests
( diagramTests
) where

import qualified Waterfall.Diagram as Diagram
import qualified Waterfall.Solid as Solid
import qualified Waterfall.Paint as Paint
import qualified Waterfall.Colour as Colour
import qualified Waterfall.TwoD.Shape as Shape
import qualified Waterfall.TwoD.Path2D as TwoD.Path2D
import qualified Waterfall.TwoD.Transforms as TwoD.Transforms
import qualified Waterfall.TwoD.BoundingBox.AxisAligned as TwoD.AABB
import qualified Waterfall.TwoD.BoundingBox.Oriented as TwoD.OBB
import Linear (V2 (..), V3 (..), zero, distance, norm, angle, crossZ)
import Data.Function ((&))
import Data.Maybe (isNothing)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit
import Assertions

diagramTests :: TestTree
diagramTests = testGroup "Diagram"
    [ testGroup "axisAlignedBoundingBox2D"
        [ testCase "empty diagram has no bounding box" $
            assertEqual "" Nothing (TwoD.AABB.axisAlignedBoundingBox2D (mempty :: Diagram.Diagram))

        , testCase "two fills" $
            case TwoD.AABB.axisAlignedBoundingBox2D twoSquares of
                Nothing -> assertFailure "expected a bounding box"
                Just (lo, hi) ->
                    assertApproxEqual "lo is the origin" 1e-6 0 (distance lo zero)
                        <> assertApproxEqual "hi is correct" 1e-6 0 (distance hi (V2 11 1))

        , testCase "fill and line" $
            let diagram = fill Shape.unitSquare <> outline (TwoD.Path2D.line (V2 (-1) 0) (V2 0 3))
            in case TwoD.AABB.axisAlignedBoundingBox2D diagram of
                Nothing -> assertFailure "expected a bounding box"
                Just (lo, hi) ->
                    assertApproxEqual "lo is correct" 1e-6 0 (distance lo (V2 (-1) 0))
                        <> assertApproxEqual "hi is correct" 1e-6 0 (distance hi (V2 1 3))

        , testCase "diagram of unit cube from above" $
            case TwoD.AABB.axisAlignedBoundingBox2D (Diagram.solidDiagram (V3 0 0 1) Solid.unitCube) of
                Nothing -> assertFailure "expected a bounding box"
                Just (lo, hi) ->
                    assertApproxEqual "is a unit square" 1e-6 0 (distance (hi - lo) (V2 1 1))
        ]
    , testGroup "orientedBoundingBox2D"
        [ testCase "empty diagram has no bounding box" $
            assertBool "" (isNothing (TwoD.OBB.orientedBoundingBox2D (mempty :: Diagram.Diagram)))

        , testCase "two fills" $
            case TwoD.OBB.orientedBoundingBox2D twoSquares of
                Nothing -> assertFailure "expected a bounding box"
                Just obb ->
                    assertApproxEqual "center is correct" 1e-6 0 (distance (TwoD.OBB.obb2DCenter obb) (V2 5.5 0.5))
                        <> assertApproxEqual "longer side is correct" 1e-5 5.5 (norm (TwoD.OBB.obb2DSideX obb))
                        <> assertApproxEqual "shorter side is correct" 1e-5 0.5 (norm (TwoD.OBB.obb2DSideY obb))

        , testCase "diagonal line" $
            let diagram = outline (TwoD.Path2D.line zero (V2 2 1))
            in case TwoD.OBB.orientedBoundingBox2D diagram of
                Nothing -> assertFailure "expected a bounding box"
                Just obb ->
                    assertApproxEqual "center is the midpoint" 1e-6 0 (distance (TwoD.OBB.obb2DCenter obb) (V2 1 0.5))
                        <> assertApproxEqual "longer side is half the line" 1e-5 (sqrt 5 / 2) (norm (TwoD.OBB.obb2DSideX obb))
                        <> assertApproxEqual "shorter side has no length" 1e-5 0 (norm (TwoD.OBB.obb2DSideY obb))

        , testCase "rotated fill" $
            let diagram = Shape.centeredSquare
                    & TwoD.Transforms.scale2D (V2 3 1)
                    & fill
                    & TwoD.Transforms.rotate2D (pi/6)
                    & TwoD.Transforms.translate2D (V2 4 5)
            in case TwoD.OBB.orientedBoundingBox2D diagram of
                Nothing -> assertFailure "expected a bounding box"
                Just obb ->
                    assertApproxEqual "center is correct" 1e-6 0 (distance (TwoD.OBB.obb2DCenter obb) (V2 4 5))
                        <> assertApproxEqual "longer side is correct" 1e-5 1.5 (norm (TwoD.OBB.obb2DSideX obb))
                        <> assertApproxEqual "shorter side is correct" 1e-5 0.5 (norm (TwoD.OBB.obb2DSideY obb))
                        <> assertApproxEqual "longer side follows the rotation" 1e-5 0 (crossZ (TwoD.OBB.obb2DSideX obb) (angle (pi/6)))
        ]
    ]

fill :: Shape.Shape -> Diagram.Diagram
fill = Diagram.shapeDiagram (Paint.paintWithColour Colour.red)

outline :: TwoD.Path2D.Path2D -> Diagram.Diagram
outline = Diagram.pathDiagram Diagram.OutLine Diagram.Visible

twoSquares :: Diagram.Diagram
twoSquares = fill Shape.unitSquare <> fill (TwoD.Transforms.translate2D (V2 10 0) Shape.unitSquare)