add controllable leniency

This commit is contained in:
OliBomby 2023-12-07 00:43:34 +01:00
parent 1930476192
commit 89859b85b7
2 changed files with 39 additions and 2 deletions

View File

@ -95,6 +95,11 @@ protected override void LoadComplete()
bSplineBuilder.CornerThreshold = e.NewValue;
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
}, true);
freehandToolboxGroup.CircleThreshold.BindValueChanged(e =>
{
Scheduler.AddOnce(updateSliderPathFromBSplineBuilder);
}, true);
}
}
@ -358,7 +363,7 @@ private void updateSliderPathFromBSplineBuilder()
private Vector2[] tryCircleArc(List<Vector2> segment)
{
if (segment.Count < 3) return null;
if (segment.Count < 3 || freehandToolboxGroup.CircleThreshold.Value == 0) return null;
// Assume the segment creates a reasonable circular arc and then check if it reasonable
var points = PathApproximator.BSplineToPiecewiseLinear(segment.ToArray(), bSplineBuilder.Degree);
@ -412,7 +417,7 @@ private Vector2[] tryCircleArc(List<Vector2> segment)
loss /= points.Count;
return loss > 0.002 || totalWinding > MathHelper.TwoPi ? null : circleArcControlPoints;
return loss > freehandToolboxGroup.CircleThreshold.Value || totalWinding > MathHelper.TwoPi ? null : circleArcControlPoints;
}
private enum SliderPlacementState

View File

@ -31,6 +31,13 @@ public FreehandSliderToolboxGroup()
Precision = 0.01f
};
public BindableFloat CircleThreshold { get; } = new BindableFloat(0.002f)
{
MinValue = 0f,
MaxValue = 0.005f,
Precision = 0.0001f
};
// We map internal ranges to a more standard range of values for display to the user.
private readonly BindableInt displayTolerance = new BindableInt(100)
{
@ -44,8 +51,15 @@ public FreehandSliderToolboxGroup()
MaxValue = 100
};
private readonly BindableInt displayCircleThreshold = new BindableInt(40)
{
MinValue = 0,
MaxValue = 100
};
private ExpandableSlider<int> toleranceSlider = null!;
private ExpandableSlider<int> cornerThresholdSlider = null!;
private ExpandableSlider<int> circleThresholdSlider = null!;
[BackgroundDependencyLoader]
private void load()
@ -59,6 +73,10 @@ private void load()
cornerThresholdSlider = new ExpandableSlider<int>
{
Current = displayCornerThreshold
},
circleThresholdSlider = new ExpandableSlider<int>
{
Current = displayCircleThreshold
}
};
}
@ -83,18 +101,32 @@ protected override void LoadComplete()
CornerThreshold.Value = displayToInternalCornerThreshold(threshold.NewValue);
}, true);
displayCircleThreshold.BindValueChanged(threshold =>
{
circleThresholdSlider.ContractedLabelText = $"P. C. T.: {threshold.NewValue:N0}";
circleThresholdSlider.ExpandedLabelText = $"Perfect Curve Threshold: {threshold.NewValue:N0}";
CircleThreshold.Value = displayToInternalCircleThreshold(threshold.NewValue);
}, true);
Tolerance.BindValueChanged(tolerance =>
displayTolerance.Value = internalToDisplayTolerance(tolerance.NewValue)
);
CornerThreshold.BindValueChanged(threshold =>
displayCornerThreshold.Value = internalToDisplayCornerThreshold(threshold.NewValue)
);
CircleThreshold.BindValueChanged(threshold =>
displayCircleThreshold.Value = internalToDisplayCircleThreshold(threshold.NewValue)
);
float displayToInternalTolerance(float v) => v / 50f;
int internalToDisplayTolerance(float v) => (int)Math.Round(v * 50f);
float displayToInternalCornerThreshold(float v) => v / 100f;
int internalToDisplayCornerThreshold(float v) => (int)Math.Round(v * 100f);
float displayToInternalCircleThreshold(float v) => v / 20000f;
int internalToDisplayCircleThreshold(float v) => (int)Math.Round(v * 20000f);
}
}
}