Instrument Neutral Distributed Interface INDI 2.2.5
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/*
22This code is described in "Computational Geometry in C" (Second Edition),
23Chapter 4. It is not written to be comprehensible without the
24explanation in that book.
25
26Input: 3n integer coordinates for the points.
27Output: the 3D convex hull, in postscript with embedded comments
28 showing the vertices and faces.
29
30Compile: gcc -o chull chull.c (or simply: make)
31
32Written by Joseph O'Rourke, with contributions by
33 Kristy Anderson, John Kutcher, Catherine Schevon, Susan Weller.
34Last modified: May 2000
35Questions to [email protected].
36
37--------------------------------------------------------------------
38This code is Copyright 2000 by Joseph O'Rourke. It may be freely
39redistributed in its entirety provided that this copyright notice is
40not removed.
41--------------------------------------------------------------------
42*/
43
44#include <gsl/gsl_matrix.h>
45
46#include <cmath>
47#include <cstring>
48#include <fstream>
49#include <limits>
50
51namespace INDI
52{
53namespace 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 */
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.
void ReadVertices()
ReadVertices: Reads in the vertices, and links them into a circular list with MakeNullVertex....
void CleanUp(tVertex *pvnext)
CleanUp goes through each data structure list and clears all flags and NULLs out some pointers....
tFace MakeNullFace()
MakeNullFace creates a new face structure and initializes all of its flags to NULL and sets all the f...
void EdgeOrderOnFaces()
EdgeOrderOnFaces: puts e0 between v0 and v1, e1 between v1 and v2, e2 between v2 and v0 on each face....
void Reset()
Frees the vertices edges and faces lists and resets the debug and check flags.
void PrintPoint(tVertex p)
Prints a single vertex to the standard output.
void Convexity()
Convexity checks that the volume between every face and every point is negative. This shows that each...
void Consistency()
Consistency runs through the edge list and checks that all adjacent faces have their endpoints in opp...
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...
bool AddOne(tVertex p)
AddOne is passed a vertex. It first determines all faces visible from that point. If none are visible...
void MakeNewVertex(double x, double y, double z, int VertexId)
Makes a vertex from the supplied information and adds it to the vertices list.
static void swap(Type &t, Type &x, Type &y)
Definition ConvexHull.h:105
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 ...
int GetScaleFactor() const
Set the floating point to integer scaling factor.
Definition ConvexHull.h:263
void PrintVertices(std::ofstream &Ofile)
Prints vertices to Ofile.
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...
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 ...
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...
void PrintOut(const char *FileName, tVertex v)
Prints vertices, edges and faces to the standard error output.
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...
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...
void CleanEdges()
CleanEdges runs through the edge list and cleans up the structure. If there is a newface then it will...
void ConstructHull()
ConstructHull adds the vertices to the hull one at a time. The hull vertices are those in the list ma...
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.
void PrintEdges(std::ofstream &Ofile)
Prints the edges Ofile.
void PrintObj(const char *FileName="chull.obj")
Outputs the faces in Lightwave obj format for 3d viewing. The files chull.obj and chull....
void Print()
Print: Prints out the vertices and the faces. Uses the vnum indices corresponding to the order in whi...
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....
void PrintFaces(std::ofstream &Ofile)
Prints the faces to Ofile.
tEdge MakeNullEdge()
MakeNullEdge creates a new cell and initializes all pointers to NULL and sets all flags to off....
void CleanFaces()
CleanFaces runs through the face list and deletes any face marked visible.
static void add(Type &head, Type p)
Definition ConvexHull.h:73
Namespace to encapsulate INDI client, drivers, and mediator classes.
Holds the connection type.