Skip to content

Commit

Permalink
Added Curve.ProjectToPlane extension method.
Browse files Browse the repository at this point in the history
This implementation keep the type on simple curve types when possible.
  • Loading branch information
kike-garbo committed Dec 27, 2024
1 parent a7ebb2b commit 6cd0507
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 24 deletions.
36 changes: 35 additions & 1 deletion src/RhinoInside.Revit.External/Extensions/RhinoCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public PlanarBrepFace(BrepFace f)

public NurbsCurve Loop
{
get { if (Plane.IsValid && loop is null) loop = Curve.ProjectToPlane(Face.OuterLoop.To3dCurve()?.ToNurbsCurve(), Plane) as NurbsCurve; return loop; }
get { if (Plane.IsValid && loop is null) loop = Face.OuterLoop.To3dCurve().ProjectToPlane(Plane) as NurbsCurve; return loop; }
}
public Point3d Centroid
{
Expand Down Expand Up @@ -897,6 +897,40 @@ public static IEnumerable<double> Discontinuities(this Curve curve, Continuity c
while (curve.GetNextDiscontinuity(continuity, t, domain.T1, tol1, tol2, out t))
yield return t;
}

public static Curve ProjectToPlane(this Curve curve, Plane plane)
{
switch (curve)
{
case null:
return null;

case LineCurve line:
line = line.DuplicateCurve() as LineCurve;
line.SetStartPoint(plane.ClosestPoint(line.PointAtStart));
line.SetEndPoint(plane.ClosestPoint(line.PointAtEnd));
return line;

case PolylineCurve polyline:
polyline = polyline.DuplicateCurve() as PolylineCurve;
for(int p = 0; p < polyline.PointCount; ++p)
polyline.SetPoint(p, plane.ClosestPoint(polyline.Point(p)));
return polyline;

case ArcCurve arc:

if (arc.Arc.Plane.Normal.EpsilonEquals(plane.Normal, RhinoMath.Epsilon))
{
arc = arc.DuplicateCurve() as ArcCurve;
arc.Translate(plane.ClosestPoint(arc.Arc.Plane.Origin) - arc.Arc.Plane.Origin);
return arc;
}
else return Curve.ProjectToPlane(curve?.ToNurbsCurve(), plane);

default:
return Curve.ProjectToPlane(curve?.ToNurbsCurve(), plane);
}
}
}

static class SurfaceExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
var plane = view.DetailPlane;
var tol = GeometryTolerance.Model;

if (Curve.ProjectToPlane(curve.ToNurbsCurve(), plane) is Curve projectedCurve)
if (curve.ProjectToPlane(plane) is Curve projectedCurve)
curve = projectedCurve;
else
throw new Exceptions.RuntimeArgumentException("Curve", "Failed to project 'Curve' into view plane", curve);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
throw new Exceptions.RuntimeArgumentException("Boundary", "Curve should be a valid planar, closed curve and parallel to the input view.", loop);
}

loops = loops.Select(x => Curve.ProjectToPlane(x, viewPlane)).ToArray();
loops = loops.Select(x => x.ProjectToPlane(viewPlane)).ToArray();

// Compute
region = Reconstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
throw new Exceptions.RuntimeArgumentException("Boundary", "Curve should be a valid planar, closed curve and perperdicular to the input view.", loop);
}

loops = loops.Select(x => Curve.ProjectToPlane(x, viewPlane)).ToArray();
loops = loops.Select(x => x.ProjectToPlane(viewPlane)).ToArray();

