Skip to content

Small polygons under rotation result in SDF flipping its sign #449

Open

Description

@sorki

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)

This has something to do with isIn

minimum dists * if isIn then -1 else 1

and
relativePairs = fmap (\(a,b) -> (a - p, b - p) ) pairs
crossing_points =
[x2 - y2*(x2-x1)/(y2-y1) | (V2 x1 y1, V2 x2 y2) <- relativePairs,
( (y2 <= 0) && (y1 >= 0) ) || ( (y2 >= 0) && (y1 <= 0) ) ]
-- FIXME: use partition instead?
seemsInRight = odd . length . filter (>0) $ nub crossing_points
seemsInLeft = odd . length . filter (<0) $ nub crossing_points
isIn = seemsInRight && seemsInLeft

and it only seems to affect small polygons + rotation. If the isIn mechanism is dropped, they don't get rendered at all.

Activity

  1. sorki commented on Oct 31, 2023

    @sorki
    MemberAuthor

    be1ee7a adds golden test (since we don't have any 2D ones). The two renders differ in one pixel and 9 bytes..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions