feat: merge overlapping buffer volumes in the visualizer

This commit is contained in:
anth64 committed 2026-11-05 21:05:06 +01:00
1 parent 6f30fc626c
commit 1c1ad9bfdd
2 files changed
+142 -1

No files matched your search

+5 -1
View File
@@ -163,7 +163,11 @@ internal static class ClaimLinkVisualizerClient
foreach ((int groupId, ClaimLinkBufferEntryDto entry) in knownEntries)
{
int color = HashToColor(groupId);
foreach (ClaimLinkBufferCuboidDto cuboid in entry.Cuboids)
List<Cuboidi> raw = new();
foreach (ClaimLinkBufferCuboidDto c in entry.Cuboids)
raw.Add(new Cuboidi(c.MinX, c.MinY, c.MinZ, c.MaxX, c.MaxY, c.MaxZ));
foreach (Cuboidi cuboid in CuboidUnion.Merge(raw))
{
positions.Add(new BlockPos(cuboid.MinX, cuboid.MinY, cuboid.MinZ));
positions.Add(new BlockPos(cuboid.MaxX, cuboid.MaxY, cuboid.MaxZ));
+137
View File
@@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using Vintagestory.API.MathTools;
namespace ClaimLink;
/// <summary>
/// Turns a set of possibly overlapping half-open cuboids into a set of disjoint cuboids covering
/// the same volume, so overlapping boxes render as one continuous shape.
/// </summary>
internal static class CuboidUnion
{
internal static List<Cuboidi> Merge(IEnumerable<Cuboidi> cuboids)
{
List<Cuboidi> result = new();
foreach (Cuboidi cuboid in cuboids)
{
List<Cuboidi> pieces = new() { Normalize(cuboid) };
foreach (Cuboidi existing in result)
{
List<Cuboidi> next = new();
foreach (Cuboidi piece in pieces)
Subtract(piece, existing, next);
pieces = next;
if (pieces.Count == 0)
break;
}
result.AddRange(pieces);
}
Coalesce(result);
return result;
}
private static Cuboidi Normalize(Cuboidi c) =>
new(c.MinX, c.MinY, c.MinZ, c.MaxX, c.MaxY, c.MaxZ);
// Appends the parts of `piece` not covered by `cut` (at most 6 boxes).
private static void Subtract(Cuboidi piece, Cuboidi cut, List<Cuboidi> output)
{
if (
piece.MaxX <= cut.MinX
|| piece.MinX >= cut.MaxX
|| piece.MaxY <= cut.MinY
|| piece.MinY >= cut.MaxY
|| piece.MaxZ <= cut.MinZ
|| piece.MinZ >= cut.MaxZ
)
{
output.Add(piece);
return;
}
int x0 = Math.Max(piece.MinX, cut.MinX);
int x1 = Math.Min(piece.MaxX, cut.MaxX);
int y0 = Math.Max(piece.MinY, cut.MinY);
int y1 = Math.Min(piece.MaxY, cut.MaxY);
int z0 = Math.Max(piece.MinZ, cut.MinZ);
int z1 = Math.Min(piece.MaxZ, cut.MaxZ);
if (piece.MinX < x0)
output.Add(new Cuboidi(piece.MinX, piece.MinY, piece.MinZ, x0, piece.MaxY, piece.MaxZ));
if (piece.MaxX > x1)
output.Add(new Cuboidi(x1, piece.MinY, piece.MinZ, piece.MaxX, piece.MaxY, piece.MaxZ));
if (piece.MinY < y0)
output.Add(new Cuboidi(x0, piece.MinY, piece.MinZ, x1, y0, piece.MaxZ));
if (piece.MaxY > y1)
output.Add(new Cuboidi(x0, y1, piece.MinZ, x1, piece.MaxY, piece.MaxZ));
if (piece.MinZ < z0)
output.Add(new Cuboidi(x0, y0, piece.MinZ, x1, y1, z0));
if (piece.MaxZ > z1)
output.Add(new Cuboidi(x0, y0, z1, x1, y1, piece.MaxZ));
}
// Joins face-adjacent boxes that share the same cross-section, to drop subtraction slivers.
private static void Coalesce(List<Cuboidi> boxes)
{
bool merged;
do
{
merged = false;
for (int i = 0; i < boxes.Count && !merged; i++)
for (int j = i + 1; j < boxes.Count && !merged; j++)
if (TryJoin(boxes[i], boxes[j], out Cuboidi joined))
{
boxes[i] = joined;
boxes.RemoveAt(j);
merged = true;
}
} while (merged);
}
private static bool TryJoin(Cuboidi a, Cuboidi b, out Cuboidi joined)
{
bool sameX = a.MinX == b.MinX && a.MaxX == b.MaxX;
bool sameY = a.MinY == b.MinY && a.MaxY == b.MaxY;
bool sameZ = a.MinZ == b.MinZ && a.MaxZ == b.MaxZ;
if (sameY && sameZ && (a.MaxX == b.MinX || b.MaxX == a.MinX))
joined = new Cuboidi(
Math.Min(a.MinX, b.MinX),
a.MinY,
a.MinZ,
Math.Max(a.MaxX, b.MaxX),
a.MaxY,
a.MaxZ
);
else if (sameX && sameZ && (a.MaxY == b.MinY || b.MaxY == a.MinY))
joined = new Cuboidi(
a.MinX,
Math.Min(a.MinY, b.MinY),
a.MinZ,
a.MaxX,
Math.Max(a.MaxY, b.MaxY),
a.MaxZ
);
else if (sameX && sameY && (a.MaxZ == b.MinZ || b.MaxZ == a.MinZ))
joined = new Cuboidi(
a.MinX,
a.MinY,
Math.Min(a.MinZ, b.MinZ),
a.MaxX,
a.MaxY,
Math.Max(a.MaxZ, b.MaxZ)
);
else
{
joined = null!;
return false;
}
return true;
}
}