From 483c3b3830ef023b2ea1e66d077dc68ad293ebce Mon Sep 17 00:00:00 2001 From: Johann Dirry Date: Wed, 19 Apr 2023 18:27:08 +0200 Subject: [PATCH] code cleanup --- .gitignore | 1 + src/Triangle.Examples/Examples/Example1.cs | 6 +- src/Triangle.Examples/Examples/Example10.cs | 18 +- src/Triangle.Examples/Examples/Example11.cs | 12 +- src/Triangle.Examples/Examples/Example2.cs | 14 +- src/Triangle.Examples/Examples/Example3.cs | 6 +- src/Triangle.Examples/Examples/Example4.cs | 14 +- src/Triangle.Examples/Examples/Example5.cs | 8 +- src/Triangle.Examples/Examples/Example6.cs | 14 +- src/Triangle.Examples/Examples/Example7.cs | 12 +- src/Triangle.Examples/Examples/Example8.cs | 16 +- src/Triangle.Examples/Examples/Example9.cs | 10 +- src/Triangle.Examples/Examples/ExamplePar.cs | 28 +- src/Triangle.Examples/Generate.cs | 34 +- src/Triangle.Examples/Program.cs | 10 +- .../Triangle.Examples.csproj | 2 + src/Triangle.Rendering/Buffer/IndexBuffer.cs | 4 +- src/Triangle.Rendering/Buffer/VertexBuffer.cs | 16 +- src/Triangle.Rendering/ColorManager.cs | 4 +- src/Triangle.Rendering/IRenderContext.cs | 4 +- src/Triangle.Rendering/IRenderLayer.cs | 4 +- src/Triangle.Rendering/Projection.cs | 48 +-- src/Triangle.Rendering/RenderContext.cs | 35 +- src/Triangle.Rendering/RenderLayer.cs | 30 +- src/Triangle.Rendering/RenderManager.cs | 58 ++- src/Triangle.Rendering/Text/EpsDocument.cs | 8 +- src/Triangle.Rendering/Text/EpsImage.cs | 82 ++-- .../Text/FormattingStreamWriter.cs | 8 +- src/Triangle.Rendering/Text/PageSize.cs | 51 +-- src/Triangle.Rendering/Text/SvgImage.cs | 56 ++- .../Triangle.Rendering.csproj | 2 + src/Triangle.Rendering/Util/ColorMap.cs | 26 +- src/Triangle.Tests/Geomerty/ContourTest.cs | 8 +- src/Triangle.Tests/Geomerty/PolygonTest.cs | 2 +- src/Triangle.Tests/Helper.cs | 4 +- src/Triangle.Tests/Tools/InterpolationTest.cs | 4 +- .../Tools/IntersectionHelperTest.cs | 6 +- .../Tools/QualityMeasureTest.cs | 2 +- src/Triangle.Tests/Tools/StatisticTest.cs | 6 +- src/Triangle.Tests/Topology/OtriTest.cs | 4 +- src/Triangle.Tests/Triangle.Tests.csproj | 2 + src/Triangle.Tests/TrianglePoolTest.cs | 6 +- src/Triangle/Behavior.cs | 124 ++---- src/Triangle/Enums.cs | 46 --- src/Triangle/Enums/FindDirectionResult.cs | 11 + src/Triangle/Enums/InsertVertexResult.cs | 12 + src/Triangle/Enums/LocateResult.cs | 10 + src/Triangle/Enums/NodeNumbering.cs | 7 + src/Triangle/Enums/VertexType.cs | 14 + src/Triangle/Geometry/Contour.cs | 42 +- src/Triangle/Geometry/ExtensionMethods.cs | 18 +- src/Triangle/Geometry/Point.cs | 57 +-- src/Triangle/Geometry/Polygon.cs | 46 +-- src/Triangle/Geometry/Rectangle.cs | 64 ++- src/Triangle/Geometry/RegionPointer.cs | 4 +- src/Triangle/Geometry/Segment.cs | 15 +- src/Triangle/Geometry/Vertex.cs | 2 +- src/Triangle/IO/DebugWriter.cs | 123 +++--- src/Triangle/IO/FileProcessor.cs | 6 +- src/Triangle/IO/IMeshFormat.cs | 2 +- src/Triangle/IO/IPolygonFormat.cs | 2 +- src/Triangle/IO/InputTriangle.cs | 12 +- src/Triangle/IO/TriangleFormat.cs | 10 +- src/Triangle/IO/TriangleReader.cs | 186 +++++---- src/Triangle/IO/TriangleWriter.cs | 374 +++++++++--------- src/Triangle/IPredicates.cs | 5 +- src/Triangle/Log.cs | 25 +- src/Triangle/Mesh.cs | 202 +++++----- src/Triangle/MeshValidator.cs | 28 +- src/Triangle/Meshing/Algorithm/Dwyer.cs | 50 +-- src/Triangle/Meshing/Algorithm/Incremental.cs | 30 +- src/Triangle/Meshing/Algorithm/SweepLine.cs | 82 ++-- src/Triangle/Meshing/ConstraintMesher.cs | 84 ++-- src/Triangle/Meshing/Converter.cs | 66 ++-- src/Triangle/Meshing/Data/BadSubseg.cs | 6 +- src/Triangle/Meshing/Data/BadTriQueue.cs | 30 +- src/Triangle/Meshing/Data/BadTriangle.cs | 6 +- src/Triangle/Meshing/GenericMesher.cs | 46 ++- src/Triangle/Meshing/IConstraintMesher.cs | 2 +- src/Triangle/Meshing/IMesh.cs | 6 +- src/Triangle/Meshing/IQualityMesher.cs | 2 +- src/Triangle/Meshing/ITriangulator.cs | 2 +- .../Meshing/Iterators/EdgeIterator.cs | 14 +- .../Meshing/Iterators/RegionIterator.cs | 18 +- .../Meshing/Iterators/VertexCirculator.cs | 12 +- src/Triangle/Meshing/QualityMesher.cs | 60 +-- src/Triangle/Meshing/QualityOptions.cs | 6 +- src/Triangle/NewLocation.cs | 58 +-- src/Triangle/RobustPredicates.cs | 16 +- src/Triangle/Smoothing/SimpleSmoother.cs | 29 +- src/Triangle/Smoothing/VoronoiFactory.cs | 63 ++- src/Triangle/Tools/AdjacencyMatrix.cs | 60 ++- src/Triangle/Tools/CuthillMcKee.cs | 92 ++--- src/Triangle/Tools/Interpolation.cs | 22 +- src/Triangle/Tools/IntersectionHelper.cs | 62 +-- src/Triangle/Tools/PolygonValidator.cs | 30 +- src/Triangle/Tools/QualityMeasure.cs | 46 +-- src/Triangle/Tools/Statistic.cs | 205 +++++----- src/Triangle/Tools/TriangleQuadTree.cs | 96 ++--- src/Triangle/Tools/VertexSorter.cs | 36 +- src/Triangle/Topology/DCEL/DcelMesh.cs | 63 ++- src/Triangle/Topology/DCEL/Face.cs | 26 +- src/Triangle/Topology/DCEL/HalfEdge.cs | 47 ++- src/Triangle/Topology/DCEL/Vertex.cs | 22 +- src/Triangle/Topology/Osub.cs | 2 +- src/Triangle/Topology/Otri.cs | 52 +-- src/Triangle/Topology/SubSegment.cs | 2 +- src/Triangle/Topology/Triangle.cs | 20 +- src/Triangle/Triangle.csproj | 2 + src/Triangle/Triangle.csproj.DotSettings | 2 + src/Triangle/TriangleLocator.cs | 10 +- src/Triangle/TrianglePool.cs | 75 ++-- src/Triangle/TriangleSampler.cs | 8 +- src/Triangle/Voronoi/BoundedVoronoi.cs | 62 +-- src/Triangle/Voronoi/DefaultVoronoiFactory.cs | 2 +- src/Triangle/Voronoi/IVoronoiFactory.cs | 2 +- .../Voronoi/Legacy/BoundedVoronoiLegacy.cs | 119 +++--- src/Triangle/Voronoi/Legacy/IVoronoi.cs | 2 +- src/Triangle/Voronoi/Legacy/SimpleVoronoi.cs | 74 ++-- src/Triangle/Voronoi/Legacy/VoronoiRegion.cs | 50 +-- src/Triangle/Voronoi/StandardVoronoi.cs | 6 +- src/Triangle/Voronoi/VoronoiBase.cs | 60 ++- 122 files changed, 1979 insertions(+), 2198 deletions(-) delete mode 100644 src/Triangle/Enums.cs create mode 100644 src/Triangle/Enums/FindDirectionResult.cs create mode 100644 src/Triangle/Enums/InsertVertexResult.cs create mode 100644 src/Triangle/Enums/LocateResult.cs create mode 100644 src/Triangle/Enums/NodeNumbering.cs create mode 100644 src/Triangle/Enums/VertexType.cs create mode 100644 src/Triangle/Triangle.csproj.DotSettings diff --git a/.gitignore b/.gitignore index 068d336..8c3670a 100644 --- a/.gitignore +++ b/.gitignore @@ -392,3 +392,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +.idea/ \ No newline at end of file diff --git a/src/Triangle.Examples/Examples/Example1.cs b/src/Triangle.Examples/Examples/Example1.cs index 09ea8e7..65dc3d7 100644 --- a/src/Triangle.Examples/Examples/Example1.cs +++ b/src/Triangle.Examples/Examples/Example1.cs @@ -1,9 +1,9 @@  namespace TriangleNet.Examples { - using TriangleNet.Geometry; - using TriangleNet.Meshing.Algorithm; - using TriangleNet.Rendering.Text; + using Geometry; + using Meshing.Algorithm; + using Rendering.Text; /// /// Simple point set triangulation. diff --git a/src/Triangle.Examples/Examples/Example10.cs b/src/Triangle.Examples/Examples/Example10.cs index ac8fe1d..26f958c 100644 --- a/src/Triangle.Examples/Examples/Example10.cs +++ b/src/Triangle.Examples/Examples/Example10.cs @@ -4,7 +4,7 @@ namespace TriangleNet.Examples using System; using System.Collections.Generic; using System.Linq; - using TriangleNet.Geometry; + using Geometry; /// /// Troubleshooting: finding degenerate boundary triangles. @@ -33,7 +33,7 @@ public static bool Run(bool print = false) // The original rectangle. var poly = Rotate(pts, 0); - for (int i = 0; i < 10; i++) + for (var i = 0; i < 10; i++) { var mesh = poly.Triangulate(); @@ -67,18 +67,18 @@ private static IPolygon Rotate(List points, double radians) { var poly = new Polygon(points.Count); - int id = 0; + var id = 0; foreach (var p in points) { - double x = p.X; - double y = p.Y; + var x = p.X; + var y = p.Y; - double s = Math.Sin(radians); - double c = Math.Cos(radians); + var s = Math.Sin(radians); + var c = Math.Cos(radians); - double xr = c * x - s * y; - double yr = s * x + c * y; + var xr = c * x - s * y; + var yr = s * x + c * y; poly.Points.Add(new Vertex(xr, yr) { ID = id++ }); } diff --git a/src/Triangle.Examples/Examples/Example11.cs b/src/Triangle.Examples/Examples/Example11.cs index 11567d6..d2cbae4 100644 --- a/src/Triangle.Examples/Examples/Example11.cs +++ b/src/Triangle.Examples/Examples/Example11.cs @@ -29,7 +29,7 @@ public static bool Run(bool print = false) //var mesh = GetStructuredDataMesh(r); // Generate function values for mesh points. - double[] data = GetFunctionValues(mesh.Vertices); + var data = GetFunctionValues(mesh.Vertices); if (print) SvgImage.Save(mesh, "example-11.svg", 500); @@ -38,13 +38,13 @@ public static bool Run(bool print = false) var xyData = InterpolateData((Mesh)mesh, data, xy); - double error = xy.Max(p => Math.Abs(xyData[p.ID] - F(p))); + var error = xy.Max(p => Math.Abs(xyData[p.ID] - F(p))); // L2 error //double error = Math.Sqrt(xy.Sum(p => Math.Pow(xyData[p.ID] - F(p), 2))); // Define tolerance dependent on mesh dimensions and size. - double tolerance = 0.5 * Math.Max(r.Width, r.Height) / SIZE; + var tolerance = 0.5 * Math.Max(r.Width, r.Height) / SIZE; return error < tolerance; } @@ -62,7 +62,7 @@ private static IMesh GetScatteredDataMesh(Rectangle domain) { var r = new Rectangle(domain); - double h = domain.Width / SIZE; + var h = domain.Width / SIZE; // Generate a rectangle boundary point set (SIZE points on each side). var input = Generate.Rectangle(r, h); @@ -71,7 +71,7 @@ private static IMesh GetScatteredDataMesh(Rectangle domain) h = -h / 2; r.Resize(h, h); - int n = Math.Max(1, SIZE * SIZE - input.Points.Count); + var n = Math.Max(1, SIZE * SIZE - input.Points.Count); // Add more input points (more sampling points, better interpolation). input.Points.AddRange(Generate.RandomPoints(n, r)); @@ -105,7 +105,7 @@ private static double[] InterpolateData(Mesh mesh, double[] data, IEnumerable /// Simple point set triangulation with convex hull. /// - public class Example2 + public sealed class Example2 { public static bool Run(bool print = false) { @@ -38,7 +38,7 @@ private static bool CheckConvexHull(IEnumerable segments) { int first = -1, prev = -1; - Point a = null, b, c; + Point a = new(); var p = RobustPredicates.Default; @@ -63,8 +63,8 @@ private static bool CheckConvexHull(IEnumerable segments) return false; } - b = s.GetVertex(1); - c = s.GetVertex(0); + Point b = s.GetVertex(1); + Point c = s.GetVertex(0); // Check whether the convex hull is traversed in counterclockwise. if (p.CounterClockwise(a, b, c) < 0) diff --git a/src/Triangle.Examples/Examples/Example3.cs b/src/Triangle.Examples/Examples/Example3.cs index d253715..4099d7a 100644 --- a/src/Triangle.Examples/Examples/Example3.cs +++ b/src/Triangle.Examples/Examples/Example3.cs @@ -1,8 +1,8 @@ namespace TriangleNet.Examples { - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Text; + using Geometry; + using Meshing; + using Rendering.Text; /// /// Triangulate a polygon with hole and set minimum angle constraint. diff --git a/src/Triangle.Examples/Examples/Example4.cs b/src/Triangle.Examples/Examples/Example4.cs index 8f43295..2e61d30 100644 --- a/src/Triangle.Examples/Examples/Example4.cs +++ b/src/Triangle.Examples/Examples/Example4.cs @@ -2,11 +2,11 @@ namespace TriangleNet.Examples { using System; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Text; - using TriangleNet.Smoothing; - using TriangleNet.Tools; + using Geometry; + using Meshing; + using Rendering.Text; + using Smoothing; + using Tools; /// /// Triangulate a polygon with hole with maximum area constraint, followed by mesh smoothing. @@ -39,10 +39,10 @@ public static bool Run(bool print = false) } // The boundary segment size of the input geometry. - const double h = 0.2; + private const double h = 0.2; // Parameter to relax the maximum area constraint. - const double relax = 1.45; + private const double relax = 1.45; public static IMesh CreateMesh() { diff --git a/src/Triangle.Examples/Examples/Example5.cs b/src/Triangle.Examples/Examples/Example5.cs index 3bb2a8c..591cd7e 100644 --- a/src/Triangle.Examples/Examples/Example5.cs +++ b/src/Triangle.Examples/Examples/Example5.cs @@ -2,10 +2,10 @@ namespace TriangleNet.Examples { using TriangleNet; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Text; - using TriangleNet.Smoothing; + using Geometry; + using Meshing; + using Rendering.Text; + using Smoothing; /// /// Refine only a part of a polygon mesh by using region pointers and an area constraint. diff --git a/src/Triangle.Examples/Examples/Example6.cs b/src/Triangle.Examples/Examples/Example6.cs index b800e30..803cf7b 100644 --- a/src/Triangle.Examples/Examples/Example6.cs +++ b/src/Triangle.Examples/Examples/Example6.cs @@ -3,10 +3,10 @@ namespace TriangleNet.Examples { using System.Collections.Generic; using TriangleNet; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Rendering.Text; + using Geometry; + using Meshing; + using Meshing.Iterators; + using Rendering.Text; /// /// Two ways finding boundary triangles. @@ -41,9 +41,9 @@ private static void FindBoundary1(IMesh mesh, bool neigbours = true) foreach (var s in mesh.Segments) { - int label = s.Label; + var label = s.Label; - for (int i = 0; i < 2; i++) + for (var i = 0; i < 2; i++) { var vertex = s.GetVertex(i); @@ -81,7 +81,7 @@ private static void FindBoundary2(IMesh mesh) foreach (var vertex in mesh.Vertices) { - int label = vertex.Label; + var label = vertex.Label; if (label > 0) { diff --git a/src/Triangle.Examples/Examples/Example7.cs b/src/Triangle.Examples/Examples/Example7.cs index 646bb57..fb01f36 100644 --- a/src/Triangle.Examples/Examples/Example7.cs +++ b/src/Triangle.Examples/Examples/Example7.cs @@ -2,11 +2,11 @@ { using System.Linq; using TriangleNet; - using TriangleNet.Geometry; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Rendering.Text; - using TriangleNet.Tools; - using TriangleNet.Topology; + using Geometry; + using Meshing.Iterators; + using Rendering.Text; + using Tools; + using Topology; /// /// Boolean operations on mesh regions (intersection, difference, xor). @@ -32,7 +32,7 @@ public static bool Run(bool print = false) // Find a seeding triangle (in this case, the point (2, 2) lies in // both rectangles). - var seed = (new TriangleQuadTree(mesh)).Query(2.0, 2.0) as Triangle; + var seed = (Triangle)new TriangleQuadTree(mesh).Query(2.0, 2.0); var iterator = new RegionIterator(mesh); diff --git a/src/Triangle.Examples/Examples/Example8.cs b/src/Triangle.Examples/Examples/Example8.cs index 4997696..a0fcf9d 100644 --- a/src/Triangle.Examples/Examples/Example8.cs +++ b/src/Triangle.Examples/Examples/Example8.cs @@ -2,17 +2,17 @@ namespace TriangleNet.Examples { using System; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Rendering.Text; + using Geometry; + using Meshing; + using Meshing.Iterators; + using Rendering.Text; /// /// Using a user test function to define a maximum edge length constraint. /// public static class Example8 { - const double MAX_EDGE_LENGTH = 0.2; + private const double MAX_EDGE_LENGTH = 0.2; public static bool Run(bool print = false) { @@ -33,7 +33,7 @@ public static bool Run(bool print = false) // Validate. foreach (var e in EdgeIterator.EnumerateEdges(mesh)) { - double length = Math.Sqrt(DistSqr(e.GetVertex(0), e.GetVertex(1))); + var length = Math.Sqrt(DistSqr(e.GetVertex(0), e.GetVertex(1))); if (length > MAX_EDGE_LENGTH) { @@ -46,7 +46,7 @@ public static bool Run(bool print = false) return true; } - static bool MaxEdgeLength(ITriangle tri, double area) + private static bool MaxEdgeLength(ITriangle tri, double area) { var p0 = tri.GetVertex(0); var p1 = tri.GetVertex(1); @@ -62,7 +62,7 @@ static bool MaxEdgeLength(ITriangle tri, double area) return s1 > maxlen || s2 > maxlen || s3 > maxlen; } - static double DistSqr(Vertex a, Vertex b) + private static double DistSqr(Vertex a, Vertex b) { var dx = a.X - b.X; var dy = a.Y - b.Y; diff --git a/src/Triangle.Examples/Examples/Example9.cs b/src/Triangle.Examples/Examples/Example9.cs index e2da3de..045c4a1 100644 --- a/src/Triangle.Examples/Examples/Example9.cs +++ b/src/Triangle.Examples/Examples/Example9.cs @@ -4,8 +4,8 @@ namespace TriangleNet.Examples using System; using System.Collections.Generic; using TriangleNet; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Tools; + using Meshing.Iterators; + using Tools; /// /// Compute the adjacency matrix of the mesh vertices. @@ -28,7 +28,7 @@ private static bool FindAdjacencyMatrix(Mesh mesh) var circulator = new VertexCirculator(mesh); - int k = 0; + var k = 0; foreach (var vertex in mesh.Vertices) { @@ -63,14 +63,14 @@ private static bool FindAdjacencyMatrix(Mesh mesh) private static bool CompareArray(int[] a, int[] b) { - int length = a.Length; + var length = a.Length; if (b.Length != length) { return false; } - for (int i = 0; i < length; i++) + for (var i = 0; i < length; i++) { if (a[i] != b[i]) { diff --git a/src/Triangle.Examples/Examples/ExamplePar.cs b/src/Triangle.Examples/Examples/ExamplePar.cs index f047642..dfe0587 100644 --- a/src/Triangle.Examples/Examples/ExamplePar.cs +++ b/src/Triangle.Examples/Examples/ExamplePar.cs @@ -7,10 +7,10 @@ namespace TriangleNet.Examples using System.Linq; using System.Threading.Tasks; using TriangleNet; - using TriangleNet.Geometry; - using TriangleNet.IO; - using TriangleNet.Meshing; - using TriangleNet.Meshing.Algorithm; + using Geometry; + using IO; + using Meshing; + using Meshing.Algorithm; /// /// Processing meshes in parallel. @@ -32,11 +32,11 @@ public static bool Run(int n = 1000) var queue = new ConcurrentQueue(sizes); - int concurrencyLevel = Environment.ProcessorCount / 2; + var concurrencyLevel = Environment.ProcessorCount / 2; var tasks = new Task[concurrencyLevel]; - for (int i = 0; i < concurrencyLevel; i++) + for (var i = 0; i < concurrencyLevel; i++) { tasks[i] = Task.Run(() => { @@ -59,7 +59,7 @@ public static bool Run(int n = 1000) while (queue.Count > 0) { - if (queue.TryDequeue(out int size)) + if (queue.TryDequeue(out var size)) { var points = Generate.RandomPoints(size, bounds); @@ -77,8 +77,8 @@ public static bool Run(int n = 1000) Task.WaitAll(tasks); - int numberOfTriangles = tasks.Sum(t => t.Result.NumberOfTriangles); - int invalid = tasks.Sum(t => t.Result.Invalid); + var numberOfTriangles = tasks.Sum(t => t.Result.NumberOfTriangles); + var invalid = tasks.Sum(t => t.Result.Invalid); Console.WriteLine("Total number of triangles processed: {0}", numberOfTriangles); @@ -103,11 +103,11 @@ public static bool Run(string dir) var queue = new ConcurrentQueue(files); - int concurrencyLevel = Environment.ProcessorCount / 2; + var concurrencyLevel = Environment.ProcessorCount / 2; var tasks = new Task[concurrencyLevel]; - for (int i = 0; i < concurrencyLevel; i++) + for (var i = 0; i < concurrencyLevel; i++) { tasks[i] = Task.Run(() => { @@ -146,8 +146,8 @@ public static bool Run(string dir) Task.WaitAll(tasks); - int numberOfTriangles = tasks.Sum(t => t.Result.NumberOfTriangles); - int invalid = tasks.Sum(t => t.Result.Invalid); + var numberOfTriangles = tasks.Sum(t => t.Result.NumberOfTriangles); + var invalid = tasks.Sum(t => t.Result.Invalid); Console.WriteLine("Total number of triangles processed: {0}", numberOfTriangles); @@ -169,7 +169,7 @@ private static void ProcessMesh(IMesh mesh, MeshResult result) } } - class MeshResult + private class MeshResult { public int NumberOfTriangles; public int Invalid; diff --git a/src/Triangle.Examples/Generate.cs b/src/Triangle.Examples/Generate.cs index 85bea7f..ba11781 100644 --- a/src/Triangle.Examples/Generate.cs +++ b/src/Triangle.Examples/Generate.cs @@ -3,9 +3,9 @@ namespace TriangleNet { using System; using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; - static class Generate + internal static class Generate { public static List RandomPoints(int n, Rectangle bounds) { @@ -19,10 +19,10 @@ public static List RandomPoints(int n, Rectangle bounds) var random = Random.Shared; - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { - double x = random.NextDouble(); - double y = random.NextDouble(); + var x = random.NextDouble(); + var y = random.NextDouble(); points.Add(new Vertex(xmin + x * width, ymin + y * height)); } @@ -51,11 +51,11 @@ public static Contour Rectangle(double x, double y, double width, double height, double size = 0d, int label = 0) { // Horizontal and vertical step sizes. - double stepH = 0d; - double stepV = 0d; + var stepH = 0d; + var stepV = 0d; - int nH = 1; - int nV = 1; + var nH = 1; + var nV = 1; if (size > 0d) { @@ -70,29 +70,29 @@ public static Contour Rectangle(double x, double y, double width, double height, var points = new List(2 * nH + 2 * nV); - double right = x + width; - double top = y + height; + var right = x + width; + var top = y + height; // Left box boundary points - for (int i = 0; i < nV; i++) + for (var i = 0; i < nV; i++) { points.Add(new Vertex(x, y + i * stepV, label)); } // Top box boundary points - for (int i = 0; i < nH; i++) + for (var i = 0; i < nH; i++) { points.Add(new Vertex(x + i * stepH, top, label)); } // Right box boundary points - for (int i = 0; i < nV; i++) + for (var i = 0; i < nV; i++) { points.Add(new Vertex(right, top - i * stepV, label)); } // Bottom box boundary points - for (int i = 0; i < nH; i++) + for (var i = 0; i < nH; i++) { points.Add(new Vertex(right - i * stepH, y, label)); } @@ -110,13 +110,13 @@ public static Contour Rectangle(double x, double y, double width, double height, /// A circular contour. public static Contour Circle(double r, Point center, double h, int label = 0) { - int n = (int)(2 * Math.PI * r / h); + var n = (int)(2 * Math.PI * r / h); var points = new List(n); double x, y, dphi = 2 * Math.PI / n; - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { x = center.X + r * Math.Cos(i * dphi); y = center.Y + r * Math.Sin(i * dphi); diff --git a/src/Triangle.Examples/Program.cs b/src/Triangle.Examples/Program.cs index 5822865..e81d367 100644 --- a/src/Triangle.Examples/Program.cs +++ b/src/Triangle.Examples/Program.cs @@ -3,13 +3,13 @@ namespace TriangleNet { using System; using System.Linq; - using TriangleNet.Examples; + using Examples; - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { - bool print = args.Contains("--print"); + var print = args.Contains("--print"); Check("Example 1", Example1.Run(print)); Check("Example 2", Example2.Run(print)); @@ -24,7 +24,7 @@ static void Main(string[] args) Check("Example 11", Example11.Run(print)); } - static void Check(string item, bool success) + private static void Check(string item, bool success) { var color = Console.ForegroundColor; diff --git a/src/Triangle.Examples/Triangle.Examples.csproj b/src/Triangle.Examples/Triangle.Examples.csproj index e5cf28f..6c8d3af 100644 --- a/src/Triangle.Examples/Triangle.Examples.csproj +++ b/src/Triangle.Examples/Triangle.Examples.csproj @@ -4,6 +4,8 @@ Exe net6.0 TriangleNet + default + enable diff --git a/src/Triangle.Rendering/Buffer/IndexBuffer.cs b/src/Triangle.Rendering/Buffer/IndexBuffer.cs index a9a2152..0013018 100644 --- a/src/Triangle.Rendering/Buffer/IndexBuffer.cs +++ b/src/Triangle.Rendering/Buffer/IndexBuffer.cs @@ -16,7 +16,7 @@ public static IBuffer Create(IEnumerable edges, int size) var data = buffer.Data; - int i = 0; + var i = 0; foreach (var e in edges) { @@ -35,7 +35,7 @@ public static IBuffer Create(ICollection elements, int size) var data = buffer.Data; - int i = 0; + var i = 0; foreach (var e in elements) { diff --git a/src/Triangle.Rendering/Buffer/VertexBuffer.cs b/src/Triangle.Rendering/Buffer/VertexBuffer.cs index b8b8815..2d68fe4 100644 --- a/src/Triangle.Rendering/Buffer/VertexBuffer.cs +++ b/src/Triangle.Rendering/Buffer/VertexBuffer.cs @@ -31,12 +31,12 @@ public static IBuffer Create(ICollection points, Rectangle bounds) var data = buffer.Data; - double dx = bounds.X; - double dy = bounds.Y; + var dx = bounds.X; + var dy = bounds.Y; - double scale = 1.0 / Math.Max(bounds.Width, bounds.Height); + var scale = 1.0 / Math.Max(bounds.Width, bounds.Height); - int i = 0; + var i = 0; double x, y; @@ -76,12 +76,12 @@ public static IBuffer Create(ICollection points, Rectangle bounds var data = buffer.Data; - double dx = bounds.X; - double dy = bounds.Y; + var dx = bounds.X; + var dy = bounds.Y; - double scale = 1.0 / Math.Max(bounds.Width, bounds.Height); + var scale = 1.0 / Math.Max(bounds.Width, bounds.Height); - int i = 0; + var i = 0; double x, y; diff --git a/src/Triangle.Rendering/ColorManager.cs b/src/Triangle.Rendering/ColorManager.cs index 14a5147..9f0e3cd 100644 --- a/src/Triangle.Rendering/ColorManager.cs +++ b/src/Triangle.Rendering/ColorManager.cs @@ -3,7 +3,7 @@ namespace TriangleNet.Rendering { using System.Collections.Generic; using System.Drawing; - using TriangleNet.Rendering.Util; + using Util; public class ColorManager { @@ -72,7 +72,7 @@ public Dictionary CreateColorDictionary(int length) { var keys = new int[length]; - for (int i = 0; i < length; i++) + for (var i = 0; i < length; i++) { keys[i] = i; } diff --git a/src/Triangle.Rendering/IRenderContext.cs b/src/Triangle.Rendering/IRenderContext.cs index c8c8b82..2d1299d 100644 --- a/src/Triangle.Rendering/IRenderContext.cs +++ b/src/Triangle.Rendering/IRenderContext.cs @@ -2,8 +2,8 @@ namespace TriangleNet.Rendering { using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; + using Geometry; + using Meshing; public interface IRenderContext { diff --git a/src/Triangle.Rendering/IRenderLayer.cs b/src/Triangle.Rendering/IRenderLayer.cs index b83e457..65f7a06 100644 --- a/src/Triangle.Rendering/IRenderLayer.cs +++ b/src/Triangle.Rendering/IRenderLayer.cs @@ -1,8 +1,8 @@  namespace TriangleNet.Rendering { - using TriangleNet.Rendering.Buffer; - using TriangleNet.Rendering.Util; + using Buffer; + using Util; using Color = System.Drawing.Color; diff --git a/src/Triangle.Rendering/Projection.cs b/src/Triangle.Rendering/Projection.cs index 6741b14..253a50a 100644 --- a/src/Triangle.Rendering/Projection.cs +++ b/src/Triangle.Rendering/Projection.cs @@ -30,16 +30,16 @@ namespace TriangleNet.Rendering public class Projection { // The original mesh bounds (needed for screen-to-world projection). - TRectangle world_; + private TRectangle world_; // Precomputed scaling factor for normalized coordinates. - double scale_; + private double scale_; // The screen dimensions. - Rectangle screen; + private Rectangle screen; // The original mesh and the viewport in normalized coordinates. - RectangleF world, viewport; + private RectangleF world, viewport; /// /// Gets or sets the current viewport (normalized coordinates). @@ -84,23 +84,23 @@ public void Initialize(TRectangle world) scale_ = Math.Max(world.Width, world.Height); // Dimensions in normalized coordinates. - float ww = (float)(world.Width / scale_); - float wh = (float)(world.Height / scale_); + var ww = (float)(world.Width / scale_); + var wh = (float)(world.Height / scale_); // Add a margin so there's some space around the screen borders. - float margin = (ww < wh) ? wh * 0.05f : ww * 0.05f; + var margin = (ww < wh) ? wh * 0.05f : ww * 0.05f; - int sw = screen.Width; - int sh = screen.Height; + var sw = screen.Width; + var sh = screen.Height; - float wRatio = ww / wh; - float sRatio = sw / (float)sh; + var wRatio = ww / wh; + var sRatio = sw / (float)sh; - float scale = (sRatio < wRatio) ? (ww + margin) / sw : (wh + margin) / sh; + var scale = (sRatio < wRatio) ? (ww + margin) / sw : (wh + margin) / sh; // Center in normalized coordinates (left = bottom = 0) - float centerX = ww / 2; - float centerY = wh / 2; + var centerX = ww / 2; + var centerY = wh / 2; // Get the initial viewport (complete mesh centered on the screen) this.world = viewport = new RectangleF( @@ -120,20 +120,20 @@ public void Resize(Rectangle newScreen) // the scaling and the center. // Get the screen scaling. - float scaleX = newScreen.Width / (float)screen.Width; - float scaleY = newScreen.Height / (float)screen.Height; + var scaleX = newScreen.Width / (float)screen.Width; + var scaleY = newScreen.Height / (float)screen.Height; screen = newScreen; var view = viewport; // Center of the viewport - float centerX = (view.Left + view.Right) / 2; - float centerY = (view.Bottom + view.Top) / 2; + var centerX = (view.Left + view.Right) / 2; + var centerY = (view.Bottom + view.Top) / 2; // The new viewport dimensions. - float width = view.Width * scaleX; - float height = view.Height * scaleY; + var width = view.Width * scaleX; + var height = view.Height * scaleY; viewport = new RectangleF( centerX - width / 2, @@ -162,8 +162,8 @@ public bool Translate(int dx, int dy) var view = viewport; - float x = view.X + dx * view.Width / 4; - float y = view.Y + dy * view.Height / 4; + var x = view.X + dx * view.Width / 4; + var y = view.Y + dy * view.Height / 4; viewport = new RectangleF(x, y, view.Width, view.Height); @@ -211,8 +211,8 @@ public bool Zoom(int amount, float focusX, float focusY) } // Current focus on viewport - float x = viewport.X + viewport.Width * focusX; - float y = viewport.Y + viewport.Height * focusY; + var x = viewport.X + viewport.Width * focusX; + var y = viewport.Y + viewport.Height * focusY; // New left and top positions x = x - width * focusX; diff --git a/src/Triangle.Rendering/RenderContext.cs b/src/Triangle.Rendering/RenderContext.cs index 703af06..a28faa7 100644 --- a/src/Triangle.Rendering/RenderContext.cs +++ b/src/Triangle.Rendering/RenderContext.cs @@ -3,19 +3,16 @@ namespace TriangleNet.Rendering { using System.Collections.Generic; using System.Linq; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Buffer; + using Geometry; + using Meshing; + using Buffer; /// /// The RenderContext class brings all the rendering parts together. /// public class RenderContext : IRenderContext { - private ColorManager colorManager; - private Projection zoom; private Rectangle bounds; - private IMesh mesh; private List renderLayers; @@ -35,21 +32,21 @@ public RenderContext(Projection zoom, ColorManager colorManager) RenderLayers[2].IsEnabled = true; RenderLayers[3].IsEnabled = true; - this.zoom = zoom; - this.colorManager = colorManager; + this.Zoom = zoom; + this.ColorManager = colorManager; } /// - public ColorManager ColorManager => colorManager; + public ColorManager ColorManager { get; } /// public IList RenderLayers => renderLayers; /// - public Projection Zoom => zoom; + public Projection Zoom { get; } /// - public IMesh Mesh => mesh; + public IMesh Mesh { get; private set; } /// public bool HasData => renderLayers.Any(layer => !layer.IsEmpty()); @@ -65,7 +62,7 @@ public void Add(IPolygon data) // Always clear Voronoi layer. RenderLayers[4].Reset(true); - int i = 0; + var i = 0; // Ensure linear numbering of polygon vertices. foreach (var p in data.Points) @@ -75,7 +72,7 @@ public void Add(IPolygon data) bounds = data.Bounds(); - zoom.Initialize(bounds); + Zoom.Initialize(bounds); RenderLayers[2].SetPoints(VertexBuffer.Create(data.Points, bounds)); RenderLayers[2].SetIndices(IndexBuffer.Create(data.Segments, 2)); @@ -95,13 +92,13 @@ public void Add(IMesh data, bool reset) RenderLayers[4].Reset(true); // Save reference to mesh. - mesh = data; + Mesh = data; bounds = data.Bounds; // Ensure linear numbering of vertices. - mesh.Renumber(); + Mesh.Renumber(); - zoom.Initialize(bounds); + Zoom.Initialize(bounds); RenderLayers[1].SetPoints(VertexBuffer.Create(data.Vertices, bounds)); RenderLayers[1].SetIndices(IndexBuffer.Create(data.Edges, 2)); @@ -125,8 +122,8 @@ public void Add(float[] data) { // Add function values for filled mesh. RenderLayers[0].SetPoints(RenderLayers[1].Points); - RenderLayers[0].SetIndices(IndexBuffer.Create(mesh.Triangles, 3)); - RenderLayers[0].AttachLayerData(data, colorManager.ColorMap); + RenderLayers[0].SetIndices(IndexBuffer.Create(Mesh.Triangles, 3)); + RenderLayers[0].AttachLayerData(data, ColorManager.ColorMap); RenderLayers[0].IsEnabled = true; } @@ -136,7 +133,7 @@ public void Add(int[] data) { // Add partition data for filled mesh. RenderLayers[0].SetPoints(RenderLayers[1].Points); - RenderLayers[0].SetIndices(IndexBuffer.Create(mesh.Triangles, 3)); + RenderLayers[0].SetIndices(IndexBuffer.Create(Mesh.Triangles, 3)); RenderLayers[0].AttachLayerData(data); RenderLayers[0].IsEnabled = true; diff --git a/src/Triangle.Rendering/RenderLayer.cs b/src/Triangle.Rendering/RenderLayer.cs index a590db6..75abb8d 100644 --- a/src/Triangle.Rendering/RenderLayer.cs +++ b/src/Triangle.Rendering/RenderLayer.cs @@ -2,17 +2,15 @@ namespace TriangleNet.Rendering { using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Buffer; - using TriangleNet.Rendering.Util; + using Geometry; + using Meshing; + using Buffer; + using Util; using Color = System.Drawing.Color; public class RenderLayer : IRenderLayer { - int count; - protected IBuffer points; protected IBuffer indices; @@ -21,11 +19,11 @@ public class RenderLayer : IRenderLayer public RenderLayer() { - this.IsEnabled = false; + IsEnabled = false; } /// - public int Count => count; + public int Count { get; private set; } /// public IBuffer Points => points; @@ -53,7 +51,7 @@ public void Reset(bool clear) { if (clear) { - count = 0; + Count = 0; points = null; } @@ -69,11 +67,11 @@ public void SetPoints(IBuffer buffer, bool reset = true) { // NOTE: we keep the old size to be able to render new Steiner // points in a different color than existing points. - count = points.Count / points.Size; + Count = points.Count / points.Size; } else { - count = buffer.Count / buffer.Size; + Count = buffer.Count / buffer.Size; } points = buffer; @@ -88,13 +86,13 @@ public void SetIndices(IBuffer buffer) /// public void AttachLayerData(float[] values, ColorMap colormap) { - int length = values.Length; + var length = values.Length; - double min = double.MaxValue; - double max = double.MinValue; + var min = double.MaxValue; + var max = double.MinValue; // Find min and max of given values. - for (int i = 0; i < length; i++) + for (var i = 0; i < length; i++) { if (values[i] < min) { @@ -109,7 +107,7 @@ public void AttachLayerData(float[] values, ColorMap colormap) var colorData = new Color[length]; - for (int i = 0; i < length; i++) + for (var i = 0; i < length; i++) { colorData[i] = colormap.GetColor(values[i], min, max); } diff --git a/src/Triangle.Rendering/RenderManager.cs b/src/Triangle.Rendering/RenderManager.cs index 86a07b6..ec92ce3 100644 --- a/src/Triangle.Rendering/RenderManager.cs +++ b/src/Triangle.Rendering/RenderManager.cs @@ -2,23 +2,21 @@ namespace TriangleNet.Rendering { using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Rendering.Util; + using Geometry; + using Meshing; + using Util; public class RenderManager { // TODO: delete public static bool VORONOI_DEBUG = false; - IRenderControl control; - IRenderContext context; - IRenderer renderer; - Projection zoom; + private IRenderer renderer; + private Projection zoom; - public IRenderControl Control => control; + public IRenderControl Control { get; private set; } - public IRenderContext Context => context; + public IRenderContext Context { get; private set; } public RenderManager() { @@ -31,16 +29,16 @@ public RenderManager(IRenderControl control, IRenderer renderer) public void Initialize(IRenderControl control, IRenderer renderer) { - this.zoom = new Projection(control.ClientRectangle); + zoom = new Projection(control.ClientRectangle); - this.context = new RenderContext(zoom, ColorManager.Default()); + Context = new RenderContext(zoom, ColorManager.Default()); this.renderer = renderer; - this.renderer.Context = context; + this.renderer.Context = Context; - this.control = control; - this.control.Initialize(); - this.control.Renderer = renderer; + this.Control = control; + this.Control.Initialize(); + this.Control.Renderer = renderer; } public bool TryCreateControl(string assemblyName, IEnumerable dependencies, @@ -51,39 +49,39 @@ public bool TryCreateControl(string assemblyName, IEnumerable dependenci public void Resize() { - control.HandleResize(); + Control.HandleResize(); } public void Clear() { - context.Clear(); - control.Refresh(); + Context.Clear(); + Control.Refresh(); } public void Enable(int layer, bool enabled) { - context.Enable(layer, enabled); + Context.Enable(layer, enabled); - control.Refresh(); + Control.Refresh(); } public void Set(IPolygon data, bool refresh = true) { - context.Add(data); + Context.Add(data); if (refresh) { - control.Refresh(); + Control.Refresh(); } } public void Set(IMesh data, bool reset, bool refresh = true) { - context.Add(data, reset); + Context.Add(data, reset); if (refresh) { - control.Refresh(); + Control.Refresh(); } } @@ -92,24 +90,24 @@ public void Set(IMesh data, bool reset, bool refresh = true) /// public void Set(ICollection points, IEnumerable edges, bool reset, bool refresh = true) { - context.Add(points, edges, reset); + Context.Add(points, edges, reset); if (refresh) { - control.Refresh(); + Control.Refresh(); } } public void Update(float[] values) { - context.Add(values); - control.Refresh(); + Context.Add(values); + Control.Refresh(); } public void Update(int[] partition) { - context.Add(partition); - control.Refresh(); + Context.Add(partition); + Control.Refresh(); } } } diff --git a/src/Triangle.Rendering/Text/EpsDocument.cs b/src/Triangle.Rendering/Text/EpsDocument.cs index 94bd5a8..6e54b83 100644 --- a/src/Triangle.Rendering/Text/EpsDocument.cs +++ b/src/Triangle.Rendering/Text/EpsDocument.cs @@ -41,19 +41,19 @@ public EpsDocument(Stream stream, PageSize pageSize) public void AddComment(string comment, int line = 1) { - for (int i = 0; i < line; i++) + for (var i = 0; i < line; i++) { _w.WriteLine("%"); } var t = comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries ); - for (int i = 0; i < t.Length; i++) + for (var i = 0; i < t.Length; i++) { _w.WriteLine("% " + t[i]); } - for (int i = 0; i < line; i++) + for (var i = 0; i < line; i++) { _w.WriteLine("%"); } @@ -170,7 +170,7 @@ private void Close() #region IDisposable implementation // Has Dispose already been called? - bool disposed = false; + private bool disposed; // Public implementation of Dispose pattern callable by consumers. public void Dispose() diff --git a/src/Triangle.Rendering/Text/EpsImage.cs b/src/Triangle.Rendering/Text/EpsImage.cs index 5de9176..97ee58f 100644 --- a/src/Triangle.Rendering/Text/EpsImage.cs +++ b/src/Triangle.Rendering/Text/EpsImage.cs @@ -10,8 +10,8 @@ namespace TriangleNet.Rendering.Text using System; using System.IO; using TriangleNet; - using TriangleNet.Geometry; - using TriangleNet.Meshing.Iterators; + using Geometry; + using Meshing.Iterators; using Color = System.Drawing.Color; using IntPoint = System.Drawing.Point; using IntRectangle = System.Drawing.Rectangle; @@ -22,12 +22,12 @@ namespace TriangleNet.Rendering.Text public class EpsImage { // EPS page metrics - PageSize ps = new PageSize(36, 126, 576, 666); - PageSize clip = new PageSize(18, 108, 594, 684); + private PageSize ps = new PageSize(36, 126, 576, 666); + private PageSize clip = new PageSize(18, 108, 594, 684); // Mesh metrics - double x_max, x_min; - double y_max, y_min; + private double x_max, x_min; + private double y_max, y_min; // TODO: use color manager private static Color ColorPoints = Color.FromArgb(0, 100, 0); @@ -56,48 +56,46 @@ public void Export(Mesh mesh, string filename, int width) UpdateMetrics(mesh.Bounds); - using (var eps = new EpsDocument(filename, ps)) - { - int n = mesh.Vertices.Count; + using var eps = new EpsDocument(filename, ps); + var n = mesh.Vertices.Count; - // Size of the points. - eps.DefaultPointSize = (n < 100) ? 3 : ((n < 500) ? 2 : 1); + // Size of the points. + eps.DefaultPointSize = (n < 100) ? 3 : ((n < 500) ? 2 : 1); - eps.WriteHeader(); + eps.WriteHeader(); - // Draw a gray border around the page. - eps.SetColor(ColorBorder); - eps.DrawRectangle(GetRectangle(ps)); + // Draw a gray border around the page. + eps.SetColor(ColorBorder); + eps.DrawRectangle(GetRectangle(ps)); - // Define a clipping polygon. - eps.SetClip(GetRectangle(clip)); + // Define a clipping polygon. + eps.SetClip(GetRectangle(clip)); - // Draw edges. - eps.AddComment("Draw edges."); - eps.SetStroke(0.4f, ColorLines); + // Draw edges. + eps.AddComment("Draw edges."); + eps.SetStroke(0.4f, ColorLines); - foreach (var e in EdgeIterator.EnumerateEdges(mesh)) - { - eps.DrawLine(Transform(e.GetVertex(0)), Transform(e.GetVertex(1))); - } + foreach (var e in EdgeIterator.EnumerateEdges(mesh)) + { + eps.DrawLine(Transform(e.GetVertex(0)), Transform(e.GetVertex(1))); + } - // Draw Segments. - eps.AddComment("Draw Segments."); - eps.SetStroke(0.8f, ColorSegments); + // Draw Segments. + eps.AddComment("Draw Segments."); + eps.SetStroke(0.8f, ColorSegments); - foreach (var s in mesh.Segments) - { - eps.DrawLine(Transform(s.GetVertex(0)), Transform(s.GetVertex(1))); - } + foreach (var s in mesh.Segments) + { + eps.DrawLine(Transform(s.GetVertex(0)), Transform(s.GetVertex(1))); + } - // Draw points. - eps.AddComment("Draw points."); - eps.SetColor(ColorPoints); + // Draw points. + eps.AddComment("Draw points."); + eps.SetColor(ColorPoints); - foreach (var node in mesh.Vertices) - { - eps.DrawPoint(Transform(node)); - } + foreach (var node in mesh.Vertices) + { + eps.DrawPoint(Transform(node)); } } @@ -127,27 +125,27 @@ private void UpdateMetrics(Rectangle bounds) y_min = bounds.Bottom; // Enlarge width 5% on each side - double x_scale = x_max - x_min; + var x_scale = x_max - x_min; x_max = x_max + 0.05 * x_scale; x_min = x_min - 0.05 * x_scale; x_scale = x_max - x_min; // Enlarge height 5% on each side - double y_scale = y_max - y_min; + var y_scale = y_max - y_min; y_max = y_max + 0.05 * y_scale; y_min = y_min - 0.05 * y_scale; y_scale = y_max - y_min; if (x_scale < y_scale) { - int delta = (int)Math.Round((ps.Right - ps.X) * (y_scale - x_scale) / (2.0 * y_scale)); + var delta = (int)Math.Round((ps.Right - ps.X) * (y_scale - x_scale) / (2.0 * y_scale)); ps.Expand(-delta, 0); clip.Expand(-delta, 0); } else { - int delta = (int)Math.Round((ps.Bottom - ps.Y) * (x_scale - y_scale) / (2.0 * x_scale)); + var delta = (int)Math.Round((ps.Bottom - ps.Y) * (x_scale - y_scale) / (2.0 * x_scale)); ps.Expand(0, -delta); clip.Expand(0, -delta); diff --git a/src/Triangle.Rendering/Text/FormattingStreamWriter.cs b/src/Triangle.Rendering/Text/FormattingStreamWriter.cs index e134a7c..f8904f4 100644 --- a/src/Triangle.Rendering/Text/FormattingStreamWriter.cs +++ b/src/Triangle.Rendering/Text/FormattingStreamWriter.cs @@ -13,8 +13,6 @@ namespace TriangleNet.Rendering.Text /// public class FormattingStreamWriter : StreamWriter { - private readonly IFormatProvider formatProvider; - /// /// Initializes a new instance of the StreamWriter class for the specified file /// by using the default encoding and buffer size. @@ -44,7 +42,7 @@ public FormattingStreamWriter(Stream stream) public FormattingStreamWriter(string path, IFormatProvider formatProvider) : base(path) { - this.formatProvider = formatProvider; + this.FormatProvider = formatProvider; } /// @@ -56,12 +54,12 @@ public FormattingStreamWriter(string path, IFormatProvider formatProvider) public FormattingStreamWriter(Stream stream, IFormatProvider formatProvider) : base(stream) { - this.formatProvider = formatProvider; + this.FormatProvider = formatProvider; } /// /// Gets an object that controls formatting. /// - public override IFormatProvider FormatProvider => formatProvider; + public override IFormatProvider FormatProvider { get; } } } diff --git a/src/Triangle.Rendering/Text/PageSize.cs b/src/Triangle.Rendering/Text/PageSize.cs index 594235b..7d0fda1 100644 --- a/src/Triangle.Rendering/Text/PageSize.cs +++ b/src/Triangle.Rendering/Text/PageSize.cs @@ -16,47 +16,24 @@ public struct PageSize public static readonly PageSize LETTER = new PageSize(8.5f * MM_PER_INCH, 11.0f * MM_PER_INCH); public static readonly PageSize LEGAL = new PageSize(8.5f * MM_PER_INCH, 14.0f * MM_PER_INCH); - private float left; - private float top; - private float right; - private float bottom; + public float X { get; private set; } - public float X - { - get { return left; } - } + public float Y { get; private set; } - public float Y - { - get { return top; } - } + public float Width => Right - X; - public float Width - { - get { return right - left; } - } + public float Height => Bottom - Y; - public float Height - { - get { return bottom - top; } - } - - public float Right - { - get { return right; } - } + public float Right { get; private set; } - public float Bottom - { - get { return bottom; } - } + public float Bottom { get; private set; } public PageSize(float left, float top, float right, float bottom) { - this.left = left; - this.top = top; - this.right = right; - this.bottom = bottom; + this.X = left; + this.Y = top; + this.Right = right; + this.Bottom = bottom; } public PageSize(float width, float height) @@ -71,11 +48,11 @@ public PageSize(Rectangle size) public void Expand(float dx, float dy) { - left -= dx; - top -= dy; + X -= dx; + Y -= dy; - right += dx; - bottom += dy; + Right += dx; + Bottom += dy; } } } diff --git a/src/Triangle.Rendering/Text/SvgImage.cs b/src/Triangle.Rendering/Text/SvgImage.cs index 67ae375..030730e 100644 --- a/src/Triangle.Rendering/Text/SvgImage.cs +++ b/src/Triangle.Rendering/Text/SvgImage.cs @@ -10,9 +10,9 @@ namespace TriangleNet.Rendering.Text using System.Collections.Generic; using System.IO; using System.Text; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Meshing.Iterators; + using Geometry; + using Meshing; + using Meshing.Iterators; /// /// Writes a mesh to an SVG file. @@ -22,7 +22,7 @@ public class SvgImage // Iterations to insert a line break in SVG path. private const int LINEBREAK_COUNT = 10; - float scale = 1f; + private float scale = 1f; /// /// Exports a mesh to SVG format. @@ -67,37 +67,35 @@ public void Export(IMesh mesh, string filename, int width, var bounds = mesh.Bounds; - float margin = 0.05f * (float)bounds.Width; + var margin = 0.05f * (float)bounds.Width; scale = width / ((float)bounds.Width + 2 * margin); - int x_offset = -(int)((bounds.Left - margin) * scale - 0.5); - int y_offset = (int)((bounds.Top + margin) * scale + 0.5); + var x_offset = -(int)((bounds.Left - margin) * scale - 0.5); + var y_offset = (int)((bounds.Top + margin) * scale + 0.5); - int height = (int)((bounds.Height + 2 * margin) * scale + 0.5); + var height = (int)((bounds.Height + 2 * margin) * scale + 0.5); - using (var svg = new FormattingStreamWriter(filename)) - { - svg.WriteLine("", width, height); + using var svg = new FormattingStreamWriter(filename); + svg.WriteLine("", width, height); - svg.WriteLine("", x_offset, y_offset); + svg.WriteLine("", x_offset, y_offset); - DrawTriangles(svg, mesh, regions, false); - //DrawEdges(svg, mesh); + DrawTriangles(svg, mesh, regions, false); + //DrawEdges(svg, mesh); - DrawSegments(svg, mesh); + DrawSegments(svg, mesh); - if (points) - { - DrawPoints(svg, mesh, false); - } + if (points) + { + DrawPoints(svg, mesh, false); + } - svg.WriteLine(""); + svg.WriteLine(""); - svg.WriteLine(""); - } + svg.WriteLine(""); } private void DrawTriangles(StreamWriter svg, IMesh mesh, bool regions, bool label) @@ -110,7 +108,7 @@ private void DrawTriangles(StreamWriter svg, IMesh mesh, bool regions, bool labe Vertex v1, v2, v3; double x1, y1, x2, y2, x3, y3, xa, ya; - int i = 1; + var i = 1; edges.Append("\tTriangleNet.Rendering Triangle.Rendering AnyCPU;x64 + default + enable diff --git a/src/Triangle.Rendering/Util/ColorMap.cs b/src/Triangle.Rendering/Util/ColorMap.cs index 37adde3..0223470 100644 --- a/src/Triangle.Rendering/Util/ColorMap.cs +++ b/src/Triangle.Rendering/Util/ColorMap.cs @@ -10,11 +10,11 @@ public class ColorMap public static ColorMap Jet(int size) { - ColorMap map = new ColorMap(size); + var map = new ColorMap(size); float v, step = 1.0f / (size - 1); - float[] rgb = new float[3]; + var rgb = new float[3]; - for (int i = 0; i < size; i += 1) + for (var i = 0; i < size; i += 1) { v = 4 * i * step; @@ -32,11 +32,11 @@ public static ColorMap Jet(int size) public static ColorMap Hot(int size) { - ColorMap map = new ColorMap(size); + var map = new ColorMap(size); float v, step = 1.0f / (size - 1); - float[] rgb = new float[3]; + var rgb = new float[3]; - for (int i = 0; i < size; i += 1) + for (var i = 0; i < size; i += 1) { v = 2.5f * i * step; @@ -58,16 +58,16 @@ public static ColorMap Hot(int size) private static Color ColorFromRgb(float r, float g, float b) { - byte max = byte.MaxValue; + var max = byte.MaxValue; return Color.FromArgb((byte)(r * max), (byte)(g * max), (byte)(b * max)); } private static void Clamp(float[] values, float min, float max) { - int n = values.Length; + var n = values.Length; - for (int i = 0; i < n; i += 1) + for (var i = 0; i < n; i += 1) { values[i] = Math.Min(max, Math.Max(min, values[i])); } @@ -93,7 +93,7 @@ private static int Clamp(int index, int max) private ColorMap(int size) { - this.colors = new Color[size]; + colors = new Color[size]; } public ColorMap(Color[] colors) @@ -103,10 +103,10 @@ public ColorMap(Color[] colors) public Color GetColor(double value, double min, double max) { - int n = this.colors.Length; - int i = (int)Math.Floor(n * (max - value) / (max - min)); + var n = colors.Length; + var i = (int)Math.Floor(n * (max - value) / (max - min)); - return this.colors[Clamp(i, n - 1)]; + return colors[Clamp(i, n - 1)]; } } } diff --git a/src/Triangle.Tests/Geomerty/ContourTest.cs b/src/Triangle.Tests/Geomerty/ContourTest.cs index 087c23f..aefc055 100644 --- a/src/Triangle.Tests/Geomerty/ContourTest.cs +++ b/src/Triangle.Tests/Geomerty/ContourTest.cs @@ -51,12 +51,12 @@ public void TestFindInteriorPointL() var h = poly.Holes[0]; var p = RobustPredicates.Default; - int count = points.Count; - int i = count - 1; + var count = points.Count; + var i = count - 1; - for (int j = 0; j < count; j++) + for (var j = 0; j < count; j++) { - double ccw = p.CounterClockwise(points[i], h, points[j]); + var ccw = p.CounterClockwise(points[i], h, points[j]); Assert.Greater(Math.Abs(ccw), 1e-12); diff --git a/src/Triangle.Tests/Geomerty/PolygonTest.cs b/src/Triangle.Tests/Geomerty/PolygonTest.cs index 3d98a5c..aa33c38 100644 --- a/src/Triangle.Tests/Geomerty/PolygonTest.cs +++ b/src/Triangle.Tests/Geomerty/PolygonTest.cs @@ -6,7 +6,7 @@ namespace TriangleNet.Tests.Geometry public class PolygonTest { // The vertices that define the polygon contour (triangle shape). - Vertex[] vertices = new Vertex[] + private Vertex[] vertices = new Vertex[] { new Vertex(0d, 0d), new Vertex(2d ,0d), diff --git a/src/Triangle.Tests/Helper.cs b/src/Triangle.Tests/Helper.cs index 23c675f..072034c 100644 --- a/src/Triangle.Tests/Helper.cs +++ b/src/Triangle.Tests/Helper.cs @@ -3,9 +3,9 @@ namespace TriangleNet.Tests { using System.Collections.Generic; using TriangleNet.Geometry; - using TriangleNet.Topology; + using Topology; - static class Helper + internal static class Helper { public static Contour Rectangle(double left, double top, double right, double bottom, int mark = 0) diff --git a/src/Triangle.Tests/Tools/InterpolationTest.cs b/src/Triangle.Tests/Tools/InterpolationTest.cs index dbd6a76..9c710a9 100644 --- a/src/Triangle.Tests/Tools/InterpolationTest.cs +++ b/src/Triangle.Tests/Tools/InterpolationTest.cs @@ -30,7 +30,7 @@ public void TestInterpolatePoint() double actual, expected; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { actual = Interpolation.InterpolatePoint(tri, vertices[i], values); expected = values[i]; @@ -42,7 +42,7 @@ public void TestInterpolatePoint() double x, y; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { x = (vertices[i].X + vertices[(i + 1) % 3].X) / 2; y = (vertices[i].Y + vertices[(i + 1) % 3].Y) / 2; diff --git a/src/Triangle.Tests/Tools/IntersectionHelperTest.cs b/src/Triangle.Tests/Tools/IntersectionHelperTest.cs index 158e405..7ae6136 100644 --- a/src/Triangle.Tests/Tools/IntersectionHelperTest.cs +++ b/src/Triangle.Tests/Tools/IntersectionHelperTest.cs @@ -9,8 +9,8 @@ public class IntersectionHelperTest { private const double EPS = 1e-8; - Rectangle box; - Point p; + private Rectangle box; + private Point p; [SetUp] public void Initialize() @@ -162,7 +162,7 @@ public void TestIsPointOnSegment() // Test point not on segment. Assert.IsFalse(IntersectionHelper.IsPointOnSegment(a, b, new Vertex(1.5, 0.5))); - double eps = 1e-12; + var eps = 1e-12; // Test point collinear near endpoint, but not on segment. Assert.IsFalse(IntersectionHelper.IsPointOnSegment(a, b, new Vertex(2.0 + eps, 2.0 + eps))); diff --git a/src/Triangle.Tests/Tools/QualityMeasureTest.cs b/src/Triangle.Tests/Tools/QualityMeasureTest.cs index 46f1ed0..7b8b5f8 100644 --- a/src/Triangle.Tests/Tools/QualityMeasureTest.cs +++ b/src/Triangle.Tests/Tools/QualityMeasureTest.cs @@ -12,7 +12,7 @@ public class QualityMeasureTest [Test, DefaultFloatingPointTolerance(1e-12)] public void TestEquilateralTriangles() { - double sqrt3 = Math.Sqrt(3); + var sqrt3 = Math.Sqrt(3); var vertices = new List() { diff --git a/src/Triangle.Tests/Tools/StatisticTest.cs b/src/Triangle.Tests/Tools/StatisticTest.cs index d381e33..299cedf 100644 --- a/src/Triangle.Tests/Tools/StatisticTest.cs +++ b/src/Triangle.Tests/Tools/StatisticTest.cs @@ -48,10 +48,10 @@ public void TestComputeAngles() Statistic.ComputeAngles(triangle, data); - bool acute = data[2] > 0; + var acute = data[2] > 0; - double min = Math.Acos(Math.Sqrt(data[0])); - double max = Math.Acos(Math.Sqrt(data[1])); + var min = Math.Acos(Math.Sqrt(data[0])); + var max = Math.Acos(Math.Sqrt(data[1])); const double deg = 180.0 / Math.PI; diff --git a/src/Triangle.Tests/Topology/OtriTest.cs b/src/Triangle.Tests/Topology/OtriTest.cs index 6719bfa..bc8bff4 100644 --- a/src/Triangle.Tests/Topology/OtriTest.cs +++ b/src/Triangle.Tests/Topology/OtriTest.cs @@ -3,7 +3,7 @@ namespace TriangleNet.Tests { using NUnit.Framework; using TriangleNet.Geometry; - using TriangleNet.Topology; + using Topology; // The Otri (orientent triangle) struct is the heart of Triangle's mesh // datastructure. It basically represents one of the three edges of the @@ -27,7 +27,7 @@ namespace TriangleNet.Tests public class OtriTest { // The vertices of the mesh. - Vertex[] vertices; + private Vertex[] vertices; private Triangle[] CreateExampleMesh() { diff --git a/src/Triangle.Tests/Triangle.Tests.csproj b/src/Triangle.Tests/Triangle.Tests.csproj index 0e1d50f..50ca2d3 100644 --- a/src/Triangle.Tests/Triangle.Tests.csproj +++ b/src/Triangle.Tests/Triangle.Tests.csproj @@ -5,6 +5,8 @@ TriangleNet.Tests Triangle.Tests false + default + enable diff --git a/src/Triangle.Tests/TrianglePoolTest.cs b/src/Triangle.Tests/TrianglePoolTest.cs index d2cf6aa..f02e5fe 100644 --- a/src/Triangle.Tests/TrianglePoolTest.cs +++ b/src/Triangle.Tests/TrianglePoolTest.cs @@ -3,7 +3,7 @@ namespace TriangleNet.Tests { - class TrianglePoolTest + internal class TrianglePoolTest { [Test] public void TestGetRelease() @@ -112,9 +112,9 @@ public void TestRestart() Assert.AreEqual(0, pool.Count); Assert.AreEqual(0, pool.Capacity); - int n = 10; + var n = 10; - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { Assert.AreEqual(i, pool.Get().ID); } diff --git a/src/Triangle/Behavior.cs b/src/Triangle/Behavior.cs index 5226774..0997b20 100644 --- a/src/Triangle/Behavior.cs +++ b/src/Triangle/Behavior.cs @@ -8,37 +8,27 @@ namespace TriangleNet { using System; - using TriangleNet.Geometry; + using Geometry; /// /// Controls the behavior of the meshing software. /// - class Behavior + internal class Behavior { - bool poly = false; - bool quality = false; - bool varArea = false; - bool convex = false; - bool jettison = false; - bool boundaryMarkers = true; - bool noHoles = false; - bool conformDel = false; + private bool quality; - Func usertest; - Func exclude; + private int noBisect; - int noBisect = 0; + private double minAngle; + private double maxAngle; + private double maxArea = -1.0; - double minAngle = 0.0; - double maxAngle = 0.0; - double maxArea = -1.0; - - internal bool fixedArea = false; + internal bool fixedArea; internal bool useSegments = true; internal bool useRegions = false; - internal double goodAngle = 0.0; - internal double maxGoodAngle = 0.0; - internal double offconstant = 0.0; + internal double goodAngle; + internal double maxGoodAngle; + internal double offconstant; /// /// Creates an instance of the Behavior class. @@ -59,38 +49,38 @@ public Behavior(bool quality = false, double minAngle = 20.0) /// private void Update() { - this.quality = true; + quality = true; - if (this.minAngle < 0 || this.minAngle > 60) + if (minAngle < 0 || minAngle > 60) { - this.minAngle = 0; - this.quality = false; + minAngle = 0; + quality = false; Log.Instance.Warning("Invalid quality option (minimum angle).", "Mesh.Behavior"); } - if ((this.maxAngle != 0.0) && (this.maxAngle < 60 || this.maxAngle > 180)) + if ((maxAngle != 0.0) && (maxAngle < 60 || maxAngle > 180)) { - this.maxAngle = 0; - this.quality = false; + maxAngle = 0; + quality = false; Log.Instance.Warning("Invalid quality option (maximum angle).", "Mesh.Behavior"); } - this.useSegments = this.Poly || this.Quality || this.Convex; - this.goodAngle = Math.Cos(this.MinAngle * Math.PI / 180.0); - this.maxGoodAngle = Math.Cos(this.MaxAngle * Math.PI / 180.0); + useSegments = Poly || Quality || Convex; + goodAngle = Math.Cos(MinAngle * Math.PI / 180.0); + maxGoodAngle = Math.Cos(MaxAngle * Math.PI / 180.0); - if (this.goodAngle == 1.0) + if (goodAngle == 1.0) { - this.offconstant = 0.0; + offconstant = 0.0; } else { - this.offconstant = 0.475 * Math.Sqrt((1.0 + this.goodAngle) / (1.0 - this.goodAngle)); + offconstant = 0.475 * Math.Sqrt((1.0 + goodAngle) / (1.0 - goodAngle)); } - this.goodAngle *= this.goodAngle; + goodAngle *= goodAngle; } #region Static properties @@ -109,7 +99,7 @@ private void Update() /// public bool Quality { - get { return quality; } + get => quality; set { quality = value; @@ -125,7 +115,7 @@ public bool Quality /// public double MinAngle { - get { return minAngle; } + get => minAngle; set { minAngle = value; Update(); } } @@ -134,7 +124,7 @@ public double MinAngle /// public double MaxAngle { - get { return maxAngle; } + get => maxAngle; set { maxAngle = value; Update(); } } @@ -143,7 +133,7 @@ public double MaxAngle /// public double MaxArea { - get { return maxArea; } + get => maxArea; set { maxArea = value; @@ -154,56 +144,32 @@ public double MaxArea /// /// Apply a maximum triangle area constraint. /// - public bool VarArea - { - get { return varArea; } - set { varArea = value; } - } + public bool VarArea { get; set; } = false; /// /// Input is a Planar Straight Line Graph. /// - public bool Poly - { - get { return poly; } - set { poly = value; } - } + public bool Poly { get; set; } /// /// Apply a user-defined triangle constraint. /// - public Func UserTest - { - get { return usertest; } - set { usertest = value; } - } + public Func? UserTest { get; set; } /// /// Exclude triangles from being refined. /// - public Func Exclude - { - get { return exclude; } - set { exclude = value; } - } + public Func? Exclude { get; set; } /// /// Enclose the convex hull with segments. /// - public bool Convex - { - get { return convex; } - set { convex = value; } - } + public bool Convex { get; set; } = false; /// /// Conforming Delaunay (all triangles are truly Delaunay). /// - public bool ConformingDelaunay - { - get { return conformDel; } - set { conformDel = value; } - } + public bool ConformingDelaunay { get; set; } = false; /// /// Suppresses boundary segment splitting. @@ -215,7 +181,7 @@ public bool ConformingDelaunay /// public int NoBisect { - get { return noBisect; } + get => noBisect; set { noBisect = value; @@ -229,29 +195,17 @@ public int NoBisect /// /// Compute boundary information. /// - public bool UseBoundaryMarkers - { - get { return boundaryMarkers; } - set { boundaryMarkers = value; } - } + public bool UseBoundaryMarkers { get; set; } = true; /// /// Ignores holes in polygons. /// - public bool NoHoles - { - get { return noHoles; } - set { noHoles = value; } - } + public bool NoHoles { get; set; } = false; /// /// Jettison unused vertices from output. /// - public bool Jettison - { - get { return jettison; } - set { jettison = value; } - } + public bool Jettison { get; set; } = false; #endregion } diff --git a/src/Triangle/Enums.cs b/src/Triangle/Enums.cs deleted file mode 100644 index aa5d5fb..0000000 --- a/src/Triangle/Enums.cs +++ /dev/null @@ -1,46 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Triangle Copyright (c) 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk -// Triangle.NET code by Christian Woltering -// -// ----------------------------------------------------------------------- - -namespace TriangleNet -{ - /// - /// The type of the mesh vertex. - /// - public enum VertexType { InputVertex, SegmentVertex, FreeVertex, DeadVertex, UndeadVertex }; - - /// - /// Node renumbering algorithms. - /// - public enum NodeNumbering { None, Linear, CuthillMcKee }; - - /// - /// Labels that signify the result of point location. - /// - /// The result of a search indicates that the point falls in the - /// interior of a triangle, on an edge, on a vertex, or outside the mesh. - /// - public enum LocateResult { InTriangle, OnEdge, OnVertex, Outside }; - - /// - /// Labels that signify the result of vertex insertion. - /// - /// The result indicates that the vertex was inserted with complete - /// success, was inserted but encroaches upon a subsegment, was not inserted - /// because it lies on a segment, or was not inserted because another vertex - /// occupies the same location. - /// - enum InsertVertexResult { Successful, Encroaching, Violating, Duplicate }; - - /// - /// Labels that signify the result of direction finding. - /// - /// The result indicates that a segment connecting the two query - /// points falls within the direction triangle, along the left edge of the - /// direction triangle, or along the right edge of the direction triangle. - /// - enum FindDirectionResult { Within, Leftcollinear, Rightcollinear }; -} diff --git a/src/Triangle/Enums/FindDirectionResult.cs b/src/Triangle/Enums/FindDirectionResult.cs new file mode 100644 index 0000000..e3b22fa --- /dev/null +++ b/src/Triangle/Enums/FindDirectionResult.cs @@ -0,0 +1,11 @@ +namespace TriangleNet +{ + /// + /// Labels that signify the result of direction finding. + /// + /// The result indicates that a segment connecting the two query + /// points falls within the direction triangle, along the left edge of the + /// direction triangle, or along the right edge of the direction triangle. + /// + internal enum FindDirectionResult { Within, Leftcollinear, Rightcollinear }; +} \ No newline at end of file diff --git a/src/Triangle/Enums/InsertVertexResult.cs b/src/Triangle/Enums/InsertVertexResult.cs new file mode 100644 index 0000000..ee36a15 --- /dev/null +++ b/src/Triangle/Enums/InsertVertexResult.cs @@ -0,0 +1,12 @@ +namespace TriangleNet +{ + /// + /// Labels that signify the result of vertex insertion. + /// + /// The result indicates that the vertex was inserted with complete + /// success, was inserted but encroaches upon a subsegment, was not inserted + /// because it lies on a segment, or was not inserted because another vertex + /// occupies the same location. + /// + internal enum InsertVertexResult { Successful, Encroaching, Violating, Duplicate }; +} \ No newline at end of file diff --git a/src/Triangle/Enums/LocateResult.cs b/src/Triangle/Enums/LocateResult.cs new file mode 100644 index 0000000..1ef6911 --- /dev/null +++ b/src/Triangle/Enums/LocateResult.cs @@ -0,0 +1,10 @@ +namespace TriangleNet +{ + /// + /// Labels that signify the result of point location. + /// + /// The result of a search indicates that the point falls in the + /// interior of a triangle, on an edge, on a vertex, or outside the mesh. + /// + public enum LocateResult { InTriangle, OnEdge, OnVertex, Outside }; +} \ No newline at end of file diff --git a/src/Triangle/Enums/NodeNumbering.cs b/src/Triangle/Enums/NodeNumbering.cs new file mode 100644 index 0000000..9268619 --- /dev/null +++ b/src/Triangle/Enums/NodeNumbering.cs @@ -0,0 +1,7 @@ +namespace TriangleNet +{ + /// + /// Node renumbering algorithms. + /// + public enum NodeNumbering { None, Linear, CuthillMcKee }; +} \ No newline at end of file diff --git a/src/Triangle/Enums/VertexType.cs b/src/Triangle/Enums/VertexType.cs new file mode 100644 index 0000000..f2c36cb --- /dev/null +++ b/src/Triangle/Enums/VertexType.cs @@ -0,0 +1,14 @@ +// ----------------------------------------------------------------------- +// +// Triangle Copyright (c) 1993, 1995, 1997, 1998, 2002, 2005 Jonathan Richard Shewchuk +// Triangle.NET code by Christian Woltering +// +// ----------------------------------------------------------------------- + +namespace TriangleNet +{ + /// + /// The type of the mesh vertex. + /// + public enum VertexType { InputVertex, SegmentVertex, FreeVertex, DeadVertex, UndeadVertex }; +} diff --git a/src/Triangle/Geometry/Contour.cs b/src/Triangle/Geometry/Contour.cs index ec9d54c..dd9d651 100644 --- a/src/Triangle/Geometry/Contour.cs +++ b/src/Triangle/Geometry/Contour.cs @@ -8,21 +8,21 @@ namespace TriangleNet.Geometry { using System; using System.Collections.Generic; - using TriangleNet.Tools; + using Tools; /// /// Represents a contour of a polygon (outer boundary or internal holes). /// public class Contour { - int marker; + private int marker; - bool convex; + private bool convex; /// /// Gets or sets the list of points making up the contour. /// - public List Points { get; set; } + public List Points { get; } = new(); /// /// Initializes a new instance of the class. @@ -67,9 +67,9 @@ public List GetSegments() var p = Points; - int count = p.Count - 1; + var count = p.Count - 1; - for (int i = 0; i < count; i++) + for (var i = 0; i < count; i++) { // Add segments to polygon. segments.Add(new Segment(p[i], p[i + 1], marker)); @@ -102,11 +102,11 @@ public Point FindInteriorPoint(int limit = 5, double eps = 2e-5) { var p = Points; - int count = p.Count; + var count = p.Count; var point = new Point(0.0, 0.0); - for (int i = 0; i < count; i++) + for (var i = 0; i < count; i++) { point.x += p[i].x; point.y += p[i].y; @@ -119,14 +119,14 @@ public Point FindInteriorPoint(int limit = 5, double eps = 2e-5) return point; } - return FindPointInPolygon(this.Points, limit, eps); + return FindPointInPolygon(Points, limit, eps); } private void AddPoints(IEnumerable points) { - Points = new List(points); + Points.AddRange(points); - int count = Points.Count - 1; + var count = Points.Count - 1; // Check if first vertex equals last vertex. if (Points[0] == Points[count]) @@ -142,7 +142,7 @@ private static Point FindPointInPolygon(List contour, int limit, double var bounds = new Rectangle(); bounds.Expand(contour); - int length = contour.Count; + var length = contour.Count; var test = new Point(); @@ -157,7 +157,7 @@ private static Point FindPointInPolygon(List contour, int limit, double a = contour[0]; b = contour[1]; - for (int i = 0; i < length; i++) + for (var i = 0; i < length; i++) { c = contour[(i + 2) % length]; @@ -189,7 +189,7 @@ private static Point FindPointInPolygon(List contour, int limit, double h = 1.0; - for (int j = 0; j < limit; j++) + for (var j = 0; j < limit; j++) { // Search in direction. test.x = bx + dx * h; @@ -230,12 +230,12 @@ private static Point FindPointInPolygon(List contour, int limit, double /// private static bool IsPointInPolygon(Point point, List poly) { - bool inside = false; + var inside = false; - double x = point.x; - double y = point.y; + var x = point.x; + var y = point.y; - int count = poly.Count; + var count = poly.Count; for (int i = 0, j = count - 1; i < count; i++) { @@ -257,11 +257,11 @@ private static bool IsPointInPolygon(Point point, List poly) /// private static bool IsPointOnSegment(Point test, List contour, double eps = 1e-12) { - int count = contour.Count; + var count = contour.Count; - int i = count - 1; + var i = count - 1; - for (int j = 0; j < count; j++) + for (var j = 0; j < count; j++) { if (IntersectionHelper.IsPointOnSegment(contour[i], contour[j], test, eps)) { diff --git a/src/Triangle/Geometry/ExtensionMethods.cs b/src/Triangle/Geometry/ExtensionMethods.cs index fae7666..775ba02 100644 --- a/src/Triangle/Geometry/ExtensionMethods.cs +++ b/src/Triangle/Geometry/ExtensionMethods.cs @@ -2,7 +2,7 @@ namespace TriangleNet.Geometry { using System; - using TriangleNet.Meshing; + using Meshing; /// /// Extension methods. @@ -96,15 +96,15 @@ public static bool Contains(this ITriangle triangle, double x, double y) var t2 = triangle.GetVertex(2); // TODO: no need to create new Point instances here - Point d0 = new Point(t1.X - t0.X, t1.Y - t0.Y); - Point d1 = new Point(t2.X - t0.X, t2.Y - t0.Y); - Point d2 = new Point(x - t0.X, y - t0.Y); + var d0 = new Point(t1.X - t0.X, t1.Y - t0.Y); + var d1 = new Point(t2.X - t0.X, t2.Y - t0.Y); + var d2 = new Point(x - t0.X, y - t0.Y); // crossproduct of (0, 0, 1) and d0 - Point c0 = new Point(-d0.Y, d0.X); + var c0 = new Point(-d0.Y, d0.X); // crossproduct of (0, 0, 1) and d1 - Point c1 = new Point(-d1.Y, d1.X); + var c1 = new Point(-d1.Y, d1.X); // Linear combination d2 = s * d0 + v * d1. // @@ -114,8 +114,8 @@ public static bool Contains(this ITriangle triangle, double x, double y) // s = d2 * c1 / d0 * c1 // v = d2 * c0 / d1 * c0 - double s = DotProduct(d2, c1) / DotProduct(d0, c1); - double v = DotProduct(d2, c0) / DotProduct(d1, c0); + var s = DotProduct(d2, c1) / DotProduct(d0, c1); + var v = DotProduct(d2, c0) / DotProduct(d1, c0); if (s >= 0 && v >= 0 && ((s + v) <= 1)) { @@ -135,7 +135,7 @@ public static Rectangle Bounds(this ITriangle triangle) { var bounds = new Rectangle(); - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { bounds.Expand(triangle.GetVertex(i)); } diff --git a/src/Triangle/Geometry/Point.cs b/src/Triangle/Geometry/Point.cs index beec352..8c44b2e 100644 --- a/src/Triangle/Geometry/Point.cs +++ b/src/Triangle/Geometry/Point.cs @@ -32,17 +32,7 @@ public class Point : IComparable, IEquatable /// Initializes a new instance of the class. /// public Point() - : this(0.0, 0.0, 0) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The x coordinate. - /// The y coordinate. - public Point(double x, double y) - : this(x, y, 0) + : this(0.0d, 0.0d) { } @@ -52,7 +42,7 @@ public Point(double x, double y) /// The x coordinate. /// The y coordinate. /// The point label. - public Point(double x, double y, int label) + public Point(double x, double y, int label = 0) { this.x = x; this.y = y; @@ -66,8 +56,8 @@ public Point(double x, double y, int label) /// public int ID { - get { return id; } - set { id = value; } + get => id; + set => id = value; } /// @@ -75,8 +65,8 @@ public int ID /// public double X { - get { return x; } - set { x = value; } + get => x; + set => x = value; } /// @@ -84,8 +74,8 @@ public double X /// public double Y { - get { return y; } - set { y = value; } + get => y; + set => y = value; } #if USE_Z @@ -107,16 +97,21 @@ public double Z /// public int Label { - get { return label; } - set { label = value; } + get => label; + set => label = value; } #endregion #region Overriding Equals() and == Operator - /// - public static bool operator ==(Point a, Point b) + /// + /// + /// + /// + /// + /// + public static bool operator ==(Point? a, Point? b) { if (a is null) { @@ -133,7 +128,12 @@ public int Label return a.Equals(b); } - /// + /// + /// + /// + /// + /// + /// public static bool operator !=(Point a, Point b) { return !(a == b); @@ -143,7 +143,7 @@ public int Label public override bool Equals(object obj) => Equals(obj as Point); /// - public bool Equals(Point p) + public bool Equals(Point? p) { // If object is null return false. if (p is null) @@ -158,23 +158,24 @@ public bool Equals(Point p) #endregion /// - public int CompareTo(Point other) + public int CompareTo(Point? other) { + if (other is null) return -1; + if (x == other.x && y == other.y) { return 0; } - return (x < other.x || (x == other.x && y < other.y)) ? -1 : 1; + return x < other.x || (x == other.x && y < other.y) ? -1 : 1; } /// public override int GetHashCode() { - int hash = 19; + var hash = 19; hash = hash * 31 + x.GetHashCode(); hash = hash * 31 + y.GetHashCode(); - return hash; } } diff --git a/src/Triangle/Geometry/Polygon.cs b/src/Triangle/Geometry/Polygon.cs index 87d5fd9..9ac265e 100644 --- a/src/Triangle/Geometry/Polygon.cs +++ b/src/Triangle/Geometry/Polygon.cs @@ -13,22 +13,17 @@ namespace TriangleNet.Geometry /// public class Polygon : IPolygon { - private readonly List points; - private readonly List segments; - private readonly List holes; - private readonly List regions; - /// - public List Points => points; + public List Points { get; } /// - public List Holes => holes; + public List Holes { get; } /// - public List Regions => regions; + public List Regions { get; } /// - public List Segments => segments; + public List Segments { get; } /// public bool HasPointMarkers { get; set; } @@ -37,10 +32,7 @@ public class Polygon : IPolygon public bool HasSegmentMarkers { get; set; } /// - public int Count - { - get { return points.Count; } - } + public int Count => Points.Count; /// /// Initializes a new instance of the class. @@ -66,11 +58,11 @@ public Polygon(int capacity) /// Use point and segment markers. public Polygon(int capacity, bool markers) { - points = new List(capacity); - holes = new List(); - regions = new List(); + Points = new List(capacity); + Holes = new List(); + Regions = new List(); - segments = new List(); + Segments = new List(); HasPointMarkers = markers; HasSegmentMarkers = markers; @@ -80,7 +72,7 @@ public Polygon(int capacity, bool markers) public Rectangle Bounds() { var bounds = new Rectangle(); - bounds.Expand(points); + bounds.Expand(Points); return bounds; } @@ -88,33 +80,33 @@ public Rectangle Bounds() /// public void Add(Vertex vertex) { - points.Add(vertex); + Points.Add(vertex); } /// public void Add(ISegment segment, bool insert = false) { - segments.Add(segment); + Segments.Add(segment); if (insert) { - points.Add(segment.GetVertex(0)); - points.Add(segment.GetVertex(1)); + Points.Add(segment.GetVertex(0)); + Points.Add(segment.GetVertex(1)); } } /// public void Add(ISegment segment, int index) { - segments.Add(segment); + Segments.Add(segment); - points.Add(segment.GetVertex(index)); + Points.Add(segment.GetVertex(index)); } private void AddContourPointsAndSegments(Contour contour) { - points.AddRange(contour.Points); - segments.AddRange(contour.GetSegments()); + Points.AddRange(contour.Points); + Segments.AddRange(contour.GetSegments()); } /// @@ -145,7 +137,7 @@ public void Add(Contour contour, bool hole = false) public void Add(Contour contour, Point hole) { AddContourPointsAndSegments(contour); - holes.Add(hole); + Holes.Add(hole); } } } diff --git a/src/Triangle/Geometry/Rectangle.cs b/src/Triangle/Geometry/Rectangle.cs index 049baca..f5c68f7 100644 --- a/src/Triangle/Geometry/Rectangle.cs +++ b/src/Triangle/Geometry/Rectangle.cs @@ -14,15 +14,13 @@ namespace TriangleNet.Geometry /// public class Rectangle { - double xmin, ymin, xmax, ymax; - /// /// Initializes a new instance of the class. /// public Rectangle() { - xmin = ymin = double.MaxValue; - xmax = ymax = -double.MaxValue; + Left = Bottom = double.MaxValue; + Right = Top = -double.MaxValue; } /// @@ -44,51 +42,51 @@ public Rectangle(Rectangle other) /// Height of the rectangle. public Rectangle(double x, double y, double width, double height) { - xmin = x; - ymin = y; - xmax = x + width; - ymax = y + height; + Left = x; + Bottom = y; + Right = x + width; + Top = y + height; } /// /// Gets the minimum x value (left boundary). /// - public double Left => xmin; + public double Left { get; private set; } /// /// Gets the maximum x value (right boundary). /// - public double Right => xmax; + public double Right { get; private set; } /// /// Gets the minimum y value (bottom boundary). /// - public double Bottom => ymin; + public double Bottom { get; private set; } /// /// Gets the maximum y value (top boundary). /// - public double Top => ymax; + public double Top { get; private set; } /// /// Gets the minimum x value (left boundary). /// - public double X => xmin; + public double X => Left; /// /// Gets the minimum y value (bottom boundary). /// - public double Y => ymin; + public double Y => Bottom; /// /// Gets the width of the rectangle. /// - public double Width => xmax - xmin; + public double Width => Right - Left; /// /// Gets the height of the rectangle. /// - public double Height => ymax - ymin; + public double Height => Top - Bottom; /// /// Update bounds. @@ -97,10 +95,10 @@ public Rectangle(double x, double y, double width, double height) /// Add dy to top and bottom bounds. public void Resize(double dx, double dy) { - xmin -= dx; - xmax += dx; - ymin -= dy; - ymax += dy; + Left -= dx; + Right += dx; + Bottom -= dy; + Top += dy; } /// @@ -109,10 +107,10 @@ public void Resize(double dx, double dy) /// Point. public void Expand(Point p) { - xmin = Math.Min(xmin, p.x); - ymin = Math.Min(ymin, p.y); - xmax = Math.Max(xmax, p.x); - ymax = Math.Max(ymax, p.y); + Left = Math.Min(Left, p.x); + Bottom = Math.Min(Bottom, p.y); + Right = Math.Max(Right, p.x); + Top = Math.Max(Top, p.y); } /// @@ -132,10 +130,10 @@ public void Expand(IEnumerable points) /// The other rectangle. public void Expand(Rectangle other) { - xmin = Math.Min(xmin, other.xmin); - ymin = Math.Min(ymin, other.ymin); - xmax = Math.Max(xmax, other.xmax); - ymax = Math.Max(ymax, other.ymax); + Left = Math.Min(Left, other.Left); + Bottom = Math.Min(Bottom, other.Bottom); + Right = Math.Max(Right, other.Right); + Top = Math.Max(Top, other.Top); } /// @@ -146,7 +144,7 @@ public void Expand(Rectangle other) /// Return true, if rectangle contains given point. public bool Contains(double x, double y) { - return (x >= xmin) && (x <= xmax) && (y >= ymin) && (y <= ymax); + return (x >= Left) && (x <= Right) && (y >= Bottom) && (y <= Top); } /// @@ -166,8 +164,8 @@ public bool Contains(Point pt) /// Return true, if this rectangle contains given rectangle. public bool Contains(Rectangle other) { - return xmin <= other.Left && other.Right <= xmax - && ymin <= other.Bottom && other.Top <= ymax; + return Left <= other.Left && other.Right <= Right + && Bottom <= other.Bottom && other.Top <= Top; } /// @@ -177,8 +175,8 @@ public bool Contains(Rectangle other) /// Return true, if given rectangle intersects this rectangle. public bool Intersects(Rectangle other) { - return other.Left < xmax && xmin < other.Right - && other.Bottom < ymax && ymin < other.Top; + return other.Left < Right && Left < other.Right + && other.Bottom < Top && Bottom < other.Top; } } } diff --git a/src/Triangle/Geometry/RegionPointer.cs b/src/Triangle/Geometry/RegionPointer.cs index 6eb93b3..e6b9999 100644 --- a/src/Triangle/Geometry/RegionPointer.cs +++ b/src/Triangle/Geometry/RegionPointer.cs @@ -23,7 +23,7 @@ public class RegionPointer /// public double Area { - get { return area; } + get => area; set { if (value < 0.0) @@ -54,7 +54,7 @@ public RegionPointer(double x, double y, int id) /// Area constraint. public RegionPointer(double x, double y, int id, double area) { - this.point = new Point(x, y); + point = new Point(x, y); this.id = id; this.area = area; } diff --git a/src/Triangle/Geometry/Segment.cs b/src/Triangle/Geometry/Segment.cs index 463f169..5b6a5ad 100644 --- a/src/Triangle/Geometry/Segment.cs +++ b/src/Triangle/Geometry/Segment.cs @@ -13,19 +13,14 @@ namespace TriangleNet.Geometry /// public class Segment : ISegment { - Vertex v0; - Vertex v1; - - int label; + private Vertex v0; + private Vertex v1; /// /// Gets or sets the segments boundary mark. /// - public int Label - { - get { return label; } - set { label = value; } - } + public int Label { get; set; } + /// /// Gets the first endpoints index. /// @@ -52,7 +47,7 @@ public Segment(Vertex v0, Vertex v1, int label) this.v0 = v0; this.v1 = v1; - this.label = label; + this.Label = label; } /// diff --git a/src/Triangle/Geometry/Vertex.cs b/src/Triangle/Geometry/Vertex.cs index e84b9e4..8e6028f 100644 --- a/src/Triangle/Geometry/Vertex.cs +++ b/src/Triangle/Geometry/Vertex.cs @@ -7,7 +7,7 @@ namespace TriangleNet.Geometry { using System; - using TriangleNet.Topology; + using Topology; /// /// The vertex data structure. diff --git a/src/Triangle/IO/DebugWriter.cs b/src/Triangle/IO/DebugWriter.cs index b78b970..da925a7 100644 --- a/src/Triangle/IO/DebugWriter.cs +++ b/src/Triangle/IO/DebugWriter.cs @@ -4,6 +4,8 @@ // // ----------------------------------------------------------------------- +using TriangleNet.Meshing; + namespace TriangleNet.IO { using System; @@ -11,8 +13,8 @@ namespace TriangleNet.IO using System.IO; using System.IO.Compression; using System.Text; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Writes a the current mesh into a text file. @@ -35,53 +37,45 @@ namespace TriangleNet.IO /// ... /// id_n p1 p2 p3 n1 n2 n3 /// - class DebugWriter + internal class DebugWriter { - static NumberFormatInfo nfi = CultureInfo.InvariantCulture.NumberFormat; + private static NumberFormatInfo nfi = CultureInfo.InvariantCulture.NumberFormat; - int iteration; - string session; - StreamWriter stream; - string tmpFile; - int[] vertices; - int triangles; + private int iteration; + private string session = string.Empty; + private StreamWriter? stream; + private string tmpFile = string.Empty; + private int[] vertices = {}; + private int triangles; #region Singleton pattern - private static readonly DebugWriter instance = new DebugWriter(); - // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static DebugWriter() { } private DebugWriter() { } - public static DebugWriter Session - { - get - { - return instance; - } - } + public static DebugWriter Session { get; } = new(); #endregion /// /// Start a new session with given name. /// - /// Name of the session (and output files). + /// Name of the session (and output files). public void Start(string session) { - this.iteration = 0; + iteration = 0; this.session = session; - if (this.stream != null) + if (stream != null) { - throw new Exception("A session is active. Finish before starting a new."); + throw new InvalidOperationException("A session is active. Finish before starting a new."); } - this.tmpFile = Path.GetTempFileName(); - this.stream = new StreamWriter(tmpFile); + tmpFile = Path.GetTempFileName(); + stream = new StreamWriter(tmpFile); } /// @@ -89,9 +83,9 @@ public void Start(string session) /// public void Write(Mesh mesh, bool skip = false) { - this.WriteMesh(mesh, skip); + WriteMesh(mesh, skip); - this.triangles = mesh.Triangles.Count; + triangles = mesh.Triangles.Count; } /// @@ -99,39 +93,38 @@ public void Write(Mesh mesh, bool skip = false) /// public void Finish() { - this.Finish(session + ".mshx"); + Finish(session + ".mshx"); } private void Finish(string path) { - if (stream != null) - { - stream.Flush(); - stream.Dispose(); - stream = null; + if (stream == null) return; - string header = "#!N" + this.iteration + Environment.NewLine; + stream.Flush(); + stream.Dispose(); + stream = null; - using (var gzFile = new FileStream(path, FileMode.Create)) + var header = "#!N" + iteration + Environment.NewLine; + + using (var gzFile = new FileStream(path, FileMode.Create)) + { + using (var gzStream = new GZipStream(gzFile, CompressionMode.Compress, false)) { - using (var gzStream = new GZipStream(gzFile, CompressionMode.Compress, false)) - { - byte[] bytes = Encoding.UTF8.GetBytes(header); - gzStream.Write(bytes, 0, bytes.Length); - - // TODO: read with stream - bytes = File.ReadAllBytes(tmpFile); - gzStream.Write(bytes, 0, bytes.Length); - } - } + var bytes = Encoding.UTF8.GetBytes(header); + gzStream.Write(bytes, 0, bytes.Length); - File.Delete(this.tmpFile); + // TODO: read with stream + bytes = File.ReadAllBytes(tmpFile); + gzStream.Write(bytes, 0, bytes.Length); + } } + + File.Delete(tmpFile); } private void WriteGeometry(IPolygon geometry) { - stream.WriteLine("#!G{0}", this.iteration++); + stream?.WriteLine("#!G{0}", iteration++); } private void WriteMesh(Mesh mesh, bool skip) @@ -143,32 +136,32 @@ private void WriteMesh(Mesh mesh, bool skip) } // Header line - stream.WriteLine("#!M{0}", this.iteration++); + stream?.WriteLine("#!M{0}", iteration++); - Vertex p1, p2, p3; + Vertex? p1, p2, p3; if (VerticesChanged(mesh)) { HashVertices(mesh); // Number of vertices. - stream.WriteLine("{0}", mesh.vertices.Count); + stream?.WriteLine("{0}", mesh.vertices.Count); foreach (var v in mesh.vertices.Values) { // Vertex number, x and y coordinates and marker. - stream.WriteLine("{0} {1} {2} {3}", v.id, v.x.ToString(nfi), v.y.ToString(nfi), v.label); + stream?.WriteLine("{0} {1} {2} {3}", v.id, v.x.ToString(nfi), v.y.ToString(nfi), v.label); } } else { - stream.WriteLine("0"); + stream?.WriteLine("0"); } // Number of segments. - stream.WriteLine("{0}", mesh.subsegs.Count); + stream?.WriteLine("{0}", mesh.subsegs.Count); - Osub subseg = default(Osub); + var subseg = default(Osub); subseg.orient = 0; foreach (var item in mesh.subsegs.Values) @@ -184,16 +177,16 @@ private void WriteMesh(Mesh mesh, bool skip) p2 = subseg.Dest(); // Segment number, indices of its two endpoints, and marker. - stream.WriteLine("{0} {1} {2} {3}", subseg.seg.hash, p1.id, p2.id, subseg.seg.boundary); + stream?.WriteLine("{0} {1} {2} {3}", subseg.seg.hash, p1.id, p2.id, subseg.seg.boundary); } - Otri tri = default(Otri), trisym = default(Otri); + Otri tri = default, trisym = default; tri.orient = 0; int n1, n2, n3, h1, h2, h3; // Number of triangles. - stream.WriteLine("{0}", mesh.triangles.Count); + stream?.WriteLine("{0}", mesh.triangles.Count); foreach (var item in mesh.triangles) { @@ -208,7 +201,7 @@ private void WriteMesh(Mesh mesh, bool skip) h3 = (p3 == null) ? -1 : p3.id; // Triangle number, indices for three vertices. - stream.Write("{0} {1} {2} {3}", tri.tri.hash, h1, h2, h3); + stream?.Write("{0} {1} {2} {3}", tri.tri.hash, h1, h2, h3); tri.orient = 1; tri.Sym(ref trisym); @@ -223,18 +216,18 @@ private void WriteMesh(Mesh mesh, bool skip) n3 = trisym.tri.hash; // Neighboring triangle numbers. - stream.WriteLine(" {0} {1} {2}", n1, n2, n3); + stream?.WriteLine(" {0} {1} {2}", n1, n2, n3); } } - private bool VerticesChanged(Mesh mesh) + private bool VerticesChanged(IMesh mesh) { - if (vertices == null || mesh.Vertices.Count != vertices.Length) + if (vertices.Length == 0 || mesh.Vertices.Count != vertices.Length) { return true; } - int i = 0; + var i = 0; foreach (var v in mesh.Vertices) { if (v.id != vertices[i++]) @@ -246,14 +239,14 @@ private bool VerticesChanged(Mesh mesh) return false; } - private void HashVertices(Mesh mesh) + private void HashVertices(IMesh mesh) { - if (vertices == null || mesh.Vertices.Count != vertices.Length) + if (vertices.Length == 0 || mesh.Vertices.Count != vertices.Length) { vertices = new int[mesh.Vertices.Count]; } - int i = 0; + var i = 0; foreach (var v in mesh.Vertices) { vertices[i++] = v.id; diff --git a/src/Triangle/IO/FileProcessor.cs b/src/Triangle/IO/FileProcessor.cs index 43dcb97..eb6f83a 100644 --- a/src/Triangle/IO/FileProcessor.cs +++ b/src/Triangle/IO/FileProcessor.cs @@ -8,12 +8,12 @@ namespace TriangleNet.IO { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; + using Geometry; + using Meshing; public static class FileProcessor { - static List formats; + private static List formats; static FileProcessor() { diff --git a/src/Triangle/IO/IMeshFormat.cs b/src/Triangle/IO/IMeshFormat.cs index 846716f..8470027 100644 --- a/src/Triangle/IO/IMeshFormat.cs +++ b/src/Triangle/IO/IMeshFormat.cs @@ -7,7 +7,7 @@ namespace TriangleNet.IO { using System.IO; - using TriangleNet.Meshing; + using Meshing; /// /// Interface for mesh I/O. diff --git a/src/Triangle/IO/IPolygonFormat.cs b/src/Triangle/IO/IPolygonFormat.cs index ba2e9a7..e2a7279 100644 --- a/src/Triangle/IO/IPolygonFormat.cs +++ b/src/Triangle/IO/IPolygonFormat.cs @@ -7,7 +7,7 @@ namespace TriangleNet.IO { using System.IO; - using TriangleNet.Geometry; + using Geometry; /// /// Interface for geometry input. diff --git a/src/Triangle/IO/InputTriangle.cs b/src/Triangle/IO/InputTriangle.cs index f98d17b..87350a0 100644 --- a/src/Triangle/IO/InputTriangle.cs +++ b/src/Triangle/IO/InputTriangle.cs @@ -7,7 +7,7 @@ namespace TriangleNet.IO { using System; - using TriangleNet.Geometry; + using Geometry; /// /// Simple triangle class for input. @@ -31,22 +31,22 @@ public InputTriangle(int p0, int p1, int p2) /// public int ID { - get { return 0; } + get => 0; set { } } /// public int Label { - get { return label; } - set { label = value; } + get => label; + set => label = value; } /// public double Area { - get { return area; } - set { area = value; } + get => area; + set => area = value; } /// diff --git a/src/Triangle/IO/TriangleFormat.cs b/src/Triangle/IO/TriangleFormat.cs index e9611f7..45c19f8 100644 --- a/src/Triangle/IO/TriangleFormat.cs +++ b/src/Triangle/IO/TriangleFormat.cs @@ -9,8 +9,8 @@ namespace TriangleNet.IO using System; using System.Collections.Generic; using System.IO; - using TriangleNet.Geometry; - using TriangleNet.Meshing; + using Geometry; + using Meshing; /// /// Implements geometry and mesh file formats of the original Triangle project. @@ -20,7 +20,7 @@ public class TriangleFormat : IPolygonFormat, IMeshFormat /// public bool IsSupported(string file) { - string ext = Path.GetExtension(file).ToLower(); + var ext = Path.GetExtension(file).ToLower(); if (ext == ".node" || ext == ".poly" || ext == ".ele") { @@ -33,7 +33,7 @@ public bool IsSupported(string file) /// public IMesh Import(string filename) { - string ext = Path.GetExtension(filename); + var ext = Path.GetExtension(filename); if (ext == ".node" || ext == ".poly" || ext == ".ele") { @@ -69,7 +69,7 @@ public void Write(IMesh mesh, Stream stream) /// public IPolygon Read(string filename) { - string ext = Path.GetExtension(filename); + var ext = Path.GetExtension(filename); if (ext == ".node") { diff --git a/src/Triangle/IO/TriangleReader.cs b/src/Triangle/IO/TriangleReader.cs index 245f3b1..f042b6a 100644 --- a/src/Triangle/IO/TriangleReader.cs +++ b/src/Triangle/IO/TriangleReader.cs @@ -11,7 +11,7 @@ namespace TriangleNet.IO using System.Collections.Generic; using System.Globalization; using System.IO; - using TriangleNet.Geometry; + using Geometry; /// /// Helper methods for reading Triangle file formats. @@ -20,7 +20,7 @@ public class TriangleReader { private static readonly NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo; - int startIndex = 0; + private int startIndex; #region Helper methods @@ -33,7 +33,7 @@ private bool TryReadLine(StreamReader reader, out string[] token) return false; } - string line = reader.ReadLine().Trim(); + var line = reader.ReadLine().Trim(); while (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) { @@ -60,8 +60,8 @@ private bool TryReadLine(StreamReader reader, out string[] token) /// Number of point markers (0 or 1) private void ReadVertex(List data, int index, string[] line, int attributes, int marks) { - double x = double.Parse(line[1], nfi); - double y = double.Parse(line[2], nfi); + var x = double.Parse(line[1], nfi); + var y = double.Parse(line[2], nfi); var v = new Vertex(x, y); @@ -101,7 +101,7 @@ private void ReadVertex(List data, int index, string[] line, int attribu /// public void Read(string filename, out Polygon polygon) { - string path = Path.ChangeExtension(filename, ".poly"); + var path = Path.ChangeExtension(filename, ".poly"); if (File.Exists(path)) { @@ -123,7 +123,7 @@ public void Read(string filename, out Polygon geometry, out List tria Read(filename, out geometry); - string path = Path.ChangeExtension(filename, ".ele"); + var path = Path.ChangeExtension(filename, ".ele"); if (File.Exists(path) && geometry != null) { @@ -136,7 +136,7 @@ public void Read(string filename, out Polygon geometry, out List tria /// public IPolygon Read(string filename) { - Read(filename, out Polygon geometry); + Read(filename, out var geometry); return geometry; } @@ -206,7 +206,7 @@ public Polygon ReadNodeFile(string nodefilename, bool readElements) // Read the vertices. if (invertices > 0) { - for (int i = 0; i < invertices; i++) + for (var i = 0; i < invertices; i++) { if (!TryReadLine(reader, out line)) { @@ -231,7 +231,7 @@ public Polygon ReadNodeFile(string nodefilename, bool readElements) if (readElements) { // Read area file - string elefile = Path.ChangeExtension(nodefilename, ".ele"); + var elefile = Path.ChangeExtension(nodefilename, ".ele"); if (File.Exists(elefile)) { ReadEleFile(elefile, true); @@ -312,7 +312,7 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre { data = new Polygon(invertices); - for (int i = 0; i < invertices; i++) + for (var i = 0; i < invertices; i++) { if (!TryReadLine(reader, out line)) { @@ -357,9 +357,9 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre throw new Exception("Can't read input file (segments)."); } - int insegments = int.Parse(line[0]); + var insegments = int.Parse(line[0]); - int segmentmarkers = 0; + var segmentmarkers = 0; if (line.Length > 1) { segmentmarkers = int.Parse(line[1]); @@ -367,7 +367,7 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre int end1, end2, mark; // Read and insert the segments. - for (int i = 0; i < insegments; i++) + for (var i = 0; i < insegments; i++) { if (!TryReadLine(reader, out line)) { @@ -419,10 +419,10 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre throw new Exception("Can't read input file (holes)."); } - int holes = int.Parse(line[0]); + var holes = int.Parse(line[0]); if (holes > 0) { - for (int i = 0; i < holes; i++) + for (var i = 0; i < holes; i++) { if (!TryReadLine(reader, out line)) { @@ -446,7 +446,7 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre if (regions > 0) { - for (int i = 0; i < regions; i++) + for (var i = 0; i < regions; i++) { if (!TryReadLine(reader, out line)) { @@ -463,7 +463,7 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre id = i; } - double area = 0.0; + var area = 0.0; if (line.Length > 4) { @@ -492,7 +492,7 @@ public Polygon ReadPolyFile(string polyfilename, bool readElements, bool readAre // Read ele file if (readElements) { - string elefile = Path.ChangeExtension(polyfilename, ".ele"); + var elefile = Path.ChangeExtension(polyfilename, ".ele"); if (File.Exists(elefile)) { ReadEleFile(elefile, readArea); @@ -527,7 +527,7 @@ private List ReadEleFile(string elefilename, bool readArea) { // Read number of elements and number of attributes. string[] line; - bool validRegion = false; + var validRegion = false; if (!TryReadLine(reader, out line)) { @@ -555,7 +555,7 @@ private List ReadEleFile(string elefilename, bool readArea) InputTriangle tri; // Read triangles. - for (int i = 0; i < intriangles; i++) + for (var i = 0; i < intriangles; i++) { if (!TryReadLine(reader, out line)) { @@ -576,7 +576,7 @@ private List ReadEleFile(string elefilename, bool readArea) // Read triangle region if (attributes > 0 && validRegion) { - int region = 0; + var region = 0; validRegion = int.TryParse(line[4], out region); tri.label = region; } @@ -588,7 +588,7 @@ private List ReadEleFile(string elefilename, bool readArea) // Read area file if (readArea) { - string areafile = Path.ChangeExtension(elefilename, ".area"); + var areafile = Path.ChangeExtension(elefilename, ".area"); if (File.Exists(areafile)) { ReadAreaFile(areafile, intriangles); @@ -607,39 +607,37 @@ private double[] ReadAreaFile(string areafilename, int intriangles) { double[] data = null; - using (var reader = new StreamReader(areafilename)) + using var reader = new StreamReader(areafilename); + string[] line; + + if (!TryReadLine(reader, out line)) { - string[] line; + throw new Exception("Can't read input file (area)."); + } + + if (int.Parse(line[0]) != intriangles) + { + Log.Instance.Warning("Number of area constraints doesn't match number of triangles.", + "TriangleReader.ReadAreaFile()"); + return null; + } + + data = new double[intriangles]; + // Read area constraints. + for (var i = 0; i < intriangles; i++) + { if (!TryReadLine(reader, out line)) { throw new Exception("Can't read input file (area)."); } - if (int.Parse(line[0]) != intriangles) + if (line.Length != 2) { - Log.Instance.Warning("Number of area constraints doesn't match number of triangles.", - "TriangleReader.ReadAreaFile()"); - return null; + throw new Exception("Triangle has no nodes."); } - data = new double[intriangles]; - - // Read area constraints. - for (int i = 0; i < intriangles; i++) - { - if (!TryReadLine(reader, out line)) - { - throw new Exception("Can't read input file (area)."); - } - - if (line.Length != 2) - { - throw new Exception("Triangle has no nodes."); - } - - data[i] = double.Parse(line[1], nfi); - } + data[i] = double.Parse(line[1], nfi); } return data; @@ -660,74 +658,72 @@ public List ReadEdgeFile(string edgeFile, int invertices) string[] line; - using (var reader = new StreamReader(edgeFile)) + using var reader = new StreamReader(edgeFile); + // Read the edges from a .edge file. + + // Read number of segments and number of boundary markers. + if (!TryReadLine(reader, out line)) { - // Read the edges from a .edge file. + throw new Exception("Can't read input file (segments)."); + } - // Read number of segments and number of boundary markers. + var inedges = int.Parse(line[0]); + + var edgemarkers = 0; + if (line.Length > 1) + { + edgemarkers = int.Parse(line[1]); + } + + if (inedges > 0) + { + data = new List(inedges); + } + + int end1, end2, mark; + // Read and insert the segments. + for (var i = 0; i < inedges; i++) + { if (!TryReadLine(reader, out line)) { throw new Exception("Can't read input file (segments)."); } - int inedges = int.Parse(line[0]); - - int edgemarkers = 0; - if (line.Length > 1) + if (line.Length < 3) { - edgemarkers = int.Parse(line[1]); + throw new Exception("Segment has no endpoints."); } - if (inedges > 0) + // TODO: startIndex ok? + end1 = int.Parse(line[1]) - startIndex; + end2 = int.Parse(line[2]) - startIndex; + mark = 0; + + if (edgemarkers > 0 && line.Length > 3) { - data = new List(inedges); + mark = int.Parse(line[3]); } - int end1, end2, mark; - // Read and insert the segments. - for (int i = 0; i < inedges; i++) + if ((end1 < 0) || (end1 >= invertices)) { - if (!TryReadLine(reader, out line)) + if (Log.Verbose) { - throw new Exception("Can't read input file (segments)."); - } - - if (line.Length < 3) - { - throw new Exception("Segment has no endpoints."); - } - - // TODO: startIndex ok? - end1 = int.Parse(line[1]) - startIndex; - end2 = int.Parse(line[2]) - startIndex; - mark = 0; - - if (edgemarkers > 0 && line.Length > 3) - { - mark = int.Parse(line[3]); - } - - if ((end1 < 0) || (end1 >= invertices)) - { - if (Log.Verbose) - { - Log.Instance.Warning("Invalid first endpoint of segment.", - "TriangleReader.ReadEdgeFile()"); - } - } - else if ((end2 < 0) || (end2 >= invertices)) - { - if (Log.Verbose) - { - Log.Instance.Warning("Invalid second endpoint of segment.", - "TriangleReader.ReadEdgeFile()"); - } + Log.Instance.Warning("Invalid first endpoint of segment.", + "TriangleReader.ReadEdgeFile()"); } - else + } + else if ((end2 < 0) || (end2 >= invertices)) + { + if (Log.Verbose) { - data.Add(new Edge(end1, end2, mark)); + Log.Instance.Warning("Invalid second endpoint of segment.", + "TriangleReader.ReadEdgeFile()"); } } + else + { + data.Add(new Edge(end1, end2, mark)); + } } return data; diff --git a/src/Triangle/IO/TriangleWriter.cs b/src/Triangle/IO/TriangleWriter.cs index b78d316..8f529e2 100644 --- a/src/Triangle/IO/TriangleWriter.cs +++ b/src/Triangle/IO/TriangleWriter.cs @@ -10,15 +10,15 @@ namespace TriangleNet.IO using System.Collections.Generic; using System.Globalization; using System.IO; - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// Helper methods for writing Triangle file formats. /// public class TriangleWriter { - static NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo; + private static NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo; /// /// Number the vertices and write them to a .node file. @@ -38,10 +38,8 @@ public void Write(Mesh mesh, string filename) /// public void WriteNodes(Mesh mesh, string filename) { - using (var writer = new StreamWriter(filename)) - { - WriteNodes(writer, mesh); - } + using var writer = new StreamWriter(filename); + WriteNodes(writer, mesh); } /// @@ -49,10 +47,10 @@ public void WriteNodes(Mesh mesh, string filename) /// private void WriteNodes(StreamWriter writer, Mesh mesh) { - int outvertices = mesh.vertices.Count; - int nextras = mesh.nextras; + var outvertices = mesh.vertices.Count; + var nextras = mesh.nextras; - Behavior behavior = mesh.behavior; + var behavior = mesh.behavior; if (behavior.Jettison) { @@ -108,7 +106,7 @@ private void WriteNodes(StreamWriter writer, Mesh mesh) private void WriteNodes(StreamWriter writer, IEnumerable nodes, bool markers, int attribs, bool jettison) { - int index = 0; + var index = 0; foreach (var vertex in nodes) { @@ -145,40 +143,38 @@ private void WriteNodes(StreamWriter writer, IEnumerable nodes, bool mar /// public void WriteElements(Mesh mesh, string filename) { - Otri tri = default(Otri); - Vertex p1, p2, p3; - bool regions = mesh.behavior.useRegions; + var tri = default(Otri); + Vertex? p1, p2, p3; + var regions = mesh.behavior.useRegions; - int j = 0; + var j = 0; tri.orient = 0; - using (var writer = new StreamWriter(filename)) - { - // Number of triangles, vertices per triangle, attributes per triangle. - writer.WriteLine("{0} 3 {1}", mesh.triangles.Count, regions ? 1 : 0); + using var writer = new StreamWriter(filename); + // Number of triangles, vertices per triangle, attributes per triangle. + writer.WriteLine("{0} 3 {1}", mesh.triangles.Count, regions ? 1 : 0); - foreach (var item in mesh.triangles) - { - tri.tri = item; + foreach (var item in mesh.triangles) + { + tri.tri = item; - p1 = tri.Org(); - p2 = tri.Dest(); - p3 = tri.Apex(); + p1 = tri.Org(); + p2 = tri.Dest(); + p3 = tri.Apex(); - // Triangle number, indices for three vertices. - writer.Write("{0} {1} {2} {3}", j, p1.id, p2.id, p3.id); + // Triangle number, indices for three vertices. + writer.Write("{0} {1} {2} {3}", j, p1.id, p2.id, p3.id); - if (regions) - { - writer.Write(" {0}", tri.tri.label); - } + if (regions) + { + writer.Write(" {0}", tri.tri.label); + } - writer.WriteLine(); + writer.WriteLine(); - // Number elements - item.id = j++; - } + // Number elements + item.id = j++; } } @@ -192,61 +188,59 @@ public void WriteElements(Mesh mesh, string filename) /// are numbered right. public void WritePoly(IPolygon polygon, string filename) { - bool hasMarkers = polygon.HasSegmentMarkers; - - using (var writer = new StreamWriter(filename)) - { - // TODO: write vertex attributes + var hasMarkers = polygon.HasSegmentMarkers; - writer.WriteLine("{0} 2 0 {1}", polygon.Points.Count, polygon.HasPointMarkers ? "1" : "0"); + using var writer = new StreamWriter(filename); + // TODO: write vertex attributes - // Write nodes to this file. - WriteNodes(writer, polygon.Points, polygon.HasPointMarkers, 0, false); + writer.WriteLine("{0} 2 0 {1}", polygon.Points.Count, polygon.HasPointMarkers ? "1" : "0"); - // Number of segments, number of boundary markers (zero or one). - writer.WriteLine("{0} {1}", polygon.Segments.Count, hasMarkers ? "1" : "0"); + // Write nodes to this file. + WriteNodes(writer, polygon.Points, polygon.HasPointMarkers, 0, false); - Vertex p, q; + // Number of segments, number of boundary markers (zero or one). + writer.WriteLine("{0} {1}", polygon.Segments.Count, hasMarkers ? "1" : "0"); - int j = 0; - foreach (var seg in polygon.Segments) - { - p = seg.GetVertex(0); - q = seg.GetVertex(1); + Vertex p, q; - // Segment number, indices of its two endpoints, and possibly a marker. - if (hasMarkers) - { - writer.WriteLine("{0} {1} {2} {3}", j, p.ID, q.ID, seg.Label); - } - else - { - writer.WriteLine("{0} {1} {2}", j, p.ID, q.ID); - } + var j = 0; + foreach (var seg in polygon.Segments) + { + p = seg.GetVertex(0); + q = seg.GetVertex(1); - j++; + // Segment number, indices of its two endpoints, and possibly a marker. + if (hasMarkers) + { + writer.WriteLine("{0} {1} {2} {3}", j, p.ID, q.ID, seg.Label); } - - // Holes - j = 0; - writer.WriteLine("{0}", polygon.Holes.Count); - foreach (var hole in polygon.Holes) + else { - writer.WriteLine("{0} {1} {2}", j++, hole.X.ToString(nfi), hole.Y.ToString(nfi)); + writer.WriteLine("{0} {1} {2}", j, p.ID, q.ID); } - // Regions - if (polygon.Regions.Count > 0) + j++; + } + + // Holes + j = 0; + writer.WriteLine("{0}", polygon.Holes.Count); + foreach (var hole in polygon.Holes) + { + writer.WriteLine("{0} {1} {2}", j++, hole.X.ToString(nfi), hole.Y.ToString(nfi)); + } + + // Regions + if (polygon.Regions.Count > 0) + { + j = 0; + writer.WriteLine("{0}", polygon.Regions.Count); + foreach (var region in polygon.Regions) { - j = 0; - writer.WriteLine("{0}", polygon.Regions.Count); - foreach (var region in polygon.Regions) - { - writer.WriteLine("{0} {1} {2} {3}", j, region.point.X.ToString(nfi), - region.point.Y.ToString(nfi), region.id); + writer.WriteLine("{0} {1} {2} {3}", j, region.point.X.ToString(nfi), + region.point.Y.ToString(nfi), region.id); - j++; - } + j++; } } } @@ -272,74 +266,72 @@ public void WritePoly(Mesh mesh, string filename) /// are numbered right. public void WritePoly(Mesh mesh, string filename, bool writeNodes) { - Osub subseg = default(Osub); + var subseg = default(Osub); Vertex pt1, pt2; - bool useBoundaryMarkers = mesh.behavior.UseBoundaryMarkers; + var useBoundaryMarkers = mesh.behavior.UseBoundaryMarkers; - using (var writer = new StreamWriter(filename)) + using var writer = new StreamWriter(filename); + if (writeNodes) { - if (writeNodes) - { - // Write nodes to this file. - WriteNodes(writer, mesh); - } - else - { - // The zero indicates that the vertices are in a separate .node file. - // Followed by number of dimensions, number of vertex attributes, - // and number of boundary markers (zero or one). - writer.WriteLine("0 {0} {1} {2}", mesh.mesh_dim, mesh.nextras, - useBoundaryMarkers ? "1" : "0"); - } - - // Number of segments, number of boundary markers (zero or one). - writer.WriteLine("{0} {1}", mesh.subsegs.Count, + // Write nodes to this file. + WriteNodes(writer, mesh); + } + else + { + // The zero indicates that the vertices are in a separate .node file. + // Followed by number of dimensions, number of vertex attributes, + // and number of boundary markers (zero or one). + writer.WriteLine("0 {0} {1} {2}", mesh.mesh_dim, mesh.nextras, useBoundaryMarkers ? "1" : "0"); + } - subseg.orient = 0; + // Number of segments, number of boundary markers (zero or one). + writer.WriteLine("{0} {1}", mesh.subsegs.Count, + useBoundaryMarkers ? "1" : "0"); - int j = 0; - foreach (var item in mesh.subsegs.Values) - { - subseg.seg = item; + subseg.orient = 0; - pt1 = subseg.Org(); - pt2 = subseg.Dest(); + var j = 0; + foreach (var item in mesh.subsegs.Values) + { + subseg.seg = item; - // Segment number, indices of its two endpoints, and possibly a marker. - if (useBoundaryMarkers) - { - writer.WriteLine("{0} {1} {2} {3}", j, pt1.id, pt2.id, subseg.seg.boundary); - } - else - { - writer.WriteLine("{0} {1} {2}", j, pt1.id, pt2.id); - } + pt1 = subseg.Org(); + pt2 = subseg.Dest(); - j++; + // Segment number, indices of its two endpoints, and possibly a marker. + if (useBoundaryMarkers) + { + writer.WriteLine("{0} {1} {2} {3}", j, pt1.id, pt2.id, subseg.seg.boundary); } - - // Holes - j = 0; - writer.WriteLine("{0}", mesh.holes.Count); - foreach (var hole in mesh.holes) + else { - writer.WriteLine("{0} {1} {2}", j++, hole.X.ToString(nfi), hole.Y.ToString(nfi)); + writer.WriteLine("{0} {1} {2}", j, pt1.id, pt2.id); } - // Regions - if (mesh.regions.Count > 0) + j++; + } + + // Holes + j = 0; + writer.WriteLine("{0}", mesh.holes.Count); + foreach (var hole in mesh.holes) + { + writer.WriteLine("{0} {1} {2}", j++, hole.X.ToString(nfi), hole.Y.ToString(nfi)); + } + + // Regions + if (mesh.regions.Count > 0) + { + j = 0; + writer.WriteLine("{0}", mesh.regions.Count); + foreach (var region in mesh.regions) { - j = 0; - writer.WriteLine("{0}", mesh.regions.Count); - foreach (var region in mesh.regions) - { - writer.WriteLine("{0} {1} {2} {3}", j, region.point.X.ToString(nfi), - region.point.Y.ToString(nfi), region.id); + writer.WriteLine("{0} {1} {2} {3}", j, region.point.X.ToString(nfi), + region.point.Y.ToString(nfi), region.id); - j++; - } + j++; } } } @@ -351,68 +343,64 @@ public void WritePoly(Mesh mesh, string filename, bool writeNodes) /// public void WriteEdges(Mesh mesh, string filename) { - Otri tri = default(Otri), trisym = default(Otri); - Osub checkmark = default(Osub); + Otri tri = default, trisym = default; + var checkmark = default(Osub); Vertex p1, p2; - Behavior behavior = mesh.behavior; + var behavior = mesh.behavior; - using (var writer = new StreamWriter(filename)) + using var writer = new StreamWriter(filename); + // Number of edges, number of boundary markers (zero or one). + writer.WriteLine("{0} {1}", mesh.NumberOfEdges, behavior.UseBoundaryMarkers ? "1" : "0"); + + long index = 0; + // To loop over the set of edges, loop over all triangles, and look at + // the three edges of each triangle. If there isn't another triangle + // adjacent to the edge, operate on the edge. If there is another + // adjacent triangle, operate on the edge only if the current triangle + // has a smaller pointer than its neighbor. This way, each edge is + // considered only once. + foreach (var item in mesh.triangles) { - // Number of edges, number of boundary markers (zero or one). - writer.WriteLine("{0} {1}", mesh.NumberOfEdges, behavior.UseBoundaryMarkers ? "1" : "0"); - - long index = 0; - // To loop over the set of edges, loop over all triangles, and look at - // the three edges of each triangle. If there isn't another triangle - // adjacent to the edge, operate on the edge. If there is another - // adjacent triangle, operate on the edge only if the current triangle - // has a smaller pointer than its neighbor. This way, each edge is - // considered only once. - foreach (var item in mesh.triangles) - { - tri.tri = item; + tri.tri = item; - for (tri.orient = 0; tri.orient < 3; tri.orient++) + for (tri.orient = 0; tri.orient < 3; tri.orient++) + { + tri.Sym(ref trisym); + if ((tri.tri.id < trisym.tri.id) || (trisym.tri.id == Mesh.DUMMY)) { - tri.Sym(ref trisym); - if ((tri.tri.id < trisym.tri.id) || (trisym.tri.id == Mesh.DUMMY)) - { - p1 = tri.Org(); - p2 = tri.Dest(); + p1 = tri.Org(); + p2 = tri.Dest(); - if (behavior.UseBoundaryMarkers) + if (behavior.UseBoundaryMarkers) + { + // Edge number, indices of two endpoints, and a boundary marker. + // If there's no subsegment, the boundary marker is zero. + if (behavior.useSegments) { - // Edge number, indices of two endpoints, and a boundary marker. - // If there's no subsegment, the boundary marker is zero. - if (behavior.useSegments) + tri.Pivot(ref checkmark); + + if (checkmark.seg.hash == Mesh.DUMMY) { - tri.Pivot(ref checkmark); - - if (checkmark.seg.hash == Mesh.DUMMY) - { - writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, 0); - } - else - { - writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, - checkmark.seg.boundary); - } + writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, 0); } else { - writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, - trisym.tri.id == Mesh.DUMMY ? "1" : "0"); + writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, checkmark.seg.boundary); } } else { - // Edge number, indices of two endpoints. - writer.WriteLine("{0} {1} {2}", index, p1.id, p2.id); + writer.WriteLine("{0} {1} {2} {3}", index, p1.id, p2.id, trisym.tri.id == Mesh.DUMMY ? "1" : "0"); } - - index++; } + else + { + // Edge number, indices of two endpoints. + writer.WriteLine("{0} {1} {2}", index, p1.id, p2.id); + } + + index++; } } } @@ -427,34 +415,32 @@ public void WriteEdges(Mesh mesh, string filename) /// so the elements are numbered right! public void WriteNeighbors(Mesh mesh, string filename) { - Otri tri = default(Otri), trisym = default(Otri); + Otri tri = default, trisym = default; int n1, n2, n3; - int i = 0; + var i = 0; - using (StreamWriter writer = new StreamWriter(filename)) - { - // Number of triangles, three neighbors per triangle. - writer.WriteLine("{0} 3", mesh.triangles.Count); + using var writer = new StreamWriter(filename); + // Number of triangles, three neighbors per triangle. + writer.WriteLine("{0} 3", mesh.triangles.Count); - foreach (var item in mesh.triangles) - { - tri.tri = item; + foreach (var item in mesh.triangles) + { + tri.tri = item; - tri.orient = 1; - tri.Sym(ref trisym); - n1 = trisym.tri.id; + tri.orient = 1; + tri.Sym(ref trisym); + n1 = trisym.tri.id; - tri.orient = 2; - tri.Sym(ref trisym); - n2 = trisym.tri.id; + tri.orient = 2; + tri.Sym(ref trisym); + n2 = trisym.tri.id; - tri.orient = 0; - tri.Sym(ref trisym); - n3 = trisym.tri.id; + tri.orient = 0; + tri.Sym(ref trisym); + n3 = trisym.tri.id; - // Triangle number, neighboring triangle numbers. - writer.WriteLine("{0} {1} {2} {3}", i++, n1, n2, n3); - } + // Triangle number, neighboring triangle numbers. + writer.WriteLine("{0} {1} {2} {3}", i++, n1, n2, n3); } } } diff --git a/src/Triangle/IPredicates.cs b/src/Triangle/IPredicates.cs index 1c027a0..c03004a 100644 --- a/src/Triangle/IPredicates.cs +++ b/src/Triangle/IPredicates.cs @@ -1,7 +1,7 @@  namespace TriangleNet { - using TriangleNet.Geometry; + using Geometry; /// /// Geometric predicates interface. @@ -65,7 +65,6 @@ public interface IPredicates /// Relative coordinate of new location. /// Off-center constant. /// Coordinates of the circumcenter (or off-center) - Point FindCircumcenter(Point org, Point dest, Point apex, ref double xi, ref double eta, - double offconstant); + Point FindCircumcenter(Point org, Point dest, Point apex, ref double xi, ref double eta, double offconstant); } } diff --git a/src/Triangle/Log.cs b/src/Triangle/Log.cs index 14ccda3..36f961d 100644 --- a/src/Triangle/Log.cs +++ b/src/Triangle/Log.cs @@ -19,30 +19,25 @@ public enum LogLevel { Info, Warning, Error } /// public class LogItem { - private readonly DateTime time; - private readonly LogLevel level; - private readonly string message; - private readonly string details; - /// /// Gets the the item was logged. /// - public DateTime Time => time; + public DateTime Time { get; } /// /// Gets the . /// - public LogLevel Level => level; + public LogLevel Level { get; } /// /// Gets the log message. /// - public string Message => message; + public string Message { get; } /// /// Gets further details of the log message. /// - public string Details => details; + public string Details { get; } /// /// Creates a new instance of the class. @@ -61,11 +56,11 @@ public LogItem(LogLevel level, string message) /// The message details. public LogItem(LogLevel level, string message, string details) { - time = DateTime.Now; + Time = DateTime.Now; - this.level = level; - this.message = message; - this.details = details; + this.Level = level; + this.Message = message; + this.Details = details; } } @@ -91,8 +86,6 @@ public sealed class Log // Singleton pattern as proposed by Jon Skeet: // https://csharpindepth.com/Articles/Singleton - private static readonly Log instance = new Log(); - // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Log() { } @@ -102,7 +95,7 @@ private Log() { } /// /// Gets the instance. /// - public static Log Instance => instance; + public static Log Instance { get; } = new Log(); #endregion diff --git a/src/Triangle/Mesh.cs b/src/Triangle/Mesh.cs index a1efd7d..4729edf 100644 --- a/src/Triangle/Mesh.cs +++ b/src/Triangle/Mesh.cs @@ -9,12 +9,12 @@ namespace TriangleNet { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Meshing.Data; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Tools; - using TriangleNet.Topology; + using Geometry; + using Meshing; + using Meshing.Data; + using Meshing.Iterators; + using Tools; + using Topology; /// /// Mesh data structure. @@ -23,14 +23,14 @@ public class Mesh : IMesh { #region Variables - IPredicates predicates; + private readonly IPredicates predicates; - Log logger = Log.Instance; + private static Log logger => Log.Instance; - QualityMesher qualityMesher; + private QualityMesher? qualityMesher; // Stack that maintains a list of recently flipped triangles. - Stack flipstack; + private Stack flipstack; // TODO: Check if custom hashmap implementation could be faster. @@ -40,9 +40,9 @@ public class Mesh : IMesh internal Dictionary vertices; // Hash seeds (should belong to mesh instance) - internal int hash_vtx = 0; - internal int hash_seg = 0; - internal int hash_tri = 0; + internal int hash_vtx; + internal int hash_seg; + internal int hash_tri; internal List holes; internal List regions; @@ -50,7 +50,7 @@ public class Mesh : IMesh // TODO: remove mesh_dim, invertices and insegments // Other variables. - internal Rectangle bounds; // x and y bounds. + internal Rectangle? bounds; // x and y bounds. internal int invertices; // Number of input vertices. internal int insegments; // Number of input segments. internal int undeads; // Number of input vertices that don't appear in the mesh. @@ -63,7 +63,7 @@ public class Mesh : IMesh internal bool checkquality; // Has quality triangulation begun yet? // Triangular bounding box vertices. - internal Vertex infvertex1, infvertex2, infvertex3; + internal Vertex? infvertex1, infvertex2, infvertex3; internal TriangleLocator locator; @@ -80,7 +80,7 @@ public class Mesh : IMesh /// /// Gets the mesh bounding box. /// - public Rectangle Bounds => bounds; + public Rectangle? Bounds => bounds; /// /// Gets the mesh vertices. @@ -249,7 +249,7 @@ public void Refine(QualityOptions quality, bool delaunay = false) /// public void Renumber() { - this.Renumber(NodeNumbering.Linear); + Renumber(NodeNumbering.Linear); } /// @@ -258,7 +258,7 @@ public void Renumber() public void Renumber(NodeNumbering num) { // Don't need to do anything if the nodes are already numbered. - if (num == this.numbering) + if (num == numbering) { return; } @@ -268,7 +268,7 @@ public void Renumber(NodeNumbering num) if (num == NodeNumbering.Linear) { id = 0; - foreach (var node in this.vertices.Values) + foreach (var node in vertices.Values) { node.id = id++; } @@ -279,7 +279,7 @@ public void Renumber(NodeNumbering num) var iperm = rcm.Renumber(this); // Permute the node indices. - foreach (var node in this.vertices.Values) + foreach (var node in vertices.Values) { node.id = iperm[node.id]; } @@ -290,7 +290,7 @@ public void Renumber(NodeNumbering num) // Triangles will always be numbered from 0 to n-1 id = 0; - foreach (var item in this.triangles) + foreach (var item in triangles) { item.id = id++; } @@ -309,19 +309,19 @@ internal void SetQualityMesher(QualityMesher qmesher) internal void CopyTo(Mesh target) { - target.vertices = this.vertices; - target.triangles = this.triangles; - target.subsegs = this.subsegs; + target.vertices = vertices; + target.triangles = triangles; + target.subsegs = subsegs; - target.holes = this.holes; - target.regions = this.regions; + target.holes = holes; + target.regions = regions; - target.hash_vtx = this.hash_vtx; - target.hash_seg = this.hash_seg; - target.hash_tri = this.hash_tri; + target.hash_vtx = hash_vtx; + target.hash_seg = hash_seg; + target.hash_tri = hash_tri; - target.numbering = this.numbering; - target.hullsize = this.hullsize; + target.numbering = numbering; + target.hullsize = hullsize; } @@ -335,9 +335,9 @@ private void ResetData() holes.Clear(); regions.Clear(); - this.hash_vtx = 0; - this.hash_seg = 0; - this.hash_tri = 0; + hash_vtx = 0; + hash_seg = 0; + hash_tri = 0; flipstack.Clear(); @@ -395,7 +395,7 @@ private void TransferNodes(IList points) // Simple heuristic to check if ids are already set. We assume that if the // first two vertex ids are distinct, then all input vertices have pairwise // distinct ids. - bool userId = (v.id != points[1].id); + var userId = (v.id != points[1].id); foreach (var p in points) { @@ -429,10 +429,10 @@ private void TransferNodes(IList points) /// internal void MakeVertexMap() { - Otri tri = default(Otri); + var tri = default(Otri); Vertex triorg; - foreach (var t in this.triangles) + foreach (var t in triangles) { tri.tri = t; // Check all three vertices of the triangle. @@ -454,7 +454,7 @@ internal void MakeVertexMap() /// Reference to the new triangle. internal void MakeTriangle(ref Otri newotri) { - Triangle tri = triangles.Get(); + var tri = triangles.Get(); //tri.id = tri.hash; @@ -478,7 +478,7 @@ internal void MakeSegment(ref Osub newsubseg) { var seg = new SubSegment(); - seg.hash = this.hash_seg++; + seg.hash = hash_seg++; seg.subsegs[0].seg = dummysub; seg.subsegs[1].seg = dummysub; @@ -548,21 +548,21 @@ internal void MakeSegment(ref Osub newsubseg) internal InsertVertexResult InsertVertex(Vertex newvertex, ref Otri searchtri, ref Osub splitseg, bool segmentflaws, bool triflaws) { - Otri horiz = default(Otri); - Otri top = default(Otri); - Otri botleft = default(Otri), botright = default(Otri); - Otri topleft = default(Otri), topright = default(Otri); - Otri newbotleft = default(Otri), newbotright = default(Otri); - Otri newtopright = default(Otri); - Otri botlcasing = default(Otri), botrcasing = default(Otri); - Otri toplcasing = default(Otri), toprcasing = default(Otri); - Otri testtri = default(Otri); - Osub botlsubseg = default(Osub), botrsubseg = default(Osub); - Osub toplsubseg = default(Osub), toprsubseg = default(Osub); - Osub brokensubseg = default(Osub); - Osub checksubseg = default(Osub); - Osub rightsubseg = default(Osub); - Osub newsubseg = default(Osub); + var horiz = default(Otri); + var top = default(Otri); + Otri botleft = default, botright = default; + Otri topleft = default, topright = default; + Otri newbotleft = default, newbotright = default; + var newtopright = default(Otri); + Otri botlcasing = default, botrcasing = default; + Otri toplcasing = default, toprcasing = default; + var testtri = default(Otri); + Osub botlsubseg = default, botrsubseg = default; + Osub toplsubseg = default, toprsubseg = default; + var brokensubseg = default(Osub); + var checksubseg = default(Osub); + var rightsubseg = default(Osub); + var newsubseg = default(Osub); BadSubseg encroached; //FlipStacker newflip; Vertex first; @@ -774,7 +774,7 @@ internal InsertVertexResult InsertVertex(Vertex newvertex, ref Otri searchtri, { flipstack.Clear(); - flipstack.Push(default(Otri)); // Dummy flip (see UndoVertex) + flipstack.Push(default); // Dummy flip (see UndoVertex) flipstack.Push(horiz); } @@ -1068,7 +1068,7 @@ internal InsertVertexResult InsertVertex(Vertex newvertex, ref Otri searchtri, // We're done. Return a triangle whose origin is the new vertex. horiz.Lnext(ref searchtri); - Otri recenttri = default(Otri); + var recenttri = default(Otri); horiz.Lnext(ref recenttri); locator.Update(ref recenttri); @@ -1092,8 +1092,8 @@ internal InsertVertexResult InsertVertex(Vertex newvertex, ref Otri searchtri, /// subsegment and, if appropriate, its vertices. internal void InsertSubseg(ref Otri tri, int subsegmark) { - Otri oppotri = default(Otri); - Osub newsubseg = default(Osub); + var oppotri = default(Otri); + var newsubseg = default(Osub); Vertex triorg, tridest; triorg = tri.Org(); @@ -1176,13 +1176,13 @@ internal void InsertSubseg(ref Otri tri, int subsegmark) /// internal void Flip(ref Otri flipedge) { - Otri botleft = default(Otri), botright = default(Otri); - Otri topleft = default(Otri), topright = default(Otri); - Otri top = default(Otri); - Otri botlcasing = default(Otri), botrcasing = default(Otri); - Otri toplcasing = default(Otri), toprcasing = default(Otri); - Osub botlsubseg = default(Osub), botrsubseg = default(Osub); - Osub toplsubseg = default(Osub), toprsubseg = default(Osub); + Otri botleft = default, botright = default; + Otri topleft = default, topright = default; + var top = default(Otri); + Otri botlcasing = default, botrcasing = default; + Otri toplcasing = default, toprcasing = default; + Osub botlsubseg = default, botrsubseg = default; + Osub toplsubseg = default, toprsubseg = default; Vertex leftvertex, rightvertex, botvertex; Vertex farvertex; @@ -1192,26 +1192,6 @@ internal void Flip(ref Otri flipedge) botvertex = flipedge.Apex(); flipedge.Sym(ref top); - // SELF CHECK - - //if (top.triangle.id == DUMMY) - //{ - // logger.Error("Attempt to flip on boundary.", "Mesh.Flip()"); - // flipedge.LnextSelf(); - // return; - //} - - //if (checksegments) - //{ - // flipedge.SegPivot(ref toplsubseg); - // if (toplsubseg.ss != Segment.Empty) - // { - // logger.Error("Attempt to flip a segment.", "Mesh.Flip()"); - // flipedge.LnextSelf(); - // return; - // } - //} - farvertex = top.Apex(); // Identify the casing of the quadrilateral. @@ -1299,13 +1279,13 @@ internal void Flip(ref Otri flipedge) /// internal void Unflip(ref Otri flipedge) { - Otri botleft = default(Otri), botright = default(Otri); - Otri topleft = default(Otri), topright = default(Otri); - Otri top = default(Otri); - Otri botlcasing = default(Otri), botrcasing = default(Otri); - Otri toplcasing = default(Otri), toprcasing = default(Otri); - Osub botlsubseg = default(Osub), botrsubseg = default(Osub); - Osub toplsubseg = default(Osub), toprsubseg = default(Osub); + Otri botleft = default, botright = default; + Otri topleft = default, topright = default; + var top = default(Otri); + Otri botlcasing = default, botrcasing = default; + Otri toplcasing = default, toprcasing = default; + Osub botlsubseg = default, botrsubseg = default; + Osub toplsubseg = default, toprsubseg = default; Vertex leftvertex, rightvertex, botvertex; Vertex farvertex; @@ -1445,17 +1425,21 @@ internal void Unflip(ref Otri flipedge) /// there's a lot of long segments that each cut many triangles. I ought to /// code a faster algorithm some day. /// - private void TriangulatePolygon(Otri firstedge, Otri lastedge, - int edgecount, bool doflip, bool triflaws) + private void TriangulatePolygon( + Otri firstedge, + Otri lastedge, + int edgecount, + bool doflip, + bool triflaws) { - Otri testtri = default(Otri); - Otri besttri = default(Otri); - Otri tempedge = default(Otri); + var testtri = default(Otri); + var besttri = default(Otri); + var tempedge = default(Otri); Vertex leftbasevertex, rightbasevertex; Vertex testvertex; Vertex bestvertex; - int bestnumber = 1; + var bestnumber = 1; // Identify the base vertices. leftbasevertex = lastedge.Apex(); @@ -1466,7 +1450,7 @@ private void TriangulatePolygon(Otri firstedge, Otri lastedge, bestvertex = besttri.Dest(); besttri.Copy(ref testtri); - for (int i = 2; i <= edgecount - 2; i++) + for (var i = 2; i <= edgecount - 2; i++) { testtri.Onext(); testvertex = testtri.Dest(); @@ -1524,12 +1508,12 @@ private void TriangulatePolygon(Otri firstedge, Otri lastedge, /// internal void DeleteVertex(ref Otri deltri) { - Otri countingtri = default(Otri); - Otri firstedge = default(Otri), lastedge = default(Otri); - Otri deltriright = default(Otri); - Otri lefttri = default(Otri), righttri = default(Otri); - Otri leftcasing = default(Otri), rightcasing = default(Otri); - Osub leftsubseg = default(Osub), rightsubseg = default(Osub); + var countingtri = default(Otri); + Otri firstedge = default, lastedge = default; + var deltriright = default(Otri); + Otri lefttri = default, righttri = default; + Otri leftcasing = default, rightcasing = default; + Osub leftsubseg = default, rightsubseg = default; Vertex delvertex; Vertex neworg; int edgecount; @@ -1601,10 +1585,10 @@ internal void UndoVertex() { Otri fliptri; - Otri botleft = default(Otri), botright = default(Otri), topright = default(Otri); - Otri botlcasing = default(Otri), botrcasing = default(Otri), toprcasing = default(Otri); - Otri gluetri = default(Otri); - Osub botlsubseg = default(Osub), botrsubseg = default(Osub), toprsubseg = default(Osub); + Otri botleft = default, botright = default, topright = default; + Otri botlcasing = default, botrcasing = default, toprcasing = default; + var gluetri = default(Otri); + Osub botlsubseg = default, botrsubseg = default, toprsubseg = default; Vertex botvertex, rightvertex; // Walk through the list of transformations (flips and a vertex insertion) diff --git a/src/Triangle/MeshValidator.cs b/src/Triangle/MeshValidator.cs index e2d9bcb..313fdbe 100644 --- a/src/Triangle/MeshValidator.cs +++ b/src/Triangle/MeshValidator.cs @@ -9,10 +9,10 @@ namespace TriangleNet { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Tools; - using TriangleNet.Topology; + using Geometry; + using Meshing; + using Tools; + using Topology; /// /// Mesh validation helper. @@ -28,18 +28,18 @@ public static class MeshValidator /// True, if mesh is topologically consistent. public static bool IsConsistent(Mesh mesh) { - Otri tri = default(Otri); - Otri oppotri = default(Otri), oppooppotri = default(Otri); + var tri = default(Otri); + Otri oppotri = default, oppooppotri = default; Vertex org, dest, apex; Vertex oppoorg, oppodest; var logger = Log.Instance; // Temporarily turn on exact arithmetic if it's off. - bool saveexact = Behavior.NoExact; + var saveexact = Behavior.NoExact; Behavior.NoExact = false; - int horrors = 0; + var horrors = 0; // Run through the list of triangles, checking each one. foreach (var t in mesh.triangles) @@ -144,9 +144,9 @@ public static bool IsConstrainedDelaunay(Mesh mesh) /// private static bool IsDelaunay(Mesh mesh, bool constrained) { - Otri loop = default(Otri); - Otri oppotri = default(Otri); - Osub opposubseg = default(Osub); + var loop = default(Otri); + var oppotri = default(Otri); + var opposubseg = default(Osub); Vertex org, dest, apex; Vertex oppoapex; @@ -155,10 +155,10 @@ private static bool IsDelaunay(Mesh mesh, bool constrained) var logger = Log.Instance; // Temporarily turn on exact arithmetic if it's off. - bool saveexact = Behavior.NoExact; + var saveexact = Behavior.NoExact; Behavior.NoExact = false; - int horrors = 0; + var horrors = 0; var inf1 = mesh.infvertex1; var inf2 = mesh.infvertex2; @@ -239,7 +239,7 @@ public static IEnumerable GetDegenerateBoundaryTriangles(IMesh mesh, foreach (var triangle in mesh.Triangles) { - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { var neighbor = triangle.GetNeighbor(i); diff --git a/src/Triangle/Meshing/Algorithm/Dwyer.cs b/src/Triangle/Meshing/Algorithm/Dwyer.cs index 7d4425e..94e01d2 100644 --- a/src/Triangle/Meshing/Algorithm/Dwyer.cs +++ b/src/Triangle/Meshing/Algorithm/Dwyer.cs @@ -9,9 +9,9 @@ namespace TriangleNet.Meshing.Algorithm { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Tools; - using TriangleNet.Topology; + using Geometry; + using Tools; + using Topology; /// /// Builds a delaunay triangulation using the divide-and-conquer algorithm. @@ -46,15 +46,15 @@ namespace TriangleNet.Meshing.Algorithm /// public class Dwyer : ITriangulator { - IPredicates predicates; + private IPredicates predicates; /// /// Gets or sets a value indicating whether to use alternating cuts (default = true). /// public bool UseDwyer = true; - Vertex[] sortarray; - Mesh mesh; + private Vertex[] sortarray; + private Mesh mesh; /// /// Compute a Delaunay triangulation by the divide-and-conquer method. @@ -70,7 +70,7 @@ public IMesh Triangulate(IList points, Configuration config) mesh = new Mesh(config, points); - Otri hullleft = default(Otri), hullright = default(Otri); + Otri hullleft = default, hullright = default; int i, j, n = points.Count; // Allocate an array of pointers to vertices for sorting. @@ -157,14 +157,14 @@ public IMesh Triangulate(IList points, Configuration config) /// merged triangulation, and the destination of 'farright' is the rightmost /// vertex. /// - void MergeHulls(ref Otri farleft, ref Otri innerleft, ref Otri innerright, + private void MergeHulls(ref Otri farleft, ref Otri innerleft, ref Otri innerright, ref Otri farright, int axis) { - Otri leftcand = default(Otri), rightcand = default(Otri); - Otri nextedge = default(Otri); - Otri sidecasing = default(Otri), topcasing = default(Otri), outercasing = default(Otri); - Otri checkedge = default(Otri); - Otri baseedge = default(Otri); + Otri leftcand = default, rightcand = default; + var nextedge = default(Otri); + Otri sidecasing = default, topcasing = default, outercasing = default; + var checkedge = default(Otri); + var baseedge = default(Otri); Vertex innerleftdest; Vertex innerrightorg; Vertex innerleftapex, innerrightapex; @@ -484,16 +484,16 @@ void MergeHulls(ref Otri farleft, ref Otri innerleft, ref Otri innerright, /// 'farright' is the rightmost vertex (breaking ties by choosing the /// lowest rightmost vertex). /// - void DivconqRecurse(int left, int right, int axis, + private void DivconqRecurse(int left, int right, int axis, ref Otri farleft, ref Otri farright) { - Otri midtri = default(Otri); - Otri tri1 = default(Otri); - Otri tri2 = default(Otri); - Otri tri3 = default(Otri); - Otri innerleft = default(Otri), innerright = default(Otri); + var midtri = default(Otri); + var tri1 = default(Otri); + var tri2 = default(Otri); + var tri3 = default(Otri); + Otri innerleft = default, innerright = default; double area; - int vertices = right - left + 1; + var vertices = right - left + 1; int divider; if (vertices == 2) @@ -638,16 +638,16 @@ void DivconqRecurse(int left, int right, int axis, /// /// /// Number of vertices on the hull. - int RemoveGhosts(ref Otri startghost) + private int RemoveGhosts(ref Otri startghost) { - Otri searchedge = default(Otri); - Otri dissolveedge = default(Otri); - Otri deadtriangle = default(Otri); + var searchedge = default(Otri); + var dissolveedge = default(Otri); + var deadtriangle = default(Otri); Vertex markorg; int hullsize; - bool noPoly = !mesh.behavior.Poly; + var noPoly = !mesh.behavior.Poly; // Find an edge on the convex hull to start point location from. startghost.Lprev(ref searchedge); diff --git a/src/Triangle/Meshing/Algorithm/Incremental.cs b/src/Triangle/Meshing/Algorithm/Incremental.cs index 100bb9d..e80c36e 100644 --- a/src/Triangle/Meshing/Algorithm/Incremental.cs +++ b/src/Triangle/Meshing/Algorithm/Incremental.cs @@ -8,15 +8,15 @@ namespace TriangleNet.Meshing.Algorithm { using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Builds a delaunay triangulation using the incremental algorithm. /// public class Incremental : ITriangulator { - Mesh mesh; + private Mesh mesh; /// /// Compute a Delaunay triangulation by incrementally inserting vertices. @@ -26,7 +26,7 @@ public IMesh Triangulate(IList points, Configuration config) { mesh = new Mesh(config, points); - Otri starttri = new Otri(); + var starttri = new Otri(); // Create a triangular bounding box. GetBoundingBox(); @@ -34,7 +34,7 @@ public IMesh Triangulate(IList points, Configuration config) foreach (var v in mesh.vertices.Values) { starttri.tri = mesh.dummytri; - Osub tmp = default(Osub); + var tmp = default(Osub); if (mesh.InsertVertex(v, ref starttri, ref tmp, false, false) == InsertVertexResult.Duplicate) { if (Log.Verbose) @@ -61,13 +61,13 @@ public IMesh Triangulate(IList points, Configuration config) /// used by the point location routines, but (mostly) ignored by the /// Delaunay edge flip routines. /// - void GetBoundingBox() + private void GetBoundingBox() { - Otri inftri = default(Otri); // Handle for the triangular bounding box. - Rectangle box = mesh.bounds; + var inftri = default(Otri); // Handle for the triangular bounding box. + var box = mesh.bounds; // Find the width (or height, whichever is larger) of the triangulation. - double width = box.Width; + var width = box.Width; if (box.Height > width) { width = box.Height; @@ -103,16 +103,16 @@ void GetBoundingBox() /// the three bounding box vertices (one triangle for each edge of the /// convex hull of the inner mesh). This routine removes these triangles. /// - int RemoveBox() + private int RemoveBox() { - Otri deadtriangle = default(Otri); - Otri searchedge = default(Otri); - Otri checkedge = default(Otri); - Otri nextedge = default(Otri), finaledge = default(Otri), dissolveedge = default(Otri); + var deadtriangle = default(Otri); + var searchedge = default(Otri); + var checkedge = default(Otri); + Otri nextedge = default, finaledge = default, dissolveedge = default; Vertex markorg; int hullsize; - bool noPoly = !mesh.behavior.Poly; + var noPoly = !mesh.behavior.Poly; // Find a boundary triangle. nextedge.tri = mesh.dummytri; diff --git a/src/Triangle/Meshing/Algorithm/SweepLine.cs b/src/Triangle/Meshing/Algorithm/SweepLine.cs index 4362fb5..545916d 100644 --- a/src/Triangle/Meshing/Algorithm/SweepLine.cs +++ b/src/Triangle/Meshing/Algorithm/SweepLine.cs @@ -9,29 +9,29 @@ namespace TriangleNet.Meshing.Algorithm { using System; using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; - using TriangleNet.Tools; + using Topology; + using Geometry; + using Tools; /// /// Builds a delaunay triangulation using the sweepline algorithm. /// public class SweepLine : ITriangulator { - static int randomseed = 1; - static int SAMPLERATE = 10; + private static int randomseed = 1; + private static int SAMPLERATE = 10; - static int randomnation(int choices) + private static int randomnation(int choices) { randomseed = (randomseed * 1366 + 150889) % 714025; return randomseed / (714025 / choices + 1); } - IPredicates predicates; + private IPredicates predicates; - Mesh mesh; - double xminextreme; // Nonexistent x value used as a flag in sweepline. - List splaynodes; + private Mesh mesh; + private double xminextreme; // Nonexistent x value used as a flag in sweepline. + private List splaynodes; /// /// Compute a Delaunay triangulation by the sweepline method. @@ -51,14 +51,14 @@ public IMesh Triangulate(IList points, Configuration config) SweepEvent nextevent; SweepEvent newevent; SplayNode splayroot; - Otri bottommost = default(Otri); - Otri searchtri = default(Otri); + var bottommost = default(Otri); + var searchtri = default(Otri); Otri fliptri; - Otri lefttri = default(Otri); - Otri righttri = default(Otri); - Otri farlefttri = default(Otri); - Otri farrighttri = default(Otri); - Otri inserttri = default(Otri); + var lefttri = default(Otri); + var righttri = default(Otri); + var farlefttri = default(Otri); + var farrighttri = default(Otri); + var inserttri = default(Otri); Vertex firstvertex, secondvertex; Vertex nextvertex, lastvertex; Vertex connectvertex; @@ -252,14 +252,14 @@ public IMesh Triangulate(IList points, Configuration config) splaynodes.Clear(); bottommost.Lprev(); - this.mesh.hullsize = RemoveGhosts(ref bottommost); + mesh.hullsize = RemoveGhosts(ref bottommost); - return this.mesh; + return mesh; } #region Heap - void HeapInsert(SweepEvent[] heap, int heapsize, SweepEvent newevent) + private void HeapInsert(SweepEvent[] heap, int heapsize, SweepEvent newevent) { double eventx, eventy; int eventnum; @@ -292,7 +292,7 @@ void HeapInsert(SweepEvent[] heap, int heapsize, SweepEvent newevent) newevent.heapposition = eventnum; } - void Heapify(SweepEvent[] heap, int heapsize, int eventnum) + private void Heapify(SweepEvent[] heap, int heapsize, int eventnum) { SweepEvent thisevent; double eventx, eventy; @@ -345,7 +345,7 @@ void Heapify(SweepEvent[] heap, int heapsize, int eventnum) } } - void HeapDelete(SweepEvent[] heap, int heapsize, int eventnum) + private void HeapDelete(SweepEvent[] heap, int heapsize, int eventnum) { SweepEvent moveevent; double eventx, eventy; @@ -381,7 +381,7 @@ void HeapDelete(SweepEvent[] heap, int heapsize, int eventnum) Heapify(heap, heapsize - 1, eventnum); } - void CreateHeap(out SweepEvent[] eventheap, int size) + private void CreateHeap(out SweepEvent[] eventheap, int size) { Vertex thisvertex; int maxevents; @@ -407,7 +407,7 @@ void CreateHeap(out SweepEvent[] eventheap, int size) #region Splaytree - SplayNode Splay(SplayNode splaytree, Point searchpoint, ref Otri searchtri) + private SplayNode Splay(SplayNode splaytree, Point searchpoint, ref Otri searchtri) { SplayNode child, grandchild; SplayNode lefttree, righttree; @@ -551,7 +551,7 @@ SplayNode Splay(SplayNode splaytree, Point searchpoint, ref Otri searchtri) } } - SplayNode SplayInsert(SplayNode splayroot, Otri newkey, Point searchpoint) + private SplayNode SplayInsert(SplayNode splayroot, Otri newkey, Point searchpoint) { SplayNode newsplaynode; @@ -579,7 +579,7 @@ SplayNode SplayInsert(SplayNode splayroot, Otri newkey, Point searchpoint) return newsplaynode; } - SplayNode FrontLocate(SplayNode splayroot, Otri bottommost, Vertex searchvertex, + private SplayNode FrontLocate(SplayNode splayroot, Otri bottommost, Vertex searchvertex, ref Otri searchtri, ref bool farright) { bool farrightflag; @@ -597,14 +597,14 @@ SplayNode FrontLocate(SplayNode splayroot, Otri bottommost, Vertex searchvertex, return splayroot; } - SplayNode CircleTopInsert(SplayNode splayroot, Otri newkey, + private SplayNode CircleTopInsert(SplayNode splayroot, Otri newkey, Vertex pa, Vertex pb, Vertex pc, double topy) { double ccwabc; double xac, yac, xbc, ybc; double aclen2, bclen2; - Point searchpoint = new Point(); // TODO: mesh.nextras - Otri dummytri = default(Otri); + var searchpoint = new Point(); // TODO: mesh.nextras + var dummytri = default(Otri); ccwabc = predicates.CounterClockwise(pa, pb, pc); xac = pa.x - pc.x; @@ -620,7 +620,7 @@ SplayNode CircleTopInsert(SplayNode splayroot, Otri newkey, #endregion - bool RightOfHyperbola(ref Otri fronttri, Point newsite) + private bool RightOfHyperbola(ref Otri fronttri, Point newsite) { Vertex leftvertex, rightvertex; double dxa, dya, dxb, dyb; @@ -652,7 +652,7 @@ bool RightOfHyperbola(ref Otri fronttri, Point newsite) return dya * (dxb * dxb + dyb * dyb) > dyb * (dxa * dxa + dya * dya); } - double CircleTop(Vertex pa, Vertex pb, Vertex pc, double ccwabc) + private double CircleTop(Vertex pa, Vertex pb, Vertex pc, double ccwabc) { double xac, yac, xbc, ybc, xab, yab; double aclen2, bclen2, ablen2; @@ -671,11 +671,11 @@ double CircleTop(Vertex pa, Vertex pb, Vertex pc, double ccwabc) return pc.y + (xac * bclen2 - xbc * aclen2 + Math.Sqrt(aclen2 * bclen2 * ablen2)) / (2.0 * ccwabc); } - void Check4DeadEvent(ref Otri checktri, SweepEvent[] eventheap, ref int heapsize) + private void Check4DeadEvent(ref Otri checktri, SweepEvent[] eventheap, ref int heapsize) { SweepEvent deadevent; SweepEventVertex eventvertex; - int eventnum = -1; + var eventnum = -1; eventvertex = checktri.Org() as SweepEventVertex; if (eventvertex != null) @@ -694,15 +694,15 @@ void Check4DeadEvent(ref Otri checktri, SweepEvent[] eventheap, ref int heapsize /// /// /// Number of vertices on the hull. - int RemoveGhosts(ref Otri startghost) + private int RemoveGhosts(ref Otri startghost) { - Otri searchedge = default(Otri); - Otri dissolveedge = default(Otri); - Otri deadtriangle = default(Otri); + var searchedge = default(Otri); + var dissolveedge = default(Otri); + var deadtriangle = default(Otri); Vertex markorg; int hullsize; - bool noPoly = !mesh.behavior.Poly; + var noPoly = !mesh.behavior.Poly; var dummytri = mesh.dummytri; @@ -759,7 +759,7 @@ int RemoveGhosts(ref Otri startghost) /// separate array. To distinguish site events from circle events, all circle events are /// given an invalid (smaller than 'xmin') x-coordinate 'xkey'. /// - class SweepEvent + private class SweepEvent { public double xkey, ykey; // Coordinates of the event. public Vertex vertexEvent; // Vertex event. @@ -772,7 +772,7 @@ class SweepEvent /// to handle the pointer magic of the original code (casting a sweep event /// to vertex etc.). /// - class SweepEventVertex : Vertex + private class SweepEventVertex : Vertex { public SweepEvent evt; @@ -798,7 +798,7 @@ public SweepEventVertex(SweepEvent e) /// been rotated (due to a circle event), it no longer represents a boundary /// edge and should be deleted. /// - class SplayNode + private class SplayNode { public Otri keyedge; // Lprev of an edge on the front. public Vertex keydest; // Used to verify that splay node is still live. diff --git a/src/Triangle/Meshing/ConstraintMesher.cs b/src/Triangle/Meshing/ConstraintMesher.cs index 211e543..696104a 100644 --- a/src/Triangle/Meshing/ConstraintMesher.cs +++ b/src/Triangle/Meshing/ConstraintMesher.cs @@ -9,31 +9,31 @@ namespace TriangleNet.Meshing { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing.Iterators; - using TriangleNet.Topology; + using Geometry; + using Iterators; + using Topology; internal class ConstraintMesher { - IPredicates predicates; + private IPredicates predicates; - Mesh mesh; - Behavior behavior; - TriangleLocator locator; + private Mesh mesh; + private Behavior behavior; + private TriangleLocator locator; - List viri; + private List viri; - Log logger = Log.Instance; + private Log logger = Log.Instance; public ConstraintMesher(Mesh mesh, Configuration config) { this.mesh = mesh; - this.predicates = config.Predicates(); + predicates = config.Predicates(); - this.behavior = mesh.behavior; - this.locator = mesh.locator; + behavior = mesh.behavior; + locator = mesh.locator; - this.viri = new List(); + viri = new List(); } @@ -42,12 +42,12 @@ public ConstraintMesher(Mesh mesh, Configuration config) /// /// The polygon. /// Constraint options. - public void Apply(IPolygon input, ConstraintOptions options) + public void Apply(IPolygon input, ConstraintOptions? options) { behavior.Poly = input.Segments.Count > 0; // Copy constraint options - if (options != null) + if (options is not null) { behavior.ConformingDelaunay = options.ConformingDelaunay; behavior.Convex = options.Convex; @@ -99,11 +99,11 @@ public void Apply(IPolygon input, ConstraintOptions options) /// private void CarveHoles() { - Otri searchtri = default(Otri); - Vertex searchorg, searchdest; + var searchtri = default(Otri); + Vertex? searchorg, searchdest; LocateResult intersect; - Triangle[] regionTris = null; + Triangle[] regionTris = {}; var dummytri = mesh.dummytri; @@ -153,7 +153,7 @@ private void CarveHoles() // which might not be convex; they can only be used with a freshly triangulated PSLG.) if (mesh.regions.Count > 0) { - int i = 0; + var i = 0; regionTris = new Triangle[mesh.regions.Count]; @@ -202,7 +202,7 @@ private void CarveHoles() { var iterator = new RegionIterator(mesh); - for (int i = 0; i < regionTris.Length; i++) + for (var i = 0; i < regionTris.Length; i++) { if (regionTris[i].id != Mesh.DUMMY) { @@ -287,10 +287,10 @@ private void FormSkeleton(IPolygon input) /// private void InfectHull() { - Otri hulltri = default(Otri); - Otri nexttri = default(Otri); - Otri starttri = default(Otri); - Osub hullsubseg = default(Osub); + var hulltri = default(Otri); + var nexttri = default(Otri); + var starttri = default(Otri); + var hullsubseg = default(Osub); Vertex horg, hdest; var dummytri = mesh.dummytri; @@ -365,11 +365,11 @@ private void InfectHull() /// The second phase actually eliminates the infected triangles. It also /// eliminates orphaned vertices. /// - void Plague() + private void Plague() { - Otri testtri = default(Otri); - Otri neighbor = default(Otri); - Osub neighborsubseg = default(Osub); + var testtri = default(Otri); + var neighbor = default(Otri); + var neighborsubseg = default(Osub); Vertex testvertex; Vertex norg, ndest; @@ -380,7 +380,7 @@ void Plague() // Loop through all the infected triangles, spreading the virus to // their neighbors, then to their neighbors' neighbors. - for (int i = 0; i < viri.Count; i++) + for (var i = 0; i < viri.Count; i++) { // WARNING: Don't use foreach, mesh.viri list may get modified. @@ -573,7 +573,7 @@ void Plague() /// private FindDirectionResult FindDirection(ref Otri searchtri, Vertex searchpoint) { - Otri checktri = default(Otri); + var checktri = default(Otri); Vertex startvertex; Vertex leftvertex, rightvertex; double leftccw, rightccw; @@ -662,7 +662,7 @@ private FindDirectionResult FindDirection(ref Otri searchtri, Vertex searchpoint /// private void SegmentIntersection(ref Otri splittri, ref Osub splitsubseg, Vertex endpoint2) { - Osub opposubseg = default(Osub); + var opposubseg = default(Osub); Vertex endpoint1; Vertex torg, tdest; Vertex leftvertex, rightvertex; @@ -795,8 +795,8 @@ private void SegmentIntersection(ref Otri splittri, ref Osub splitsubseg, Vertex /// private bool ScoutSegment(ref Otri searchtri, Vertex endpoint2, int newmark) { - Otri crosstri = default(Otri); - Osub crosssubseg = default(Osub); + var crosstri = default(Otri); + var crosssubseg = default(Osub); Vertex leftvertex, rightvertex; FindDirectionResult collinear; @@ -891,9 +891,9 @@ private bool ScoutSegment(ref Otri searchtri, Vertex endpoint2, int newmark) /// private void DelaunayFixup(ref Otri fixuptri, bool leftside) { - Otri neartri = default(Otri); - Otri fartri = default(Otri); - Osub faredge = default(Osub); + var neartri = default(Otri); + var fartri = default(Otri); + var faredge = default(Osub); Vertex nearvertex, leftvertex, rightvertex, farvertex; fixuptri.Lnext(ref neartri); @@ -1008,8 +1008,8 @@ private void DelaunayFixup(ref Otri fixuptri, bool leftside) /// private void ConstrainedEdge(ref Otri starttri, Vertex endpoint2, int newmark) { - Otri fixuptri = default(Otri), fixuptri2 = default(Otri); - Osub crosssubseg = default(Osub); + Otri fixuptri = default, fixuptri2 = default; + var crosssubseg = default(Osub); Vertex endpoint1; Vertex farvertex; double area; @@ -1113,7 +1113,7 @@ private void ConstrainedEdge(ref Otri starttri, Vertex endpoint2, int newmark) /// private void InsertSegment(Vertex endpoint1, Vertex endpoint2, int newmark) { - Otri searchtri1 = default(Otri), searchtri2 = default(Otri); + Otri searchtri1 = default, searchtri2 = default; Vertex checkvertex = null; var dummytri = mesh.dummytri; @@ -1194,9 +1194,9 @@ private void InsertSegment(Vertex endpoint1, Vertex endpoint2, int newmark) /// private void MarkHull() { - Otri hulltri = default(Otri); - Otri nexttri = default(Otri); - Otri starttri = default(Otri); + var hulltri = default(Otri); + var nexttri = default(Otri); + var starttri = default(Otri); // Find a triangle handle on the hull. hulltri.tri = mesh.dummytri; diff --git a/src/Triangle/Meshing/Converter.cs b/src/Triangle/Meshing/Converter.cs index 7d3f0ce..0fd1937 100644 --- a/src/Triangle/Meshing/Converter.cs +++ b/src/Triangle/Meshing/Converter.cs @@ -10,12 +10,12 @@ namespace TriangleNet.Meshing using System; using System.Collections.Generic; using System.Linq; - using TriangleNet.Geometry; - using TriangleNet.Topology; - using TriangleNet.Topology.DCEL; + using Geometry; + using Topology; + using Topology.DCEL; - using HVertex = TriangleNet.Topology.DCEL.Vertex; - using TVertex = TriangleNet.Geometry.Vertex; + using HVertex = Topology.DCEL.Vertex; + using TVertex = Geometry.Vertex; /// /// The Converter class provides methods for mesh reconstruction and conversion. @@ -27,7 +27,7 @@ public class Converter /// /// Gets the instance. /// - public static Converter Instance { get { return lazy.Value; } } + public static Converter Instance => lazy.Value; private Converter() { @@ -46,13 +46,15 @@ public Mesh ToMesh(Polygon polygon, ICollection triangles) /// /// Reconstruct a triangulation from its raw data representation. /// - public Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration config = null) + public Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration? config = null) { - Otri tri = default(Otri); - Osub subseg = default(Osub); + if (triangles is null) throw new ArgumentNullException(nameof(triangles)); - int elements = triangles == null ? 0 : triangles.Length; - int segments = polygon.Segments.Count; + var tri = default(Otri); + var subseg = default(Osub); + + var elements = triangles.Length; + var segments = polygon.Segments.Count; var mesh = new Mesh(config ?? new Configuration(), polygon.Points); @@ -66,7 +68,7 @@ public Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration config } // Create the triangles. - for (int i = 0; i < elements; i++) + for (var i = 0; i < elements; i++) { mesh.MakeTriangle(ref tri); } @@ -76,7 +78,7 @@ public Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration config mesh.insegments = segments; // Create the subsegments. - for (int i = 0; i < segments; i++) + for (var i = 0; i < segments; i++) { mesh.MakeSegment(ref subseg); } @@ -95,14 +97,14 @@ public Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration config /// private List[] SetNeighbors(Mesh mesh, ITriangle[] triangles) { - Otri tri = default(Otri); - Otri triangleleft = default(Otri); - Otri checktri = default(Otri); - Otri checkleft = default(Otri); + var tri = default(Otri); + var triangleleft = default(Otri); + var checktri = default(Otri); + var checkleft = default(Otri); Otri nexttri; TVertex tdest, tapex; TVertex checkdest, checkapex; - int[] corner = new int[3]; + var corner = new int[3]; int aroundvertex; int i; @@ -112,7 +114,7 @@ private List[] SetNeighbors(Mesh mesh, ITriangle[] triangles) // Each vertex is initially unrepresented. for (i = 0; i < mesh.vertices.Count; i++) { - Otri tmp = default(Otri); + var tmp = default(Otri); tmp.tri = mesh.dummytri; vertexarray[i] = new List(3); vertexarray[i].Add(tmp); @@ -127,7 +129,7 @@ private List[] SetNeighbors(Mesh mesh, ITriangle[] triangles) tri.tri = item; // Copy the triangle's three corners. - for (int j = 0; j < 3; j++) + for (var j = 0; j < 3; j++) { corner[j] = triangles[i].GetVertexID(j); @@ -159,7 +161,7 @@ private List[] SetNeighbors(Mesh mesh, ITriangle[] triangles) // Take the number for the origin of triangleloop. aroundvertex = corner[tri.orient]; - int index = vertexarray[aroundvertex].Count - 1; + var index = vertexarray[aroundvertex].Count - 1; // Look for other triangles having this vertex. nexttri = vertexarray[aroundvertex][index]; @@ -212,11 +214,11 @@ private List[] SetNeighbors(Mesh mesh, ITriangle[] triangles) /// private void SetSegments(Mesh mesh, Polygon polygon, List[] vertexarray) { - Otri checktri = default(Otri); + var checktri = default(Otri); Otri nexttri; // Triangle TVertex checkdest; - Otri checkneighbor = default(Otri); - Osub subseg = default(Osub); + var checkneighbor = default(Otri); + var subseg = default(Osub); Otri prevlink; // Triangle TVertex tmp; @@ -229,7 +231,7 @@ private void SetSegments(Mesh mesh, Polygon polygon, List[] vertexarray) int aroundvertex; int i; - int hullsize = 0; + var hullsize = 0; // Prepare to count the boundary edges. if (mesh.behavior.Poly) @@ -264,7 +266,7 @@ private void SetSegments(Mesh mesh, Polygon polygon, List[] vertexarray) // Take the number for the destination of subsegloop. aroundvertex = subseg.orient == 1 ? sorg.id : sdest.id; - int index = vertexarray[aroundvertex].Count - 1; + var index = vertexarray[aroundvertex].Count - 1; // Look for triangles having this vertex. prevlink = vertexarray[aroundvertex][index]; @@ -321,7 +323,7 @@ private void SetSegments(Mesh mesh, Polygon polygon, List[] vertexarray) for (i = 0; i < mesh.vertices.Count; i++) { // Search the stack of triangles adjacent to a vertex. - int index = vertexarray[i].Count - 1; + var index = vertexarray[i].Count - 1; nexttri = vertexarray[i][index]; checktri = nexttri; @@ -393,7 +395,7 @@ public DcelMesh ToDCEL(Mesh mesh) map[t.id] = new List(3); } - Otri tri = default(Otri), neighbor = default(Otri); + Otri tri = default, neighbor = default; TVertex org, dest; int id, nid, count = mesh.triangles.Count; @@ -403,7 +405,7 @@ public DcelMesh ToDCEL(Mesh mesh) var edges = dcel.HalfEdges; // Count half-edges (edge ids). - int k = 0; + var k = 0; // Maps a vertex to its leaving boundary edge. var boundary = new Dictionary(); @@ -414,7 +416,7 @@ public DcelMesh ToDCEL(Mesh mesh) tri.tri = t; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { tri.orient = i; tri.Sym(ref neighbor); @@ -445,8 +447,8 @@ public DcelMesh ToDCEL(Mesh mesh) } // Set leaving edges. - edge.origin.leaving = edge; - twin.origin.leaving = twin; + edge.origin.Leaving = edge; + twin.origin.Leaving = twin; // Set twin edges. edge.twin = twin; diff --git a/src/Triangle/Meshing/Data/BadSubseg.cs b/src/Triangle/Meshing/Data/BadSubseg.cs index 3d7c13f..9700fc8 100644 --- a/src/Triangle/Meshing/Data/BadSubseg.cs +++ b/src/Triangle/Meshing/Data/BadSubseg.cs @@ -8,8 +8,8 @@ namespace TriangleNet.Meshing.Data { using System; - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// A queue used to store encroached subsegments. @@ -18,7 +18,7 @@ namespace TriangleNet.Meshing.Data /// Each subsegment's vertices are stored so that we can check whether a /// subsegment is still the same. /// - class BadSubseg + internal class BadSubseg { public Osub subseg; // An encroached subsegment. public Vertex org, dest; // Its two vertices. diff --git a/src/Triangle/Meshing/Data/BadTriQueue.cs b/src/Triangle/Meshing/Data/BadTriQueue.cs index f8e382e..8b6ad54 100644 --- a/src/Triangle/Meshing/Data/BadTriQueue.cs +++ b/src/Triangle/Meshing/Data/BadTriQueue.cs @@ -7,8 +7,8 @@ namespace TriangleNet.Meshing.Data { - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// A (priority) queue for bad triangles. @@ -18,20 +18,18 @@ namespace TriangleNet.Meshing.Data /// give priority to smaller angles. I originally implemented a heap, but /// the queues are faster by a larger margin than I'd suspected. /// - class BadTriQueue + internal class BadTriQueue { - const double SQRT2 = 1.4142135623730950488016887242096980785696718753769480732; + private const double SQRT2 = 1.4142135623730950488016887242096980785696718753769480732; - public int Count { get { return this.count; } } + public int Count { get; private set; } // Variables that maintain the bad triangle queues. The queues are // ordered from 4095 (highest priority) to 0 (lowest priority). - BadTriangle[] queuefront; - BadTriangle[] queuetail; - int[] nextnonemptyq; - int firstnonemptyq; - - int count; + private BadTriangle[] queuefront; + private BadTriangle[] queuetail; + private int[] nextnonemptyq; + private int firstnonemptyq; public BadTriQueue() { @@ -41,7 +39,7 @@ public BadTriQueue() firstnonemptyq = -1; - count = 0; + Count = 0; } /// @@ -56,7 +54,7 @@ public void Enqueue(BadTriangle badtri) int posexponent; int i; - this.count++; + Count++; // Determine the appropriate queue to put the bad triangle into. // Recall that the key is the square of its shortest edge length. @@ -152,7 +150,7 @@ public void Enqueue(BadTriangle badtri) public void Enqueue(ref Otri enqtri, double minedge, Vertex apex, Vertex org, Vertex dest) { // Allocate space for the bad triangle. - BadTriangle newbad = new BadTriangle(); + var newbad = new BadTriangle(); newbad.poortri = enqtri; newbad.key = minedge; @@ -175,10 +173,10 @@ public BadTriangle Dequeue() return null; } - this.count--; + Count--; // Find the first triangle of the highest-priority queue. - BadTriangle result = queuefront[firstnonemptyq]; + var result = queuefront[firstnonemptyq]; // Remove the triangle from the queue. queuefront[firstnonemptyq] = result.next; // If this queue is now empty, note the new highest-priority diff --git a/src/Triangle/Meshing/Data/BadTriangle.cs b/src/Triangle/Meshing/Data/BadTriangle.cs index 7337e07..f5f5960 100644 --- a/src/Triangle/Meshing/Data/BadTriangle.cs +++ b/src/Triangle/Meshing/Data/BadTriangle.cs @@ -8,8 +8,8 @@ namespace TriangleNet.Meshing.Data { using System; - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// A queue used to store bad triangles. @@ -19,7 +19,7 @@ namespace TriangleNet.Meshing.Data /// Each triangle's vertices are stored so that one can check whether a /// triangle is still the same. /// - class BadTriangle + internal class BadTriangle { public Otri poortri; // A skinny or too-large triangle. public double key; // cos^2 of smallest (apical) angle. diff --git a/src/Triangle/Meshing/GenericMesher.cs b/src/Triangle/Meshing/GenericMesher.cs index f29bfe5..a99482c 100644 --- a/src/Triangle/Meshing/GenericMesher.cs +++ b/src/Triangle/Meshing/GenericMesher.cs @@ -8,9 +8,9 @@ namespace TriangleNet.Meshing { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.IO; - using TriangleNet.Meshing.Algorithm; + using Geometry; + using IO; + using Algorithm; /// /// Create meshes of point sets or polygons. @@ -20,8 +20,8 @@ namespace TriangleNet.Meshing /// public class GenericMesher { - Configuration config; - ITriangulator triangulator; + private Configuration config; + private ITriangulator triangulator; /// /// Initializes a new instance of the class. @@ -60,32 +60,56 @@ public GenericMesher(ITriangulator triangulator, Configuration config) this.triangulator = triangulator; } - /// + /// + /// + /// + /// + /// public IMesh Triangulate(IList points) { return triangulator.Triangulate(points, config); } - /// + /// + /// + /// + /// + /// public IMesh Triangulate(IPolygon polygon) { return Triangulate(polygon, null, null); } - /// + /// + /// + /// + /// + /// + /// public IMesh Triangulate(IPolygon polygon, ConstraintOptions options) { return Triangulate(polygon, options, null); } - /// + /// + /// + /// + /// + /// + /// public IMesh Triangulate(IPolygon polygon, QualityOptions quality) { return Triangulate(polygon, null, quality); } - /// - public IMesh Triangulate(IPolygon polygon, ConstraintOptions options, QualityOptions quality) + /// + /// + /// + /// + /// + /// + /// + public IMesh Triangulate(IPolygon polygon, ConstraintOptions? options, QualityOptions? quality) { var mesh = (Mesh)triangulator.Triangulate(polygon.Points, config); diff --git a/src/Triangle/Meshing/IConstraintMesher.cs b/src/Triangle/Meshing/IConstraintMesher.cs index d54ed32..0de6cab 100644 --- a/src/Triangle/Meshing/IConstraintMesher.cs +++ b/src/Triangle/Meshing/IConstraintMesher.cs @@ -1,7 +1,7 @@  namespace TriangleNet.Meshing { - using TriangleNet.Geometry; + using Geometry; /// /// Interface for polygon triangulation. diff --git a/src/Triangle/Meshing/IMesh.cs b/src/Triangle/Meshing/IMesh.cs index b1be86b..a1951b1 100644 --- a/src/Triangle/Meshing/IMesh.cs +++ b/src/Triangle/Meshing/IMesh.cs @@ -2,8 +2,8 @@ namespace TriangleNet.Meshing { using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Mesh interface. @@ -38,7 +38,7 @@ public interface IMesh /// /// Gets the bounds of the mesh. /// - Rectangle Bounds { get; } + Rectangle? Bounds { get; } /// /// Renumber mesh vertices and triangles. diff --git a/src/Triangle/Meshing/IQualityMesher.cs b/src/Triangle/Meshing/IQualityMesher.cs index 8f97960..263e4f8 100644 --- a/src/Triangle/Meshing/IQualityMesher.cs +++ b/src/Triangle/Meshing/IQualityMesher.cs @@ -1,7 +1,7 @@  namespace TriangleNet.Meshing { - using TriangleNet.Geometry; + using Geometry; /// /// Interface for polygon triangulation with quality constraints. diff --git a/src/Triangle/Meshing/ITriangulator.cs b/src/Triangle/Meshing/ITriangulator.cs index c091599..3312edb 100644 --- a/src/Triangle/Meshing/ITriangulator.cs +++ b/src/Triangle/Meshing/ITriangulator.cs @@ -2,7 +2,7 @@ namespace TriangleNet.Meshing { using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; /// /// Interface for point set triangulation. diff --git a/src/Triangle/Meshing/Iterators/EdgeIterator.cs b/src/Triangle/Meshing/Iterators/EdgeIterator.cs index 6e7db3f..8732169 100644 --- a/src/Triangle/Meshing/Iterators/EdgeIterator.cs +++ b/src/Triangle/Meshing/Iterators/EdgeIterator.cs @@ -7,8 +7,8 @@ namespace TriangleNet.Meshing.Iterators { using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Enumerates the edges of a triangulation. @@ -33,11 +33,11 @@ public IEnumerable EnumerateEdges(IMesh mesh) tri.tri = t; tri.orient = 0; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { tri.Sym(ref neighbor); - int nid = neighbor.tri.id; + var nid = neighbor.tri.id; if ((tri.tri.id < nid) || (nid == Mesh.DUMMY)) { @@ -73,18 +73,18 @@ public static IEnumerable EnumerateEdges(IMesh mesh, bool skipSegments Vertex p1, p2; - bool segments = !skipSegments; + var segments = !skipSegments; foreach (var t in mesh.Triangles) { tri.tri = t; tri.orient = 0; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { tri.Sym(ref neighbor); - int nid = neighbor.tri.id; + var nid = neighbor.tri.id; if ((tri.tri.id < nid) || (nid == Mesh.DUMMY)) { diff --git a/src/Triangle/Meshing/Iterators/RegionIterator.cs b/src/Triangle/Meshing/Iterators/RegionIterator.cs index ae6511a..67fba83 100644 --- a/src/Triangle/Meshing/Iterators/RegionIterator.cs +++ b/src/Triangle/Meshing/Iterators/RegionIterator.cs @@ -9,7 +9,7 @@ namespace TriangleNet.Meshing.Iterators { using System; using System.Collections.Generic; - using TriangleNet.Topology; + using Topology; /// /// Iterates the region a given triangle belongs to and applies an action @@ -20,14 +20,14 @@ namespace TriangleNet.Meshing.Iterators /// public class RegionIterator { - List region; + private List region; /// /// Initializes a new instance of the class. /// public RegionIterator(Mesh mesh) { - this.region = new List(); + region = new List(); } /// @@ -38,7 +38,7 @@ public RegionIterator(Mesh mesh) /// region that is enclosed by segments with given boundary label. public void Process(Triangle triangle, int boundary = 0) { - this.Process(triangle, (tri) => + Process(triangle, (tri) => { // Set the region id and area constraint. tri.label = triangle.label; @@ -87,16 +87,16 @@ public void Process(Triangle triangle, Action action, int boundary = 0 /// /// /// - void ProcessRegion(Action action, Func protector) + private void ProcessRegion(Action action, Func protector) { - Otri testtri = default(Otri); - Otri neighbor = default(Otri); - Osub neighborsubseg = default(Osub); + var testtri = default(Otri); + var neighbor = default(Otri); + var neighborsubseg = default(Osub); // Loop through all the infected triangles, spreading the attribute // and/or area constraint to their neighbors, then to their neighbors' // neighbors. - for (int i = 0; i < region.Count; i++) + for (var i = 0; i < region.Count; i++) { // WARNING: Don't use foreach, viri list gets modified. diff --git a/src/Triangle/Meshing/Iterators/VertexCirculator.cs b/src/Triangle/Meshing/Iterators/VertexCirculator.cs index 2d33833..4a15923 100644 --- a/src/Triangle/Meshing/Iterators/VertexCirculator.cs +++ b/src/Triangle/Meshing/Iterators/VertexCirculator.cs @@ -7,15 +7,15 @@ namespace TriangleNet.Meshing.Iterators { using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// Vertex circulator to enumerate the vertices connected to a mesh vertex. /// public class VertexCirculator { - List cache = new List(); + private List cache = new List(); /// /// Initializes a new instance of the class. @@ -59,9 +59,9 @@ private void BuildCache(Vertex vertex, bool vertices) { cache.Clear(); - Otri init = vertex.tri; - Otri next = default(Otri); - Otri prev = default(Otri); + var init = vertex.tri; + var next = default(Otri); + var prev = default(Otri); init.Copy(ref next); diff --git a/src/Triangle/Meshing/QualityMesher.cs b/src/Triangle/Meshing/QualityMesher.cs index dfa19ec..1f16049 100644 --- a/src/Triangle/Meshing/QualityMesher.cs +++ b/src/Triangle/Meshing/QualityMesher.cs @@ -9,31 +9,31 @@ namespace TriangleNet.Meshing { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing.Data; - using TriangleNet.Topology; + using Geometry; + using Data; + using Topology; /// /// Provides methods for mesh quality enforcement and testing. /// - class QualityMesher + internal class QualityMesher { - IPredicates predicates; + private IPredicates predicates; - Queue badsubsegs; - BadTriQueue queue; - Mesh mesh; - Behavior behavior; + private Queue badsubsegs; + private BadTriQueue queue; + private Mesh mesh; + private Behavior behavior; - NewLocation newLocation; + private NewLocation newLocation; - Log logger = Log.Instance; + private Log logger = Log.Instance; // Stores the vertices of the triangle that contains newvertex // in SplitTriangle method. - Triangle newvertex_tri; + private Triangle newvertex_tri; - bool enableAcute = true; + private bool enableAcute = true; public QualityMesher(Mesh mesh, Configuration config) { @@ -41,9 +41,9 @@ public QualityMesher(Mesh mesh, Configuration config) queue = new BadTriQueue(); this.mesh = mesh; - this.predicates = config.Predicates(); + predicates = config.Predicates(); - this.behavior = mesh.behavior; + behavior = mesh.behavior; newLocation = new NewLocation(mesh, predicates); @@ -55,7 +55,7 @@ public QualityMesher(Mesh mesh, Configuration config) /// /// The quality constraints. /// A value indicating, whether the refined mesh should be Conforming Delaunay. - public void Apply(QualityOptions quality, bool delaunay = false) + public void Apply(QualityOptions? quality, bool delaunay = false) { // Copy quality options if (quality != null) @@ -135,8 +135,8 @@ public void AddBadSubseg(BadSubseg badseg) /// public int CheckSeg4Encroach(ref Osub testsubseg) { - Otri neighbortri = default(Otri); - Osub testsym = default(Osub); + var neighbortri = default(Otri); + var testsym = default(Osub); BadSubseg encroachedseg; double dotproduct; int encroached; @@ -240,12 +240,12 @@ public int CheckSeg4Encroach(ref Osub testsubseg) /// public void TestTriangle(ref Otri testtri) { - Otri tri1 = default(Otri), tri2 = default(Otri); - Osub testsub = default(Osub); + Otri tri1 = default, tri2 = default; + var testsub = default(Osub); Vertex torg, tdest, tapex; Vertex base1, base2; Vertex org1, dest1, org2, dest2; - Vertex joinvertex; + Vertex? joinvertex; double dxod, dyod, dxda, dyda, dxao, dyao; double dxod2, dyod2, dxda2, dyda2, dxao2, dyao2; double apexlen, orglen, destlen, minedge; @@ -446,7 +446,7 @@ public void TestTriangle(ref Otri testtri) /// private void TallyEncs() { - Osub subsegloop = default(Osub); + var subsegloop = default(Osub); subsegloop.orient = 0; foreach (var seg in mesh.subsegs.Values) @@ -470,10 +470,10 @@ private void TallyEncs() /// private void SplitEncSegs(bool triflaws) { - Otri enctri = default(Otri); - Otri testtri = default(Otri); - Osub testsh = default(Osub); - Osub currentenc = default(Osub); + var enctri = default(Otri); + var testtri = default(Otri); + var testsh = default(Osub); + var currentenc = default(Osub); BadSubseg seg; Vertex eorg, edest, eapex; Vertex newvertex; @@ -694,7 +694,7 @@ private void SplitEncSegs(bool triflaws) /// private void TallyFaces() { - Otri triangleloop = default(Otri); + var triangleloop = default(Otri); triangleloop.orient = 0; foreach (var tri in mesh.triangles) @@ -713,7 +713,7 @@ private void TallyFaces() /// private void SplitTriangle(BadTriangle badtri) { - Otri badotri = default(Otri); + var badotri = default(Otri); Vertex borg, bdest, bapex; Point newloc; // Location of the new vertex double xi = 0, eta = 0; @@ -757,7 +757,7 @@ private void SplitTriangle(BadTriangle badtri) { // The new vertex must be in the interior, and therefore is a // free vertex with a marker of zero. - Vertex newvertex = new Vertex(newloc.x, newloc.y, 0 + var newvertex = new Vertex(newloc.x, newloc.y, 0 #if USE_ATTRIBS , mesh.nextras #endif @@ -782,7 +782,7 @@ private void SplitTriangle(BadTriangle badtri) // Insert the circumcenter, searching from the edge of the triangle, // and maintain the Delaunay property of the triangulation. - Osub tmp = default(Osub); + var tmp = default(Osub); success = mesh.InsertVertex(newvertex, ref badotri, ref tmp, true, true); if (success == InsertVertexResult.Successful) diff --git a/src/Triangle/Meshing/QualityOptions.cs b/src/Triangle/Meshing/QualityOptions.cs index 6980c16..67dcecc 100644 --- a/src/Triangle/Meshing/QualityOptions.cs +++ b/src/Triangle/Meshing/QualityOptions.cs @@ -2,7 +2,7 @@ namespace TriangleNet.Meshing { using System; - using TriangleNet.Geometry; + using Geometry; /// /// Mesh constraint options for quality triangulation. @@ -32,7 +32,7 @@ public class QualityOptions /// second argument is the area of the triangle tested. If the function /// returns true, the triangle is considered bad and will be refined. /// - public Func UserTest { get; set; } + public Func? UserTest { get; set; } /// /// Gets or sets a test function for excluding triangles from refinement. @@ -41,7 +41,7 @@ public class QualityOptions /// The exclude test takes higher priority than all other test, i.e. if a /// triangle passes the exclude test, no other test will be executed. /// - public Func Exclude { get; set; } + public Func? Exclude { get; set; } /// /// Gets or sets an area constraint per triangle. diff --git a/src/Triangle/NewLocation.cs b/src/Triangle/NewLocation.cs index a520fcb..a96fbd6 100644 --- a/src/Triangle/NewLocation.cs +++ b/src/Triangle/NewLocation.cs @@ -8,9 +8,9 @@ namespace TriangleNet { using System; - using TriangleNet.Topology; - using TriangleNet.Geometry; - using TriangleNet.Tools; + using Topology; + using Geometry; + using Tools; /// /// Find new Steiner point locations. @@ -18,38 +18,38 @@ namespace TriangleNet /// /// See https://www.cise.ufl.edu/~ungor/aCute/index.html /// - class NewLocation + internal class NewLocation { - const double EPS = 1e-50; + private const double EPS = 1e-50; - IPredicates predicates; + private IPredicates predicates; - Mesh mesh; - Behavior behavior; + private Mesh mesh; + private Behavior behavior; // Work arrays for wegde intersection - double[] petalx = new double[20]; - double[] petaly = new double[20]; - double[] petalr = new double[20]; - double[] wedges = new double[500]; - double[] initialConvexPoly = new double[500]; + private double[] petalx = new double[20]; + private double[] petaly = new double[20]; + private double[] petalr = new double[20]; + private double[] wedges = new double[500]; + private double[] initialConvexPoly = new double[500]; // Work arrays for smoothing - double[] points_p = new double[500]; - double[] points_q = new double[500]; - double[] points_r = new double[500]; + private double[] points_p = new double[500]; + private double[] points_q = new double[500]; + private double[] points_r = new double[500]; // Work arrays for convex polygon split - double[] poly1 = new double[100]; - double[] poly2 = new double[100]; - double[][] polys = new double[3][]; + private double[] poly1 = new double[100]; + private double[] poly2 = new double[100]; + private double[][] polys = new double[3][]; public NewLocation(Mesh mesh, IPredicates predicates) { this.mesh = mesh; this.predicates = predicates; - this.behavior = mesh.behavior; + behavior = mesh.behavior; } /// @@ -90,7 +90,7 @@ public Point FindLocation(Vertex org, Vertex dest, Vertex apex, private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex tapex, ref double xi, ref double eta, bool offcenter, Otri badotri) { - double offconstant = behavior.offconstant; + var offconstant = behavior.offconstant; // for calculating the distances of the edges double xdo, ydo, xao, yao, xda, yda; @@ -110,12 +110,12 @@ private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex t Point smallestAngleCorner, middleAngleCorner, largestAngleCorner; // keeps the type of orientation if the triangle - int orientation = 0; + var orientation = 0; // keeps the coordinates of circumcenter of itself and neighbor triangle circumcenter Point myCircumcenter, neighborCircumcenter; // keeps if bad triangle is almost good or not - int almostGood = 0; + var almostGood = 0; // keeps the cosine of the largest angle double cosMaxAngle; bool isObtuse; // 1: obtuse 0: nonobtuse @@ -125,8 +125,8 @@ private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex t double xPetalCtr_1, yPetalCtr_1, xPetalCtr_2, yPetalCtr_2, xPetalCtr, yPetalCtr, xMidOfShortestEdge, yMidOfShortestEdge; double dxcenter1, dycenter1, dxcenter2, dycenter2; // for finding neighbor - Otri neighborotri = default(Otri); - double[] thirdPoint = new double[2]; + var neighborotri = default(Otri); + var thirdPoint = new double[2]; //int neighborNotFound = -1; bool neighborNotFound; // for keeping the vertices of the neighbor triangle @@ -143,13 +143,13 @@ private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex t // for vector calculations in perturbation double ax, ay, d; - double pertConst = 0.06; // perturbation constant + var pertConst = 0.06; // perturbation constant double lengthConst = 1; // used at comparing circumcenter's distance to proposed point's distance double justAcute = 1; // used for making the program working for one direction only // for smoothing - int relocated = 0;// used to differentiate between calling the deletevertex and just proposing a steiner point - double[] newloc = new double[2]; // new location suggested by smoothing + var relocated = 0;// used to differentiate between calling the deletevertex and just proposing a steiner point + var newloc = new double[2]; // new location suggested by smoothing double origin_x = 0, origin_y = 0; // for keeping torg safe Otri delotri; // keeping the original orientation for relocation process // keeps the first and second direction suggested points @@ -745,7 +745,7 @@ private Point FindNewLocationWithoutMaxAngle(Vertex torg, Vertex tdest, Vertex t }// end of relocation }// end of almostGood - Point circumcenter = new Point(); + var circumcenter = new Point(); if (relocated <= 0) { diff --git a/src/Triangle/RobustPredicates.cs b/src/Triangle/RobustPredicates.cs index 336d66a..053be9b 100644 --- a/src/Triangle/RobustPredicates.cs +++ b/src/Triangle/RobustPredicates.cs @@ -8,8 +8,8 @@ namespace TriangleNet { using System; - using TriangleNet.Geometry; - using TriangleNet.Tools; + using Geometry; + using Tools; /// /// Adaptive exact arithmetic geometric predicates. @@ -1247,14 +1247,14 @@ private double InCircleAdapt(Point pa, Point pb, Point pc, Point pd, double perm #region Workspace // InCircleAdapt workspace: - double[] fin1, fin2, abdet; + private double[] fin1, fin2, abdet; - double[] axbc, axxbc, aybc, ayybc, adet; - double[] bxca, bxxca, byca, byyca, bdet; - double[] cxab, cxxab, cyab, cyyab, cdet; + private double[] axbc, axxbc, aybc, ayybc, adet; + private double[] bxca, bxxca, byca, byyca, bdet; + private double[] cxab, cxxab, cyab, cyyab, cdet; - double[] temp8, temp16a, temp16b, temp16c; - double[] temp32a, temp32b, temp48, temp64; + private double[] temp8, temp16a, temp16b, temp16c; + private double[] temp32a, temp32b, temp48, temp64; private void AllocateWorkspace() { diff --git a/src/Triangle/Smoothing/SimpleSmoother.cs b/src/Triangle/Smoothing/SimpleSmoother.cs index acbd06e..28b0fe3 100644 --- a/src/Triangle/Smoothing/SimpleSmoother.cs +++ b/src/Triangle/Smoothing/SimpleSmoother.cs @@ -8,10 +8,10 @@ namespace TriangleNet.Smoothing { - using TriangleNet.Geometry; - using TriangleNet.Meshing; - using TriangleNet.Topology.DCEL; - using TriangleNet.Voronoi; + using Geometry; + using Meshing; + using Topology.DCEL; + using Voronoi; /// /// Simple mesh smoother implementation (Lloyd's relaxation algorithm). @@ -22,13 +22,12 @@ namespace TriangleNet.Smoothing /// public class SimpleSmoother { + private TrianglePool pool; + private Configuration config; - TrianglePool pool; - Configuration config; + private IVoronoiFactory factory; - IVoronoiFactory factory; - - ConstraintOptions options; + private ConstraintOptions options; /// /// Initializes a new instance of the class. @@ -44,13 +43,13 @@ public SimpleSmoother() public SimpleSmoother(IVoronoiFactory factory) { this.factory = factory; - this.pool = new TrianglePool(); + pool = new TrianglePool(); - this.config = new Configuration( + config = new Configuration( () => RobustPredicates.Default, () => pool.Restart()); - this.options = new ConstraintOptions() { ConformingDelaunay = true }; + options = new ConstraintOptions() { ConformingDelaunay = true }; } /// @@ -63,7 +62,7 @@ public SimpleSmoother(IVoronoiFactory factory, Configuration config) this.factory = factory; this.config = config; - this.options = new ConstraintOptions() { ConformingDelaunay = true }; + options = new ConstraintOptions() { ConformingDelaunay = true }; } /// @@ -93,7 +92,7 @@ public int Smooth(IMesh mesh, int limit = 10, double tol = .01) var predicates = config.Predicates(); // The smoother should respect the mesh segment splitting behavior. - this.options.SegmentSplitting = smoothedMesh.behavior.NoBisect; + options.SegmentSplitting = smoothedMesh.behavior.NoBisect; // The maximum distances moved from any site at the previous and // current iterations. @@ -103,7 +102,7 @@ public int Smooth(IMesh mesh, int limit = 10, double tol = .01) // Take a few smoothing rounds (Lloyd's algorithm). The stop // criteria are the maximum number of iterations and the convergence // criterion. - int i = 1; + var i = 1; while (i < limit && Math.Abs(currMax - prevMax) > tol * currMax) { prevMax = currMax; diff --git a/src/Triangle/Smoothing/VoronoiFactory.cs b/src/Triangle/Smoothing/VoronoiFactory.cs index c310693..02da7ea 100644 --- a/src/Triangle/Smoothing/VoronoiFactory.cs +++ b/src/Triangle/Smoothing/VoronoiFactory.cs @@ -2,8 +2,8 @@ namespace TriangleNet.Smoothing { using System; - using TriangleNet.Topology.DCEL; - using TriangleNet.Voronoi; + using Topology.DCEL; + using Voronoi; /// /// Factory which re-uses objects in the smoothing loop to enhance performance. @@ -11,11 +11,11 @@ namespace TriangleNet.Smoothing /// /// See . /// - class VoronoiFactory : IVoronoiFactory + internal class VoronoiFactory : IVoronoiFactory { - ObjectPool vertices; - ObjectPool edges; - ObjectPool faces; + private ObjectPool vertices; + private ObjectPool edges; + private ObjectPool faces; public VoronoiFactory() { @@ -30,18 +30,18 @@ public void Initialize(int vertexCount, int edgeCount, int faceCount) edges.Capacity = edgeCount; faces.Capacity = faceCount; - for (int i = vertices.Count; i < vertexCount; i++) + for (var i = vertices.Count; i < vertexCount; i++) { vertices.Put(new Vertex(0, 0)); } - for (int i = edges.Count; i < edgeCount; i++) + for (var i = edges.Count; i < edgeCount; i++) { edges.Put(new HalfEdge(null)); } - for (int i = faces.Count; i < faceCount; i++) + for (var i = faces.Count; i < faceCount; i++) { faces.Put(new Face(null)); } @@ -64,7 +64,7 @@ public Vertex CreateVertex(double x, double y) { vertex.x = x; vertex.y = y; - vertex.leaving = null; + vertex.Leaving = null; return vertex; } @@ -122,45 +122,42 @@ public Face CreateFace(Geometry.Vertex vertex) return face; } - class ObjectPool where T : class + private class ObjectPool where T : class { - int index, count; + private int index; - T[] pool; + private T[] pool; - public int Count - { - get { return count; } - } + public int Count { get; private set; } public int Capacity { - get { return this.pool.Length; } - set { Resize(value); } + get => pool.Length; + set => Resize(value); } public ObjectPool(int capacity = 3) { - this.index = 0; - this.count = 0; + index = 0; + Count = 0; - this.pool = new T[capacity]; + pool = new T[capacity]; } public ObjectPool(T[] pool) { - this.index = 0; - this.count = 0; + index = 0; + Count = 0; this.pool = pool; } public bool TryGet(out T obj) { - if (this.index < this.count) + if (index < Count) { - obj = this.pool[this.index++]; + obj = pool[index++]; return true; } @@ -172,28 +169,28 @@ public bool TryGet(out T obj) public void Put(T obj) { - var capacity = this.pool.Length; + var capacity = pool.Length; - if (capacity <= this.count) + if (capacity <= Count) { Resize(2 * capacity); } - this.pool[this.count++] = obj; + pool[Count++] = obj; - this.index++; + index++; } public void Release() { - this.index = 0; + index = 0; } private void Resize(int size) { - if (size > this.count) + if (size > Count) { - Array.Resize(ref this.pool, size); + Array.Resize(ref pool, size); } } } diff --git a/src/Triangle/Tools/AdjacencyMatrix.cs b/src/Triangle/Tools/AdjacencyMatrix.cs index 0602dab..481ad66 100644 --- a/src/Triangle/Tools/AdjacencyMatrix.cs +++ b/src/Triangle/Tools/AdjacencyMatrix.cs @@ -15,15 +15,13 @@ namespace TriangleNet.Tools public class AdjacencyMatrix { // Number of adjacency entries. - int nnz; + private int nnz; // Pointers into the actual adjacency structure adj. Information about row k is // stored in entries pcol(k) through pcol(k+1)-1 of adj. Size: N + 1 - int[] pcol; // The adjacency structure. For each row, it contains the column indices // of the nonzero entries. Size: nnz - int[] irow; /// /// Gets the number of columns (nodes of the mesh). @@ -33,12 +31,12 @@ public class AdjacencyMatrix /// /// Gets the column pointers. /// - public int[] ColumnPointers => pcol; + public int[] ColumnPointers { get; } /// /// Gets the row indices. /// - public int[] RowIndices => irow; + public int[] RowIndices { get; } /// /// Initializes a new instance of the class. @@ -62,7 +60,7 @@ public AdjacencyMatrix(Mesh mesh) /// Determines whether nodes should automatically be renumbered. public AdjacencyMatrix(Mesh mesh, bool renumber) { - int n = mesh.vertices.Count; + var n = mesh.vertices.Count; // Undead vertices should not be considered in the adjacency matrix. ColumnCount = n - mesh.undeads; @@ -70,7 +68,7 @@ public AdjacencyMatrix(Mesh mesh, bool renumber) if (renumber) { // Renumber nodes, excluding undeads. - int i = 0; + var i = 0; foreach (var vertex in mesh.vertices.Values) { vertex.id = vertex.type == VertexType.UndeadVertex ? -i : i++; @@ -78,11 +76,11 @@ public AdjacencyMatrix(Mesh mesh, bool renumber) } // Set up the adj_row adjacency pointer array. - pcol = AdjacencyCount(mesh); - nnz = pcol[ColumnCount]; + ColumnPointers = AdjacencyCount(mesh); + nnz = ColumnPointers[ColumnCount]; // Set up the adj adjacency array. - irow = AdjacencySet(mesh, pcol); + RowIndices = AdjacencySet(mesh, ColumnPointers); SortIndices(); } @@ -99,8 +97,8 @@ public AdjacencyMatrix(int[] pcol, int[] irow) nnz = pcol[ColumnCount]; - this.pcol = pcol; - this.irow = irow; + this.ColumnPointers = pcol; + this.RowIndices = irow; if (pcol[0] != 0) { @@ -119,14 +117,14 @@ public AdjacencyMatrix(int[] pcol, int[] irow) /// Bandwidth of the adjacency matrix. public int Bandwidth() { - int band_lo = 0; - int band_hi = 0; + var band_lo = 0; + var band_hi = 0; - for (int i = 0; i < ColumnCount; i++) + for (var i = 0; i < ColumnCount; i++) { - for (int j = pcol[i]; j < pcol[i + 1]; j++) + for (var j = ColumnPointers[i]; j < ColumnPointers[i + 1]; j++) { - int col = irow[j]; + var col = RowIndices[j]; band_lo = Math.Max(band_lo, i - col); band_hi = Math.Max(band_hi, col - i); } @@ -150,16 +148,16 @@ public int Bandwidth() /// Two nodes are "adjacent" if they are both nodes in some triangle. /// Also, a node is considered to be adjacent to itself. /// - int[] AdjacencyCount(Mesh mesh) + private int[] AdjacencyCount(Mesh mesh) { - int n = ColumnCount; + var n = ColumnCount; int n1, n2, n3; int tid, nid; - int[] pcol = new int[n + 1]; + var pcol = new int[n + 1]; // Set every node to be adjacent to itself. - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { pcol[i] = 1; } @@ -205,13 +203,13 @@ int[] AdjacencyCount(Mesh mesh) // We used PCOL to count the number of entries in each column. // Convert it to pointers into the ADJ array. - for (int i = n; i > 0; i--) + for (var i = n; i > 0; i--) { pcol[i] = pcol[i - 1]; } pcol[0] = 0; - for (int i = 1; i <= n; i++) + for (var i = 1; i <= n; i++) { pcol[i] = pcol[i - 1] + pcol[i]; } @@ -227,11 +225,11 @@ int[] AdjacencyCount(Mesh mesh) /// for a linear triangle finite element discretization of Poisson's /// equation in two dimensions. /// - int[] AdjacencySet(Mesh mesh, int[] pcol) + private int[] AdjacencySet(Mesh mesh, int[] pcol) { - int n = ColumnCount; + var n = ColumnCount; - int[] col = new int[n]; + var col = new int[n]; // Copy of the adjacency rows input. Array.Copy(pcol, col, n); @@ -239,7 +237,7 @@ int[] AdjacencySet(Mesh mesh, int[] pcol) int i, nnz = pcol[n]; // Output list, stores the actual adjacency information. - int[] list = new int[nnz]; + var list = new int[nnz]; // Set every node to be adjacent to itself. for (i = 0; i < n; i++) @@ -300,13 +298,13 @@ public void SortIndices() { int k1, k2, n = ColumnCount; - var list = irow; + var list = RowIndices; // Ascending sort the entries for each column. - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { - k1 = pcol[i]; - k2 = pcol[i + 1]; + k1 = ColumnPointers[i]; + k2 = ColumnPointers[i + 1]; Array.Sort(list, k1, k2 - k1); } } diff --git a/src/Triangle/Tools/CuthillMcKee.cs b/src/Triangle/Tools/CuthillMcKee.cs index 2a61854..7276ec6 100644 --- a/src/Triangle/Tools/CuthillMcKee.cs +++ b/src/Triangle/Tools/CuthillMcKee.cs @@ -33,7 +33,7 @@ namespace TriangleNet.Tools public class CuthillMcKee { // The adjacency matrix of the mesh. - AdjacencyMatrix matrix; + private AdjacencyMatrix matrix; /// /// Gets the permutation vector for the Reverse Cuthill-McKee numbering. @@ -62,17 +62,17 @@ public int[] Renumber(AdjacencyMatrix matrix) // TODO: Make RCM work with 0-based matrix. // Compute the RCM permutation. - int[] perm = GenerateRcm(); + var perm = GenerateRcm(); - int[] perm_inv = PermInverse(perm); + var perm_inv = PermInverse(perm); // Adjust column pointers (0-based indexing). Shift(pcol, false); if (Log.Verbose) { - int bandwidth1 = matrix.Bandwidth(); - int bandwidth2 = PermBandwidth(perm, perm_inv); + var bandwidth1 = matrix.Bandwidth(); + var bandwidth2 = PermBandwidth(perm, perm_inv); Log.Instance.Info(string.Format("Reverse Cuthill-McKee (Bandwidth: {0} > {1})", bandwidth1, bandwidth2)); @@ -91,23 +91,23 @@ public int[] Renumber(AdjacencyMatrix matrix) /// For each connected component in the graph, the routine obtains /// an ordering by calling RCM. /// - int[] GenerateRcm() + private int[] GenerateRcm() { // Number of nodes in the mesh. - int n = matrix.ColumnCount; + var n = matrix.ColumnCount; - int[] perm = new int[n]; + var perm = new int[n]; int i, num, root; - int iccsze = 0; - int level_num = 0; + var iccsze = 0; + var level_num = 0; // Index vector for a level structure. The level structure is stored in the // currently unused spaces in the permutation vector PERM. - int[] level_row = new int[n + 1]; + var level_row = new int[n + 1]; // Marks variables that have been numbered. - int[] mask = new int[n]; + var mask = new int[n]; for (i = 0; i < n; i++) { @@ -168,10 +168,10 @@ int[] GenerateRcm() /// /// When done, reverse the ordering. /// - void Rcm(int root, int[] mask, int[] perm, int offset, ref int iccsze) + private void Rcm(int root, int[] mask, int[] perm, int offset, ref int iccsze) { - int[] pcol = matrix.ColumnPointers; - int[] irow = matrix.RowIndices; + var pcol = matrix.ColumnPointers; + var irow = matrix.RowIndices; int fnbr; int i, j, k, l; @@ -180,11 +180,11 @@ void Rcm(int root, int[] mask, int[] perm, int offset, ref int iccsze) int nbr, node; // Number of nodes in the mesh. - int n = matrix.ColumnCount; + var n = matrix.ColumnCount; // Workspace, int DEG[NODE_NUM], a temporary vector used to hold // the degree of the nodes in the section graph specified by mask and root. - int[] deg = new int[n]; + var deg = new int[n]; // Find the degrees of the nodes in the component specified by MASK and ROOT. Degree(root, mask, deg, ref iccsze, perm, offset); @@ -309,11 +309,11 @@ void Rcm(int root, int[] mask, int[] perm, int offset, ref int iccsze) /// of the list of nodes that are at a distance of 0, 1, 2, ..., /// NODE_NUM-1 from the pseudo-peripheral node. /// - void FindRoot(ref int root, int[] mask, ref int level_num, int[] level_row, + private void FindRoot(ref int root, int[] mask, ref int level_num, int[] level_row, int[] level, int offset) { - int[] pcol = matrix.ColumnPointers; - int[] irow = matrix.RowIndices; + var pcol = matrix.ColumnPointers; + var irow = matrix.RowIndices; int iccsze; int j, jstrt; @@ -321,7 +321,7 @@ void FindRoot(ref int root, int[] mask, ref int level_num, int[] level_row, int mindeg; int nghbor, ndeg; int node; - int level_num2 = 0; + var level_num2 = 0; // Determine the level structure rooted at ROOT. GetLevelSet(ref root, mask, ref level_num, level_row, level, offset); @@ -417,11 +417,11 @@ void FindRoot(ref int root, int[] mask, ref int level_num, int[] level_row, /// are no unmasked nodes adjacent to any node in the current level. /// The number of levels may vary between 2 and NODE_NUM. /// - void GetLevelSet(ref int root, int[] mask, ref int level_num, int[] level_row, + private void GetLevelSet(ref int root, int[] mask, ref int level_num, int[] level_row, int[] level, int offset) { - int[] pcol = matrix.ColumnPointers; - int[] irow = matrix.RowIndices; + var pcol = matrix.ColumnPointers; + var irow = matrix.RowIndices; int i, iccsze; int j, jstop, jstrt; @@ -500,15 +500,15 @@ void GetLevelSet(ref int root, int[] mask, ref int level_num, int[] level_row, /// The connected component is specified by MASK and ROOT. /// Nodes for which MASK is zero are ignored. /// - void Degree(int root, int[] mask, int[] deg, ref int iccsze, int[] ls, int offset) + private void Degree(int root, int[] mask, int[] deg, ref int iccsze, int[] ls, int offset) { - int[] pcol = matrix.ColumnPointers; - int[] irow = matrix.RowIndices; + var pcol = matrix.ColumnPointers; + var irow = matrix.RowIndices; int i, ideg; int j, jstop, jstrt; int lbegin, lvlend; - int lvsize = 1; + var lvsize = 1; int nbr, node; // The sign of ADJ_ROW(I) is used to indicate if node I has been considered. @@ -581,23 +581,23 @@ void Degree(int root, int[] mask, int[] deg, ref int iccsze, int[] ls, int offse /// The matrix is defined by the adjacency information and a permutation. /// The routine also computes the bandwidth and the size of the envelope. /// - int PermBandwidth(int[] perm, int[] perm_inv) + private int PermBandwidth(int[] perm, int[] perm_inv) { - int[] pcol = matrix.ColumnPointers; - int[] irow = matrix.RowIndices; + var pcol = matrix.ColumnPointers; + var irow = matrix.RowIndices; int col, end; - int band_lo = 0; - int band_hi = 0; + var band_lo = 0; + var band_hi = 0; - int n = matrix.ColumnCount; + var n = matrix.ColumnCount; - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { end = pcol[perm[i] + 1]; - for (int j = pcol[perm[i]]; j < end; j++) + for (var j = pcol[perm[i]]; j < end; j++) { col = perm_inv[irow[j]]; band_lo = Math.Max(band_lo, i - col); @@ -613,13 +613,13 @@ int PermBandwidth(int[] perm, int[] perm_inv) /// /// PERM[N], a permutation. /// The inverse permutation. - int[] PermInverse(int[] perm) + private int[] PermInverse(int[] perm) { - int n = matrix.ColumnCount; + var n = matrix.ColumnCount; - int[] perm_inv = new int[n]; + var perm_inv = new int[n]; - for (int i = 0; i < n; i++) + for (var i = 0; i < n; i++) { perm_inv[perm[i]] = i; } @@ -641,12 +641,12 @@ int[] PermInverse(int[] perm) /// Output: /// A = ( 15, 14, 13, 12, 11 ). /// - void ReverseVector(int[] a, int offset, int size) + private void ReverseVector(int[] a, int offset, int size) { int j, middle = offset + size / 2, end = offset + size - 1; - for (int i = offset; i < middle; i++) + for (var i = offset; i < middle; i++) { j = a[i]; a[i] = a[end - i + offset]; @@ -654,17 +654,17 @@ void ReverseVector(int[] a, int offset, int size) } } - void Shift(int[] a, bool up) + private void Shift(int[] a, bool up) { - int length = a.Length; + var length = a.Length; if (up) { - for (int i = 0; i < length; a[i]++, i++) ; + for (var i = 0; i < length; a[i]++, i++) ; } else { - for (int i = 0; i < length; a[i]--, i++) ; + for (var i = 0; i < length; a[i]--, i++) ; } } diff --git a/src/Triangle/Tools/Interpolation.cs b/src/Triangle/Tools/Interpolation.cs index 99e9747..6a48b6f 100644 --- a/src/Triangle/Tools/Interpolation.cs +++ b/src/Triangle/Tools/Interpolation.cs @@ -7,7 +7,7 @@ namespace TriangleNet.Tools { - using TriangleNet.Geometry; + using Geometry; /// /// Interpolation helper. @@ -30,24 +30,24 @@ public static double InterpolatePoint(ITriangle tri, Point p, double[] data) var dest = tri.GetVertex(1); var apex = tri.GetVertex(2); - double xdo = dest.x - org.x; - double ydo = dest.y - org.y; - double xao = apex.x - org.x; - double yao = apex.y - org.y; + var xdo = dest.x - org.x; + var ydo = dest.y - org.y; + var xao = apex.x - org.x; + var yao = apex.y - org.y; - double denominator = 0.5 / (xdo * yao - xao * ydo); + var denominator = 0.5 / (xdo * yao - xao * ydo); - double dx = p.x - org.x; - double dy = p.y - org.y; + var dx = p.x - org.x; + var dy = p.y - org.y; // To interpolate z value for the given point inserted, define a // coordinate system with a xi-axis, directed from the triangle's // origin to its destination, and an eta-axis, directed from its // origin to its apex. - double xi = (yao * dx - xao * dy) * (2.0 * denominator); - double eta = (xdo * dy - ydo * dx) * (2.0 * denominator); + var xi = (yao * dx - xao * dy) * (2.0 * denominator); + var eta = (xdo * dy - ydo * dx) * (2.0 * denominator); - double orgz = data[org.id]; + var orgz = data[org.id]; return orgz + xi * (data[dest.id] - orgz) + eta * (data[apex.id] - orgz); } diff --git a/src/Triangle/Tools/IntersectionHelper.cs b/src/Triangle/Tools/IntersectionHelper.cs index 0310481..fba013b 100644 --- a/src/Triangle/Tools/IntersectionHelper.cs +++ b/src/Triangle/Tools/IntersectionHelper.cs @@ -7,7 +7,7 @@ namespace TriangleNet.Tools { using System; - using TriangleNet.Geometry; + using Geometry; /// /// Segment intersection helper. @@ -25,19 +25,19 @@ public static class IntersectionHelper public static bool IsPointOnSegment(Point a, Point b, Point test, double eps = 1e-12) { // The cross product. - double cross = (test.Y - a.Y) * (b.X - a.X) - (test.X - a.X) * (b.Y - a.Y); + var cross = (test.Y - a.Y) * (b.X - a.X) - (test.X - a.X) * (b.Y - a.Y); // Check if points are collinear. if (Math.Abs(cross) > eps) return false; // The dot product (projection of test point onto segment). - double dot = (test.X - a.X) * (b.X - a.X) + (test.Y - a.Y) * (b.Y - a.Y); + var dot = (test.X - a.X) * (b.X - a.X) + (test.Y - a.Y) * (b.Y - a.Y); // Check if test point is actually between a and b (left of a). if (dot < 0) return false; // Length of the segment. - double ab = (b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y); + var ab = (b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y); // Ignore duplicate input points. if (ab == 0) return false; @@ -62,15 +62,15 @@ public static bool IsPointOnSegment(Point a, Point b, Point test, double eps = 1 /// public static void IntersectSegments(Point p0, Point p1, Point q0, Point q1, ref Point c0) { - double ux = p1.x - p0.x; - double uy = p1.y - p0.y; - double vx = q1.x - q0.x; - double vy = q1.y - q0.y; - double wx = p0.x - q0.x; - double wy = p0.y - q0.y; + var ux = p1.x - p0.x; + var uy = p1.y - p0.y; + var vx = q1.x - q0.x; + var vy = q1.y - q0.y; + var wx = p0.x - q0.x; + var wy = p0.y - q0.y; - double d = (ux * vy - uy * vx); - double s = (vx * wy - vy * wx) / d; + var d = (ux * vy - uy * vx); + var s = (vx * wy - vy * wx) / d; // Intersection point c0.x = p0.X + s * ux; @@ -93,26 +93,26 @@ public static void IntersectSegments(Point p0, Point p1, Point q0, Point q1, ref public static bool LiangBarsky(Rectangle rect, Point p0, Point p1, ref Point c0, ref Point c1) { // Define the x/y clipping values for the border. - double xmin = rect.Left; - double xmax = rect.Right; - double ymin = rect.Bottom; - double ymax = rect.Top; + var xmin = rect.Left; + var xmax = rect.Right; + var ymin = rect.Bottom; + var ymax = rect.Top; // Define the start and end points of the line. - double x0 = p0.X; - double y0 = p0.Y; - double x1 = p1.X; - double y1 = p1.Y; + var x0 = p0.X; + var y0 = p0.Y; + var x1 = p1.X; + var y1 = p1.Y; - double t0 = 0.0; - double t1 = 1.0; + var t0 = 0.0; + var t1 = 1.0; - double dx = x1 - x0; - double dy = y1 - y0; + var dx = x1 - x0; + var dy = y1 - y0; double p = 0.0, q = 0.0, r; - for (int edge = 0; edge < 4; edge++) + for (var edge = 0; edge < 4; edge++) { // Traverse through left, right, bottom, top edges. if (edge == 0) { p = -dx; q = -(xmin - x0); } @@ -185,16 +185,16 @@ public static Point BoxRayIntersection(Rectangle rect, Point p, double dx, doubl /// Returns false, if startpoint is outside the box. public static bool BoxRayIntersection(Rectangle rect, Point p, double dx, double dy, ref Point c) { - double x = p.X; - double y = p.Y; + var x = p.X; + var y = p.Y; double t1, x1, y1, t2, x2, y2; // Bounding box - double xmin = rect.Left; - double xmax = rect.Right; - double ymin = rect.Bottom; - double ymax = rect.Top; + var xmin = rect.Left; + var xmax = rect.Right; + var ymin = rect.Bottom; + var ymax = rect.Top; // Check if point is inside the bounds if (x < xmin || x > xmax || y < ymin || y > ymax) diff --git a/src/Triangle/Tools/PolygonValidator.cs b/src/Triangle/Tools/PolygonValidator.cs index 944ec8e..fbe014f 100644 --- a/src/Triangle/Tools/PolygonValidator.cs +++ b/src/Triangle/Tools/PolygonValidator.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Tools { using System; using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; /// /// Polygon validation helper. @@ -24,10 +24,10 @@ public static bool IsConsistent(IPolygon poly) var points = poly.Points; - int horrors = 0; + var horrors = 0; - int i = 0; - int count = points.Count; + var i = 0; + var count = points.Count; if (count < 3) { @@ -101,13 +101,13 @@ public static bool HasDuplicateVertices(IPolygon poly) { var logger = Log.Instance; - int horrors = 0; + var horrors = 0; var points = poly.Points.ToArray(); VertexSorter.Sort(points); - for (int i = 1; i < points.Length; i++) + for (var i = 1; i < points.Length; i++) { if (points[i - 1] == points[i]) { @@ -133,8 +133,8 @@ public static (double min, double max) GetSegmentRatio(IPolygon poly, double thr { var logger = Log.Instance; - double min = double.MaxValue; - double max = 0.0; + var min = double.MaxValue; + var max = 0.0; foreach (var seg in poly.Segments) { @@ -157,7 +157,7 @@ public static (double min, double max) GetSegmentRatio(IPolygon poly, double thr max = Math.Max(max, length); } - double ratio = min / max; + var ratio = min / max; if (ratio < threshold) { @@ -182,8 +182,8 @@ public static bool HasBadAngles(IPolygon poly, double threshold = 2e-12) { var logger = Log.Instance; - int horrors = 0; - int i = 0; + var horrors = 0; + var i = 0; Point p0 = null, p1 = null; Point q0, q1; @@ -224,8 +224,8 @@ public static bool HasBadAngles(IPolygon poly, double threshold = 2e-12) private static bool IsBadAngle(Point a, Point b, Point c, double threshold = 0.0) { - double x = DotProduct(a, b, c); - double y = CrossProductLength(a, b, c); + var x = DotProduct(a, b, c); + var y = CrossProductLength(a, b, c); return Math.Abs(Math.Atan2(y, x)) <= threshold; } @@ -248,9 +248,9 @@ private static int CheckVertexIDs(IPolygon poly, int count) { var logger = Log.Instance; - int horrors = 0; + var horrors = 0; - int i = 0; + var i = 0; Vertex p, q; diff --git a/src/Triangle/Tools/QualityMeasure.cs b/src/Triangle/Tools/QualityMeasure.cs index dcf5144..6377023 100644 --- a/src/Triangle/Tools/QualityMeasure.cs +++ b/src/Triangle/Tools/QualityMeasure.cs @@ -8,8 +8,8 @@ namespace TriangleNet.Tools { using System; using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Meshing; + using Geometry; + using Meshing; /// /// Base class for quality measures. @@ -114,10 +114,10 @@ public virtual void Finalize(int n, double totalArea) /// public class QualityMeasure { - MeasureArea _area; - MeasureAlpha _alpha; - MeasureEta _eta; - MeasureQ _q; + private MeasureArea _area; + private MeasureAlpha _alpha; + private MeasureEta _eta; + private MeasureQ _q; #region Public properties @@ -161,7 +161,7 @@ public class QualityMeasure #endregion - List measures; + private List measures; /// /// Initializes a new instance of the class. @@ -208,7 +208,7 @@ public void Update(IEnumerable triangles) double lx, ly; double area; - int n = 0; + var n = 0; foreach (var m in measures) { @@ -290,11 +290,11 @@ public static int Bandwidth(IMesh mesh) foreach (var tri in mesh.Triangles) { - for (int j = 0; j < 3; j++) + for (var j = 0; j < 3; j++) { gi = tri.GetVertexID(j); - for (int k = 0; k < 3; k++) + for (var k = 0; k < 3; k++) { gj = tri.GetVertexID(k); @@ -307,7 +307,7 @@ public static int Bandwidth(IMesh mesh) return ml + 1 + mu; } - class MeasureArea : Measure + private class MeasureArea : Measure { private readonly double EPS = 0.0; @@ -357,16 +357,16 @@ public override void Finalize(int n, double area_total) /// is 60). The best possible value is 1, and the worst 0. A good /// triangulation should have an alpha score close to 1. /// - class MeasureAlpha : Measure + private class MeasureAlpha : Measure { /// public override double Update(double ab, double bc, double ca, double area) { - double alpha = double.MaxValue; + var alpha = double.MaxValue; - double ab2 = ab * ab; - double bc2 = bc * bc; - double ca2 = ca * ca; + var ab2 = ab * ab; + var bc2 = bc * bc; + var ca2 = ca * ca; double a_angle; double b_angle; @@ -439,7 +439,7 @@ public override double Update(double ab, double bc, double ca, double area) /// possible value is 1, and the worst 0. A good triangulation should have an /// eta score close to 1. /// - class MeasureEta : Measure + private class MeasureEta : Measure { // Normalization factor to ensure that a perfect triangle // (equal edges) has a quality of 1. @@ -448,11 +448,11 @@ class MeasureEta : Measure /// public override double Update(double ab, double bc, double ca, double area) { - double ab2 = ab * ab; - double bc2 = bc * bc; - double ca2 = ca * ca; + var ab2 = ab * ab; + var bc2 = bc * bc; + var ca2 = ca * ca; - double eta = Factor * area / (ab2 + bc2 + ca2); + var eta = Factor * area / (ab2 + bc2 + ca2); avg += eta; are += area * eta; @@ -477,12 +477,12 @@ public override double Update(double ab, double bc, double ca, double area) /// equilateral shape, for which Q = 1. A good mesh would have /// 0.5 < Q. /// - class MeasureQ : Measure + private class MeasureQ : Measure { /// public override double Update(double ab, double bc, double ca, double area) { - double q = (bc + ca - ab) * (ca + ab - bc) * (ab + bc - ca) / (ab * bc * ca); + var q = (bc + ca - ab) * (ca + ab - bc) * (ab + bc - ca) / (ab * bc * ca); min = Math.Min(min, q); max = Math.Max(max, q); diff --git a/src/Triangle/Tools/Statistic.cs b/src/Triangle/Tools/Statistic.cs index 642ceb1..a2f373e 100644 --- a/src/Triangle/Tools/Statistic.cs +++ b/src/Triangle/Tools/Statistic.cs @@ -8,8 +8,8 @@ namespace TriangleNet.Tools { using System; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Gather mesh statistics. @@ -67,72 +67,61 @@ public class Statistic #region Properties - double minEdge = 0; /// /// Gets the shortest edge. /// - public double ShortestEdge => minEdge; + public double ShortestEdge { get; private set; } - double maxEdge = 0; /// /// Gets the longest edge. /// - public double LongestEdge => maxEdge; + public double LongestEdge { get; private set; } // - double minAspect = 0; /// /// Gets the shortest altitude. /// - public double ShortestAltitude => minAspect; + public double ShortestAltitude { get; private set; } - double maxAspect = 0; /// /// Gets the largest aspect ratio. /// - public double LargestAspectRatio => maxAspect; + public double LargestAspectRatio { get; private set; } - double minArea = 0; /// /// Gets the smallest area. /// - public double SmallestArea => minArea; + public double SmallestArea { get; private set; } - double maxArea = 0; /// /// Gets the largest area. /// - public double LargestArea => maxArea; + public double LargestArea { get; private set; } - double minAngle = 0; /// /// Gets the smallest angle. /// - public double SmallestAngle => minAngle; + public double SmallestAngle { get; private set; } - double maxAngle = 0; /// /// Gets the largest angle. /// - public double LargestAngle => maxAngle; + public double LargestAngle { get; private set; } - int[] angleTable; /// /// Gets the angle histogram. /// - public int[] AngleHistogram => angleTable; + public int[] AngleHistogram { get; private set; } - int[] minAngles; /// /// Gets the min angles histogram. /// - public int[] MinAngleHistogram => minAngles; + public int[] MinAngleHistogram { get; private set; } - int[] maxAngles; /// /// Gets the max angles histogram. /// - public int[] MaxAngleHistogram => maxAngles; + public int[] MaxAngleHistogram { get; private set; } #endregion @@ -149,10 +138,10 @@ private void GetAspectHistogram(Mesh mesh) 100.0, 300.0, 1000.0, 10000.0, 100000.0, 0.0 }; - Otri tri = default(Otri); + var tri = default(Otri); Vertex[] p = new Vertex[3]; double[] dx = new double[3], dy = new double[3]; - double[] edgelength = new double[3]; + var edgelength = new double[3]; double triarea; double trilongest2; double triminaltitude2; @@ -202,8 +191,8 @@ private void GetAspectHistogram(Mesh mesh) #endregion - static readonly int[] plus1Mod3 = { 1, 2, 0 }; - static readonly int[] minus1Mod3 = { 2, 0, 1 }; + private static readonly int[] plus1Mod3 = { 1, 2, 0 }; + private static readonly int[] minus1Mod3 = { 2, 0, 1 }; /// /// Update statistics about the quality of the mesh. @@ -222,10 +211,10 @@ public void Update(Mesh mesh, int sampleDegrees) //sampleDegrees = 45; // sample every 4 degrees sampleDegrees = 60; // sample every 3 degrees - double[] cosSquareTable = new double[sampleDegrees / 2 - 1]; - double[] dx = new double[3]; - double[] dy = new double[3]; - double[] edgeLength = new double[3]; + var cosSquareTable = new double[sampleDegrees / 2 - 1]; + var dx = new double[3]; + var dy = new double[3]; + var edgeLength = new double[3]; double dotProduct; double cosSquare; double triArea; @@ -233,36 +222,36 @@ public void Update(Mesh mesh, int sampleDegrees) double triMinAltitude2; double triAspect2; - double radconst = Math.PI / sampleDegrees; - double degconst = 180.0 / Math.PI; + var radconst = Math.PI / sampleDegrees; + var degconst = 180.0 / Math.PI; // New angle table - angleTable = new int[sampleDegrees]; - minAngles = new int[sampleDegrees]; - maxAngles = new int[sampleDegrees]; + AngleHistogram = new int[sampleDegrees]; + MinAngleHistogram = new int[sampleDegrees]; + MaxAngleHistogram = new int[sampleDegrees]; - for (int i = 0; i < sampleDegrees / 2 - 1; i++) + for (var i = 0; i < sampleDegrees / 2 - 1; i++) { cosSquareTable[i] = Math.Cos(radconst * (i + 1)); cosSquareTable[i] = cosSquareTable[i] * cosSquareTable[i]; } - for (int i = 0; i < sampleDegrees; i++) + for (var i = 0; i < sampleDegrees; i++) { - angleTable[i] = 0; + AngleHistogram[i] = 0; } - minAspect = mesh.bounds.Width + mesh.bounds.Height; - minAspect = minAspect * minAspect; - maxAspect = 0.0; - minEdge = minAspect; - maxEdge = 0.0; - minArea = minAspect; - maxArea = 0.0; - minAngle = 0.0; - maxAngle = 2.0; + ShortestAltitude = mesh.bounds.Width + mesh.bounds.Height; + ShortestAltitude = ShortestAltitude * ShortestAltitude; + LargestAspectRatio = 0.0; + ShortestEdge = ShortestAltitude; + LongestEdge = 0.0; + SmallestArea = ShortestAltitude; + LargestArea = 0.0; + SmallestAngle = 0.0; + LargestAngle = 2.0; - bool acuteBiggest = true; - bool acuteBiggestTri = true; + var acuteBiggest = true; + var acuteBiggestTri = true; double triMinAngle, triMaxAngle = 1; @@ -277,7 +266,7 @@ public void Update(Mesh mesh, int sampleDegrees) triLongest2 = 0.0; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { k1 = plus1Mod3[i]; k2 = minus1Mod3[i]; @@ -292,14 +281,14 @@ public void Update(Mesh mesh, int sampleDegrees) triLongest2 = edgeLength[i]; } - if (edgeLength[i] > maxEdge) + if (edgeLength[i] > LongestEdge) { - maxEdge = edgeLength[i]; + LongestEdge = edgeLength[i]; } - if (edgeLength[i] < minEdge) + if (edgeLength[i] < ShortestEdge) { - minEdge = edgeLength[i]; + ShortestEdge = edgeLength[i]; } } @@ -307,29 +296,29 @@ public void Update(Mesh mesh, int sampleDegrees) triArea = Math.Abs((p[2].x - p[0].x) * (p[1].y - p[0].y) - (p[1].x - p[0].x) * (p[2].y - p[0].y)); - if (triArea < minArea) + if (triArea < SmallestArea) { - minArea = triArea; + SmallestArea = triArea; } - if (triArea > maxArea) + if (triArea > LargestArea) { - maxArea = triArea; + LargestArea = triArea; } triMinAltitude2 = triArea * triArea / triLongest2; - if (triMinAltitude2 < minAspect) + if (triMinAltitude2 < ShortestAltitude) { - minAspect = triMinAltitude2; + ShortestAltitude = triMinAltitude2; } triAspect2 = triLongest2 / triMinAltitude2; - if (triAspect2 > maxAspect) + if (triAspect2 > LargestAspectRatio) { - maxAspect = triAspect2; + LargestAspectRatio = triAspect2; } - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { k1 = plus1Mod3[i]; k2 = minus1Mod3[i]; @@ -338,7 +327,7 @@ public void Update(Mesh mesh, int sampleDegrees) cosSquare = dotProduct * dotProduct / (edgeLength[k1] * edgeLength[k2]); degreeStep = sampleDegrees / 2 - 1; - for (int j = degreeStep - 1; j >= 0; j--) + for (var j = degreeStep - 1; j >= 0; j--) { if (cosSquare > cosSquareTable[j]) { @@ -348,14 +337,14 @@ public void Update(Mesh mesh, int sampleDegrees) if (dotProduct <= 0.0) { - angleTable[degreeStep]++; - if (cosSquare > minAngle) + AngleHistogram[degreeStep]++; + if (cosSquare > SmallestAngle) { - minAngle = cosSquare; + SmallestAngle = cosSquare; } - if (acuteBiggest && (cosSquare < maxAngle)) + if (acuteBiggest && (cosSquare < LargestAngle)) { - maxAngle = cosSquare; + LargestAngle = cosSquare; } // Update min/max angle per triangle @@ -370,10 +359,10 @@ public void Update(Mesh mesh, int sampleDegrees) } else { - angleTable[sampleDegrees - degreeStep - 1]++; - if (acuteBiggest || (cosSquare > maxAngle)) + AngleHistogram[sampleDegrees - degreeStep - 1]++; + if (acuteBiggest || (cosSquare > LargestAngle)) { - maxAngle = cosSquare; + LargestAngle = cosSquare; acuteBiggest = false; } @@ -389,19 +378,19 @@ public void Update(Mesh mesh, int sampleDegrees) // Update min angle histogram degreeStep = sampleDegrees / 2 - 1; - for (int j = degreeStep - 1; j >= 0; j--) + for (var j = degreeStep - 1; j >= 0; j--) { if (triMinAngle > cosSquareTable[j]) { degreeStep = j; } } - minAngles[degreeStep]++; + MinAngleHistogram[degreeStep]++; // Update max angle histogram degreeStep = sampleDegrees / 2 - 1; - for (int j = degreeStep - 1; j >= 0; j--) + for (var j = degreeStep - 1; j >= 0; j--) { if (triMaxAngle > cosSquareTable[j]) { @@ -411,44 +400,44 @@ public void Update(Mesh mesh, int sampleDegrees) if (acuteBiggestTri) { - maxAngles[degreeStep]++; + MaxAngleHistogram[degreeStep]++; } else { - maxAngles[sampleDegrees - degreeStep - 1]++; + MaxAngleHistogram[sampleDegrees - degreeStep - 1]++; } acuteBiggestTri = true; } - minEdge = Math.Sqrt(minEdge); - maxEdge = Math.Sqrt(maxEdge); - minAspect = Math.Sqrt(minAspect); - maxAspect = Math.Sqrt(maxAspect); - minArea *= 0.5; - maxArea *= 0.5; - if (minAngle >= 1.0) + ShortestEdge = Math.Sqrt(ShortestEdge); + LongestEdge = Math.Sqrt(LongestEdge); + ShortestAltitude = Math.Sqrt(ShortestAltitude); + LargestAspectRatio = Math.Sqrt(LargestAspectRatio); + SmallestArea *= 0.5; + LargestArea *= 0.5; + if (SmallestAngle >= 1.0) { - minAngle = 0.0; + SmallestAngle = 0.0; } else { - minAngle = degconst * Math.Acos(Math.Sqrt(minAngle)); + SmallestAngle = degconst * Math.Acos(Math.Sqrt(SmallestAngle)); } - if (maxAngle >= 1.0) + if (LargestAngle >= 1.0) { - maxAngle = 180.0; + LargestAngle = 180.0; } else { if (acuteBiggest) { - maxAngle = degconst * Math.Acos(Math.Sqrt(maxAngle)); + LargestAngle = degconst * Math.Acos(Math.Sqrt(LargestAngle)); } else { - maxAngle = 180.0 - degconst * Math.Acos(Math.Sqrt(maxAngle)); + LargestAngle = 180.0 - degconst * Math.Acos(Math.Sqrt(LargestAngle)); } } } @@ -466,29 +455,29 @@ public void Update(Mesh mesh, int sampleDegrees) /// public static void ComputeAngles(ITriangle triangle, double[] data) { - double min = 0.0; - double max = 1.0; + var min = 0.0; + var max = 1.0; var va = triangle.GetVertex(0); var vb = triangle.GetVertex(1); var vc = triangle.GetVertex(2); - double dxa = vb.x - vc.x; - double dya = vb.y - vc.y; - double lena = dxa * dxa + dya * dya; + var dxa = vb.x - vc.x; + var dya = vb.y - vc.y; + var lena = dxa * dxa + dya * dya; - double dxb = vc.x - va.x; - double dyb = vc.y - va.y; - double lenb = dxb * dxb + dyb * dyb; + var dxb = vc.x - va.x; + var dyb = vc.y - va.y; + var lenb = dxb * dxb + dyb * dyb; - double dxc = va.x - vb.x; - double dyc = va.y - vb.y; - double lenc = dxc * dxc + dyc * dyc; + var dxc = va.x - vb.x; + var dyc = va.y - vb.y; + var lenc = dxc * dxc + dyc * dyc; // Dot products. - double dota = data[0] = dxb * dxc + dyb * dyc; - double dotb = data[1] = dxc * dxa + dyc * dya; - double dotc = data[2] = dxa * dxb + dya * dyb; + var dota = data[0] = dxb * dxc + dyb * dyc; + var dotb = data[1] = dxc * dxa + dyc * dya; + var dotc = data[2] = dxa * dxb + dya * dyb; // Squared cosines. data[3] = (dota * dota) / (lenb * lenc); @@ -498,11 +487,11 @@ public static void ComputeAngles(ITriangle triangle, double[] data) // The sign of the dot product will tell us, if the angle is // acute (value < 0) or obtuse (value > 0). - bool acute = true; + var acute = true; double cos, dot; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { dot = data[i]; cos = data[3 + i]; diff --git a/src/Triangle/Tools/TriangleQuadTree.cs b/src/Triangle/Tools/TriangleQuadTree.cs index 8c14f31..f16c6af 100644 --- a/src/Triangle/Tools/TriangleQuadTree.cs +++ b/src/Triangle/Tools/TriangleQuadTree.cs @@ -9,14 +9,14 @@ namespace TriangleNet.Tools { using System.Collections.Generic; using System.Linq; - using TriangleNet.Geometry; + using Geometry; /// /// A Quadtree implementation optimized for triangles. /// public class TriangleQuadTree { - QuadNode root; + private QuadNode root; internal ITriangle[] triangles; @@ -44,7 +44,7 @@ public TriangleQuadTree(Mesh mesh, int maxDepth = 10, int sizeBound = 10) triangles = mesh.Triangles.ToArray(); - int currentDepth = 0; + var currentDepth = 0; root = new QuadNode(mesh.Bounds, this, true); root.CreateSubRegion(++currentDepth); @@ -63,7 +63,7 @@ public ITriangle Query(double x, double y) foreach (var i in indices) { - var tri = this.triangles[i]; + var tri = triangles[i]; if (IsPointInTriangle(point, tri.GetVertex(0), tri.GetVertex(1), tri.GetVertex(2))) { @@ -85,15 +85,15 @@ public ITriangle Query(double x, double y) internal static bool IsPointInTriangle(Point p, Point t0, Point t1, Point t2) { // TODO: no need to create new Point instances here - Point d0 = new Point(t1.x - t0.x, t1.y - t0.y); - Point d1 = new Point(t2.x - t0.x, t2.y - t0.y); - Point d2 = new Point(p.x - t0.x, p.y - t0.y); + var d0 = new Point(t1.x - t0.x, t1.y - t0.y); + var d1 = new Point(t2.x - t0.x, t2.y - t0.y); + var d2 = new Point(p.x - t0.x, p.y - t0.y); // crossproduct of (0, 0, 1) and d0 - Point c0 = new Point(-d0.y, d0.x); + var c0 = new Point(-d0.y, d0.x); // crossproduct of (0, 0, 1) and d1 - Point c1 = new Point(-d1.y, d1.x); + var c1 = new Point(-d1.y, d1.x); // Linear combination d2 = s * d0 + v * d1. // @@ -103,8 +103,8 @@ internal static bool IsPointInTriangle(Point p, Point t0, Point t1, Point t2) // s = d2 * c1 / d0 * c1 // v = d2 * c0 / d1 * c0 - double s = DotProduct(d2, c1) / DotProduct(d0, c1); - double v = DotProduct(d2, c0) / DotProduct(d1, c0); + var s = DotProduct(d2, c1) / DotProduct(d0, c1); + var v = DotProduct(d2, c0) / DotProduct(d1, c0); if (s >= 0 && v >= 0 && ((s + v) <= 1)) { @@ -123,24 +123,24 @@ internal static double DotProduct(Point p, Point q) /// /// A node of the quadtree. /// - class QuadNode + private class QuadNode { - const int SW = 0; - const int SE = 1; - const int NW = 2; - const int NE = 3; + private const int SW = 0; + private const int SE = 1; + private const int NW = 2; + private const int NE = 3; - const double EPS = 1e-6; + private const double EPS = 1e-6; - static readonly byte[] BITVECTOR = { 0x1, 0x2, 0x4, 0x8 }; + private static readonly byte[] BITVECTOR = { 0x1, 0x2, 0x4, 0x8 }; - Rectangle bounds; - Point pivot; - TriangleQuadTree tree; - QuadNode[] regions; - List triangles; + private Rectangle bounds; + private Point pivot; + private TriangleQuadTree tree; + private QuadNode[] regions; + private List triangles; - byte bitRegions; + private byte bitRegions; public QuadNode(Rectangle box, TriangleQuadTree tree) : this(box, tree, false) @@ -151,22 +151,22 @@ public QuadNode(Rectangle box, TriangleQuadTree tree, bool init) { this.tree = tree; - this.bounds = new Rectangle(box.Left, box.Bottom, box.Width, box.Height); - this.pivot = new Point((box.Left + box.Right) / 2, (box.Bottom + box.Top) / 2); + bounds = new Rectangle(box.Left, box.Bottom, box.Width, box.Height); + pivot = new Point((box.Left + box.Right) / 2, (box.Bottom + box.Top) / 2); - this.bitRegions = 0; + bitRegions = 0; - this.regions = new QuadNode[4]; - this.triangles = new List(); + regions = new QuadNode[4]; + triangles = new List(); if (init) { - int count = tree.triangles.Length; + var count = tree.triangles.Length; // Allocate memory upfront triangles.Capacity = count; - for (int i = 0; i < count; i++) + for (var i = 0; i < count; i++) { triangles.Add(i); } @@ -175,7 +175,7 @@ public QuadNode(Rectangle box, TriangleQuadTree tree, bool init) public List FindTriangles(Point searchPoint) { - int region = FindRegion(searchPoint); + var region = FindRegion(searchPoint); if (regions[region] == null) { return triangles; @@ -217,7 +217,7 @@ public void CreateSubRegion(int currentDepth) // Find region for every triangle vertex foreach (var index in triangles) { - ITriangle tri = tree.triangles[index]; + var tri = tree.triangles[index]; triangle[0] = tri.GetVertex(0); triangle[1] = tri.GetVertex(1); @@ -226,7 +226,7 @@ public void CreateSubRegion(int currentDepth) AddTriangleToRegion(triangle, index); } - for (int i = 0; i < 4; i++) + for (var i = 0; i < 4; i++) { if (regions[i].triangles.Count > tree.sizeBound && currentDepth < tree.maxDepth) { @@ -235,10 +235,10 @@ public void CreateSubRegion(int currentDepth) } } - void AddTriangleToRegion(Point[] triangle, int index) + private void AddTriangleToRegion(Point[] triangle, int index) { bitRegions = 0; - if (TriangleQuadTree.IsPointInTriangle(pivot, triangle[0], triangle[1], triangle[2])) + if (IsPointInTriangle(pivot, triangle[0], triangle[1], triangle[2])) { AddToRegion(index, SW); AddToRegion(index, SE); @@ -252,12 +252,12 @@ void AddTriangleToRegion(Point[] triangle, int index) if (bitRegions == 0) { // we didn't find any intersection so we add this triangle to a point's region - int region = FindRegion(triangle[0]); + var region = FindRegion(triangle[0]); regions[region].triangles.Add(index); } } - void FindTriangleIntersections(Point[] triangle, int index) + private void FindTriangleIntersections(Point[] triangle, int index) { // PLEASE NOTE: // Handling of component comparison is tightly associated with the implementation @@ -268,11 +268,11 @@ void FindTriangleIntersections(Point[] triangle, int index) // pivot.x = triangle[0].x + t * (triangle[1].x - triangle[0].x) // pivot.y = triangle[0].y + t * (triangle[1].y - triangle[0].y) - int k = 2; + var k = 2; double dx, dy; // Iterate through all triangle laterals and find bounding box intersections - for (int i = 0; i < 3; k = i++) + for (var i = 0; i < 3; k = i++) { dx = triangle[i].x - triangle[k].x; dy = triangle[i].y - triangle[k].y; @@ -288,7 +288,7 @@ void FindTriangleIntersections(Point[] triangle, int index) } } - void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, int k) + private void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, int k) { double t; @@ -297,7 +297,7 @@ void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, i if (t < (1 + EPS) && t > -EPS) { // we have an intersection - double yComponent = triangle[k].y + t * dy; + var yComponent = triangle[k].y + t * dy; if (yComponent < pivot.y && yComponent >= bounds.Bottom) { @@ -316,7 +316,7 @@ void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, i if (t < (1 + EPS) && t > -EPS) { // we have an intersection - double yComponent = triangle[k].y + t * dy; + var yComponent = triangle[k].y + t * dy; if (yComponent < pivot.y && yComponent >= bounds.Bottom) { @@ -333,7 +333,7 @@ void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, i if (t < (1 + EPS) && t > -EPS) { // we have an intersection - double yComponent = triangle[k].y + t * dy; + var yComponent = triangle[k].y + t * dy; if (yComponent < pivot.y && yComponent >= bounds.Bottom) { @@ -346,7 +346,7 @@ void FindIntersectionsWithX(double dx, double dy, Point[] triangle, int index, i } } - void FindIntersectionsWithY(double dx, double dy, Point[] triangle, int index, int k) + private void FindIntersectionsWithY(double dx, double dy, Point[] triangle, int index, int k) { double t, xComponent; @@ -404,9 +404,9 @@ void FindIntersectionsWithY(double dx, double dy, Point[] triangle, int index, i } } - int FindRegion(Point point) + private int FindRegion(Point point) { - int b = 2; + var b = 2; if (point.y < pivot.y) { b = 0; @@ -418,7 +418,7 @@ int FindRegion(Point point) return b; } - void AddToRegion(int index, int region) + private void AddToRegion(int index, int region) { //if (!(m_bitRegions & BITVECTOR[region])) if ((bitRegions & BITVECTOR[region]) == 0) diff --git a/src/Triangle/Tools/VertexSorter.cs b/src/Triangle/Tools/VertexSorter.cs index 87092f5..8093fe8 100644 --- a/src/Triangle/Tools/VertexSorter.cs +++ b/src/Triangle/Tools/VertexSorter.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Tools { using System; - using TriangleNet.Geometry; + using Geometry; /// /// Sort an array of points using quicksort. @@ -17,14 +17,14 @@ public class VertexSorter { private const int RANDOM_SEED = 57113; - Random rand; + private Random rand; - Vertex[] points; + private Vertex[] points; - VertexSorter(Vertex[] points, int seed) + private VertexSorter(Vertex[] points, int seed) { this.points = points; - this.rand = new Random(seed); + rand = new Random(seed); } /// @@ -49,7 +49,7 @@ public static void Alternate(Vertex[] array, int length, int seed = RANDOM_SEED) { var qs = new VertexSorter(array, seed); - int divider = length >> 1; + var divider = length >> 1; // Re-sort the array of vertices to accommodate alternating cuts. if (length - divider >= 2) @@ -76,22 +76,22 @@ public static void Alternate(Vertex[] array, int length, int seed = RANDOM_SEED) /// private void QuickSort(int left, int right) { - int oleft = left; - int oright = right; - int arraysize = right - left + 1; + var oleft = left; + var oright = right; + var arraysize = right - left + 1; int pivot; double pivotx, pivoty; Vertex temp; - var array = this.points; + var array = points; if (arraysize < 32) { // Insertion sort - for (int i = left + 1; i <= right; i++) + for (var i = left + 1; i <= right; i++) { var a = array[i]; - int j = i - 1; + var j = i - 1; while (j >= left && (array[j].x > a.x || (array[j].x == a.x && array[j].y > a.y))) { array[j + 1] = array[j]; @@ -168,8 +168,8 @@ private void QuickSort(int left, int right) /// private void AlternateAxes(int left, int right, int axis) { - int size = right - left + 1; - int divider = size >> 1; + var size = right - left + 1; + var divider = size >> 1; if (size <= 3) { @@ -213,13 +213,13 @@ private void AlternateAxes(int left, int right, int axis) /// private void VertexMedianX(int left, int right, int median) { - int arraysize = right - left + 1; + var arraysize = right - left + 1; int oleft = left, oright = right; int pivot; double px, py; // pivot x and y coordinatex Vertex temp; - var array = this.points; + var array = points; if (arraysize == 2) { @@ -296,13 +296,13 @@ private void VertexMedianX(int left, int right, int median) /// private void VertexMedianY(int left, int right, int median) { - int arraysize = right - left + 1; + var arraysize = right - left + 1; int oleft = left, oright = right; int pivot; double px, py; // pivot x and y coordinatex Vertex temp; - var array = this.points; + var array = points; if (arraysize == 2) { diff --git a/src/Triangle/Topology/DCEL/DcelMesh.cs b/src/Triangle/Topology/DCEL/DcelMesh.cs index 4dcfd67..7353318 100644 --- a/src/Triangle/Topology/DCEL/DcelMesh.cs +++ b/src/Triangle/Topology/DCEL/DcelMesh.cs @@ -7,22 +7,13 @@ namespace TriangleNet.Topology.DCEL { using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; /// /// DCEL mesh. /// public class DcelMesh { - /// List of vertices. - protected List vertices; - - /// List of half-edges. - protected List edges; - - /// List of faces. - protected List faces; - /// /// Initializes a new instance of the class. /// @@ -39,43 +30,33 @@ protected DcelMesh(bool initialize) { if (initialize) { - vertices = new List(); - edges = new List(); - faces = new List(); + Vertices.Clear(); + edges.Clear(); + Faces.Clear(); } } /// /// Gets the vertices of the Voronoi diagram. /// - public List Vertices - { - get { return vertices; } - } + public List Vertices { get; set; } = new(); /// /// Gets the list of half-edges specify the Voronoi diagram topology. /// - public List HalfEdges - { - get { return edges; } - } + public List HalfEdges { get; set; } = new(); + protected List edges = new(); + /// /// Gets the faces of the Voronoi diagram. /// - public List Faces - { - get { return faces; } - } + public List Faces { get; set; } = new(); /// /// Gets the collection of edges of the Voronoi diagram. /// - public IEnumerable Edges - { - get { return EnumerateEdges(); } - } + public IEnumerable Edges => EnumerateEdges(); /// /// Check if the DCEL is consistent. @@ -93,14 +74,14 @@ public IEnumerable Edges public virtual bool IsConsistent(bool closed = true, int depth = 0) { // Check vertices for null pointers. - foreach (var vertex in vertices) + foreach (var vertex in Vertices) { if (vertex.id < 0) { continue; } - if (vertex.leaving == null) + if (vertex.Leaving == null) { return false; } @@ -112,7 +93,7 @@ public virtual bool IsConsistent(bool closed = true, int depth = 0) } // Check faces for null pointers. - foreach (var face in faces) + foreach (var face in Faces) { if (face.ID < 0) { @@ -133,6 +114,8 @@ public virtual bool IsConsistent(bool closed = true, int depth = 0) // Check half-edges for null pointers. foreach (var edge in edges) { + if (edge is null) continue; + if (edge.id < 0) { continue; @@ -192,7 +175,7 @@ public virtual bool IsConsistent(bool closed = true, int depth = 0) if (closed && depth > 0) { // Check if faces are closed. - foreach (var face in faces) + foreach (var face in Faces) { if (face.id < 0) { @@ -200,10 +183,12 @@ public virtual bool IsConsistent(bool closed = true, int depth = 0) } var edge = face.edge; + if (edge is null) continue; + var next = edge.next; - int id = edge.id; - int k = 0; + var id = edge.id; + var k = 0; while (next.id != id && k < depth) { @@ -234,9 +219,9 @@ public void ResolveBoundaryEdges() var map = new Dictionary(); // TODO: parallel? - foreach (var edge in this.edges) + foreach (var edge in edges) { - if (edge.twin == null) + if (edge.twin is null) { var twin = edge.twin = new HalfEdge(edge.next.origin, Face.Empty); twin.twin = edge; @@ -245,14 +230,14 @@ public void ResolveBoundaryEdges() } } - int j = edges.Count; + var j = edges.Count; foreach (var edge in map.Values) { edge.id = j++; edge.next = map[edge.twin.origin.id]; - this.edges.Add(edge); + edges.Add(edge); } } diff --git a/src/Triangle/Topology/DCEL/Face.cs b/src/Triangle/Topology/DCEL/Face.cs index db4a10b..4f712da 100644 --- a/src/Triangle/Topology/DCEL/Face.cs +++ b/src/Triangle/Topology/DCEL/Face.cs @@ -7,7 +7,7 @@ namespace TriangleNet.Topology.DCEL { using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; /// /// A face of the DCEL datastructure. @@ -33,9 +33,9 @@ static Face() internal int mark; // If the face is a Voronoi cell, this is the point that generates the cell. - internal Point generator; + internal Point? generator; - internal HalfEdge edge; + internal HalfEdge? edge; internal bool bounded; /// @@ -49,17 +49,17 @@ static Face() /// public int ID { - get { return id; } - set { id = value; } + get => id; + set => id = value; } /// /// Gets or sets a half-edge connected to the face. /// - public HalfEdge Edge + public HalfEdge? Edge { - get { return edge; } - set { edge = value; } + get => edge; + set => edge = value; } /// @@ -67,8 +67,8 @@ public HalfEdge Edge /// public bool Bounded { - get { return bounded; } - set { bounded = value; } + get => bounded; + set => bounded = value; } /// @@ -85,14 +85,14 @@ public Face(Point generator) /// /// The generator of this face (for Voronoi diagram) /// The half-edge connected to this face. - public Face(Point generator, HalfEdge edge) + public Face(Point? generator, HalfEdge? edge) { this.generator = generator; this.edge = edge; bounded = true; - if (generator != null) + if (generator is not null) { id = generator.ID; } @@ -105,7 +105,7 @@ public Face(Point generator, HalfEdge edge) public IEnumerable EnumerateEdges() { var edge = Edge; - int first = edge.ID; + var first = edge.ID; do { diff --git a/src/Triangle/Topology/DCEL/HalfEdge.cs b/src/Triangle/Topology/DCEL/HalfEdge.cs index cbf536d..4605dd3 100644 --- a/src/Triangle/Topology/DCEL/HalfEdge.cs +++ b/src/Triangle/Topology/DCEL/HalfEdge.cs @@ -14,18 +14,18 @@ public class HalfEdge internal int id; internal int mark; - internal Vertex origin; - internal Face face; - internal HalfEdge twin; - internal HalfEdge next; + internal Vertex? origin; + internal Face? face; + internal HalfEdge? twin; + internal HalfEdge? next; /// /// Gets or sets the half-edge id. /// public int ID { - get { return id; } - set { id = value; } + get => id; + set => id = value; } /// @@ -33,44 +33,44 @@ public int ID /// public int Boundary { - get { return mark; } - set { mark = value; } + get => mark; + set => mark = value; } /// /// Gets or sets the origin of the half-edge. /// - public Vertex Origin + public Vertex? Origin { - get { return origin; } - set { origin = value; } + get => origin; + set => origin = value; } /// /// Gets or sets the face connected to the half-edge. /// - public Face Face + public Face? Face { - get { return face; } - set { face = value; } + get => face; + set => face = value; } /// /// Gets or sets the twin of the half-edge. /// - public HalfEdge Twin + public HalfEdge? Twin { - get { return twin; } - set { twin = value; } + get => twin; + set => twin = value; } /// /// Gets or sets the next pointer of the half-edge. /// - public HalfEdge Next + public HalfEdge? Next { - get { return next; } - set { next = value; } + get => next; + set => next = value; } /// @@ -87,7 +87,7 @@ public HalfEdge(Vertex origin) /// /// The origin of this half-edge. /// The face connected to this half-edge. - public HalfEdge(Vertex origin, Face face) + public HalfEdge(Vertex origin, Face? face) { this.origin = origin; this.face = face; @@ -100,9 +100,6 @@ public HalfEdge(Vertex origin, Face face) } /// - public override string ToString() - { - return string.Format("HE-ID {0} (Origin = VID-{1})", id, origin.id); - } + public override string ToString() => $"HE-ID {id} (Origin = VID-{origin?.id})"; } } diff --git a/src/Triangle/Topology/DCEL/Vertex.cs b/src/Triangle/Topology/DCEL/Vertex.cs index 1239f6d..e355192 100644 --- a/src/Triangle/Topology/DCEL/Vertex.cs +++ b/src/Triangle/Topology/DCEL/Vertex.cs @@ -11,18 +11,12 @@ namespace TriangleNet.Topology.DCEL /// /// A vertex of the DCEL datastructure. /// - public class Vertex : TriangleNet.Geometry.Point + public class Vertex : Geometry.Point { - internal HalfEdge leaving; - /// /// Gets or sets a half-edge leaving the vertex. /// - public HalfEdge Leaving - { - get { return leaving; } - set { leaving = value; } - } + public HalfEdge? Leaving { get; set; } /// /// Initializes a new instance of the class. @@ -43,7 +37,7 @@ public Vertex(double x, double y) public Vertex(double x, double y, HalfEdge leaving) : base(x, y) { - this.leaving = leaving; + Leaving = leaving; } /// @@ -52,8 +46,9 @@ public Vertex(double x, double y, HalfEdge leaving) /// public IEnumerable EnumerateEdges() { - var edge = this.Leaving; - int first = edge.ID; + var edge = Leaving; + if (edge is null) yield break; + var first = edge.ID; do { @@ -64,9 +59,6 @@ public IEnumerable EnumerateEdges() } /// - public override string ToString() - { - return string.Format("V-ID {0}", base.id); - } + public override string ToString() => $"V-ID {id}"; } } diff --git a/src/Triangle/Topology/Osub.cs b/src/Triangle/Topology/Osub.cs index fba9438..d9e5602 100644 --- a/src/Triangle/Topology/Osub.cs +++ b/src/Triangle/Topology/Osub.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Topology { using System; - using TriangleNet.Geometry; + using Geometry; /// /// An oriented subsegment. diff --git a/src/Triangle/Topology/Otri.cs b/src/Triangle/Topology/Otri.cs index be8c207..a0dd367 100644 --- a/src/Triangle/Topology/Otri.cs +++ b/src/Triangle/Topology/Otri.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Topology { using System; - using TriangleNet.Geometry; + using Geometry; /// /// An oriented triangle. @@ -20,7 +20,7 @@ namespace TriangleNet.Topology /// public struct Otri { - internal Triangle tri; + internal Triangle? tri; internal int orient; // Ranges from 0 to 2. public int Orient => orient; @@ -28,10 +28,10 @@ public struct Otri /// /// Gets or sets the triangle. /// - public Triangle Triangle + public Triangle? Triangle { - get { return tri; } - set { tri = value; } + get => tri; + set => tri = value; } /// @@ -47,8 +47,8 @@ public override string ToString() #region Otri primitives (public) // For fast access - static readonly int[] plus1Mod3 = { 1, 2, 0 }; - static readonly int[] minus1Mod3 = { 2, 0, 1 }; + private static readonly int[] plus1Mod3 = { 1, 2, 0 }; + private static readonly int[] minus1Mod3 = { 2, 0, 1 }; // The following primitives are all described by Guibas and Stolfi. // However, Guibas and Stolfi use an edge-based data structure, @@ -98,7 +98,7 @@ public void Sym(ref Otri ot) /// public void Sym() { - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; } @@ -147,7 +147,7 @@ public void Onext(ref Otri ot) ot.orient = minus1Mod3[orient]; //ot.SymSelf(); - int tmp = ot.orient; + var tmp = ot.orient; ot.orient = ot.tri.neighbors[tmp].orient; ot.tri = ot.tri.neighbors[tmp].tri; } @@ -161,7 +161,7 @@ public void Onext() orient = minus1Mod3[orient]; //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; } @@ -185,7 +185,7 @@ public void Oprev(ref Otri ot) public void Oprev() { //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; @@ -212,7 +212,7 @@ public void Dnext(ref Otri ot) public void Dnext() { //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; @@ -230,7 +230,7 @@ public void Dprev(ref Otri ot) ot.orient = plus1Mod3[orient]; //ot.SymSelf(); - int tmp = ot.orient; + var tmp = ot.orient; ot.orient = ot.tri.neighbors[tmp].orient; ot.tri = ot.tri.neighbors[tmp].tri; } @@ -244,7 +244,7 @@ public void Dprev() orient = plus1Mod3[orient]; //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; } @@ -262,7 +262,7 @@ public void Rnext(ref Otri ot) ot.orient = plus1Mod3[ot.orient]; //ot.SymSelf(); - int tmp = ot.orient; + var tmp = ot.orient; ot.orient = ot.tri.neighbors[tmp].orient; ot.tri = ot.tri.neighbors[tmp].tri; } @@ -273,7 +273,7 @@ public void Rnext(ref Otri ot) public void Rnext() { //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; @@ -299,7 +299,7 @@ public void Rprev(ref Otri ot) ot.orient = minus1Mod3[ot.orient]; //ot.SymSelf(); - int tmp = ot.orient; + var tmp = ot.orient; ot.orient = ot.tri.neighbors[tmp].orient; ot.tri = ot.tri.neighbors[tmp].tri; } @@ -310,7 +310,7 @@ public void Rprev(ref Otri ot) public void Rprev() { //SymSelf(); - int tmp = orient; + var tmp = orient; orient = tri.neighbors[tmp].orient; tri = tri.neighbors[tmp].tri; @@ -326,25 +326,25 @@ public void Rprev() /// /// Origin [org(abc) -> a] /// - public Vertex Org() + public Vertex? Org() { - return tri.vertices[plus1Mod3[orient]]; + return tri?.vertices[plus1Mod3[orient]]; } /// /// Destination [dest(abc) -> b] /// - public Vertex Dest() + public Vertex? Dest() { - return tri.vertices[minus1Mod3[orient]]; + return tri?.vertices[minus1Mod3[orient]]; } /// /// Apex [apex(abc) -> c] /// - public Vertex Apex() + public Vertex? Apex() { - return tri.vertices[orient]; + return tri?.vertices[orient]; } /// @@ -408,8 +408,8 @@ internal void Bond(ref Otri ot) tri.neighbors[orient].tri = ot.tri; tri.neighbors[orient].orient = ot.orient; - ot.tri.neighbors[ot.orient].tri = this.tri; - ot.tri.neighbors[ot.orient].orient = this.orient; + ot.tri.neighbors[ot.orient].tri = tri; + ot.tri.neighbors[ot.orient].orient = orient; } /// diff --git a/src/Triangle/Topology/SubSegment.cs b/src/Triangle/Topology/SubSegment.cs index d12752e..c5c4ee6 100644 --- a/src/Triangle/Topology/SubSegment.cs +++ b/src/Triangle/Topology/SubSegment.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Topology { using System; - using TriangleNet.Geometry; + using Geometry; /// /// The subsegment data structure. diff --git a/src/Triangle/Topology/Triangle.cs b/src/Triangle/Topology/Triangle.cs index d73d01f..c397463 100644 --- a/src/Triangle/Topology/Triangle.cs +++ b/src/Triangle/Topology/Triangle.cs @@ -8,7 +8,7 @@ namespace TriangleNet.Topology { using System; - using TriangleNet.Geometry; + using Geometry; /// /// The triangle data structure. @@ -52,8 +52,8 @@ public Triangle() /// public int ID { - get { return this.id; } - set { this.id = value; } + get => id; + set => id = value; } /// @@ -61,8 +61,8 @@ public int ID /// public int Label { - get { return this.label; } - set { this.label = value; } + get => label; + set => label = value; } /// @@ -70,8 +70,8 @@ public int Label /// public double Area { - get { return this.area; } - set { this.area = value; } + get => area; + set => area = value; } /// @@ -81,7 +81,7 @@ public double Area /// public Vertex GetVertex(int index) { - return this.vertices[index]; // TODO: Check range? + return vertices[index]; // TODO: Check range? } /// @@ -91,7 +91,7 @@ public Vertex GetVertex(int index) /// public int GetVertexID(int index) { - return this.vertices[index].id; + return vertices[index].id; } /// @@ -125,7 +125,7 @@ public ISegment GetSegment(int index) /// public override int GetHashCode() { - return this.hash; + return hash; } /// diff --git a/src/Triangle/Triangle.csproj b/src/Triangle/Triangle.csproj index 662a324..45eeb89 100644 --- a/src/Triangle/Triangle.csproj +++ b/src/Triangle/Triangle.csproj @@ -9,6 +9,8 @@ Christian Woltering 1.0.0 1.0.0-beta5 + default + enable diff --git a/src/Triangle/Triangle.csproj.DotSettings b/src/Triangle/Triangle.csproj.DotSettings new file mode 100644 index 0000000..3d80b14 --- /dev/null +++ b/src/Triangle/Triangle.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/src/Triangle/TriangleLocator.cs b/src/Triangle/TriangleLocator.cs index 05b2295..9c982e2 100644 --- a/src/Triangle/TriangleLocator.cs +++ b/src/Triangle/TriangleLocator.cs @@ -8,8 +8,8 @@ namespace TriangleNet { using System; - using TriangleNet.Geometry; - using TriangleNet.Topology; + using Geometry; + using Topology; /// /// Locate triangles in a mesh. @@ -132,8 +132,8 @@ internal void Reset() public LocateResult PreciseLocate(Point searchpoint, ref Otri searchtri, bool stopatsubsegment) { - Otri backtracktri = default(Otri); - Osub checkedge = default(Osub); + var backtracktri = default(Otri); + var checkedge = default(Osub); Vertex forg, fdest, fapex; double orgorient, destorient; bool moveleft; @@ -274,7 +274,7 @@ public LocateResult PreciseLocate(Point searchpoint, ref Otri searchtri, /// public LocateResult Locate(Point searchpoint, ref Otri searchtri) { - Otri sampletri = default(Otri); + var sampletri = default(Otri); Vertex torg, tdest; double searchdist, dist; double ahead; diff --git a/src/Triangle/TrianglePool.cs b/src/Triangle/TrianglePool.cs index 8b7f1ef..112c430 100644 --- a/src/Triangle/TrianglePool.cs +++ b/src/Triangle/TrianglePool.cs @@ -8,7 +8,7 @@ namespace TriangleNet { using System; using System.Collections.Generic; - using TriangleNet.Topology; + using Topology; /// /// Pool datastructure storing triangles of a . @@ -19,31 +19,30 @@ public class TrianglePool : ICollection private const int BLOCKSIZE = 1024; // The total number of currently allocated triangles. - int size; // The number of triangles currently used. - int count; + private int count; // The pool. - Triangle[][] pool; + private Triangle[][] pool; // A stack of free triangles. - Stack stack; + private Stack stack; /// /// Gets the total number of currently allocated triangles. /// - public int Capacity => size; + public int Capacity { get; private set; } /// /// Initializes a new instance of the class. /// public TrianglePool() { - size = 0; + Capacity = 0; // On startup, the pool should be able to hold 2^16 triangles. - int n = Math.Max(1, 65536 / BLOCKSIZE); + var n = Math.Max(1, 65536 / BLOCKSIZE); pool = new Triangle[n][]; pool[0] = new Triangle[BLOCKSIZE]; @@ -66,7 +65,7 @@ public Triangle Get() Cleanup(triangle); } - else if (count < size) + else if (count < Capacity) { triangle = pool[count / BLOCKSIZE][count % BLOCKSIZE]; triangle.id = triangle.hash; @@ -78,10 +77,10 @@ public Triangle Get() else { triangle = new Triangle(); - triangle.hash = size; + triangle.hash = Capacity; triangle.id = triangle.hash; - int block = size / BLOCKSIZE; + var block = Capacity / BLOCKSIZE; if (pool[block] == null) { @@ -95,9 +94,9 @@ public Triangle Get() } // Add triangle to pool. - pool[block][size % BLOCKSIZE] = triangle; + pool[block][Capacity % BLOCKSIZE] = triangle; - count = ++size; + count = ++Capacity; } return triangle; @@ -140,7 +139,7 @@ public TrianglePool Restart() /// internal IEnumerable Sample(int k, Random random) { - int i, count = this.Count; + int i, count = Count; if (k > count) { @@ -172,12 +171,12 @@ private void Cleanup(Triangle triangle) triangle.area = 0.0; triangle.infected = false; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { triangle.vertices[i] = null; - triangle.subsegs[i] = default(Osub); - triangle.neighbors[i] = default(Otri); + triangle.subsegs[i] = default; + triangle.neighbors[i] = default; } } @@ -196,30 +195,30 @@ public void Clear() { stack.Clear(); - int blocks = (size / BLOCKSIZE) + 1; + var blocks = (Capacity / BLOCKSIZE) + 1; - for (int i = 0; i < blocks; i++) + for (var i = 0; i < blocks; i++) { var block = pool[i]; // Number of triangles in current block: - int length = (size - i * BLOCKSIZE) % BLOCKSIZE; + var length = (Capacity - i * BLOCKSIZE) % BLOCKSIZE; - for (int j = 0; j < length; j++) + for (var j = 0; j < length; j++) { block[j] = null; } } - size = count = 0; + Capacity = count = 0; } /// public bool Contains(Triangle item) { - int i = item.hash; + var i = item.hash; - if (i < 0 || i > size) + if (i < 0 || i > Capacity) { return false; } @@ -230,7 +229,7 @@ public bool Contains(Triangle item) /// public void CopyTo(Triangle[] array, int index) { - var enumerator = GetEnumerator(); + using var enumerator = GetEnumerator(); while (enumerator.MoveNext()) { @@ -262,50 +261,42 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() return GetEnumerator(); } - class Enumerator : IEnumerator + private class Enumerator : IEnumerator { // TODO: enumerator should be able to tell if collection changed. - int count; + private int count; - Triangle[][] pool; + private Triangle[][] pool; - Triangle current; - - int index, offset; + private int index, offset; public Enumerator(TrianglePool pool) { - this.count = pool.Count; + count = pool.Count; this.pool = pool.pool; index = 0; offset = 0; } - public Triangle Current - { - get { return current; } - } + public Triangle Current { get; private set; } public void Dispose() { } - object System.Collections.IEnumerator.Current - { - get { return current; } - } + object System.Collections.IEnumerator.Current => Current; public bool MoveNext() { while (index < count) { - current = pool[offset / BLOCKSIZE][offset % BLOCKSIZE]; + Current = pool[offset / BLOCKSIZE][offset % BLOCKSIZE]; offset++; - if (current.hash >= 0) + if (Current.hash >= 0) { index++; return true; diff --git a/src/Triangle/TriangleSampler.cs b/src/Triangle/TriangleSampler.cs index cf9ab0d..a9ba9b1 100644 --- a/src/Triangle/TriangleSampler.cs +++ b/src/Triangle/TriangleSampler.cs @@ -9,12 +9,12 @@ namespace TriangleNet { using System; using System.Collections.Generic; - using TriangleNet.Topology; + using Topology; /// /// Used for triangle sampling in the class. /// - class TriangleSampler : IEnumerable + internal class TriangleSampler : IEnumerable { // Empirically chosen factor. private const int samplefactor = 11; @@ -26,7 +26,7 @@ class TriangleSampler : IEnumerable private int samples = 1; // Number of triangles in mesh. - private int triangleCount = 0; + private int triangleCount; public TriangleSampler(Mesh mesh, Random random) { @@ -48,7 +48,7 @@ public void Reset() /// public void Update() { - int count = mesh.triangles.Count; + var count = mesh.triangles.Count; if (triangleCount != count) { diff --git a/src/Triangle/Voronoi/BoundedVoronoi.cs b/src/Triangle/Voronoi/BoundedVoronoi.cs index d82bc1e..70cb293 100644 --- a/src/Triangle/Voronoi/BoundedVoronoi.cs +++ b/src/Triangle/Voronoi/BoundedVoronoi.cs @@ -6,18 +6,18 @@ namespace TriangleNet.Voronoi { - using TriangleNet.Geometry; - using TriangleNet.Tools; - using TriangleNet.Topology.DCEL; + using Geometry; + using Tools; + using Topology.DCEL; - using TVertex = TriangleNet.Geometry.Vertex; + using TVertex = Geometry.Vertex; /// /// Computing the bounded Voronoi diagram of a constrained and conforming Delaunay triangulation. /// public class BoundedVoronoi : VoronoiBase { - int offset; + private int offset; /// /// Initializes a new instance of the class. @@ -39,10 +39,10 @@ public BoundedVoronoi(Mesh mesh, IVoronoiFactory factory, IPredicates predicates { // We explicitly told the base constructor to call the Generate method, so // at this point the basic Voronoi diagram is already created. - offset = base.vertices.Count; + offset = Vertices.Count; // Each vertex of the hull will be part of a Voronoi cell. - base.vertices.Capacity = offset + mesh.hullsize; + Vertices.Capacity = offset + mesh.hullsize; // Create bounded Voronoi diagram. PostProcess(); @@ -62,7 +62,7 @@ private void PostProcess() var v1 = (TVertex)edge.face.generator; var v2 = (TVertex)twin.face.generator; - double dir = predicates.CounterClockwise(v1, v2, edge.origin); + var dir = predicates.CounterClockwise(v1, v2, edge.origin); if (dir <= 0) { @@ -100,21 +100,21 @@ private void HandleCase1(HalfEdge edge, TVertex v1, TVertex v2) h1.next = h2; h2.next = edge.face.edge; - gen.leaving = h2; + gen.Leaving = h2; // Let the face edge point to the edge leaving at generator. edge.face.edge = h2; - base.edges.Add(h1); - base.edges.Add(h2); + edges.Add(h1); + edges.Add(h2); - int count = base.edges.Count; + var count = edges.Count; h1.id = count; h2.id = count + 1; gen.id = offset++; - base.vertices.Add(gen); + Vertices.Add(gen); } /// @@ -160,7 +160,7 @@ private void HandleCase2(HalfEdge edge, TVertex v1, TVertex v2) var gen = factory.CreateVertex(v1.x, v1.y); var he = factory.CreateHalfEdge(gen, edge.face); - gen.leaving = he; + gen.Leaving = he; edge.next = he; he.next = edge.face.edge; @@ -169,40 +169,12 @@ private void HandleCase2(HalfEdge edge, TVertex v1, TVertex v2) // Let the face edge point to the edge leaving at generator. edge.face.edge = he; - base.edges.Add(he); + edges.Add(he); - he.id = base.edges.Count; + he.id = edges.Count; gen.id = offset++; - base.vertices.Add(gen); + Vertices.Add(gen); } - - /* - private int GetBoundaryMark(Vertex v) - { - Otri tri = default(Otri); - Otri next = default(Otri); - Osub seg = default(Osub); - - // Get triangle connected to generator. - v.tri.Copy(ref tri); - v.tri.Copy(ref next); - - // Find boundary triangle. - while (next.triangle.id != -1) - { - next.Copy(ref tri); - next.OnextSelf(); - } - - // Find edge dual to current half-edge. - tri.LnextSelf(); - tri.LnextSelf(); - - tri.SegPivot(ref seg); - - return seg.seg.boundary; - } - //*/ } } diff --git a/src/Triangle/Voronoi/DefaultVoronoiFactory.cs b/src/Triangle/Voronoi/DefaultVoronoiFactory.cs index 43f2cfc..1291405 100644 --- a/src/Triangle/Voronoi/DefaultVoronoiFactory.cs +++ b/src/Triangle/Voronoi/DefaultVoronoiFactory.cs @@ -2,7 +2,7 @@ namespace TriangleNet.Voronoi { using System; - using TriangleNet.Topology.DCEL; + using Topology.DCEL; /// /// Default factory for Voronoi / DCEL mesh objects. diff --git a/src/Triangle/Voronoi/IVoronoiFactory.cs b/src/Triangle/Voronoi/IVoronoiFactory.cs index 2c3108d..c92f3f1 100644 --- a/src/Triangle/Voronoi/IVoronoiFactory.cs +++ b/src/Triangle/Voronoi/IVoronoiFactory.cs @@ -1,7 +1,7 @@  namespace TriangleNet.Voronoi { - using TriangleNet.Topology.DCEL; + using Topology.DCEL; /// /// Factory for Voronoi DCEL datastructure. diff --git a/src/Triangle/Voronoi/Legacy/BoundedVoronoiLegacy.cs b/src/Triangle/Voronoi/Legacy/BoundedVoronoiLegacy.cs index c574bcb..651aafd 100644 --- a/src/Triangle/Voronoi/Legacy/BoundedVoronoiLegacy.cs +++ b/src/Triangle/Voronoi/Legacy/BoundedVoronoiLegacy.cs @@ -8,8 +8,8 @@ namespace TriangleNet.Voronoi.Legacy { using System; using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// The Bounded Voronoi Diagram is the dual of a PSLG triangulation. @@ -21,20 +21,20 @@ namespace TriangleNet.Voronoi.Legacy [Obsolete("Use TriangleNet.Voronoi.BoundedVoronoi class instead.")] public class BoundedVoronoiLegacy : IVoronoi { - IPredicates predicates = RobustPredicates.Default; + private IPredicates predicates = RobustPredicates.Default; - Mesh mesh; + private Mesh mesh; - Point[] points; - List regions; + private Point[] points; + private List regions; // Used for new points on segments. - List segPoints; - int segIndex; + private List segPoints; + private int segIndex; - Dictionary subsegMap; + private Dictionary subsegMap; - bool includeBoundary = true; + private bool includeBoundary = true; /// /// Initializes a new instance of the class. @@ -60,23 +60,14 @@ public BoundedVoronoiLegacy(Mesh mesh, bool includeBoundary) /// /// Gets the list of Voronoi vertices. /// - public Point[] Points - { - get { return points; } - } + public Point[] Points => points; /// /// Gets the list of Voronoi regions. /// - public ICollection Regions - { - get { return regions; } - } + public ICollection Regions => regions; - public IEnumerable Edges - { - get { return EnumerateEdges(); } - } + public IEnumerable Edges => EnumerateEdges(); /// /// Computes the bounded voronoi diagram. @@ -87,10 +78,10 @@ private void Generate() mesh.MakeVertexMap(); // Allocate space for voronoi diagram - this.regions = new List(mesh.vertices.Count); + regions = new List(mesh.vertices.Count); - this.points = new Point[mesh.triangles.Count]; - this.segPoints = new List(mesh.subsegs.Count * 4); + points = new Point[mesh.triangles.Count]; + segPoints = new List(mesh.subsegs.Count * 4); ComputeCircumCenters(); @@ -110,22 +101,22 @@ private void Generate() } // Add the new points on segments to the point array. - int length = points.Length; + var length = points.Length; Array.Resize(ref points, length + segPoints.Count); - for (int i = 0; i < segPoints.Count; i++) + for (var i = 0; i < segPoints.Count; i++) { points[length + i] = segPoints[i]; } - this.segPoints.Clear(); - this.segPoints = null; + segPoints.Clear(); + segPoints = null; } private void ComputeCircumCenters() { - Otri tri = default(Otri); + var tri = default(Otri); double xi = 0, eta = 0; Point pt; @@ -150,15 +141,15 @@ private void ComputeCircumCenters() /// private void TagBlindTriangles() { - int blinded = 0; + var blinded = 0; Stack triangles; subsegMap = new Dictionary(); - Otri f = default(Otri); - Otri f0 = default(Otri); - Osub e = default(Osub); - Osub sub1 = default(Osub); + var f = default(Otri); + var f0 = default(Otri); + var e = default(Osub); + var sub1 = default(Osub); // Tag all triangles non-blind foreach (var t in mesh.triangles) @@ -241,14 +232,14 @@ private bool TriangleIsBlinded(ref Otri tri, ref Osub seg) { Point c, pt; - Vertex torg = tri.Org(); - Vertex tdest = tri.Dest(); - Vertex tapex = tri.Apex(); + var torg = tri.Org(); + var tdest = tri.Dest(); + var tapex = tri.Apex(); - Vertex sorg = seg.Org(); - Vertex sdest = seg.Dest(); + var sorg = seg.Org(); + var sdest = seg.Dest(); - c = this.points[tri.tri.id]; + c = points[tri.tri.id]; if (SegmentsIntersect(sorg, sdest, c, torg, out pt, true)) { @@ -270,18 +261,18 @@ private bool TriangleIsBlinded(ref Otri tri, ref Osub seg) private void ConstructCell(Vertex vertex) { - VoronoiRegion region = new VoronoiRegion(vertex); + var region = new VoronoiRegion(vertex); regions.Add(region); - Otri f = default(Otri); - Otri f_init = default(Otri); - Otri f_next = default(Otri); - Osub sf = default(Osub); - Osub sfn = default(Osub); + var f = default(Otri); + var f_init = default(Otri); + var f_next = default(Otri); + var sf = default(Osub); + var sfn = default(Osub); Point cc_f, cc_f_next, p; - int n = mesh.triangles.Count; + var n = mesh.triangles.Count; // Call P the polygon (cell) in construction List vpoints = new List(); @@ -303,8 +294,8 @@ private void ConstructCell(Vertex vertex) do { // Call Lffnext the line going through the circumcenters of f and f_next - cc_f = this.points[f.tri.id]; - cc_f_next = this.points[f_next.tri.id]; + cc_f = points[f.tri.id]; + cc_f_next = points[f_next.tri.id]; // if f is tagged non-blind then if (!f.tri.infected) @@ -382,20 +373,20 @@ private void ConstructCell(Vertex vertex) private void ConstructBoundaryCell(Vertex vertex) { - VoronoiRegion region = new VoronoiRegion(vertex); + var region = new VoronoiRegion(vertex); regions.Add(region); - Otri f = default(Otri); - Otri f_init = default(Otri); - Otri f_next = default(Otri); - Otri f_prev = default(Otri); - Osub sf = default(Osub); - Osub sfn = default(Osub); + var f = default(Otri); + var f_init = default(Otri); + var f_next = default(Otri); + var f_prev = default(Otri); + var sf = default(Osub); + var sfn = default(Osub); Vertex torg, tdest, tapex, sorg, sdest; Point cc_f, cc_f_next, p; - int n = mesh.triangles.Count; + var n = mesh.triangles.Count; // Call P the polygon (cell) in construction List vpoints = new List(); @@ -452,7 +443,7 @@ private void ConstructBoundaryCell(Vertex vertex) do { // Call Lffnext the line going through the circumcenters of f and f_next - cc_f = this.points[f.tri.id]; + cc_f = points[f.tri.id]; if (f_next.tri.id == Mesh.DUMMY) { @@ -475,7 +466,7 @@ private void ConstructBoundaryCell(Vertex vertex) break; } - cc_f_next = this.points[f_next.tri.id]; + cc_f_next = points[f_next.tri.id]; // if f is tagged non-blind then if (!f.tri.infected) @@ -515,7 +506,7 @@ private void ConstructBoundaryCell(Vertex vertex) // have to add the intersection with the segment. // Center of f edge dest->apex - Point bisec = new Point((tdest.x + tapex.x) / 2, (tdest.y + tapex.y) / 2); + var bisec = new Point((tdest.x + tapex.x) / 2, (tdest.y + tapex.y) / 2); // Find intersection of seg with line through f's bisector and circumcenter if (SegmentsIntersect(sorg, sdest, bisec, cc_f, out p, false)) @@ -562,7 +553,7 @@ private void ConstructBoundaryCell(Vertex vertex) // have to add the intersection with the segment. // Center of f_next edge org->dest - Point bisec = new Point((torg.x + tdest.x) / 2, (torg.y + tdest.y) / 2); + var bisec = new Point((torg.x + tdest.x) / 2, (torg.y + tdest.y) / 2); // Find intersection of seg with line through f_next's bisector and circumcenter if (SegmentsIntersect(sorg, sdest, bisec, cc_f_next, out p, false)) @@ -659,8 +650,8 @@ private IEnumerable EnumerateEdges() { // Copy edges Point first, last; - var edges = new List(this.Regions.Count * 2); - foreach (var region in this.Regions) + var edges = new List(Regions.Count * 2); + foreach (var region in Regions) { first = null; last = null; diff --git a/src/Triangle/Voronoi/Legacy/IVoronoi.cs b/src/Triangle/Voronoi/Legacy/IVoronoi.cs index d5b681d..5c8e7a7 100644 --- a/src/Triangle/Voronoi/Legacy/IVoronoi.cs +++ b/src/Triangle/Voronoi/Legacy/IVoronoi.cs @@ -7,7 +7,7 @@ namespace TriangleNet.Voronoi.Legacy { using System.Collections.Generic; - using TriangleNet.Geometry; + using Geometry; /// /// Voronoi diagram interface. diff --git a/src/Triangle/Voronoi/Legacy/SimpleVoronoi.cs b/src/Triangle/Voronoi/Legacy/SimpleVoronoi.cs index 68a3d24..d2c09c9 100644 --- a/src/Triangle/Voronoi/Legacy/SimpleVoronoi.cs +++ b/src/Triangle/Voronoi/Legacy/SimpleVoronoi.cs @@ -9,9 +9,9 @@ namespace TriangleNet.Voronoi.Legacy { using System; using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; - using TriangleNet.Tools; + using Topology; + using Geometry; + using Tools; /// /// The Voronoi Diagram is the dual of a pointset triangulation. @@ -19,19 +19,18 @@ namespace TriangleNet.Voronoi.Legacy [Obsolete("Use TriangleNet.Voronoi.StandardVoronoi class instead.")] public class SimpleVoronoi : IVoronoi { - IPredicates predicates = RobustPredicates.Default; + private IPredicates predicates = RobustPredicates.Default; - Mesh mesh; + private Mesh mesh; - Point[] points; - Dictionary regions; + private Dictionary regions; // Stores the endpoints of rays of unbounded Voronoi cells - Dictionary rayPoints; - int rayIndex; + private Dictionary rayPoints; + private int rayIndex; // Bounding box of the triangles circumcenters. - Rectangle bounds; + private Rectangle bounds; /// /// Initializes a new instance of the class. @@ -50,26 +49,17 @@ public SimpleVoronoi(Mesh mesh) /// /// Gets the list of Voronoi vertices. /// - public Point[] Points - { - get { return points; } - } + public Point[] Points { get; private set; } /// /// Gets the list of Voronoi regions. /// - public ICollection Regions - { - get { return regions.Values; } - } + public ICollection Regions => regions.Values; /// /// Enumerates the edges of the Voronoi diagram. /// - public IEnumerable Edges - { - get { return EnumerateEdges(); } - } + public IEnumerable Edges => EnumerateEdges(); /// /// Generate the Voronoi diagram. @@ -86,8 +76,8 @@ private void Generate() mesh.MakeVertexMap(); // Allocate space for voronoi diagram - this.points = new Point[mesh.triangles.Count + mesh.hullsize]; - this.regions = new Dictionary(mesh.vertices.Count); + Points = new Point[mesh.triangles.Count + mesh.hullsize]; + regions = new Dictionary(mesh.vertices.Count); rayPoints = new Dictionary(); rayIndex = 0; @@ -115,7 +105,7 @@ private void Generate() private void ComputeCircumCenters() { - Otri tri = default(Otri); + var tri = default(Otri); double xi = 0, eta = 0; Point pt; @@ -127,12 +117,12 @@ private void ComputeCircumCenters() pt = predicates.FindCircumcenter(tri.Org(), tri.Dest(), tri.Apex(), ref xi, ref eta); pt.id = item.id; - points[item.id] = pt; + Points[item.id] = pt; bounds.Expand(pt); } - double ds = Math.Max(bounds.Width, bounds.Height); + var ds = Math.Max(bounds.Width, bounds.Height); bounds.Resize(ds / 10, ds / 10); } @@ -146,12 +136,12 @@ private void ConstructCell(VoronoiRegion region) var vpoints = new List(); - Otri f = default(Otri); - Otri f_init = default(Otri); - Otri f_next = default(Otri); - Otri f_prev = default(Otri); + var f = default(Otri); + var f_init = default(Otri); + var f_next = default(Otri); + var f_prev = default(Otri); - Osub sub = default(Osub); + var sub = default(Osub); // Call f_init a triangle incident to x vertex.tri.Copy(ref f_init); @@ -177,7 +167,7 @@ private void ConstructCell(VoronoiRegion region) while (f_next.tri.id != Mesh.DUMMY) { // Add circumcenter of current triangle - vpoints.Add(points[f.tri.id]); + vpoints.Add(Points[f.tri.id]); region.AddNeighbor(f.tri.id, regions[f.Apex().id]); @@ -205,7 +195,7 @@ private void ConstructCell(VoronoiRegion region) sid = sub.seg.hash; // Last valid f lies at the boundary. Add the circumcenter. - vpoints.Add(points[f.tri.id]); + vpoints.Add(Points[f.tri.id]); region.AddNeighbor(f.tri.id, regions[f.Apex().id]); // Check if the intersection with the bounding box has already been computed. @@ -213,12 +203,12 @@ private void ConstructCell(VoronoiRegion region) { torg = f.Org(); tapex = f.Apex(); - intersection = IntersectionHelper.BoxRayIntersection(bounds, points[f.tri.id], torg.y - tapex.y, tapex.x - torg.x); + intersection = IntersectionHelper.BoxRayIntersection(bounds, Points[f.tri.id], torg.y - tapex.y, tapex.x - torg.x); // Set the correct id for the vertex intersection.id = n + rayIndex; - points[n + rayIndex] = intersection; + Points[n + rayIndex] = intersection; rayIndex++; rayPoints.Add(sid, intersection); @@ -234,7 +224,7 @@ private void ConstructCell(VoronoiRegion region) while (f_prev.tri.id != Mesh.DUMMY) { - vpoints.Add(points[f_prev.tri.id]); + vpoints.Add(Points[f_prev.tri.id]); region.AddNeighbor(f_prev.tri.id, regions[f_prev.Apex().id]); f_prev.Copy(ref f); @@ -251,14 +241,14 @@ private void ConstructCell(VoronoiRegion region) torg = f.Org(); tdest = f.Dest(); - intersection = IntersectionHelper.BoxRayIntersection(bounds, points[f.tri.id], tdest.y - torg.y, torg.x - tdest.x); + intersection = IntersectionHelper.BoxRayIntersection(bounds, Points[f.tri.id], tdest.y - torg.y, torg.x - tdest.x); // Set the correct id for the vertex intersection.id = n + rayIndex; rayPoints.Add(sid, intersection); - points[n + rayIndex] = intersection; + Points[n + rayIndex] = intersection; rayIndex++; } @@ -276,10 +266,10 @@ private IEnumerable EnumerateEdges() { // Copy edges Point first, last; - var edges = new List(this.Regions.Count * 2); - foreach (var region in this.Regions) + var edges = new List(Regions.Count * 2); + foreach (var region in Regions) { - var ve = region.Vertices.GetEnumerator(); + using var ve = region.Vertices.GetEnumerator(); ve.MoveNext(); diff --git a/src/Triangle/Voronoi/Legacy/VoronoiRegion.cs b/src/Triangle/Voronoi/Legacy/VoronoiRegion.cs index a7e2f2a..9891968 100644 --- a/src/Triangle/Voronoi/Legacy/VoronoiRegion.cs +++ b/src/Triangle/Voronoi/Legacy/VoronoiRegion.cs @@ -8,73 +8,57 @@ namespace TriangleNet.Voronoi.Legacy { using System; using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; + using Topology; + using Geometry; /// /// Represents a region in the Voronoi diagram. /// public class VoronoiRegion { - int id; - Point generator; - List vertices; - bool bounded; + private List vertices; // A map (vertex id) -> (neighbor across adjacent edge) - Dictionary neighbors; + private Dictionary neighbors; /// /// Gets the Voronoi region id (which is the same as the generators vertex id). /// - public int ID - { - get { return id; } - } + public int ID { get; } /// /// Gets the Voronoi regions generator. /// - public Point Generator - { - get { return generator; } - } + public Point Generator { get; } /// /// Gets the Voronoi vertices on the regions boundary. /// - public ICollection Vertices - { - get { return vertices; } - } + public ICollection Vertices => vertices; /// /// Gets or sets whether the Voronoi region is bounded. /// - public bool Bounded - { - get { return bounded; } - set { bounded = value; } - } + public bool Bounded { get; set; } public VoronoiRegion(Vertex generator) { - this.id = generator.id; - this.generator = generator; - this.vertices = new List(); - this.bounded = true; + ID = generator.id; + this.Generator = generator; + vertices = new List(); + Bounded = true; - this.neighbors = new Dictionary(); + neighbors = new Dictionary(); } public void Add(Point point) { - this.vertices.Add(point); + vertices.Add(point); } public void Add(List points) { - this.vertices.AddRange(points); + vertices.AddRange(points); } /// @@ -100,12 +84,12 @@ public VoronoiRegion GetNeighbor(Point p) internal void AddNeighbor(int id, VoronoiRegion neighbor) { - this.neighbors.Add(id, neighbor); + neighbors.Add(id, neighbor); } public override string ToString() { - return String.Format("R-ID {0}", id); + return String.Format("R-ID {0}", ID); } } } diff --git a/src/Triangle/Voronoi/StandardVoronoi.cs b/src/Triangle/Voronoi/StandardVoronoi.cs index 5a88830..df65fe3 100644 --- a/src/Triangle/Voronoi/StandardVoronoi.cs +++ b/src/Triangle/Voronoi/StandardVoronoi.cs @@ -7,9 +7,9 @@ namespace TriangleNet.Voronoi { using System.Collections.Generic; - using TriangleNet.Geometry; - using TriangleNet.Tools; - using TriangleNet.Topology.DCEL; + using Geometry; + using Tools; + using Topology.DCEL; /// /// Computing the standard Voronoi diagram of a Delaunay triangulation. diff --git a/src/Triangle/Voronoi/VoronoiBase.cs b/src/Triangle/Voronoi/VoronoiBase.cs index c015dd3..4e3f631 100644 --- a/src/Triangle/Voronoi/VoronoiBase.cs +++ b/src/Triangle/Voronoi/VoronoiBase.cs @@ -8,11 +8,11 @@ namespace TriangleNet.Voronoi { using System.Collections.Generic; - using TriangleNet.Topology; - using TriangleNet.Geometry; - using TriangleNet.Topology.DCEL; + using Topology; + using Geometry; + using Topology.DCEL; - using Vertex = TriangleNet.Topology.DCEL.Vertex; + using Vertex = Topology.DCEL.Vertex; /// /// The Voronoi diagram is the dual of a point set triangulation. @@ -20,16 +20,16 @@ namespace TriangleNet.Voronoi public abstract class VoronoiBase : DcelMesh { /// Predicates - protected IPredicates predicates; + protected readonly IPredicates predicates; /// Voronoi factory - protected IVoronoiFactory factory; + protected readonly IVoronoiFactory factory; /// /// List of infinite half-edges, i.e. half-edges that start at circumcenters /// of triangles on the domain boundary. /// - protected List rays; + protected List rays = new(); /// /// Initializes a new instance of the class. @@ -39,7 +39,10 @@ public abstract class VoronoiBase : DcelMesh /// Geometric predicates implementation. /// If set to true, the constructor will call the Generate /// method, which builds the Voronoi diagram. - protected VoronoiBase(Mesh mesh, IVoronoiFactory factory, IPredicates predicates, + protected VoronoiBase( + Mesh mesh, + IVoronoiFactory factory, + IPredicates predicates, bool generate) : base(false) { this.factory = factory; @@ -57,28 +60,23 @@ protected VoronoiBase(Mesh mesh, IVoronoiFactory factory, IPredicates predicates /// protected void Generate(Mesh mesh) { - base.edges = new List(); - this.rays = new List(); + edges.Clear(); + rays.Clear(); // Undead vertices cannot be Voronoi cell generators. - int count = mesh.vertices.Count - mesh.undeads; + var count = mesh.vertices.Count - mesh.undeads; // Allocate space for Voronoi diagram. var vertices = new Vertex[mesh.triangles.Count + mesh.hullsize]; var faces = new Face[count]; - if (factory == null) - { - factory = new DefaultVoronoiFactory(); - } - factory.Initialize(vertices.Length, 2 * mesh.NumberOfEdges, faces.Length); // Compute triangles circumcenters. var map = ComputeVertices(mesh, vertices); // Ensure linear numbering of vertices (excluding undeads). - int vid = 0; + var vid = 0; // Create all Voronoi faces, skipping undead vertices. foreach (var vertex in mesh.vertices.Values) @@ -99,8 +97,8 @@ protected void Generate(Mesh mesh) // At this point all edges are computed, but the (edge.next) pointers aren't set. ConnectEdges(map); - base.vertices = new List(vertices); - base.faces = new List(faces); + Vertices = new List(vertices); + Faces = new List(faces); } /// @@ -112,7 +110,7 @@ protected void Generate(Mesh mesh) /// protected List[] ComputeVertices(Mesh mesh, Vertex[] vertices) { - Otri tri = default(Otri); + var tri = default(Otri); double xi = 0, eta = 0; Vertex vertex; Point pt; @@ -148,7 +146,7 @@ protected List[] ComputeVertices(Mesh mesh, Vertex[] vertices) /// Empty vertex map. protected void ComputeEdges(Mesh mesh, Vertex[] vertices, Face[] faces, List[] map) { - Otri tri, neighbor = default(Otri); + Otri tri, neighbor = default; TriangleNet.Geometry.Vertex org, dest; double px, py; @@ -159,10 +157,10 @@ protected void ComputeEdges(Mesh mesh, Vertex[] vertices, Face[] faces, ListMaps all vertices to a list of leaving edges. protected virtual void ConnectEdges(List[] map) { - int length = map.Length; + var length = map.Length; // For each half-edge, find its successor in the connected face. - foreach (var edge in this.edges) + foreach (var edge in edges) { var face = edge.face.generator.id; // The id of the dest vertex of current edge. - int id = edge.twin.origin.id; + var id = edge.twin.origin.id; // The edge origin can also be an infinite vertex. Sort them out // by checking the id.