{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
module DiagramGoldenTests
( diagramGoldenTests
) where
import Test.Tasty (TestTree, TestName, testGroup)
import Test.Tasty.Golden.Advanced (goldenTest)
import Text.XML.Light as XML
import DarkModeSVG (darkModeSVG)
import qualified Graphics.Svg as Svg
import Waterfall.Diagram (Diagram)
import qualified Waterfall.Diagram as Diagram
import qualified Waterfall.TwoD.Transforms as TwoD.Transforms
import Waterfall.Solid (Solid)
import qualified Waterfall.Solid as Solid
import qualified Waterfall.Transforms as Transforms
import qualified Waterfall.Offset as Offset
import qualified Waterfall.Booleans as Booleans
import qualified Waterfall.Path as Path
import qualified Waterfall.TwoD.Path2D as Path2D
import Waterfall.TwoD.Shape (Shape)
import qualified Waterfall.TwoD.Shape as Shape
import qualified Waterfall.Colour as Colour
import qualified Waterfall.Sweep as Sweep
import qualified Waterfall.Loft as Loft
import qualified Waterfall.Fillet as Fillet
import qualified Waterfall.Revolution as Revolution
import qualified Waterfall.BoundingBox.AxisAligned as AABB
import qualified Waterfall.TwoD.BoundingBox.AxisAligned as TwoD.AABB
import qualified Waterfall.TwoD.BoundingBox.Oriented as TwoD.OBB
import qualified Waterfall.SVG as SVG
import Linear
import qualified CsgExample
import Control.Monad.Except (runExceptT, liftEither)
import Control.Exception (throwIO, ErrorCall (..))
import qualified Data.ByteString.Char8 as BSC8
import qualified Graphics.Rasterific.Svg as RSvg
import qualified Graphics.Text.TrueType as FF
import Control.Monad.IO.Class (liftIO)
import Control.Monad (unless, join)
import qualified Codec.Picture as JP
import Control.Lens
import Data.Function (on)
import Data.Monoid (Sum (getSum))
import System.FilePath (takeBaseName, (</>))
import qualified FilletExample
import qualified GearExample
import qualified RevolutionExample
import qualified SweepExample
import qualified OffsetExample
import qualified TextExample
import qualified BoundingBoxExample
import qualified LoftExample
import qualified TwoDBooleansExample
import qualified PlatonicSolidsExample
import qualified TakePathFractionExample
import qualified Paths_waterfall_cad_examples as Paths
import qualified FlagExample
import qualified LoosePartsExample
import qualified Waterfall.Paint as Paint
import qualified Waterfall as Shapes
import qualified Waterfall.TwoD.Offset as TwoD.Offset
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
xmlToSvg :: String -> XML.Element -> Either String Svg.Document
xmlToSvg fileDesc = maybe (Left $ "failed to parse " <> fileDesc) Right . Svg.parseSvgFile "file.svg" . BSC8.pack . XML.showTopElement
-- this is unlawful - it assumes the two images have the same size
zippingTraversal :: JP.Pixel px => Traversal' (JP.Image px, JP.Image px) (px, px)
zippingTraversal =
alongside (partsOf JP.imagePixels) (partsOf JP.imagePixels)
. iso (uncurry zip) unzip
. traverse
pixelDiffThreshold :: Integer
pixelDiffThreshold = 16
pixelCountThreshold :: Integer
pixelCountThreshold = 128
comparePixels :: JP.PixelRGBA8 -> JP.PixelRGBA8 -> Bool
comparePixels (JP.PixelRGBA8 r1 g1 b1 a1) (JP.PixelRGBA8 r2 g2 b2 a2) =
let c x y = abs (fromIntegral x - fromIntegral y)
in (c r1 r2 + c g1 g2 + c b1 b2 + c a1 a2 ) < pixelDiffThreshold
size :: JP.Image px -> (Int, Int)
size a = (JP.imageWidth a, JP.imageHeight a)
similarSizes :: JP.Image px -> JP.Image px -> Bool
similarSizes a b =
let within1 x y = abs (x - y) <= 1
in (within1 `on` JP.imageWidth) a b
&& (within1 `on` JP.imageHeight) a b
render :: Svg.Document -> IO (JP.Image JP.PixelRGBA8, RSvg.LoadedElements)
render doc =
let roundSize (Just (Svg.Num w, Svg.Num h)) = Just (round w, round h)
roundSize _ = Nothing
-- setting an explicit size helps work around floating point rounding issues
-- which were causing the tests to behave differently on intel/arm
docSize = roundSize ((,) <$> doc ^. Svg.width <*> doc ^. Svg.height)
in RSvg.renderSvgDocument
FF.emptyFontCache
docSize
72 -- DPI, a so-so DPI for screens, higher would just mean making the test less sensitive
doc
countMismatchedPixel :: (JP.PixelRGBA8, JP.PixelRGBA8) -> Sum Integer
countMismatchedPixel (x, y) = if comparePixels x y then 0 else 1
colourMismatchedPixel :: (JP.PixelRGBA8, JP.PixelRGBA8) -> JP.PixelRGBA8
colourMismatchedPixel (x, y) = if comparePixels x y then x else JP.PixelRGBA8 255 0 0 255
dup :: a -> (a, a)
dup = join (,)
cropToSmallest :: JP.Pixel a => JP.Image a -> JP.Image a -> (JP.Image a, JP.Image a)
cropToSmallest a b =
if size a == size b
then (a, b)
else
let w = (min `on` JP.imageWidth) a b
h = (min `on` JP.imageHeight) a b
crop img = JP.generateImage (JP.pixelAt img) w h
in (crop a, crop b)
compareOutput :: FilePath -> XML.Element -> XML.Element -> IO (Maybe String)
compareOutput inputPath expected actual =
let extract (Left s) = Just s
extract (Right ()) = Nothing
in fmap extract . runExceptT $ do
expectedSvg <- liftEither $ xmlToSvg "expected" expected
actualSvg <- liftEither $ xmlToSvg "actual" actual
(expectedRendered, _) <- liftIO $ render expectedSvg
(actualRendered, _) <- liftIO $ render actualSvg
projectRoot <- liftIO Paths.getDataDir
let makePath kind = projectRoot </> "test-results" </> takeBaseName inputPath <> "." <> kind <> ".png"
let expectedPath = makePath "expected"
let diffPath = makePath "diff"
let actualPath = makePath "actual"
unless (similarSizes expectedRendered actualRendered) $ do
liftIO $ JP.writePng expectedPath expectedRendered
liftIO $ JP.writePng actualPath actualRendered
liftEither . Left $
( "incompatible sizes, expected: "
<> show (size expectedRendered)
<> ", actual "
<> show (size actualRendered)
<> ", actual written to "
<> actualPath)
let (expectedRenderedCropped, actualRenderedCropped) = cropToSmallest expectedRendered actualRendered
(width, height) = size expectedRenderedCropped
let mismatchedCount = getSum $
foldMapOf
zippingTraversal
countMismatchedPixel
(expectedRenderedCropped, actualRenderedCropped)
unless (mismatchedCount < pixelCountThreshold) $ do
let totalPixels = width * height
let diffImage =
(expectedRenderedCropped, actualRenderedCropped)
& zippingTraversal %~ (dup . colourMismatchedPixel)
& (^. _1)
liftIO $ JP.writePng expectedPath expectedRendered
liftIO $ JP.writePng diffPath diffImage
liftIO $ JP.writePng actualPath actualRendered
liftEither . Left $
( show mismatchedCount <> "/" <> show totalPixels
<> " pixels didn't match, diff written to "
<> diffPath
<> " actual written to "
<> actualPath
)
doTest :: TestName -> FilePath -> IO Diagram -> TestTree
doTest testName goldenPath makeDiagram =
let qualifyGoldenPath = (</> "test-data" </> goldenPath) <$> Paths.getDataDir
readGoldenFile = maybe
(throwIO . ErrorCall $ "failed to read " <> goldenPath)
pure
=<< fmap parseXMLDoc . readFile =<< qualifyGoldenPath
-- most paths are relative to Paths_waterfall_cad_examples.getDataDir
-- however when updating the file, we want to modify the source file
-- therefore this is sensitive to the cwd
updateGoldenFile = writeFile ("test-data" </> goldenPath) . ppTopElement
in goldenTest
testName
readGoldenFile
(darkModeSVG <$> makeDiagram)
(\a b -> do
path <- qualifyGoldenPath
compareOutput path a b
)
updateGoldenFile
galleryGoldenTest :: GoldenSpec -> TestTree
galleryGoldenTest spec =
let goldenPath = "README.md"
qualifyGoldenPath = (</> "test-data" </> goldenPath) <$> Paths.getDataDir
readGoldenFile = T.readFile =<< qualifyGoldenPath
updateGoldenFile = T.writeFile ("test-data" </> goldenPath)
in goldenTest
"Golden README.md"
readGoldenFile
(pure . printGoldenSpec $ spec)
(\a b -> if a == b
then pure Nothing
else pure . Just $ "test-data/README.md needs negenerating"
)
updateGoldenFile
standardSolidDiagram :: Solid -> Diagram
standardSolidDiagram = withWidth 800 . Diagram.solidDiagram (V3 2 3 1)
normalizeSize :: (V2 Double -> Double) -> Double -> Diagram -> Diagram
normalizeSize getter target d =
case TwoD.AABB.axisAlignedBoundingBox2D d of
Just (lo, hi) ->
let s = getter (hi - lo)
in TwoD.Transforms.uScale2D (target / s) d
Nothing -> d
withHeight :: Double -> Diagram -> Diagram
withHeight = normalizeSize (^. _y)
withWidth :: Double -> Diagram -> Diagram
withWidth = normalizeSize (^. _x)
diagramTest :: TestName -> FilePath -> Diagram -> GoldenSpec
diagramTest name filepath =
GoldenCase name filepath . pure
shapeTest :: TestName -> FilePath -> Shape -> GoldenSpec
shapeTest name filepath =
diagramTest name filepath
. foldMap (Diagram.pathDiagram Diagram.OutLine Diagram.Visible)
. Shape.shapePaths
. TwoD.Transforms.uScale2D 100
solidTest :: TestName -> FilePath -> Solid -> GoldenSpec
solidTest name filepath =
diagramTest name filepath . standardSolidDiagram
smallSolidTest :: TestName -> FilePath -> Solid -> GoldenSpec
smallSolidTest name filepath =
diagramTest name filepath . withHeight 200 . standardSolidDiagram
svgPathsTest :: TestName -> FilePath -> Either SVG.SVGError [Path2D.Path2D] -> GoldenSpec
svgPathsTest name filepath =
GoldenCase name filepath
. either
(throwIO . ErrorCall . show)
(pure . foldMap (Diagram.pathDiagram Diagram.OutLine Diagram.Visible))
svgPathTest :: TestName -> FilePath -> String -> GoldenSpec
svgPathTest name filepath =
svgPathsTest name filepath . SVG.parsePath
svgElementsTest :: TestName -> FilePath -> String -> GoldenSpec
svgElementsTest name filepath elements =
let document = "<svg xmlns=\"http://www.w3.org/2000/svg\">" <> elements <> "</svg>"
parsed = maybe
(Left (SVG.SVGError SVG.SVGTreeError "failed to parse test document"))
Right
(Svg.parseSvgFile "test.svg" (BSC8.pack document))
in svgPathsTest name filepath (SVG.convertDocument =<< parsed)
data GoldenSpec
= GoldenGroup TestName [GoldenSpec]
| GoldenCase TestName FilePath (IO Diagram)
evalGoldenSpec :: GoldenSpec -> TestTree
evalGoldenSpec (GoldenGroup name children) = testGroup name (evalGoldenSpec <$> children)
evalGoldenSpec (GoldenCase testName filePath action) = doTest testName filePath action
printGoldenSpec :: GoldenSpec -> Text
printGoldenSpec s =
let go level (GoldenGroup name children) =
T.replicate level "#" <> " " <> T.pack name <> "\n"
<> T.unlines (go (level+1) <$> children)
go _ (GoldenCase testName filePath _) =
"#### " <> T.pack testName <> "\n"
<> ""
in go 1 s
diagramGoldenTests :: TestTree
diagramGoldenTests =
testGroup "Golden Tests"
[ evalGoldenSpec goldenSpec
, galleryGoldenTest goldenSpec
]
goldenSpec :: GoldenSpec
goldenSpec = GoldenGroup "Diagram Golden Tests"
[ GoldenGroup "Example Objects"
[ smallSolidTest "CSG" "csg.svg" CsgExample.csgExample
, solidTest "Gear" "gear.svg" $ GearExample.gearExample 1 5 20 (20*pi/180)
, smallSolidTest "Revolution" "revolution.svg" RevolutionExample.revolutionExample
, smallSolidTest "Sweep" "sweep.svg" SweepExample.sweepExample
, solidTest "Offset" "offset.svg" OffsetExample.offsetExample
, GoldenCase "Text" "text.svg" $ do
projectRoot <- Paths.getDataDir
standardSolidDiagram <$> TextExample.textExample
(projectRoot </> "test-data/fonts/varela/VarelaRound-Regular.ttf")
12.0
"Waterfall-CAD"
10
, smallSolidTest "Bound" "bounding-boxes.svg" BoundingBoxExample.boundingBoxExample
, solidTest "Loft" "loft.svg" LoftExample.loftExample
, solidTest "Fillet" "fillet.svg" FilletExample.filletExample
, smallSolidTest "2D Booleans" "2d-booleans.svg" TwoDBooleansExample.twoDBooleansExample
, solidTest "Platonic Solids" "platonic.svg" PlatonicSolidsExample.platonicSolidsExample
, solidTest "Take Path Fraction" "takePathFraction.svg" TakePathFractionExample.takePathFractionExample
, solidTest "Flags" "flags.svg" FlagExample.flagExample
, smallSolidTest "Loose Parts" "loose-parts.svg" LoosePartsExample.loosePartsExample
]
, GoldenGroup "Transforms"
[ GoldenGroup "Three-D"
[ solidTest "Negative Scaled Cube" "negativeScaledCube.svg"
(Transforms.scale (V3 (negate 1) 1 1) Solid.unitCube)
, solidTest "Negative Scaled Cube II" "negativeScaledCube-II.svg"
(Transforms.scale (negate $ V3 1 1 1) Solid.unitCube)
, solidTest "Negative Scaled Cube III" "negativeScaledCube-III.svg"
(Transforms.scale (V3 (negate 10) (negate 10) 10) Solid.unitCube)
, solidTest "Negative Scaled Cuboid" "negativeScaledCuboid.svg"
(Transforms.scale (V3 (negate 1) 2 3) Solid.unitCube)
, solidTest "scale near unity" "nearUnityScale.svg"
. Solid.colourSolid Colour.red $
(Transforms.scale (V3 0.9999999999999998 1 1) Solid.unitCylinder
<> Transforms.translate (V3 0 0 0.5) Solid.unitCube
)
, solidTest "orthogonal matrix" "orthogonalMatrix.svg"
. Solid.colourSolid Colour.red $
-- rotation by 1 radian around the -y axis
( Transforms.matTransform
(V3 (V4 (sin 1) 0 (cos 1) 0)
(V4 0 1 0 0)
(V4 (cos 1) 0 (-sin 1) 0))
Solid.unitCylinder
<> Transforms.translate (V3 0 0 0.5) Solid.unitCube
)
]
, GoldenGroup "Two-D"
[ shapeTest "scale near unity" "nearUnityScaleTwoD.svg"
( TwoD.Transforms.scale2D (V2 0.9999999999999998 1) Shape.unitCircle
<> TwoD.Transforms.translate2D (V2 0 0.5) Shape.unitSquare
)
, shapeTest "orthogonal matrix" "orthogonalMatrixTwoD.svg"
( TwoD.Transforms.matTransform2D
(V2 (V3 (sin 1) (cos 1) 0)
(V3 (cos 1) (-sin 1) 0)
)
Shape.unitSquare
<> TwoD.Transforms.translate2D (V2 0 0.5) Shape.unitSquare
)
, shapeTest "negative determinant GTrsf" "negativeDeterminantGTrsfTwoD.svg"
( TwoD.Transforms.matTransform2D (V2 (V3 1 1 0) (V3 1 0 0)) Shape.centeredSquare
<> TwoD.Transforms.translate2D (V2 0.5 0) Shape.centeredSquare
)
, shapeTest "negative non-uniform scale" "negativeNonUniformScaleTwoD.svg"
( TwoD.Transforms.scale2D (V2 2 (-1)) Shape.unitSquare
<> TwoD.Transforms.translate2D (V2 1.5 0) Shape.centeredSquare
)
, shapeTest "negative uniform scale" "negativeUniformScaleTwoD.svg"
( TwoD.Transforms.scale2D (V2 (-1) (-1)) Shape.unitSquare
<> TwoD.Transforms.translate2D (V2 (-1) 0) Shape.centeredSquare
)
, shapeTest "negative non-uniform scale with hole" "negativeNonUniformScaleWithHoleTwoD.svg"
( Shape.centeredSquare
& TwoD.Transforms.uScale2D 2.5
& (`Booleans.difference` Shape.unitCircle)
& TwoD.Transforms.scale2D (V2 (-1) 1.2)
& (<> Shape.centeredSquare)
)
, shapeTest "negative uniform scale with hole" "negativeUniformScaleWithHoleTwoD.svg"
( Shape.centeredSquare
& TwoD.Transforms.uScale2D 2.5
& (`Booleans.difference` Shape.unitCircle)
& TwoD.Transforms.scale2D (V2 (-1) 1)
& (<> Shape.centeredSquare)
)
, shapeTest "negative matTransform with hole" "negativeMatTransformWithHoleTwoD.svg"
( Shape.centeredSquare
& TwoD.Transforms.uScale2D 2.5
& (`Booleans.difference` Shape.unitCircle)
& TwoD.Transforms.matTransform2D (V2 (V3 1 1 0) (V3 1 0 0))
& (<> TwoD.Transforms.uScale2D 0.5 Shape.centeredSquare)
)
]
]
, GoldenGroup "Sweeps"
[ solidTest "curvy sweep" "curvySweep.svg"
( Path2D.bezier (V2 0 0) (V2 0 10) (V2 10 10) (V2 10 0)
& Path.fromPath2D
& (`Sweep.sweep` Shape.unitCircle)
)
, solidTest "curvy sweep two circles" "curvySweepTwoCircles.svg"
( let profile = mconcat
[ TwoD.Transforms.translate2D (unit _x ^* i) Shape.unitCircle
| i <- [-1.5, 1.5]
]
in Path2D.bezier (V2 0 0) (V2 0 10) (V2 10 10) (V2 10 0)
& Path.fromPath2D
& (`Sweep.sweep` profile)
)
, solidTest "curvy sweep concentric squares" "curvySweepConcentricSquares.svg"
( let profile =
( TwoD.Transforms.uScale2D 2 Shape.centeredSquare
`Booleans.difference`
TwoD.Transforms.uScale2D 1.5 Shape.centeredSquare
) <> Shape.centeredSquare
in Path2D.bezier (V2 0 0) (V2 0 (-10)) (V2 10 (-10)) (V2 10 0)
& Path.fromPath2D
& (`Sweep.sweep` profile)
)
, solidTest "sweep squares" "sweepSquare.svg"
( let path = Path.fromPath2D . mconcat . Shape.shapePaths $ Shape.unitSquare
profile = TwoD.Transforms.uScale2D 0.2 Shape.unitCircle
in Sweep.sweep path profile
)
, solidTest "curvy sweep of offset shape" "curvySweepOffsetShape.svg"
( Path2D.bezier (V2 0 0) (V2 0 (-10)) (V2 10 (-10)) (V2 10 0)
& Path.fromPath2D
& (`Sweep.sweep`
TwoD.Transforms.rotate2D pi
(TwoD.Offset.offsetShape 0.5 (Shape.unitPolygon 3)))
)
]
, GoldenGroup "Lofts"
[ solidTest "loft squares" "loftSquares.svg"
( Loft.loft
[ Path.fromPath2D $ mconcat $ Shape.shapePaths Shape.unitSquare
, Transforms.translate (V3 0 0 10) $ Path.fromPath2D $ mconcat $ Shape.shapePaths Shape.unitSquare
]
)
]
, GoldenGroup "Loose Parts and Shapes"
[ solidTest "looseParts with Colour then transform" "loosePartsThenTransform.svg" $
let a = Solid.centeredCube
& Solid.colourSolid Colour.maroon
& Transforms.translate (unit _x)
b = Solid.centeredCube
& Solid.colourSolid Colour.navy
& Transforms.translate (unit _x ^* (-1))
separator = Solid.centeredCube
& Solid.colourSolid Colour.lime
& Transforms.scale (V3 5 0.1 5)
in ((a <> b) `Booleans.difference` separator)
& Solid.looseParts
& fmap (Transforms.rotate (unit _z) (pi/2))
& take 3
& mconcat
, diagramTest "looseShapes diagram" "looseShapesDiagram.svg"
( Shape.unitCircle
& (`Booleans.difference` TwoD.Transforms.scale2D (V2 0.1 3) Shape.centeredSquare)
& (`Booleans.difference` TwoD.Transforms.scale2D (V2 3 0.1) Shape.centeredSquare)
& Shape.looseShapes
& zipWith TwoD.Transforms.uScale2D [1, 1.05 ..]
& zipWith Diagram.shapeDiagram
(Paint.paintWithColour <$> [Colour.red, Colour.blue, Colour.green, Colour.yellow])
& mconcat
& TwoD.Transforms.scale2D 50
)
]
, GoldenGroup "Sections"
[ diagramTest "sphere section" "sphereSection.svg" (
Solid.unitSphere
& Shape.section
& Diagram.shapeDiagram (Paint.paintWithColour Colour.red)
& TwoD.Transforms.scale2D 50
)
, diagramTest "compound section" "compoundSection.svg" (
Solid.unitSphere
& (`Booleans.difference` (Solid.unitSphere & Transforms.uScale 0.4))
& (`Booleans.difference` (Solid.centeredCube & Transforms.translate (unit _x)))
& Booleans.union (Solid.centeredCube
& Transforms.uScale 0.8
& Transforms.translate (unit _x)
)
& Shape.section
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
]
, GoldenGroup "Silhouettes"
[ diagramTest "sphere silhouette" "sphereSilhouette.svg" (
Solid.unitSphere
& Shape.silhouette
& Diagram.shapeDiagram (Paint.paintWithColour Colour.fuchsia)
& TwoD.Transforms.scale2D 50
)
, diagramTest "torus silhouette" "torusSilhouette.svg" (
Solid.torus 2 1
& Shape.silhouette
& Diagram.shapeDiagram (Paint.paintWithColour Colour.yellow)
& TwoD.Transforms.scale2D 50
)
, diagramTest "compound silhouette" "compoundSilhouette.svg" (
Solid.unitSphere
& (`Booleans.difference` (Solid.unitSphere & Transforms.uScale 0.4))
& (`Booleans.difference` (Solid.centeredCube & Transforms.translate (unit _x)))
& Booleans.union (Solid.centeredCube
& Transforms.uScale 0.8
& Transforms.translate (unit _x)
)
& Shape.silhouette
& Diagram.shapeDiagram (Paint.paintWithColour Colour.aqua)
& TwoD.Transforms.scale2D 50
)
]
, GoldenGroup "Painted Primitives"
[ solidTest "painted sphere" "paintedSphere.svg" (
Solid.unitSphere
& Solid.colourSolid Colour.blue
)
]
, GoldenGroup "Fillets"
[ solidTest "noop fillet cube" "noopFilletCube.svg" (
Solid.unitCube
& Fillet.roundConditionalFillet (const Nothing)
& Solid.colourSolid Colour.blue
)
, solidTest "fillet cone" "filletCone.svg" (
Solid.unitCone
& Fillet.roundFillet 0.1
& Solid.colourSolid Colour.aqua
)
]
, GoldenGroup "Offsets"
[ GoldenGroup "Three-D"
[ solidTest "Offset of Compound" "offsetOfCompound.svg"
(Offset.offset 1 $ (id <> Transforms.translate (unit _x ^* 3)) Solid.unitCube)
, solidTest "offset hole" "offsetHole.svg" (
let s = (Solid.centeredCube & Transforms.uScale 3 & Solid.colourSolid Colour.aqua)
`Booleans.difference` (Solid.centeredCube & Solid.colourSolid Colour.fuchsia)
mask x = x `Booleans.difference` (
Solid.centeredCube
& Transforms.translate (unit _y * 0.5)
& Transforms.uScale 4
& Solid.colourSolid Colour.lime
)
s' = Offset.offset 0.2 s
s'' = Offset.offset (-0.2) (s)
in s'
<> Transforms.translate (unit _x ^* 4) (mask s')
<> Transforms.translate (unit _z ^* 4) s''
<> Transforms.translate ((unit _x + unit _z) ^* 4) (mask s'')
)
, solidTest "offset of scaled cylinder" "offsetOfScaledCylinder.svg" (
Solid.unitCylinder
& Transforms.scale (V3 1 1 3)
& Offset.offset 0.5
)
, solidTest "offset of through hole cylinder" "offsetOfThroughHole.svg" (
let a = Solid.centeredCube
& Transforms.scale (V3 3 3 0.5)
& Solid.colourSolid Colour.blue
b = Solid.centeredCylinder
& Transforms.scale (V3 1 1 3)
& Solid.colourSolid Colour.red
in Offset.offsetWithPaint (Paint.mixPaints) 0.25 (a `Booleans.difference` b)
& Transforms.rotate (unit _x) (pi/2)
)
]
, GoldenGroup "Two-D" $
let curvedPath :: Path2D.Path2D
curvedPath = Path2D.pathFrom (V2 (-1) 0)
[ Path2D.bezierTo (V2 (-1) 1) (V2 (-0.5) 1.5) (V2 0 1)
, Path2D.bezierTo (V2 0.5 0.5) (V2 1 1) (V2 1 0)
, Path2D.bezierTo (V2 1 (-1)) (V2 0.5 (-1.2)) (V2 0 (-1))
, Path2D.bezierTo (V2 (-0.5) (-0.8)) (V2 (-1) (-1)) (V2 (-1) 0)
]
squareWithSquareHole :: Shape
squareWithSquareHole =
TwoD.Transforms.uScale2D 2.2 Shapes.centeredSquare
`Booleans.difference` TwoD.Transforms.uScale2D 0.6 Shapes.centeredSquare
ringWithIsland :: Shape
ringWithIsland =
(TwoD.Transforms.uScale2D 3 Shapes.centeredSquare
`Booleans.difference` TwoD.Transforms.uScale2D 1.6 Shapes.centeredSquare)
<> TwoD.Transforms.uScale2D 0.4 (Shape.makeShape curvedPath)
in [ diagramTest "offset square" "offsetSquare.svg" (
Shapes.unitSquare
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset rectangle" "offsetRectangle.svg" (
Shapes.unitSquare
& TwoD.Transforms.scale2D (V2 1.2 3.4)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset rectangle negative" "offsetRectangleNegative.svg" (
Shapes.unitSquare
& TwoD.Transforms.scale2D (V2 1.2 3.4)
& TwoD.Offset.offsetShape (-0.25)
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset ellipse" "offsetEllipse.svg" (
Shapes.unitCircle
& TwoD.Transforms.scale2D (V2 1.2 3.4)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset ellipse negative" "offsetEllipseNegative.svg" (
Shapes.unitCircle
& TwoD.Transforms.scale2D (V2 1.2 3.4)
& TwoD.Offset.offsetShape (-0.25)
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset rectangle and ellipse" "offsetRectangleEllipse.svg" (
Shapes.unitSquare
& (<> TwoD.Transforms.translate2D (unit _x ^* 2.5) Shapes.unitCircle)
& TwoD.Transforms.scale2D (V2 1.2 3.4)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.green)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset cross positive" "offsetCrossPositive.svg" (
(TwoD.Transforms.scale2D (V2 1 3) Shapes.centeredSquare
<> TwoD.Transforms.scale2D (V2 3 1) Shapes.centeredSquare)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.yellow)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset cross negative" "offsetCrossNegative.svg" (
(TwoD.Transforms.scale2D (V2 1 3) Shapes.centeredSquare
<> TwoD.Transforms.scale2D (V2 3 1) Shapes.centeredSquare)
& TwoD.Offset.offsetShape (negate 0.25)
& Diagram.shapeDiagram (Paint.paintWithColour Colour.teal)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset hole positive" "offsetHolePositive.svg" (
(TwoD.Transforms.uScale2D 2.2 Shapes.centeredSquare
`Booleans.difference` Shapes.unitCircle)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.red)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset hole scaled" "offsetHoleScaled.svg" (
(TwoD.Transforms.uScale2D 2.2 Shapes.centeredSquare
`Booleans.difference` Shapes.unitCircle)
& TwoD.Transforms.scale2D (V2 1.25 1.5)
& TwoD.Offset.offsetShape 0.25
& Diagram.shapeDiagram (Paint.paintWithColour Colour.red)
& TwoD.Transforms.scale2D 50
)
, diagramTest "offset hole negative" "offsetHoleNegative.svg" (
(TwoD.Transforms.uScale2D 2.2 Shapes.centeredSquare
`Booleans.difference` Shapes.unitCircle)
& TwoD.Offset.offsetShape (negate 0.1)
& Diagram.shapeDiagram (Paint.paintWithColour Colour.fuchsia)
& TwoD.Transforms.scale2D 50
)
, shapeTest "offset curved shape positive" "offsetCurvedShapePositive.svg" (
Shape.makeShape curvedPath
& TwoD.Offset.offsetShape 0.25
)
, shapeTest "offset curved shape negative" "offsetCurvedShapeNegative.svg" (
Shape.makeShape curvedPath
& TwoD.Offset.offsetShape (-0.25)
)
, shapeTest "offset curved shape scaled negative" "offsetCurvedShapeScaledNegative.svg" (
Shape.makeShape curvedPath
& TwoD.Transforms.scale2D (V2 1.5 1)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset square with curved hole negative" "offsetSquareCurvedHoleNegative.svg" (
(TwoD.Transforms.uScale2D 3 Shapes.centeredSquare
`Booleans.difference` TwoD.Transforms.uScale2D 0.6 (Shape.makeShape curvedPath))
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset square hole scaled tall negative" "offsetSquareHoleScaledTallNegative.svg" (
squareWithSquareHole
& TwoD.Transforms.scale2D (V2 1 1.5)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset hole breaks through negative" "offsetHoleBreaksThroughNegative.svg" (
(TwoD.Transforms.uScale2D 2.2 Shapes.centeredSquare
`Booleans.difference` TwoD.Transforms.translate2D (V2 0.6 0) (TwoD.Transforms.uScale2D 0.4 Shapes.unitCircle))
& TwoD.Offset.offsetShape (-0.2)
)
, shapeTest "offset ring with island positive" "offsetRingWithIslandPositive.svg" (
ringWithIsland
& TwoD.Offset.offsetShape 0.25
)
, shapeTest "offset ring with island negative" "offsetRingWithIslandNegative.svg" (
ringWithIsland
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset round tripped scaled square" "offsetRoundTrippedScaledSquare.svg" (
Shapes.centeredSquare
& TwoD.Transforms.scale2D (V2 1.5 1)
& foldMap Shape.makeShape . Shape.shapePaths
& TwoD.Offset.offsetShape 0.25
)
, shapeTest "offset round tripped scaled circle" "offsetRoundTrippedScaledCircle.svg" (
Shapes.unitCircle
& TwoD.Transforms.scale2D (V2 1.5 1)
& foldMap Shape.makeShape . Shape.shapePaths
& TwoD.Offset.offsetShape 0.25
)
, shapeTest "offset curved shape with square hole negative" "offsetCurvedShapeSquareHoleNegative.svg" (
(Shape.makeShape curvedPath
`Booleans.difference` TwoD.Transforms.uScale2D 0.5 Shapes.centeredSquare)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset square hole scaled wide negative" "offsetSquareHoleScaledWideNegative.svg" (
squareWithSquareHole
& TwoD.Transforms.scale2D (V2 1.5 1)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset square hole sheared negative" "offsetSquareHoleShearedNegative.svg" (
squareWithSquareHole
& TwoD.Transforms.matTransform2D (V2 (V3 1 0.5 0) (V3 0 1.5 0))
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset square hole mirrored and scaled negative" "offsetSquareHoleMirroredScaledNegative.svg" (
squareWithSquareHole
& TwoD.Transforms.scale2D (V2 (-1.5) 1)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset scaled paths with hole negative" "offsetScaledPathsHoleNegative.svg" (
let square :: Double -> Path2D.Path2D
square s = Path2D.pathFrom (V2 (-s) (-s))
[ Path2D.lineTo (V2 s (-s))
, Path2D.lineTo (V2 s s)
, Path2D.lineTo (V2 (-s) s)
, Path2D.lineTo (V2 (-s) (-s))
]
scaledSquare = Shape.makeShape . TwoD.Transforms.scale2D (V2 1.5 1) . square
in (scaledSquare 1.1 `Booleans.difference` scaledSquare 0.3)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset D with D hole scaled negative" "offsetDWithDHoleScaledNegative.svg" (
let dShape = Shape.makeShape $ Path2D.pathFrom (V2 (-1) (-1))
[ Path2D.lineTo (V2 0 (-1))
, Path2D.bezierTo (V2 1.5 (-1)) (V2 1.5 1) (V2 0 1)
, Path2D.lineTo (V2 (-1) 1)
, Path2D.lineTo (V2 (-1) (-1))
]
in (dShape `Booleans.difference` TwoD.Transforms.uScale2D 0.3 dShape)
& TwoD.Transforms.scale2D (V2 1.5 1)
& TwoD.Offset.offsetShape (-0.1)
)
, shapeTest "offset two scaled holed squares negative" "offsetTwoScaledHoledSquaresNegative.svg" (
TwoD.Transforms.scale2D (V2 1.5 1) squareWithSquareHole
& ((<>) <*> TwoD.Transforms.translate2D (V2 4 0))
& TwoD.Offset.offsetShape (-0.1)
)
]
]
, GoldenGroup "Booleans"
[ GoldenGroup "Three-D"
[ solidTest "intersections of cubes" "intersectionsOfCubes.svg" (
Booleans.intersections $
zipWith Solid.colourSolid
[Colour.red, Colour.lime, Colour.aqua]
[
Solid.centeredCube
& Transforms.rotate (unit _z) theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, solidTest "unions of cubes" "unionsOfCubes.svg" (
Booleans.unions $
zipWith Solid.colourSolid
[Colour.red, Colour.lime, Colour.aqua]
[
Solid.centeredCube
& Transforms.rotate (unit _z) theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, solidTest "fold intersection of cubes" "foldIntersectionOfCubes.svg" (
foldl1 Booleans.intersection $
zipWith Solid.colourSolid
[Colour.red, Colour.lime, Colour.aqua]
[
Solid.centeredCube
& Transforms.rotate (unit _z) theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, solidTest "fold union of cubes" "foldUnionOfCubes.svg" (
foldl' Booleans.union mempty $
zipWith Solid.colourSolid
[Colour.red, Colour.lime, Colour.aqua]
[
Solid.centeredCube
& Transforms.rotate (unit _z) theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, solidTest "intersections of identical cubes" "intersectionsOfIdenticalCubes.svg" (
Booleans.intersections $
Solid.colourSolid
<$> [Colour.red, Colour.lime, Colour.aqua]
<*> pure Solid.centeredCube
)
, solidTest "intersections with repeated cube" "intersectionsWithRepeatedCube.svg" (
Booleans.intersections
[ Solid.colourSolid Colour.red Solid.centeredCube
, Solid.colourSolid Colour.lime (Transforms.rotate (unit _z) (pi/4) Solid.centeredCube)
, Solid.colourSolid Colour.aqua Solid.centeredCube
]
)
, solidTest "intersections with mempty" "intersectionsWithMempty.svg" (
Solid.colourSolid Colour.red Solid.unitSphere <>
Booleans.intersections
[ Transforms.uScale 2 $ Solid.centeredCube
, mempty
, Transforms.uScale 2 $ Transforms.rotate (unit _z) (pi/4) Solid.centeredCube
]
)
, solidTest "unions of identical cubes" "unionsOfIdenticalCubes.svg" (
Booleans.unions $
Solid.colourSolid
<$> [Colour.red, Colour.lime, Colour.aqua]
<*> pure Solid.centeredCube
)
, solidTest "unions with repeated cube" "unionsWithRepeatedCube.svg" (
Booleans.unions
[ Solid.colourSolid Colour.red Solid.centeredCube
, Solid.colourSolid Colour.lime (Transforms.rotate (unit _z)
(pi/4) Solid.centeredCube)
, Solid.colourSolid Colour.aqua Solid.centeredCube
]
)
, solidTest "unions with mempty" "unionsWithMempty.svg" (
Solid.colourSolid Colour.red Solid.unitSphere <>
Booleans.unions
[ Transforms.uScale 1.5 $ Solid.centeredCube
, mempty
, Transforms.uScale 1.5 $ Transforms.rotate (unit _z) (pi/4) Solid.centeredCube
]
)
]
, GoldenGroup "Two-D"
[ solidTest "Union 2D" "union2D.svg"
( Shape.unitCircle
& TwoD.Transforms.translate2D (V2 0.5 0.5)
& Booleans.union Shape.unitCircle
& TwoD.Transforms.scale2D 10
& Solid.prism 5
)
, diagramTest "Union 2D Diagram" "union2Ddiagram.svg"
( Shape.unitCircle
& TwoD.Transforms.translate2D (V2 0.5 0.5)
& Booleans.union Shape.unitCircle
& TwoD.Transforms.scale2D 50
& Shape.shapePaths
& foldMap (Diagram.pathDiagram Diagram.OutLine Diagram.Visible)
)
, diagramTest "mconcat 2D Diagram" "mconcat2Ddiagram.svg"
( foldMap (Diagram.pathDiagram Diagram.OutLine Diagram.Visible)
. Shape.shapePaths
. mconcat $
[ Shape.unitCircle
& TwoD.Transforms.translate2D (V2 0.5 0.5 ^* fromIntegral i)
& TwoD.Transforms.scale2D 50
| i :: Int <- [0 .. 3]
]
)
, shapeTest "intersections of squares" "intersectionsOfSquares.svg" (
Booleans.intersections
[
Shape.centeredSquare
& TwoD.Transforms.rotate2D theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, shapeTest "unions of squares" "unionsOfSquares.svg" (
Booleans.unions
[
Shape.centeredSquare
& TwoD.Transforms.rotate2D theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, shapeTest "fold intersection of squares" "foldIntersectionOfSquares.svg" (
foldl1 Booleans.intersection
[
Shape.centeredSquare
& TwoD.Transforms.rotate2D theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, shapeTest "fold union of squares" "foldUnionOfSquares.svg" (
foldl' Booleans.union mempty $
[
Shape.centeredSquare
& TwoD.Transforms.rotate2D theta
| theta <- [0, pi/3, 2*pi/3]
]
)
, shapeTest "intersections of identical squares" "intersectionsOfIdenticalSquares.svg" (
Booleans.intersections
(let s = Shape.centeredSquare in [s, s, s])
)
, shapeTest "intersections with repeated Square" "intersectionsWithRepeatedSquare.svg" (
Booleans.intersections
[ Shape.centeredSquare
, TwoD.Transforms.rotate2D (pi/4) Shape.centeredSquare
, Shape.centeredSquare
]
)
, shapeTest "intersections with mempty Shape" "intersectionsWithMemptyShape.svg" (
Shape.unitCircle <>
Booleans.intersections
[ TwoD.Transforms.uScale2D 2 $ Shape.centeredSquare
, mempty
, TwoD.Transforms.uScale2D 2 $ TwoD.Transforms.rotate2D (pi/4) Shape.centeredSquare
]
)
, shapeTest "unions of identical squares" "unionsOfIdenticalSquares.svg" (
Booleans.unions $
(let s = Shape.centeredSquare in [s, s, s])
)
, shapeTest "unions with repeated square" "unionsWithRepeatedSquare.svg" (
Booleans.unions
[ Shape.centeredSquare
, TwoD.Transforms.rotate2D (pi/4) Shape.centeredSquare
, Shape.centeredSquare
]
)
, shapeTest "unions with mempty shape" "unionsWithMemptyShape.svg" (
Shape.unitCircle <>
Booleans.unions
[ TwoD.Transforms.uScale2D 1.6 $ Shape.centeredSquare
, mempty
, TwoD.Transforms.uScale2D 1.6 $ TwoD.Transforms.rotate2D (pi/4) Shape.centeredSquare
]
)
]
]
, GoldenGroup "Revolutions" $
let halveAtBoundingBoxCentre s =
case AABB.axisAlignedBoundingBox s of
Nothing -> mempty
Just (lo, hi) ->
s `Booleans.intersection`
(AABB.aabbToSolid (lo, hi) & Transforms.translate (unit _y ^* ((hi - lo) ^. _y / 2)))
lug z = Solid.centeredCube & Transforms.scale (V3 0.3 0.4 0.4) & Transforms.translate (V3 0.25 0 z)
skirting = Path2D.pathFrom zero
[ Path2D.lineRelative (V2 0.05 0.025)
, Path2D.lineRelative (V2 (-0.05) 0.075)
, Path2D.arcViaRelative (V2 0.05 0.05) (V2 0 0.1)
]
in [ solidTest "partial revolution" "partialRevolution.svg" (
Revolution.revolutionByAngle 1
(TwoD.Transforms.translate2D (unit _x ^* 1.5) Shape.unitCircle)
)
, solidTest "partial revolution II" "partialRevolutionII.svg" (
Revolution.revolutionByAngle 6
(TwoD.Transforms.translate2D (unit _x ^* 0.5) Shape.unitSquare)
)
, solidTest "partial revolution negative" "partialRevolutionNegative.svg" (
Revolution.revolutionByAngle (negate 1)
(TwoD.Transforms.translate2D (unit _x ^* 1.5) Shape.unitCircle)
)
, solidTest "revolution of shape cutting axis" "revolutionOfShapeCuttingAxis.svg" (
Revolution.revolution
( Shapes.centeredSquare
& TwoD.Transforms.scale2D (V2 8 1)
& TwoD.Transforms.translate2D (unit _x)
& TwoD.Transforms.rotate2D (pi/8)
)
)
, solidTest "revolution of shape on axis" "revolutionOfShapeOnAxis.svg" (
Revolution.revolution
( Shapes.unitSquare
& TwoD.Transforms.scale2D (V2 8 1)
& TwoD.Transforms.rotate2D (-pi/8)
)
)
, diagramTest "silhouette of revolution" "silhouetteOfRevolution.svg" (
Shape.unitCircle
& TwoD.Transforms.translate2D (unit _x ^* 1.5)
& Revolution.revolutionByAngle 1
& Shape.silhouette
& (<> TwoD.Transforms.scale2D (V2 0.1 1) Shape.unitSquare)
& Diagram.shapeDiagram (Paint.paintWithColour Colour.red)
& TwoD.Transforms.scale2D 50
)
, solidTest "revolution touching axis, halved at bounding box centre" "revolutionTouchingAxisHalved.svg"
( halveAtBoundingBoxCentre
( Revolution.revolution
( Shape.makeShape $ Path2D.pathFrom zero
[ Path2D.lineTo (V2 1.48 0)
, Path2D.arcViaTo (V2 1.53 0.05) (V2 1.48 0.1)
, Path2D.lineTo (V2 0.72 0.1)
, Path2D.splice2D (TwoD.Transforms.scale2D (V2 1 2) (Path2D.line (V2 0.72 0) zero))
]
) <> lug 0.3
)
)
, solidTest "revolution of chess profile, halved at bounding box centre" "revolutionOfChessProfileHalved.svg"
( halveAtBoundingBoxCentre
( Revolution.revolution
( Shape.makeShape . Path2D.pathFrom zero $ Path2D.splice2D <$>
[ Path2D.line zero (V2 1.48 0)
, skirting
, TwoD.Transforms.scale2D (V2 1 1.7) (Path2D.bezier (V2 1.48 0) (V2 0.74 0) (V2 0.53 0.5) (V2 0.53 1))
, Path2D.line (V2 0.53 0) (V2 0.72 0)
, skirting
, TwoD.Transforms.scale2D (V2 1 1.7) (Path2D.line (V2 0.72 0) zero)
]
) <> lug 2.3
)
)
]
, GoldenGroup "SVG"
[ GoldenCase "read svg and make prism" "svgPrism.svg" $ do
projectRoot <- Paths.getDataDir
Right gearProfile <- SVG.readSVG (projectRoot </> "test-data/gear-profile.svg")
let s = foldMap (Solid.prism 1 . Shape.makeShape) gearProfile
return $ standardSolidDiagram s
, svgPathTest "svg relative smooth quadratic" "svgRelativeSmoothQuadratic.svg"
"M 0 0 q 25 50 50 0 t 50 0 t 50 0"
, svgPathTest "svg smooth quadratic without control point" "svgSmoothQuadraticWithoutControlPoint.svg"
"M 0 0 L 50 0 T 100 50 T 150 0"
, svgPathTest "svg smooth cubic after cubic" "svgSmoothCubicAfterCubic.svg"
"M 0 50 C 0 0 50 0 50 50 S 100 100 100 50"
, svgPathTest "svg smooth cubic without control point" "svgSmoothCubicWithoutControlPoint.svg"
"M 0 0 L 0 50 S 50 100 100 50"
, svgPathTest "svg smooth cubic after quadratic" "svgSmoothCubicAfterQuadratic.svg"
"M 0 0 Q 25 50 50 0 S 100 50 100 0"
, svgPathTest "svg smooth quadratic after cubic" "svgSmoothQuadraticAfterCubic.svg"
"M 0 50 C 0 0 50 0 50 50 T 100 100"
, svgPathTest "svg arc with zero radius" "svgArcZeroRadius.svg"
"M 50 50 L 100 50 A 0 0 0 0 1 150 100 L 50 100 Z"
, svgPathTest "svg arc with negative radii" "svgArcNegativeRadii.svg"
"M 50 50 L 100 50 A -40 -25 0 0 1 150 100 L 50 100 Z"
, svgElementsTest "svg skewX" "svgSkewX.svg"
"<rect width=\"100\" height=\"50\"/><rect width=\"100\" height=\"50\" transform=\"skewX(30)\"/>"
, svgElementsTest "svg skewY" "svgSkewY.svg"
"<rect width=\"100\" height=\"50\"/><rect width=\"100\" height=\"50\" transform=\"skewY(30)\"/>"
, svgElementsTest "svg rounded rect position" "svgRoundedRectPosition.svg"
"<rect width=\"100\" height=\"100\"/><rect x=\"120\" y=\"20\" width=\"80\" height=\"60\" rx=\"20\" ry=\"10\"/>"
, svgElementsTest "svg rounded rect single radius" "svgRoundedRectSingleRadius.svg"
"<rect width=\"80\" height=\"60\" rx=\"20\"/><rect x=\"100\" width=\"80\" height=\"60\" ry=\"10\"/>"
]
, GoldenGroup "Diagram Fills"
[ diagramTest "hole with centre of mass over solid material" "holeWithCentreOfMassOverSolid.svg" (
let bar lo hi = Shape.unitSquare
& TwoD.Transforms.scale2D (hi - lo)
& TwoD.Transforms.translate2D lo
slot = Shape.unions2D
[ bar (V2 (-1.2) (-1.2)) (V2 (-0.8) 1.2)
, bar (V2 (-1.2) 0.8) (V2 1.2 1.2)
, bar (V2 (-1.2) (-1.2)) (V2 1.2 (-0.8))
]
plate = Solid.prism 0.25 (TwoD.Transforms.uScale2D 4 Shape.centeredSquare)
`Booleans.difference` (Solid.prism 3 slot & Transforms.translate (V3 0 0 (-1)))
in withWidth 800 . Diagram.solidDiagram (V3 1 2 8)
$ Solid.colourSolid Colour.aqua plate
)
]
, GoldenGroup "Bounding Boxes"
[ diagramTest "bounding boxes of ellipse" "boundingBoxesOfEllipse.svg" (
let ellipse = Shape.unitCircle
& TwoD.Transforms.scale2D (V2 1.5 2)
& TwoD.Transforms.rotate2D (pi/8)
& TwoD.Transforms.translate2D (V2 0.5 0.75)
obb = TwoD.OBB.orientedBoundingBox2D ellipse
& foldMap (TwoD.OBB.obbToShape)
aabb = TwoD.AABB.axisAlignedBoundingBox2D ellipse
& foldMap (TwoD.AABB.aabbToShape)
in TwoD.Transforms.uScale2D 100
( Diagram.shapeDiagram (Paint.paintWithColour (Colour.aqua)) aabb
<> Diagram.shapeDiagram (Paint.paintWithColour (Colour.lime)) obb
<> Diagram.shapeDiagram (Paint.paintWithColour (Colour.red)) ellipse
)
)
]
]