From 843711be09dd8149865f546e352ab03449e4c402 Mon Sep 17 00:00:00 2001 From: Michael Jansen Date: Mon, 30 Sep 2024 15:45:59 -0400 Subject: [PATCH] enforce minimum control length better --- lib/path/waypoint.dart | 25 +++++++++++++++++-- lib/util/path_optimizer.dart | 4 +-- .../editor/tree_widgets/waypoints_tree.dart | 4 +-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/path/waypoint.dart b/lib/path/waypoint.dart index 0b9fac4b..c069f2fc 100644 --- a/lib/path/waypoint.dart +++ b/lib/path/waypoint.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'dart:math'; class Waypoint { + static const num minControlLength = 0.25; static HashMap linked = HashMap(); Point anchor; @@ -24,6 +25,14 @@ class Waypoint { if (linkedName != null) { linked[linkedName!] = Point(anchor.x, anchor.y); } + + // Set the lengths to their current length to enforce minimum + if (prevControl != null) { + setPrevControlLength(getPrevControlLength()); + } + if (nextControl != null) { + setNextControlLength(getNextControlLength()); + } } bool get isAnchorDragging => _isAnchorDragging; @@ -190,7 +199,11 @@ class Waypoint { var mag = dir.magnitude; dir = Point(dir.x / mag, dir.y / mag); - var control = Point(dir.x * length, dir.y * length); + if (!length.isFinite) { + length = minControlLength; + } + final l = max(length, minControlLength); + var control = Point(dir.x * l, dir.y * l); prevControl = Point(anchor.x + control.x, anchor.y + control.y); } } @@ -201,7 +214,11 @@ class Waypoint { var mag = dir.magnitude; dir = Point(dir.x / mag, dir.y / mag); - var control = Point(dir.x * length, dir.y * length); + if (!length.isFinite) { + length = minControlLength; + } + final l = max(length, minControlLength); + var control = Point(dir.x * l, dir.y * l); nextControl = Point(anchor.x + control.x, anchor.y + control.y); } } @@ -252,6 +269,8 @@ class Waypoint { } _updatePrevControlFromNext(); + // Set the length to enforce minimum + setNextControlLength(getNextControlLength()); } else if (_isPrevControlDragging) { if (isLocked) { Point lineEnd = prevControl! + (prevControl! - anchor); @@ -264,6 +283,8 @@ class Waypoint { } _updateNextControlFromPrev(); + // Set the length to enforce minimum + setPrevControlLength(getPrevControlLength()); } } diff --git a/lib/util/path_optimizer.dart b/lib/util/path_optimizer.dart index 97c9650b..786ea814 100644 --- a/lib/util/path_optimizer.dart +++ b/lib/util/path_optimizer.dart @@ -191,14 +191,14 @@ class _Individual { if (mutated.prevControl != null) { // Mutate by changing prev control length randomly between +/- 0.2 m double x = (rand.nextDouble() - 0.5) * 0.2; - double prevLength = max(0.5, mutated.getPrevControlLength() + x); + double prevLength = mutated.getPrevControlLength() + x; mutated.setPrevControlLength(prevLength); } if (mutated.nextControl != null) { // Mutate by changing next control length randomly between +/- 0.2 m double x = (rand.nextDouble() - 0.5) * 0.2; - double nextLength = max(0.5, mutated.getNextControlLength() + x); + double nextLength = mutated.getNextControlLength() + x; mutated.setNextControlLength(nextLength); } diff --git a/lib/widgets/editor/tree_widgets/waypoints_tree.dart b/lib/widgets/editor/tree_widgets/waypoints_tree.dart index c75d35d6..22d1eaf8 100644 --- a/lib/widgets/editor/tree_widgets/waypoints_tree.dart +++ b/lib/widgets/editor/tree_widgets/waypoints_tree.dart @@ -244,7 +244,7 @@ class _WaypointsTreeState extends State { waypoint.getPrevControlLength().toStringAsFixed(2), label: 'Previous Control Length (M)', onSubmitted: (value) { - if (value != null && value >= 0.5) { + if (value != null) { Waypoint wRef = waypoints[waypointIdx]; widget.undoStack.add(_waypointChange( wRef, @@ -268,7 +268,7 @@ class _WaypointsTreeState extends State { waypoint.getNextControlLength().toStringAsFixed(2), label: 'Next Control Length (M)', onSubmitted: (value) { - if (value != null && value >= 0.5) { + if (value != null) { Waypoint wRef = waypoints[waypointIdx]; widget.undoStack.add(_waypointChange( wRef,