integrating offline changes

This commit is contained in:
Lars Brubaker 2023-03-10 17:15:55 -08:00
parent 121623bad3
commit 3f8eeda65b
125 changed files with 5442 additions and 5434 deletions

View file

@ -1,5 +1,5 @@
/*
Copyright (c) 2017, Lars Brubaker, John Lewin
Copyright (c) 2023, Lars Brubaker, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -45,36 +45,36 @@ using MatterHackers.VectorMath;
namespace MatterHackers.MatterControl.PartPreviewWindow.View3D
{
[ShowUpdateButton]
public class SubtractAndReplaceObject3D_2 : OperationSourceContainerObject3D, ISelectableChildContainer, ICustomEditorDraw, IPropertyGridModifier
{
public SubtractAndReplaceObject3D_2()
{
Name = "Subtract and Replace";
}
[ShowUpdateButton]
public class SubtractAndReplaceObject3D_2 : OperationSourceContainerObject3D, ISelectableChildContainer, ICustomEditorDraw, IPropertyGridModifier
{
public SubtractAndReplaceObject3D_2()
{
Name = "Subtract and Replace";
}
public bool DoEditorDraw(bool isSelected)
{
return isSelected;
}
public bool DoEditorDraw(bool isSelected)
{
return isSelected;
}
[HideFromEditor]
public SelectedChildren ComputedChildren { get; set; } = new SelectedChildren();
[HideFromEditor]
public SelectedChildren ComputedChildren { get; set; } = new SelectedChildren();
[DisplayName("Part(s) to Subtract and Replace")]
public SelectedChildren SelectedChildren { get; set; } = new SelectedChildren();
[DisplayName("Part(s) to Subtract and Replace")]
public SelectedChildren SelectedChildren { get; set; } = new SelectedChildren();
#if DEBUG
public ProcessingModes Processing { get; set; } = ProcessingModes.Polygons;
public ProcessingModes Processing { get; set; } = ProcessingModes.Polygons;
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ProcessingResolution OutputResolution { get; set; } = ProcessingResolution._64;
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ProcessingResolution OutputResolution { get; set; } = ProcessingResolution._64;
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public IplicitSurfaceMethod MeshAnalysis { get; set; }
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public IplicitSurfaceMethod MeshAnalysis { get; set; }
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ProcessingResolution InputResolution { get; set; } = ProcessingResolution._64;
[EnumDisplay(Mode = EnumDisplayAttribute.PresentationMode.Buttons)]
public ProcessingResolution InputResolution { get; set; } = ProcessingResolution._64;
#else
private ProcessingModes Processing { get; set; } = ProcessingModes.Polygons;
private ProcessingResolution OutputResolution { get; set; } = ProcessingResolution._64;
@ -82,255 +82,247 @@ namespace MatterHackers.MatterControl.PartPreviewWindow.View3D
private ProcessingResolution InputResolution { get; set; } = ProcessingResolution._64;
#endif
public void AddEditorTransparents(Object3DControlsLayer layer, List<Object3DView> transparentMeshes, DrawEventArgs e)
{
if (layer.Scene.SelectedItem != null
&& layer.Scene.SelectedItem == this)
{
var parentOfSubtractTargets = this.SourceContainer.FirstWithMultipleChildrenDescendantsAndSelf();
public void AddEditorTransparents(Object3DControlsLayer layer, List<Object3DView> transparentMeshes, DrawEventArgs e)
{
if (layer.Scene.SelectedItem != null
&& layer.Scene.SelectedItem == this)
{
var parentOfSubtractTargets = this.SourceContainer.FirstWithMultipleChildrenDescendantsAndSelf();
var removeObjects = parentOfSubtractTargets.Children
.Where(i => SelectedChildren.Contains(i.ID))
.SelectMany(c => c.VisibleMeshes())
.ToList();
var removeObjects = parentOfSubtractTargets.Children
.Where(i => SelectedChildren.Contains(i.ID))
.SelectMany(c => c.VisibleMeshes())
.ToList();
foreach (var item in removeObjects)
{
var color = item.WorldColor(checkOutputType: true);
transparentMeshes.Add(new Object3DView(item, color.WithAlpha(color.Alpha0To1 * .2)));
}
foreach (var item in removeObjects)
{
var color = item.WorldColor(checkOutputType: true);
transparentMeshes.Add(new Object3DView(item, color.WithAlpha(color.Alpha0To1 * .2)));
}
}
}
}
}
public void DrawEditor(Object3DControlsLayer layer, DrawEventArgs e)
{
return;
}
public void DrawEditor(Object3DControlsLayer layer, DrawEventArgs e)
{
return;
}
public AxisAlignedBoundingBox GetEditorWorldspaceAABB(Object3DControlsLayer layer)
{
return AxisAlignedBoundingBox.Empty();
}
public AxisAlignedBoundingBox GetEditorWorldspaceAABB(Object3DControlsLayer layer)
{
return AxisAlignedBoundingBox.Empty();
}
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();
}
else
{
base.OnInvalidate(invalidateType);
}
}
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();
}
else
{
base.OnInvalidate(invalidateType);
}
}
private CancellationTokenSource cancellationToken;
private CancellationTokenSource cancellationToken;
public bool IsBuilding => this.cancellationToken != null;
public bool IsBuilding => this.cancellationToken != null;
public void CancelBuild()
{
var threadSafe = this.cancellationToken;
if (threadSafe != null)
{
threadSafe.Cancel();
}
}
public void CancelBuild()
{
var threadSafe = this.cancellationToken;
if (threadSafe != null)
{
threadSafe.Cancel();
}
}
public override Task Rebuild()
{
var rebuildLocks = this.RebuilLockAll();
public override Task Rebuild()
{
var rebuildLocks = this.RebuilLockAll();
// spin up a task to calculate the paint
return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(),
null,
(reporter, cancellationTokenSource) =>
{
this.cancellationToken = cancellationTokenSource as CancellationTokenSource;
try
{
SubtractAndReplace(cancellationTokenSource.Token, reporter);
var newComputedChildren = new SelectedChildren();
// spin up a task to calculate the paint
return ApplicationController.Instance.Tasks.Execute("Replacing".Localize(),
null,
(reporter, cancellationTokenSource) =>
{
this.cancellationToken = cancellationTokenSource as CancellationTokenSource;
try
{
SubtractAndReplace(cancellationTokenSource.Token, reporter);
var newComputedChildren = new SelectedChildren();
foreach (var id in SelectedChildren)
{
newComputedChildren.Add(id);
}
foreach (var id in SelectedChildren)
{
newComputedChildren.Add(id);
}
ComputedChildren = newComputedChildren;
}
catch
{
}
ComputedChildren = newComputedChildren;
}
catch
{
}
this.cancellationToken = null;
UiThread.RunOnIdle(() =>
{
rebuildLocks.Dispose();
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children));
});
this.cancellationToken = null;
UiThread.RunOnIdle(() =>
{
rebuildLocks.Dispose();
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Children));
});
return Task.CompletedTask;
});
}
return Task.CompletedTask;
});
}
public void SubtractAndReplace()
{
SubtractAndReplace(CancellationToken.None, null);
}
public void SubtractAndReplace()
{
SubtractAndReplace(CancellationToken.None, null);
}
private void SubtractAndReplace(CancellationToken cancellationToken, IProgress<ProgressStatus> reporter)
{
SourceContainer.Visible = true;
RemoveAllButSource();
private void SubtractAndReplace(CancellationToken cancellationToken, Action<double, string> reporter)
{
SourceContainer.Visible = true;
RemoveAllButSource();
var parentOfPaintTargets = SourceContainer.FirstWithMultipleChildrenDescendantsAndSelf();
var parentOfPaintTargets = SourceContainer.FirstWithMultipleChildrenDescendantsAndSelf();
if (parentOfPaintTargets.Children.Count() < 2)
{
if (parentOfPaintTargets.Children.Count() == 1)
{
this.Children.Add(SourceContainer.Clone());
SourceContainer.Visible = false;
}
if (parentOfPaintTargets.Children.Count() < 2)
{
if (parentOfPaintTargets.Children.Count() == 1)
{
this.Children.Add(SourceContainer.Clone());
SourceContainer.Visible = false;
}
return;
}
return;
}
SubtractObject3D_2.CleanUpSelectedChildrenIDs(this);
SubtractObject3D_2.CleanUpSelectedChildrenIDs(this);
var paintObjects = parentOfPaintTargets.Children
.Where((i) => SelectedChildren
.Contains(i.ID))
.SelectMany(c => c.VisibleMeshes())
.ToList();
var paintObjects = parentOfPaintTargets.Children
.Where((i) => SelectedChildren
.Contains(i.ID))
.SelectMany(c => c.VisibleMeshes())
.ToList();
var keepItems = parentOfPaintTargets.Children
.Where((i) => !SelectedChildren
.Contains(i.ID));
var keepItems = parentOfPaintTargets.Children
.Where((i) => !SelectedChildren
.Contains(i.ID));
var keepVisibleItems = keepItems.SelectMany(c => c.VisibleMeshes()).ToList();
var keepVisibleItems = keepItems.SelectMany(c => c.VisibleMeshes()).ToList();
if (paintObjects.Any()
&& keepVisibleItems.Any())
{
var totalOperations = paintObjects.Count * keepVisibleItems.Count;
double amountPerOperation = 1.0 / totalOperations;
double ratioCompleted = 0;
if (paintObjects.Any()
&& keepVisibleItems.Any())
{
var totalOperations = paintObjects.Count * keepVisibleItems.Count;
double amountPerOperation = 1.0 / totalOperations;
double ratioCompleted = 0;
var progressStatus = new ProgressStatus
{
Status = "Do CSG"
};
foreach (var keep in keepVisibleItems)
{
var keepResultsMesh = keep.Mesh;
var keepWorldMatrix = keep.WorldMatrix(SourceContainer);
foreach (var keep in keepVisibleItems)
{
var keepResultsMesh = keep.Mesh;
var keepWorldMatrix = keep.WorldMatrix(SourceContainer);
foreach (var paint in paintObjects)
{
if (cancellationToken.IsCancellationRequested)
{
SourceContainer.Visible = true;
RemoveAllButSource();
return;
}
foreach (var paint in paintObjects)
{
if (cancellationToken.IsCancellationRequested)
{
SourceContainer.Visible = true;
RemoveAllButSource();
return;
}
Mesh paintMesh = BooleanProcessing.Do(keepResultsMesh,
keepWorldMatrix,
// paint data
paint.Mesh,
paint.WorldMatrix(SourceContainer),
// operation type
CsgModes.Intersect,
Processing,
InputResolution,
OutputResolution,
// reporting data
reporter,
amountPerOperation,
ratioCompleted,
cancellationToken);
Mesh paintMesh = BooleanProcessing.Do(keepResultsMesh,
keepWorldMatrix,
// paint data
paint.Mesh,
paint.WorldMatrix(SourceContainer),
// operation type
CsgModes.Intersect,
Processing,
InputResolution,
OutputResolution,
// reporting data
reporter,
amountPerOperation,
ratioCompleted,
progressStatus,
cancellationToken);
keepResultsMesh = BooleanProcessing.Do(keepResultsMesh,
keepWorldMatrix,
// point data
paint.Mesh,
paint.WorldMatrix(SourceContainer),
// operation type
CsgModes.Subtract,
Processing,
InputResolution,
OutputResolution,
// reporting data
reporter,
amountPerOperation,
ratioCompleted,
cancellationToken);
keepResultsMesh = BooleanProcessing.Do(keepResultsMesh,
keepWorldMatrix,
// point data
paint.Mesh,
paint.WorldMatrix(SourceContainer),
// operation type
CsgModes.Subtract,
Processing,
InputResolution,
OutputResolution,
// reporting data
reporter,
amountPerOperation,
ratioCompleted,
progressStatus,
cancellationToken);
// after the first time we get a result the results mesh is in the right coordinate space
keepWorldMatrix = Matrix4X4.Identity;
// after the first time we get a result the results mesh is in the right coordinate space
keepWorldMatrix = Matrix4X4.Identity;
// store our intersection (paint) results mesh
var paintResultsItem = new Object3D()
{
Mesh = paintMesh,
Visible = false,
OwnerID = paint.ID
};
// copy all the properties but the matrix
paintResultsItem.CopyWorldProperties(paint, SourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible)));
// and add it to this
this.Children.Add(paintResultsItem);
// store our intersection (paint) results mesh
var paintResultsItem = new Object3D()
{
Mesh = paintMesh,
Visible = false,
OwnerID = paint.ID
};
// copy all the properties but the matrix
paintResultsItem.CopyWorldProperties(paint, SourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible)));
// and add it to this
this.Children.Add(paintResultsItem);
// report our progress
ratioCompleted += amountPerOperation;
reporter?.Invoke(ratioCompleted, "Do CSG".Localize());
}
// report our progress
ratioCompleted += amountPerOperation;
progressStatus.Progress0To1 = ratioCompleted;
reporter?.Report(progressStatus);
}
// store our results mesh
var keepResultsItem = new Object3D()
{
Mesh = keepResultsMesh,
Visible = false,
OwnerID = keep.ID
};
// copy all the properties but the matrix
keepResultsItem.CopyWorldProperties(keep, SourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible)));
// and add it to this
this.Children.Add(keepResultsItem);
}
// store our results mesh
var keepResultsItem = new Object3D()
{
Mesh = keepResultsMesh,
Visible = false,
OwnerID = keep.ID
};
// copy all the properties but the matrix
keepResultsItem.CopyWorldProperties(keep, SourceContainer, Object3DPropertyFlags.All & (~(Object3DPropertyFlags.Matrix | Object3DPropertyFlags.Visible)));
// and add it to this
this.Children.Add(keepResultsItem);
}
foreach (var child in Children)
{
child.Visible = true;
}
foreach (var child in Children)
{
child.Visible = true;
}
SourceContainer.Visible = false;
}
}
SourceContainer.Visible = false;
}
}
public void UpdateControls(PublicPropertyChange change)
{
change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(OutputResolution), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(MeshAnalysis), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons && MeshAnalysis == IplicitSurfaceMethod.Grid);
}
}
public void UpdateControls(PublicPropertyChange change)
{
change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(OutputResolution), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(MeshAnalysis), () => Processing != ProcessingModes.Polygons);
change.SetRowVisible(nameof(InputResolution), () => Processing != ProcessingModes.Polygons && MeshAnalysis == IplicitSurfaceMethod.Grid);
}
}
}