From 024070a4ade97c061ec87c2c5c7d6ac65c0435bc Mon Sep 17 00:00:00 2001 From: katehryhorenko Date: Mon, 23 Oct 2023 20:49:12 -0300 Subject: [PATCH] Fixed Intersection method; --- Elements/src/GeometricElement.cs | 68 +++++++++---------- .../Representations/SolidRepresentation.cs | 6 +- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/Elements/src/GeometricElement.cs b/Elements/src/GeometricElement.cs index 173b3b7db..95f81e3dc 100644 --- a/Elements/src/GeometricElement.cs +++ b/Elements/src/GeometricElement.cs @@ -213,7 +213,6 @@ public bool Intersects(Plane plane, var graphVertices = new List(); var graphEdges = new List>(); - var intersectionPoints = new List(); var beyondPolygonsList = new List(); if (Representation != null && _csg != null) @@ -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); } } @@ -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, @@ -364,6 +335,33 @@ public bool Intersects(Plane plane, } } + private static void AddToGraph(List intersectionPoints, List graphVertices, List> 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); + } + } + } + /// /// Get the computed csg solid. /// The csg is centered on the origin by default. diff --git a/Elements/src/Representations/SolidRepresentation.cs b/Elements/src/Representations/SolidRepresentation.cs index 6abab334b..5832a154a 100644 --- a/Elements/src/Representations/SolidRepresentation.cs +++ b/Elements/src/Representations/SolidRepresentation.cs @@ -147,9 +147,9 @@ public BBox3 ComputeBounds(GeometricElement element) /// The intersecting plane. /// The output collection of the polygons beyond the input plane. /// Returns the collection of intersection points. - public List CalculateIntersectionPoints(GeometricElement element, Plane plane, out List beyondPolygons) + public List> CalculateIntersectionPoints(GeometricElement element, Plane plane, out List beyondPolygons) { - var intersectionPoints = new List(); + var intersectionPoints = new List>(); beyondPolygons = new List(); var csg = SolidOperationUtils.GetFinalCsgFromSolids(SolidOperations, element, true); @@ -186,7 +186,7 @@ public List CalculateIntersectionPoints(GeometricElement element, Plane var d = csgNormal.Cross(plane.Normal).Unitized(); edgeResults.Sort(new DirectionComparer(d)); - intersectionPoints.AddRange(edgeResults); + intersectionPoints.Add(edgeResults); } return intersectionPoints;