From d987cf09ed320e348d3010bb4cf7023894effb31 Mon Sep 17 00:00:00 2001 From: Bert Temme Date: Tue, 30 Jul 2024 09:49:39 +0200 Subject: [PATCH] upgrading dependencies --- README.md | 2 +- nuget.config | 1 - .../triangulator.tests.csproj | 12 ++-- src/triangulator/Triangulator.cs | 70 +++++++++++++++++++ src/triangulator/triangulator.csproj | 6 +- 5 files changed, 80 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 6ea4b62..5f47009 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Triangulator -.NET 6 library for triangulating 2D/3D WKB geometries (PolyhedralSurface/MultiPolygon/Polygon/Multiline/Line) using Earcut algorithm. Lines +.NET 8 library for triangulating 2D/3D WKB geometries (PolyhedralSurface/MultiPolygon/Polygon/Multiline/Line) using Earcut algorithm. Lines can be triangulated using the TubeGeometry algorithm from Three.JS. ## NuGet diff --git a/nuget.config b/nuget.config index 4fe2182..554c2f6 100644 --- a/nuget.config +++ b/nuget.config @@ -2,6 +2,5 @@ - \ No newline at end of file diff --git a/src/triangulator.tests/triangulator.tests.csproj b/src/triangulator.tests/triangulator.tests.csproj index 54915e9..77cea61 100644 --- a/src/triangulator.tests/triangulator.tests.csproj +++ b/src/triangulator.tests/triangulator.tests.csproj @@ -1,7 +1,7 @@  - net6.0 + net8.0 false @@ -9,15 +9,15 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + - + diff --git a/src/triangulator/Triangulator.cs b/src/triangulator/Triangulator.cs index f08233f..1338502 100644 --- a/src/triangulator/Triangulator.cs +++ b/src/triangulator/Triangulator.cs @@ -1,6 +1,7 @@ using EarcutNet; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Numerics; using Wkx; @@ -53,6 +54,75 @@ public static MultiPolygon Triangulate(MultiLineString lineString, float radius } + public static MultiPolygon Triangulate1(LineString lineString, float radius = 1, int? tubularSegments = 64, int? radialSegments = 8, bool closed = false) + { + if (lineString.Points.Count < 2) + { + throw new ArgumentException("LineString must contain at least 2 points"); + } + + var polygons = new List(); + + var points = new List(); + foreach (var point in lineString.Points) + { + points.Add(new THREE.Vector3((float)point.X, (float)point.Y, (float)point.Z)); + Debug.WriteLine($"new THREE.Vector3({point.X}, {point.Y}, {point.Z}),"); + } + + THREE.Curve curve = points.Count == 2 ? new THREE.LineCurve3(points[0], points[1]) : new THREE.CatmullRomCurve3(points); + + var divisions = 2; + + + var splinePoints = new List(); + for (var i = 0; i < points.Count - 1; i++) + { + var start = points[i]; + var end = points[i + 1]; + + splinePoints.Add(start); + + for (var j = 1; j <= divisions; j++) + { + var t = j / divisions; + var intermediatePoint = curve.GetPointAt((i + t) / (points.Count - 1)); + splinePoints.Add(intermediatePoint); + } + + splinePoints.Add(end); + } + + + var splineCurve = new THREE.CatmullRomCurve3(splinePoints); + + var tubeGeometry = new THREE.TubeGeometry(splineCurve, tubularSegments, radius, radialSegments, closed); + + foreach (var face in tubeGeometry.Faces) + { + var p0 = tubeGeometry.Vertices[face.a]; + var p1 = tubeGeometry.Vertices[face.b]; + var p2 = tubeGeometry.Vertices[face.c]; + + var polygon = new Polygon(); + polygon.ExteriorRing.Points.Add(new Point(p0.X, p0.Y, p0.Z)); + polygon.ExteriorRing.Points.Add(new Point(p1.X, p1.Y, p1.Z)); + polygon.ExteriorRing.Points.Add(new Point(p2.X, p2.Y, p2.Z)); + polygon.ExteriorRing.Points.Add(new Point(p0.X, p0.Y, p0.Z)); + + polygons.Add(polygon); + } + + var result = new MultiPolygon + { + Dimension = Dimension.Xyz + }; + result.Geometries.AddRange(polygons); + return result; + } + + + public static MultiPolygon Triangulate(LineString lineString, float radius = 1, int? tubularSegments = 64, int? radialSegments = 8, bool closed = false) { if(lineString.Points.Count < 2) diff --git a/src/triangulator/triangulator.csproj b/src/triangulator/triangulator.csproj index a2b7bac..16ba07d 100644 --- a/src/triangulator/triangulator.csproj +++ b/src/triangulator/triangulator.csproj @@ -1,7 +1,7 @@  - net6.0 + net8.0 true bertt.triangulator Bert Temme @@ -21,8 +21,8 @@ - - + +