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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to the `IfcIO` library are documented in this file.
## [Unreleased]

### Fixed
- Import meshes swept disks along open 3D polylines and linear indexed curves, including optional trimming,
mitered joints and hollow sections.
- Import reads IFC4 and IFC4.3 `IfcPolygonalFaceSet` geometry, including concave polygons, holes and `PnIndex`
point reindexing. Invalid faces are omitted without altering the other faces.
- Import reads additional concrete IFC products with a `Body` representation, including curtain walls, piles,
Expand Down
7 changes: 7 additions & 0 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ IFC4 and IFC4.3 `IfcPolygonalFaceSet` import also triangulates indexed polygons
honours optional `PnIndex` remapping, and omits malformed faces. It keeps the resulting triangle mesh,
not the original indexed polygon groups.

`IfcSweptDiskSolid` import supports open 3D `IfcPolyline` and linear `IfcIndexedPolyCurve` paths, and meshes
the circular section with 16 sides. It honours parameter trims and the optional inner radius, and uses
mitered joints and end caps. `IfcSweptDiskSolidPolygonal` is read without fillets only. Closed paths,
arcs and other curved directrices are not read by this path. Input curves must not self-intersect;
the mesher checks adjacent miters, not intersections between distant parts of the sweep.
A readable `Body-FallBack` takes precedence when the primary body has unreadable items.

Imported meshes use world coordinates. If a swept body cannot be represented by one equivalent parametric
solid, the reader keeps its placed mesh instead; IFC export writes that mesh rather than a partial or
misplaced solid. Boolean operands also use placed meshes where necessary. Applying arbitrary solid-to-solid
Expand Down
3 changes: 3 additions & 0 deletions src/IfcIO/Core/StepReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public List<int> GetRefList(int index)

private static bool Omitted(ReadOnlySpan<char> arg) => arg.Length == 1 && arg[0] is '$' or '*';

internal bool IsNullParameter(int index) =>
TryGetArgument(index, out var arg) && arg.Length == 1 && arg[0] == '$';

