Skip to content

Commit

Permalink
Merge pull request #1042 from hypar-io/Fixes
Browse files Browse the repository at this point in the history
Fix: GeometrisElement.Intersection method (#1042)

Co-Authored-By: katehryhorenko <[email protected]>
  • Loading branch information
katehryhorenko and katehryhorenko authored Oct 24, 2023
2 parents 7ba0558 + 024070a commit 12b323e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
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

0 comments on commit 12b323e

Please sign in to comment.