mattercontrol/MatterControlLib/DesignTools/Operations/AlignObject3D.cs
2019-01-28 17:44:10 -08:00

543 lines
No EOL
17 KiB
C#

/*
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.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MatterHackers.Agg.UI;
using MatterHackers.DataConverters3D;
using MatterHackers.MatterControl.PartPreviewWindow.View3D;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace MatterHackers.MatterControl.DesignTools.Operations
{
using Aabb = AxisAlignedBoundingBox;
[JsonConverter(typeof(StringEnumConverter))]
public enum Align { None, Min, Center, Max, Origin }
[JsonConverter(typeof(StringEnumConverter))]
public enum Alignment { X, Y, Z, negX, negY, negZ };
[JsonConverter(typeof(StringEnumConverter))]
[Flags]
public enum Edge
{
LeftFront = FaceAlign.Left | FaceAlign.Front,
LeftBack = FaceAlign.Left | FaceAlign.Back,
LeftBottom = FaceAlign.Left | FaceAlign.Bottom,
LeftTop = FaceAlign.Left | FaceAlign.Top,
RightFront = FaceAlign.Right | FaceAlign.Front,
RightBack = FaceAlign.Right | FaceAlign.Back,
RightBottom = FaceAlign.Right | FaceAlign.Bottom,
RightTop = FaceAlign.Right | FaceAlign.Top,
FrontBottom = FaceAlign.Front | FaceAlign.Bottom,
FrontTop = FaceAlign.Front | FaceAlign.Top,
BackBottom = FaceAlign.Back | FaceAlign.Bottom,
BackTop = FaceAlign.Back | FaceAlign.Top
}
[JsonConverter(typeof(StringEnumConverter))]
[Flags]
public enum FaceAlign
{
Left = 0x01,
Right = 0x02,
Front = 0x04,
Back = 0x08,
Bottom = 0x10,
Top = 0x20,
};
[JsonConverter(typeof(StringEnumConverter))]
[Flags]
public enum Side2D
{
Left = 0x01,
Right = 0x02,
Bottom = 0x10,
Top = 0x20,
};
public class AlignObject3D : Object3D, IPropertyGridModifier
{
// We need to serialize this so we can remove the arrange and get back to the objects before arranging
public List<Aabb> OriginalChildrenBounds = new List<Aabb>();
private SelectedChildren _anchorObjectSelector = new SelectedChildren();
public AlignObject3D()
{
Name = "Align";
}
public AlignObject3D(IObject3D objectToAlign, FaceAlign boundingFacesToAlign, IObject3D objectToAlignTo, FaceAlign boundingFacesToAlignTo, double offsetX = 0, double offsetY = 0, double offsetZ = 0, string name = "")
: this(objectToAlign, boundingFacesToAlign, GetPositionToAlignTo(objectToAlignTo, boundingFacesToAlignTo, new Vector3(offsetX, offsetY, offsetZ)), name)
{
if (objectToAlign == objectToAlignTo)
{
throw new Exception("You cannot align an object to itself.");
}
}
public AlignObject3D(IObject3D objectToAlign, FaceAlign boundingFacesToAlign, double positionToAlignToX = 0, double positionToAlignToY = 0, double positionToAlignToZ = 0, string name = "")
: this(objectToAlign, boundingFacesToAlign, new Vector3(positionToAlignToX, positionToAlignToY, positionToAlignToZ), name)
{
}
public AlignObject3D(IObject3D objectToAlign, FaceAlign boundingFacesToAlign, Vector3 positionToAlignTo, double offsetX, double offsetY, double offsetZ, string name = "")
: this(objectToAlign, boundingFacesToAlign, positionToAlignTo + new Vector3(offsetX, offsetY, offsetZ), name)
{
}
public AlignObject3D(IObject3D item, FaceAlign boundingFacesToAlign, Vector3 positionToAlignTo, string name = "")
{
AxisAlignedBoundingBox bounds = item.GetAxisAlignedBoundingBox();
if (IsSet(boundingFacesToAlign, FaceAlign.Left, FaceAlign.Right))
{
positionToAlignTo.X = positionToAlignTo.X - bounds.MinXYZ.X;
}
if (IsSet(boundingFacesToAlign, FaceAlign.Right, FaceAlign.Left))
{
positionToAlignTo.X = positionToAlignTo.X - bounds.MinXYZ.X - (bounds.MaxXYZ.X - bounds.MinXYZ.X);
}
if (IsSet(boundingFacesToAlign, FaceAlign.Front, FaceAlign.Back))
{
positionToAlignTo.Y = positionToAlignTo.Y - bounds.MinXYZ.Y;
}
if (IsSet(boundingFacesToAlign, FaceAlign.Back, FaceAlign.Front))
{
positionToAlignTo.Y = positionToAlignTo.Y - bounds.MinXYZ.Y - (bounds.MaxXYZ.Y - bounds.MinXYZ.Y);
}
if (IsSet(boundingFacesToAlign, FaceAlign.Bottom, FaceAlign.Top))
{
positionToAlignTo.Z = positionToAlignTo.Z - bounds.MinXYZ.Z;
}
if (IsSet(boundingFacesToAlign, FaceAlign.Top, FaceAlign.Bottom))
{
positionToAlignTo.Z = positionToAlignTo.Z - bounds.MinXYZ.Z - (bounds.MaxXYZ.Z - bounds.MinXYZ.Z);
}
Matrix *= Matrix4X4.CreateTranslation(positionToAlignTo);
Children.Add(item.Clone());
}
[ShowAsList]
[DisplayName("Primary")]
public SelectedChildren AnchorObjectSelector
{
get
{
if (Children.Count > 0)
{
if (_anchorObjectSelector.Count != 1)
{
_anchorObjectSelector.Clear();
_anchorObjectSelector.Add(Children.First().ID);
}
if (!this.Children.Any(c => c.ID == _anchorObjectSelector[0]))
{
// we don't have an id of any of our current children
_anchorObjectSelector.Clear();
_anchorObjectSelector.Add(Children.First().ID);
}
}
else
{
_anchorObjectSelector.Clear();
}
return _anchorObjectSelector;
}
set => _anchorObjectSelector = value;
}
public bool Advanced { get; set; } = false;
[SectionStart("X Axis"), DisplayName("Align")]
[Icons(new string[] { "424.png", "align_left.png", "align_center_x.png", "align_right.png", "align_origin.png" })]
public Align XAlign { get; set; } = Align.None;
[DisplayName("Anchor")]
[Icons(new string[] { "424.png", "align_to_left.png", "align_to_center_x.png", "align_to_right.png", "" }, InvertIcons = true)]
public Align XAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double XOffset { get; set; } = 0;
[SectionStart("Y Axis"), DisplayName("Align")]
[Icons(new string[] { "424.png", "align_bottom.png", "align_center_y.png", "align_Top.png", "align_origin.png" })]
public Align YAlign { get; set; } = Align.None;
[DisplayName("Anchor")]
[Icons(new string[] { "424.png", "align_to_bottom.png", "align_to_center_y.png", "align_to_top.png", "" }, InvertIcons = true)]
public Align YAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double YOffset { get; set; } = 0;
[SectionStart("Z Axis"), DisplayName("Align")]
[Icons(new string[] { "424.png", "align_bottom.png", "align_center_y.png", "align_Top.png", "align_origin.png" })]
public Align ZAlign { get; set; } = Align.None;
[DisplayName("Anchor")]
[Icons(new string[] { "424.png", "align_to_bottom.png", "align_to_center_y.png", "align_to_top.png", "" }, InvertIcons = true)]
public Align ZAlignTo { get; set; } = Align.None;
[DisplayName("Offset")]
public double ZOffset { get; set; } = 0;
public override bool CanFlatten => true;
private List<Aabb> CurrentChildrenBounds
{
get
{
var currentChildrenBounds = new List<Aabb>();
this.Children.Modify(list =>
{
foreach (var child in list)
{
currentChildrenBounds.Add(child.GetAxisAlignedBoundingBox());
}
});
return currentChildrenBounds;
}
}
[JsonIgnore]
private IObject3D AnchorObject
{
get
{
return this.Children.Where(c => c.ID == AnchorObjectSelector[0]).FirstOrDefault();
}
}
[JsonIgnore]
private int AnchorObjectIndex
{
get
{
int index = 0;
foreach (var child in this.Children)
{
if (child == AnchorObject)
{
return index;
}
index++;
}
return 0;
}
}
public static Vector3 GetPositionToAlignTo(IObject3D objectToAlignTo, FaceAlign boundingFacesToAlignTo, Vector3 extraOffset)
{
Vector3 positionToAlignTo = new Vector3();
if (IsSet(boundingFacesToAlignTo, FaceAlign.Left, FaceAlign.Right))
{
positionToAlignTo.X = objectToAlignTo.GetAxisAlignedBoundingBox().MinXYZ.X;
}
if (IsSet(boundingFacesToAlignTo, FaceAlign.Right, FaceAlign.Left))
{
positionToAlignTo.X = objectToAlignTo.GetAxisAlignedBoundingBox().MaxXYZ.X;
}
if (IsSet(boundingFacesToAlignTo, FaceAlign.Front, FaceAlign.Back))
{
positionToAlignTo.Y = objectToAlignTo.GetAxisAlignedBoundingBox().MinXYZ.Y;
}
if (IsSet(boundingFacesToAlignTo, FaceAlign.Back, FaceAlign.Front))
{
positionToAlignTo.Y = objectToAlignTo.GetAxisAlignedBoundingBox().MaxXYZ.Y;
}
if (IsSet(boundingFacesToAlignTo, FaceAlign.Bottom, FaceAlign.Top))
{
positionToAlignTo.Z = objectToAlignTo.GetAxisAlignedBoundingBox().MinXYZ.Z;
}
if (IsSet(boundingFacesToAlignTo, FaceAlign.Top, FaceAlign.Bottom))
{
positionToAlignTo.Z = objectToAlignTo.GetAxisAlignedBoundingBox().MaxXYZ.Z;
}
return positionToAlignTo + extraOffset;
}
public override async void OnInvalidate(InvalidateArgs invalidateType)
{
if ((invalidateType.InvalidateType.HasFlag(InvalidateType.Children)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Matrix)
|| invalidateType.InvalidateType.HasFlag(InvalidateType.Mesh))
&& invalidateType.Source != this
&& !RebuildLocked)
{
await Rebuild();
}
else if (invalidateType.InvalidateType.HasFlag(InvalidateType.Properties)
&& invalidateType.Source == this)
{
await Rebuild();
}
// and also always pass back the actual type
base.OnInvalidate(invalidateType);
}
public override Task Rebuild()
{
this.DebugDepth("Rebuild");
var childrenIds = Children.Select(c => c.ID).ToArray();
// if the count of our children changed clear our cache of the bounds
if (Children.Count != OriginalChildrenBounds.Count)
{
OriginalChildrenBounds.Clear();
}
using (RebuildLock())
{
var aabb = this.GetAxisAlignedBoundingBox();
// TODO: check if the has code for the children
if (OriginalChildrenBounds.Count == 0)
{
this.Children.Modify(list =>
{
foreach (var child in list)
{
OriginalChildrenBounds.Add(child.GetAxisAlignedBoundingBox());
}
});
}
this.Children.Modify((Action<List<IObject3D>>)((List<IObject3D> list) =>
{
if (list.Count == 0)
{
return;
}
int anchorIndex = AnchorObjectIndex;
var anchorBounds = CurrentChildrenBounds[anchorIndex];
int i = 0;
// first align the anchor object
foreach (var child in list)
{
if (XAlign == Align.None
|| i == anchorIndex)
{
if (i < OriginalChildrenBounds.Count)
{
// make sure it is where it started
AlignAxis(0, Align.Min, (double)OriginalChildrenBounds[i].MinXYZ.X, 0, child);
}
}
if (YAlign == Align.None
|| i == anchorIndex)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(1, Align.Min, (double)OriginalChildrenBounds[i].MinXYZ.Y, 0, child);
}
}
if (ZAlign == Align.None
|| i == anchorIndex)
{
if (i < OriginalChildrenBounds.Count)
{
AlignAxis(2, Align.Min, (double)OriginalChildrenBounds[i].MinXYZ.Z, 0, child);
}
}
i++;
}
// the align all the objects to it
i = 0;
foreach (var child in list)
{
if (XAlign != Align.None
&& i != anchorIndex)
{
if (XAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3Ex.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3Ex.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(-(childOrigin - firstOrigin).X + (Advanced ? XOffset : 0), 0, 0));
}
else
{
AlignAxis(0, XAlign, GetAlignToOffset(CurrentChildrenBounds, 0, (!Advanced || XAlignTo == Align.None) ? XAlign : XAlignTo), XOffset, child);
}
}
if (YAlign != Align.None
&& i != anchorIndex)
{
if (YAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3Ex.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3Ex.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, -(childOrigin - firstOrigin).Y + (Advanced ? YOffset : 0), 0));
}
else
{
AlignAxis(1, YAlign, GetAlignToOffset(CurrentChildrenBounds, 1, (!Advanced || YAlignTo == Align.None) ? YAlign : YAlignTo), YOffset, child);
}
}
if (ZAlign != Align.None
&& i != anchorIndex)
{
if (ZAlign == Align.Origin)
{
// find the origin in world space of the child
var firstOrigin = Vector3Ex.Transform(Vector3.Zero, AnchorObject.WorldMatrix());
var childOrigin = Vector3Ex.Transform(Vector3.Zero, child.WorldMatrix());
child.Translate(new Vector3(0, 0, -(childOrigin - firstOrigin).Z + (Advanced ? ZOffset : 0)));
}
else
{
AlignAxis(2, ZAlign, GetAlignToOffset(CurrentChildrenBounds, 2, (!Advanced || ZAlignTo == Align.None) ? ZAlign : ZAlignTo), ZOffset, child);
}
}
i++;
}
}));
}
Invalidate(InvalidateType.Matrix);
return Task.CompletedTask;
}
public override void Remove(UndoBuffer undoBuffer)
{
using (RebuildLock())
{
// put everything back to where it was before the arrange started
if (OriginalChildrenBounds.Count == Children.Count)
{
int i = 0;
foreach (var child in Children)
{
// Where you are minus where you started to get back to where you started
child.Translate(-(child.GetAxisAlignedBoundingBox().MinXYZ - OriginalChildrenBounds[i].MinXYZ));
i++;
}
}
base.Remove(undoBuffer);
}
Invalidate(InvalidateType.Children);
}
public void UpdateControls(PublicPropertyChange change)
{
var editRow = change.Context.GetEditRow(nameof(XAlignTo));
if (editRow != null) editRow.Visible = Advanced && XAlign != Align.Origin;
editRow = change.Context.GetEditRow(nameof(XOffset));
if (editRow != null) editRow.Visible = Advanced;
editRow = change.Context.GetEditRow(nameof(YAlignTo));
if (editRow != null) editRow.Visible = Advanced && YAlign != Align.Origin;
editRow = change.Context.GetEditRow(nameof(YOffset));
if (editRow != null) editRow.Visible = Advanced;
editRow = change.Context.GetEditRow(nameof(ZAlignTo));
if (editRow != null) editRow.Visible = Advanced && ZAlign != Align.Origin;
editRow = change.Context.GetEditRow(nameof(ZOffset));
if (editRow != null) editRow.Visible = Advanced;
}
private static bool IsSet(FaceAlign variableToCheck, FaceAlign faceToCheckFor, FaceAlign faceToAssertNot)
{
if ((variableToCheck & faceToCheckFor) != 0)
{
if ((variableToCheck & faceToAssertNot) != 0)
{
throw new Exception("You cannot have both " + faceToCheckFor.ToString() + " and " + faceToAssertNot.ToString() + " set when calling Align. The are mutually exclusive.");
}
return true;
}
return false;
}
private void AlignAxis(int axis, Align align, double alignTo, double offset, IObject3D item)
{
var aabb = item.GetAxisAlignedBoundingBox();
var translate = Vector3.Zero;
switch (align)
{
case Align.Min:
translate[axis] = alignTo - aabb.MinXYZ[axis] + offset;
break;
case Align.Center:
translate[axis] = alignTo - aabb.Center[axis] + offset;
break;
case Align.Max:
translate[axis] = alignTo - aabb.MaxXYZ[axis] + offset;
break;
}
item.Translate(translate);
}
private double GetAlignToOffset(List<Aabb> currentChildrenBounds, int axis, Align alignTo)
{
switch (alignTo)
{
case Align.Min:
return currentChildrenBounds[AnchorObjectIndex].MinXYZ[axis];
case Align.Center:
return currentChildrenBounds[AnchorObjectIndex].Center[axis];
case Align.Max:
return currentChildrenBounds[AnchorObjectIndex].MaxXYZ[axis];
default:
throw new NotImplementedException();
}
}
}
}