ReferenceΒΆ

  1/***
  2 * libccd
  3 * ---------------------------------
  4 * Copyright (c)2010,2011 Daniel Fiser <danfis@danfis.cz>
  5 *
  6 *
  7 *  This file is part of libccd.
  8 *
  9 *  Distributed under the OSI-approved BSD License (the "License");
 10 *  see accompanying file BDS-LICENSE for details or see
 11 *  <http://www.opensource.org/licenses/bsd-license.php>.
 12 *
 13 *  This software is distributed WITHOUT ANY WARRANTY; without even the
 14 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15 *  See the License for more information.
 16 */
 17
 18#ifndef __CCD_H__
 19#define __CCD_H__
 20
 21#include <ccd/vec3.h>
 22
 23#ifdef __cplusplus
 24extern "C" {
 25#endif /* __cplusplus */
 26
 27/**
 28 * Type of *support* function that takes pointer to 3D object and direction
 29 * and returns (via vec argument) furthest point from object in specified
 30 * direction.
 31 */
 32typedef void (*ccd_support_fn)(const void *obj, const ccd_vec3_t *dir,
 33                               ccd_vec3_t *vec);
 34
 35/**
 36 * Returns (via dir argument) first direction vector that will be used in
 37 * initialization of algorithm.
 38 */
 39typedef void (*ccd_first_dir_fn)(const void *obj1, const void *obj2,
 40                                 ccd_vec3_t *dir);
 41
 42
 43/**
 44 * Returns (via center argument) geometric center (some point near center)
 45 * of given object.
 46 */
 47typedef void (*ccd_center_fn)(const void *obj1, ccd_vec3_t *center);
 48
 49/**
 50 * Main structure of CCD algorithm.
 51 */
 52struct _ccd_t {
 53    ccd_first_dir_fn first_dir; //!< Returns initial direction where first
 54                                //!< support point will be searched
 55    ccd_support_fn support1; //!< Function that returns support point of
 56                             //!< first object
 57    ccd_support_fn support2; //!< Function that returns support point of
 58                             //!< second object
 59
 60    ccd_center_fn center1; //!< Function that returns geometric center of
 61                           //!< first object
 62    ccd_center_fn center2; //!< Function that returns geometric center of
 63                           //!< second object
 64
 65    unsigned long max_iterations; //!< Maximal number of iterations
 66    ccd_real_t epa_tolerance;
 67    ccd_real_t mpr_tolerance; //!< Boundary tolerance for MPR algorithm
 68    ccd_real_t dist_tolerance;
 69};
 70typedef struct _ccd_t ccd_t;
 71
 72/**
 73 * Default first direction.
 74 */
 75CCD_EXPORT void ccdFirstDirDefault(const void *o1, const void *o2,
 76                                    ccd_vec3_t *dir);
 77
 78#define CCD_INIT(ccd) \
 79    do { \
 80        (ccd)->first_dir = ccdFirstDirDefault; \
 81        (ccd)->support1 = NULL; \
 82        (ccd)->support2 = NULL; \
 83        (ccd)->center1  = NULL; \
 84        (ccd)->center2  = NULL; \
 85        \
 86        (ccd)->max_iterations = (unsigned long)-1; \
 87        (ccd)->epa_tolerance = CCD_REAL(0.0001); \
 88        (ccd)->mpr_tolerance = CCD_REAL(0.0001); \
 89        (ccd)->dist_tolerance = CCD_REAL(1E-6); \
 90    } while(0)
 91
 92
 93/**
 94 * Returns true if two given objects interest.
 95 */
 96CCD_EXPORT int ccdGJKIntersect(const void *obj1, const void *obj2,
 97                                const ccd_t *ccd);
 98
 99/**
100 * This function computes separation vector of two objects. Separation
101 * vector is minimal translation of obj2 to get obj1 and obj2 speparated
102 * (without intersection).
103 * Returns 0 if obj1 and obj2 intersect and sep is filled with translation
104 * vector. If obj1 and obj2 don't intersect -1 is returned.
105 * If memory allocation fails -2 is returned.
106 */
107CCD_EXPORT int ccdGJKSeparate(const void *obj1, const void *obj2,
108                               const ccd_t *ccd, ccd_vec3_t *sep);
109
110/**
111 * Computes penetration of obj2 into obj1.
112 * Depth of penetration, direction and position is returned. It means that
113 * if obj2 is translated by distance depth in direction dir objects will
114 * have touching contact, pos should be position in global coordinates
115 * where force should take a place.
116 *
117 * CCD+EPA algorithm is used.
118 *
119 * Returns 0 if obj1 and obj2 intersect and depth, dir and pos are filled
120 * if given non-NULL pointers.
121 * If obj1 and obj2 don't intersect -1 is returned.
122 * If memory allocation fails -2 is returned.
123 */
124CCD_EXPORT int ccdGJKPenetration(const void *obj1, const void *obj2,
125                                  const ccd_t *ccd, ccd_real_t *depth,
126                                  ccd_vec3_t *dir, ccd_vec3_t *pos);
127
128/**
129 * Returns true if two given objects intersect - MPR algorithm is used.
130 */
131CCD_EXPORT int ccdMPRIntersect(const void *obj1, const void *obj2,
132                                const ccd_t *ccd);
133
134/**
135 * Computes penetration of obj2 into obj1.
136 * Depth of penetration, direction and position is returned, i.e. if obj2
137 * is translated by computed depth in resulting direction obj1 and obj2
138 * would have touching contact. Position is point in global coordinates
139 * where force should take a place.
140 *
141 * Minkowski Portal Refinement algorithm is used (MPR, a.k.a. XenoCollide,
142 * see Game Programming Gem 7).
143 *
144 * Returns 0 if obj1 and obj2 intersect, otherwise -1 is returned.
145 */
146CCD_EXPORT int ccdMPRPenetration(const void *obj1, const void *obj2,
147                                  const ccd_t *ccd, ccd_real_t *depth,
148                                  ccd_vec3_t *dir, ccd_vec3_t *pos);
149
150#ifdef __cplusplus
151} /* extern "C" */
152#endif /* __cplusplus */
153
154#endif /* __CCD_H__ */