From babd4884ddc7a3f0d6a47f52e5e3c27ddfda10be Mon Sep 17 00:00:00 2001
From: Lex Neva <github.com@lexneva.name>
Date: Wed, 18 Mar 2020 22:54:25 -0400
Subject: [PATCH] avoid math domain error

---
 lib/extensions/convert_to_satin.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/extensions/convert_to_satin.py b/lib/extensions/convert_to_satin.py
index c666b6251..cf292281a 100644
--- a/lib/extensions/convert_to_satin.py
+++ b/lib/extensions/convert_to_satin.py
@@ -164,6 +164,13 @@ class ConvertToSatin(InkstitchExtension):
                 # The dot product of two vectors is |v1| * |v2| * cos(angle).
                 # These are unit vectors, so their magnitudes are 1.
                 cos_angle_between = prev_direction * direction
+
+                # Clamp to the valid range for a cosine.  The above _should_
+                # already be in this range, but floating point inaccuracy can
+                # push it outside the range causing math.acos to throw
+                # ValueError ("math domain error").
+                cos_angle_between = max(-1.0, min(1.0, cos_angle_between))
+
                 angle = abs(math.degrees(math.acos(cos_angle_between)))
 
                 # Use the square of the angle, measured in degrees.