Skip to content
Draft
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
36 changes: 27 additions & 9 deletions src/RvtIO/Parsing/TfBlockDeserializer.GElementGeometryGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal sealed record GElementFaceSurfaceLink(int FaceQueueIndex, int SurfaceQu
internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader,
IReadOnlyList<GElementGraphNode> Nodes, IReadOnlyList<GElementFaceSurfaceLink> FaceSurfaces, int End);

private enum GraphReadStep { Root, Queue, Tail, FaceSurface, Field }

/// <summary>Complete, footer-exact FIFO for the physical node kinds of the r22 layout. Owned Face -> Surface links
/// use queue indices, never handles, order or shape matching. Non-owning references and flags remain raw: this does
/// not reconstruct runtime pointers, document lookup, active faces or extrusion validity.</summary>
Expand All @@ -34,20 +36,23 @@ internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader,
int position = start + 14, end = start + 12 + (int)size, itemCount = 0;
var queue = new List<(ushort ClassId, uint Handle)>();
var nodes = new List<GElementGraphNode>();
string context = "root";
var step = GraphReadStep.Root;
int contextIndex = 0, contextValue = 0;
string? fieldName = null;
try
{
nodes.Add(Node(-1, 2007, (uint)owner));
for (int index = 0; index < queue.Count; index++)
{
var entry = queue[index];
context = $"fifo:{index}:{entry.ClassId}";
step = GraphReadStep.Queue;
(contextIndex, contextValue) = (index, entry.ClassId);
if (entry.ClassId is not (2007 or 2015 or 2009 or 2092 or 1996 or 1589 or 1243 or 1254
or 2014 or 522 or 1850 or 1848 or 1736 or 1999 or 1257 or 1974 or 983 or 1991 or 2238))
throw Fail;
nodes.Add(Node(index, entry.ClassId, entry.Handle));
}
context = "tail";
step = GraphReadStep.Tail;
if (position != end) throw Fail;
var surfaces = new List<GElementFaceSurfaceLink>();
// A missing, external or hand pointer stays as stored in its field.
Expand All @@ -57,14 +62,26 @@ internal sealed record OwnGElementGeometryGraph(int OwnerId, uint OpaqueHeader,
var pointer = node.Fields.Single(f => f.Declaring == 1589 && f.Name == "m_pSurf")
.Items.Single().Pointer!;
if (pointer.QueueIndex is not int surface) continue;
context = $"face-surface:{node.QueueIndex}:{surface}";
step = GraphReadStep.FaceSurface;
(contextIndex, contextValue) = (node.QueueIndex, surface);
if (nodes[surface + 1].ClassId is not (522 or 983)) throw Fail;
surfaces.Add(new(node.QueueIndex, surface));
}
failure = null;
return new(owner, BitConverter.ToUInt32(data, start + 4), nodes, surfaces, position - start);
}
catch (DecodeFail) { failure = context; return null; }
catch (DecodeFail)
{
failure = step switch
{
GraphReadStep.Queue => $"fifo:{contextIndex}:{contextValue}",
GraphReadStep.Tail => "tail",
GraphReadStep.FaceSurface => $"face-surface:{contextIndex}:{contextValue}",
GraphReadStep.Field => $"field:{contextIndex}:{fieldName}:{contextValue}",
_ => "root",
};
return null;
}

GElementGraphNode Node(int index, ushort cls, uint handle)
{
Expand All @@ -75,16 +92,17 @@ GElementGraphNode Node(int index, ushort cls, uint handle)

IReadOnlyList<GElementGraphField> ReadFields(ushort cls, int depth)
{
if (depth >= 32 || !GElementGraphSchemas.ContainsKey(cls)) throw Fail;
var result = new List<GElementGraphField>();
foreach (var field in GElementGraphFields(cls)) result.Add(Field(field, depth));
if (depth >= 32 || !GElementGraphFieldsByClass.TryGetValue(cls, out var fields)) throw Fail;
var result = new List<GElementGraphField>(fields.Length);
foreach (var field in fields) result.Add(Field(field, depth));
return result;
}

GElementGraphField Field(TfField field, int depth)
{
context = $"field:{field.Declaring}:{field.Name}:{position - start}";
int first = position - start;
step = GraphReadStep.Field;
(contextIndex, contextValue, fieldName) = (field.Declaring, first, field.Name);
int high = field.Mods >> 4;
uint? count = high == 5 ? (uint)Raw(4, false) : high == 1 ? (uint)field.Cnt!.Value : null;
uint length = count ?? 1;
Expand Down
3 changes: 3 additions & 0 deletions src/RvtIO/Parsing/TfBlockDeserializer.GElementGraphSchemas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ internal static partial class TfBlockDeserializer
[new("m_cda", 14, 0, 413, null)]),
};

private static readonly Dictionary<ushort, TfField[]> GElementGraphFieldsByClass =
GElementGraphSchemas.Keys.ToDictionary(id => id, id => GElementGraphFields(id).ToArray());

private static bool HasGElementGraphSchemas() => GElementGraphSchemas.All(pair =>
Schemas.TryGetValue(pair.Key, out var actual) && actual.Name == pair.Value.Name
&& actual.Base == pair.Value.Base && actual.Fields.SequenceEqual(pair.Value.Fields));
Expand Down
9 changes: 6 additions & 3 deletions tests/RvtIO.Tests/GElementGeometryGraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ public void EveryByteTruncationIncludingLaterPayloadRejectsAtomicallyWithReframe
}

[Theory]
[InlineData("faces")] [InlineData("edges")] [InlineData("interior")]
public void CountLimitAndRemainingBytesAreBothEnforced(string key)
[InlineData("faces", 1978, "m_pFaces")]
[InlineData("edges", 2092, "m_pEdges")]
[InlineData("interior", 1244, "m_interiorEdgePnts")]
public void CountLimitAndRemainingBytesAreBothEnforced(string key, int declaring, string field)
{
foreach(uint count in new[] { 100001u,100000u })
{
var (bytes,offsets) = Block(); BitConverter.GetBytes(count).CopyTo(bytes,offsets[key]);
Assert.Null(Read(bytes,out var failure)); Assert.NotNull(failure);
Assert.Null(Read(bytes, out var failure));
Assert.Equal($"field:{declaring}:{field}:{offsets[key]}", failure);
}
}

Expand Down
Loading