TrueSync
TSConvexHull.cs
1 /* Copyright (C) <2009-2011> <Thorben Linneweber, Jitter Physics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 * 2. Altered source versions must be plainly marked as such, and must not be
16 * misrepresented as being the original software.
17 * 3. This notice may not be removed or altered from any source distribution.
18 */
19 
20 #region Using Statements
21 using System.Collections.Generic;
22 #endregion
23 
24 namespace TrueSync
25 {
26 
31  public static class TSConvexHull
32  {
33  #region public enum Approximation
34  public enum Approximation
35  {
36  Level1 = 6,
37  Level2 = 7,
38  Level3 = 8,
39  Level4 = 9,
40  Level5 = 10,
41  Level6 = 11,
42  Level7 = 12,
43  Level8 = 13,
44  Level9 = 15,
45  Level10 = 20,
46  Level15 = 25,
47  Level20 = 30
48  }
49  #endregion
50 
51  public static int[] Build(List<TSVector> pointCloud, Approximation factor)
52  {
53  List<int> allIndices = new List<int>();
54 
55  int steps = (int)factor;
56 
57  for (int thetaIndex = 0; thetaIndex < steps; thetaIndex++)
58  {
59  // [0,PI]
60  FP theta = TSMath.Pi / (steps - 1) * thetaIndex;
61  FP sinTheta = FP.Sin(theta);
62  FP cosTheta = FP.Cos(theta);
63 
64  for (int phiIndex = 0; phiIndex < steps; phiIndex++)
65  {
66  // [-PI,PI]
67  FP phi = ((2 * FP.One) * TSMath.Pi) / (steps - 0) * phiIndex - TSMath.Pi;
68  FP sinPhi = FP.Sin(phi);
69  FP cosPhi = FP.Cos(phi);
70 
71  TSVector dir = new TSVector(sinTheta * cosPhi, cosTheta, sinTheta * sinPhi);
72 
73  int index = FindExtremePoint(pointCloud, ref dir);
74  allIndices.Add(index);
75  }
76  }
77 
78  allIndices.Sort();
79 
80  for (int i = 1; i < allIndices.Count; i++)
81  {
82  if (allIndices[i - 1] == allIndices[i])
83  { allIndices.RemoveAt(i - 1); i--; }
84  }
85 
86  return allIndices.ToArray();
87 
88  // or using 3.5 extensions
89  // return allIndices.Distinct().ToArray();
90  }
91 
92  private static int FindExtremePoint(List<TSVector> points,ref TSVector dir)
93  {
94  int index = 0;
95  FP current = FP.MinValue;
96 
97  TSVector point; FP value;
98 
99  for (int i = 1; i < points.Count; i++)
100  {
101  point = points[i];
102 
103  value = TSVector.Dot(ref point, ref dir);
104  if (value > current) { current = value; index= i; }
105  }
106 
107  return index;
108  }
109  }
110 }