Skip to content

Commit

Permalink
more converters done!
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Jun 18, 2024
1 parent 34760f5 commit 91d2336
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

[NameAndRankValue(nameof(RG.Mesh), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class MeshToSpeckleConverter : ITypedConverter<RG.Mesh, SOG.Mesh>
[NameAndRankValue(nameof(IRhinoMesh), NameAndRankValueAttribute.SPECKLE_DEFAULT_RANK)]
public class MeshToSpeckleConverter : ITypedConverter<IRhinoMesh, SOG.Mesh>
{
private readonly ITypedConverter<RG.Point3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<RG.Box, SOG.Box> _boxConverter;
private readonly ITypedConverter<IRhinoBox, SOG.Box> _boxConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly IRhinoBoxFactory _rhinoBoxFactory;

public MeshToSpeckleConverter(
ITypedConverter<RG.Point3d, SOG.Point> pointConverter,
ITypedConverter<RG.Box, SOG.Box> boxConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack
)
ITypedConverter<IRhinoBox, SOG.Box> boxConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack, IRhinoBoxFactory rhinoBoxFactory)
{
_pointConverter = pointConverter;
_boxConverter = boxConverter;
_contextStack = contextStack;
_rhinoBoxFactory = rhinoBoxFactory;
}

/// <summary>
Expand All @@ -28,7 +27,7 @@ IConversionContextStack<RhinoDoc, UnitSystem> contextStack
/// <param name="target">The Rhino Mesh to be converted.</param>
/// <returns>The converted Speckle Mesh.</returns>
/// <exception cref="SpeckleConversionException">Thrown when the Rhino Mesh has 0 vertices or faces.</exception>
public SOG.Mesh Convert(RG.Mesh target)
public SOG.Mesh Convert(IRhinoMesh target)
{
if (target.Vertices.Count == 0 || target.Faces.Count == 0)
{
Expand All @@ -38,7 +37,7 @@ public SOG.Mesh Convert(RG.Mesh target)
var vertexCoordinates = target.Vertices.ToPoint3dArray().SelectMany(pt => new[] { pt.X, pt.Y, pt.Z }).ToList();
var faces = new List<int>();

foreach (RG.MeshNgon polygon in target.GetNgonAndFacesEnumerable())
foreach (IRhinoMeshNgon polygon in target.GetNgonAndFacesEnumerable())
{
var vertIndices = polygon.BoundaryVertexIndexList();
int n = vertIndices.Length;
Expand All @@ -55,7 +54,7 @@ public SOG.Mesh Convert(RG.Mesh target)

var colors = target.VertexColors.Select(cl => cl.ToArgb()).ToList();
var volume = target.IsClosed ? target.Volume() : 0;
var bbox = _boxConverter.Convert(new RG.Box(target.GetBoundingBox(false)));
var bbox = _boxConverter.Convert(_rhinoBoxFactory.CreateBox(target.GetBoundingBox(false)));

return new SOG.Mesh(vertexCoordinates, faces, colors, textureCoordinates, _contextStack.Current.SpeckleUnits)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class NurbsCurveConverter : ITypedConverter<RG.NurbsCurve, SOG.Curve>
public class NurbsCurveConverter : ITypedConverter<IRhinoNurbsCurve, SOG.Curve>
{
private readonly ITypedConverter<RG.Polyline, SOG.Polyline> _polylineConverter;
private readonly ITypedConverter<RG.Interval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<RG.Box, SOG.Box> _boxConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly ITypedConverter<IRhinoPolyline, SOG.Polyline> _polylineConverter;
private readonly ITypedConverter<IRhinoInterval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<IRhinoBox, SOG.Box> _boxConverter;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;
private readonly IRhinoBoxFactory _rhinoBoxFactory;

public NurbsCurveConverter(
ITypedConverter<RG.Polyline, SOG.Polyline> polylineConverter,
ITypedConverter<RG.Interval, SOP.Interval> intervalConverter,
ITypedConverter<RG.Box, SOG.Box> boxConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack
)
ITypedConverter<IRhinoPolyline, SOG.Polyline> polylineConverter,
ITypedConverter<IRhinoInterval, SOP.Interval> intervalConverter,
ITypedConverter<IRhinoBox, SOG.Box> boxConverter,
IConversionContextStack<IRhinoDoc, RhinoUnitSystem>contextStack , IRhinoBoxFactory rhinoBoxFactory)
{
_polylineConverter = polylineConverter;
_intervalConverter = intervalConverter;
_boxConverter = boxConverter;
_contextStack = contextStack;
_rhinoBoxFactory = rhinoBoxFactory;
}

/// <summary>
Expand All @@ -34,7 +35,7 @@ IConversionContextStack<RhinoDoc, UnitSystem> contextStack
/// It adds 1 extra knot at the start and end of the vector by repeating the first and last value.
/// This is because Rhino's standard of (controlPoints + degree + 1) wasn't followed on other software.
/// </remarks>
public SOG.Curve Convert(RG.NurbsCurve target)
public SOG.Curve Convert(IRhinoNurbsCurve target)
{
target.ToPolyline(0, 1, 0, 0, 0, 0.1, 0, 0, true).TryGetPolyline(out var poly);
if (target.IsClosed)
Expand Down Expand Up @@ -63,7 +64,7 @@ public SOG.Curve Convert(RG.NurbsCurve target)
domain = _intervalConverter.Convert(nurbsCurve.Domain),
closed = nurbsCurve.IsClosed,
length = nurbsCurve.GetLength(),
bbox = _boxConverter.Convert(new RG.Box(nurbsCurve.GetBoundingBox(true)))
bbox = _boxConverter.Convert(_rhinoBoxFactory.CreateBox(nurbsCurve.GetBoundingBox(true)))
};

return myCurve;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
using Rhino;
using Rhino.Geometry.Collections;
using Speckle.Converters.Common;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class NurbsSurfaceToSpeckleConverter : ITypedConverter<RG.NurbsSurface, SOG.Surface>
public class NurbsSurfaceToSpeckleConverter : ITypedConverter<IRhinoNurbsSurface, SOG.Surface>
{
private readonly ITypedConverter<RG.Box, SOG.Box> _boxConverter;
private readonly ITypedConverter<RG.Interval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<RG.ControlPoint, SOG.ControlPoint> _controlPointConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly ITypedConverter<IRhinoBox, SOG.Box> _boxConverter;
private readonly ITypedConverter<IRhinoInterval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<IRhinoControlPoint, SOG.ControlPoint> _controlPointConverter;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;
private readonly IRhinoBoxFactory _rhinoBoxFactory;

public NurbsSurfaceToSpeckleConverter(
ITypedConverter<RG.Box, SOG.Box> boxConverter,
ITypedConverter<RG.Interval, SOP.Interval> intervalConverter,
ITypedConverter<RG.ControlPoint, SOG.ControlPoint> controlPointConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack
)
ITypedConverter<IRhinoBox, SOG.Box> boxConverter,
ITypedConverter<IRhinoInterval, SOP.Interval> intervalConverter,
ITypedConverter<IRhinoControlPoint, SOG.ControlPoint> controlPointConverter,
IConversionContextStack<IRhinoDoc, RhinoUnitSystem> contextStack, IRhinoBoxFactory rhinoBoxFactory)
{
_boxConverter = boxConverter;
_intervalConverter = intervalConverter;
_controlPointConverter = controlPointConverter;
_contextStack = contextStack;
_rhinoBoxFactory = rhinoBoxFactory;
}

/// <summary>
/// Converts a NurbsSurface object to a Surface object.
/// </summary>
/// <param name="target">The NurbsSurface object to convert.</param>
/// <returns>A Surface object representing the converted NurbsSurface.</returns>
public SOG.Surface Convert(RG.NurbsSurface target)
public SOG.Surface Convert(IRhinoNurbsSurface target)
{
var result = new SOG.Surface
{
Expand All @@ -44,15 +44,15 @@ public SOG.Surface Convert(RG.NurbsSurface target)
knotsU = target.KnotsU.ToList(),
knotsV = target.KnotsV.ToList(),
units = _contextStack.Current.SpeckleUnits,
bbox = _boxConverter.Convert(new RG.Box(target.GetBoundingBox(true)))
bbox = _boxConverter.Convert(_rhinoBoxFactory.CreateBox(target.GetBoundingBox(true)))
};

result.SetControlPoints(ControlPointsToSpeckle(target.Points));

return result;
}

private List<List<SOG.ControlPoint>> ControlPointsToSpeckle(NurbsSurfacePointList controlPoints)
private List<List<SOG.ControlPoint>> ControlPointsToSpeckle(IRhinoNurbsSurfacePointList controlPoints)
{
var points = new List<List<SOG.ControlPoint>>();
for (var i = 0; i < controlPoints.CountU; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class PlaneToSpeckleConverter : ITypedConverter<RG.Plane, SOG.Plane>
public class PlaneToSpeckleConverter : ITypedConverter<IRhinoPlane, SOG.Plane>
{
private readonly ITypedConverter<RG.Vector3d, SOG.Vector> _vectorConverter;
private readonly ITypedConverter<RG.Point3d, SOG.Point> _pointConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly ITypedConverter<IRhinoVector3d, SOG.Vector> _vectorConverter;
private readonly ITypedConverter<IRhinoPoint3d, SOG.Point> _pointConverter;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;

public PlaneToSpeckleConverter(
ITypedConverter<RG.Vector3d, SOG.Vector> vectorConverter,
ITypedConverter<RG.Point3d, SOG.Point> pointConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack
ITypedConverter<IRhinoVector3d, SOG.Vector> vectorConverter,
ITypedConverter<IRhinoPoint3d, SOG.Point> pointConverter,
IConversionContextStack<IRhinoDoc, RhinoUnitSystem> contextStack
)
{
_vectorConverter = vectorConverter;
Expand All @@ -26,7 +26,7 @@ IConversionContextStack<RhinoDoc, UnitSystem> contextStack
/// </summary>
/// <param name="target">The instance of Rhino Plane to convert.</param>
/// <returns>The converted instance of Speckle Plane.</returns>
public SOG.Plane Convert(RG.Plane target) =>
public SOG.Plane Convert(IRhinoPlane target) =>
new(
_pointConverter.Convert(target.Origin),
_vectorConverter.Convert(target.ZAxis),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class PointToSpeckleConverter : ITypedConverter<RG.Point3d, SOG.Point>, ITypedConverter<RG.Point, SOG.Point>
public class PointToSpeckleConverter : ITypedConverter<IRhinoPoint3d, SOG.Point>, ITypedConverter<IRhinoPoint, SOG.Point>
{
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;

public PointToSpeckleConverter(IConversionContextStack<RhinoDoc, UnitSystem> contextStack)
public PointToSpeckleConverter(IConversionContextStack<IRhinoDoc, RhinoUnitSystem> contextStack)
{
_contextStack = contextStack;
}
Expand All @@ -18,7 +18,7 @@ public PointToSpeckleConverter(IConversionContextStack<RhinoDoc, UnitSystem> con
/// </summary>
/// <param name="target">The Rhino 3D point to convert.</param>
/// <returns>The converted Speckle point.</returns>
public SOG.Point Convert(RG.Point3d target) => new(target.X, target.Y, target.Z, _contextStack.Current.SpeckleUnits);
public SOG.Point Convert(IRhinoPoint3d target) => new(target.X, target.Y, target.Z, _contextStack.Current.SpeckleUnits);

public SOG.Point Convert(RG.Point target) => Convert(target.Location);
public SOG.Point Convert(IRhinoPoint target) => Convert(target.Location);
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using Objects;
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class PolyCurveToSpeckleConverter : ITypedConverter<RG.PolyCurve, SOG.Polycurve>
public class PolyCurveToSpeckleConverter : ITypedConverter<IRhinoPolyCurve, SOG.Polycurve>
{
public ITypedConverter<RG.Curve, ICurve>? CurveConverter { get; set; } // POC: CNX-9279 This created a circular dependency on the constructor, making it a property allows for the container to resolve it correctly
private readonly ITypedConverter<RG.Interval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<RG.Box, SOG.Box> _boxConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
public ITypedConverter<IRhinoCurve, ICurve>? CurveConverter { get; set; } // POC: CNX-9279 This created a circular dependency on the constructor, making it a property allows for the container to resolve it correctly
private readonly ITypedConverter<IRhinoInterval, SOP.Interval> _intervalConverter;
private readonly ITypedConverter<IRhinoBox, SOG.Box> _boxConverter;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;
private readonly IRhinoBoxFactory _rhinoBoxFactory;

public PolyCurveToSpeckleConverter(
ITypedConverter<RG.Interval, SOP.Interval> intervalConverter,
ITypedConverter<RG.Box, SOG.Box> boxConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack
)
ITypedConverter<IRhinoInterval, SOP.Interval> intervalConverter,
ITypedConverter<IRhinoBox, SOG.Box> boxConverter,
IConversionContextStack<IRhinoDoc, RhinoUnitSystem> contextStack, IRhinoBoxFactory rhinoBoxFactory)
{
_intervalConverter = intervalConverter;
_boxConverter = boxConverter;
_contextStack = contextStack;
_rhinoBoxFactory = rhinoBoxFactory;
}

/// <summary>
Expand All @@ -32,14 +33,14 @@ IConversionContextStack<RhinoDoc, UnitSystem> contextStack
/// This method removes the nesting of the PolyCurve by duplicating the segments at a granular level.
/// All PolyLIne, PolyCurve and NURBS curves with G1 discontinuities will be broken down.
/// </remarks>
public SOG.Polycurve Convert(RG.PolyCurve target)
public SOG.Polycurve Convert(IRhinoPolyCurve target)
{
var myPoly = new SOG.Polycurve
{
closed = target.IsClosed,
domain = _intervalConverter.Convert(target.Domain),
length = target.GetLength(),
bbox = _boxConverter.Convert(new RG.Box(target.GetBoundingBox(true))),
bbox = _boxConverter.Convert(_rhinoBoxFactory.CreateBox(target.GetBoundingBox(true))),
segments = target.DuplicateSegments().Select(x => CurveConverter.NotNull().Convert(x)).ToList(),
units = _contextStack.Current.SpeckleUnits
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
using Rhino;
using Speckle.Converters.Common;
using Speckle.Converters.Common;
using Speckle.Converters.Common.Objects;
using Speckle.Rhino7.Interfaces;

namespace Speckle.Converters.Rhino7.ToSpeckle.Raw;

public class PolylineToSpeckleConverter
: ITypedConverter<RG.Polyline, SOG.Polyline>,
ITypedConverter<RG.PolylineCurve, SOG.Polyline>
: ITypedConverter<IRhinoPolyline, SOG.Polyline>,
ITypedConverter<IRhinoPolylineCurve, SOG.Polyline>
{
private readonly ITypedConverter<RG.Point3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<RG.Box, SOG.Box> _boxConverter;
private readonly ITypedConverter<RG.Interval, SOP.Interval> _intervalConverter;
private readonly IConversionContextStack<RhinoDoc, UnitSystem> _contextStack;
private readonly ITypedConverter<IRhinoPoint3d, SOG.Point> _pointConverter;
private readonly ITypedConverter<IRhinoBox, SOG.Box> _boxConverter;
private readonly ITypedConverter<IRhinoInterval, SOP.Interval> _intervalConverter;
private readonly IConversionContextStack<IRhinoDoc, RhinoUnitSystem> _contextStack;
private readonly IRhinoBoxFactory _rhinoBoxFactory;

public PolylineToSpeckleConverter(
ITypedConverter<RG.Point3d, SOG.Point> pointConverter,
ITypedConverter<RG.Box, SOG.Box> boxConverter,
IConversionContextStack<RhinoDoc, UnitSystem> contextStack,
ITypedConverter<RG.Interval, SOP.Interval> intervalConverter
)
ITypedConverter<IRhinoPoint3d, SOG.Point> pointConverter,
ITypedConverter<IRhinoBox, SOG.Box> boxConverter,
IConversionContextStack<IRhinoDoc, RhinoUnitSystem> contextStack,
ITypedConverter<IRhinoInterval, SOP.Interval> intervalConverter, IRhinoBoxFactory rhinoBoxFactory)
{
_pointConverter = pointConverter;
_boxConverter = boxConverter;
_contextStack = contextStack;
_intervalConverter = intervalConverter;
_rhinoBoxFactory = rhinoBoxFactory;
}

/// <summary>
Expand All @@ -32,9 +33,9 @@ public PolylineToSpeckleConverter(
/// <param name="target">The Rhino polyline to be converted.</param>
/// <returns>The converted Speckle polyline.</returns>
/// <remarks>⚠️ This conversion assumes domain interval is (0,LENGTH) as Rhino Polylines have no domain. If needed, you may want to use PolylineCurve conversion instead. </remarks>
public SOG.Polyline Convert(RG.Polyline target)
public SOG.Polyline Convert(IRhinoPolyline target)
{
var box = _boxConverter.Convert(new RG.Box(target.BoundingBox));
var box = _boxConverter.Convert(_rhinoBoxFactory.CreateBox(target.BoundingBox));
var points = target.Select(pt => _pointConverter.Convert(pt)).ToList();

if (target.IsClosed)
Expand All @@ -48,8 +49,8 @@ public SOG.Polyline Convert(RG.Polyline target)
)
{
bbox = box,
length = target.Length,
domain = new(0, target.Length),
length = target.Count,
domain = new(0, target.Count),
closed = target.IsClosed
};
}
Expand All @@ -60,7 +61,7 @@ public SOG.Polyline Convert(RG.Polyline target)
/// <param name="target">The Rhino PolylineCurve to be converted.</param>
/// <returns>The converted Speckle polyline.</returns>
/// <remarks>✅ This conversion respects the domain of the original PolylineCurve</remarks>
public SOG.Polyline Convert(RG.PolylineCurve target)
public SOG.Polyline Convert(IRhinoPolylineCurve target)
{
var result = Convert(target.ToPolyline());
result.domain = _intervalConverter.Convert(target.Domain);
Expand Down
Loading

0 comments on commit 91d2336

Please sign in to comment.