/****************************************************************************** * * The MIT License (MIT) * * LiquidVolumeFX.MIConvexHull, Copyright (c) 2015 David Sehnal, Matthew Campbell * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * *****************************************************************************/ using System; using System.Collections.Generic; using System.Linq; namespace LiquidVolumeFX.MIConvexHull { /// /// A factory class for creating a Voronoi mesh. /// public static class VoronoiMesh { /// /// Create the voronoi mesh. /// /// The type of the t vertex. /// The type of the t cell. /// The type of the t edge. /// The data. /// VoronoiMesh<TVertex, TCell, TEdge>. public static VoronoiMesh Create(IList data, double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance) where TCell : TriangulationCell, new() where TVertex : IVertex where TEdge : VoronoiEdge, new() { return VoronoiMesh.Create(data, PlaneDistanceTolerance); } /// /// Create the voronoi mesh. /// /// The type of the t vertex. /// The data. /// VoronoiMesh<TVertex, DefaultTriangulationCell<TVertex>, VoronoiEdge<TVertex, DefaultTriangulationCell<TVertex>>>. public static VoronoiMesh , VoronoiEdge>> Create(IList data, double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance) where TVertex : IVertex { return VoronoiMesh , VoronoiEdge> >.Create(data, PlaneDistanceTolerance); } /// /// Create the voronoi mesh. /// /// The data. /// VoronoiMesh<DefaultVertex, DefaultTriangulationCell<DefaultVertex>, VoronoiEdge<DefaultVertex, DefaultTriangulationCell<DefaultVertex>>>. public static VoronoiMesh , VoronoiEdge>> Create(IList data, double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance) { var points = data.Select(p => new DefaultVertex { Position = p.ToArray() }).ToList(); return VoronoiMesh , VoronoiEdge>>.Create(points, PlaneDistanceTolerance); } /// /// Create the voronoi mesh. /// /// The type of the t vertex. /// The type of the t cell. /// The data. /// VoronoiMesh<TVertex, TCell, VoronoiEdge<TVertex, TCell>>. public static VoronoiMesh> Create( IList data, double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance) where TVertex : IVertex where TCell : TriangulationCell, new() { return VoronoiMesh>.Create(data, PlaneDistanceTolerance); } } /// /// A representation of a voronoi mesh. /// /// The type of the t edge. /// The type of the t vertex. /// The type of the t cell. public class VoronoiMesh where TCell : TriangulationCell, new() where TVertex : IVertex where TEdge : VoronoiEdge, new() { /// /// Can only be created using a factory method. /// private VoronoiMesh() { } /// /// Vertices of the diagram. /// /// The vertices. public IEnumerable Vertices { get; private set; } /// /// Edges connecting the cells. /// The same information can be retrieved Cells' Adjacency. /// /// The edges. public IEnumerable Edges { get; private set; } /// /// Create a Voronoi diagram of the input data. /// /// The data. /// The plane distance tolerance (default is 1e-10). If too high, points /// will be missed. If too low, the algorithm may break. Only adjust if you notice problems. /// VoronoiMesh<TVertex, TCell, TEdge>. /// data /// data public static VoronoiMesh Create(IList data, double PlaneDistanceTolerance = Constants.DefaultPlaneDistanceTolerance) { if (data == null) throw new ArgumentNullException("data"); var t = DelaunayTriangulation.Create(data, PlaneDistanceTolerance); var vertices = t.Cells.ToList(); var edges = new HashSet(new EdgeComparer()); foreach (var f in vertices) { for (var i = 0; i < f.Adjacency.Length; i++) { var af = f.Adjacency[i]; if (af != null) edges.Add(new TEdge { Source = f, Target = af }); } } return new VoronoiMesh { Vertices = vertices, Edges = edges.ToList() }; } /// /// This is probably not needed, but might make things a tiny bit faster. /// /// private class EdgeComparer : IEqualityComparer { /// /// Equals the specified x. /// /// The x. /// The y. /// System.Boolean. public bool Equals(TEdge x, TEdge y) { return (x.Source == y.Source && x.Target == y.Target) || (x.Source == y.Target && x.Target == y.Source); } /// /// Returns a hash code for this instance. /// /// The for which a hash code is to be returned. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public int GetHashCode(TEdge obj) { return obj.Source.GetHashCode() ^ obj.Target.GetHashCode(); } } } }