Skip to content

Commit

Permalink
Add goldenFormat2, add golden test for troublesome polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
sorki committed Oct 31, 2023
1 parent 17b521d commit be1ee7a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
28 changes: 26 additions & 2 deletions tests/GoldenSpec/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

module GoldenSpec.Spec (spec) where

import GoldenSpec.Util (golden, goldenAllFormats)
import GoldenSpec.Util (golden, goldenAllFormats, goldenFormat2)
import Graphics.Implicit
import Graphics.Implicit.Export.OutputFormat (OutputFormat (PNG))
import Prelude
import Test.Hspec ( describe, Spec )
import Graphics.Implicit.Primitives (torus, ellipsoid, cone)
Expand Down Expand Up @@ -193,4 +194,27 @@ spec = describe "golden tests" $ do
(C1 1)
(Left 0)
(union [circle 10])
$ Left 40
$ Left 40

-- These two should be equal, but internally when sampled at (V2 (-1) 0)
-- the sign of the SDF differs yet they both get rendered correctly.
let funPoly = polygon [V2 0 0, V2 0 (-0.1), V2 (-2) 0, V2 0 (-1)]
rotFunPoly = rotate (2*pi) funPoly
--
-- > getImplicit funPoly (V2 (-1) 0)
-- -4.993761694389224e-2
-- > getBox funPoly
-- (V2 (-2.0) (-1.0),V2 0.0 0.0)
--
-- vs
--
-- > getImplicit rotFunPoly (V2 (-1) 0))
-- 4.9937616943891996e-2
-- > getBox rotFunPoly
-- (V2 (-2.0000000000000004) (-1.0),V2 0.0 4.898587196589413e-16)
--
-- TODO(srk): investigate, see also #449

describe "2d" $ do
goldenFormat2 PNG "troublesome-polygon" 1 $ funPoly
goldenFormat2 PNG "troublesome-polygon-under-rotation" 1 $ rotFunPoly
36 changes: 33 additions & 3 deletions tests/GoldenSpec/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}

module GoldenSpec.Util (golden, goldenAllFormats, goldenFormat) where
module GoldenSpec.Util (golden, goldenAllFormats, goldenFormat, goldenFormat2) where

import Control.Monad (forM_)
import Control.Monad.IO.Class (liftIO)
import Graphics.Implicit (SymbolicObj3)
import Graphics.Implicit.Export (export3)
import Graphics.Implicit (SymbolicObj2, SymbolicObj3)
import Graphics.Implicit.Export (export2, export3)
import Graphics.Implicit.Export.OutputFormat (OutputFormat (ASCIISTL), formats3D, formatExtension)
import Prelude (IO, FilePath, Bool (True, False), String, Double, pure, (==), (>>=), (<>), ($), show)
import System.Directory (getTemporaryDirectory, doesFileExist)
Expand Down Expand Up @@ -57,6 +57,36 @@ goldenFormat fmt name resolution sym = it (name <> " (golden, format: " <> show
then pure ()
else False `shouldBe` True

-- | Construct a golden test for rendering the given 'SymbolicObj2' at the
-- specified resolution. On the first run of this test, it will render the
-- object and cache the results. Subsequent test runs will compare their result
-- to the cached one. This is valuable for ensuring mesh generation doesn't
-- break across commits.
--
-- The objects are cached under @tests/golden/@, with the given name. Deleting
-- this file is sufficient to update the test if changes in the mesh generation
-- are intended.
-- TODO(srk): polymorphic export would be nice, related to #446
goldenFormat2 :: OutputFormat -> String -> Double -> SymbolicObj2 -> SpecWith ()
goldenFormat2 fmt name resolution sym = it (name <> " (golden, format: " <> show fmt <> ")") $ do
(res, cached) <- liftIO $ do
temp_fp <- getTemporaryFilePath "golden"
-- Output the rendered mesh
export2 fmt resolution temp_fp sym
!res <- readFile temp_fp
let golden_fp = "./tests/golden/" <> name <> "." <> formatExtension fmt
-- Check if the cached results already exist.
doesFileExist golden_fp >>= \case
True -> pure ()
-- If not, save the mesh we just created in the cache.
False -> writeFile golden_fp res
!cached <- readFile golden_fp
pure (res, cached)
-- Finally, ceck if the two meshes are equal.
if res == cached
then pure ()
else False `shouldBe` True

------------------------------------------------------------------------------
-- | Get a temporary filepath with the desired extension. On unix systems, this
-- is a file under @/tmp@. Useful for tests that need to write files.
Expand Down
Binary file added tests/golden/troublesome-polygon-under-rotation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/golden/troublesome-polygon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit be1ee7a

Please sign in to comment.