public static List<string> ParseArguments(string args)
{
var result = new List<string>();
Expand Down
111 changes: 111 additions & 0 deletions src/IfcIO/Geometry/IfcSweptDiskMesher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
namespace IfcIO.Geometry;

internal static class IfcSweptDiskMesher
{
internal const int Sides = 16;
private static readonly (double Cos, double Sin)[] Circle = Enumerable.Range(0, Sides)
.Select(i => (Math.Cos(i * Math.Tau / Sides), Math.Sin(i * Math.Tau / Sides))).ToArray();

internal static IfcMesh? Build(int id, string name, List<IfcVector3> path, double radius, double innerRadius)
{
bool hollow = innerRadius > 0;
int stride = hollow ? Sides * 2 : Sides;
var vertices = new List<IfcVector3>(path.Count * stride + (hollow ? 0 : 2));
var indices = new List<int>();
var incoming = Direction(0);
if (incoming == IfcVector3.Zero) return null;
var reference = Math.Abs(incoming.Z) < 0.9 ? IfcVector3.UnitZ : IfcVector3.UnitX;
var x = IfcVector3.Cross(reference, incoming).Normalized();
var y = IfcVector3.Cross(incoming, x);
var previousAdvance = new double[Sides];
for (int ring = 0; ring < path.Count; ring++)
{
bool atEnd = ring == path.Count - 1;
var outgoing = atEnd ? incoming : Direction(ring);
if (outgoing == IfcVector3.Zero) return null;
double dot = IfcVector3.Dot(incoming, outgoing);
if (dot <= -1 + 1e-9) return null;

// Project the incoming section along its axis onto the joint's half-angle plane.
var plane = incoming + outgoing;
double denominator = 1 + dot;
var turn = IfcVector3.Cross(incoming, outgoing);
double length = ring == 0 ? 0 : (path[ring] - path[ring - 1]).Length;
for (int side = 0; side < Sides; side++)
{
var section = x * Circle[side].Cos + y * Circle[side].Sin;
double advance = -IfcVector3.Dot(section, plane) / denominator;
if (ring > 0 && length + radius * (advance - previousAdvance[side]) <= 0) return null;
// The same joint point lies on both cylinders. Express its advance on the next leg.
var projected = section + incoming * advance;
previousAdvance[side] = IfcVector3.Dot(projected, outgoing);
vertices.Add(path[ring] + projected * radius);
}
if (hollow)
{
for (int side = 0; side < Sides; side++)
{
var outer = vertices[ring * stride + side];
vertices.Add(path[ring] + (outer - path[ring]) * (innerRadius / radius));
}
}

// Transport the frame by the shortest rotation so the ring indexing does not twist at bends.
var delta = IfcVector3.Cross(turn, x);
x += delta + IfcVector3.Cross(turn, delta) * (1 / denominator);
x = x.Normalized();
y = IfcVector3.Cross(outgoing, x).Normalized();
incoming = outgoing;
}

if (vertices.Exists(p => !double.IsFinite(p.X) || !double.IsFinite(p.Y) || !double.IsFinite(p.Z))) return null;
for (int ring = 0; ring < path.Count - 1; ring++)
{
int a = ring * stride, b = a + stride;
for (int side = 0; side < Sides; side++)
{
int next = (side + 1) % Sides;
Quad(a + side, a + next, b + next, b + side);
if (hollow) Quad(a + next + Sides, a + side + Sides, b + side + Sides, b + next + Sides);
}
}

int last = (path.Count - 1) * stride;
if (hollow)
{
for (int side = 0; side < Sides; side++)
{
int next = (side + 1) % Sides;
Quad(next, side, side + Sides, next + Sides);
Quad(last + side, last + next, last + next + Sides, last + side + Sides);
}
}
else
{
int startCenter = vertices.Count;
vertices.Add(path[0]);
int endCenter = vertices.Count;
vertices.Add(path[^1]);
for (int side = 0; side < Sides; side++)
{
int next = (side + 1) % Sides;
Triangle(startCenter, next, side);
Triangle(endCenter, last + side, last + next);
}
}
return IfcMesh.Create(id, name, vertices, indices);

IfcVector3 Direction(int edge) => (path[edge + 1] - path[edge]).Normalized();
void Triangle(int a, int b, int c)
{
indices.Add(a);
indices.Add(b);
indices.Add(c);
}
void Quad(int a, int b, int c, int d)
{
Triangle(a, b, c);
Triangle(a, c, d);
}
}
}
131 changes: 131 additions & 0 deletions src/IfcIO/IfcModel.SweptDisks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System.Globalization;
using IfcIO.Core;
using IfcIO.Geometry;
using IfcIO.Schema;

namespace IfcIO;

public sealed partial class IfcModel
{
private static IfcMesh? ParseSweptDisk(GeometryReadContext ctx, StepEntity item, string meshName)
{
var step = ctx.Step;
bool polygonal = item.Type.Equals("IFCSWEPTDISKSOLIDPOLYGONAL", StringComparison.OrdinalIgnoreCase);
bool ifc2x3 = ctx.Schema == IfcSchema.Ifc2x3;
if (polygonal && (ifc2x3 || !item.IsNullParameter(5))) return null; // Filleted joints need a curved sweep.

if (item.GetReal(1) is not double radius || !double.IsFinite(radius) || radius <= 0) return null;
double innerRadius = 0;
if (!item.IsNullParameter(2))
{
if (item.GetReal(2) is not double inner || !double.IsFinite(inner) || inner <= 0 || inner >= radius) return null;
innerRadius = inner;
}

if (step.Find(item.GetRef(0)) is not { } curve) return null;
if (ifc2x3 && !curve.Type.Equals("IFCPOLYLINE", StringComparison.OrdinalIgnoreCase)) return null;
var points = ReadLinearDirectrix(ctx, curve, polygonal);
if (points is not { Count: >= 2 }) return null;

double start = !ifc2x3 && item.IsNullParameter(3) ? 0 : item.GetReal(3) ?? double.NaN;
double end = !ifc2x3 && item.IsNullParameter(4) ? points.Count - 1 : item.GetReal(4) ?? double.NaN;
if (!double.IsFinite(start) || !double.IsFinite(end) || start < 0 || end > points.Count - 1 || end <= start) return null;

// Each linear edge occupies one unit in the IFC curve's parameter domain.
int first = (int)Math.Floor(start);
int limit = (int)Math.Ceiling(end);
long vertexCount = ((long)limit - first + 1) * IfcSweptDiskMesher.Sides * (innerRadius > 0 ? 2 : 1)
+ (innerRadius > 0 ? 0 : 2);
ctx.CheckVertexBudget(vertexCount);
var path = new List<IfcVector3>(limit - first + 1) { Interpolate(first, start) };
for (int i = first + 1; i < end; i++) AddPathPoint(points[i]);
AddPathPoint(Interpolate(limit - 1, end));
if (path[0] == path[^1]) return null; // Closed directrices need a seam-aware frame.

var mesh = IfcSweptDiskMesher.Build(item.Id, meshName, path, radius, innerRadius);
// The caller charges success. Bound repeated failed attempts that could allocate all the rings.
if (mesh is null) ctx.ChargeVertices((int)vertexCount);
return mesh;

IfcVector3 Interpolate(int segment, double parameter)
{
double fraction = parameter - segment;
return points[segment] * (1 - fraction) + points[segment + 1] * fraction;
}

void AddPathPoint(IfcVector3 point)
{
// Trim first: a redundant straight-line vertex still occupies a parameter interval.
if (path.Count >= 2)
{
var incoming = (path[^1] - path[^2]).Normalized();
var outgoing = (point - path[^1]).Normalized();
if (IfcVector3.Dot(incoming, outgoing) > 0
&& IfcVector3.Cross(incoming, outgoing).Length <= 1e-12)
{
path[^1] = point;
return;
}
}
path.Add(point);
}
}

private static List<IfcVector3>? ReadLinearDirectrix(GeometryReadContext ctx, StepEntity curve, bool polygonal)
{
var points = new List<IfcVector3>();
if (curve.Type.Equals("IFCPOLYLINE", StringComparison.OrdinalIgnoreCase))
{
if (ReadReferenceList(curve, 0) is not { Count: >= 2 } ids) return null;
foreach (int id in ids)
{
if (!TryReadFacetedPoint(ctx.Step.Find(id), out var point)) return null;
points.Add(point);
}
return points;
}

if (!curve.Type.Equals("IFCINDEXEDPOLYCURVE", StringComparison.OrdinalIgnoreCase)
|| ReadTessellatedPoints(ctx, curve.GetRef(0)) is not { Length: >= 2 } coordinates
|| (!curve.IsNullParameter(2) && curve.GetBool(2) != false)) return null;

if (curve.IsNullParameter(1))
{
foreach (var point in coordinates)
{
if (point is not IfcVector3 coordinate) return null;
points.Add(coordinate);
}
return points;
}
if (polygonal) return null; // The polygonal subtype requires Segments to be omitted.

int lastIndex = -1;
var segments = ParseStepList(curve.GetRaw(1));
foreach (string segment in segments)
{
const string lineIndex = "IFCLINEINDEX";
if (!segment.StartsWith(lineIndex, StringComparison.OrdinalIgnoreCase)) return null;
var wrapper = ParseStepList(segment[lineIndex.Length..].TrimStart());
if (wrapper.Count != 1) return null;
var indices = ParseStepList(wrapper[0]);
if (indices.Count < 2) return null;
for (int i = 0; i < indices.Count; i++)
{
if (!int.TryParse(indices[i], NumberStyles.Integer, CultureInfo.InvariantCulture, out int index)
|| index <= 0 || index > coordinates.Length || coordinates[index - 1] is not IfcVector3 point)
return null;
if (i == 0 && lastIndex != -1)
{
if (index != lastIndex) return null;
}
else
{
points.Add(point);
}
lastIndex = index;
}
}
return points;
}
}
Loading