Merge pull request #5362 from larsbrubaker/main

main
This commit is contained in:
Lars Brubaker 2022-08-18 11:26:21 -07:00 committed by GitHub
commit dbe3e5f609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 127 additions and 73 deletions

View file

@ -27,6 +27,7 @@ of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Threading.Tasks;
using DualContouring;
using g3;
@ -49,6 +50,7 @@ namespace MatterHackers.MatterControl.DesignTools
Boxes_3,
Sphere,
Cylinder,
Subtract,
}
public DualContouringObject3D()
@ -100,27 +102,37 @@ namespace MatterHackers.MatterControl.DesignTools
{
this.DebugDepth("Rebuild");
return Task.Run(() =>
{
using (RebuildLock())
{
using (new CenterAndHeightMaintainer(this))
{
ISdf shape = null;
switch (Shape)
{
case Shapes.Box:
shape = new Box()
{
Size = new Vector3(Size, Size, Size)
};
break;
var title = "Dual Contouring";
case Shapes.Boxes_3:
shape = new Union()
{
Items = new ISdf[]
if (Ouptput != OuptutTypes.DualContouring)
{
title = "Marching Cubes";
}
return ApplicationController.Instance.Tasks.Execute(
title,
null,
(reporter, cancellationTokenSource) =>
{
using (RebuildLock())
{
using (new CenterAndHeightMaintainer(this))
{
ISdf shape = null;
switch (Shape)
{
case Shapes.Box:
shape = new Box()
{
Size = new Vector3(Size, Size, Size)
};
break;
case Shapes.Boxes_3:
shape = new Union()
{
Items = new ISdf[]
{
new Transform(new Box()
{
Size = new Vector3(Size, Size, Size)
@ -133,15 +145,15 @@ namespace MatterHackers.MatterControl.DesignTools
{
Size = new Vector3(Size, Size, Size)
}, Matrix4X4.CreateRotationY(MathHelper.Tau * .2) * Matrix4X4.CreateRotationX(MathHelper.Tau * .3)),
}
};
break;
}
};
break;
case Shapes.Box_And_Sphere:
shape = new Union()
{
Items = new ISdf[]
case Shapes.Box_And_Sphere:
shape = new Union()
{
Items = new ISdf[]
{
new Transform(new Sphere()
{
Radius = Size / 2
@ -150,61 +162,103 @@ namespace MatterHackers.MatterControl.DesignTools
{
Size = new Vector3(Size, Size, Size)
}, Matrix4X4.CreateRotationZ(MathHelper.Tau * .2)),
}
};
break;
}
};
break;
case Shapes.Sphere:
shape = new Sphere()
{
Radius = Size
};
break;
case Shapes.Sphere:
shape = new Sphere()
{
Radius = Size
};
break;
case Shapes.Cylinder:
shape = new Cylinder()
{
Radius = Size,
Height = Size
};
break;
}
case Shapes.Cylinder:
shape = new Cylinder()
{
Radius = Size,
Height = Size
};
break;
var bounds = shape.Bounds;
bounds.Expand(.1);
if (Iterations > 7)
{
Iterations = 7;
}
case Shapes.Subtract:
shape = new Intersection()
{
Items = new ISdf[]
{
new Subtraction()
{
Items = new ISdf[]
{
new Sphere()
{
Radius = Size
},
new Transform(new Cylinder()
{
Height = Size*2,
Radius = Size/2
}, Matrix4X4.CreateRotationX(MathHelper.Tau / 4)),
new Transform(new Cylinder()
{
Height = Size*2,
Radius = Size/2
}, Matrix4X4.CreateRotationY(MathHelper.Tau / 4)),
new Cylinder()
{
Height = Size*2,
Radius = Size/2
}
}
},
new Box()
{
Size = new Vector3(Size * 1.5, Size * 1.5, Size * 1.5)
}
}
};
break;
}
if (Ouptput == OuptutTypes.DualContouring)
{
var root = Octree.BuildOctree(shape.Sdf, bounds.MinXYZ, bounds.Size, Iterations, Threshold);
Mesh = Octree.GenerateMeshFromOctree(root);
}
else
{
var min = shape.Bounds.MinXYZ;
var max = shape.Bounds.MaxXYZ;
var c = new MarchingCubes()
var bounds = shape.Bounds;
bounds.Expand(.1);
if (Iterations > 8)
{
Implicit = new SdfToImplicit(shape),
Bounds = new AxisAlignedBox3d(min.X, min.Y, min.Z, max.X, max.Y, max.Z),
};
c.Generate();
MeshNormals.QuickCompute(c.Mesh); // generate normals
Mesh = c.Mesh.ToMesh();
Iterations = 8;
}
if (Ouptput == OuptutTypes.DualContouring)
{
var root = Octree.BuildOctree(shape.Sdf, bounds.MinXYZ, bounds.Size, Iterations, Threshold);
Mesh = Octree.GenerateMeshFromOctree(root);
}
else
{
var min = shape.Bounds.MinXYZ;
var max = shape.Bounds.MaxXYZ;
var size = shape.Bounds.Size;
var maxSize = Math.Max(size.X, Math.Max(size.Y, size.Z));
var c = new MarchingCubes()
{
Implicit = new SdfToImplicit(shape),
Bounds = new AxisAlignedBox3d(min.X, min.Y, min.Z, max.X, max.Y, max.Z),
CubeSize = maxSize / Math.Pow(2, Iterations)
};
c.Generate();
MeshNormals.QuickCompute(c.Mesh); // generate normals
Mesh = c.Mesh.ToMesh();
}
}
}
}
Invalidate(InvalidateType.DisplayValues);
Invalidate(InvalidateType.DisplayValues);
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh));
return Task.CompletedTask;
});
this.CancelAllParentBuilding();
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Mesh));
return Task.CompletedTask;
});
}
public class SdfToImplicit : ImplicitFunction3d

@ -1 +1 @@
Subproject commit 84b6947e49f3fb1cdb5e777ecf7451f8369c91ea
Subproject commit ad2d82da3826dd2f64b4b1ab6d696a7c758539d7