Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/NwcIO/Internal/Parsing/MeshParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static void ParseViaToc(
}

int curPos = 0;
var usedIds = new HashSet<int>();
var usedIds = new MeshIdAllocator();
for (int r = 0; r < recordCount; r++)
{
int rLen = recordLengths[r];
Expand Down Expand Up @@ -281,6 +281,35 @@ private static void ParseViaHeuristicScan(
}
}

/// <summary>Tracks claimed mesh ids. Fallback probing resumes from the last fallback, so many duplicates stay linear.</summary>
internal sealed class MeshIdAllocator
{
private readonly HashSet<int> _used = [];
private int _nextFallback = int.MinValue;

/// <summary>Number of fallback candidates tried so far (exposed for complexity tests).</summary>
public int Probes { get; private set; }

public int Claim(int id, int fallbackStart)
{
if (_used.Add(id))
{
return id;
}

id = Math.Max(fallbackStart, _nextFallback);
Probes++;
while (!_used.Add(id))
{
id++;
Probes++;
}

_nextFallback = id + 1;
return id;
}
}

private static NwcMesh? TryBuildMesh(
byte[] buffer,
int recordOffset,
Expand All @@ -290,7 +319,7 @@ private static void ParseViaHeuristicScan(
int recordLength,
int meshId,
bool lines,
HashSet<int>? usedIds = null)
MeshIdAllocator? usedIds = null)
{
if (vertexOffset < 0 || vertexCount > (buffer.Length - vertexOffset) / 12)
{
Expand Down Expand Up @@ -342,13 +371,9 @@ private static void ParseViaHeuristicScan(

// A merged document can repeat ids. Keep the first occurrence exact and give later duplicates a
// unique fallback, so clash detection and IFC export do not silently pair unrelated meshes.
if (usedIds is not null && !usedIds.Add(id))
if (usedIds is not null)
{
id = meshId;
while (!usedIds.Add(id))
{
id++;
}
id = usedIds.Claim(id, meshId);
}

return new NwcMesh
Expand Down
36 changes: 36 additions & 0 deletions tests/NwcIO.Tests/MeshParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,42 @@ public void Parse_TrailerIds_PreserveZeroAndMakeDuplicatesUnique()
Assert.Equal([5, 0, 2], meshes.Select(mesh => mesh.Id));
}

[Fact]
public void Parse_ManyDuplicateTrailerIds_AreAllUnique()
{
const int count = 2000;
var records = Enumerable.Range(0, count)
.Select(i => TrailerRecord(i, meshId: 5, [0, 1, 2], "M"))
.ToArray();

var meshes = ParseRecords(records);

Assert.Equal(count, meshes.Count);
Assert.Equal(count, meshes.Select(mesh => mesh.Id).Distinct().Count());
Assert.Equal(5, meshes[0].Id);
}

[Fact]
public void MeshIdAllocator_DuplicatesOfOccupiedRange_ProbeLinearly()
{
var allocator = new MeshParser.MeshIdAllocator();
for (int id = 1000; id < 2000; id++)
{
allocator.Claim(id, id);
}

var claimed = new HashSet<int>();
for (int i = 0; i < 1000; i++)
{
claimed.Add(allocator.Claim(1000, fallbackStart: 1000 + i));
}

Assert.Equal(1000, claimed.Count);
Assert.DoesNotContain(claimed, id => id is >= 1000 and < 2000);
// A probe restarted from fallbackStart for every duplicate needs about 1,000,000 tries here.
Assert.True(allocator.Probes < 10_000, $"Probes: {allocator.Probes}");
}

[Theory]
[InlineData("magic")]
[InlineData("indexCount")]
Expand Down
Loading