2018-01-31 18:12:56 -08:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2018, Lars Brubaker, John Lewin
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2020-01-09 07:57:42 -08:00
|
|
|
|
using System;
|
2022-02-23 08:09:56 -08:00
|
|
|
|
using System.ComponentModel;
|
2020-01-09 07:57:42 -08:00
|
|
|
|
using System.Threading.Tasks;
|
2018-01-31 18:12:56 -08:00
|
|
|
|
using ClipperLib;
|
2018-03-08 17:23:03 -08:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2020-01-09 07:57:42 -08:00
|
|
|
|
using MatterHackers.Agg.VertexSource;
|
|
|
|
|
|
using MatterHackers.DataConverters2D;
|
2018-01-31 18:12:56 -08:00
|
|
|
|
using MatterHackers.DataConverters3D;
|
2018-06-06 15:46:30 -07:00
|
|
|
|
using MatterHackers.Localizations;
|
2020-01-09 07:57:42 -08:00
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
2021-11-26 12:49:42 -08:00
|
|
|
|
using MatterHackers.PolygonMesh.Processors;
|
2022-03-02 00:52:04 +00:00
|
|
|
|
using MatterHackers.VectorMath;
|
2020-01-09 07:57:42 -08:00
|
|
|
|
using Polygon = System.Collections.Generic.List<ClipperLib.IntPoint>;
|
|
|
|
|
|
using Polygons = System.Collections.Generic.List<System.Collections.Generic.List<ClipperLib.IntPoint>>;
|
2018-01-31 18:12:56 -08:00
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools.Operations
|
|
|
|
|
|
{
|
2022-08-09 08:48:05 -07:00
|
|
|
|
public class SmoothPathObject3D : Object3D, IEditorDraw, IObject3DControlsProvider
|
2018-01-31 18:12:56 -08:00
|
|
|
|
{
|
2018-06-21 21:02:37 -07:00
|
|
|
|
public SmoothPathObject3D()
|
2018-01-31 18:12:56 -08:00
|
|
|
|
{
|
2018-06-06 15:46:30 -07:00
|
|
|
|
Name = "Smooth Path".Localize();
|
2018-01-31 18:12:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-23 08:09:56 -08:00
|
|
|
|
[Description("Max distance to move bumps to make lines")]
|
2021-10-01 16:59:58 -07:00
|
|
|
|
[MaxDecimalPlaces(2)]
|
|
|
|
|
|
[Slider(.01, 1, VectorMath.Easing.EaseType.Quadratic, snapDistance: .01)]
|
2021-06-05 11:12:26 -07:00
|
|
|
|
public DoubleOrExpression SmoothDistance { get; set; } = .3;
|
2020-01-09 07:57:42 -08:00
|
|
|
|
|
2022-02-23 08:09:56 -08:00
|
|
|
|
[Description("The number of smoothing passes")]
|
2021-06-05 11:12:26 -07:00
|
|
|
|
public IntOrExpression Iterations { get; set; } = 3;
|
2018-06-07 15:08:23 -07:00
|
|
|
|
|
2020-10-11 14:53:46 -07:00
|
|
|
|
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
|
|
|
|
|
|
{
|
|
|
|
|
|
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-19 21:28:16 -07:00
|
|
|
|
public override async void OnInvalidate(InvalidateArgs invalidateArgs)
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2021-06-19 21:28:16 -07:00
|
|
|
|
if ((invalidateArgs.InvalidateType.HasFlag(InvalidateType.Children)
|
|
|
|
|
|
|| invalidateArgs.InvalidateType.HasFlag(InvalidateType.Matrix)
|
|
|
|
|
|
|| invalidateArgs.InvalidateType.HasFlag(InvalidateType.Path))
|
|
|
|
|
|
&& invalidateArgs.Source != this
|
2018-06-20 17:16:38 -07:00
|
|
|
|
&& !RebuildLocked)
|
|
|
|
|
|
{
|
2019-01-24 10:32:22 -08:00
|
|
|
|
await Rebuild();
|
2018-06-20 17:16:38 -07:00
|
|
|
|
}
|
2021-06-19 21:28:16 -07:00
|
|
|
|
else if ((invalidateArgs.InvalidateType.HasFlag(InvalidateType.Properties) && invalidateArgs.Source == this))
|
|
|
|
|
|
{
|
|
|
|
|
|
await Rebuild();
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (SheetObject3D.NeedsRebuild(this, invalidateArgs))
|
2018-06-06 15:46:30 -07:00
|
|
|
|
{
|
2019-01-24 10:32:22 -08:00
|
|
|
|
await Rebuild();
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
2019-01-28 14:19:40 -08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-06-19 21:28:16 -07:00
|
|
|
|
base.OnInvalidate(invalidateArgs);
|
2019-01-28 14:19:40 -08:00
|
|
|
|
}
|
2018-06-06 15:46:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-24 10:32:22 -08:00
|
|
|
|
public override Task Rebuild()
|
2018-01-31 18:12:56 -08:00
|
|
|
|
{
|
2018-05-29 17:46:59 -07:00
|
|
|
|
this.DebugDepth("Rebuild");
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2019-01-24 10:32:22 -08:00
|
|
|
|
var rebuildLock = RebuildLock();
|
|
|
|
|
|
return ApplicationController.Instance.Tasks.Execute(
|
|
|
|
|
|
"Smooth Path".Localize(),
|
|
|
|
|
|
null,
|
|
|
|
|
|
(reporter, cancellationToken) =>
|
|
|
|
|
|
{
|
2021-06-05 11:12:26 -07:00
|
|
|
|
DoSmoothing((long)(SmoothDistance.Value(this) * 1000), Iterations.Value(this));
|
2019-01-24 10:32:22 -08:00
|
|
|
|
|
2020-10-16 16:25:11 -07:00
|
|
|
|
// set the mesh to show the path
|
2022-08-12 18:05:37 -07:00
|
|
|
|
this.Mesh = VertexStorage.Extrude(Constants.PathPolygonsHeight);
|
2020-10-16 16:25:11 -07:00
|
|
|
|
|
2021-05-03 17:58:03 -07:00
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
rebuildLock.Dispose();
|
2021-10-01 16:59:58 -07:00
|
|
|
|
this.Invalidate(InvalidateType.DisplayValues);
|
2021-12-05 22:01:50 -08:00
|
|
|
|
this.CancelAllParentBuilding();
|
2021-05-03 17:58:03 -07:00
|
|
|
|
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Path));
|
|
|
|
|
|
});
|
2019-01-24 10:32:22 -08:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
});
|
2018-01-31 18:12:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-07 15:08:23 -07:00
|
|
|
|
private void DoSmoothing(long maxDist, int interations)
|
2018-01-31 18:12:56 -08:00
|
|
|
|
{
|
2018-06-07 15:08:23 -07:00
|
|
|
|
bool closedPath = true;
|
2022-08-12 18:05:37 -07:00
|
|
|
|
var path = this.CombinedVisibleChildrenPaths();
|
2020-01-09 07:57:42 -08:00
|
|
|
|
if (path == null)
|
2018-06-24 08:54:44 -07:00
|
|
|
|
{
|
2018-11-29 13:41:24 -08:00
|
|
|
|
// clear our existing data
|
2022-08-12 18:05:37 -07:00
|
|
|
|
VertexStorage = new VertexStorage();
|
2018-06-24 08:54:44 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-01-09 07:57:42 -08:00
|
|
|
|
|
2022-08-12 18:05:37 -07:00
|
|
|
|
var sourceVertices = path;
|
2018-06-07 14:20:39 -07:00
|
|
|
|
|
|
|
|
|
|
var inputPolygons = sourceVertices.CreatePolygons();
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2018-01-31 18:12:56 -08:00
|
|
|
|
Polygons outputPolygons = new Polygons();
|
|
|
|
|
|
foreach (Polygon inputPolygon in inputPolygons)
|
|
|
|
|
|
{
|
|
|
|
|
|
int numVerts = inputPolygon.Count;
|
|
|
|
|
|
long maxDistSquared = maxDist * maxDist;
|
|
|
|
|
|
|
|
|
|
|
|
var smoothedPositions = new Polygon(numVerts);
|
|
|
|
|
|
foreach (IntPoint inputPosition in inputPolygon)
|
|
|
|
|
|
{
|
|
|
|
|
|
smoothedPositions.Add(inputPosition);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int iteration = 0; iteration < interations; iteration++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var positionsThisPass = new Polygon(numVerts);
|
|
|
|
|
|
foreach (IntPoint inputPosition in smoothedPositions)
|
|
|
|
|
|
{
|
|
|
|
|
|
positionsThisPass.Add(inputPosition);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int startIndex = closedPath ? 0 : 1;
|
|
|
|
|
|
int endIndex = closedPath ? numVerts : numVerts - 1;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = startIndex; i < endIndex; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
// wrap back to the previous index
|
|
|
|
|
|
IntPoint prev = positionsThisPass[(i + numVerts - 1) % numVerts];
|
|
|
|
|
|
IntPoint cur = positionsThisPass[i];
|
|
|
|
|
|
IntPoint next = positionsThisPass[(i + 1) % numVerts];
|
|
|
|
|
|
|
|
|
|
|
|
IntPoint newPos = (prev + cur + next) / 3;
|
|
|
|
|
|
IntPoint delta = newPos - inputPolygon[i];
|
|
|
|
|
|
if (delta.LengthSquared() > maxDistSquared)
|
|
|
|
|
|
{
|
|
|
|
|
|
delta = delta.GetLength(maxDist);
|
|
|
|
|
|
newPos = inputPolygon[i] + delta;
|
|
|
|
|
|
}
|
2020-01-09 07:57:42 -08:00
|
|
|
|
|
2018-01-31 18:12:56 -08:00
|
|
|
|
smoothedPositions[i] = newPos;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
outputPolygons.Add(smoothedPositions);
|
2018-06-07 15:08:23 -07:00
|
|
|
|
|
2020-01-09 07:57:42 -08:00
|
|
|
|
outputPolygons = ClipperLib.Clipper.CleanPolygons(outputPolygons, Math.Max(maxDist / 10, 1.415));
|
2018-01-31 18:12:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-12 18:05:37 -07:00
|
|
|
|
VertexStorage = outputPolygons.CreateVertexStorage();
|
2018-01-31 18:12:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-19 15:44:00 -08:00
|
|
|
|
public void DrawEditor(Object3DControlsLayer layer, DrawEventArgs e)
|
2018-06-07 14:20:39 -07:00
|
|
|
|
{
|
2020-10-11 19:29:42 -07:00
|
|
|
|
this.DrawPath();
|
2018-06-07 14:20:39 -07:00
|
|
|
|
}
|
2022-03-02 00:52:04 +00:00
|
|
|
|
|
|
|
|
|
|
public AxisAlignedBoundingBox GetEditorWorldspaceAABB(Object3DControlsLayer layer)
|
|
|
|
|
|
{
|
|
|
|
|
|
return this.GetWorldspaceAabbOfDrawPath();
|
|
|
|
|
|
}
|
2018-01-31 18:12:56 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|