Skip to content

Commit

Permalink
Refactored ITransformable
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-Morgan committed Jan 13, 2025
1 parent 912971d commit c31d0a0
Show file tree
Hide file tree
Showing 17 changed files with 208 additions and 206 deletions.
17 changes: 17 additions & 0 deletions src/Speckle.Objects/Exceptions/TransformationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Speckle.Sdk;

namespace Speckle.Objects.Exceptions;

/// <summary>
/// <see cref="ITransformable{T}"/> object failed to transform
/// </summary>
public sealed class TransformationException : SpeckleException
{
public TransformationException() { }

public TransformationException(string? message)
: base(message) { }

public TransformationException(string? message, Exception? innerException)
: base(message, innerException) { }
}
23 changes: 7 additions & 16 deletions src/Speckle.Objects/Geometry/Arc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ public class Arc : Base, IHasBoundingBox, ICurve, ITransformable<Arc>
public Box? bbox { get; set; }

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Arc transformed)
public Arc TransformTo(Transform transform)
{
startPoint.TransformTo(transform, out Point transformedStartPoint);
midPoint.TransformTo(transform, out Point transformedMidpoint);
endPoint.TransformTo(transform, out Point transformedEndPoint);
plane.TransformTo(transform, out Plane pln);
Arc arc = new()
var transformedStartPoint = startPoint.TransformTo(transform);
var transformedMidpoint = midPoint.TransformTo(transform);
var transformedEndPoint = endPoint.TransformTo(transform);
var pln = plane.TransformTo(transform);
Arc transformed = new()
{
startPoint = transformedStartPoint,
endPoint = transformedEndPoint,
Expand All @@ -103,16 +103,7 @@ public bool TransformTo(Transform transform, out Arc transformed)
domain = domain,
units = units,
};
transformed = arc;
return true;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Arc arc);
transformed = arc;
return res;
return transformed;
}

/// <summary>
Expand Down
36 changes: 15 additions & 21 deletions src/Speckle.Objects/Geometry/Brep.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.Serialization;
using Speckle.Newtonsoft.Json;
using Speckle.Objects.Exceptions;
using Speckle.Objects.Other;
using Speckle.Objects.Primitive;
using Speckle.Sdk.Common;
Expand Down Expand Up @@ -434,49 +435,50 @@ public List<int> FacesValue
public double volume { get; set; }

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Brep transformed)
public Brep TransformTo(Transform transform)
{
// transform display values
var displayValues = new List<Mesh>(displayValue.Count);
foreach (Mesh v in displayValue)
{
v.TransformTo(transform, out Mesh mesh);
displayValues.Add(mesh);
var transformedMesh = v.TransformTo(transform);
displayValues.Add(transformedMesh);
}

// transform surfaces
var surfaces = new List<Surface>(Surfaces.Count);
foreach (var srf in Surfaces)
{
srf.TransformTo(transform, out Surface surface);
surfaces.Add(surface);
var transformedSurface = srf.TransformTo(transform);
surfaces.Add(transformedSurface);
}

// transform curve3d
var success3D = true;
var transformedCurve3D = new List<ICurve>();
foreach (var curve in Curve3D)
{
if (curve is ITransformable c)
if (curve is ITransformable<ICurve> c)
{
c.TransformTo(transform, out ITransformable tc);
transformedCurve3D.Add((ICurve)tc);
var tc = c.TransformTo(transform);
transformedCurve3D.Add(tc);
}
else
{
success3D = false;
throw new TransformationException(
$"BREP could not be transformed because it contains {curve.GetType()} curves that are not transformable"
);
}
}

// transform vertices
var transformedVertices = new List<Point>(Vertices.Count);
foreach (var vertex in Vertices)
{
vertex.TransformTo(transform, out Point transformedVertex);
Point transformedVertex = vertex.TransformTo(transform);
transformedVertices.Add(transformedVertex);
}

transformed = new Brep
var transformed = new Brep
{
units = units,
displayValue = displayValues,
Expand Down Expand Up @@ -556,15 +558,7 @@ public bool TransformTo(Transform transform, out Brep transformed)
);
}

return success3D;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Brep brep);
transformed = brep;
return res;
return transformed;
}

[OnDeserialized]
Expand Down
19 changes: 8 additions & 11 deletions src/Speckle.Objects/Geometry/ControlPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,15 @@ public ControlPoint(double x, double y, double z, double w, string units, string
}
}

public bool TransformTo(Transform transform, out ControlPoint transformed)
public override Point TransformTo(Transform transform)
{
TransformTo(transform, out Point transformedPoint);
transformed = new ControlPoint(
transformedPoint.x,
transformedPoint.y,
transformedPoint.z,
weight,
units,
applicationId
);
return true;
return ((ITransformable<ControlPoint>)this).TransformTo(transform);
}

ControlPoint ITransformable<ControlPoint>.TransformTo(Transform transform)
{
Point transformedPoint = base.TransformTo(transform);
return new ControlPoint(transformedPoint.x, transformedPoint.y, transformedPoint.z, weight, units, applicationId);
}

