deadsy / sdfx

A simple CAD package using signed distance functions
MIT License
518 stars 52 forks source link

Zero length line segments ruin Polygon2D objects #37

Open zostay opened 3 years ago

zostay commented 3 years ago

I'm not really sure this is a bug, but I recently discovered that if you build a polygon from a set of points, you need to be careful to make sure that no consecutive points are equal. This results in a 0 length line segment and causes the SDF code to fail. It will either end up finding no object at all or it will find a series of points that evaluate to NaN coordinates.

I was working on extruding objects from some dirty polygon data that included such. Also my manipulation code expects a closed polygon to be represented with the first and last point being equal. So, I had to make sure to write my code such that the last point is skipped and with code to guard against 0 length segments.

It might be nice to add those the Polygon2D constructor, or maybe just add a note in the docs about the problem.

deadsy commented 3 years ago

I'm not suprised - I didn't anticipate useless vertices. The helper routines in poly.go do vertex manipulation. There's a "fixups" routine that could possibly be used to strip out successive duplicate vertices.