TORCS  1.3.9
The Open Racing Car Simulator
C-api.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 <SOLID/solid.h>
32 
33 #include <algorithm>
34 
35 #include "Box.h"
36 #include "Cone.h"
37 #include "Cylinder.h"
38 #include "Sphere.h"
39 #include "Complex.h"
40 #include "Encounter.h"
41 #include "Object.h"
42 #include "Simplex.h"
43 #include "Polygon.h"
44 #include "Polyhedron.h"
45 #include "Response.h"
46 #include "RespTable.h"
47 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
48 # include <sys/types.h>
49 #endif
50 
51 #include <map>
52 #include <set>
53 #include <vector>
54 
55 typedef vector<Point> PointBuf;
56 typedef vector<unsigned int> IndexBuf;
57 typedef vector<const Polytope *> PolyList;
58 typedef vector<Complex *> ComplexList;
59 typedef map<DtObjectRef, Object *> ObjectList;
60 typedef set<Encounter> ProxList;
61 
62 #ifdef WIN32
63 #define uint unsigned int
64 #endif
65 
73 
77 bool caching = true;
78 
79 extern Scalar rel_error;
80 
81 inline void move() { if (caching && currentObject) currentObject->move(); }
82 
83 
85  return new Box(x, y, z);
86 }
87 
89  return new Cone(radius, height);
90 }
91 
93  return new Cylinder(radius, height);
94 }
95 
97  return new Sphere(radius);
98 }
99 
102  return currentComplex;
103 }
104 
106  if (currentComplex->getBase().getPointer() == 0) {
107  Point *ptr = new Point[pointBuf.size()];
108  copy(pointBuf.begin(), pointBuf.end(), ptr);
109  currentComplex->setBase(ptr, true);
110  pointBuf.erase(pointBuf.begin(), pointBuf.end());
111  }
112  currentComplex->finish(polyList.size(), &polyList[0]);
113  polyList.erase(polyList.begin(), polyList.end());
114  complexList.push_back(currentComplex);
115  currentComplex = 0;
116 }
117 
118 void dtBegin(DtPolyType type) { currentType = type; }
119 void dtEnd() {
121  indexBuf.erase(indexBuf.begin(), indexBuf.end());
122 }
123 
125  Point p(x, y, z);
126  int i = pointBuf.size()-20;
127  if (i < 0) i = 0;
128  while ((uint)i < pointBuf.size() && !(pointBuf[i] == p)) ++i;
129  if ((uint)i == pointBuf.size()) pointBuf.push_back(p);
130  indexBuf.push_back(i);
131 }
132 
133 void dtVertexBase(const Point *base) {
134  currentComplex->setBase(base);
135 }
136 
137 void dtVertexIndex(DtIndex index) { indexBuf.push_back(index); }
138 
140  const DtIndex *indices) {
141  if (currentComplex) {
142  const Polytope *poly;
143  switch (type) {
144  case DT_SIMPLEX:
145  poly = new Simplex(currentComplex->getBase(), count, indices);
146  break;
147  case DT_POLYGON:
148  poly = new Polygon(currentComplex->getBase(), count, indices);
149  break;
150  case DT_POLYHEDRON:
151  if (currentComplex->getBase().getPointer() == 0) {
153  poly = new Polyhedron(currentComplex->getBase(), count, indices);
155  }
156  else poly = new Polyhedron(currentComplex->getBase(), count, indices);
157  break;
158  }
159  polyList.push_back(poly);
160  }
161 }
162 
163 void dtVertexRange(DtPolyType type, DtIndex first, DtCount count) {
164  DtIndex *indices = new DtIndex[count];
165  for (uint i = 0; i < count; ++i) indices[i] = first + i;
166  dtVertexIndices(type, count, indices);
167  delete [] indices;
168 }
169 
170 void dtDeleteShape(DtShapeRef shape) {
171  if (((Shape *)shape)->getType() == COMPLEX) {
172  ComplexList::iterator i =
173  find(complexList.begin(), complexList.end(), (Complex *)shape);
174  if (i != complexList.end()) complexList.erase(i);
175  }
176  delete (Shape *)shape;
177 }
178 
179 void dtChangeVertexBase(DtShapeRef shape, const Point *base) {
180  if (((Shape *)shape)->getType() == COMPLEX)
181  ((Complex *)shape)->changeBase(base);
182  for (ObjectList::const_iterator i = objectList.begin();
183  i != objectList.end(); ++i) {
184  if ((*i).second->shapePtr == (Shape *)shape) {
185  (*i).second->move();
186  }
187  }
188 }
189 
190 
191 // Object instantiation
192 
193 void dtCreateObject(DtObjectRef object, DtShapeRef shape) {
194  move();
195  currentObject = objectList[object] = new Object(object, (Shape *)shape);
196 }
197 
199  ObjectList::iterator i = objectList.find(object);
200  if (i != objectList.end()) {
201  move();
202  currentObject = (*i).second;
203  }
204 }
205 
207  ObjectList::iterator i = objectList.find(object);
208  if (i != objectList.end()) {
209  if (currentObject == (*i).second) currentObject = 0;
210  delete (*i).second;
211  objectList.erase(i);
212  }
213  respTable.cleanObject(object);
214 }
215 
218 }
219 
221  if (currentObject) currentObject->rotate(Quaternion(x, y, z, w));
222 }
223 
225  if (currentObject) currentObject->scale(x, y, z);
226 }
227 
228 void dtLoadIdentity() {
230 }
231 
232 void dtLoadMatrixf(const float *m) {
234 }
235 
236 void dtLoadMatrixd(const double *m) {
238 }
239 
240 void dtMultMatrixf(const float *m) {
242 }
243 
244 void dtMultMatrixd(const double *m) {
246 }
247 
248 // Response
249 
251  void *client_data) {
252  respTable.setDefault(Response(response, type, client_data));
253 }
254 
257 }
258 
260  DtResponseType type, void *client_data) {
261  respTable.setSingle(object, Response(response, type, client_data));
262 }
263 
265  respTable.setSingle(object, Response());
266 }
267 
269  respTable.resetSingle(object);
270 }
271 
273  DtResponse response, DtResponseType type,
274  void * client_data) {
275  respTable.setPair(object1, object2, Response(response, type, client_data));
276 }
277 
279  respTable.setPair(object1, object2, Response());
280 }
281 
283  respTable.resetPair(object1, object2);
284 }
285 
286 // Runtime
287 
288 void dtProceed() {
289  for (ComplexList::iterator i = complexList.begin();
290  i != complexList.end(); ++i)
291  (*i)->proceed();
292  for (ObjectList::const_iterator j = objectList.begin();
293  j != objectList.end(); ++j)
294  (*j).second->proceed();
295 }
296 
298  for (ObjectList::const_iterator i = objectList.begin();
299  i != objectList.end(); ++i)
300  (*i).second->move();
301  caching = true;
302 }
303 
304 void dtDisableCaching() { caching = false; }
305 
306 void dtSetTolerance(DtScalar tol) { rel_error = tol; }
307 
308 void addPair(ObjectPtr object1, ObjectPtr object2) {
309  proxList.insert(Encounter(object1, object2));
310 }
311 
312 void removePair(ObjectPtr object1, ObjectPtr object2) {
313  proxList.erase(Encounter(object1, object2));
314 }
315 
317  static Point p1, p2;
318  const Response& resp = respTable.find(e.obj1->ref, e.obj2->ref);
319  switch (resp.type) {
320  case DT_SIMPLE_RESPONSE:
321  if (intersect(*e.obj1, *e.obj2, e.sep_axis)) {
322  resp(e.obj1->ref, e.obj2->ref);
323  return true;
324  }
325  break;
326  case DT_SMART_RESPONSE:
327  if (prev_closest_points(*e.obj1, *e.obj2, e.sep_axis, p1, p2)) {
328  Vector v = e.obj1->prev(p1) - e.obj2->prev(p2);
329  resp(e.obj1->ref, e.obj2->ref, p1, p2, v);
330  return true;
331  }
332  break;
334  if (common_point(*e.obj1, *e.obj2, e.sep_axis, p1, p2)) {
335  resp(e.obj1->ref, e.obj2->ref, p1, p2, Vector(0, 0, 0));
336  return true;
337  }
338  break;
339  // Eric Espie: warning
340  case DT_NO_RESPONSE:
341  break;
342  }
343  return false;
344 }
345 
347  move();
348  DtCount count = 0;
349  if (caching) {
350  for (ProxList::iterator i = proxList.begin(); i != proxList.end(); ++i)
351  if (object_test((Encounter &)*i)) ++count;
352  }
353  else {
354  for (ObjectList::const_iterator j = objectList.begin();
355  j != objectList.end(); ++j)
356  for (ObjectList::const_iterator i = objectList.begin();
357  i != j; ++i) {
358  Encounter e((*i).second, (*j).second);
359  if (object_test(e)) ++count;
360  }
361  }
362  return count;
363 }
DtObjectRef ref
Definition: Object.h:64
void dtClearPairResponse(DtObjectRef object1, DtObjectRef object2)
Definition: C-api.cpp:278
void finish(int n, const Polytope **p)
Definition: Complex.cpp:65
ObjectPtr obj2
Definition: Encounter.h:42
void dtScale(DtScalar x, DtScalar y, DtScalar z)
Definition: C-api.cpp:224
void dtCreateObject(DtObjectRef object, DtShapeRef shape)
Definition: C-api.cpp:193
DtCount dtTest()
Definition: C-api.cpp:346
void dtClearDefaultResponse()
Definition: C-api.cpp:255
Definition: Shape.h:40
bool prev_closest_points(const Object &a, const Object &b, Vector &v, Point &pa, Point &pb)
Definition: Object.cpp:138
void rotate(const Quaternion &q)
Definition: Object.h:49
void addPair(ObjectPtr object1, ObjectPtr object2)
Definition: C-api.cpp:308
set< Encounter > ProxList
Definition: C-api.cpp:60
void dtTranslate(DtScalar x, DtScalar y, DtScalar z)
Definition: C-api.cpp:216
void move()
Definition: C-api.cpp:81
void dtSetDefaultResponse(DtResponse response, DtResponseType type, void *client_data)
Definition: C-api.cpp:250
void resetPair(DtObjectRef obj1, DtObjectRef obj2)
Definition: RespTable.h:64
void multMatrix(const float v[16])
Definition: Object.h:57
void resetSingle(DtObjectRef obj)
Definition: RespTable.h:58
map< DtObjectRef, Object * > ObjectList
Definition: C-api.cpp:59
void dtLoadMatrixd(const double *m)
Definition: C-api.cpp:236
void dtSetObjectResponse(DtObjectRef object, DtResponse response, DtResponseType type, void *client_data)
Definition: C-api.cpp:259
ObjectPtr obj1
Definition: Encounter.h:41
bool object_test(Encounter &e)
Definition: C-api.cpp:316
void dtEnableCaching()
Definition: C-api.cpp:297
void dtVertexIndices(DtPolyType type, DtCount count, const DtIndex *indices)
Definition: C-api.cpp:139
vector< Complex * > ComplexList
Definition: C-api.cpp:58
Definition: Cone.h:36
void dtEndComplexShape()
Definition: C-api.cpp:105
PointBuf pointBuf
Definition: C-api.cpp:66
void dtSelectObject(DtObjectRef object)
Definition: C-api.cpp:198
DtPolyType currentType
Definition: C-api.cpp:74
void dtMultMatrixd(const double *m)
Definition: C-api.cpp:244
bool common_point(const BBoxNode *tree, const Convex &c, const BBox &bb, const Transform &b2a, Vector &v, Point &pa, Point &pb)
Definition: BBoxTree.cpp:204
ProxList proxList
Definition: C-api.cpp:72
const VertexBase & getBase() const
Definition: Complex.h:55
void scale(Scalar x, Scalar y, Scalar z)
Definition: Object.h:50
void move()
Definition: Object.cpp:54
void setDefault(const Response &resp)
Definition: RespTable.h:52
void dtDeleteObject(DtObjectRef object)
Definition: C-api.cpp:206
Object * currentObject
Definition: C-api.cpp:76
void dtDeleteShape(DtShapeRef shape)
Definition: C-api.cpp:170
vector< const Polytope * > PolyList
Definition: C-api.cpp:57
void dtVertexRange(DtPolyType type, DtIndex first, DtCount count)
Definition: C-api.cpp:163
bool caching
Definition: C-api.cpp:77
void cleanObject(DtObjectRef obj)
Definition: RespTable.cpp:48
ComplexList complexList
Definition: C-api.cpp:69
void dtEnd()
Definition: C-api.cpp:119
PolyList polyList
Definition: C-api.cpp:68
unsigned int DtCount
Definition: solid.h:37
void setPair(DtObjectRef obj1, DtObjectRef obj2, const Response &resp)
Definition: RespTable.h:60
void dtLoadIdentity()
Definition: C-api.cpp:228
void(* DtResponse)(void *client_data, DtObjectRef object1, DtObjectRef object2, const DtCollData *coll_data)
Definition: solid.h:61
Complex * currentComplex
Definition: C-api.cpp:75
vector< Point > PointBuf
Definition: C-api.cpp:55
void translate(const Vector &v)
Definition: Object.h:48
double DtScalar
Definition: solid.h:34
static Point p[4]
Definition: Convex.cpp:54
vector< unsigned int > IndexBuf
Definition: C-api.cpp:56
Definition: Shape.h:44
void dtResetObjectResponse(DtObjectRef object)
Definition: C-api.cpp:268
void dtResetPairResponse(DtObjectRef object1, DtObjectRef object2)
Definition: C-api.cpp:282
void dtMultMatrixf(const float *m)
Definition: C-api.cpp:240
DtPolyType
Definition: solid.h:42
void dtDisableCaching()
Definition: C-api.cpp:304
DtResponseType type
Definition: Response.h:43
Transform prev
Definition: Object.h:63
void dtBegin(DtPolyType type)
Definition: C-api.cpp:118
DtShapeRef dtNewComplexShape()
Definition: C-api.cpp:100
unsigned int DtIndex
Definition: solid.h:36
DtShapeRef dtCone(DtScalar radius, DtScalar height)
Definition: C-api.cpp:88
void dtSetTolerance(DtScalar tol)
Definition: C-api.cpp:306
static Vector y[4]
Definition: Convex.cpp:56
void setIdentity()
Definition: Object.h:52
bool intersect(const BBox &a, const BBox &b)
Definition: BBox.h:92
void dtVertex(DtScalar x, DtScalar y, DtScalar z)
Definition: C-api.cpp:124
void dtSetPairResponse(DtObjectRef object1, DtObjectRef object2, DtResponse response, DtResponseType type, void *client_data)
Definition: C-api.cpp:272
Definition: Sphere.h:36
void dtRotate(DtScalar x, DtScalar y, DtScalar z, DtScalar w)
Definition: C-api.cpp:220
void dtVertexBase(const Point *base)
Definition: C-api.cpp:133
IndexBuf indexBuf
Definition: C-api.cpp:67
#define Scalar
Definition: Basic.h:34
DtShapeRef dtBox(DtScalar x, DtScalar y, DtScalar z)
Definition: C-api.cpp:84
RespTable respTable
Definition: C-api.cpp:71
Scalar rel_error
Definition: Convex.cpp:35
DtShapeRef dtCylinder(DtScalar radius, DtScalar height)
Definition: C-api.cpp:92
ObjectList objectList
Definition: C-api.cpp:70
void dtChangeVertexBase(DtShapeRef shape, const Point *base)
Definition: C-api.cpp:179
void removePair(ObjectPtr object1, ObjectPtr object2)
Definition: C-api.cpp:312
DtShapeRef dtSphere(DtScalar radius)
Definition: C-api.cpp:96
Definition: Vector.h:32
Definition: Object.h:41
void dtVertexIndex(DtIndex index)
Definition: C-api.cpp:137
Definition: Box.h:36
void dtClearObjectResponse(DtObjectRef object)
Definition: C-api.cpp:264
void * DtObjectRef
Definition: solid.h:39
void setBase(const Point *ptr, bool free=false)
Definition: Complex.h:56
void * DtShapeRef
Definition: solid.h:40
void dtLoadMatrixf(const float *m)
Definition: C-api.cpp:232
Definition: Point.h:34
void setSingle(DtObjectRef obj, const Response &resp)
Definition: RespTable.h:54
const Response & find(DtObjectRef obj1, DtObjectRef obj2) const
Definition: RespTable.cpp:35
Vector sep_axis
Definition: Encounter.h:43
const Point * getPointer() const
Definition: VertexBase.h:40
DtResponseType
Definition: solid.h:48
void dtProceed()
Definition: C-api.cpp:288
void setMatrix(const float v[16])
Definition: Object.h:54