Added: dSpaceSetSublevel/dSpaceGetSublevel and possibility to collide a space as...
[ode.git] / ode / src / collision_kernel.h
blob5eb454d13056e54e441ad572e431e898b361bcb5
1 /*************************************************************************
2 * *
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
12 * file LICENSE.TXT. *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
15 * *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20 * *
21 *************************************************************************/
25 internal data structures and functions for collision detection.
29 #ifndef _ODE_COLLISION_KERNEL_H_
30 #define _ODE_COLLISION_KERNEL_H_
32 #include <ode/common.h>
33 #include <ode/contact.h>
34 #include <ode/collision.h>
35 #include "config.h"
36 #include "objects.h"
38 //****************************************************************************
39 // constants and macros
41 // mask for the number-of-contacts field in the dCollide() flags parameter
42 #define NUMC_MASK (0xffff)
44 #define IS_SPACE(geom) \
45 ((geom)->type >= dFirstSpaceClass && (geom)->type <= dLastSpaceClass)
47 //****************************************************************************
48 // geometry object base class
51 // geom flags.
53 // GEOM_DIRTY means that the space data structures for this geom are
54 // potentially not up to date. NOTE THAT all space parents of a dirty geom
55 // are themselves dirty. this is an invariant that must be enforced.
57 // GEOM_AABB_BAD means that the cached AABB for this geom is not up to date.
58 // note that GEOM_DIRTY does not imply GEOM_AABB_BAD, as the geom might
59 // recalculate its own AABB but does not know how to update the space data
60 // structures for the space it is in. but GEOM_AABB_BAD implies GEOM_DIRTY.
61 // the valid combinations are:
62 // 0
63 // GEOM_DIRTY
64 // GEOM_DIRTY|GEOM_AABB_BAD
65 // GEOM_DIRTY|GEOM_AABB_BAD|GEOM_POSR_BAD
67 enum {
68 GEOM_DIRTY = 1, // geom is 'dirty', i.e. position unknown
69 GEOM_POSR_BAD = 2, // geom's final posr is not valid
70 GEOM_AABB_BAD = 4, // geom's AABB is not valid
71 GEOM_PLACEABLE = 8, // geom is placeable
72 GEOM_ENABLED = 16, // geom is enabled
74 // Ray specific
75 RAY_FIRSTCONTACT = 0x10000,
76 RAY_BACKFACECULL = 0x20000,
77 RAY_CLOSEST_HIT = 0x40000
81 // geometry object base class. pos and R will either point to a separately
82 // allocated buffer (if body is 0 - pos points to the dxPosR object) or to
83 // the pos and R of the body (if body nonzero).
84 // a dGeomID is a pointer to this object.
86 struct dxGeom : public dBase {
87 int type; // geom type number, set by subclass constructor
88 int gflags; // flags used by geom and space
89 void *data; // user-defined data pointer
90 dBodyID body; // dynamics body associated with this object (if any)
91 dxGeom *body_next; // next geom in body's linked list of associated geoms
92 dxPosR *final_posr; // final position of the geom in world coordinates
93 dxPosR *offset_posr; // offset from body in local coordinates
95 // information used by spaces
96 dxGeom *next; // next geom in linked list of geoms
97 dxGeom **tome; // linked list backpointer
98 dxSpace *parent_space;// the space this geom is contained in, 0 if none
99 dReal aabb[6]; // cached AABB for this space
100 unsigned long category_bits,collide_bits;
102 dxGeom (dSpaceID _space, int is_placeable);
103 virtual ~dxGeom();
106 // calculate our new final position from our offset and body
107 void computePosr();
109 // recalculate our new final position if needed
110 void recomputePosr()
112 if (gflags & GEOM_POSR_BAD) {
113 computePosr();
114 gflags &= ~GEOM_POSR_BAD;
118 virtual void computeAABB()=0;
119 // compute the AABB for this object and put it in aabb. this function
120 // always performs a fresh computation, it does not inspect the
121 // GEOM_AABB_BAD flag.
123 virtual int AABBTest (dxGeom *o, dReal aabb[6]);
124 // test whether the given AABB object intersects with this object, return
125 // 1=yes, 0=no. this is used as an early-exit test in the space collision
126 // functions. the default implementation returns 1, which is the correct
127 // behavior if no more detailed implementation can be provided.
129 // utility functions
131 // compute the AABB only if it is not current. this function manipulates
132 // the GEOM_AABB_BAD flag.
134 void recomputeAABB() {
135 if (gflags & GEOM_AABB_BAD) {
136 // our aabb functions assume final_posr is up to date
137 recomputePosr();
138 computeAABB();
139 gflags &= ~GEOM_AABB_BAD;
143 // add and remove this geom from a linked list maintained by a space.
145 void spaceAdd (dxGeom **first_ptr) {
146 next = *first_ptr;
147 tome = first_ptr;
148 if (*first_ptr) (*first_ptr)->tome = &next;
149 *first_ptr = this;
151 void spaceRemove() {
152 if (next) next->tome = tome;
153 *tome = next;
156 // add and remove this geom from a linked list maintained by a body.
158 void bodyAdd (dxBody *b) {
159 body = b;
160 body_next = b->geom;
161 b->geom = this;
163 void bodyRemove();
166 //****************************************************************************
167 // the base space class
169 // the contained geoms are divided into two kinds: clean and dirty.
170 // the clean geoms have not moved since they were put in the list,
171 // and their AABBs are valid. the dirty geoms have changed position, and
172 // their AABBs are may not be valid. the two types are distinguished by the
173 // GEOM_DIRTY flag. all dirty geoms come *before* all clean geoms in the list.
175 struct dxSpace : public dxGeom {
176 int count; // number of geoms in this space
177 dxGeom *first; // first geom in list
178 int cleanup; // cleanup mode, 1=destroy geoms on exit
179 int sublevel; // space sublevel (used in dSpaceCollide2). NOT TRACKED AUTOMATICALLY!!!
181 // cached state for getGeom()
182 int current_index; // only valid if current_geom != 0
183 dxGeom *current_geom; // if 0 then there is no information
185 // locking stuff. the space is locked when it is currently traversing its
186 // internal data structures, e.g. in collide() and collide2(). operations
187 // that modify the contents of the space are not permitted when the space
188 // is locked.
189 int lock_count;
191 dxSpace (dSpaceID _space);
192 ~dxSpace();
194 void computeAABB();
196 void setCleanup (int mode);
197 int getCleanup();
198 void setSublevel(int value);
199 int getSublevel() const;
200 int query (dxGeom *geom);
201 int getNumGeoms();
202 virtual dxGeom *getGeom (int i);
204 virtual void add (dxGeom *);
205 virtual void remove (dxGeom *);
206 virtual void dirty (dxGeom *);
208 virtual void cleanGeoms()=0;
209 // turn all dirty geoms into clean geoms by computing their AABBs and any
210 // other space data structures that are required. this should clear the
211 // GEOM_DIRTY and GEOM_AABB_BAD flags of all geoms.
213 virtual void collide (void *data, dNearCallback *callback)=0;
214 virtual void collide2 (void *data, dxGeom *geom, dNearCallback *callback)=0;
218 //****************************************************************************
219 // Initialization and finalization functions
221 void dInitColliders();
222 void dFinitColliders();
224 void dClearPosrCache(void);
225 void dFinitUserClasses();
228 #endif