Instrument Neutral Distributed Interface INDI  2.0.2
ConvexHull.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 // This C++ code is based on the c code described below
12 // it was ported to c++ by Roger James in December 2013
13 // !!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!
14 // This must code must use integer coordinates. A naive conversion
15 // to floating point WILL NOT work. For the reasons behind this
16 // have a look at at section 4.3.5 of the O'Rourke book. For more
17 // information try http://www.mpi-inf.mpg.de/departments/d1/ClassroomExamples/
18 // For INDI alignment purposes we need to scale floating point coordinates
19 // into the integer space before using this algorithm.
20 
21 /*
22 This code is described in "Computational Geometry in C" (Second Edition),
23 Chapter 4. It is not written to be comprehensible without the
24 explanation in that book.
25 
26 Input: 3n integer coordinates for the points.
27 Output: the 3D convex hull, in postscript with embedded comments
28  showing the vertices and faces.
29 
30 Compile: gcc -o chull chull.c (or simply: make)
31 
32 Written by Joseph O'Rourke, with contributions by
33  Kristy Anderson, John Kutcher, Catherine Schevon, Susan Weller.
34 Last modified: May 2000
35 Questions to orourke@cs.smith.edu.
36 
37 --------------------------------------------------------------------
38 This code is Copyright 2000 by Joseph O'Rourke. It may be freely
39 redistributed in its entirety provided that this copyright notice is
40 not removed.
41 --------------------------------------------------------------------
42 */
43 
44 #include <gsl/gsl_matrix.h>
45 
46 #include <cmath>
47 #include <cstring>
48 #include <fstream>
49 #include <limits>
50 
51 namespace INDI
52 {
53 namespace AlignmentSubsystem
54 {
60 {
61  public:
62  ConvexHull();
63  virtual ~ConvexHull() {}
64 
65  enum
66  {
67  X = 0,
68  Y = 1,
69  Z = 2
70  };
71 
72  template <class Type>
73  static void add(Type &head, Type p)
74  {
75  if (NULL != head)
76  {
77  p->next = head;
78  p->prev = head->prev;
79  head->prev = p;
80  p->prev->next = p;
81  }
82  else
83  {
84  head = p;
85  head->next = head->prev = p;
86  }
87  };
88 
89  template <class Type>
90  static void remove(Type &head, Type p)
91  {
92  if (NULL != head)
93  {
94  if (head == head->next)
95  head = NULL;
96  else if (p == head)
97  head = head->next;
98  p->next->prev = p->prev;
99  p->prev->next = p->next;
100  delete p;
101  }
102  };
103 
104  template <class Type>
105  static void swap(Type &t, Type &x, Type &y)
106  {
107  t = x;
108  x = y;
109  y = t;
110  };
111 
112  // Explicit forward declarations
113  struct tVertexStructure;
114  struct tFaceStructure;
115  struct tEdgeStructure;
116 
117  /* Define structures for vertices, edges and faces */
118  typedef struct tVertexStructure tsVertex;
119  typedef tsVertex *tVertex;
120 
121  typedef struct tEdgeStructure tsEdge;
122  typedef tsEdge *tEdge;
123 
124  typedef struct tFaceStructure tsFace;
125  typedef tsFace *tFace;
126 
128  {
129  int v[3];
130  int vnum;
131  tEdge duplicate; // pointer to incident cone edge (or NULL)
132  bool onhull; // True iff point on hull.
133  bool mark; // True iff point already processed.
135  };
136 
138  {
141  tFace newface; // pointer to incident cone face.
142  bool delete_it; // True iff edge should be delete.
144  };
145 
147  {
149  {
150  pMatrix = gsl_matrix_alloc(3, 3);
151  }
153  {
154  gsl_matrix_free(pMatrix);
155  }
158  bool visible; // True iff face visible from new point.
160  gsl_matrix *pMatrix;
161  };
162 
163  /* Define flags */
164  static const bool ONHULL = true;
165  static const bool REMOVED = true;
166  static const bool VISIBLE = true;
167  static const bool PROCESSED = true;
168  static const int SAFE = 1000000; /* Range of safe coord values. */
169 
173 
180  bool AddOne(tVertex p);
181 
186  void CheckEndpts();
187 
192  void CheckEuler(int V, int E, int F);
193 
197  void Checks();
198 
203  void CleanEdges();
204 
207  void CleanFaces();
208 
213  void CleanUp(tVertex *pvnext);
214 
221  void CleanVertices(tVertex *pvnext);
222 
226  bool Collinear(tVertex a, tVertex b, tVertex c);
227 
232  void Consistency();
233 
237  void ConstructHull();
238 
243  void Convexity();
244 
253  bool DoubleTriangle();
254 
259  void EdgeOrderOnFaces();
260 
263  int GetScaleFactor() const
264  {
265  return ScaleFactor;
266  }
267 
277  void MakeCcw(tFace f, tEdge e, tVertex p);
278 
284 
288  tFace MakeFace(tVertex v0, tVertex v1, tVertex v2, tFace f);
289 
292  void MakeNewVertex(double x, double y, double z, int VertexId);
293 
298 
304 
308 
315  void Print();
316 
319  void PrintEdges(std::ofstream &Ofile);
320 
323  void PrintFaces(std::ofstream &Ofile);
324 
329  void PrintObj(const char *FileName = "chull.obj");
330 
333  void PrintOut(const char *FileName, tVertex v);
334 
337  void PrintPoint(tVertex p);
338 
341  void PrintVertices(std::ofstream &Ofile);
342 
348  void ReadVertices();
349 
352  void Reset();
353 
359  void SetScaleFactor(const int NewScaleFactor)
360  {
361  ScaleFactor = NewScaleFactor;
362  }
363 
366  void SubVec(int a[3], int b[3], int c[3]);
367 
371  int Volumei(tFace f, tVertex p);
372 
379  int VolumeSign(tFace f, tVertex p);
380 
381  private:
382  bool debug;
383  bool check;
384 
385  int ScaleFactor; // Scale factor to be used when converting from floating point to integers and vice versa
386 };
387 
388 } // namespace AlignmentSubsystem
389 } // namespace INDI
This class computes the convex hull of a set of 3d points.
Definition: ConvexHull.h:60
void Checks()
Checks the consistency of the hull and prints the results to the standard error output.
Definition: ConvexHull.cpp:160
void ReadVertices()
ReadVertices: Reads in the vertices, and links them into a circular list with MakeNullVertex....
Definition: ConvexHull.cpp:966
void CleanUp(tVertex *pvnext)
CleanUp goes through each data structure list and clears all flags and NULLs out some pointers....
Definition: ConvexHull.cpp:264
tFace MakeNullFace()
MakeNullFace creates a new face structure and initializes all of its flags to NULL and sets all the f...
Definition: ConvexHull.cpp:651
void EdgeOrderOnFaces()
EdgeOrderOnFaces: puts e0 between v0 and v1, e1 between v1 and v2, e2 between v2 and v0 on each face....
Definition: ConvexHull.cpp:473
void Reset()
Frees the vertices edges and faces lists and resets the debug and check flags.
Definition: ConvexHull.cpp:988
void PrintPoint(tVertex p)
Prints a single vertex to the standard output.
Definition: ConvexHull.cpp:938
void Convexity()
Convexity checks that the volume between every face and every point is negative. This shows that each...
Definition: ConvexHull.cpp:387
void Consistency()
Consistency runs through the edge list and checks that all adjacent faces have their endpoints in opp...
Definition: ConvexHull.cpp:329
void SetScaleFactor(const int NewScaleFactor)
Set the floating point to integer scaling factor. If you want to tweak this a good value to start fro...
Definition: ConvexHull.h:359
tFace MakeFace(tVertex v0, tVertex v1, tVertex v2, tFace f)
MakeFace creates a new face structure from three vertices (in ccw order). It returns a pointer to the...
Definition: ConvexHull.cpp:583
bool AddOne(tVertex p)
AddOne is passed a vertex. It first determines all faces visible from that point. If none are visible...
Definition: ConvexHull.cpp:49
void MakeNewVertex(double x, double y, double z, int VertexId)
Makes a vertex from the supplied information and adds it to the vertices list.
Definition: ConvexHull.cpp:623
static void swap(Type &t, Type &x, Type &y)
Definition: ConvexHull.h:105
struct tVertexStructure tsVertex
Definition: ConvexHull.h:118
void CheckEndpts()
Checks that, for each face, for each i={0,1,2}, the [i]th vertex of that face is either the [0]th or ...
Definition: ConvexHull.cpp:103
int GetScaleFactor() const
Set the floating point to integer scaling factor.
Definition: ConvexHull.h:263
void PrintVertices(std::ofstream &Ofile)
Prints vertices to Ofile.
Definition: ConvexHull.cpp:946
void CheckEuler(int V, int E, int F)
CheckEuler checks Euler's relation, as well as its implications when all faces are known to be triang...
Definition: ConvexHull.cpp:139
static void remove(Type &head, Type p)
Definition: ConvexHull.h:90
void CleanVertices(tVertex *pvnext)
CleanVertices runs through the vertex list and deletes the vertices that are marked as processed but ...
Definition: ConvexHull.cpp:271
bool Collinear(tVertex a, tVertex b, tVertex c)
Collinear checks to see if the three points given are collinear, by checking to see if each element o...
Definition: ConvexHull.cpp:322
void PrintOut(const char *FileName, tVertex v)
Prints vertices, edges and faces to the standard error output.
Definition: ConvexHull.cpp:924
int Volumei(tFace f, tVertex p)
Volumei returns the volume of the tetrahedron determined by f and p.
bool DoubleTriangle()
DoubleTriangle builds the initial double triangle. It first finds 3 noncollinear points and makes two...
Definition: ConvexHull.cpp:421
void MakeCcw(tFace f, tEdge e, tVertex p)
MakeCcw puts the vertices in the face structure in counterclock wise order. We want to store the vert...
Definition: ConvexHull.cpp:511
void CleanEdges()
CleanEdges runs through the edge list and cleans up the structure. If there is a newface then it will...
Definition: ConvexHull.cpp:196
void ConstructHull()
ConstructHull adds the vertices to the hull one at a time. The hull vertices are those in the list ma...
Definition: ConvexHull.cpp:360
int VolumeSign(tFace f, tVertex p)
VolumeSign returns the sign of the volume of the tetrahedron determined by f and p....
tVertex MakeNullVertex()
MakeNullVertex: Makes a vertex, nulls out fields.
Definition: ConvexHull.cpp:666
void PrintEdges(std::ofstream &Ofile)
Prints the edges Ofile.
Definition: ConvexHull.cpp:801
void PrintObj(const char *FileName="chull.obj")
Outputs the faces in Lightwave obj format for 3d viewing. The files chull.obj and chull....
Definition: ConvexHull.cpp:847
void Print()
Print: Prints out the vertices and the faces. Uses the vnum indices corresponding to the order in whi...
Definition: ConvexHull.cpp:679
void SubVec(int a[3], int b[3], int c[3])
SubVec: Computes a - b and puts it into c.
tFace MakeConeFace(tEdge e, tVertex p)
MakeConeFace makes a new face and two new edges between the edge and the point that are passed to it....
Definition: ConvexHull.cpp:545
void PrintFaces(std::ofstream &Ofile)
Prints the faces to Ofile.
Definition: ConvexHull.cpp:824
tEdge MakeNullEdge()
MakeNullEdge creates a new cell and initializes all pointers to NULL and sets all flags to off....
Definition: ConvexHull.cpp:639
void CleanFaces()
CleanFaces runs through the face list and deletes any face marked visible.
Definition: ConvexHull.cpp:239
static void add(Type &head, Type p)
Definition: ConvexHull.h:73
Namespace to encapsulate INDI client, drivers, and mediator classes.
Holds the connection type.