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
10 changes: 9 additions & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Stream overloads leave the supplied stream open.

| Type | Description |
|---|---|
| `NwcClashDetector` | Broad-phase BVH and narrow-phase 3D triangle-to-triangle collision engine (`Run` over a document or federated assembly, driven by an `NwcClashTest`). Frozen: bug fixes only. |
| `NwcClashDetector` | Broad-phase bounding-box checks and narrow-phase 3D triangle-to-triangle collision detection (`Run` over a document or federated assembly, driven by an `NwcClashTest`). Frozen: bug fixes only. |
| `NwcClashTest`, `NwcClashSelection`, `NwcClashResult`, `NwcClashObject` | Clash test definition (type, tolerance, per-side selections), results, and the involved elements. Frozen: bug fixes only. |
| `NwcClashBatch`, `NwcClashRule`, `NwcClashComment` | Batch test container, filtering rules (ignore thresholds, custom predicates), and review comments. Frozen: bug fixes only. |
| `NwcClashGroup` | Grouped clash clusters produced by `NwcClashGrouper`. Frozen: bug fixes only. |
Expand All @@ -59,6 +59,14 @@ Stream overloads leave the supplied stream open.
| `NwcXmlClashSerializer` | Roundtrip Navisworks Clash Report XML (`nw-clashreport-11.0.xsd`) reader and writer. Frozen: bug fixes only. |
| `NwcNwfManifest` | Federation manifest used by `NwcAssembly` tooling. |

`NwcClashDetector.Run` and the `RunClashDetection` methods on `NwcDocument` and `NwcAssembly`
offer overloads with `IProgress<NwcProgress>` and `CancellationToken`. The `clash-detection` phase
reports the number of meshes processed in the first selection, starting at zero and reaching the
total on completion. An empty selection reports `0/0` once. Cancellation is checked between meshes
and during triangle comparisons, including clearance tests. If cancelled, the run throws
`OperationCanceledException` and discards the results it added, preserving prior results and groups.
The convenience methods register a test only after it completes.

## 4D TimeLiner, Selection Sets & Appearance (`NwcIO.Model.TimeLiner`, `.Sets`, `.Appearance`)

| Type | Description |
Expand Down
103 changes: 86 additions & 17 deletions src/NwcIO/Compatibility/Clash/NwcClashDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,45 @@ public static class NwcClashDetector
/// Executes clash detection across an individual Navisworks document according to the specified test configuration.
/// </summary>
/// <exception cref="ArgumentException">An active set selection names a set that the document doesn't contain.</exception>
public static NwcClashTest Run(NwcDocument document, NwcClashTest test)
public static NwcClashTest Run(NwcDocument document, NwcClashTest test) =>
Run(document, test, null, default);

/// <summary>Runs clash detection across a document with cancellation and progress.</summary>
/// <exception cref="OperationCanceledException">The cancellation token was cancelled.</exception>
public static NwcClashTest Run(
NwcDocument document, NwcClashTest test, IProgress<NwcProgress>? progress = null,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(document);
ArgumentNullException.ThrowIfNull(test);
cancellationToken.ThrowIfCancellationRequested();
ThrowIfSetIsMissing([document], test);

var meshesA = ResolveSelection(document, test.SelectionA);
var meshesB = ResolveSelection(document, test.SelectionB);
var meshesA = ResolveSelection(document, test.SelectionA, cancellationToken);
var meshesB = ResolveSelection(document, test.SelectionB, cancellationToken);

RunCore(meshesA, meshesB, test, document.FileInfo.SourcePath ?? "Model", document);
RunWithCancellation(test, meshesA.Count, progress,
() => RunCore(meshesA, meshesB, test, document.FileInfo.SourcePath ?? "Model", document, progress, cancellationToken),
cancellationToken);
return test;
}

