Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: GeometrisElement.Intersection method #1042

Merged
merged 1 commit into from
Oct 24, 2023
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
68 changes: 33 additions & 35 deletions Elements/src/GeometricElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ public bool Intersects(Plane plane,
var graphVertices = new List<Vector3>();
var graphEdges = new List<List<(int from, int to, int? tag)>>();

var intersectionPoints = new List<Vector3>();
var beyondPolygonsList = new List<Polygon>();

if (Representation != null && _csg != null)
Expand Down Expand Up @@ -252,7 +251,7 @@ public bool Intersects(Plane plane,

var d = csgNormal.Cross(plane.Normal).Unitized();
edgeResults.Sort(new DirectionComparer(d));
intersectionPoints.AddRange(edgeResults);
AddToGraph(edgeResults, graphVertices, graphEdges);
}
}

Expand All @@ -268,43 +267,15 @@ public bool Intersects(Plane plane,

if (instance.Representation is SolidRepresentation solidRepresentation)
{
intersectionPoints.AddRange(solidRepresentation.CalculateIntersectionPoints(this, plane,
out var beyondPolygonsLocal));
beyondPolygonsList.AddRange(beyondPolygonsLocal);
foreach (var intersection in solidRepresentation.CalculateIntersectionPoints(this, plane,
out var beyondPolygonsLocal))
{
AddToGraph(intersection, graphVertices, graphEdges);
}
}
}
}

if (!intersectionPoints.Any())
{
return false;
}

// Draw segments through the results and add to the
// half edge graph.
for (var j = 0; j < intersectionPoints.Count - 1; j += 2)
{
// Don't create zero-length edges.
if (intersectionPoints[j].IsAlmostEqualTo(intersectionPoints[j + 1]))
{
continue;
}

var a = Solid.FindOrCreateGraphVertex(intersectionPoints[j], graphVertices, graphEdges);
var b = Solid.FindOrCreateGraphVertex(intersectionPoints[j + 1], graphVertices, graphEdges);
var e1 = (a, b, 0);
var e2 = (b, a, 0);
if (graphEdges[a].Contains(e1) || graphEdges[b].Contains(e2))
{
continue;
}
else
{
graphEdges[a].Add(e1);
}
}
// }

var heg = new HalfEdgeGraph2d()
{
Vertices = graphVertices,
Expand Down Expand Up @@ -364,6 +335,33 @@ public bool Intersects(Plane plane,
}
}

private static void AddToGraph(List<Vector3> intersectionPoints, List<Vector3> graphVertices, List<List<(int from, int to, int? tag)>> graphEdges)
{
// Draw segments through the results and add to the
// half edge graph.
for (var j = 0; j < intersectionPoints.Count - 1; j += 2)
{
// Don't create zero-length edges.
if (intersectionPoints[j].IsAlmostEqualTo(intersectionPoints[j + 1]))
{
continue;
}

var a = Solid.FindOrCreateGraphVertex(intersectionPoints[j], graphVertices, graphEdges);
var b = Solid.FindOrCreateGraphVertex(intersectionPoints[j + 1], graphVertices, graphEdges);
var e1 = (a, b, 0);
var e2 = (b, a, 0);
if (graphEdges[a].Contains(e1) || graphEdges[b].Contains(e2))
{
continue;
}
else
{
graphEdges[a].Add(e1);
}
}
}

/// <summary>
/// Get the computed csg solid.
/// The csg is centered on the origin by default.
Expand Down
6 changes: 3 additions & 3 deletions Elements/src/Representations/SolidRepresentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ public BBox3 ComputeBounds(GeometricElement element)
/// <param name="plane">The intersecting plane.</param>
/// <param name="beyondPolygons">The output collection of the polygons beyond the input plane.</param>
/// <returns>Returns the collection of intersection points.</returns>
public List<Vector3> CalculateIntersectionPoints(GeometricElement element, Plane plane, out List<Polygon> beyondPolygons)
public List<List<Vector3>> CalculateIntersectionPoints(GeometricElement element, Plane plane, out List<Polygon> beyondPolygons)
{
var intersectionPoints = new List<Vector3>();
var intersectionPoints = new List<List<Vector3>>();
beyondPolygons = new List<Polygon>();

var csg = SolidOperationUtils.GetFinalCsgFromSolids(SolidOperations, element, true);
Expand Down Expand Up @@ -186,7 +186,7 @@ public List<Vector3> CalculateIntersectionPoints(GeometricElement element, Plane

var d = csgNormal.Cross(plane.Normal).Unitized();
edgeResults.Sort(new DirectionComparer(d));
intersectionPoints.AddRange(edgeResults);
intersectionPoints.Add(edgeResults);
}

return intersectionPoints;
Expand Down
Loading