mattercontrol/MatterControlLib/DesignTools/Operations/Path/RevolveObject3D.cs

228 lines
7.1 KiB
C#
Raw Normal View History

/*
Copyright (c) 2017, 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.
*/
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.UI;
using MatterHackers.Agg.VertexSource;
using MatterHackers.DataConverters3D;
using MatterHackers.DataConverters3D.UndoCommands;
using MatterHackers.Localizations;
2020-10-11 19:29:42 -07:00
using MatterHackers.MatterControl.DesignTools.Operations;
using MatterHackers.MatterControl.PartPreviewWindow;
using MatterHackers.PolygonMesh;
2021-11-26 12:49:42 -08:00
using MatterHackers.PolygonMesh.Processors;
2020-10-11 19:29:42 -07:00
using MatterHackers.RenderOpenGl;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
2020-10-13 22:24:30 -07:00
namespace MatterHackers.MatterControl.DesignTools.Operations
{
public class RevolveObject3D : Object3D, IEditorDraw
{
[MaxDecimalPlaces(2)]
2022-08-14 08:13:22 -07:00
[Slider(0, 360, snapDistance: 1)]
public DoubleOrExpression Rotation { get; set; } = 0;
[MaxDecimalPlaces(2)]
[Slider(-30, 30, snapDistance: 1)]
2021-06-05 11:12:26 -07:00
public DoubleOrExpression AxisPosition { get; set; } = 0;
[MaxDecimalPlaces(2)]
2022-08-14 08:13:22 -07:00
[Slider(0, 360, snapDistance: 1)]
2021-06-05 11:12:26 -07:00
public DoubleOrExpression StartingAngle { get; set; } = 0;
[MaxDecimalPlaces(2)]
2021-09-12 20:23:40 -07:00
[Slider(3, 360, snapDistance: 1)]
2021-06-05 11:12:26 -07:00
public DoubleOrExpression EndingAngle { get; set; } = 45;
2021-09-12 20:23:40 -07:00
[Slider(3, 360, Easing.EaseType.Quadratic, snapDistance: 1)]
2021-06-05 11:12:26 -07:00
public IntOrExpression Sides { get; set; } = 30;
2022-01-22 15:43:50 -08:00
public override bool CanApply => true;
2022-01-22 15:43:50 -08:00
public override void Apply(UndoBuffer undoBuffer)
{
if (Mesh == null)
{
2022-01-22 15:43:50 -08:00
Cancel(undoBuffer);
}
else
{
// only keep the mesh and get rid of everything else
using (RebuildLock())
{
var meshOnlyItem = new Object3D()
{
Mesh = this.Mesh.Copy(CancellationToken.None)
};
meshOnlyItem.CopyProperties(this, Object3DPropertyFlags.All);
2020-10-13 22:24:30 -07:00
// and replace us with the children
undoBuffer.AddAndDo(new ReplaceCommand(new[] { this }, new[] { meshOnlyItem }));
}
Invalidate(InvalidateType.Children);
}
}
public RevolveObject3D()
{
Name = "Revolve".Localize();
}
public override async void OnInvalidate(InvalidateArgs eventArgs)
{
if ((eventArgs.InvalidateType.HasFlag(InvalidateType.Path)
|| eventArgs.InvalidateType.HasFlag(InvalidateType.Children))
&& eventArgs.Source != this
&& !RebuildLocked)
{
await Rebuild();
}
else if (eventArgs.InvalidateType.HasFlag(InvalidateType.Properties)
&& eventArgs.Source == this)
{
await Rebuild();
}
else
{
base.OnInvalidate(eventArgs);
}
}
2022-08-12 18:05:37 -07:00
(Vector3, Vector3) GetStartEnd(IObject3D pathObject, IVertexSource path)
{
// draw the line that is the rotation point
var aabb = this.GetAxisAlignedBoundingBox();
2022-08-12 18:05:37 -07:00
var vertexSource = path.Transform(Matrix);
var bounds = vertexSource.GetBounds();
var lineX = bounds.Left + AxisPosition.Value(this);
var start = new Vector3(lineX, aabb.MinXYZ.Y, aabb.MinXYZ.Z);
var end = new Vector3(lineX, aabb.MaxXYZ.Y, aabb.MinXYZ.Z);
return (start, end);
}
public void DrawEditor(Object3DControlsLayer layer, DrawEventArgs e)
2020-10-11 19:29:42 -07:00
{
2022-08-12 18:05:37 -07:00
var path = this.CombinedVisibleChildrenPaths();
2022-08-14 08:13:22 -07:00
if (path != null)
2020-10-11 19:29:42 -07:00
{
2022-08-12 18:05:37 -07:00
var (start, end) = GetStartEnd(this, path);
2020-10-11 19:29:42 -07:00
layer.World.Render3DLine(start, end, Color.Red, true);
layer.World.Render3DLine(start, end, Color.Red.WithAlpha(20), false);
}
}
public AxisAlignedBoundingBox GetEditorWorldspaceAABB(Object3DControlsLayer layer)
{
2022-08-12 18:05:37 -07:00
return this.GetWorldspaceAabbOfDrawPath();
}
2021-12-05 22:01:50 -08:00
private CancellationTokenSource cancellationToken;
public bool IsBuilding => this.cancellationToken != null;
public void CancelBuild()
{
var threadSafe = this.cancellationToken;
if (threadSafe != null)
{
threadSafe.Cancel();
}
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");
bool valuesChanged = false;
2022-08-14 08:13:22 -07:00
var rotation = MathHelper.DegreesToRadians(Rotation.ClampIfNotCalculated(this, 0, 360, ref valuesChanged));
var startingAngle = StartingAngle.ClampIfNotCalculated(this, 0, 360 - .01, ref valuesChanged);
2021-06-05 11:12:26 -07:00
var endingAngle = EndingAngle.ClampIfNotCalculated(this, startingAngle + .01, 360, ref valuesChanged);
var sides = Sides.Value(this);
var axisPosition = AxisPosition.Value(this);
2021-06-05 11:12:26 -07:00
if (startingAngle > 0 || endingAngle < 360)
{
2021-06-05 11:12:26 -07:00
Sides = agg_basics.Clamp(sides, 1, 360, ref valuesChanged);
}
else
{
2021-06-05 11:12:26 -07:00
Sides = agg_basics.Clamp(sides, 3, 360, ref valuesChanged);
}
2021-10-01 12:28:06 -07:00
Invalidate(InvalidateType.DisplayValues);
var rebuildLock = RebuildLock();
// now create a long running task to process the image
return ApplicationController.Instance.Tasks.Execute(
"Revolve".Localize(),
null,
2021-12-05 22:01:50 -08:00
(reporter, cancellationTokenSource) =>
{
2021-12-05 22:01:50 -08:00
this.cancellationToken = cancellationTokenSource as CancellationTokenSource;
2022-08-12 18:05:37 -07:00
var vertexSource = this.CombinedVisibleChildrenPaths();
2022-08-14 08:13:22 -07:00
vertexSource = vertexSource.Rotate(rotation);
var pathBounds = vertexSource.GetBounds();
2021-06-05 11:12:26 -07:00
vertexSource = vertexSource.Translate(-pathBounds.Left - axisPosition, 0);
Mesh mesh = VertexSourceToMesh.Revolve(vertexSource,
2021-06-05 11:12:26 -07:00
sides,
MathHelper.DegreesToRadians(360 - endingAngle),
MathHelper.DegreesToRadians(360 - startingAngle),
false);
2022-08-14 08:13:22 -07:00
var transform = Matrix4X4.CreateTranslation(pathBounds.Left + axisPosition, 0, 0) * Matrix4X4.CreateRotationZ(-rotation);
// take the axis offset out
2022-08-14 08:13:22 -07:00
mesh.Transform(transform);
2020-10-11 15:57:00 -07:00
if (mesh.Vertices.Count == 0)
{
2020-10-11 15:57:00 -07:00
mesh = null;
}
2020-10-11 15:57:00 -07:00
Mesh = mesh;
2021-12-05 22:01:50 -08:00
this.cancellationToken = null;
UiThread.RunOnIdle(() =>
{
rebuildLock.Dispose();
2021-12-05 22:01:50 -08:00
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children));
});
2021-10-09 21:14:02 -07:00
return Task.CompletedTask;
});
}
}
}