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
3 changes: 2 additions & 1 deletion docs/tools-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## [0.1.0-preview.1]

- `IOScene.Formats` reads embedded GLB and OBJ geometry and writes GLB from an IOScene document.
- `IOCoord` provides measurements, triangle surface intersections and clearance, grid queries, revision comparison and BCF 2.1 export.
- `IOCoord` provides measurements, surface clearance, hard clashes with an estimated depth, box overlap checks,
duplicate geometry detection, grid queries, revision comparison and BCF 2.1 export.
- Both packages use the public IOScene 0.1 API and can be installed independently.

See [Optional IOScene tools](tools.md) for supported inputs and scope.
12 changes: 10 additions & 2 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ box hierarchy narrows the triangle pairs, and a surface check finds a contact po
pair. It returns a `SceneClashTest` for export or review, with a contact tolerance of one nanometre.
`SceneClash.FindSurfaceClearances` returns the shortest gap between the placed triangle surfaces of each
element pair within an inclusive limit in metres. The result includes the distance and the midpoint of the
gap. Surface contacts report zero using the same tolerance. Solid containment and penetration depth require
different queries.
gap. Surface contacts report zero using the same tolerance.

`SceneClash.FindHardClashes` checks triangle contact and estimates depth from the minimum side of the overlap
of the two contacting mesh bounds, as in NwcIO. It reports negative depth in metres, skips positive depths
below the requested minimum, and keeps zero-depth contacts. `SceneClash.FindBoundingBoxOverlaps` uses the same
overlap metric and reports the centre of overlapping instance boxes without checking their surfaces. A box
overlap can include empty space. Exact depth and closed-solid containment are outside these queries.

`SceneClash.FindDuplicates` compares placed vertices and effective index order within a tolerance in metres.
It reports a pair when any two of its instances match, independent of material and display name.

`SceneDiff.Compare` matches elements by model key and source id. It compares names and types, ordered property sets,
and ordered vertices and indices in scene coordinates. It reports elements without an identity separately and refuses
Expand Down
10 changes: 4 additions & 6 deletions src/IOCoord/SceneClash.Clearance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ public static partial class SceneClash
public static SceneClashTest FindSurfaceClearances(SceneDocument scene, IEnumerable<long> first, IEnumerable<long> second,
double maxClearanceMetres, CancellationToken cancellationToken = default)
{
ArgumentOutOfRangeException.ThrowIfNegative(maxClearanceMetres);
if (!double.IsFinite(maxClearanceMetres))
throw new ArgumentOutOfRangeException(nameof(maxClearanceMetres), "Clearance must be finite.");
return Find(scene, first, second, maxClearanceMetres, cancellationToken);
ValidateDistance(maxClearanceMetres, nameof(maxClearanceMetres));
return Find(scene, first, second, Query.Clearance, maxClearanceMetres, cancellationToken);
}

