TORCS  1.3.9
The Open Racing Car Simulator
Polyhedron.cpp
Go to the documentation of this file.
1 /*
2  SOLID - Software Library for Interference Detection
3  Copyright (C) 1997-1998 Gino van den Bergen
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public
16  License along with this library; if not, write to the Free
17  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 
19  Please send remarks, questions and bug reports to gino@win.tue.nl,
20  or write to:
21  Gino van den Bergen
22  Department of Mathematics and Computing Science
23  Eindhoven University of Technology
24  P.O. Box 513, 5600 MB Eindhoven, The Netherlands
25 */
26 
27 #ifdef _MSC_VER
28 #pragma warning(disable:4786) // identifier was truncated to '255'
29 #endif // _MSC_VER
30 
31 #include "Polyhedron.h"
32 
33 #ifdef QHULL
34 
35 extern "C" {
36 #include <qhull_a.h>
37 }
38 
39 #include <vector>
40 #include <new>
41 
42 typedef vector<unsigned int> IndexBuf;
43 
44 char qh_version[] = "SOLID 2.0";
45 
46 Polyhedron::Polyhedron(const VertexBase& b, int c, const unsigned int v[]) :
47  Polytope(b, c, v), cobound(0) {
48  boolT ismalloc;
49  int curlong, totlong, exitcode;
50  char options[200];
51 
52  facetT *facet;
53  vertexT *vertex;
54  vertexT **vertexp;
55 
56  coordT *array = new coordT[numVerts()*3];
57  coordT *p = &array[0];
58  int i;
59  for (i = 0; i < numVerts(); ++i) {
60  *p++ = (*this)[i][X];
61  *p++ = (*this)[i][Y];
62  *p++ = (*this)[i][Z];
63  }
64 
65  ismalloc = False; // True if qh_freeqhull should 'free(array)'
66  qh_init_A(stdin, stdout, stderr, 0, NULL);
67  if (exitcode = setjmp(qh errexit)) exit(exitcode);
68  sprintf(options, "qhull Qx i s Tcv C-0");
69  qh_initflags(options);
70  qh_init_B(&array[0], numVerts(), 3, ismalloc);
71  qh_qhull();
72  qh_check_output();
73 
75  IndexBuf facetIndices;
76  FORALLfacets {
77  setT *vertices = qh_facet3vertex(facet);
78  FOREACHvertex_(vertices) facetIndices.push_back(qh_pointid(vertex->point));
79  for (int i = 0, j = facetIndices.size()-1;
80  i < facetIndices.size(); j = i++)
81  indexBuf[facetIndices[j]].push_back(facetIndices[i]);
82  facetIndices.erase(facetIndices.begin(), facetIndices.end());
83  }
84 
85  cobound = new IndexArray[numVerts()];
86  for (i = 0; i < numVerts(); ++i)
87  if (indexBuf[i].size())
88  new(&cobound[i]) IndexArray(indexBuf[i].size(), &indexBuf[i][0]);
89 
90  curr_vertex = 0;
91  while (indexBuf[curr_vertex].size() == 0) ++curr_vertex;
92 
93  delete [] indexBuf;
94  delete [] array;
95 
96  qh NOerrexit = True;
97  qh_freeqhull(!qh_ALL);
98  qh_memfreeshort(&curlong, &totlong);
99 }
100 
102  delete [] cobound;
103 }
104 
105 Point Polyhedron::support(const Vector& v) const {
106  int last_vertex = -1;
107  Scalar h = dot((*this)[curr_vertex], v), d;
108  for (;;) {
109  IndexArray& curr_cobound = cobound[curr_vertex];
110  int i = 0, n = curr_cobound.size();
111  while (i != n &&
112  (curr_cobound[i] == last_vertex || (d = dot((*this)[curr_cobound[i]], v)) <= h))
113  ++i;
114  if (i == n) break;
115  last_vertex = curr_vertex;
116  curr_vertex = curr_cobound[i];
117  h = d;
118  }
119  return (*this)[curr_vertex];
120 }
121 
122 #else
123 
124 Polyhedron::Polyhedron(const VertexBase& b, int c, const unsigned int v[]) :
125  Polytope(b, c, v), cobound(0), curr_vertex(0) {}
126 
128 
130  int c = 0;
131  Scalar h = dot((*this)[0], v), d;
132  for (int i = 1; i < numVerts(); ++i) {
133  if ((d = dot((*this)[i], v)) > h) { c = i; h = d; }
134  }
135  return (*this)[c];
136 }
137 
138 #endif
139 
static ssgLoaderOptionsEx options
Definition: grscene.cpp:165
unsigned int curr_vertex
Definition: Polyhedron.h:46
int size() const
Definition: IndexArray.h:46
Scalar dot(const Quaternion &q1, const Quaternion &q2)
Definition: Quaternion.h:163
Polyhedron(const VertexBase &b, int c, const unsigned int v[])
Definition: Polyhedron.cpp:124
Definition: Basic.h:58
Point support(const Vector &) const
Definition: Polyhedron.cpp:129
Definition: Basic.h:58
int numVerts() const
Definition: Polytope.h:46
static Point p[4]
Definition: Convex.cpp:54
Definition: Basic.h:58
vector< unsigned int > IndexBuf
Definition: C-api.cpp:56
IndexBuf indexBuf
Definition: C-api.cpp:67
#define Scalar
Definition: Basic.h:34
Definition: Vector.h:32
Definition: Point.h:34
IndexArray * cobound
Definition: Polyhedron.h:45