if (revision is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
var tol = GeometryTolerance.Model;
var axisPlane = sketchPlane.Location;

curve = Curve.ProjectToPlane(curve, axisPlane);
curve = curve.ProjectToPlane(axisPlane);
if (curve is null) throw new RuntimeArgumentException("Curve", "Failed to project Curve on to Work Plane.", curve);

curve = curve.ToArcsAndLines(tol.VertexTolerance, 10.0 * tol.AngleTolerance, tol.ShortCurveTolerance, 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
if (!curve.IsParallelToPlane(plane, tol.VertexTolerance, tol.AngleTolerance))
throw new Exceptions.RuntimeArgumentException("Curve", $"Curve should be planar and parallel to view plane.\nTolerance is {Rhino.RhinoMath.ToDegrees(tol.AngleTolerance):N1}°", curve);

if ((curve = Curve.ProjectToPlane(curve, plane)) is null)
if ((curve = curve.ProjectToPlane(plane)) is null)
throw new Exceptions.RuntimeArgumentException("Curve", "Failed to project Curve into 'Work Plane'", curve);

if (curve.IsClosed || curve.PointAtStart.DistanceTo(curve.PointAtEnd) < tol.VertexTolerance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)

foreach (var profile in profiles.OfType<Curve>())
{
var loop = Curve.ProjectToPlane(profile, projectionPlane);
var loop = profile.ProjectToPlane(projectionPlane);

var segments = loop.TryGetPolyCurve(out var polyCurve, tol.AngleTolerance) ?
polyCurve.DuplicateSegments() : new Curve[] { loop };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ ARDB.Opening Reconstruct(ARDB.Opening opening, ARDB.Document doc, ARDB.HostObjec
{
var hostPlane = sketch.SketchPlane.GetPlane().ToPlane();
normal = hostPlane.Normal;
boundary = boundary.Select(x => Curve.ProjectToPlane(x, hostPlane)).ToArray();
boundary = boundary.Select(x => x.ProjectToPlane(hostPlane)).ToArray();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ [Optional] bool flipped
new Point3d(0.0, 0.0, level.Value.GetElevation() * GeometryDecoder.ModelScaleFactor),
Vector3d.ZAxis
);
curve = Curve.ProjectToPlane(curve, levelPlane);
curve = curve.ProjectToPlane(levelPlane);
curve = curve.Simplify(CurveSimplifyOptions.All & ~CurveSimplifyOptions.Merge, tol.VertexTolerance, tol.AngleTolerance) ?? curve;

// Type
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Components/Model/AddModelLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
var plane = sketchPlane.Location;
var tol = GeometryTolerance.Model;

if (Curve.ProjectToPlane(curve.ToNurbsCurve(), plane) is Curve projectedCurve)
if (curve.ProjectToPlane(plane) is Curve projectedCurve)
curve = projectedCurve;
else
throw new Exceptions.RuntimeArgumentException("Curve", "Failed to project 'Curve' into 'Work Plane'", curve);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
var normal = default(ARDB.XYZ);
if (plane.HasValue)
{
curve = Curve.ProjectToPlane(curve, plane.Value) ?? curve;
curve = curve.ProjectToPlane(plane.Value) ?? curve;
normal = plane.Value.Normal.ToXYZ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
is3D = true;

if (!boundary[i].IsInPlane(sketchPlane.Location, tol.VertexTolerance))
boundary[i] = Curve.ProjectToPlane(boundary[i], sketchPlane.Location);
boundary[i] = boundary[i].ProjectToPlane(sketchPlane.Location);

boundaryElevation = Interval.FromUnion(boundaryElevation, new Interval(sketchPlane.Location.OriginZ, sketchPlane.Location.OriginZ));
}
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Components/Structure/AddTruss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
var tol = GeometryTolerance.Model;
var plane = sketchPlane.Location;

if (Curve.ProjectToPlane(curve.ToNurbsCurve(), plane) is Curve projectedCurve)
if (curve.ProjectToPlane(plane) is Curve projectedCurve)
curve = projectedCurve;
else
throw new Exceptions.RuntimeArgumentException("Curve", "Failed to project 'Curve' into 'Work Plane'", curve);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool Reuse(ARDB.ModelCurve modelCurve, ARDB.ViewPlan view, Curve curve)

using (var geometryCurve = modelCurve.GeometryCurve)
{
using (var projectedCurve = Curve.ProjectToPlane(curve, levelPlane).ToCurve())
using (var projectedCurve = curve.ProjectToPlane(levelPlane).ToCurve())
{
if (!projectedCurve.IsSameKindAs(geometryCurve)) return false;
if (!projectedCurve.AlmostEquals(geometryCurve, modelCurve.Document.Application.VertexTolerance))
Expand All @@ -137,7 +137,7 @@ ARDB.ModelCurve Create(ARDB.ViewPlan view, Curve curve)
if (view.GenLevel is ARDB.Level level)
{
using (var sketchPlane = level.GetSketchPlane(ensureSketchPlane: true))
using (var projectedCurve = Curve.ProjectToPlane(curve, sketchPlane.GetPlane().ToPlane()))
using (var projectedCurve = curve.ProjectToPlane(sketchPlane.GetPlane().ToPlane()))
return view.Document.Create.NewAreaBoundaryLine(sketchPlane, projectedCurve.ToCurve(), view);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ bool Reuse(ARDB.ModelCurve roomSeparator, ARDB.ViewPlan view, Curve curve)

using (var geometryCurve = roomSeparator.GeometryCurve)
{
using (var projectedCurve = Curve.ProjectToPlane(curve, levelPlane).ToCurve())
using (var projectedCurve = curve.ProjectToPlane(levelPlane).ToCurve())
{
if (!projectedCurve.IsSameKindAs(geometryCurve)) return false;
if (!projectedCurve.AlmostEquals(geometryCurve, GeometryTolerance.Internal.VertexTolerance))
Expand All @@ -143,7 +143,7 @@ ARDB.ModelCurve Create(ARDB.ViewPlan view, Curve curve)
if (view.GenLevel is ARDB.Level level)
{
using (var sketchPlane = level.GetSketchPlane(ensureSketchPlane: true))
using (var projectedCurve = Curve.ProjectToPlane(curve, sketchPlane.GetPlane().ToPlane()))
using (var projectedCurve = curve.ProjectToPlane(sketchPlane.GetPlane().ToPlane()))
using (var curveArray = new ARDB.CurveArray())
{
curveArray.Append(projectedCurve.ToCurve());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ bool Reuse(ARDB.ModelCurve spaceSeparator, ARDB.ViewPlan view, Curve curve)

using (var geometryCurve = spaceSeparator.GeometryCurve)
{
using (var projectedCurve = Curve.ProjectToPlane(curve, levelPlane).ToCurve())
using (var projectedCurve = curve.ProjectToPlane(levelPlane).ToCurve())
{
if (!projectedCurve.IsSameKindAs(geometryCurve)) return false;
if (!projectedCurve.AlmostEquals(geometryCurve, GeometryTolerance.Internal.VertexTolerance))
Expand All @@ -142,7 +142,7 @@ ARDB.ModelCurve Create(ARDB.ViewPlan view, Curve curve)
if (view.GenLevel is ARDB.Level level)
{
using (var sketchPlane = level.GetSketchPlane(ensureSketchPlane: true))
using (var projectedCurve = Curve.ProjectToPlane(curve, sketchPlane.GetPlane().ToPlane()))
using (var projectedCurve = curve.ProjectToPlane(sketchPlane.GetPlane().ToPlane()))
using (var curveArray = new ARDB.CurveArray())
{
curveArray.Append(projectedCurve.ToCurve());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ protected static bool SolveOptionalLevels(ARDB.Document doc, Rhino.Geometry.Curv
#region Geometry Conversion
public static bool TryGetCurveAtPlane(Curve curve, Plane plane, out ARDB.Curve projected)
{
if (Curve.ProjectToPlane(curve, plane) is Curve p)
if (curve.ProjectToPlane(plane) is Curve p)
{
var tol = GeometryTolerance.Model;

Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Components/Views/CropRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected override void TrySolveInstance(IGH_DataAccess DA)
if (Params.GetData(DA, "Crop Region", out Curve cropRegion))
{
var viewLocation = view.Location;
var curveLoop = Curve.ProjectToPlane(cropRegion, viewLocation)?.ToCurveLoop();
var curveLoop = cropRegion.ProjectToPlane(viewLocation)?.ToCurveLoop();
if (curveLoop is null || !cropManager.IsCropRegionShapeValid(curveLoop))
{
AddRuntimeMessage
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/HostObjects/CurtainCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Surface Surface
var curves = new List<Curve>();
foreach (var loop in planarCurves)
{
var curve = Curve.ProjectToPlane(loop, plane);
var curve = loop.ProjectToPlane(plane);
bbox.Union(curve.GetBoundingBox(plane));
curves.Add(curve);
}
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/Sketch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void RemoveConstraints()
var pi = 0;
foreach (var boundary in boundaries)
{
var profile = Curve.ProjectToPlane(boundary, plane);
var profile = boundary.ProjectToPlane(plane);

if
(
Expand Down
2 changes: 1 addition & 1 deletion src/RhinoInside.Revit.GH/Types/Topology/SpatialElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Curve[] Boundaries
var segments = spatial.GetBoundarySegments(options);
_Boundaries = segments.Select
(
loop => Curve.JoinCurves(loop.Select(x => Curve.ProjectToPlane(x.GetCurve().ToCurve(), plane)), tol.VertexTolerance, preserveDirection: false)[0]
loop => Curve.JoinCurves(loop.Select(x => x.GetCurve().ToCurve().ProjectToPlane(plane)), tol.VertexTolerance, preserveDirection: false)[0]
).ToArray();
}
}
Expand Down

0 comments on commit 6cd0507

Please sign in to comment.