PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/MIConvexHull/ConvexHull/Algorithm/Result.cs

#
C# | 149 lines | 89 code | 17 blank | 43 comment | 13 complexity | 004cc3978a09c2c323a49f1db3f9df7e MD5 | raw file
  1. /******************************************************************************
  2. *
  3. * The MIT License (MIT)
  4. *
  5. * MIConvexHull, Copyright (c) 2015 David Sehnal, Matthew Campbell
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. *****************************************************************************/
  26. namespace MIConvexHull
  27. {
  28. using System.Collections.Generic;
  29. /*
  30. * Code here transforms the result to its final form
  31. */
  32. internal partial class ConvexHullInternal
  33. {
  34. /// <summary>
  35. /// This is called by the "ConvexHull" class.
  36. /// </summary>
  37. /// <typeparam name="TVertex"></typeparam>
  38. /// <typeparam name="TFace"></typeparam>
  39. /// <param name="data"></param>
  40. /// <param name="config">If null, default ConvexHullComputationConfig.GetDefault() is used.</param>
  41. /// <returns></returns>
  42. internal static ConvexHull<TVertex, TFace> GetConvexHull<TVertex, TFace>(IList<TVertex> data, ConvexHullComputationConfig config)
  43. where TFace : ConvexFace<TVertex, TFace>, new()
  44. where TVertex : IVertex
  45. {
  46. config = config ?? new ConvexHullComputationConfig();
  47. var vertices = new IVertex[data.Count];
  48. for (int i = 0; i < data.Count; i++) vertices[i] = data[i];
  49. ConvexHullInternal ch = new ConvexHullInternal(vertices, false, config);
  50. ch.FindConvexHull();
  51. var hull = ch.GetHullVertices(data);
  52. return new ConvexHull<TVertex, TFace> { Points = hull, Faces = ch.GetConvexFaces<TVertex, TFace>() };
  53. }
  54. TVertex[] GetHullVertices<TVertex>(IList<TVertex> data)
  55. {
  56. int cellCount = ConvexFaces.Count;
  57. int hullVertexCount = 0;
  58. int vertexCount = Vertices.Length;
  59. for (int i = 0; i < vertexCount; i++) VertexMarks[i] = false;
  60. for (int i = 0; i < cellCount; i++)
  61. {
  62. var vs = FacePool[ConvexFaces[i]].Vertices;
  63. for (int j = 0; j < vs.Length; j++)
  64. {
  65. var v = vs[j];
  66. if (!VertexMarks[v])
  67. {
  68. VertexMarks[v] = true;
  69. hullVertexCount++;
  70. }
  71. }
  72. }
  73. var result = new TVertex[hullVertexCount];
  74. for (int i = 0; i < vertexCount; i++)
  75. {
  76. if (VertexMarks[i]) result[--hullVertexCount] = data[i];
  77. }
  78. return result;
  79. }
  80. /// <summary>
  81. /// Finds the convex hull and creates the TFace objects.
  82. /// </summary>
  83. /// <typeparam name="TVertex"></typeparam>
  84. /// <typeparam name="TFace"></typeparam>
  85. /// <returns></returns>
  86. TFace[] GetConvexFaces<TVertex, TFace>()
  87. where TFace : ConvexFace<TVertex, TFace>, new()
  88. where TVertex : IVertex
  89. {
  90. var faces = ConvexFaces;
  91. int cellCount = faces.Count;
  92. var cells = new TFace[cellCount];
  93. for (int i = 0; i < cellCount; i++)
  94. {
  95. var face = FacePool[faces[i]];
  96. var vertices = new TVertex[Dimension];
  97. for (int j = 0; j < Dimension; j++)
  98. {
  99. vertices[j] = (TVertex)this.Vertices[face.Vertices[j]];
  100. }
  101. cells[i] = new TFace
  102. {
  103. Vertices = vertices,
  104. Adjacency = new TFace[Dimension],
  105. Normal = IsLifted ? null : face.Normal
  106. };
  107. face.Tag = i;
  108. }
  109. for (int i = 0; i < cellCount; i++)
  110. {
  111. var face = FacePool[faces[i]];
  112. var cell = cells[i];
  113. for (int j = 0; j < Dimension; j++)
  114. {
  115. if (face.AdjacentFaces[j] < 0) continue;
  116. cell.Adjacency[j] = cells[FacePool[face.AdjacentFaces[j]].Tag];
  117. }
  118. // Fix the vertex orientation.
  119. if (face.IsNormalFlipped)
  120. {
  121. var tempVert = cell.Vertices[0];
  122. cell.Vertices[0] = cell.Vertices[Dimension - 1];
  123. cell.Vertices[Dimension - 1] = tempVert;
  124. var tempAdj = cell.Adjacency[0];
  125. cell.Adjacency[0] = cell.Adjacency[Dimension - 1];
  126. cell.Adjacency[Dimension - 1] = tempAdj;
  127. }
  128. }
  129. return cells;
  130. }
  131. }
  132. }