To modify your ProcessBlockCurves method to create vertical and horizontal curves based on the intersection points (InterssectPoints), you can follow these steps:
-
Group Intersection Points:
- Vertical Curves: Group points that share the same X-coordinate.
- Horizontal Curves: Group points that share the same Y-coordinate.
-
Create Curves from Groups:
- For each group of points with the same X-coordinate, create a vertical line spanning from the minimum to the maximum Y-coordinate.
- For each group of points with the same Y-coordinate, create a horizontal line spanning from the minimum to the maximum X-coordinate.
-
Integrate into the Existing Code:
- Replace the existing section where you create
SectionLine1 and SectionLine2 with the new logic for creating vertical and horizontal curves.
Here's the updated version of your method with the required modifications:
private void ProcessBlockCurves(List<Curve> blockCurves, Curve region,
out List<Curve> r1, out List<Curve> r2, out List<Curve> sectionCurve)
{
List<Curve> In = new List<Curve>();
List<Curve> Out = new List<Curve>();
List<Curve> AllSSegments = new List<Curve>();
List<double> passs = new List<double>();
List<Point3d> InterssectPoints = new List<Point3d>();
List<Curve> sectionCurves = new List<Curve>();
// Duplicate all segments from blockCurves
foreach (var blockCurve in blockCurves)
{
AllSSegments.AddRange(blockCurve.DuplicateSegments());
}
// Process each segment
foreach (var curve in AllSSegments)
{
// Project the curve onto the region's plane if necessary
Curve projectedCurve = Curve.ProjectToPlane(curve.DuplicateCurve(), Plane.WorldXY);
// Find intersection points with the region
var intersectPoints = CurveCurveIntersection(projectedCurve, region, out passs);
// Check if the curve is completely inside the region
if (IsCurveInsideRegion(projectedCurve, region))
{
In.Add(projectedCurve);
}
else
{
if (intersectPoints.Count > 0)
{
InterssectPoints.AddRange(intersectPoints);
// Shatter the curve at the intersection parameters
var shatteredCurves = projectedCurve.Split(passs);
// Classify each segment as inside or outside
foreach (var segment in shatteredCurves)
{
var midpoint = segment.PointAtNormalizedLength(0.5);
bool isInside = region.Contains(midpoint, Plane.WorldXY, 0.001) == PointContainment.Inside;
if (isInside)
{
In.Add(segment);
}
else
{
Out.Add(segment);
}
}
}
else
{
Out.Add(projectedCurve);
}
}
}
// Group intersection points by X and Y coordinates
var verticalGroups = InterssectPoints
.GroupBy(pt => pt.X)
.Where(g => g.Count() >= 2); // Ensure at least two points to form a line
var horizontalGroups = InterssectPoints
.GroupBy(pt => pt.Y)
.Where(g => g.Count() >= 2);
// Create vertical lines
foreach (var group in verticalGroups)
{
double x = group.Key;
double yMin = group.Min(pt => pt.Y);
double yMax = group.Max(pt => pt.Y);
Point3d start = new Point3d(x, yMin, 0);
Point3d end = new Point3d(x, yMax, 0);
Line verticalLine = new Line(start, end);
sectionCurves.Add(verticalLine.ToNurbsCurve());
}
// Create horizontal lines
foreach (var group in horizontalGroups)
{
double y = group.Key;
double xMin = group.Min(pt => pt.X);
double xMax = group.Max(pt => pt.X);
Point3d start = new Point3d(xMin, y, 0);
Point3d end = new Point3d(xMax, y, 0);
Line horizontalLine = new Line(start, end);
sectionCurves.Add(horizontalLine.ToNurbsCurve());
}
// Assign output parameters
r1 = In;
r2 = Out;
sectionCurve = sectionCurves;
}
// Helper method for curve-curve intersection
private List<Point3d> CurveCurveIntersection(Curve curve1, Curve curve2, out List<double> parameters)
{
parameters = new List<double>();
var events = Rhino.Geometry.Intersect.Intersection.CurveCurve(curve1, curve2, 0.001, 0.001);
List<Point3d> points = new List<Point3d>();
if (events != null)
{
foreach (var evt in events)
{
if (evt.IsPoint)
{
points.Add(evt.PointA);
parameters.Add(evt.ParameterA);
}
}
}
return points;
}
// Placeholder for your existing method to check if a curve is inside a region
private bool IsCurveInsideRegion(Curve curve, Curve region)
{
// Implement your logic to determine if the curve is entirely inside the region
// This could involve checking control points, bounding boxes, etc.
// For demonstration purposes, let's assume it returns false
return false;
}
Explanation of the Changes:
-
Grouping Intersection Points:
- Vertical Groups: We group
InterssectPoints by their X value using GroupBy(pt => pt.X). We filter groups to include only those with two or more points using .Where(g => g.Count() >= 2).
- Horizontal Groups: Similarly, we group by
Y value.
-
Creating Vertical Lines:
- For each vertical group, determine the minimum and maximum Y-coordinates.
- Create a
Line from (X, Ymin, 0) to (X, Ymax, 0) and convert it to a NurbsCurve.
-
Creating Horizontal Lines:
- For each horizontal group, determine the minimum and maximum X-coordinates.
- Create a
Line from (Xmin, Y, 0) to (Xmax, Y, 0) and convert it to a NurbsCurve.
-
Filtering Groups:
- Only groups with two or more points are considered valid for creating lines. This prevents creating lines from single intersection points.
-
Optimizations:
- Midpoint Check Simplification: Instead of checking multiple midpoints (
midpoint1, midpoint2, midpoint3), a single midpoint is generally sufficient for determining if a segment is inside the region.
- Projection Handling: Ensured that the curve is projected only once using
Curve.ProjectToPlane.
-
Helper Method for Intersection:
- Added a helper method
CurveCurveIntersection to encapsulate the curve-curve intersection logic. This keeps the main method cleaner and separates concerns.
Additional Recommendations:
-
Tolerance Handling: Adjust the tolerance (0.001 in the example) as needed based on the precision requirements of your application.
-
Error Checking: Add error handling to manage cases where intersection points might not form valid lines or when curves fail to split correctly.
-
Performance Optimization: If dealing with a large number of curves or intersection points, consider optimizing the grouping and line creation logic to improve performance.
-
Visualization and Debugging:
- It might be helpful to visualize the created vertical and horizontal curves to ensure they meet your expectations.
- Use debugging statements or logs to verify the grouping and line creation processes during development.
By integrating these changes, your method will now create vertical and horizontal section curves based on the equality of the X and Y coordinates of the intersection points, respectively.