From 37ab83ae1f70eacf09873b72ed76aee05ee05893 Mon Sep 17 00:00:00 2001 From: wo80 Date: Fri, 18 Feb 2022 16:30:58 +0100 Subject: [PATCH] Make mesh.TransferNodes() private and instead pass input points to Mesh ctor. --- src/Triangle/Mesh.cs | 77 ++++++------------- src/Triangle/Meshing/Algorithm/Dwyer.cs | 13 ++-- src/Triangle/Meshing/Algorithm/Incremental.cs | 7 +- src/Triangle/Meshing/Algorithm/SweepLine.cs | 5 +- src/Triangle/Meshing/Converter.cs | 12 +-- 5 files changed, 38 insertions(+), 76 deletions(-) diff --git a/src/Triangle/Mesh.cs b/src/Triangle/Mesh.cs index 98147f6..3e7a270 100644 --- a/src/Triangle/Mesh.cs +++ b/src/Triangle/Mesh.cs @@ -80,85 +80,52 @@ namespace TriangleNet /// /// Gets the mesh bounding box. /// - public Rectangle Bounds - { - get { return this.bounds; } - } + public Rectangle Bounds => bounds; /// /// Gets the mesh vertices. /// - public ICollection Vertices - { - get { return this.vertices.Values; } - } + public ICollection Vertices => vertices.Values; /// /// Gets the mesh holes. /// - public IList Holes - { - get { return this.holes; } - } + public IList Holes => holes; /// /// Gets the mesh triangles. /// - public ICollection Triangles - { - get { return this.triangles; } - } + public ICollection Triangles => triangles; /// /// Gets the mesh segments. /// - public ICollection Segments - { - get { return this.subsegs.Values; } - } + public ICollection Segments => subsegs.Values; /// /// Gets the mesh edges. /// - public IEnumerable Edges - { - get - { - return new EdgeIterator().EnumerateEdges(this); - } - } + public IEnumerable Edges => new EdgeIterator().EnumerateEdges(this); /// /// Gets the number of input vertices. /// - public int NumberOfInputPoints - { - get { return invertices; } - } + public int NumberOfInputPoints => invertices; /// /// Gets the number of mesh edges. /// - public int NumberOfEdges - { - get { return (3 * triangles.Count + hullsize) / 2; } - } + public int NumberOfEdges => (3 * triangles.Count + hullsize) / 2; /// /// Indicates whether the input is a PSLG or a point set. /// - public bool IsPolygon - { - get { return this.insegments > 0; } - } + public bool IsPolygon => insegments > 0; /// /// Gets the current node numbering. /// - public NodeNumbering CurrentNumbering - { - get { return numbering; } - } + public NodeNumbering CurrentNumbering => numbering; #endregion @@ -227,13 +194,13 @@ namespace TriangleNet /// /// Initializes a new instance of the class. /// - public Mesh(Configuration config) + public Mesh(Configuration config, IList points) { Initialize(); behavior = new Behavior(); - vertices = new Dictionary(); + vertices = new Dictionary(points.Count); subsegs = new Dictionary(); triangles = config.TrianglePool(); @@ -245,9 +212,11 @@ namespace TriangleNet steinerleft = -1; - this.predicates = config.Predicates(); + predicates = config.Predicates(); - this.locator = new TriangleLocator(this, predicates); + locator = new TriangleLocator(this, predicates); + + TransferNodes(points); } public void Refine(QualityOptions quality, bool delaunay = false) @@ -401,13 +370,13 @@ namespace TriangleNet /// Read the vertices from memory. /// /// The input data. - internal void TransferNodes(IList points) + private void TransferNodes(IList points) { - this.invertices = points.Count; - this.mesh_dim = 2; - this.bounds = new Rectangle(); + invertices = points.Count; + mesh_dim = 2; + bounds = new Rectangle(); - if (this.invertices < 3) + if (invertices < 3) { logger.Error("Input must have at least three input vertices.", "Mesh.TransferNodes()"); throw new Exception("Input must have at least three input vertices."); @@ -439,8 +408,8 @@ namespace TriangleNet p.hash = p.id = hash_vtx++; } - this.vertices.Add(p.hash, p); - this.bounds.Expand(p); + vertices.Add(p.hash, p); + bounds.Expand(p); } } diff --git a/src/Triangle/Meshing/Algorithm/Dwyer.cs b/src/Triangle/Meshing/Algorithm/Dwyer.cs index d24653d..d25c22b 100644 --- a/src/Triangle/Meshing/Algorithm/Dwyer.cs +++ b/src/Triangle/Meshing/Algorithm/Dwyer.cs @@ -66,16 +66,15 @@ namespace TriangleNet.Meshing.Algorithm /// public IMesh Triangulate(IList points, Configuration config) { - this.predicates = config.Predicates(); + predicates = config.Predicates(); - this.mesh = new Mesh(config); - this.mesh.TransferNodes(points); + mesh = new Mesh(config, points); Otri hullleft = default(Otri), hullright = default(Otri); int i, j, n = points.Count; // Allocate an array of pointers to vertices for sorting. - this.sortarray = new Vertex[n]; + sortarray = new Vertex[n]; i = 0; foreach (var v in points) { @@ -94,7 +93,7 @@ namespace TriangleNet.Meshing.Algorithm if (Log.Verbose) { Log.Instance.Warning( - String.Format("A duplicate vertex appeared and was ignored (ID {0}).", sortarray[j].id), + string.Format("A duplicate vertex appeared and was ignored (ID {0}).", sortarray[j].id), "Dwyer.Triangulate()"); } sortarray[j].type = VertexType.UndeadVertex; @@ -116,9 +115,9 @@ namespace TriangleNet.Meshing.Algorithm // Form the Delaunay triangulation. DivconqRecurse(0, i - 1, 0, ref hullleft, ref hullright); - this.mesh.hullsize = RemoveGhosts(ref hullleft); + mesh.hullsize = RemoveGhosts(ref hullleft); - return this.mesh; + return mesh; } /// diff --git a/src/Triangle/Meshing/Algorithm/Incremental.cs b/src/Triangle/Meshing/Algorithm/Incremental.cs index 709b151..9ce173f 100644 --- a/src/Triangle/Meshing/Algorithm/Incremental.cs +++ b/src/Triangle/Meshing/Algorithm/Incremental.cs @@ -25,8 +25,7 @@ namespace TriangleNet.Meshing.Algorithm /// triangulation. public IMesh Triangulate(IList points, Configuration config) { - this.mesh = new Mesh(config); - this.mesh.TransferNodes(points); + mesh = new Mesh(config, points); Otri starttri = new Otri(); @@ -50,9 +49,9 @@ namespace TriangleNet.Meshing.Algorithm } // Remove the bounding box. - this.mesh.hullsize = RemoveBox(); + mesh.hullsize = RemoveBox(); - return this.mesh; + return mesh; } /// diff --git a/src/Triangle/Meshing/Algorithm/SweepLine.cs b/src/Triangle/Meshing/Algorithm/SweepLine.cs index 2e93749..f44e081 100644 --- a/src/Triangle/Meshing/Algorithm/SweepLine.cs +++ b/src/Triangle/Meshing/Algorithm/SweepLine.cs @@ -35,10 +35,9 @@ namespace TriangleNet.Meshing.Algorithm public IMesh Triangulate(IList points, Configuration config) { - this.predicates = config.Predicates(); + predicates = config.Predicates(); - this.mesh = new Mesh(config); - this.mesh.TransferNodes(points); + mesh = new Mesh(config, points); // Nonexistent x value used as a flag to mark circle events in sweepline // Delaunay algorithm. diff --git a/src/Triangle/Meshing/Converter.cs b/src/Triangle/Meshing/Converter.cs index 51c4b86..edd32fc 100644 --- a/src/Triangle/Meshing/Converter.cs +++ b/src/Triangle/Meshing/Converter.cs @@ -35,19 +35,15 @@ namespace TriangleNet.Meshing /// /// Reconstruct a triangulation from its raw data representation. /// - public static Mesh ToMesh(Polygon polygon, ITriangle[] triangles) + public static Mesh ToMesh(Polygon polygon, ITriangle[] triangles, Configuration config = null) { Otri tri = default(Otri); Osub subseg = default(Osub); - int i = 0; int elements = triangles == null ? 0 : triangles.Length; int segments = polygon.Segments.Count; - // TODO: Configuration should be a function argument. - var mesh = new Mesh(new Configuration()); - - mesh.TransferNodes(polygon.Points); + var mesh = new Mesh(config ?? new Configuration(), polygon.Points); mesh.regions.AddRange(polygon.Regions); mesh.behavior.useRegions = polygon.Regions.Count > 0; @@ -59,7 +55,7 @@ namespace TriangleNet.Meshing } // Create the triangles. - for (i = 0; i < elements; i++) + for (int i = 0; i < elements; i++) { mesh.MakeTriangle(ref tri); } @@ -69,7 +65,7 @@ namespace TriangleNet.Meshing mesh.insegments = segments; // Create the subsegments. - for (i = 0; i < segments; i++) + for (int i = 0; i < segments; i++) { mesh.MakeSegment(ref subseg); }