1 | namespace MIConvexHull
|
---|
2 | {
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 |
|
---|
6 | /// <summary>
|
---|
7 | /// Factory class for computing convex hulls.
|
---|
8 | /// </summary>
|
---|
9 | public static class ConvexHull
|
---|
10 | {
|
---|
11 | /// <summary>
|
---|
12 | /// Creates a convex hull of the input data.
|
---|
13 | /// </summary>
|
---|
14 | /// <typeparam name="TVertex"></typeparam>
|
---|
15 | /// <typeparam name="TFace"></typeparam>
|
---|
16 | /// <param name="data"></param>
|
---|
17 | /// <returns></returns>
|
---|
18 | public static ConvexHull<TVertex, TFace> Create<TVertex, TFace>(IEnumerable<TVertex> data)
|
---|
19 | where TVertex : IVertex
|
---|
20 | where TFace : ConvexFace<TVertex, TFace>, new()
|
---|
21 | {
|
---|
22 | return ConvexHull<TVertex, TFace>.Create(data);
|
---|
23 | }
|
---|
24 |
|
---|
25 | /// <summary>
|
---|
26 | /// Creates a convex hull of the input data.
|
---|
27 | /// </summary>
|
---|
28 | /// <typeparam name="TVertex"></typeparam>
|
---|
29 | /// <param name="data"></param>
|
---|
30 | /// <returns></returns>
|
---|
31 | public static ConvexHull<TVertex, DefaultConvexFace<TVertex>> Create<TVertex>(IEnumerable<TVertex> data)
|
---|
32 | where TVertex : IVertex
|
---|
33 | {
|
---|
34 | return ConvexHull<TVertex, DefaultConvexFace<TVertex>>.Create(data);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Creates a convex hull of the input data.
|
---|
39 | /// </summary>
|
---|
40 | /// <param name="data"></param>
|
---|
41 | /// <returns></returns>
|
---|
42 | public static ConvexHull<DefaultVertex, DefaultConvexFace<DefaultVertex>> Create(IEnumerable<double[]> data)
|
---|
43 | {
|
---|
44 | var points = data.Select(p => new DefaultVertex { Position = p.ToArray() });
|
---|
45 | return ConvexHull<DefaultVertex, DefaultConvexFace<DefaultVertex>>.Create(points);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Representation of a convex hull.
|
---|
51 | /// </summary>
|
---|
52 | /// <typeparam name="TVertex"></typeparam>
|
---|
53 | /// <typeparam name="TFace"></typeparam>
|
---|
54 | public class ConvexHull<TVertex, TFace>
|
---|
55 | where TVertex : IVertex
|
---|
56 | where TFace : ConvexFace<TVertex, TFace>, new()
|
---|
57 | {
|
---|
58 | /// <summary>
|
---|
59 | /// Points of the convex hull.
|
---|
60 | /// </summary>
|
---|
61 | public IEnumerable<TVertex> Points { get; private set; }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Faces of the convex hull.
|
---|
65 | /// </summary>
|
---|
66 | public IEnumerable<TFace> Faces { get; private set; }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Creates the convex hull.
|
---|
70 | /// </summary>
|
---|
71 | /// <param name="data"></param>
|
---|
72 | /// <returns></returns>
|
---|
73 | public static ConvexHull<TVertex, TFace> Create(IEnumerable<TVertex> data)
|
---|
74 | {
|
---|
75 | if (!(data is IList<TVertex>)) data = data.ToArray();
|
---|
76 | var ch = ConvexHullInternal.GetConvexHullAndFaces<TVertex, TFace>(data.Cast<IVertex>());
|
---|
77 | return new ConvexHull<TVertex, TFace> { Points = ch.Item1, Faces = ch.Item2 };
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Can only be created using a factory method.
|
---|
82 | /// </summary>
|
---|
83 | private ConvexHull()
|
---|
84 | {
|
---|
85 |
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|