Skip to content

Commit

Permalink
Merge pull request #366 from isovector/fix-cylinder
Browse files Browse the repository at this point in the history
Fix cylinders of zero and negative heights
  • Loading branch information
julialongtin authored Dec 24, 2020
2 parents a72423c + e265e3b commit aea51dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Graphics/Implicit/Primitives.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module Graphics.Implicit.Primitives (
Object
) where

import Prelude(id, Num, (+), (-), (*), (/), (.), negate, Bool(True, False), Maybe(Just, Nothing), Either, fmap, ($))
import Prelude(abs, (<), otherwise, id, Num, (+), (-), (*), (/), (.), negate, Bool(True, False), Maybe(Just, Nothing), Either, fmap, ($))

import Graphics.Implicit.Definitions (ObjectContext, , ℝ2, ℝ3, Box2,
SharedObj(Empty,
Expand Down Expand Up @@ -124,7 +124,10 @@ cylinder2 ::
-> -- ^ Height of the cylinder
-> SymbolicObj3 -- ^ Resulting cylinder

cylinder2 r1 r2 h = Cylinder h r1 r2
cylinder2 _ _ 0 = emptySpace -- necessary to prevent a NaN
cylinder2 r1 r2 h
| h < 0 = mirror (V3 0 0 1) $ cylinder2 r1 r2 (abs h)
| otherwise = Cylinder h r1 r2

cylinder ::
-- ^ Radius of the cylinder
Expand Down
5 changes: 5 additions & 0 deletions tests/ImplicitSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import Linear ( V3(V3), (^*) )
import Graphics.Implicit (unionR)
import Graphics.Implicit (intersectR)
import Graphics.Implicit (extrude)
import Graphics.Implicit (cylinder2)
import Graphics.Implicit (mirror)


------------------------------------------------------------------------------
Expand Down Expand Up @@ -233,6 +235,9 @@ misc3dSpec = describe "misc 3d tests" $ do
withRounding r . extrude obj
=~= withRounding r . extrude (withRounding 0 obj)

prop "cylinder with negative height is a flipped cylinder with positive height" $ \r1 r2 h ->
cylinder2 r1 r2 h =~= mirror (V3 0 0 1) (cylinder2 r1 r2 (-h))


------------------------------------------------------------------------------
-- | Misc identity proofs that should hold for all symbolic objects.
Expand Down

0 comments on commit aea51dd

Please sign in to comment.