public override string ToString()
Expand Down
31 changes: 12 additions & 19 deletions src/Speckle.Objects/Geometry/Curve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,34 @@ public class Curve : Base, ICurve, IHasBoundingBox, IHasArea, ITransformable<Cur
public Box? bbox { get; set; }

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Curve transformed)
public Curve TransformTo(Transform transform)
{
// transform points
var transformedPoints = new List<Point>();
foreach (var point in GetPoints())
var originalPoints = GetPoints();
var transformedPoints = new List<double>(originalPoints.Count * 3);
foreach (var point in originalPoints)
{
point.TransformTo(transform, out Point transformedPoint);
transformedPoints.Add(transformedPoint);
Point transformedPoint = point.TransformTo(transform);
transformedPoints.Add(transformedPoint.x);
transformedPoints.Add(transformedPoint.y);
transformedPoints.Add(transformedPoint.z);
}

var result = displayValue.TransformTo(transform, out ITransformable polyline);
transformed = new Curve
var transformedPolyline = displayValue.TransformTo(transform);
return new Curve
{
degree = degree,
periodic = periodic,
rational = rational,
points = transformedPoints.SelectMany(o => o.ToList()).ToList(),
points = transformedPoints,
weights = weights,
knots = knots,
displayValue = (Polyline)polyline,
displayValue = transformedPolyline,
closed = closed,
units = units,
applicationId = applicationId,
domain = domain != null ? new Interval { start = domain.start, end = domain.end } : Interval.UnitInterval,
};

return result;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Curve curve);
transformed = curve;
return res;
}

/// <returns><see cref="points"/> as list of <see cref="Point"/>s</returns>
Expand Down
16 changes: 4 additions & 12 deletions src/Speckle.Objects/Geometry/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,18 @@ public Line(IList<double> coordinates, string units, string? applicationId = nul

public Box? bbox { get; set; }

public bool TransformTo(Transform transform, out Line transformed)
public Line TransformTo(Transform transform)
{
start.TransformTo(transform, out Point transformedStart);
end.TransformTo(transform, out Point transformedEnd);
transformed = new Line
Point transformedStart = start.TransformTo(transform);
Point transformedEnd = end.TransformTo(transform);
return new Line
{
start = transformedStart,
end = transformedEnd,
applicationId = applicationId,
units = units,
domain = new() { start = domain.start, end = domain.end },
};
return true;
}

public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Line line);
transformed = line;
return res;
}

public List<double> ToList()
Expand Down
31 changes: 12 additions & 19 deletions src/Speckle.Objects/Geometry/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool Transform(Transform transform)
vertices = GetPoints()
.SelectMany(vertex =>
{
vertex.TransformTo(transform, out Point transformedVertex);
var transformedVertex = vertex.TransformTo(transform);
return transformedVertex.ToList();
})
.ToList();
Expand All @@ -53,36 +53,29 @@ public bool Transform(Transform transform)
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Mesh transformed)
public Mesh TransformTo(Transform transform)
{
// transform vertices
var transformedVertices = new List<Point>();
foreach (var vertex in GetPoints())
var originalVertices = GetPoints();
var transformedVertices = new List<double>(originalVertices.Count * 3);
foreach (var vertex in originalVertices)
{
vertex.TransformTo(transform, out Point transformedVertex);
transformedVertices.Add(transformedVertex);
Point transformedVertex = vertex.TransformTo(transform);
transformedVertices.Add(transformedVertex.x);
transformedVertices.Add(transformedVertex.y);
transformedVertices.Add(transformedVertex.z);
}

transformed = new Mesh
return new Mesh
{
vertices = transformedVertices.SelectMany(o => o.ToList()).ToList(),
vertices = transformedVertices,
textureCoordinates = textureCoordinates,
applicationId = applicationId ?? id,
faces = faces,
colors = colors,
units = units,
["renderMaterial"] = this["renderMaterial"],
};
transformed["renderMaterial"] = this["renderMaterial"];

return true;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Mesh brep);
transformed = brep;
return res;
}

#region Convenience Methods
Expand Down
22 changes: 6 additions & 16 deletions src/Speckle.Objects/Geometry/Plane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class Plane : Base, ITransformable<Plane>
public required string units { get; set; }

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Plane transformed)
public Plane TransformTo(Transform transform)
{
origin.TransformTo(transform, out Point transformedOrigin);
normal.TransformTo(transform, out Vector transformedNormal);
xdir.TransformTo(transform, out Vector transformedXdir);
ydir.TransformTo(transform, out Vector transformedYdir);
transformed = new Plane
var transformedOrigin = origin.TransformTo(transform);
var transformedNormal = normal.TransformTo(transform);
var transformedXdir = xdir.TransformTo(transform);
var transformedYdir = ydir.TransformTo(transform);
return new Plane
{
origin = transformedOrigin,
normal = transformedNormal,
Expand All @@ -52,16 +52,6 @@ public bool TransformTo(Transform transform, out Plane transformed)
applicationId = applicationId,
units = units,
};

return true;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Plane plane);
transformed = plane;
return res;
}

/// <summary>
Expand Down
13 changes: 2 additions & 11 deletions src/Speckle.Objects/Geometry/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Point(double x, double y, double z, string units, string? applicationId =
public required string units { get; set; }

/// <inheritdoc/>
public bool TransformTo(Transform transform, out Point transformed)
public virtual Point TransformTo(Transform transform)
{
var matrix = transform.matrix;

Expand All @@ -68,16 +68,7 @@ public bool TransformTo(Transform transform, out Point transformed)
var y = (this.x * matrix.M21 + this.y * matrix.M22 + this.z * matrix.M23 + unitFactor * matrix.M24) / divisor;
var z = (this.x * matrix.M31 + this.y * matrix.M32 + this.z * matrix.M33 + unitFactor * matrix.M34) / divisor;

transformed = new Point(x, y, z, units, applicationId);
return true;
}

/// <inheritdoc/>
public bool TransformTo(Transform transform, out ITransformable transformed)
{
var res = TransformTo(transform, out Point pt);
transformed = pt;
return res;
return new Point(x, y, z, units, applicationId);
}

/// <summary>
Expand Down
Loading

0 comments on commit c31d0a0

Please sign in to comment.