private static (Vector3d Point, double Distance) ClosestBetween(Triangle a, Triangle b)
private static (Vector3d Point, double Distance) ClosestBetween(Primitive a, Primitive b)
{
ReadOnlySpan<Vector3d> first = [a.A, a.B, a.C];
ReadOnlySpan<Vector3d> second = [b.A, b.B, b.C];
Expand All @@ -47,7 +45,7 @@ void Consider(Vector3d one, Vector3d two)
double distance = SceneMeasure.Length(one - two);
if (distance >= shortest) return;
shortest = distance;
midpoint = one * 0.5 + two * 0.5;
midpoint = Midpoint(one, two);
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/IOCoord/SceneClash.Duplicates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using IOScene;

namespace IOCoord;

public static partial class SceneClash
{
/// <summary>Finds element pairs with matching placed geometry and ordered topology.</summary>
/// <param name="scene">The scene whose placed geometry is tested.</param>
/// <param name="first">Ids of elements in the first selection.</param>
/// <param name="second">Ids of elements in the second selection.</param>
/// <param name="toleranceMetres">Inclusive maximum distance between corresponding vertices, in metres.</param>
/// <param name="cancellationToken">Cancellation token for the query.</param>
/// <returns>One zero-distance result per element pair with at least one matching instance.</returns>
/// <remarks>Topology, vertex order and effective index order must match. Material and names do not affect the query.</remarks>
public static SceneClashTest FindDuplicates(SceneDocument scene, IEnumerable<long> first, IEnumerable<long> second,
double toleranceMetres = 0, CancellationToken cancellationToken = default)
{
ValidateDistance(toleranceMetres, nameof(toleranceMetres));
return Find(scene, first, second, Query.Duplicate, toleranceMetres, cancellationToken);
}

private static bool AreDuplicates(SceneInstance first, SceneInstance second,
Dictionary<int, SceneGeometry> geometries, double tolerance, CancellationToken token)
{
var a = geometries[first.GeometryId];
var b = geometries[second.GeometryId];
if (a.Topology != b.Topology || a.Vertices.Count != b.Vertices.Count) return false;

int aCount = a.Indices.Count == 0 ? a.Vertices.Count : a.Indices.Count;
int bCount = b.Indices.Count == 0 ? b.Vertices.Count : b.Indices.Count;
if (aCount != bCount) return false;
if (a.Id != b.Id)
{
for (int i = 0; i < aCount; i++)
{
token.ThrowIfCancellationRequested();
int ai = a.Indices.Count == 0 ? i : a.Indices[i];
int bi = b.Indices.Count == 0 ? i : b.Indices[i];
if (ai != bi) return false;
}
}

var transformA = first.Transform ?? Transform3d.Identity;
var transformB = second.Transform ?? Transform3d.Identity;
if (a.Id == b.Id && transformA == transformB) return true;
for (int i = 0; i < a.Vertices.Count; i++)
{
token.ThrowIfCancellationRequested();
var delta = transformA.Apply(a.Vertices[i]) - transformB.Apply(b.Vertices[i]);
if (double.Hypot(double.Hypot(delta.X, delta.Y), delta.Z) > tolerance) return false;
}
return true;
}
}
54 changes: 54 additions & 0 deletions src/IOCoord/SceneClash.Overlap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using IOScene;

namespace IOCoord;

public static partial class SceneClash
{
/// <summary>Finds surface clashes using the overlap of the colliding mesh bounds to estimate depth.</summary>
/// <param name="scene">The scene whose placed triangles are tested.</param>
/// <param name="first">Ids of elements in the first selection.</param>
/// <param name="second">Ids of elements in the second selection.</param>
/// <param name="minDepthMetres">Inclusive minimum positive depth in metres, finite and nonnegative.</param>
/// <param name="cancellationToken">Cancellation token for the query.</param>
/// <returns>The deepest contact per element pair, with a signed distance of zero or less in metres.</returns>
/// <remarks>Zero-depth contacts are retained. Depth is the minimum side length of the overlap of the two
/// contacting instances' axis-aligned bounds. Enclosed solids whose surfaces do not touch have no result.</remarks>
public static SceneClashTest FindHardClashes(SceneDocument scene, IEnumerable<long> first, IEnumerable<long> second,
double minDepthMetres = 0, CancellationToken cancellationToken = default)
{
ValidateDistance(minDepthMetres, nameof(minDepthMetres));
return Find(scene, first, second, Query.Hard, minDepthMetres, cancellationToken);
}

/// <summary>Finds potentially clashing element pairs by comparing the bounds of their placed geometry.</summary>
/// <param name="scene">The scene whose placed geometry is tested.</param>
/// <param name="first">Ids of elements in the first selection.</param>
/// <param name="second">Ids of elements in the second selection.</param>
/// <param name="cancellationToken">Cancellation token for the query.</param>
/// <returns>The deepest box overlap per pair, with the centre of that overlap and its negative depth in metres.</returns>
/// <remarks>Touching boxes have a distance of zero. The box overlap can include empty space.</remarks>
public static SceneClashTest FindBoundingBoxOverlaps(SceneDocument scene, IEnumerable<long> first, IEnumerable<long> second,
CancellationToken cancellationToken = default) =>
Find(scene, first, second, Query.Overlap, 0, cancellationToken);

private static BoundingBox3d Overlap(BoundingBox3d a, BoundingBox3d b) => new(
new(Math.Max(a.Min.X, b.Min.X), Math.Max(a.Min.Y, b.Min.Y), Math.Max(a.Min.Z, b.Min.Z)),
new(Math.Min(a.Max.X, b.Max.X), Math.Min(a.Max.Y, b.Max.Y), Math.Min(a.Max.Z, b.Max.Z)));

private static double OverlapDepth(BoundingBox3d overlap)
{
if (overlap.IsEmpty) return 0;
var size = overlap.Size;
double depth = Math.Min(size.X, Math.Min(size.Y, size.Z));
if (!double.IsFinite(depth)) throw new InvalidDataException("The overlap depth is outside the finite range.");
return depth;
}

private static Vector3d Midpoint(Vector3d a, Vector3d b) => a * 0.5 + b * 0.5;

private static void ValidateDistance(double distance, string parameter)
{
if (distance < 0 || !double.IsFinite(distance))
throw new ArgumentOutOfRangeException(parameter, "The distance must be finite and nonnegative.");
}
}
Loading
Loading