#region License /* MIT License Copyright(c) 2017-2018 Mattias Edlund 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. */ #endregion using System; using MeshDecimator.Algorithms; namespace MeshDecimator { #region Algorithm /// /// The decimation algorithms. /// public enum Algorithm { /// /// The default algorithm. /// Default, /// /// The fast quadric mesh simplification algorithm. /// FastQuadricMesh } #endregion /// /// The mesh decimation API. /// public static class MeshDecimation { #region Public Methods #region Create Algorithm /// /// Creates a specific decimation algorithm. /// /// The desired algorithm. /// The decimation algorithm. public static DecimationAlgorithm CreateAlgorithm(Algorithm algorithm) { DecimationAlgorithm alg = null; switch (algorithm) { case Algorithm.Default: case Algorithm.FastQuadricMesh: alg = new FastQuadricMeshSimplification(); break; default: throw new ArgumentException("The specified algorithm is not supported.", "algorithm"); } return alg; } #endregion #region Decimate Mesh /// /// Decimates a mesh. /// /// The mesh to decimate. /// The target triangle count. /// The decimated mesh. public static Mesh DecimateMesh(Mesh mesh, int targetTriangleCount) { return DecimateMesh(Algorithm.Default, mesh, targetTriangleCount); } /// /// Decimates a mesh. /// /// The desired algorithm. /// The mesh to decimate. /// The target triangle count. /// The decimated mesh. public static Mesh DecimateMesh(Algorithm algorithm, Mesh mesh, int targetTriangleCount) { if (mesh == null) throw new ArgumentNullException("mesh"); var decimationAlgorithm = CreateAlgorithm(algorithm); return DecimateMesh(decimationAlgorithm, mesh, targetTriangleCount); } /// /// Decimates a mesh. /// /// The decimation algorithm. /// The mesh to decimate. /// The target triangle count. /// The decimated mesh. public static Mesh DecimateMesh(DecimationAlgorithm algorithm, Mesh mesh, int targetTriangleCount) { if (algorithm == null) throw new ArgumentNullException("algorithm"); else if (mesh == null) throw new ArgumentNullException("mesh"); int currentTriangleCount = mesh.TriangleCount; if (targetTriangleCount > currentTriangleCount) targetTriangleCount = currentTriangleCount; else if (targetTriangleCount < 0) targetTriangleCount = 0; algorithm.Initialize(mesh); algorithm.DecimateMesh(targetTriangleCount); return algorithm.ToMesh(); } #endregion #region Decimate Mesh Lossless /// /// Decimates a mesh without losing any quality. /// /// The mesh to decimate. /// The decimated mesh. public static Mesh DecimateMeshLossless(Mesh mesh) { return DecimateMeshLossless(Algorithm.Default, mesh); } /// /// Decimates a mesh without losing any quality. /// /// The desired algorithm. /// The mesh to decimate. /// The decimated mesh. public static Mesh DecimateMeshLossless(Algorithm algorithm, Mesh mesh) { if (mesh == null) throw new ArgumentNullException("mesh"); var decimationAlgorithm = CreateAlgorithm(algorithm); return DecimateMeshLossless(decimationAlgorithm, mesh); } /// /// Decimates a mesh without losing any quality. /// /// The decimation algorithm. /// The mesh to decimate. /// The decimated mesh. public static Mesh DecimateMeshLossless(DecimationAlgorithm algorithm, Mesh mesh) { if (algorithm == null) throw new ArgumentNullException("algorithm"); else if (mesh == null) throw new ArgumentNullException("mesh"); int currentTriangleCount = mesh.TriangleCount; algorithm.Initialize(mesh); algorithm.DecimateMeshLossless(); return algorithm.ToMesh(); } #endregion #endregion } }