Skip to content

Commit

Permalink
enforce minimum control length better
Browse files Browse the repository at this point in the history
  • Loading branch information
mjansen4857 committed Sep 30, 2024
1 parent 738220a commit 843711b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
25 changes: 23 additions & 2 deletions lib/path/waypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:collection';
import 'dart:math';

class Waypoint {
static const num minControlLength = 0.25;
static HashMap<String, Point> linked = HashMap();

Point anchor;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -264,6 +283,8 @@ class Waypoint {
}

_updateNextControlFromPrev();
// Set the length to enforce minimum
setPrevControlLength(getPrevControlLength());
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/util/path_optimizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/editor/tree_widgets/waypoints_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class _WaypointsTreeState extends State<WaypointsTree> {
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,
Expand All @@ -268,7 +268,7 @@ class _WaypointsTreeState extends State<WaypointsTree> {
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,
Expand Down

0 comments on commit 843711b

Please sign in to comment.