Skip to content

Commit

Permalink
Add retesselate and canonicalize controls
Browse files Browse the repository at this point in the history
  • Loading branch information
praeclarum committed Feb 2, 2025
1 parent f076021 commit dc1a539
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
53 changes: 32 additions & 21 deletions Csg/Solid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public static Solid FromPolygons(List<Polygon> polygons)
}

public Solid Union(params Solid[] others)
{
return Union(others, true, true);
}

public Solid Union(Solid[] others, bool retesselate, bool canonicalize)
{
if (others.Length == 0) {
return this;
Expand All @@ -47,14 +52,17 @@ public Solid Union(params Solid[] others)
var a = csgs.Dequeue();
var b = csgs.Dequeue();

var n = a.UnionSub(b, false, false);
var n = a.UnionSub(b);
csgs.Enqueue(n);
}

return csgs.Dequeue().Retesselated().Canonicalized();
var result = csgs.Dequeue();
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
}

Solid UnionSub(Solid csg, bool retesselate, bool canonicalize)
Solid UnionSub(Solid csg)
{
if (!MayOverlap(csg))
{
Expand All @@ -72,10 +80,7 @@ Solid UnionSub(Solid csg, bool retesselate, bool canonicalize)

var newpolygons = new List<Polygon>(a.AllPolygons());
newpolygons.AddRange(b.AllPolygons());
var result = Solid.FromPolygons(newpolygons);
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
return FromPolygons(newpolygons);
}
}

Expand All @@ -90,17 +95,23 @@ Solid UnionForNonIntersecting(Solid csg)
}

public Solid Subtract(params Solid[] csgs)
{
return Subtract(csgs, true, true);
}

public Solid Subtract(Solid[] csgs, bool retesselate, bool canonicalize)
{
Solid result = this;
for (var i = 0; i < csgs.Length; i++)
{
var islast = (i == (csgs.Length - 1));
result = result.SubtractSub(csgs[i], islast, islast);
result = result.SubtractSub(csgs[i]);
}
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
}

Solid SubtractSub(Solid csg, bool retesselate, bool canonicalize)
Solid SubtractSub(Solid csg)
{
var a = new Tree(Bounds, Polygons);
var b = new Tree(csg.Bounds, csg.Polygons);
Expand All @@ -111,24 +122,27 @@ Solid SubtractSub(Solid csg, bool retesselate, bool canonicalize)
a.AddPolygons(b.AllPolygons());
a.Invert();

var result = Solid.FromPolygons(a.AllPolygons());
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
return FromPolygons(a.AllPolygons());
}

public Solid Intersect(params Solid[] csgs)
{
return Intersect(csgs, true, true);
}

public Solid Intersect(Solid[] csgs, bool retesselate, bool canonicalize)
{
var result = this;
for (var i = 0; i < csgs.Length; i++)
{
var islast = (i == (csgs.Length - 1));
result = result.IntersectSub(csgs[i], islast, islast);
result = result.IntersectSub(csgs[i]);
}
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
}

Solid IntersectSub(Solid csg, bool retesselate, bool canonicalize)
Solid IntersectSub(Solid csg)
{
var a = new Tree(Bounds, Polygons);
var b = new Tree(csg.Bounds, csg.Polygons);
Expand All @@ -141,10 +155,7 @@ Solid IntersectSub(Solid csg, bool retesselate, bool canonicalize)
a.AddPolygons(b.AllPolygons());
a.Invert();

var result = Solid.FromPolygons(a.AllPolygons());
if (retesselate) result = result.Retesselated();
if (canonicalize) result = result.Canonicalized();
return result;
return FromPolygons(a.AllPolygons());
}

public Solid Transform(Matrix4x4 matrix4x4)
Expand Down
26 changes: 20 additions & 6 deletions Csg/Solids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static Solid Cylinder(double r, double h, bool center = false)
return Cylinder(new CylinderOptions { Start = start, End = end, RadiusStart = r, RadiusEnd = r, });
}

public static Solid Union(params Solid[] csgs)
public static Solid Union(Solid[] csgs, bool retesselate, bool canonicalize)
{
if (csgs.Length == 0)
{
Expand All @@ -228,11 +228,16 @@ public static Solid Union(params Solid[] csgs)
{
var head = csgs[0];
var rest = csgs.Skip(1).ToArray();
return head.Union(rest);
return head.Union(rest, retesselate: retesselate, canonicalize: canonicalize);
}
}

public static Solid Difference(params Solid[] csgs)
public static Solid Union(params Solid[] csgs)
{
return Union(csgs, retesselate: true, canonicalize: true);
}

public static Solid Difference(Solid[] csgs, bool retesselate, bool canonicalize)
{
if (csgs.Length == 0)
{
Expand All @@ -246,11 +251,15 @@ public static Solid Difference(params Solid[] csgs)
{
var head = csgs[0];
var rest = csgs.Skip(1).ToArray();
return head.Subtract(rest);
return head.Subtract(rest, retesselate: retesselate, canonicalize: canonicalize);
}
}
public static Solid Difference(params Solid[] csgs)
{
return Difference(csgs, retesselate: true, canonicalize: true);
}

public static Solid Intersection(params Solid[] csgs)
public static Solid Intersection(Solid[] csgs, bool retesselate, bool canonicalize)
{
if (csgs.Length == 0 || csgs.Length == 1)
{
Expand All @@ -260,10 +269,15 @@ public static Solid Intersection(params Solid[] csgs)
{
var head = csgs[0];
var rest = csgs.Skip(1).ToArray();
return head.Intersect(rest);
return head.Intersect(rest, retesselate: retesselate, canonicalize: canonicalize);
}
}

public static Solid Intersection(params Solid[] csgs)
{
return Intersection(csgs, retesselate: true, canonicalize: true);
}

static Vertex NoTexVertex (Vector3D pos) => new Vertex (pos, new Vector2D (0, 0));

static readonly int[][][] cubeData =
Expand Down

0 comments on commit dc1a539

Please sign in to comment.