/// <summary>
/// Executes clash detection across all contributing models of a federated Navisworks assembly.
/// </summary>
/// <exception cref="ArgumentException">An active set selection names a set that none of the models contains.</exception>
public static NwcClashTest Run(NwcAssembly assembly, NwcClashTest test)
public static NwcClashTest Run(NwcAssembly assembly, NwcClashTest test) =>
Run(assembly, test, null, default);

/// <summary>Runs clash detection across a federated assembly with cancellation and progress.</summary>
/// <exception cref="OperationCanceledException">The cancellation token was cancelled.</exception>
public static NwcClashTest Run(
NwcAssembly assembly, NwcClashTest test, IProgress<NwcProgress>? progress = null,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(assembly);
ArgumentNullException.ThrowIfNull(test);
cancellationToken.ThrowIfCancellationRequested();
ThrowIfSetIsMissing(assembly.Models, test);

var itemsA = new List<(NwcMesh Mesh, string ModelName, string? Layer, string ModelKey)>();
Expand All @@ -42,32 +60,64 @@ public static NwcClashTest Run(NwcAssembly assembly, NwcClashTest test)

for (int m = 0; m < assembly.Models.Count; m++)
{
cancellationToken.ThrowIfCancellationRequested();
var doc = assembly.Models[m];
string docName = Path.GetFileNameWithoutExtension(doc.FileInfo.SourcePath ?? $"Model_{m + 1}");

var selA = ResolveSelection(doc, test.SelectionA);
var selA = ResolveSelection(doc, test.SelectionA, cancellationToken);
for (int i = 0; i < selA.Count; i++)
{
itemsA.Add((selA[i], docName, selA[i].Node?.Parent?.Name, modelKeys[m]));
}

var selB = ResolveSelection(doc, test.SelectionB);
var selB = ResolveSelection(doc, test.SelectionB, cancellationToken);
for (int i = 0; i < selB.Count; i++)
{
itemsB.Add((selB[i], docName, selB[i].Node?.Parent?.Name, modelKeys[m]));
}
}

RunCoreMultiModel(itemsA, itemsB, test);
RunWithCancellation(test, itemsA.Count, progress,
() => RunCoreMultiModel(itemsA, itemsB, test, progress, cancellationToken), cancellationToken);
return test;
}

