Skip to content

Commit

Permalink
perf(civil): CNX-9178 Civil: Optimised ToSpeckle conversion performan…
Browse files Browse the repository at this point in the history
…ce of Tin Surfaces (#3274)

* Potential optimisation for civil converter

* formatting

* removed ToNative

* removed redundant check for invisible faces

---------

Co-authored-by: Claire Kuang <[email protected]>
  • Loading branch information
JR-Morgan and clairekuang authored Apr 11, 2024
1 parent 118f6bb commit f0f9eb3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,44 +736,40 @@ private Featureline FeaturelineToSpeckle(CivilDB.CorridorFeatureLine featureline
public Mesh SurfaceToSpeckle(TinSurface surface)
{
// output vars
List<Point3d> vertices = new();
var faces = new List<int>();
List<double> vertices = new();
List<int> faces = new ();
Dictionary<Point3d, int> indices = new();

int indexCounter = 0;
foreach (var triangle in surface.GetTriangles(false))
{
var triangleVertices = new List<Point3d> { triangle.Vertex1.Location, triangle.Vertex2.Location, triangle.Vertex3.Location };

#if CIVIL2023 || CIVIL2024 // skip any triangles that are hidden in the surface!
if (!triangle.IsVisible)
{
triangle.Dispose();
continue;
}
#endif

// store vertices
var faceIndices = new List<int>();
foreach (var vertex in triangleVertices)
try
{
if (!vertices.Contains(vertex))
Point3d[] triangleVertices = { triangle.Vertex1.Location, triangle.Vertex2.Location, triangle.Vertex3.Location };
foreach (Point3d p in triangleVertices)
{
faceIndices.Add(vertices.Count);
vertices.Add(vertex);
}
else
{
faceIndices.Add(vertices.IndexOf(vertex));
if (!indices.ContainsKey(p))
{
var scaledP = ToExternalCoordinates(p);
vertices.Add(scaledP.X);
vertices.Add(scaledP.Y);
vertices.Add(scaledP.Z);
indices.Add(p, indexCounter);
indexCounter++;
}
}
faces.Add(3);
faces.Add(indices[triangleVertices[0]]);
faces.Add(indices[triangleVertices[1]]);
faces.Add(indices[triangleVertices[2]]);
}
finally
{
triangle.Dispose();
}

// get face
faces.AddRange(new List<int> { 3, faceIndices[0], faceIndices[1], faceIndices[2] });

triangle.Dispose();
}

var speckleVertices = vertices.SelectMany(o => PointToSpeckle(o).ToList()).ToList();

var mesh = new Mesh(speckleVertices, faces)

var mesh = new Mesh(vertices, faces)
{
units = ModelUnits,
bbox = BoxToSpeckle(surface.GeometricExtents)
Expand Down Expand Up @@ -962,7 +958,7 @@ public ApplicationObject TinSurfaceToNative(Mesh mesh, Base props)
{
continue;
}

var a1 = surface.FindVertexAtXY(edgeStart.X, edgeStart.Y);
var a2 = surface.FindVertexAtXY(vertexToAdd.X, vertexToAdd.Y);
surface.AddLine(a1, a2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ public Point PointToSpeckle(Point2d point, string units = null)

public Point3d PointToNative(Point point)
{
var _point = new Point3d(
ScaleToNative(point.x, point.units),
ScaleToNative(point.y, point.units),
ScaleToNative(point.z, point.units)
);
var intPt = ToInternalCoordinates(_point);
var scaleFactor = Units.GetConversionFactor(point.units, ModelUnits);
var scaledPoint = new Point3d(point.x * scaleFactor, point.y * scaleFactor, point.z * scaleFactor);
var intPt = ToInternalCoordinates(scaledPoint);
return intPt;
}

Expand Down

0 comments on commit f0f9eb3

Please sign in to comment.