Skip to content

Commit

Permalink
Adding better sanitization of NaN and Infinite
Browse files Browse the repository at this point in the history
Fix #53
  • Loading branch information
Twinside committed Nov 2, 2019
1 parent 78755ef commit 8b8df4d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change log
==========

v0.7.5 ??? 2019
---------------

* Adding better sanitization of geometry in presence of NaN
and infinity.

v0.7.4.3 May 2019
-----------------

Expand Down
23 changes: 13 additions & 10 deletions src/Graphics/Rasterific.hs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ drawOrdersOfDrawing width height dpi background drawing =
stupidDefaultTexture =
SolidTexture $ colorMap (const clipBackground) background

orderOf ctxt method primitives = DrawOrder
{ _orderPrimitives = primitives
, _orderTexture = textureOf ctxt
, _orderFillMethod = method
, _orderMask = currentClip ctxt
, _orderDirect = return ()
}

go :: RenderContext px -> Free (DrawCommand px) () -> [DrawOrder px]
-> [DrawOrder px]
go _ (Pure ()) rest = rest
Expand Down Expand Up @@ -670,17 +678,12 @@ drawOrdersOfDrawing width height dpi background drawing =

go ctxt (Free (Fill method prims next)) rest = order : after where
after = go ctxt next rest
order = DrawOrder
{ _orderPrimitives = [geometryOf ctxt prims]
, _orderTexture = textureOf ctxt
, _orderFillMethod = method
, _orderMask = currentClip ctxt
, _orderDirect = return ()
}
order = orderOf ctxt method [geometryOf ctxt prims >>= listOfContainer . sanitize]

go ctxt (Free (Stroke w j cap prims next)) rest =
go ctxt (Free $ Fill FillWinding prim' next) rest
where prim' = listOfContainer $ strokize w j cap prims
go ctxt (Free (Stroke w j cap prims next)) rest = order : after where
after = go ctxt next rest
order = orderOf ctxt FillWinding [prim']
prim' = listOfContainer $ strokize w j cap prims

go ctxt (Free (SetTexture tx sub next)) rest =
go (ctxt { currentTexture = tx }) (fromF sub) $
Expand Down
6 changes: 5 additions & 1 deletion src/Graphics/Rasterific/Operators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ ifZero u v | nearZero u = v
-- | Tell if two points are nearly indistinguishable.
-- If indistinguishable, we can treat them as the same
-- point.
-- point with degenerate coordinates (Infinity/NaN) will be considered
-- as nearby.
isNearby :: Point -> Point -> Bool
{-# INLINE isNearby #-}
isNearby p1 p2 = squareDist < 0.1
isNearby p1 p2 =
squareDist < 0.1 ||
isNaN squareDist || isInfinite squareDist -- degenerate case protection
where vec = p1 ^-^ p2
squareDist = vec `dot` vec

Expand Down
1 change: 1 addition & 0 deletions src/Graphics/Rasterific/StrokeInternal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Graphics.Rasterific.StrokeInternal
, splitPrimitiveUntil
, approximatePathLength
, isPrimitivePoint
, sanitize
) where

import Data.Monoid( (<>) )
Expand Down

0 comments on commit 8b8df4d

Please sign in to comment.