private static void RunWithCancellation(
NwcClashTest test, int total, IProgress<NwcProgress>? progress, Action run, CancellationToken cancellationToken)
{
int firstNewResult = test.Results.Count;
try
{
cancellationToken.ThrowIfCancellationRequested();
run();
ReportProgress(progress, total, total, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
if (test.Results.Count > firstNewResult)
{
test.Results.RemoveRange(firstNewResult, test.Results.Count - firstNewResult);
}

throw;
}
}

private static void ReportProgress(
IProgress<NwcProgress>? progress, int completed, int total, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
progress?.Report(new NwcProgress("clash-detection", completed, total));
}

private static void RunCore(
IReadOnlyList<NwcMesh> meshesA,
IReadOnlyList<NwcMesh> meshesB,
NwcClashTest test,
string modelName,
NwcDocument document)
NwcDocument document,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
bool sameSelection = ReferenceEquals(meshesA, meshesB) ||
meshesA.SequenceEqual(meshesB, ReferenceEqualityComparer.Instance);
Expand All @@ -85,13 +135,16 @@ private static void RunCore(

for (int i = 0; i < meshesA.Count; i++)
{
ReportProgress(progress, i, meshesA.Count, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
var meshA = meshesA[i];
var boxA = meshA.GetWorldBoundingBox();
if (boxA.IsEmpty) continue;
bool canVisitReverse = !sameSelection && meshesInB!.Contains(meshA);

for (var j = sameSelection ? i + 1 : 0; j < meshesB.Count; j++)
{
cancellationToken.ThrowIfCancellationRequested();
var meshB = meshesB[j];
if (ReferenceEquals(meshA, meshB) || meshA.Id == meshB.Id || IsSameElement(meshA, meshB)) continue;
// The reverse pair was already visited from the earlier mesh in A.
Expand All @@ -112,7 +165,7 @@ private static void RunCore(
if (!expandedA.Intersects(boxB)) continue;

// Narrow phase: triangle intersection (Hard) or mesh distance (Clearance)
if (TryGetClash(test, meshA, meshB, out var clashPoint, out double distance))
if (TryGetClash(test, meshA, meshB, cancellationToken, out var clashPoint, out double distance))
{
int clashIndex = test.Results.Count + 1;
string clashName = $"Clash{clashIndex}";
Expand Down Expand Up @@ -167,7 +220,9 @@ private static void RunCore(
private static void RunCoreMultiModel(
List<(NwcMesh Mesh, string ModelName, string? Layer, string ModelKey)> itemsA,
List<(NwcMesh Mesh, string ModelName, string? Layer, string ModelKey)> itemsB,
NwcClashTest test)
NwcClashTest test,
IProgress<NwcProgress>? progress,
CancellationToken cancellationToken)
{
bool sameSelection = itemsA.Select(item => item.Mesh)
.SequenceEqual(itemsB.Select(item => item.Mesh), ReferenceEqualityComparer.Instance);
Expand All @@ -184,13 +239,16 @@ private static void RunCoreMultiModel(
}
for (int i = 0; i < itemsA.Count; i++)
{
ReportProgress(progress, i, itemsA.Count, cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
var itemA = itemsA[i];
var boxA = itemA.Mesh.GetWorldBoundingBox();
if (boxA.IsEmpty) continue;
bool canVisitReverse = !sameSelection && meshesInB!.Contains(itemA.Mesh);

for (var j = sameSelection ? i + 1 : 0; j < itemsB.Count; j++)
{
cancellationToken.ThrowIfCancellationRequested();
var itemB = itemsB[j];
if (ReferenceEquals(itemA.Mesh, itemB.Mesh) || IsSameElement(itemA.Mesh, itemB.Mesh)) continue;
// Mesh IDs can repeat across models; only references identify pairs.
Expand All @@ -209,7 +267,7 @@ private static void RunCoreMultiModel(

if (!expandedA.Intersects(boxB)) continue;

if (TryGetClash(test, itemA.Mesh, itemB.Mesh, out var clashPoint, out double distance))
if (TryGetClash(test, itemA.Mesh, itemB.Mesh, cancellationToken, out var clashPoint, out double distance))
{
int clashIndex = test.Results.Count + 1;
string clashName = $"Clash{clashIndex}";
Expand Down Expand Up @@ -270,6 +328,7 @@ private static bool TryGetClash(
NwcClashTest test,
NwcMesh meshA,
NwcMesh meshB,
CancellationToken cancellationToken,
out Vector3d clashPoint,
out double distance)
{
Expand All @@ -286,14 +345,14 @@ private static bool TryGetClash(
return false;
}

var measurement = NwcIO.Model.Measure.NwcMeasureEngine.MeasureMeshToMesh(meshA, meshB);
var measurement = Measure.NwcMeasureEngine.MeasureMeshToMesh(meshA, meshB, useWorldCoordinates: true, cancellationToken);
clashPoint = (measurement.PointA + measurement.PointB) * 0.5;
distance = measurement.Distance;
return measurement.Distance <= tolerance;
}

case NwcClashTestType.Duplicates:
if (!IsDuplicate(meshA, meshB, tolerance))
if (!IsDuplicate(meshA, meshB, tolerance, cancellationToken))
{
distance = 0;
return false;
Expand All @@ -318,7 +377,7 @@ private static bool TryGetClash(
}

default:
if (CheckMeshIntersection(meshA, meshB, out clashPoint, out double penetration))
if (CheckMeshIntersection(meshA, meshB, cancellationToken, out clashPoint, out double penetration))
{
// A zero-depth AABB overlap is a coplanar or touching surface; it is kept rather than
// classified as a below-tolerance penetration.
Expand All @@ -341,7 +400,8 @@ private static bool TryGetClash(
/// Duplicate geometry: same world vertices within the tolerance and the same triangle/line topology.
/// Transform, name, id and material are ignored.
/// </summary>
internal static bool IsDuplicate(NwcMesh meshA, NwcMesh meshB, double tolerance)
internal static bool IsDuplicate(
NwcMesh meshA, NwcMesh meshB, double tolerance, CancellationToken cancellationToken = default)
{
if (ReferenceEquals(meshA, meshB))
{
Expand All @@ -364,6 +424,7 @@ internal static bool IsDuplicate(NwcMesh meshA, NwcMesh meshB, double tolerance)
double toleranceSquared = tolerance * tolerance;
for (int i = 0; i < verticesA.Count; i++)
{
if ((i & 255) == 0) cancellationToken.ThrowIfCancellationRequested();
Vector3d delta = verticesA[i] - verticesB[i];
// Negated form so a NaN tolerance fails closed.
if (!(Vector3d.Dot(delta, delta) <= toleranceSquared))
Expand All @@ -374,6 +435,7 @@ internal static bool IsDuplicate(NwcMesh meshA, NwcMesh meshB, double tolerance)

for (int i = 0; i < meshA.Indices.Count; i++)
{
if ((i & 255) == 0) cancellationToken.ThrowIfCancellationRequested();
if (meshA.Indices[i] != meshB.Indices[i])
{
return false;
Expand All @@ -382,6 +444,7 @@ internal static bool IsDuplicate(NwcMesh meshA, NwcMesh meshB, double tolerance)

for (int i = 0; i < meshA.LineIndices.Count; i++)
{
if ((i & 255) == 0) cancellationToken.ThrowIfCancellationRequested();
if (meshA.LineIndices[i] != meshB.LineIndices[i])
{
return false;
Expand All @@ -394,6 +457,7 @@ internal static bool IsDuplicate(NwcMesh meshA, NwcMesh meshB, double tolerance)
private static bool CheckMeshIntersection(
NwcMesh a,
NwcMesh b,
CancellationToken cancellationToken,
out Vector3d clashPoint,
out double penetrationDepth)
{
Expand Down Expand Up @@ -433,6 +497,7 @@ private static bool CheckMeshIntersection(

for (int i = 0; i + 2 < indicesA.Count; i += 3)
{
cancellationToken.ThrowIfCancellationRequested();
int ia0 = indicesA[i];
int ia1 = indicesA[i + 1];
int ia2 = indicesA[i + 2];
Expand All @@ -444,6 +509,7 @@ private static bool CheckMeshIntersection(

for (int j = 0; j + 2 < indicesB.Count; j += 3)
{
if (j % (3 * 256) == 0) cancellationToken.ThrowIfCancellationRequested();
int ib0 = indicesB[j];
int ib1 = indicesB[j + 1];
int ib2 = indicesB[j + 2];
Expand Down Expand Up @@ -494,8 +560,10 @@ private static List<int> CreateDefaultIndices(int count)
return indices;
}

private static IReadOnlyList<NwcMesh> ResolveSelection(NwcDocument document, NwcClashSelection sel)
private static IReadOnlyList<NwcMesh> ResolveSelection(
NwcDocument document, NwcClashSelection sel, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (sel.ExplicitNodeIds.Count > 0)
{
var idSet = new HashSet<int>(sel.ExplicitNodeIds);
Expand All @@ -515,6 +583,7 @@ private static IReadOnlyList<NwcMesh> ResolveSelection(NwcDocument document, Nwc
var matchedMeshes = new List<NwcMesh>();
for (int s = 0; s < sel.SetNames.Count; s++)
{
cancellationToken.ThrowIfCancellationRequested();
var set = document.FindSet(sel.SetNames[s]);
if (set is NwcSelectionSet selSet)
{
Expand Down
8 changes: 7 additions & 1 deletion src/NwcIO/Compatibility/Measure/NwcMeasureEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ public static NwcPointToMeshMeasurement MeasurePointToMesh(Vector3d point, NwcMe
/// <summary>
/// Measures the clearance (minimum distance) between two 3D polygon meshes.
/// </summary>
public static NwcMeshToMeshMeasurement MeasureMeshToMesh(NwcMesh meshA, NwcMesh meshB, bool useWorldCoordinates = true)
public static NwcMeshToMeshMeasurement MeasureMeshToMesh(NwcMesh meshA, NwcMesh meshB, bool useWorldCoordinates = true) =>
MeasureMeshToMesh(meshA, meshB, useWorldCoordinates, default);

internal static NwcMeshToMeshMeasurement MeasureMeshToMesh(
NwcMesh meshA, NwcMesh meshB, bool useWorldCoordinates, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(meshA);
ArgumentNullException.ThrowIfNull(meshB);
Expand All @@ -277,13 +281,15 @@ public static NwcMeshToMeshMeasurement MeasureMeshToMesh(NwcMesh meshA, NwcMesh

for (var i = 0; i + 2 < indicesA.Count; i += 3)
{
cancellationToken.ThrowIfCancellationRequested();
if (!ReadTriangle(vertsA, indicesA, i, a))
{
continue;
}

for (var j = 0; j + 2 < indicesB.Count; j += 3)
{
if (j % (3 * 256) == 0) cancellationToken.ThrowIfCancellationRequested();
if (!ReadTriangle(vertsB, indicesB, j, b))
{
continue;
Expand Down
10 changes: 8 additions & 2 deletions src/NwcIO/Compatibility/NwcAssembly.Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,16 @@ public IReadOnlyList<NwcClash> FindClashes(double tolerance = 0.0)
/// <summary>
/// Executes full mesh-level triangle collision detection across all contributing models of the assembly.
/// </summary>
public Clash.NwcClashTest RunClashDetection(string testName = "Federated Clash Test", double tolerance = 0.001)
public Clash.NwcClashTest RunClashDetection(string testName = "Federated Clash Test", double tolerance = 0.001) =>
RunClashDetection(testName, tolerance, null, default);

/// <summary>Runs a hard clash test across all models with cancellation and progress.</summary>
/// <exception cref="OperationCanceledException">The cancellation token was cancelled.</exception>
public Clash.NwcClashTest RunClashDetection(
string testName, double tolerance, IProgress<NwcProgress>? progress = null, CancellationToken cancellationToken = default)
{
var test = new Clash.NwcClashTest(testName, Clash.NwcClashTestType.Hard, tolerance);
Clash.NwcClashDetector.Run(this, test);
Clash.NwcClashDetector.Run(this, test, progress, cancellationToken);
ClashTests.Add(test);
return test;
}
Expand Down
10 changes: 8 additions & 2 deletions src/NwcIO/Compatibility/NwcDocument.Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,16 @@ public void ImportTimeLinerPrimavera(string path)
/// <summary>
/// Executes clash detection across the document using the specified or default test configuration.
/// </summary>
public Model.Clash.NwcClashTest RunClashDetection(Model.Clash.NwcClashTest? test = null)
public Model.Clash.NwcClashTest RunClashDetection(Model.Clash.NwcClashTest? test = null) =>
RunClashDetection(test, null, default);

/// <summary>Runs the specified or default clash test with cancellation and progress.</summary>
/// <exception cref="OperationCanceledException">The cancellation token was cancelled.</exception>
public Model.Clash.NwcClashTest RunClashDetection(
Model.Clash.NwcClashTest? test, IProgress<NwcProgress>? progress = null, CancellationToken cancellationToken = default)
{
test ??= new Model.Clash.NwcClashTest("Clash Test");
Model.Clash.NwcClashDetector.Run(this, test);
Model.Clash.NwcClashDetector.Run(this, test, progress, cancellationToken);
if (!ClashTests.Contains(test)) ClashTests.Add(test);
return test;
}
Expand Down
Loading
Loading