├── .gitignore ├── .travis.yml ├── BSD-LICENSE ├── CMakeLists.txt ├── Makefile.am ├── README.md ├── bootstrap ├── ccd-config.cmake.in ├── ccd.pc.in ├── configure.ac ├── doc ├── CMakeLists.txt ├── Makefile ├── _build │ └── .dir ├── _static │ └── .dir ├── _templates │ └── .dir ├── compile-and-install.rst ├── conf.py ├── examples.rst ├── index.rst └── reference.rst ├── make-release.sh └── src ├── .gitignore ├── CMakeLists.txt ├── Makefile ├── Makefile.am ├── Makefile.include ├── alloc.h ├── ccd.c ├── ccd ├── ccd.h ├── ccd_export.h ├── compiler.h ├── config.h.cmake.in ├── config.h.m4 ├── quat.h └── vec3.h ├── dbg.h ├── list.h ├── mpr.c ├── polytope.c ├── polytope.h ├── simplex.h ├── support.c ├── support.h ├── testsuites ├── .gitignore ├── CMakeLists.txt ├── Makefile ├── Makefile.am ├── bench.c ├── bench2.c ├── boxbox.c ├── boxbox.h ├── boxcyl.c ├── boxcyl.h ├── common.c ├── common.h ├── cu │ ├── .dir │ ├── .gitignore │ ├── CMakeLists.txt │ ├── COPYING │ ├── COPYING.LESSER │ ├── Makefile │ ├── Makefile.am │ ├── check-regressions │ ├── cu.c │ ├── cu.h │ └── latest.sh ├── cylcyl.c ├── cylcyl.h ├── main.c ├── mpr_boxbox.c ├── mpr_boxbox.h ├── mpr_boxcyl.c ├── mpr_boxcyl.h ├── mpr_cylcyl.c ├── mpr_cylcyl.h ├── polytope.c ├── polytope.h ├── regressions │ ├── .dir │ ├── TSBoxBox.err │ ├── TSBoxBox.out │ ├── TSBoxCyl.err │ ├── TSBoxCyl.out │ ├── TSCylCyl.err │ ├── TSCylCyl.out │ ├── TSMPRBoxBox.err │ ├── TSMPRBoxBox.out │ ├── TSMPRBoxCyl.err │ ├── TSMPRBoxCyl.out │ ├── TSMPRCylCyl.err │ ├── TSMPRCylCyl.out │ ├── TSPt.err │ ├── TSPt.out │ ├── TSSphereSphere.err │ ├── TSSphereSphere.out │ ├── TSVec3.err │ └── TSVec3.out ├── spheresphere.c ├── spheresphere.h ├── support.c ├── support.h ├── vec3.c └── vec3.h └── vec3.c /.gitignore: -------------------------------------------------------------------------------- 1 | Makefile.in 2 | autom4te.cache/* 3 | aclocal.m4 4 | config.guess 5 | config.sub 6 | configure 7 | depcomp 8 | install-sh 9 | ltmain.sh 10 | missing 11 | *~ 12 | src/gjk/config.h.in 13 | build/* 14 | ccd.pc 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | 4 | language: c 5 | compiler: 6 | - gcc 7 | - clang 8 | 9 | env: 10 | global: 11 | - PREFIX="$TRAVIS_BUILD_DIR/build/install" 12 | matrix: 13 | - USE_AUTOTOOLS=yes 14 | - USE_CMAKE=yes 15 | - USE_MAKEFILE=yes USE_DOUBLE=yes 16 | - USE_MAKEFILE=yes USE_SINGLE=yes 17 | 18 | script: 19 | - mkdir -p "$PREFIX" 20 | - if [[ "$USE_AUTOTOOLS" == "yes" ]]; then ./bootstrap && cd build && ../configure --prefix "$PREFIX"; fi 21 | - if [[ "$USE_CMAKE" == "yes" ]]; then cd build && cmake "-DCMAKE_INSTALL_PREFIX=$PREFIX" ..; fi 22 | - if [[ "$USE_MAKEFILE" == "yes" ]]; then cd src; fi 23 | - make && make install 24 | -------------------------------------------------------------------------------- /BSD-LICENSE: -------------------------------------------------------------------------------- 1 | libccd 2 | ------- 3 | 4 | Copyright (c)2010-2012 Daniel Fiser , 5 | Intelligent and Mobile Robotics Group, Department of Cybernetics, 6 | Faculty of Electrical Engineering, Czech Technical University in Prague. 7 | All rights reserved. 8 | 9 | 10 | This work was supported by SYMBRION and REPLICATOR projects. 11 | The SYMBRION project is funded by European Commission within the work 12 | "Future and Emergent Technologies Proactive" under grant agreement no. 13 | 216342. 14 | The REPLICATOR project is funded within the work programme "Cognitive 15 | Systems, Interaction, Robotics" under grant agreement no. 216240. 16 | http://www.symbrion.eu/ 17 | http://www.replicators.eu/ 18 | 19 | 20 | Redistribution and use in source and binary forms, with or without 21 | modification, are permitted provided that the following conditions are met: 22 | 23 | - Redistributions of source code must retain the above copyright notice, 24 | this list of conditions and the following disclaimer. 25 | 26 | - Redistributions in binary form must reproduce the above copyright 27 | notice, this list of conditions and the following disclaimer in the 28 | documentation and/or other materials provided with the distribution. 29 | 30 | - Neither the name of the University nor the names of its contributors 31 | may be used to endorse or promote products derived from this software 32 | without specific prior written permission. 33 | 34 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 35 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 38 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 39 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 40 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 41 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 42 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 43 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 44 | POSSIBILITY OF SUCH DAMAGE. 45 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8.11) 2 | 3 | if(POLICY CMP0042) 4 | cmake_policy(SET CMP0042 NEW) 5 | endif() 6 | 7 | # Can not explicitly declared the software as C in project command due to bug: 8 | # https://gitlab.kitware.com/cmake/cmake/issues/16967 9 | project(libccd) 10 | 11 | set(CCD_VERSION_MAJOR 2) 12 | set(CCD_VERSION_MINOR 0) 13 | set(CCD_VERSION ${CCD_VERSION_MAJOR}.${CCD_VERSION_MINOR}) 14 | 15 | set(CCD_SOVERSION 2) 16 | 17 | # Include GNUInstallDirs to get canonical paths 18 | include(GNUInstallDirs) 19 | include(CTest) 20 | 21 | option(BUILD_DOCUMENTATION "Build the documentation" OFF) 22 | 23 | option(BUILD_SHARED_LIBS "Build libccd as a shared library" ON) 24 | 25 | option(ENABLE_DOUBLE_PRECISION 26 | "Enable double precision computations instead of single precision" OFF) 27 | 28 | # Option for some bundle-like build system in order not to expose 29 | # any FCL binary symbols in their public ABI 30 | option(CCD_HIDE_ALL_SYMBOLS "Hide all binary symbols" OFF) 31 | if (CCD_HIDE_ALL_SYMBOLS) 32 | add_definitions("-DCCD_STATIC_DEFINE") 33 | endif() 34 | 35 | # set the default build type 36 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 37 | set(CMAKE_BUILD_TYPE Release CACHE STRING 38 | "Choose the type of build; options are Debug Release RelWithDebInfo MinSizeRel" 39 | FORCE) 40 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY 41 | STRINGS 42 | Debug 43 | Release 44 | RelWithDebInfo 45 | MinSizeRel) 46 | endif() 47 | 48 | add_subdirectory(src) 49 | 50 | if(BUILD_DOCUMENTATION) 51 | add_subdirectory(doc) 52 | endif() 53 | 54 | include(CMakePackageConfigHelpers) 55 | 56 | configure_package_config_file(ccd-config.cmake.in ccd-config.cmake 57 | INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/ccd" 58 | PATH_VARS CMAKE_INSTALL_INCLUDEDIR CMAKE_INSTALL_LIBDIR 59 | NO_CHECK_REQUIRED_COMPONENTS_MACRO) 60 | 61 | write_basic_package_version_file(ccd-config-version.cmake 62 | VERSION ${CCD_VERSION} COMPATIBILITY AnyNewerVersion) 63 | 64 | install(FILES 65 | "${CMAKE_BINARY_DIR}/ccd-config.cmake" 66 | "${CMAKE_BINARY_DIR}/ccd-config-version.cmake" 67 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/ccd") 68 | 69 | set(CCD_PKGCONFIG_DESCRIPTION 70 | "Library for collision detection between convex shapes") 71 | configure_file(ccd.pc.in ccd.pc @ONLY) 72 | install(FILES "${CMAKE_BINARY_DIR}/ccd.pc" 73 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 74 | 75 | install(FILES BSD-LICENSE DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/ccd") 76 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = src 2 | 3 | EXTRA_DIST = doc \ 4 | BSD-LICENSE \ 5 | README.md \ 6 | make-release.sh 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libccd [![Build Status](https://travis-ci.org/danfis/libccd.svg?branch=master)](https://travis-ci.org/danfis/libccd) 2 | 3 | ***libccd*** is library for a collision detection between two convex shapes. 4 | libccd implements variation on Gilbert–Johnson–Keerthi algorithm plus Expand 5 | Polytope Algorithm (EPA) and also implements algorithm Minkowski Portal 6 | Refinement (MPR, a.k.a. XenoCollide) as described in Game Programming Gems 7. 7 | 8 | libccd is the only available open source library of my knowledge that include 9 | MPR algorithm working in 3-D space. However, there is a library called 10 | [mpr2d](http://code.google.com/p/mpr2d/), implemented in D programming 11 | language, that works in 2-D space. 12 | 13 | libccd is currently part of: 14 | 15 | 1. [ODE](http://www.ode.org/) library (see ODE's *./configure --help* how to enable it), 16 | 2. [FCL](http://www.ros.org/wiki/fcl) library from [Willow Garage](http://www.willowgarage.com/), 17 | 3. [Bullet3](http://bulletphysics.org/) library (https://github.com/bulletphysics/bullet3). 18 | 19 | For implementation details on GJK algorithm, see 20 | http://www.win.tue.nl/~gino/solid/jgt98convex.pdf. 21 | 22 | 23 | ## Dependencies 24 | 25 | This library is currently based only on standard libraries. 26 | The only exception are testsuites that are built on top of CU 27 | (https://github.com/danfis/cu) library licensed under LGPL, however only 28 | testing depends on it and libccd library itself can be distributed without it. 29 | 30 | 31 | ## License 32 | 33 | libccd is licensed under OSI-approved 3-clause BSD License, text of license 34 | is distributed along with source code in BSD-LICENSE file. 35 | Each file should include license notice, the rest should be considered as 36 | licensed under 3-clause BSD License. 37 | 38 | 39 | ## Compile And Install 40 | 41 | libccd contains several mechanisms for compiling and installing. Using a simple Makefile, using autotools, and using CMake. 42 | 43 | ### 1. Using Makefile 44 | 45 | Directory src/ contains Makefile that should contain everything needed for compilation and installation: 46 | ```sh 47 | $ cd src/ 48 | $ make 49 | $ make install 50 | ``` 51 | 52 | Library libccd is by default compiled in double precision of floating point numbers - you can change this by options *USE_SINGLE/USE_DOUBLE*, i.e.: 53 | ```sh 54 | $ make USE_SINGLE=yes 55 | ``` 56 | will compile library in single precision. 57 | 58 | Installation directory can be changed by options PREFIX, INCLUDEDIR and LIBDIR. 59 | For more info type 'make help'. 60 | 61 | ### 2. Using Autotools 62 | 63 | libccd also contains support for autotools: 64 | Generate configure script etc.: 65 | ```sh 66 | $ ./bootstrap 67 | ``` 68 | 69 | Create new build/ directory: 70 | ```sh 71 | $ mkdir build && cd build 72 | ``` 73 | 74 | Run configure script: 75 | ```sh 76 | $ ../configure 77 | ``` 78 | 79 | Run make and make install: 80 | ```sh 81 | $ make && make install 82 | ``` 83 | 84 | configure script can change the way libccd is compiled and installed, most significant option is *--enable-double-precision* which enables double precision (single is default in this case). 85 | 86 | ### 3. Using CMake 87 | 88 | To build using `make`: 89 | ```sh 90 | $ mkdir build && cd build 91 | $ cmake -G "Unix Makefiles" .. 92 | $ make && make install 93 | ``` 94 | 95 | To build using `ninja`: 96 | ```sh 97 | $ mkdir build && cd build 98 | $ cmake -G Ninja .. 99 | $ ninja && ninja install 100 | ``` 101 | 102 | Other build tools may be using by specifying a different generator. For example: 103 | ```sh 104 | $ cmake -G Xcode .. 105 | ``` 106 | 107 | ```bat 108 | > cmake -G "Visual Studio 14 2015" .. 109 | ``` 110 | 111 | To compile using double precision, set the `ENABLE_DOUBLE_PRECISION` option: 112 | ```sh 113 | $ mkdir build && cd build 114 | $ cmake -G "Unix Makefiles" -DENABLE_DOUBLE_PRECISION=ON .. 115 | $ make && make install 116 | ``` 117 | 118 | To build libccd as a shared library, set the `BUILD_SHARED_LIBS` option: 119 | ```sh 120 | $ mkdir build && cd build 121 | $ cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON .. 122 | $ make && make install 123 | ``` 124 | 125 | To build the test suite, set the `BUILD_TESTING` option: 126 | ```sh 127 | $ mkdir build && cd build 128 | $ cmake -G "Unix Makefiles" -DBUILD_TESTING=ON .. 129 | $ make && make test 130 | ``` 131 | 132 | The installation directory may be changed using the `CMAKE_INSTALL_PREFIX` variable: 133 | ```sh 134 | $ mkdir build && cd build 135 | $ cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/path/to/install .. 136 | $ make && make install 137 | ``` 138 | 139 | ## GJK - Intersection Test 140 | 141 | This section describes how to use libccd for testing if two convex objects intersects (i.e., 'yes/no' test) using Gilbert-Johnson-Keerthi (GJK) algorithm. 142 | 143 | Procedure is very simple (and is similar for usages of library): 144 | 145 | 1. Include ** file. 146 | 2. Implement support function for specific shapes. Support function is function that returns furthest point from object (shape) in specified direction. 147 | 3. Set up *ccd_t* structure. 148 | 4. Run ccdGJKIntersect() function on desired objects. 149 | 150 | Here is skeleton of simple program: 151 | ```cpp 152 | #include 153 | #include // for work with quaternions 154 | 155 | /** Support function for box */ 156 | void support(const void *obj, const ccd_vec3_t *dir, ccd_vec3_t *vec) 157 | { 158 | // assume that obj_t is user-defined structure that holds info about 159 | // object (in this case box: x, y, z, pos, quat - dimensions of box, 160 | // position and rotation) 161 | obj_t *obj = (obj_t *)_obj; 162 | ccd_vec3_t dir; 163 | ccd_quat_t qinv; 164 | 165 | // apply rotation on direction vector 166 | ccdVec3Copy(&dir, _dir); 167 | ccdQuatInvert2(&qinv, &obj->quat); 168 | ccdQuatRotVec(&dir, &qinv); 169 | 170 | // compute support point in specified direction 171 | ccdVec3Set(v, ccdSign(ccdVec3X(&dir)) * box->x * CCD_REAL(0.5), 172 | ccdSign(ccdVec3Y(&dir)) * box->y * CCD_REAL(0.5), 173 | ccdSign(ccdVec3Z(&dir)) * box->z * CCD_REAL(0.5)); 174 | 175 | // transform support point according to position and rotation of object 176 | ccdQuatRotVec(v, &obj->quat); 177 | ccdVec3Add(v, &obj->pos); 178 | } 179 | 180 | int main(int argc, char *argv[]) 181 | { 182 | ... 183 | 184 | ccd_t ccd; 185 | CCD_INIT(&ccd); // initialize ccd_t struct 186 | 187 | // set up ccd_t struct 188 | ccd.support1 = support; // support function for first object 189 | ccd.support2 = support; // support function for second object 190 | ccd.max_iterations = 100; // maximal number of iterations 191 | 192 | int intersect = ccdGJKIntersect(obj1, obj2, &ccd); 193 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 194 | } 195 | ``` 196 | 197 | ## GJK + EPA - Penetration Of Two Objects 198 | 199 | If you want to obtain also penetration info about two intersection objects ccdGJKPenetration() function can be used. 200 | 201 | Procedure is almost same as for previous case: 202 | ```cpp 203 | #include 204 | #include // for work with quaternions 205 | 206 | /** Support function is same as in previous case */ 207 | 208 | int main(int argc, char *argv[]) 209 | { 210 | ... 211 | ccd_t ccd; 212 | CCD_INIT(&ccd); // initialize ccd_t struct 213 | 214 | // set up ccd_t struct 215 | ccd.support1 = support; // support function for first object 216 | ccd.support2 = support; // support function for second object 217 | ccd.max_iterations = 100; // maximal number of iterations 218 | ccd.epa_tolerance = 0.0001; // maximal tolerance fro EPA part 219 | 220 | ccd_real_t depth; 221 | ccd_vec3_t dir, pos; 222 | int intersect = ccdGJKPenetration(obj1, obj2, &ccd, &depth, &dir, &pos); 223 | // now intersect holds 0 if obj1 and obj2 intersect, -1 otherwise 224 | // in depth, dir and pos is stored penetration depth, direction of 225 | // separation vector and position in global coordinate system 226 | } 227 | ``` 228 | 229 | ## MPR - Intersection Test 230 | 231 | libccd also provides MPR - Minkowski Portal Refinement algorithm that can be used for testing if two objects intersects. 232 | 233 | Procedure is similar to the one used for GJK algorithm. Support function is same but also function that returns center (or any point near center) of given object must be implemented: 234 | ```cpp 235 | #include 236 | #include // for work with quaternions 237 | 238 | /** Support function is same as in previous case */ 239 | 240 | /** Center function - returns center of object */ 241 | void center(const void *_obj, ccd_vec3_t *center) 242 | { 243 | obj_t *obj = (obj_t *)_obj; 244 | ccdVec3Copy(center, &obj->pos); 245 | } 246 | 247 | int main(int argc, char *argv[]) 248 | { 249 | ... 250 | ccd_t ccd; 251 | CCD_INIT(&ccd); // initialize ccd_t struct 252 | 253 | // set up ccd_t struct 254 | ccd.support1 = support; // support function for first object 255 | ccd.support2 = support; // support function for second object 256 | ccd.center1 = center; // center function for first object 257 | ccd.center2 = center; // center function for second object 258 | ccd.mpr_tolerance = 0.0001; // maximal tolerance 259 | 260 | int intersect = ccdMPRIntersect(obj1, obj2, &ccd); 261 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 262 | } 263 | ``` 264 | 265 | 266 | ## MPR - Penetration Of Two Objects 267 | 268 | Using MPR algorithm for obtaining penetration info about two intersection objects is equally easy as in previous case instead ccdMPRPenetration() function is used: 269 | ```cpp 270 | #include 271 | #include // for work with quaternions 272 | 273 | /** Support function is same as in previous case */ 274 | /** Center function is same as in prevous case */ 275 | 276 | int main(int argc, char *argv[]) 277 | { 278 | ... 279 | ccd_t ccd; 280 | CCD_INIT(&ccd); // initialize ccd_t struct 281 | 282 | // set up ccd_t struct 283 | ccd.support1 = support; // support function for first object 284 | ccd.support2 = support; // support function for second object 285 | ccd.center1 = center; // center function for first object 286 | ccd.center2 = center; // center function for second object 287 | ccd.mpr_tolerance = 0.0001; // maximal tolerance 288 | 289 | ccd_real_t depth; 290 | ccd_vec3_t dir, pos; 291 | int intersect = ccdMPRPenetration(obj1, obj2, &ccd, &depth, &dir, &pos); 292 | // now intersect holds 0 if obj1 and obj2 intersect, -1 otherwise 293 | // in depth, dir and pos is stored penetration depth, direction of 294 | // separation vector and position in global coordinate system 295 | } 296 | ``` 297 | -------------------------------------------------------------------------------- /bootstrap: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | libtoolize -f -c 4 | aclocal 5 | autoheader -f 6 | autoconf 7 | automake -a --foreign -f -c 8 | -------------------------------------------------------------------------------- /ccd-config.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ 2 | 3 | set(CCD_VERSION_MAJOR @CCD_VERSION_MAJOR@) 4 | set(CCD_VERSION_MINOR @CCD_VERSION_MINOR@) 5 | set(CCD_VERSION @CCD_VERSION@) 6 | 7 | set(CCD_SOVERSION @CCD_SOVERSION@) 8 | 9 | set(CCD_FOUND ON) 10 | set_and_check(CCD_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@") 11 | set_and_check(CCD_LIBRARY_DIRS "@PACKAGE_CMAKE_INSTALL_LIBDIR@") 12 | set(CCD_LIBRARIES ccd) 13 | 14 | include("${CMAKE_CURRENT_LIST_DIR}/ccd-targets.cmake") 15 | -------------------------------------------------------------------------------- /ccd.pc.in: -------------------------------------------------------------------------------- 1 | # Generated by CMake @CMAKE_VERSION@ for ccd 2 | 3 | prefix=@CMAKE_INSTALL_PREFIX@ 4 | exec_prefix=${prefix} 5 | libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ 6 | includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ 7 | 8 | Name: @PROJECT_NAME@ 9 | Description: @CCD_PKGCONFIG_DESCRIPTION@ 10 | Version: @CCD_VERSION@ 11 | Requires: @CCD_PKGCONFIG_REQUIRES@ 12 | Libs: -L${libdir} -lccd @CCD_PKGCONFIG_EXTRA_LIBS@ 13 | Cflags: -I${includedir} 14 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | #AC_PREREQ([2.65]) 5 | AC_INIT([libccd], [2.0], [danfis@danfis.cz]) 6 | AC_CONFIG_SRCDIR([src/ccd.c]) 7 | AC_CONFIG_HEADERS([src/ccd/config.h]) 8 | AM_INIT_AUTOMAKE 9 | 10 | # Checks for programs. 11 | AC_PROG_CXX 12 | AC_PROG_CC 13 | AC_PROG_INSTALL 14 | AC_DISABLE_SHARED 15 | LT_INIT 16 | 17 | # Checks for libraries. 18 | AC_CHECK_LIB([m], [main]) 19 | # FIXME: Replace `main' with a function in `-lrt': 20 | AC_CHECK_LIB([rt], [main]) 21 | 22 | # Checks for header files. 23 | AC_CHECK_HEADERS([float.h stdlib.h string.h unistd.h]) 24 | 25 | # Checks for typedefs, structures, and compiler characteristics. 26 | AC_TYPE_SIZE_T 27 | 28 | # Checks for library functions. 29 | AC_FUNC_FORK 30 | AC_FUNC_REALLOC 31 | AC_CHECK_FUNCS([clock_gettime]) 32 | 33 | use_double=no 34 | AC_ARG_ENABLE(double-precision, 35 | AS_HELP_STRING([--enable-double-precision], 36 | [enable double precision computations instead of single precision]), 37 | [use_double=yes]) 38 | if test $use_double = no 39 | then 40 | AC_DEFINE([CCD_SINGLE], [], [use single precision]) 41 | else 42 | AC_DEFINE([CCD_DOUBLE], [], [use double precision]) 43 | fi 44 | 45 | 46 | AC_CONFIG_FILES([Makefile 47 | src/Makefile 48 | src/testsuites/Makefile 49 | src/testsuites/cu/Makefile]) 50 | AC_OUTPUT 51 | -------------------------------------------------------------------------------- /doc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_program(SPHINX_EXECUTABLE NAMES sphinx-build sphinx-build2) 2 | 3 | if(NOT SPHINX_EXECUTABLE) 4 | message(FATAL_ERROR "Could NOT find required executable sphinx-build") 5 | endif() 6 | 7 | add_custom_target(doc ALL) 8 | 9 | set(CCD_DOCTREE_DIR "${CMAKE_CURRENT_BINARY_DIR}/.doctrees") 10 | set(CCD_HTML_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/html") 11 | 12 | add_custom_target(html COMMAND 13 | "${SPHINX_EXECUTABLE}" -b html -d "${CCD_DOCTREE_DIR}" -q 14 | "${CMAKE_CURRENT_SOURCE_DIR}" "${CCD_HTML_OUTPUT_DIR}") 15 | add_dependencies(doc html) 16 | 17 | install(DIRECTORY "${CCD_HTML_OUTPUT_DIR}" 18 | DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/ccd") 19 | 20 | set(CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES 21 | "${CCD_DOCTREE_DIR}" 22 | "${CCD_HTML_OUTPUT_DIR}") 23 | 24 | if(NOT WIN32) 25 | set(CCD_MAN_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/man") 26 | 27 | add_custom_target(man COMMAND 28 | "${SPHINX_EXECUTABLE}" -b man -d "${CCD_DOCTREE_DIR}" -q 29 | "${CMAKE_CURRENT_SOURCE_DIR}" "${CCD_MAN_OUTPUT_DIR}") 30 | add_dependencies(doc man) 31 | 32 | install(DIRECTORY "${CCD_MAN_OUTPUT_DIR}/" 33 | DESTINATION "${CMAKE_INSTALL_MANDIR}/man1") 34 | 35 | list(APPEND CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES "${CCD_MAN_OUTPUT_DIR}") 36 | endif() 37 | 38 | set_directory_properties(PROPERTIES 39 | ADDITIONAL_MAKE_CLEAN_FILES ${CCD_DOC_ADDITIONAL_MAKE_CLEAN_FILES}) 40 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 16 | 17 | .PHONY: help 18 | help: 19 | @echo "Please use \`make ' where is one of" 20 | @echo " html to make standalone HTML files" 21 | @echo " dirhtml to make HTML files named index.html in directories" 22 | @echo " singlehtml to make a single large HTML file" 23 | @echo " pickle to make pickle files" 24 | @echo " json to make JSON files" 25 | @echo " htmlhelp to make HTML files and a HTML help project" 26 | @echo " qthelp to make HTML files and a qthelp project" 27 | @echo " applehelp to make an Apple Help Book" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " epub3 to make an epub3" 31 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 32 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 33 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 34 | @echo " text to make text files" 35 | @echo " man to make manual pages" 36 | @echo " texinfo to make Texinfo files" 37 | @echo " info to make Texinfo files and run them through makeinfo" 38 | @echo " gettext to make PO message catalogs" 39 | @echo " changes to make an overview of all changed/added/deprecated items" 40 | @echo " xml to make Docutils-native XML files" 41 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 42 | @echo " linkcheck to check all external links for integrity" 43 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 44 | @echo " coverage to run coverage check of the documentation (if enabled)" 45 | @echo " dummy to check syntax errors of document sources" 46 | 47 | .PHONY: clean 48 | clean: 49 | rm -rf $(BUILDDIR)/* 50 | 51 | .PHONY: html 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | .PHONY: dirhtml 58 | dirhtml: 59 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 60 | @echo 61 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 62 | 63 | .PHONY: singlehtml 64 | singlehtml: 65 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 66 | @echo 67 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 68 | 69 | .PHONY: pickle 70 | pickle: 71 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 72 | @echo 73 | @echo "Build finished; now you can process the pickle files." 74 | 75 | .PHONY: json 76 | json: 77 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 78 | @echo 79 | @echo "Build finished; now you can process the JSON files." 80 | 81 | .PHONY: htmlhelp 82 | htmlhelp: 83 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 84 | @echo 85 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 86 | ".hhp project file in $(BUILDDIR)/htmlhelp." 87 | 88 | .PHONY: qthelp 89 | qthelp: 90 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 91 | @echo 92 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 93 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 94 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/libccd.qhcp" 95 | @echo "To view the help file:" 96 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/libccd.qhc" 97 | 98 | .PHONY: applehelp 99 | applehelp: 100 | $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp 101 | @echo 102 | @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." 103 | @echo "N.B. You won't be able to view it unless you put it in" \ 104 | "~/Library/Documentation/Help or install it in your application" \ 105 | "bundle." 106 | 107 | .PHONY: devhelp 108 | devhelp: 109 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 110 | @echo 111 | @echo "Build finished." 112 | @echo "To view the help file:" 113 | @echo "# mkdir -p $$HOME/.local/share/devhelp/libccd" 114 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/libccd" 115 | @echo "# devhelp" 116 | 117 | .PHONY: epub 118 | epub: 119 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 120 | @echo 121 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 122 | 123 | .PHONY: epub3 124 | epub3: 125 | $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 126 | @echo 127 | @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." 128 | 129 | .PHONY: latex 130 | latex: 131 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 132 | @echo 133 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 134 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 135 | "(use \`make latexpdf' here to do that automatically)." 136 | 137 | .PHONY: latexpdf 138 | latexpdf: 139 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 140 | @echo "Running LaTeX files through pdflatex..." 141 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 142 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 143 | 144 | .PHONY: latexpdfja 145 | latexpdfja: 146 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 147 | @echo "Running LaTeX files through platex and dvipdfmx..." 148 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 149 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 150 | 151 | .PHONY: text 152 | text: 153 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 154 | @echo 155 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 156 | 157 | .PHONY: man 158 | man: 159 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 160 | @echo 161 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 162 | 163 | .PHONY: texinfo 164 | texinfo: 165 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 166 | @echo 167 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 168 | @echo "Run \`make' in that directory to run these through makeinfo" \ 169 | "(use \`make info' here to do that automatically)." 170 | 171 | .PHONY: info 172 | info: 173 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 174 | @echo "Running Texinfo files through makeinfo..." 175 | make -C $(BUILDDIR)/texinfo info 176 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 177 | 178 | .PHONY: gettext 179 | gettext: 180 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 181 | @echo 182 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 183 | 184 | .PHONY: changes 185 | changes: 186 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 187 | @echo 188 | @echo "The overview file is in $(BUILDDIR)/changes." 189 | 190 | .PHONY: linkcheck 191 | linkcheck: 192 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 193 | @echo 194 | @echo "Link check complete; look for any errors in the above output " \ 195 | "or in $(BUILDDIR)/linkcheck/output.txt." 196 | 197 | .PHONY: doctest 198 | doctest: 199 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 200 | @echo "Testing of doctests in the sources finished, look at the " \ 201 | "results in $(BUILDDIR)/doctest/output.txt." 202 | 203 | .PHONY: coverage 204 | coverage: 205 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 206 | @echo "Testing of coverage in the sources finished, look at the " \ 207 | "results in $(BUILDDIR)/coverage/python.txt." 208 | 209 | .PHONY: xml 210 | xml: 211 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 212 | @echo 213 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 214 | 215 | .PHONY: pseudoxml 216 | pseudoxml: 217 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 218 | @echo 219 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 220 | 221 | .PHONY: dummy 222 | dummy: 223 | $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy 224 | @echo 225 | @echo "Build finished. Dummy builder generates no files." 226 | -------------------------------------------------------------------------------- /doc/_build/.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/doc/_build/.dir -------------------------------------------------------------------------------- /doc/_static/.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/doc/_static/.dir -------------------------------------------------------------------------------- /doc/_templates/.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/doc/_templates/.dir -------------------------------------------------------------------------------- /doc/compile-and-install.rst: -------------------------------------------------------------------------------- 1 | Compile And Install 2 | ==================== 3 | 4 | libccd contains several mechanisms for compiling and installing. 5 | Using a simple Makefile, using autotools, and using CMake. 6 | 7 | 8 | 1. Using Makefile 9 | ------------------ 10 | Directory ``src/`` contains Makefile that should contain everything needed for compilation and installation: 11 | 12 | .. code-block:: bash 13 | 14 | $ cd src/ 15 | $ make 16 | $ make install 17 | 18 | Library libccd is by default compiled in double precision of floating point 19 | numbers - you can change this by options ``USE_SINGLE``/``USE_DOUBLE``, i.e.: 20 | 21 | .. code-block:: bash 22 | 23 | $ make USE_SINGLE=yes 24 | 25 | will compile library in single precision. 26 | Installation directory can be changed by options ``PREFIX``, ``INCLUDEDIR`` 27 | and ``LIBDIR``. 28 | For more info type '``make help``'. 29 | 30 | 31 | 2. Using Autotools 32 | ------------------- 33 | libccd also contains support for autotools: 34 | Generate configure script etc.: 35 | 36 | .. code-block:: bash 37 | 38 | $ ./bootstrap 39 | 40 | Create new ``build/`` directory: 41 | 42 | .. code-block:: bash 43 | 44 | $ mkdir build && cd build 45 | 46 | Run configure script: 47 | 48 | .. code-block:: bash 49 | 50 | $ ../configure 51 | 52 | Run make and make install: 53 | 54 | .. code-block:: bash 55 | 56 | $ make && make install 57 | 58 | configure script can change the way libccd is compiled and installed, most 59 | significant option is ``--enable-double-precision`` which enables double 60 | precision (single is default in this case). 61 | 62 | 3. Using CMake 63 | --------------- 64 | 65 | To build using ``make``: 66 | 67 | .. code-block:: bash 68 | 69 | $ mkdir build && cd build 70 | $ cmake -G "Unix Makefiles" .. 71 | $ make && make install 72 | 73 | To build using ``ninja``: 74 | 75 | .. code-block:: bash 76 | 77 | $ mkdir build && cd build 78 | $ cmake -G Ninja .. 79 | $ ninja && ninja install 80 | 81 | Other build tools may be using by specifying a different generator. For example: 82 | 83 | .. code-block:: bash 84 | 85 | $ cmake -G Xcode .. 86 | 87 | .. code-block:: batch 88 | 89 | > cmake -G "Visual Studio 14 2015" .. 90 | 91 | To compile using double precision, set the ``ENABLE_DOUBLE_PRECISION`` option: 92 | 93 | .. code-block:: bash 94 | 95 | $ mkdir build && cd build 96 | $ cmake -G "Unix Makefiles" -DENABLE_DOUBLE_PRECISION=ON .. 97 | $ make && make install 98 | 99 | To build libccd as a shared library, set the ``BUILD_SHARED_LIBS`` option: 100 | 101 | .. code-block:: bash 102 | 103 | $ mkdir build && cd build 104 | $ cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=ON .. 105 | $ make && make install 106 | 107 | To build the test suite, set the ``BUILD_TESTING`` option: 108 | 109 | .. code-block:: bash 110 | 111 | $ mkdir build && cd build 112 | $ cmake -G "Unix Makefiles" -DBUILD_TESTING=ON .. 113 | $ make && make test 114 | 115 | 116 | The installation directory may be changed by specifying the ``CMAKE_INSTALL_PREFIX`` variable: 117 | 118 | .. code-block:: bash 119 | 120 | $ mkdir build && cd build 121 | $ cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/path/to/install .. 122 | $ make && make install 123 | -------------------------------------------------------------------------------- /doc/examples.rst: -------------------------------------------------------------------------------- 1 | Example of Usage 2 | ================= 3 | 4 | 1. GJK - Intersection Test 5 | --------------------------- 6 | This section describes how to use **libccd** for testing if two convex objects intersects (i.e., 'yes/no' test) using Gilbert-Johnson-Keerthi (GJK) algorithm. 7 | 8 | Procedure is very simple (and similar to the usage of the rest of the 9 | library): 10 | 11 | #. Include ```` file. 12 | #. Implement support function for specific shapes. Support function is 13 | function that returns furthest point from object (shape) in specified 14 | direction. 15 | #. Set up ``ccd_t`` structure. 16 | #. Run ``ccdGJKIntersect()`` function on desired objects. 17 | 18 | 19 | Here is a skeleton of simple program: 20 | 21 | .. code-block:: c 22 | 23 | #include 24 | #include // for work with quaternions 25 | 26 | /** Support function for box */ 27 | void support(const void *obj, const ccd_vec3_t *dir, 28 | ccd_vec3_t *vec) 29 | { 30 | // assume that obj_t is user-defined structure that holds info about 31 | // object (in this case box: x, y, z, pos, quat - dimensions of box, 32 | // position and rotation) 33 | obj_t *obj = (obj_t *)_obj; 34 | ccd_vec3_t dir; 35 | ccd_quat_t qinv; 36 | 37 | // apply rotation on direction vector 38 | ccdVec3Copy(&dir, _dir); 39 | ccdQuatInvert2(&qinv, &obj->quat); 40 | ccdQuatRotVec(&dir, &qinv); 41 | 42 | // compute support point in specified direction 43 | ccdVec3Set(v, ccdSign(ccdVec3X(&dir)) * box->x * CCD_REAL(0.5), 44 | ccdSign(ccdVec3Y(&dir)) * box->y * CCD_REAL(0.5), 45 | ccdSign(ccdVec3Z(&dir)) * box->z * CCD_REAL(0.5)); 46 | 47 | // transform support point according to position and rotation of object 48 | ccdQuatRotVec(v, &obj->quat); 49 | ccdVec3Add(v, &obj->pos); 50 | } 51 | 52 | 53 | int main(int argc, char *argv[]) 54 | { 55 | ... 56 | 57 | ccd_t ccd; 58 | CCD_INIT(&ccd); // initialize ccd_t struct 59 | 60 | // set up ccd_t struct 61 | ccd.support1 = support; // support function for first object 62 | ccd.support2 = support; // support function for second object 63 | ccd.max_iterations = 100; // maximal number of iterations 64 | 65 | int intersect = ccdGJKIntersect(obj1, obj2, &ccd); 66 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 67 | } 68 | 69 | 70 | 71 | 72 | 2. GJK + EPA - Penetration Of Two Objects 73 | ------------------------------------------ 74 | 75 | If you want to obtain also penetration info about two intersection objects 76 | ``ccdGJKPenetration()`` function can be used. 77 | 78 | Procedure is almost the same as for the previous case: 79 | 80 | .. code-block:: c 81 | 82 | #include 83 | #include // for work with quaternions 84 | 85 | /** Support function is same as in previous case */ 86 | 87 | int main(int argc, char *argv[]) 88 | { 89 | ... 90 | ccd_t ccd; 91 | CCD_INIT(&ccd); // initialize ccd_t struct 92 | 93 | // set up ccd_t struct 94 | ccd.support1 = support; // support function for first object 95 | ccd.support2 = support; // support function for second object 96 | ccd.max_iterations = 100; // maximal number of iterations 97 | ccd.epa_tolerance = 0.0001; // maximal tolerance fro EPA part 98 | 99 | ccd_real_t depth; 100 | ccd_vec3_t dir, pos; 101 | int intersect = ccdGJKPenetration(obj1, obj2, &ccd, &depth, &dir, &pos); 102 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 103 | // in depth, dir and pos is stored penetration depth, direction of 104 | // separation vector and position in global coordinate system 105 | } 106 | 107 | 108 | 3. MPR - Intersection Test 109 | --------------------------- 110 | 111 | **libccd** also provides *MPR* - Minkowski Portal Refinement algorithm that 112 | can be used for testing if two objects intersects. 113 | 114 | Procedure is similar to the one used for GJK algorithm. Support function is 115 | the same but also function that returns a center (or any point near center) 116 | of a given object must be implemented: 117 | 118 | .. code-block:: c 119 | 120 | #include 121 | #include // for work with quaternions 122 | 123 | /** Support function is same as in previous case */ 124 | 125 | /** Center function - returns center of object */ 126 | void center(const void *_obj, ccd_vec3_t *center) 127 | { 128 | obj_t *obj = (obj_t *)_obj; 129 | ccdVec3Copy(center, &obj->pos); 130 | } 131 | 132 | int main(int argc, char *argv[]) 133 | { 134 | ... 135 | ccd_t ccd; 136 | CCD_INIT(&ccd); // initialize ccd_t struct 137 | 138 | // set up ccd_t struct 139 | ccd.support1 = support; // support function for first object 140 | ccd.support2 = support; // support function for second object 141 | ccd.center1 = center; // center function for first object 142 | ccd.center2 = center; // center function for second object 143 | ccd.mpr_tolerance = 0.0001; // maximal tolerance 144 | 145 | int intersect = ccdMPRIntersect(obj1, obj2, &ccd); 146 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 147 | } 148 | 149 | 150 | 4. MPR - Penetration Of Two Objects 151 | ------------------------------------ 152 | 153 | Using MPR algorithm for obtaining penetration info about two intersection 154 | objects is equally easy as in the previous case instead but 155 | ``ccdMPRPenetration()`` function is used: 156 | 157 | .. code-block:: c 158 | 159 | #include 160 | #include // for work with quaternions 161 | 162 | /** Support function is same as in previous case */ 163 | /** Center function is same as in prevous case */ 164 | 165 | int main(int argc, char *argv[]) 166 | { 167 | ... 168 | ccd_t ccd; 169 | CCD_INIT(&ccd); // initialize ccd_t struct 170 | 171 | // set up ccd_t struct 172 | ccd.support1 = support; // support function for first object 173 | ccd.support2 = support; // support function for second object 174 | ccd.center1 = center; // center function for first object 175 | ccd.center2 = center; // center function for second object 176 | ccd.mpr_tolerance = 0.0001; // maximal tolerance 177 | 178 | ccd_real_t depth; 179 | ccd_vec3_t dir, pos; 180 | int intersect = ccdMPRPenetration(obj1, obj2, &ccd, &depth, &dir, &pos); 181 | // now intersect holds true if obj1 and obj2 intersect, false otherwise 182 | // in depth, dir and pos is stored penetration depth, direction of 183 | // separation vector and position in global coordinate system 184 | } 185 | 186 | -------------------------------------------------------------------------------- /doc/index.rst: -------------------------------------------------------------------------------- 1 | .. libccd documentation master file, created by 2 | sphinx-quickstart2 on Thu May 23 13:49:12 2013. 3 | 4 | libccd's documentation 5 | ======================= 6 | 7 | See homepage: http://libccd.danfis.cz 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | compile-and-install.rst 15 | examples.rst 16 | reference.rst 17 | 18 | 19 | .. Indices and tables 20 | .. ================== 21 | 22 | .. * :ref:`genindex` 23 | .. * :ref:`modindex` 24 | .. * :ref:`search` 25 | 26 | -------------------------------------------------------------------------------- /doc/reference.rst: -------------------------------------------------------------------------------- 1 | Reference 2 | ========== 3 | 4 | .. literalinclude:: ../src/ccd/ccd.h 5 | :language: c 6 | :linenos: 7 | -------------------------------------------------------------------------------- /make-release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Creates .tar.gz package of specified version. 4 | # Takes one argument - identification of commit 5 | 6 | NAME=libccd 7 | COMMIT="" 8 | CMD="git archive" 9 | 10 | # read arguments 11 | COMMIT="$1" 12 | 13 | if [ "$COMMIT" = "" ]; then 14 | echo "Usage: $0 commit [--notest] [--nodoc]" 15 | echo "Error: you must specify commit which should be packed" 16 | exit -1; 17 | fi; 18 | 19 | 20 | PREFIX=${NAME}-$COMMIT/ 21 | FN=${NAME}-$COMMIT.tar.gz 22 | 23 | if echo "$COMMIT" | grep '^v[0-9]\.[0-9]\+' >/dev/null 2>&1; then 24 | tmp=$(echo "$COMMIT" | sed 's/^v//') 25 | PREFIX=${NAME}-$tmp/ 26 | FN=${NAME}-$tmp.tar.gz 27 | fi 28 | 29 | $CMD --prefix="$PREFIX" --format=tar $COMMIT | gzip >"$FN" 30 | echo "Package: $FN" 31 | 32 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.a 3 | ccd/config.h 4 | ccd/config.h.in 5 | 6 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if(DEFINED CCD_SINGLE OR DEFINED CCD_DOUBLE) 2 | # make sure only DOUBLE or SINGLE is set; default to SINGLE 3 | if(CCD_SINGLE) 4 | set(CCD_DOUBLE OFF) 5 | else() 6 | set(CCD_SINGLE ON) 7 | endif() 8 | if(CCD_DOUBLE) 9 | set(CCD_SINGLE OFF) 10 | endif() 11 | elseif(ENABLE_DOUBLE_PRECISION) 12 | set(CCD_DOUBLE ON) 13 | set(CCD_SINGLE OFF) 14 | else() 15 | set(CCD_DOUBLE OFF) 16 | set(CCD_SINGLE ON) 17 | endif() 18 | 19 | configure_file(ccd/config.h.cmake.in ccd/config.h) 20 | 21 | set(CCD_INCLUDES 22 | ccd/ccd.h 23 | ccd/compiler.h 24 | ccd/ccd_export.h 25 | ccd/quat.h 26 | ccd/vec3.h 27 | "${CMAKE_CURRENT_BINARY_DIR}/ccd/config.h") 28 | 29 | set(CCD_SOURCES 30 | alloc.h 31 | ccd.c 32 | dbg.h 33 | list.h 34 | mpr.c 35 | polytope.c 36 | polytope.h 37 | simplex.h 38 | support.c 39 | support.h 40 | vec3.c) 41 | 42 | add_library(ccd ${CCD_INCLUDES} ${CCD_SOURCES}) 43 | set_target_properties(ccd PROPERTIES 44 | PUBLIC_HEADER "${CCD_INCLUDES}" 45 | SOVERSION ${CCD_SOVERSION} 46 | VERSION ${CCD_VERSION}) 47 | target_include_directories(ccd PUBLIC 48 | $ 49 | $) 50 | 51 | if(NOT WIN32) 52 | find_library(LIBM_LIBRARY NAMES m) 53 | if(NOT LIBM_LIBRARY) 54 | message(FATAL_ERROR "Could NOT find required library LibM") 55 | endif() 56 | target_link_libraries(ccd "${LIBM_LIBRARY}") 57 | if(BUILD_SHARED_LIBS) 58 | set(CCD_PKGCONFIG_EXTRA_LIBS -lm PARENT_SCOPE) 59 | endif() 60 | endif() 61 | 62 | export(TARGETS ccd FILE "${CMAKE_BINARY_DIR}/ccd-targets.cmake") 63 | 64 | install(TARGETS ccd 65 | EXPORT ccd-targets 66 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" 67 | INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" 68 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 69 | PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/ccd" 70 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") 71 | install(EXPORT ccd-targets DESTINATION "${CMAKE_INSTALL_LIBDIR}/ccd") 72 | 73 | macro (check_compiler_visibility) 74 | include (CheckCXXCompilerFlag) 75 | check_cxx_compiler_flag(-fvisibility=hidden COMPILER_SUPPORTS_VISIBILITY) 76 | endmacro() 77 | 78 | if(UNIX) 79 | check_compiler_visibility() 80 | if (COMPILER_SUPPORTS_VISIBILITY) 81 | set_target_properties(ccd 82 | PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") 83 | endif() 84 | endif() 85 | 86 | if(NOT WIN32 AND BUILD_TESTING AND NOT CCD_HIDE_ALL_SYMBOLS) 87 | add_subdirectory(testsuites) 88 | endif() 89 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | ### 2 | # libccd 3 | # --------------------------------- 4 | # Copyright (c)2010 Daniel Fiser 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 | # . 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 | -include Makefile.include 19 | 20 | CFLAGS += -I. -fvisibility=hidden 21 | 22 | TARGETS = libccd.a 23 | OBJS = ccd.o mpr.o support.o vec3.o polytope.o 24 | 25 | all: $(TARGETS) 26 | 27 | libccd.a: $(OBJS) 28 | ar cr $@ $(OBJS) 29 | ranlib $@ 30 | 31 | ccd/config.h: ccd/config.h.m4 32 | $(M4) $(CONFIG_FLAGS) $< >$@ 33 | 34 | %.o: %.c %.h ccd/config.h 35 | $(CC) $(CFLAGS) $(DEFS) -c -o $@ $< 36 | %.o: %.c ccd/config.h 37 | $(CC) $(CFLAGS) $(DEFS) -c -o $@ $< 38 | %.h: ccd/config.h 39 | %.c: ccd/config.h 40 | 41 | install: 42 | mkdir -p $(PREFIX)/$(INCLUDEDIR)/ccd 43 | mkdir -p $(PREFIX)/$(LIBDIR) 44 | cp ccd/*.h $(PREFIX)/$(INCLUDEDIR)/ccd/ 45 | cp libccd.a $(PREFIX)/$(LIBDIR) 46 | 47 | clean: 48 | rm -f $(OBJS) 49 | rm -f $(TARGETS) 50 | rm -f ccd/config.h 51 | if [ -d testsuites ]; then $(MAKE) -C testsuites clean; fi; 52 | 53 | check: 54 | $(MAKE) -C testsuites check 55 | check-valgrind: 56 | $(MAKE) -C testsuites check-valgrind 57 | 58 | help: 59 | @echo "Targets:" 60 | @echo " all - Build library" 61 | @echo " install - Install library into system" 62 | @echo "" 63 | @echo "Options:" 64 | @echo " CC - Path to C compiler" 65 | @echo " M4 - Path to m4 macro processor" 66 | @echo "" 67 | @echo " DEBUG 'yes'/'no' - Turn on/off debugging (default: 'no')" 68 | @echo " PROFIL 'yes'/'no' - Compiles profiling info (default: 'no')" 69 | @echo " NOWALL 'yes'/'no' - Turns off -Wall gcc option (default: 'no')" 70 | @echo " NOPEDANTIC 'yes'/'no' - Turns off -pedantic gcc option (default: 'no')" 71 | @echo "" 72 | @echo " USE_SINGLE 'yes' - Use single precision (default: 'no')" 73 | @echo " USE_DOUBLE 'yes' - Use double precision (default: 'yes')" 74 | @echo "" 75 | @echo " PREFIX - Prefix where library will be installed (default: /usr/local)" 76 | @echo " INCLUDEDIR - Directory where header files will be installed (PREFIX/INCLUDEDIR) (default: include)" 77 | @echo " LIBDIR - Directory where library will be installed (PREFIX/LIBDIR) (default: lib)" 78 | @echo "" 79 | 80 | .PHONY: all clean check check-valgrind help 81 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = . testsuites 2 | 3 | lib_LTLIBRARIES = libccd.la 4 | 5 | libccd_la_SOURCES = alloc.h \ 6 | ccd/compiler.h \ 7 | dbg.h \ 8 | ccd.c ccd/ccd.h \ 9 | ccd/ccd_export.h \ 10 | list.h \ 11 | polytope.c polytope.h \ 12 | ccd/quat.h \ 13 | simplex.h \ 14 | support.c support.h \ 15 | vec3.c ccd/vec3.h \ 16 | mpr.c 17 | 18 | libccd_la_CFLAGS = -fvisibility=hidden 19 | -------------------------------------------------------------------------------- /src/Makefile.include: -------------------------------------------------------------------------------- 1 | ### 2 | # libccd 3 | # --------------------------------- 4 | # Copyright (c)2010 Daniel Fiser 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 | # . 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 | CC ?= gcc 19 | M4 ?= m4 20 | PYTHON ?= python 21 | 22 | SYSTEM = $(shell uname) 23 | 24 | SYSTEM_CXXFLAGS = 25 | SYSTEM_LDFLAGS = 26 | 27 | ifeq '$(SYSTEM)' 'FreeBSD' 28 | SYSTEM_CXXFLAGS = -Wno-long-long 29 | else 30 | endif 31 | 32 | NOWALL ?= no 33 | NOPEDANTIC ?= no 34 | DEBUG ?= no 35 | PROFIL ?= no 36 | 37 | ifeq '$(PROFIL)' 'yes' 38 | DEBUG = yes 39 | endif 40 | 41 | ifeq '$(DEBUG)' 'yes' 42 | CFLAGS = -g 43 | endif 44 | ifeq '$(PROFIL)' 'yes' 45 | CFLAGS += -pg 46 | endif 47 | 48 | ifneq '$(NOWALL)' 'yes' 49 | CFLAGS += -Wall 50 | endif 51 | ifneq '$(NOPEDANTIC)' 'yes' 52 | CFLAGS += -pedantic 53 | endif 54 | 55 | CONFIG_FLAGS = 56 | USE_DOUBLE ?= yes 57 | USE_SINGLE ?= no 58 | 59 | ifeq '$(USE_SINGLE)' 'yes' 60 | CONFIG_FLAGS += -DUSE_SINGLE 61 | USE_DOUBLE = no 62 | endif 63 | ifeq '$(USE_DOUBLE)' 'yes' 64 | CONFIG_FLAGS += -DUSE_DOUBLE 65 | endif 66 | 67 | CFLAGS += --std=gnu99 68 | LDFLAGS += $(SYSTEM_LDFLAGS) 69 | 70 | CHECKTARGETS = 71 | check-dep: $(CHECKTARGETS) 72 | 73 | PREFIX ?= /usr/local 74 | INCLUDEDIR ?= include 75 | LIBDIR ?= lib 76 | 77 | showvars: 78 | @echo "SYSTEM = "$(SYSTEM) 79 | @echo "" 80 | @echo "CC = $(CC)" 81 | @echo "M4 = $(M4)" 82 | @echo "" 83 | @echo "DEBUG = $(DEBUG)" 84 | @echo "PROFIL = $(PROFIL)" 85 | @echo "NOWALL = $(NOWALL)" 86 | @echo "NOPEDANTIC = $(NOPEDANTIC)" 87 | @echo "USE_SINGLE = $(USE_SINGLE)" 88 | @echo "USE_DOUBLE = $(USE_DOUBLE)" 89 | @echo "" 90 | @echo "CFLAGS = $(CFLAGS)" 91 | @echo "LDFLAGS = $(LDFLAGS)" 92 | @echo "CONFIG_FLAGS = $(CONFIG_FLAGS)" 93 | @echo "" 94 | @echo "PREFIX = $(PREFIX)" 95 | @echo "INCLUDEDIR = $(INCLUDEDIR)" 96 | @echo "LIBDIR = $(LIBDIR)" 97 | @echo "" 98 | 99 | .DEFAULT_GOAL := all 100 | .PHONY: showvars 101 | -------------------------------------------------------------------------------- /src/alloc.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_ALLOC_H__ 19 | #define __CCD_ALLOC_H__ 20 | 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif /* __cplusplus */ 26 | 27 | /** 28 | * Functions and macros required for memory allocation. 29 | */ 30 | 31 | /* Memory allocation: */ 32 | #define __CCD_ALLOC_MEMORY(type, ptr_old, size) \ 33 | (type *)realloc((void *)ptr_old, (size)) 34 | 35 | /** Allocate memory for one element of type. */ 36 | #define CCD_ALLOC(type) \ 37 | __CCD_ALLOC_MEMORY(type, NULL, sizeof(type)) 38 | 39 | /** Allocate memory for array of elements of type type. */ 40 | #define CCD_ALLOC_ARR(type, num_elements) \ 41 | __CCD_ALLOC_MEMORY(type, NULL, sizeof(type) * (num_elements)) 42 | 43 | #define CCD_REALLOC_ARR(ptr, type, num_elements) \ 44 | __CCD_ALLOC_MEMORY(type, ptr, sizeof(type) * (num_elements)) 45 | 46 | #ifdef __cplusplus 47 | } /* extern "C" */ 48 | #endif /* __cplusplus */ 49 | 50 | #endif /* __CCD_ALLOC_H__ */ 51 | -------------------------------------------------------------------------------- /src/ccd/ccd.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010,2011 Daniel Fiser 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 | * . 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 22 | 23 | #ifdef __cplusplus 24 | extern "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 | */ 32 | typedef 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 | */ 39 | typedef 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 | */ 47 | typedef void (*ccd_center_fn)(const void *obj1, ccd_vec3_t *center); 48 | 49 | /** 50 | * Main structure of CCD algorithm. 51 | */ 52 | struct _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 | }; 70 | typedef struct _ccd_t ccd_t; 71 | 72 | /** 73 | * Default first direction. 74 | */ 75 | CCD_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 | */ 96 | CCD_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 | */ 107 | CCD_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 | */ 124 | CCD_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 | */ 131 | CCD_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 | */ 146 | CCD_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__ */ 155 | -------------------------------------------------------------------------------- /src/ccd/ccd_export.h: -------------------------------------------------------------------------------- 1 | #ifndef CCD_EXPORT_H 2 | #define CCD_EXPORT_H 3 | 4 | #ifdef CCD_STATIC_DEFINE 5 | # define CCD_EXPORT 6 | #else 7 | # ifdef _MSC_VER 8 | # ifdef ccd_EXPORTS 9 | # define CCD_EXPORT __declspec(dllexport) 10 | # else /* ccd_EXPORTS */ 11 | # define CCD_EXPORT __declspec(dllimport) 12 | # endif /* ccd_EXPORTS */ 13 | # else 14 | # ifndef CCD_EXPORT 15 | # ifdef ccd_EXPORTS 16 | /* We are building this library */ 17 | # define CCD_EXPORT __attribute__((visibility("default"))) 18 | # else 19 | /* We are using this library */ 20 | # define CCD_EXPORT __attribute__((visibility("default"))) 21 | # endif 22 | # endif 23 | # endif 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/ccd/compiler.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_COMPILER_H__ 19 | #define __CCD_COMPILER_H__ 20 | 21 | #include 22 | 23 | #define ccd_offsetof(TYPE, MEMBER) offsetof(TYPE, MEMBER) 24 | 25 | #define ccd_container_of(ptr, type, member) \ 26 | (type *)( (char *)ptr - ccd_offsetof(type, member)) 27 | 28 | /** 29 | * Marks inline function. 30 | */ 31 | #ifdef __GNUC__ 32 | # define _ccd_inline static inline __attribute__((always_inline)) 33 | #else /* __GNUC__ */ 34 | # define _ccd_inline static __inline 35 | #endif /* __GNUC__ */ 36 | 37 | 38 | /** 39 | * __prefetch(x) - prefetches the cacheline at "x" for read 40 | * __prefetchw(x) - prefetches the cacheline at "x" for write 41 | */ 42 | #ifdef __GNUC__ 43 | # define _ccd_prefetch(x) __builtin_prefetch(x) 44 | # define _ccd_prefetchw(x) __builtin_prefetch(x,1) 45 | #else /* __GNUC__ */ 46 | # define _ccd_prefetch(x) ((void)0) 47 | # define _ccd_prefetchw(x) ((void)0) 48 | #endif /* __GNUC__ */ 49 | 50 | 51 | #ifdef __ICC 52 | // disable unused parameter warning 53 | # pragma warning(disable:869) 54 | // disable annoying "operands are evaluated in unspecified order" warning 55 | # pragma warning(disable:981) 56 | #endif /* __ICC */ 57 | 58 | #ifdef _MSC_VER 59 | // disable unsafe function warning 60 | # define _CRT_SECURE_NO_WARNINGS 61 | #endif /* _MSC_VER */ 62 | 63 | #endif /* __CCD_COMPILER_H__ */ 64 | 65 | -------------------------------------------------------------------------------- /src/ccd/config.h.cmake.in: -------------------------------------------------------------------------------- 1 | #ifndef __CCD_CONFIG_H__ 2 | #define __CCD_CONFIG_H__ 3 | 4 | #cmakedefine CCD_SINGLE 5 | #cmakedefine CCD_DOUBLE 6 | 7 | #endif /* __CCD_CONFIG_H__ */ 8 | -------------------------------------------------------------------------------- /src/ccd/config.h.m4: -------------------------------------------------------------------------------- 1 | #ifndef __CCD_CONFIG_H__ 2 | #define __CCD_CONFIG_H__ 3 | 4 | ifdef(`USE_SINGLE', `#define CCD_SINGLE') 5 | ifdef(`USE_DOUBLE', `#define CCD_DOUBLE') 6 | 7 | #endif /* __CCD_CONFIG_H__ */ 8 | -------------------------------------------------------------------------------- /src/ccd/quat.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_QUAT_H__ 19 | #define __CCD_QUAT_H__ 20 | 21 | #include 22 | #include 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif /* __cplusplus */ 27 | 28 | struct _ccd_quat_t { 29 | ccd_real_t q[4]; //!< x, y, z, w 30 | }; 31 | typedef struct _ccd_quat_t ccd_quat_t; 32 | 33 | #define CCD_QUAT(name, x, y, z, w) \ 34 | ccd_quat_t name = { {x, y, z, w} } 35 | 36 | _ccd_inline ccd_real_t ccdQuatLen2(const ccd_quat_t *q); 37 | _ccd_inline ccd_real_t ccdQuatLen(const ccd_quat_t *q); 38 | 39 | _ccd_inline void ccdQuatSet(ccd_quat_t *q, ccd_real_t x, ccd_real_t y, ccd_real_t z, ccd_real_t w); 40 | _ccd_inline void ccdQuatCopy(ccd_quat_t *dest, const ccd_quat_t *src); 41 | 42 | _ccd_inline int ccdQuatNormalize(ccd_quat_t *q); 43 | 44 | _ccd_inline void ccdQuatSetAngleAxis(ccd_quat_t *q, 45 | ccd_real_t angle, const ccd_vec3_t *axis); 46 | 47 | _ccd_inline void ccdQuatScale(ccd_quat_t *q, ccd_real_t k); 48 | 49 | /** 50 | * q = q * q2 51 | */ 52 | _ccd_inline void ccdQuatMul(ccd_quat_t *q, const ccd_quat_t *q2); 53 | 54 | /** 55 | * q = a * b 56 | */ 57 | _ccd_inline void ccdQuatMul2(ccd_quat_t *q, 58 | const ccd_quat_t *a, const ccd_quat_t *b); 59 | 60 | /** 61 | * Inverts quaternion. 62 | * Returns 0 on success. 63 | */ 64 | _ccd_inline int ccdQuatInvert(ccd_quat_t *q); 65 | _ccd_inline int ccdQuatInvert2(ccd_quat_t *dest, const ccd_quat_t *src); 66 | 67 | 68 | /** 69 | * Rotate vector v by quaternion q. 70 | */ 71 | _ccd_inline void ccdQuatRotVec(ccd_vec3_t *v, const ccd_quat_t *q); 72 | 73 | 74 | /**** INLINES ****/ 75 | _ccd_inline ccd_real_t ccdQuatLen2(const ccd_quat_t *q) 76 | { 77 | ccd_real_t len; 78 | 79 | len = q->q[0] * q->q[0]; 80 | len += q->q[1] * q->q[1]; 81 | len += q->q[2] * q->q[2]; 82 | len += q->q[3] * q->q[3]; 83 | 84 | return len; 85 | } 86 | 87 | _ccd_inline ccd_real_t ccdQuatLen(const ccd_quat_t *q) 88 | { 89 | return CCD_SQRT(ccdQuatLen2(q)); 90 | } 91 | 92 | _ccd_inline void ccdQuatSet(ccd_quat_t *q, ccd_real_t x, ccd_real_t y, ccd_real_t z, ccd_real_t w) 93 | { 94 | q->q[0] = x; 95 | q->q[1] = y; 96 | q->q[2] = z; 97 | q->q[3] = w; 98 | } 99 | 100 | _ccd_inline void ccdQuatCopy(ccd_quat_t *dest, const ccd_quat_t *src) 101 | { 102 | *dest = *src; 103 | } 104 | 105 | 106 | _ccd_inline int ccdQuatNormalize(ccd_quat_t *q) 107 | { 108 | ccd_real_t len = ccdQuatLen(q); 109 | if (len < CCD_EPS) 110 | return 0; 111 | 112 | ccdQuatScale(q, CCD_ONE / len); 113 | return 1; 114 | } 115 | 116 | _ccd_inline void ccdQuatSetAngleAxis(ccd_quat_t *q, 117 | ccd_real_t angle, const ccd_vec3_t *axis) 118 | { 119 | ccd_real_t a, x, y, z, n, s; 120 | 121 | a = angle/2; 122 | x = ccdVec3X(axis); 123 | y = ccdVec3Y(axis); 124 | z = ccdVec3Z(axis); 125 | n = CCD_SQRT(x*x + y*y + z*z); 126 | 127 | // axis==0? (treat this the same as angle==0 with an arbitrary axis) 128 | if (n < CCD_EPS){ 129 | q->q[0] = q->q[1] = q->q[2] = CCD_ZERO; 130 | q->q[3] = CCD_ONE; 131 | }else{ 132 | s = sin(a)/n; 133 | 134 | q->q[3] = cos(a); 135 | q->q[0] = x*s; 136 | q->q[1] = y*s; 137 | q->q[2] = z*s; 138 | 139 | ccdQuatNormalize(q); 140 | } 141 | } 142 | 143 | 144 | _ccd_inline void ccdQuatScale(ccd_quat_t *q, ccd_real_t k) 145 | { 146 | size_t i; 147 | for (i = 0; i < 4; i++) 148 | q->q[i] *= k; 149 | } 150 | 151 | _ccd_inline void ccdQuatMul(ccd_quat_t *q, const ccd_quat_t *q2) 152 | { 153 | ccd_quat_t a; 154 | ccdQuatCopy(&a, q); 155 | ccdQuatMul2(q, &a, q2); 156 | } 157 | 158 | _ccd_inline void ccdQuatMul2(ccd_quat_t *q, 159 | const ccd_quat_t *a, const ccd_quat_t *b) 160 | { 161 | q->q[0] = a->q[3] * b->q[0] 162 | + a->q[0] * b->q[3] 163 | + a->q[1] * b->q[2] 164 | - a->q[2] * b->q[1]; 165 | q->q[1] = a->q[3] * b->q[1] 166 | + a->q[1] * b->q[3] 167 | - a->q[0] * b->q[2] 168 | + a->q[2] * b->q[0]; 169 | q->q[2] = a->q[3] * b->q[2] 170 | + a->q[2] * b->q[3] 171 | + a->q[0] * b->q[1] 172 | - a->q[1] * b->q[0]; 173 | q->q[3] = a->q[3] * b->q[3] 174 | - a->q[0] * b->q[0] 175 | - a->q[1] * b->q[1] 176 | - a->q[2] * b->q[2]; 177 | } 178 | 179 | _ccd_inline int ccdQuatInvert(ccd_quat_t *q) 180 | { 181 | ccd_real_t len2 = ccdQuatLen2(q); 182 | if (len2 < CCD_EPS) 183 | return -1; 184 | 185 | len2 = CCD_ONE / len2; 186 | 187 | q->q[0] = -q->q[0] * len2; 188 | q->q[1] = -q->q[1] * len2; 189 | q->q[2] = -q->q[2] * len2; 190 | q->q[3] = q->q[3] * len2; 191 | 192 | return 0; 193 | } 194 | _ccd_inline int ccdQuatInvert2(ccd_quat_t *dest, const ccd_quat_t *src) 195 | { 196 | ccdQuatCopy(dest, src); 197 | return ccdQuatInvert(dest); 198 | } 199 | 200 | _ccd_inline void ccdQuatRotVec(ccd_vec3_t *v, const ccd_quat_t *q) 201 | { 202 | // original version: 31 mul + 21 add 203 | // optimized version: 18 mul + 12 add 204 | // formula: v = v + 2 * cross(q.xyz, cross(q.xyz, v) + q.w * v) 205 | ccd_real_t cross1_x, cross1_y, cross1_z, cross2_x, cross2_y, cross2_z; 206 | ccd_real_t x, y, z, w; 207 | ccd_real_t vx, vy, vz; 208 | 209 | vx = ccdVec3X(v); 210 | vy = ccdVec3Y(v); 211 | vz = ccdVec3Z(v); 212 | 213 | w = q->q[3]; 214 | x = q->q[0]; 215 | y = q->q[1]; 216 | z = q->q[2]; 217 | 218 | cross1_x = y * vz - z * vy + w * vx; 219 | cross1_y = z * vx - x * vz + w * vy; 220 | cross1_z = x * vy - y * vx + w * vz; 221 | cross2_x = y * cross1_z - z * cross1_y; 222 | cross2_y = z * cross1_x - x * cross1_z; 223 | cross2_z = x * cross1_y - y * cross1_x; 224 | ccdVec3Set(v, vx + 2 * cross2_x, vy + 2 * cross2_y, vz + 2 * cross2_z); 225 | } 226 | 227 | #ifdef __cplusplus 228 | } /* extern "C" */ 229 | #endif /* __cplusplus */ 230 | 231 | #endif /* __CCD_QUAT_H__ */ 232 | -------------------------------------------------------------------------------- /src/ccd/vec3.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010-2013 Daniel Fiser 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 | * . 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_VEC3_H__ 19 | #define __CCD_VEC3_H__ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif /* __cplusplus */ 31 | 32 | 33 | #ifndef CCD_SINGLE 34 | # ifndef CCD_DOUBLE 35 | # error You must define CCD_SINGLE or CCD_DOUBLE 36 | # endif /* CCD_DOUBLE */ 37 | #endif /* CCD_SINGLE */ 38 | 39 | #ifdef WIN32 40 | # define CCD_FMIN(x, y) ((x) < (y) ? (x) : (y)) 41 | #endif /* WIN32 */ 42 | 43 | #ifdef CCD_SINGLE 44 | # ifdef CCD_DOUBLE 45 | # error You can define either CCD_SINGLE or CCD_DOUBLE, not both! 46 | # endif /* CCD_DOUBLE */ 47 | 48 | typedef float ccd_real_t; 49 | 50 | //# define CCD_EPS 1E-6 51 | # define CCD_EPS FLT_EPSILON 52 | 53 | # define CCD_REAL_MAX FLT_MAX 54 | 55 | # define CCD_REAL(x) (x ## f) /*!< form a constant */ 56 | # define CCD_SQRT(x) (sqrtf(x)) /*!< square root */ 57 | # define CCD_FABS(x) (fabsf(x)) /*!< absolute value */ 58 | # define CCD_FMAX(x, y) (fmaxf((x), (y))) /*!< maximum of two floats */ 59 | 60 | # ifndef CCD_FMIN 61 | # define CCD_FMIN(x, y) (fminf((x), (y))) /*!< minimum of two floats */ 62 | # endif /* CCD_FMIN */ 63 | 64 | #endif /* CCD_SINGLE */ 65 | 66 | #ifdef CCD_DOUBLE 67 | typedef double ccd_real_t; 68 | 69 | //# define CCD_EPS 1E-10 70 | # define CCD_EPS DBL_EPSILON 71 | 72 | # define CCD_REAL_MAX DBL_MAX 73 | 74 | # define CCD_REAL(x) (x) /*!< form a constant */ 75 | # define CCD_SQRT(x) (sqrt(x)) /*!< square root */ 76 | # define CCD_FABS(x) (fabs(x)) /*!< absolute value */ 77 | # define CCD_FMAX(x, y) (fmax((x), (y))) /*!< maximum of two floats */ 78 | 79 | # ifndef CCD_FMIN 80 | # define CCD_FMIN(x, y) (fmin((x), (y))) /*!< minimum of two floats */ 81 | # endif /* CCD_FMIN */ 82 | 83 | #endif /* CCD_DOUBLE */ 84 | 85 | #define CCD_ONE CCD_REAL(1.) 86 | #define CCD_ZERO CCD_REAL(0.) 87 | 88 | struct _ccd_vec3_t { 89 | ccd_real_t v[3]; 90 | }; 91 | typedef struct _ccd_vec3_t ccd_vec3_t; 92 | 93 | 94 | /** 95 | * Holds origin (0,0,0) - this variable is meant to be read-only! 96 | */ 97 | CCD_EXPORT extern ccd_vec3_t *ccd_vec3_origin; 98 | 99 | /** 100 | * Array of points uniformly distributed on unit sphere. 101 | */ 102 | CCD_EXPORT extern ccd_vec3_t *ccd_points_on_sphere; 103 | CCD_EXPORT extern size_t ccd_points_on_sphere_len; 104 | 105 | /** Returns sign of value. */ 106 | _ccd_inline int ccdSign(ccd_real_t val); 107 | /** Returns true if val is zero. **/ 108 | _ccd_inline int ccdIsZero(ccd_real_t val); 109 | /** Returns true if a and b equal. **/ 110 | _ccd_inline int ccdEq(ccd_real_t a, ccd_real_t b); 111 | 112 | 113 | #define CCD_VEC3_STATIC(x, y, z) \ 114 | { { (x), (y), (z) } } 115 | 116 | #define CCD_VEC3(name, x, y, z) \ 117 | ccd_vec3_t name = CCD_VEC3_STATIC((x), (y), (z)) 118 | 119 | _ccd_inline ccd_real_t ccdVec3X(const ccd_vec3_t *v); 120 | _ccd_inline ccd_real_t ccdVec3Y(const ccd_vec3_t *v); 121 | _ccd_inline ccd_real_t ccdVec3Z(const ccd_vec3_t *v); 122 | 123 | /** 124 | * Returns true if a and b equal. 125 | */ 126 | _ccd_inline int ccdVec3Eq(const ccd_vec3_t *a, const ccd_vec3_t *b); 127 | 128 | /** 129 | * Returns squared length of vector. 130 | */ 131 | _ccd_inline ccd_real_t ccdVec3Len2(const ccd_vec3_t *v); 132 | 133 | /** 134 | * Returns distance between a and b. 135 | */ 136 | _ccd_inline ccd_real_t ccdVec3Dist2(const ccd_vec3_t *a, const ccd_vec3_t *b); 137 | 138 | 139 | _ccd_inline void ccdVec3Set(ccd_vec3_t *v, ccd_real_t x, ccd_real_t y, ccd_real_t z); 140 | 141 | /** 142 | * v = w 143 | */ 144 | _ccd_inline void ccdVec3Copy(ccd_vec3_t *v, const ccd_vec3_t *w); 145 | 146 | /** 147 | * Substracts coordinates of vector w from vector v. v = v - w 148 | */ 149 | _ccd_inline void ccdVec3Sub(ccd_vec3_t *v, const ccd_vec3_t *w); 150 | 151 | /** 152 | * Adds coordinates of vector w to vector v. v = v + w 153 | */ 154 | _ccd_inline void ccdVec3Add(ccd_vec3_t *v, const ccd_vec3_t *w); 155 | 156 | /** 157 | * d = v - w 158 | */ 159 | _ccd_inline void ccdVec3Sub2(ccd_vec3_t *d, const ccd_vec3_t *v, const ccd_vec3_t *w); 160 | 161 | /** 162 | * d = d * k; 163 | */ 164 | _ccd_inline void ccdVec3Scale(ccd_vec3_t *d, ccd_real_t k); 165 | 166 | /** 167 | * Normalizes given vector to unit length. 168 | */ 169 | _ccd_inline void ccdVec3Normalize(ccd_vec3_t *d); 170 | 171 | 172 | /** 173 | * Dot product of two vectors. 174 | */ 175 | _ccd_inline ccd_real_t ccdVec3Dot(const ccd_vec3_t *a, const ccd_vec3_t *b); 176 | 177 | /** 178 | * Cross product: d = a x b. 179 | */ 180 | _ccd_inline void ccdVec3Cross(ccd_vec3_t *d, const ccd_vec3_t *a, const ccd_vec3_t *b); 181 | 182 | 183 | /** 184 | * Returns distance^2 of point P to segment ab. 185 | * If witness is non-NULL it is filled with coordinates of point from which 186 | * was computed distance to point P. 187 | */ 188 | CCD_EXPORT ccd_real_t ccdVec3PointSegmentDist2(const ccd_vec3_t *P, 189 | const ccd_vec3_t *a, 190 | const ccd_vec3_t *b, 191 | ccd_vec3_t *witness); 192 | 193 | /** 194 | * Returns distance^2 of point P from triangle formed by triplet a, b, c. 195 | * If witness vector is provided it is filled with coordinates of point 196 | * from which was computed distance to point P. 197 | */ 198 | CCD_EXPORT ccd_real_t ccdVec3PointTriDist2(const ccd_vec3_t *P, 199 | const ccd_vec3_t *a, 200 | const ccd_vec3_t *b, 201 | const ccd_vec3_t *c, 202 | ccd_vec3_t *witness); 203 | 204 | 205 | /**** INLINES ****/ 206 | _ccd_inline int ccdSign(ccd_real_t val) 207 | { 208 | if (ccdIsZero(val)){ 209 | return 0; 210 | }else if (val < CCD_ZERO){ 211 | return -1; 212 | } 213 | return 1; 214 | } 215 | 216 | _ccd_inline int ccdIsZero(ccd_real_t val) 217 | { 218 | return CCD_FABS(val) < CCD_EPS; 219 | } 220 | 221 | _ccd_inline int ccdEq(ccd_real_t _a, ccd_real_t _b) 222 | { 223 | ccd_real_t ab; 224 | ccd_real_t a, b; 225 | 226 | ab = CCD_FABS(_a - _b); 227 | if (CCD_FABS(ab) < CCD_EPS) 228 | return 1; 229 | 230 | a = CCD_FABS(_a); 231 | b = CCD_FABS(_b); 232 | if (b > a){ 233 | return ab < CCD_EPS * b; 234 | }else{ 235 | return ab < CCD_EPS * a; 236 | } 237 | } 238 | 239 | 240 | _ccd_inline ccd_real_t ccdVec3X(const ccd_vec3_t *v) 241 | { 242 | return v->v[0]; 243 | } 244 | 245 | _ccd_inline ccd_real_t ccdVec3Y(const ccd_vec3_t *v) 246 | { 247 | return v->v[1]; 248 | } 249 | 250 | _ccd_inline ccd_real_t ccdVec3Z(const ccd_vec3_t *v) 251 | { 252 | return v->v[2]; 253 | } 254 | 255 | _ccd_inline int ccdVec3Eq(const ccd_vec3_t *a, const ccd_vec3_t *b) 256 | { 257 | return ccdEq(ccdVec3X(a), ccdVec3X(b)) 258 | && ccdEq(ccdVec3Y(a), ccdVec3Y(b)) 259 | && ccdEq(ccdVec3Z(a), ccdVec3Z(b)); 260 | } 261 | 262 | _ccd_inline ccd_real_t ccdVec3Len2(const ccd_vec3_t *v) 263 | { 264 | return ccdVec3Dot(v, v); 265 | } 266 | 267 | _ccd_inline ccd_real_t ccdVec3Dist2(const ccd_vec3_t *a, const ccd_vec3_t *b) 268 | { 269 | ccd_vec3_t ab; 270 | ccdVec3Sub2(&ab, a, b); 271 | return ccdVec3Len2(&ab); 272 | } 273 | 274 | _ccd_inline void ccdVec3Set(ccd_vec3_t *v, ccd_real_t x, ccd_real_t y, ccd_real_t z) 275 | { 276 | v->v[0] = x; 277 | v->v[1] = y; 278 | v->v[2] = z; 279 | } 280 | 281 | _ccd_inline void ccdVec3Copy(ccd_vec3_t *v, const ccd_vec3_t *w) 282 | { 283 | *v = *w; 284 | } 285 | 286 | _ccd_inline void ccdVec3Sub(ccd_vec3_t *v, const ccd_vec3_t *w) 287 | { 288 | v->v[0] -= w->v[0]; 289 | v->v[1] -= w->v[1]; 290 | v->v[2] -= w->v[2]; 291 | } 292 | _ccd_inline void ccdVec3Sub2(ccd_vec3_t *d, const ccd_vec3_t *v, const ccd_vec3_t *w) 293 | { 294 | d->v[0] = v->v[0] - w->v[0]; 295 | d->v[1] = v->v[1] - w->v[1]; 296 | d->v[2] = v->v[2] - w->v[2]; 297 | } 298 | 299 | _ccd_inline void ccdVec3Add(ccd_vec3_t *v, const ccd_vec3_t *w) 300 | { 301 | v->v[0] += w->v[0]; 302 | v->v[1] += w->v[1]; 303 | v->v[2] += w->v[2]; 304 | } 305 | 306 | _ccd_inline void ccdVec3Scale(ccd_vec3_t *d, ccd_real_t k) 307 | { 308 | d->v[0] *= k; 309 | d->v[1] *= k; 310 | d->v[2] *= k; 311 | } 312 | 313 | _ccd_inline void ccdVec3Normalize(ccd_vec3_t *d) 314 | { 315 | ccd_real_t k = CCD_ONE / CCD_SQRT(ccdVec3Len2(d)); 316 | ccdVec3Scale(d, k); 317 | } 318 | 319 | _ccd_inline ccd_real_t ccdVec3Dot(const ccd_vec3_t *a, const ccd_vec3_t *b) 320 | { 321 | ccd_real_t dot; 322 | 323 | dot = a->v[0] * b->v[0]; 324 | dot += a->v[1] * b->v[1]; 325 | dot += a->v[2] * b->v[2]; 326 | return dot; 327 | } 328 | 329 | _ccd_inline void ccdVec3Cross(ccd_vec3_t *d, const ccd_vec3_t *a, const ccd_vec3_t *b) 330 | { 331 | d->v[0] = (a->v[1] * b->v[2]) - (a->v[2] * b->v[1]); 332 | d->v[1] = (a->v[2] * b->v[0]) - (a->v[0] * b->v[2]); 333 | d->v[2] = (a->v[0] * b->v[1]) - (a->v[1] * b->v[0]); 334 | } 335 | 336 | #ifdef __cplusplus 337 | } /* extern "C" */ 338 | #endif /* __cplusplus */ 339 | 340 | #endif /* __CCD_VEC3_H__ */ 341 | -------------------------------------------------------------------------------- /src/dbg.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_DBG_H__ 19 | #define __CCD_DBG_H__ 20 | 21 | /** 22 | * Some macros which can be used for printing debug info to stderr if macro 23 | * NDEBUG not defined. 24 | * 25 | * DBG_PROLOGUE can be specified as string and this string will be 26 | * prepended to output text 27 | */ 28 | #ifndef NDEBUG 29 | 30 | #include 31 | 32 | #ifndef DBG_PROLOGUE 33 | # define DBG_PROLOGUE 34 | #endif 35 | 36 | # define DBG(format, ...) do { \ 37 | fprintf(stderr, DBG_PROLOGUE "%s :: " format "\n", __func__, ## __VA_ARGS__); \ 38 | fflush(stderr); \ 39 | } while (0) 40 | 41 | # define DBG2(str) do { \ 42 | fprintf(stderr, DBG_PROLOGUE "%s :: " str "\n", __func__); \ 43 | fflush(stderr); \ 44 | } while (0) 45 | 46 | # define DBG_VEC3(vec, prefix) do {\ 47 | fprintf(stderr, DBG_PROLOGUE "%s :: %s[%lf %lf %lf]\n", \ 48 | __func__, prefix, ccdVec3X(vec), ccdVec3Y(vec), ccdVec3Z(vec)); \ 49 | fflush(stderr); \ 50 | } while (0) 51 | /* 52 | # define DBG_VEC3(vec, prefix) do {\ 53 | fprintf(stderr, DBG_PROLOGUE "%s :: %s[%.20lf %.20lf %.20lf]\n", \ 54 | __func__, prefix, ccdVec3X(vec), ccdVec3Y(vec), ccdVec3Z(vec)); \ 55 | fflush(stderr); \ 56 | } while (0) 57 | */ 58 | 59 | #else 60 | # define DBG(format, ...) 61 | # define DBG2(str) 62 | # define DBG_VEC3(v, prefix) 63 | #endif 64 | 65 | #endif /* __CCD_DBG_H__ */ 66 | -------------------------------------------------------------------------------- /src/list.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_LIST_H__ 19 | #define __CCD_LIST_H__ 20 | 21 | #include 22 | #include 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif /* __cplusplus */ 27 | 28 | struct _ccd_list_t { 29 | struct _ccd_list_t *next, *prev; 30 | }; 31 | typedef struct _ccd_list_t ccd_list_t; 32 | 33 | 34 | 35 | /** 36 | * Get the struct for this entry. 37 | * @ptr: the &ccd_list_t pointer. 38 | * @type: the type of the struct this is embedded in. 39 | * @member: the name of the list_struct within the struct. 40 | */ 41 | #define ccdListEntry(ptr, type, member) \ 42 | ccd_container_of(ptr, type, member) 43 | 44 | /** 45 | * Iterates over list. 46 | */ 47 | #define ccdListForEach(list, item) \ 48 | for (item = (list)->next; \ 49 | _ccd_prefetch((item)->next), item != (list); \ 50 | item = (item)->next) 51 | 52 | /** 53 | * Iterates over list safe against remove of list entry 54 | */ 55 | #define ccdListForEachSafe(list, item, tmp) \ 56 | for (item = (list)->next, tmp = (item)->next; \ 57 | item != (list); \ 58 | item = tmp, tmp = (item)->next) 59 | 60 | /** 61 | * Iterates over list of given type. 62 | * @pos: the type * to use as a loop cursor. 63 | * @head: the head for your list. 64 | * @member: the name of the list_struct within the struct. 65 | */ 66 | #define ccdListForEachEntry(head, pos, postype, member) \ 67 | for (pos = ccdListEntry((head)->next, postype, member); \ 68 | _ccd_prefetch(pos->member.next), &pos->member != (head); \ 69 | pos = ccdListEntry(pos->member.next, postype, member)) 70 | 71 | /** 72 | * Iterates over list of given type safe against removal of list entry 73 | * @pos: the type * to use as a loop cursor. 74 | * @n: another type * to use as temporary storage 75 | * @head: the head for your list. 76 | * @member: the name of the list_struct within the struct. 77 | */ 78 | #define ccdListForEachEntrySafe(head, pos, postype, n, ntype, member) \ 79 | for (pos = ccdListEntry((head)->next, postype, member), \ 80 | n = ccdListEntry(pos->member.next, postype, member); \ 81 | &pos->member != (head); \ 82 | pos = n, n = ccdListEntry(n->member.next, ntype, member)) 83 | 84 | 85 | /** 86 | * Initialize list. 87 | */ 88 | _ccd_inline void ccdListInit(ccd_list_t *l); 89 | 90 | _ccd_inline ccd_list_t *ccdListNext(ccd_list_t *l); 91 | _ccd_inline ccd_list_t *ccdListPrev(ccd_list_t *l); 92 | 93 | /** 94 | * Returns true if list is empty. 95 | */ 96 | _ccd_inline int ccdListEmpty(const ccd_list_t *head); 97 | 98 | /** 99 | * Appends item to end of the list l. 100 | */ 101 | _ccd_inline void ccdListAppend(ccd_list_t *l, ccd_list_t *item); 102 | 103 | /** 104 | * Removes item from list. 105 | */ 106 | _ccd_inline void ccdListDel(ccd_list_t *item); 107 | 108 | 109 | 110 | /// 111 | /// INLINES: 112 | /// 113 | 114 | _ccd_inline void ccdListInit(ccd_list_t *l) 115 | { 116 | l->next = l; 117 | l->prev = l; 118 | } 119 | 120 | _ccd_inline ccd_list_t *ccdListNext(ccd_list_t *l) 121 | { 122 | return l->next; 123 | } 124 | 125 | _ccd_inline ccd_list_t *ccdListPrev(ccd_list_t *l) 126 | { 127 | return l->prev; 128 | } 129 | 130 | _ccd_inline int ccdListEmpty(const ccd_list_t *head) 131 | { 132 | return head->next == head; 133 | } 134 | 135 | _ccd_inline void ccdListAppend(ccd_list_t *l, ccd_list_t *new) 136 | { 137 | new->prev = l->prev; 138 | new->next = l; 139 | l->prev->next = new; 140 | l->prev = new; 141 | } 142 | 143 | _ccd_inline void ccdListDel(ccd_list_t *item) 144 | { 145 | item->next->prev = item->prev; 146 | item->prev->next = item->next; 147 | item->next = item; 148 | item->prev = item; 149 | } 150 | 151 | #ifdef __cplusplus 152 | } /* extern "C" */ 153 | #endif /* __cplusplus */ 154 | 155 | #endif /* __CCD_LIST_H__ */ 156 | -------------------------------------------------------------------------------- /src/polytope.c: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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 | #include 19 | #include 20 | #include "polytope.h" 21 | #include "alloc.h" 22 | 23 | _ccd_inline void _ccdPtNearestUpdate(ccd_pt_t *pt, ccd_pt_el_t *el) 24 | { 25 | if (ccdEq(pt->nearest_dist, el->dist)){ 26 | if (el->type < pt->nearest_type){ 27 | pt->nearest = el; 28 | pt->nearest_dist = el->dist; 29 | pt->nearest_type = el->type; 30 | } 31 | }else if (el->dist < pt->nearest_dist){ 32 | pt->nearest = el; 33 | pt->nearest_dist = el->dist; 34 | pt->nearest_type = el->type; 35 | } 36 | } 37 | 38 | static void _ccdPtNearestRenew(ccd_pt_t *pt) 39 | { 40 | ccd_pt_vertex_t *v; 41 | ccd_pt_edge_t *e; 42 | ccd_pt_face_t *f; 43 | 44 | pt->nearest_dist = CCD_REAL_MAX; 45 | pt->nearest_type = 3; 46 | pt->nearest = NULL; 47 | 48 | ccdListForEachEntry(&pt->vertices, v, ccd_pt_vertex_t, list){ 49 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)v); 50 | } 51 | 52 | ccdListForEachEntry(&pt->edges, e, ccd_pt_edge_t, list){ 53 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)e); 54 | } 55 | 56 | ccdListForEachEntry(&pt->faces, f, ccd_pt_face_t, list){ 57 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)f); 58 | } 59 | } 60 | 61 | 62 | 63 | void ccdPtInit(ccd_pt_t *pt) 64 | { 65 | ccdListInit(&pt->vertices); 66 | ccdListInit(&pt->edges); 67 | ccdListInit(&pt->faces); 68 | 69 | pt->nearest = NULL; 70 | pt->nearest_dist = CCD_REAL_MAX; 71 | pt->nearest_type = 3; 72 | } 73 | 74 | void ccdPtDestroy(ccd_pt_t *pt) 75 | { 76 | ccd_pt_face_t *f, *f2; 77 | ccd_pt_edge_t *e, *e2; 78 | ccd_pt_vertex_t *v, *v2; 79 | 80 | // first delete all faces 81 | ccdListForEachEntrySafe(&pt->faces, f, ccd_pt_face_t, f2, ccd_pt_face_t, list){ 82 | ccdPtDelFace(pt, f); 83 | } 84 | 85 | // delete all edges 86 | ccdListForEachEntrySafe(&pt->edges, e, ccd_pt_edge_t, e2, ccd_pt_edge_t, list){ 87 | ccdPtDelEdge(pt, e); 88 | } 89 | 90 | // delete all vertices 91 | ccdListForEachEntrySafe(&pt->vertices, v, ccd_pt_vertex_t, v2, ccd_pt_vertex_t, list){ 92 | ccdPtDelVertex(pt, v); 93 | } 94 | } 95 | 96 | 97 | ccd_pt_vertex_t *ccdPtAddVertex(ccd_pt_t *pt, const ccd_support_t *v) 98 | { 99 | ccd_pt_vertex_t *vert; 100 | 101 | vert = CCD_ALLOC(ccd_pt_vertex_t); 102 | if (vert == NULL) 103 | return NULL; 104 | 105 | vert->type = CCD_PT_VERTEX; 106 | ccdSupportCopy(&vert->v, v); 107 | 108 | vert->dist = ccdVec3Len2(&vert->v.v); 109 | ccdVec3Copy(&vert->witness, &vert->v.v); 110 | 111 | ccdListInit(&vert->edges); 112 | 113 | // add vertex to list 114 | ccdListAppend(&pt->vertices, &vert->list); 115 | 116 | // update position in .nearest array 117 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)vert); 118 | 119 | return vert; 120 | } 121 | 122 | ccd_pt_edge_t *ccdPtAddEdge(ccd_pt_t *pt, ccd_pt_vertex_t *v1, 123 | ccd_pt_vertex_t *v2) 124 | { 125 | const ccd_vec3_t *a, *b; 126 | ccd_pt_edge_t *edge; 127 | 128 | if (v1 == NULL || v2 == NULL) 129 | return NULL; 130 | 131 | edge = CCD_ALLOC(ccd_pt_edge_t); 132 | if (edge == NULL) 133 | return NULL; 134 | 135 | edge->type = CCD_PT_EDGE; 136 | edge->vertex[0] = v1; 137 | edge->vertex[1] = v2; 138 | edge->faces[0] = edge->faces[1] = NULL; 139 | 140 | a = &edge->vertex[0]->v.v; 141 | b = &edge->vertex[1]->v.v; 142 | edge->dist = ccdVec3PointSegmentDist2(ccd_vec3_origin, a, b, &edge->witness); 143 | 144 | ccdListAppend(&edge->vertex[0]->edges, &edge->vertex_list[0]); 145 | ccdListAppend(&edge->vertex[1]->edges, &edge->vertex_list[1]); 146 | 147 | ccdListAppend(&pt->edges, &edge->list); 148 | 149 | // update position in .nearest array 150 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)edge); 151 | 152 | return edge; 153 | } 154 | 155 | ccd_pt_face_t *ccdPtAddFace(ccd_pt_t *pt, ccd_pt_edge_t *e1, 156 | ccd_pt_edge_t *e2, 157 | ccd_pt_edge_t *e3) 158 | { 159 | const ccd_vec3_t *a, *b, *c; 160 | ccd_pt_face_t *face; 161 | ccd_pt_edge_t *e; 162 | size_t i; 163 | 164 | if (e1 == NULL || e2 == NULL || e3 == NULL) 165 | return NULL; 166 | 167 | face = CCD_ALLOC(ccd_pt_face_t); 168 | if (face == NULL) 169 | return NULL; 170 | 171 | face->type = CCD_PT_FACE; 172 | face->edge[0] = e1; 173 | face->edge[1] = e2; 174 | face->edge[2] = e3; 175 | 176 | // obtain triplet of vertices 177 | a = &face->edge[0]->vertex[0]->v.v; 178 | b = &face->edge[0]->vertex[1]->v.v; 179 | e = face->edge[1]; 180 | if (e->vertex[0] != face->edge[0]->vertex[0] 181 | && e->vertex[0] != face->edge[0]->vertex[1]){ 182 | c = &e->vertex[0]->v.v; 183 | }else{ 184 | c = &e->vertex[1]->v.v; 185 | } 186 | face->dist = ccdVec3PointTriDist2(ccd_vec3_origin, a, b, c, &face->witness); 187 | 188 | 189 | for (i = 0; i < 3; i++){ 190 | if (face->edge[i]->faces[0] == NULL){ 191 | face->edge[i]->faces[0] = face; 192 | }else{ 193 | face->edge[i]->faces[1] = face; 194 | } 195 | } 196 | 197 | ccdListAppend(&pt->faces, &face->list); 198 | 199 | // update position in .nearest array 200 | _ccdPtNearestUpdate(pt, (ccd_pt_el_t *)face); 201 | 202 | return face; 203 | } 204 | 205 | 206 | void ccdPtRecomputeDistances(ccd_pt_t *pt) 207 | { 208 | ccd_pt_vertex_t *v; 209 | ccd_pt_edge_t *e; 210 | ccd_pt_face_t *f; 211 | const ccd_vec3_t *a, *b, *c; 212 | ccd_real_t dist; 213 | 214 | ccdListForEachEntry(&pt->vertices, v, ccd_pt_vertex_t, list){ 215 | dist = ccdVec3Len2(&v->v.v); 216 | v->dist = dist; 217 | ccdVec3Copy(&v->witness, &v->v.v); 218 | } 219 | 220 | ccdListForEachEntry(&pt->edges, e, ccd_pt_edge_t, list){ 221 | a = &e->vertex[0]->v.v; 222 | b = &e->vertex[1]->v.v; 223 | dist = ccdVec3PointSegmentDist2(ccd_vec3_origin, a, b, &e->witness); 224 | e->dist = dist; 225 | } 226 | 227 | ccdListForEachEntry(&pt->faces, f, ccd_pt_face_t, list){ 228 | // obtain triplet of vertices 229 | a = &f->edge[0]->vertex[0]->v.v; 230 | b = &f->edge[0]->vertex[1]->v.v; 231 | e = f->edge[1]; 232 | if (e->vertex[0] != f->edge[0]->vertex[0] 233 | && e->vertex[0] != f->edge[0]->vertex[1]){ 234 | c = &e->vertex[0]->v.v; 235 | }else{ 236 | c = &e->vertex[1]->v.v; 237 | } 238 | 239 | dist = ccdVec3PointTriDist2(ccd_vec3_origin, a, b, c, &f->witness); 240 | f->dist = dist; 241 | } 242 | } 243 | 244 | ccd_pt_el_t *ccdPtNearest(ccd_pt_t *pt) 245 | { 246 | if (!pt->nearest){ 247 | _ccdPtNearestRenew(pt); 248 | } 249 | return pt->nearest; 250 | } 251 | 252 | 253 | void ccdPtDumpSVT(ccd_pt_t *pt, const char *fn) 254 | { 255 | FILE *fout; 256 | 257 | fout = fopen(fn, "a"); 258 | if (fout == NULL) 259 | return; 260 | 261 | ccdPtDumpSVT2(pt, fout); 262 | 263 | fclose(fout); 264 | } 265 | 266 | void ccdPtDumpSVT2(ccd_pt_t *pt, FILE *fout) 267 | { 268 | ccd_pt_vertex_t *v, *a, *b, *c; 269 | ccd_pt_edge_t *e; 270 | ccd_pt_face_t *f; 271 | size_t i; 272 | 273 | fprintf(fout, "-----\n"); 274 | 275 | fprintf(fout, "Points:\n"); 276 | i = 0; 277 | ccdListForEachEntry(&pt->vertices, v, ccd_pt_vertex_t, list){ 278 | v->id = i++; 279 | fprintf(fout, "%lf %lf %lf\n", 280 | ccdVec3X(&v->v.v), ccdVec3Y(&v->v.v), ccdVec3Z(&v->v.v)); 281 | } 282 | 283 | fprintf(fout, "Edges:\n"); 284 | ccdListForEachEntry(&pt->edges, e, ccd_pt_edge_t, list){ 285 | fprintf(fout, "%d %d\n", e->vertex[0]->id, e->vertex[1]->id); 286 | } 287 | 288 | fprintf(fout, "Faces:\n"); 289 | ccdListForEachEntry(&pt->faces, f, ccd_pt_face_t, list){ 290 | a = f->edge[0]->vertex[0]; 291 | b = f->edge[0]->vertex[1]; 292 | c = f->edge[1]->vertex[0]; 293 | if (c == a || c == b){ 294 | c = f->edge[1]->vertex[1]; 295 | } 296 | fprintf(fout, "%d %d %d\n", a->id, b->id, c->id); 297 | } 298 | } 299 | -------------------------------------------------------------------------------- /src/polytope.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_POLYTOPE_H__ 19 | #define __CCD_POLYTOPE_H__ 20 | 21 | #include 22 | #include 23 | #include "support.h" 24 | #include "list.h" 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif /* __cplusplus */ 29 | 30 | #define CCD_PT_VERTEX 1 31 | #define CCD_PT_EDGE 2 32 | #define CCD_PT_FACE 3 33 | 34 | 35 | #define __CCD_PT_EL \ 36 | int type; /*! type of element */ \ 37 | ccd_real_t dist; /*! distance from origin */ \ 38 | ccd_vec3_t witness; /*! witness point of projection of origin */ \ 39 | ccd_list_t list; /*! list of elements of same type */ 40 | 41 | /** 42 | * General polytope element. 43 | * Could be vertex, edge or triangle. 44 | */ 45 | struct _ccd_pt_el_t { 46 | __CCD_PT_EL 47 | }; 48 | typedef struct _ccd_pt_el_t ccd_pt_el_t; 49 | 50 | struct _ccd_pt_edge_t; 51 | struct _ccd_pt_face_t; 52 | 53 | /** 54 | * Polytope's vertex. 55 | */ 56 | struct _ccd_pt_vertex_t { 57 | __CCD_PT_EL 58 | 59 | int id; 60 | ccd_support_t v; 61 | ccd_list_t edges; //!< List of edges 62 | }; 63 | typedef struct _ccd_pt_vertex_t ccd_pt_vertex_t; 64 | 65 | /** 66 | * Polytope's edge. 67 | */ 68 | struct _ccd_pt_edge_t { 69 | __CCD_PT_EL 70 | 71 | ccd_pt_vertex_t *vertex[2]; //!< Reference to vertices 72 | struct _ccd_pt_face_t *faces[2]; //!< Reference to faces 73 | 74 | ccd_list_t vertex_list[2]; //!< List items in vertices' lists 75 | }; 76 | typedef struct _ccd_pt_edge_t ccd_pt_edge_t; 77 | 78 | /** 79 | * Polytope's triangle faces. 80 | */ 81 | struct _ccd_pt_face_t { 82 | __CCD_PT_EL 83 | 84 | ccd_pt_edge_t *edge[3]; //!< Reference to surrounding edges 85 | }; 86 | typedef struct _ccd_pt_face_t ccd_pt_face_t; 87 | 88 | 89 | /** 90 | * Struct containing polytope. 91 | */ 92 | struct _ccd_pt_t { 93 | ccd_list_t vertices; //!< List of vertices 94 | ccd_list_t edges; //!< List of edges 95 | ccd_list_t faces; //!< List of faces 96 | 97 | ccd_pt_el_t *nearest; 98 | ccd_real_t nearest_dist; 99 | int nearest_type; 100 | }; 101 | typedef struct _ccd_pt_t ccd_pt_t; 102 | 103 | 104 | CCD_EXPORT void ccdPtInit(ccd_pt_t *pt); 105 | CCD_EXPORT void ccdPtDestroy(ccd_pt_t *pt); 106 | 107 | /** 108 | * Returns vertices surrounding given triangle face. 109 | */ 110 | _ccd_inline void ccdPtFaceVec3(const ccd_pt_face_t *face, 111 | ccd_vec3_t **a, 112 | ccd_vec3_t **b, 113 | ccd_vec3_t **c); 114 | _ccd_inline void ccdPtFaceVertices(const ccd_pt_face_t *face, 115 | ccd_pt_vertex_t **a, 116 | ccd_pt_vertex_t **b, 117 | ccd_pt_vertex_t **c); 118 | _ccd_inline void ccdPtFaceEdges(const ccd_pt_face_t *f, 119 | ccd_pt_edge_t **a, 120 | ccd_pt_edge_t **b, 121 | ccd_pt_edge_t **c); 122 | 123 | _ccd_inline void ccdPtEdgeVec3(const ccd_pt_edge_t *e, 124 | ccd_vec3_t **a, 125 | ccd_vec3_t **b); 126 | _ccd_inline void ccdPtEdgeVertices(const ccd_pt_edge_t *e, 127 | ccd_pt_vertex_t **a, 128 | ccd_pt_vertex_t **b); 129 | _ccd_inline void ccdPtEdgeFaces(const ccd_pt_edge_t *e, 130 | ccd_pt_face_t **f1, 131 | ccd_pt_face_t **f2); 132 | 133 | 134 | /** 135 | * Adds vertex to polytope and returns pointer to newly created vertex. 136 | */ 137 | CCD_EXPORT ccd_pt_vertex_t *ccdPtAddVertex(ccd_pt_t *pt, const ccd_support_t *v); 138 | _ccd_inline ccd_pt_vertex_t *ccdPtAddVertexCoords(ccd_pt_t *pt, 139 | ccd_real_t x, ccd_real_t y, ccd_real_t z); 140 | 141 | /** 142 | * Adds edge to polytope. 143 | */ 144 | CCD_EXPORT ccd_pt_edge_t *ccdPtAddEdge(ccd_pt_t *pt, ccd_pt_vertex_t *v1, 145 | ccd_pt_vertex_t *v2); 146 | 147 | /** 148 | * Adds face to polytope. 149 | */ 150 | CCD_EXPORT ccd_pt_face_t *ccdPtAddFace(ccd_pt_t *pt, ccd_pt_edge_t *e1, 151 | ccd_pt_edge_t *e2, 152 | ccd_pt_edge_t *e3); 153 | 154 | /** 155 | * Deletes vertex from polytope. 156 | * Returns 0 on success, -1 otherwise. 157 | */ 158 | _ccd_inline int ccdPtDelVertex(ccd_pt_t *pt, ccd_pt_vertex_t *); 159 | _ccd_inline int ccdPtDelEdge(ccd_pt_t *pt, ccd_pt_edge_t *); 160 | _ccd_inline int ccdPtDelFace(ccd_pt_t *pt, ccd_pt_face_t *); 161 | 162 | 163 | /** 164 | * Recompute distances from origin for all elements in pt. 165 | */ 166 | CCD_EXPORT void ccdPtRecomputeDistances(ccd_pt_t *pt); 167 | 168 | /** 169 | * Returns nearest element to origin. 170 | */ 171 | CCD_EXPORT ccd_pt_el_t *ccdPtNearest(ccd_pt_t *pt); 172 | 173 | 174 | CCD_EXPORT void ccdPtDumpSVT(ccd_pt_t *pt, const char *fn); 175 | CCD_EXPORT void ccdPtDumpSVT2(ccd_pt_t *pt, FILE *); 176 | 177 | 178 | /**** INLINES ****/ 179 | _ccd_inline ccd_pt_vertex_t *ccdPtAddVertexCoords(ccd_pt_t *pt, 180 | ccd_real_t x, ccd_real_t y, ccd_real_t z) 181 | { 182 | ccd_support_t s; 183 | ccdVec3Set(&s.v, x, y, z); 184 | return ccdPtAddVertex(pt, &s); 185 | } 186 | 187 | _ccd_inline int ccdPtDelVertex(ccd_pt_t *pt, ccd_pt_vertex_t *v) 188 | { 189 | // test if any edge is connected to this vertex 190 | if (!ccdListEmpty(&v->edges)) 191 | return -1; 192 | 193 | // delete vertex from main list 194 | ccdListDel(&v->list); 195 | 196 | if ((void *)pt->nearest == (void *)v){ 197 | pt->nearest = NULL; 198 | } 199 | 200 | free(v); 201 | return 0; 202 | } 203 | 204 | _ccd_inline int ccdPtDelEdge(ccd_pt_t *pt, ccd_pt_edge_t *e) 205 | { 206 | // text if any face is connected to this edge (faces[] is always 207 | // aligned to lower indices) 208 | if (e->faces[0] != NULL) 209 | return -1; 210 | 211 | // disconnect edge from lists of edges in vertex struct 212 | ccdListDel(&e->vertex_list[0]); 213 | ccdListDel(&e->vertex_list[1]); 214 | 215 | // disconnect edge from main list 216 | ccdListDel(&e->list); 217 | 218 | if ((void *)pt->nearest == (void *)e){ 219 | pt->nearest = NULL; 220 | } 221 | 222 | free(e); 223 | return 0; 224 | } 225 | 226 | _ccd_inline int ccdPtDelFace(ccd_pt_t *pt, ccd_pt_face_t *f) 227 | { 228 | ccd_pt_edge_t *e; 229 | size_t i; 230 | 231 | // remove face from edges' recerence lists 232 | for (i = 0; i < 3; i++){ 233 | e = f->edge[i]; 234 | if (e->faces[0] == f){ 235 | e->faces[0] = e->faces[1]; 236 | } 237 | e->faces[1] = NULL; 238 | } 239 | 240 | // remove face from list of all faces 241 | ccdListDel(&f->list); 242 | 243 | if ((void *)pt->nearest == (void *)f){ 244 | pt->nearest = NULL; 245 | } 246 | 247 | free(f); 248 | return 0; 249 | } 250 | 251 | _ccd_inline void ccdPtFaceVec3(const ccd_pt_face_t *face, 252 | ccd_vec3_t **a, 253 | ccd_vec3_t **b, 254 | ccd_vec3_t **c) 255 | { 256 | *a = &face->edge[0]->vertex[0]->v.v; 257 | *b = &face->edge[0]->vertex[1]->v.v; 258 | 259 | if (face->edge[1]->vertex[0] != face->edge[0]->vertex[0] 260 | && face->edge[1]->vertex[0] != face->edge[0]->vertex[1]){ 261 | *c = &face->edge[1]->vertex[0]->v.v; 262 | }else{ 263 | *c = &face->edge[1]->vertex[1]->v.v; 264 | } 265 | } 266 | 267 | _ccd_inline void ccdPtFaceVertices(const ccd_pt_face_t *face, 268 | ccd_pt_vertex_t **a, 269 | ccd_pt_vertex_t **b, 270 | ccd_pt_vertex_t **c) 271 | { 272 | *a = face->edge[0]->vertex[0]; 273 | *b = face->edge[0]->vertex[1]; 274 | 275 | if (face->edge[1]->vertex[0] != face->edge[0]->vertex[0] 276 | && face->edge[1]->vertex[0] != face->edge[0]->vertex[1]){ 277 | *c = face->edge[1]->vertex[0]; 278 | }else{ 279 | *c = face->edge[1]->vertex[1]; 280 | } 281 | } 282 | 283 | _ccd_inline void ccdPtFaceEdges(const ccd_pt_face_t *f, 284 | ccd_pt_edge_t **a, 285 | ccd_pt_edge_t **b, 286 | ccd_pt_edge_t **c) 287 | { 288 | *a = f->edge[0]; 289 | *b = f->edge[1]; 290 | *c = f->edge[2]; 291 | } 292 | 293 | _ccd_inline void ccdPtEdgeVec3(const ccd_pt_edge_t *e, 294 | ccd_vec3_t **a, 295 | ccd_vec3_t **b) 296 | { 297 | *a = &e->vertex[0]->v.v; 298 | *b = &e->vertex[1]->v.v; 299 | } 300 | 301 | _ccd_inline void ccdPtEdgeVertices(const ccd_pt_edge_t *e, 302 | ccd_pt_vertex_t **a, 303 | ccd_pt_vertex_t **b) 304 | { 305 | *a = e->vertex[0]; 306 | *b = e->vertex[1]; 307 | } 308 | 309 | _ccd_inline void ccdPtEdgeFaces(const ccd_pt_edge_t *e, 310 | ccd_pt_face_t **f1, 311 | ccd_pt_face_t **f2) 312 | { 313 | *f1 = e->faces[0]; 314 | *f2 = e->faces[1]; 315 | } 316 | 317 | 318 | #ifdef __cplusplus 319 | } /* extern "C" */ 320 | #endif /* __cplusplus */ 321 | 322 | #endif /* __CCD_POLYTOPE_H__ */ 323 | -------------------------------------------------------------------------------- /src/simplex.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_SIMPLEX_H__ 19 | #define __CCD_SIMPLEX_H__ 20 | 21 | #include 22 | #include "support.h" 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif /* __cplusplus */ 27 | 28 | struct _ccd_simplex_t { 29 | ccd_support_t ps[4]; 30 | int last; //!< index of last added point 31 | }; 32 | typedef struct _ccd_simplex_t ccd_simplex_t; 33 | 34 | 35 | _ccd_inline void ccdSimplexInit(ccd_simplex_t *s); 36 | _ccd_inline int ccdSimplexSize(const ccd_simplex_t *s); 37 | _ccd_inline const ccd_support_t *ccdSimplexLast(const ccd_simplex_t *s); 38 | _ccd_inline const ccd_support_t *ccdSimplexPoint(const ccd_simplex_t *s, int idx); 39 | _ccd_inline ccd_support_t *ccdSimplexPointW(ccd_simplex_t *s, int idx); 40 | 41 | _ccd_inline void ccdSimplexAdd(ccd_simplex_t *s, const ccd_support_t *v); 42 | _ccd_inline void ccdSimplexSet(ccd_simplex_t *s, size_t pos, const ccd_support_t *a); 43 | _ccd_inline void ccdSimplexSetSize(ccd_simplex_t *s, int size); 44 | _ccd_inline void ccdSimplexSwap(ccd_simplex_t *s, size_t pos1, size_t pos2); 45 | 46 | 47 | /**** INLINES ****/ 48 | 49 | _ccd_inline void ccdSimplexInit(ccd_simplex_t *s) 50 | { 51 | s->last = -1; 52 | } 53 | 54 | _ccd_inline int ccdSimplexSize(const ccd_simplex_t *s) 55 | { 56 | return s->last + 1; 57 | } 58 | 59 | _ccd_inline const ccd_support_t *ccdSimplexLast(const ccd_simplex_t *s) 60 | { 61 | return ccdSimplexPoint(s, s->last); 62 | } 63 | 64 | _ccd_inline const ccd_support_t *ccdSimplexPoint(const ccd_simplex_t *s, int idx) 65 | { 66 | // here is no check on boundaries 67 | return &s->ps[idx]; 68 | } 69 | _ccd_inline ccd_support_t *ccdSimplexPointW(ccd_simplex_t *s, int idx) 70 | { 71 | return &s->ps[idx]; 72 | } 73 | 74 | _ccd_inline void ccdSimplexAdd(ccd_simplex_t *s, const ccd_support_t *v) 75 | { 76 | // here is no check on boundaries in sake of speed 77 | ++s->last; 78 | ccdSupportCopy(s->ps + s->last, v); 79 | } 80 | 81 | _ccd_inline void ccdSimplexSet(ccd_simplex_t *s, size_t pos, const ccd_support_t *a) 82 | { 83 | ccdSupportCopy(s->ps + pos, a); 84 | } 85 | 86 | _ccd_inline void ccdSimplexSetSize(ccd_simplex_t *s, int size) 87 | { 88 | s->last = size - 1; 89 | } 90 | 91 | _ccd_inline void ccdSimplexSwap(ccd_simplex_t *s, size_t pos1, size_t pos2) 92 | { 93 | ccd_support_t supp; 94 | 95 | ccdSupportCopy(&supp, &s->ps[pos1]); 96 | ccdSupportCopy(&s->ps[pos1], &s->ps[pos2]); 97 | ccdSupportCopy(&s->ps[pos2], &supp); 98 | } 99 | 100 | #ifdef __cplusplus 101 | } /* extern "C" */ 102 | #endif /* __cplusplus */ 103 | 104 | #endif /* __CCD_SIMPLEX_H__ */ 105 | -------------------------------------------------------------------------------- /src/support.c: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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 | #include "support.h" 19 | 20 | void __ccdSupport(const void *obj1, const void *obj2, 21 | const ccd_vec3_t *_dir, const ccd_t *ccd, 22 | ccd_support_t *supp) 23 | { 24 | ccd_vec3_t dir; 25 | 26 | ccdVec3Copy(&dir, _dir); 27 | 28 | ccd->support1(obj1, &dir, &supp->v1); 29 | 30 | ccdVec3Scale(&dir, -CCD_ONE); 31 | ccd->support2(obj2, &dir, &supp->v2); 32 | 33 | ccdVec3Sub2(&supp->v, &supp->v1, &supp->v2); 34 | } 35 | -------------------------------------------------------------------------------- /src/support.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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_SUPPORT_H__ 19 | #define __CCD_SUPPORT_H__ 20 | 21 | #include 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif /* __cplusplus */ 26 | 27 | struct _ccd_support_t { 28 | ccd_vec3_t v; //!< Support point in minkowski sum 29 | ccd_vec3_t v1; //!< Support point in obj1 30 | ccd_vec3_t v2; //!< Support point in obj2 31 | }; 32 | typedef struct _ccd_support_t ccd_support_t; 33 | 34 | _ccd_inline void ccdSupportCopy(ccd_support_t *, const ccd_support_t *s); 35 | 36 | /** 37 | * Computes support point of obj1 and obj2 in direction dir. 38 | * Support point is returned via supp. 39 | */ 40 | CCD_EXPORT void __ccdSupport(const void *obj1, const void *obj2, 41 | const ccd_vec3_t *dir, const ccd_t *ccd, 42 | ccd_support_t *supp); 43 | 44 | 45 | /**** INLINES ****/ 46 | _ccd_inline void ccdSupportCopy(ccd_support_t *d, const ccd_support_t *s) 47 | { 48 | *d = *s; 49 | } 50 | 51 | #ifdef __cplusplus 52 | } /* extern "C" */ 53 | #endif /* __cplusplus */ 54 | 55 | #endif /* __CCD_SUPPORT_H__ */ 56 | -------------------------------------------------------------------------------- /src/testsuites/.gitignore: -------------------------------------------------------------------------------- 1 | regressions/tmp.* 2 | bench-out/* 3 | *.o 4 | test 5 | bench 6 | bench2 7 | -------------------------------------------------------------------------------- /src/testsuites/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(cu) 2 | 3 | set(MAIN_SOURCES 4 | main.c 5 | common.c 6 | common.h 7 | support.c 8 | support.h 9 | vec3.c 10 | vec3.h 11 | polytope.c 12 | polytope.h 13 | boxbox.c 14 | boxbox.h 15 | spheresphere.c 16 | spheresphere.h 17 | cylcyl.c 18 | cylcyl.h 19 | boxcyl.c 20 | boxcyl.h 21 | mpr_boxbox.c 22 | mpr_boxbox.h 23 | mpr_cylcyl.c 24 | mpr_cylcyl.h 25 | mpr_boxcyl.c 26 | mpr_boxcyl.h 27 | support.c 28 | support.h) 29 | 30 | add_executable(main ${MAIN_SOURCES}) 31 | target_link_libraries(main ccd cu) 32 | add_test(NAME main COMMAND main) 33 | 34 | 35 | if(NOT APPLE) 36 | set(BENCH_SOURCES 37 | bench.c 38 | support.c 39 | support.h) 40 | 41 | add_executable(bench ${BENCH_SOURCES}) 42 | target_link_libraries(bench ccd cu) 43 | add_test(NAME bench COMMAND bench) 44 | 45 | set(BENCH2_SOURCES 46 | bench2.c 47 | support.c 48 | support.h) 49 | 50 | add_executable(bench2 ${BENCH2_SOURCES}) 51 | target_link_libraries(bench2 ccd cu) 52 | add_test(NAME bench2 COMMAND bench2) 53 | endif() 54 | -------------------------------------------------------------------------------- /src/testsuites/Makefile: -------------------------------------------------------------------------------- 1 | # force some options 2 | DEBUG = yes 3 | 4 | -include ../Makefile.include 5 | CFLAGS += -I./ -I../ -Icu/ 6 | LDFLAGS += -L./ -Lcu/ -lcu -lrt -lm -L../ -lccd 7 | 8 | CHECK_REG=cu/check-regressions 9 | CHECK_TS ?= 10 | 11 | OBJS = common.o support.o vec3.o polytope.o boxbox.o spheresphere.o \ 12 | cylcyl.o boxcyl.o mpr_boxbox.o mpr_cylcyl.o mpr_boxcyl.o 13 | BENCH_OBJS = bench-boxbox.o 14 | 15 | 16 | all: test bench bench2 17 | 18 | test: cu $(OBJS) main.c 19 | $(CC) $(CFLAGS) -o $@ main.c $(OBJS) $(LDFLAGS) 20 | 21 | bench: cu bench.c support.o 22 | $(CC) $(CFLAGS) -o $@ bench.c support.o $(LDFLAGS) 23 | bench2: cu bench2.c support.o 24 | $(CC) $(CFLAGS) -o $@ bench2.c support.o $(LDFLAGS) 25 | 26 | %.o: %.c %.h 27 | $(CC) $(CFLAGS) -c -o $@ $< 28 | %.o: %.c 29 | $(CC) $(CFLAGS) -c -o $@ $< 30 | 31 | check: all 32 | @echo "" 33 | @echo "----------------------------------------"; 34 | ./test $(CHECK_TS) 35 | @echo "----------------------------------------"; 36 | @echo "Checking regressions:"; 37 | $(PYTHON) $(CHECK_REG) regressions 38 | @echo "" 39 | 40 | check-valgrind: all 41 | valgrind -q --leak-check=full --show-reachable=yes --trace-children=yes \ 42 | --error-limit=no \ 43 | ./test $(CHECK_TS) 44 | 45 | check-valgrind-gen-suppressions: all 46 | valgrind -q --leak-check=full --show-reachable=yes --trace-children=yes \ 47 | --gen-suppressions=all --log-file=out --error-limit=no \ 48 | ./test $(CHECK_TS) 49 | 50 | cu: 51 | $(MAKE) ENABLE_TIMER=yes -C cu/ 52 | 53 | clean: 54 | rm -f *.o 55 | rm -f objs/*.o 56 | rm -f test bench bench2 57 | rm -f tmp.* 58 | rm -f regressions/tmp.* 59 | 60 | .PHONY: all clean check check-valgrind cu 61 | 62 | -------------------------------------------------------------------------------- /src/testsuites/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = cu 2 | 3 | AM_CPPFLAGS = -I $(srcdir)/.. -I $(builddir)/.. -I $(srcdir)/cu 4 | 5 | LDADD = $(builddir)/cu/libcu.la $(builddir)/../libccd.la 6 | 7 | 8 | check_PROGRAMS = test bench bench2 9 | 10 | test_SOURCES = main.c \ 11 | common.c common.h \ 12 | support.c support.h \ 13 | vec3.c vec3.h \ 14 | polytope.c polytope.h \ 15 | boxbox.c boxbox.h \ 16 | spheresphere.c spheresphere.h \ 17 | cylcyl.c cylcyl.h \ 18 | boxcyl.c boxcyl.h \ 19 | mpr_boxbox.c mpr_boxbox.h \ 20 | mpr_cylcyl.c mpr_cylcyl.h \ 21 | mpr_boxcyl.c mpr_boxcyl.h 22 | 23 | bench_SOURCES = bench.c \ 24 | support.c support.h 25 | 26 | bench2_SOURCES = bench2.c \ 27 | support.c support.h 28 | 29 | -------------------------------------------------------------------------------- /src/testsuites/bench.c: -------------------------------------------------------------------------------- 1 | #define CU_ENABLE_TIMER 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "support.h" 7 | 8 | TEST_SUITES { 9 | TEST_SUITES_CLOSURE 10 | }; 11 | 12 | static int bench_num = 1; 13 | static size_t cycles = 10000; 14 | 15 | static void runBench(const void *o1, const void *o2, const ccd_t *ccd) 16 | { 17 | ccd_real_t depth; 18 | ccd_vec3_t dir, pos; 19 | size_t i; 20 | const struct timespec *timer; 21 | 22 | cuTimerStart(); 23 | for (i = 0; i < cycles; i++){ 24 | ccdGJKPenetration(o1, o2, ccd, &depth, &dir, &pos); 25 | } 26 | timer = cuTimerStop(); 27 | fprintf(stdout, "%02d: %ld %ld\n", bench_num, 28 | (long)timer->tv_sec, (long)timer->tv_nsec); 29 | fflush(stdout); 30 | 31 | bench_num++; 32 | } 33 | 34 | static void boxbox(void) 35 | { 36 | fprintf(stdout, "%s:\n", __func__); 37 | 38 | ccd_t ccd; 39 | CCD_BOX(box1); 40 | CCD_BOX(box2); 41 | ccd_vec3_t axis; 42 | ccd_quat_t rot; 43 | 44 | box1.x = box1.y = box1.z = 1.; 45 | box2.x = 0.5; 46 | box2.y = 1.; 47 | box2.z = 1.5; 48 | 49 | bench_num = 1; 50 | 51 | CCD_INIT(&ccd); 52 | ccd.support1 = ccdSupport; 53 | ccd.support2 = ccdSupport; 54 | 55 | runBench(&box1, &box2, &ccd); 56 | runBench(&box2, &box1, &ccd); 57 | 58 | ccdVec3Set(&box1.pos, -0.3, 0.5, 1.); 59 | runBench(&box1, &box2, &ccd); 60 | runBench(&box2, &box1, &ccd); 61 | 62 | box1.x = box1.y = box1.z = 1.; 63 | box2.x = box2.y = box2.z = 1.; 64 | ccdVec3Set(&axis, 0., 0., 1.); 65 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 66 | ccdVec3Set(&box1.pos, 0., 0., 0.); 67 | runBench(&box1, &box2, &ccd); 68 | runBench(&box2, &box1, &ccd); 69 | 70 | box1.x = box1.y = box1.z = 1.; 71 | box2.x = box2.y = box2.z = 1.; 72 | ccdVec3Set(&axis, 0., 0., 1.); 73 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 74 | ccdVec3Set(&box1.pos, -0.5, 0., 0.); 75 | runBench(&box1, &box2, &ccd); 76 | runBench(&box2, &box1, &ccd); 77 | 78 | box1.x = box1.y = box1.z = 1.; 79 | box2.x = box2.y = box2.z = 1.; 80 | ccdVec3Set(&axis, 0., 0., 1.); 81 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 82 | ccdVec3Set(&box1.pos, -0.5, 0.5, 0.); 83 | runBench(&box1, &box2, &ccd); 84 | runBench(&box2, &box1, &ccd); 85 | 86 | box1.x = box1.y = box1.z = 1.; 87 | ccdVec3Set(&axis, 0., 1., 1.); 88 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 89 | ccdVec3Set(&box1.pos, -0.5, 0.1, 0.4); 90 | runBench(&box1, &box2, &ccd); 91 | runBench(&box2, &box1, &ccd); 92 | 93 | box1.x = box1.y = box1.z = 1.; 94 | ccdVec3Set(&axis, 0., 1., 1.); 95 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 96 | ccdVec3Set(&axis, 1., 1., 1.); 97 | ccdQuatSetAngleAxis(&rot, M_PI / 4., &axis); 98 | ccdQuatMul(&box1.quat, &rot); 99 | ccdVec3Set(&box1.pos, -0.5, 0.1, 0.4); 100 | runBench(&box1, &box2, &ccd); 101 | runBench(&box2, &box1, &ccd); 102 | 103 | 104 | box1.x = box1.y = box1.z = 1.; 105 | box2.x = 0.2; box2.y = 0.5; box2.z = 1.; 106 | box2.x = box2.y = box2.z = 1.; 107 | 108 | ccdVec3Set(&axis, 0., 0., 1.); 109 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 110 | ccdVec3Set(&axis, 1., 0., 0.); 111 | ccdQuatSetAngleAxis(&rot, M_PI / 4., &axis); 112 | ccdQuatMul(&box1.quat, &rot); 113 | ccdVec3Set(&box1.pos, -1.3, 0., 0.); 114 | 115 | ccdVec3Set(&box2.pos, 0., 0., 0.); 116 | runBench(&box1, &box2, &ccd); 117 | runBench(&box2, &box1, &ccd); 118 | 119 | 120 | fprintf(stdout, "\n----\n\n"); 121 | } 122 | 123 | void cylcyl(void) 124 | { 125 | fprintf(stdout, "%s:\n", __func__); 126 | 127 | ccd_t ccd; 128 | CCD_CYL(cyl1); 129 | CCD_CYL(cyl2); 130 | ccd_vec3_t axis; 131 | 132 | cyl1.radius = 0.35; 133 | cyl1.height = 0.5; 134 | cyl2.radius = 0.5; 135 | cyl2.height = 1.; 136 | 137 | CCD_INIT(&ccd); 138 | ccd.support1 = ccdSupport; 139 | ccd.support2 = ccdSupport; 140 | 141 | runBench(&cyl1, &cyl2, &ccd); 142 | runBench(&cyl2, &cyl1, &ccd); 143 | 144 | ccdVec3Set(&cyl1.pos, 0.3, 0.1, 0.1); 145 | runBench(&cyl1, &cyl2, &ccd); 146 | runBench(&cyl2, &cyl1, &ccd); 147 | 148 | ccdVec3Set(&axis, 0., 1., 1.); 149 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 150 | ccdVec3Set(&cyl2.pos, 0., 0., 0.); 151 | runBench(&cyl1, &cyl2, &ccd); 152 | runBench(&cyl2, &cyl1, &ccd); 153 | 154 | ccdVec3Set(&axis, 0., 1., 1.); 155 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 156 | ccdVec3Set(&cyl2.pos, -0.2, 0.7, 0.2); 157 | runBench(&cyl1, &cyl2, &ccd); 158 | runBench(&cyl2, &cyl1, &ccd); 159 | 160 | ccdVec3Set(&axis, 0.567, 1.2, 1.); 161 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 162 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 163 | runBench(&cyl1, &cyl2, &ccd); 164 | runBench(&cyl2, &cyl1, &ccd); 165 | 166 | ccdVec3Set(&axis, -4.567, 1.2, 0.); 167 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 3., &axis); 168 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 169 | runBench(&cyl1, &cyl2, &ccd); 170 | runBench(&cyl2, &cyl1, &ccd); 171 | 172 | fprintf(stdout, "\n----\n\n"); 173 | } 174 | 175 | void boxcyl(void) 176 | { 177 | fprintf(stdout, "%s:\n", __func__); 178 | 179 | ccd_t ccd; 180 | CCD_BOX(box); 181 | CCD_CYL(cyl); 182 | ccd_vec3_t axis; 183 | 184 | box.x = 0.5; 185 | box.y = 1.; 186 | box.z = 1.5; 187 | cyl.radius = 0.4; 188 | cyl.height = 0.7; 189 | 190 | CCD_INIT(&ccd); 191 | ccd.support1 = ccdSupport; 192 | ccd.support2 = ccdSupport; 193 | 194 | runBench(&box, &cyl, &ccd); 195 | runBench(&cyl, &box, &ccd); 196 | 197 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 198 | runBench(&box, &cyl, &ccd); 199 | runBench(&cyl, &box, &ccd); 200 | 201 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 202 | runBench(&box, &cyl, &ccd); 203 | runBench(&cyl, &box, &ccd); 204 | 205 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 206 | runBench(&box, &cyl, &ccd); 207 | runBench(&cyl, &box, &ccd); 208 | 209 | ccdVec3Set(&axis, 0., 1., 0.); 210 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 211 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 212 | runBench(&box, &cyl, &ccd); 213 | runBench(&cyl, &box, &ccd); 214 | 215 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 216 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 217 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 218 | runBench(&box, &cyl, &ccd); 219 | runBench(&cyl, &box, &ccd); 220 | 221 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 222 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 223 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 224 | ccdVec3Set(&axis, 1., 1., 0.); 225 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 226 | ccdVec3Set(&box.pos, .6, 0., 0.5); 227 | runBench(&box, &cyl, &ccd); 228 | runBench(&cyl, &box, &ccd); 229 | 230 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 231 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 232 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 233 | ccdVec3Set(&axis, 1., 1., 0.); 234 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 235 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 236 | runBench(&box, &cyl, &ccd); 237 | runBench(&cyl, &box, &ccd); 238 | 239 | fprintf(stdout, "\n----\n\n"); 240 | } 241 | 242 | int main(int argc, char *argv[]) 243 | { 244 | if (argc > 1){ 245 | cycles = atol(argv[1]); 246 | } 247 | 248 | fprintf(stdout, "Cycles: %u\n", (unsigned int)cycles); 249 | fprintf(stdout, "\n"); 250 | 251 | boxbox(); 252 | cylcyl(); 253 | boxcyl(); 254 | 255 | return 0; 256 | } 257 | -------------------------------------------------------------------------------- /src/testsuites/bench2.c: -------------------------------------------------------------------------------- 1 | #define CU_ENABLE_TIMER 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "support.h" 7 | 8 | TEST_SUITES { 9 | TEST_SUITES_CLOSURE 10 | }; 11 | 12 | static int bench_num = 1; 13 | static size_t cycles = 10000; 14 | 15 | static void runBench(const void *o1, const void *o2, const ccd_t *ccd) 16 | { 17 | ccd_real_t depth; 18 | ccd_vec3_t dir, pos; 19 | size_t i; 20 | const struct timespec *timer; 21 | 22 | cuTimerStart(); 23 | for (i = 0; i < cycles; i++){ 24 | ccdMPRPenetration(o1, o2, ccd, &depth, &dir, &pos); 25 | } 26 | timer = cuTimerStop(); 27 | fprintf(stdout, "%02d: %ld %ld\n", bench_num, 28 | (long)timer->tv_sec, (long)timer->tv_nsec); 29 | fflush(stdout); 30 | 31 | bench_num++; 32 | } 33 | 34 | static void boxbox(void) 35 | { 36 | fprintf(stdout, "%s:\n", __func__); 37 | 38 | ccd_t ccd; 39 | CCD_BOX(box1); 40 | CCD_BOX(box2); 41 | ccd_vec3_t axis; 42 | ccd_quat_t rot; 43 | 44 | box1.x = box1.y = box1.z = 1.; 45 | box2.x = 0.5; 46 | box2.y = 1.; 47 | box2.z = 1.5; 48 | 49 | bench_num = 1; 50 | 51 | CCD_INIT(&ccd); 52 | ccd.support1 = ccdSupport; 53 | ccd.support2 = ccdSupport; 54 | ccd.center1 = ccdObjCenter; 55 | ccd.center2 = ccdObjCenter; 56 | 57 | runBench(&box1, &box2, &ccd); 58 | runBench(&box2, &box1, &ccd); 59 | 60 | ccdVec3Set(&box1.pos, -0.3, 0.5, 1.); 61 | runBench(&box1, &box2, &ccd); 62 | runBench(&box2, &box1, &ccd); 63 | 64 | box1.x = box1.y = box1.z = 1.; 65 | box2.x = box2.y = box2.z = 1.; 66 | ccdVec3Set(&axis, 0., 0., 1.); 67 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 68 | ccdVec3Set(&box1.pos, 0., 0., 0.); 69 | runBench(&box1, &box2, &ccd); 70 | runBench(&box2, &box1, &ccd); 71 | 72 | box1.x = box1.y = box1.z = 1.; 73 | box2.x = box2.y = box2.z = 1.; 74 | ccdVec3Set(&axis, 0., 0., 1.); 75 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 76 | ccdVec3Set(&box1.pos, -0.5, 0., 0.); 77 | runBench(&box1, &box2, &ccd); 78 | runBench(&box2, &box1, &ccd); 79 | 80 | box1.x = box1.y = box1.z = 1.; 81 | box2.x = box2.y = box2.z = 1.; 82 | ccdVec3Set(&axis, 0., 0., 1.); 83 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 84 | ccdVec3Set(&box1.pos, -0.5, 0.5, 0.); 85 | runBench(&box1, &box2, &ccd); 86 | runBench(&box2, &box1, &ccd); 87 | 88 | box1.x = box1.y = box1.z = 1.; 89 | ccdVec3Set(&axis, 0., 1., 1.); 90 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 91 | ccdVec3Set(&box1.pos, -0.5, 0.1, 0.4); 92 | runBench(&box1, &box2, &ccd); 93 | runBench(&box2, &box1, &ccd); 94 | 95 | box1.x = box1.y = box1.z = 1.; 96 | ccdVec3Set(&axis, 0., 1., 1.); 97 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 98 | ccdVec3Set(&axis, 1., 1., 1.); 99 | ccdQuatSetAngleAxis(&rot, M_PI / 4., &axis); 100 | ccdQuatMul(&box1.quat, &rot); 101 | ccdVec3Set(&box1.pos, -0.5, 0.1, 0.4); 102 | runBench(&box1, &box2, &ccd); 103 | runBench(&box2, &box1, &ccd); 104 | 105 | 106 | box1.x = box1.y = box1.z = 1.; 107 | box2.x = 0.2; box2.y = 0.5; box2.z = 1.; 108 | box2.x = box2.y = box2.z = 1.; 109 | 110 | ccdVec3Set(&axis, 0., 0., 1.); 111 | ccdQuatSetAngleAxis(&box1.quat, M_PI / 4., &axis); 112 | ccdVec3Set(&axis, 1., 0., 0.); 113 | ccdQuatSetAngleAxis(&rot, M_PI / 4., &axis); 114 | ccdQuatMul(&box1.quat, &rot); 115 | ccdVec3Set(&box1.pos, -1.3, 0., 0.); 116 | 117 | ccdVec3Set(&box2.pos, 0., 0., 0.); 118 | runBench(&box1, &box2, &ccd); 119 | runBench(&box2, &box1, &ccd); 120 | 121 | 122 | fprintf(stdout, "\n----\n\n"); 123 | } 124 | 125 | void cylcyl(void) 126 | { 127 | fprintf(stdout, "%s:\n", __func__); 128 | 129 | ccd_t ccd; 130 | CCD_CYL(cyl1); 131 | CCD_CYL(cyl2); 132 | ccd_vec3_t axis; 133 | 134 | cyl1.radius = 0.35; 135 | cyl1.height = 0.5; 136 | cyl2.radius = 0.5; 137 | cyl2.height = 1.; 138 | 139 | CCD_INIT(&ccd); 140 | ccd.support1 = ccdSupport; 141 | ccd.support2 = ccdSupport; 142 | ccd.center1 = ccdObjCenter; 143 | ccd.center2 = ccdObjCenter; 144 | 145 | runBench(&cyl1, &cyl2, &ccd); 146 | runBench(&cyl2, &cyl1, &ccd); 147 | 148 | ccdVec3Set(&cyl1.pos, 0.3, 0.1, 0.1); 149 | runBench(&cyl1, &cyl2, &ccd); 150 | runBench(&cyl2, &cyl1, &ccd); 151 | 152 | ccdVec3Set(&axis, 0., 1., 1.); 153 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 154 | ccdVec3Set(&cyl2.pos, 0., 0., 0.); 155 | runBench(&cyl1, &cyl2, &ccd); 156 | runBench(&cyl2, &cyl1, &ccd); 157 | 158 | ccdVec3Set(&axis, 0., 1., 1.); 159 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 160 | ccdVec3Set(&cyl2.pos, -0.2, 0.7, 0.2); 161 | runBench(&cyl1, &cyl2, &ccd); 162 | runBench(&cyl2, &cyl1, &ccd); 163 | 164 | ccdVec3Set(&axis, 0.567, 1.2, 1.); 165 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 166 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 167 | runBench(&cyl1, &cyl2, &ccd); 168 | runBench(&cyl2, &cyl1, &ccd); 169 | 170 | ccdVec3Set(&axis, -4.567, 1.2, 0.); 171 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 3., &axis); 172 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 173 | runBench(&cyl1, &cyl2, &ccd); 174 | runBench(&cyl2, &cyl1, &ccd); 175 | 176 | fprintf(stdout, "\n----\n\n"); 177 | } 178 | 179 | void boxcyl(void) 180 | { 181 | fprintf(stdout, "%s:\n", __func__); 182 | 183 | ccd_t ccd; 184 | CCD_BOX(box); 185 | CCD_CYL(cyl); 186 | ccd_vec3_t axis; 187 | 188 | box.x = 0.5; 189 | box.y = 1.; 190 | box.z = 1.5; 191 | cyl.radius = 0.4; 192 | cyl.height = 0.7; 193 | 194 | CCD_INIT(&ccd); 195 | ccd.support1 = ccdSupport; 196 | ccd.support2 = ccdSupport; 197 | ccd.center1 = ccdObjCenter; 198 | ccd.center2 = ccdObjCenter; 199 | 200 | runBench(&box, &cyl, &ccd); 201 | runBench(&cyl, &box, &ccd); 202 | 203 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 204 | runBench(&box, &cyl, &ccd); 205 | runBench(&cyl, &box, &ccd); 206 | 207 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 208 | runBench(&box, &cyl, &ccd); 209 | runBench(&cyl, &box, &ccd); 210 | 211 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 212 | runBench(&box, &cyl, &ccd); 213 | runBench(&cyl, &box, &ccd); 214 | 215 | ccdVec3Set(&axis, 0., 1., 0.); 216 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 217 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 218 | runBench(&box, &cyl, &ccd); 219 | runBench(&cyl, &box, &ccd); 220 | 221 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 222 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 223 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 224 | runBench(&box, &cyl, &ccd); 225 | runBench(&cyl, &box, &ccd); 226 | 227 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 228 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 229 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 230 | ccdVec3Set(&axis, 1., 1., 0.); 231 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 232 | ccdVec3Set(&box.pos, .6, 0., 0.5); 233 | runBench(&box, &cyl, &ccd); 234 | runBench(&cyl, &box, &ccd); 235 | 236 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 237 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 238 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 239 | ccdVec3Set(&axis, 1., 1., 0.); 240 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 241 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 242 | runBench(&box, &cyl, &ccd); 243 | runBench(&cyl, &box, &ccd); 244 | 245 | fprintf(stdout, "\n----\n\n"); 246 | } 247 | 248 | int main(int argc, char *argv[]) 249 | { 250 | if (argc > 1){ 251 | cycles = atol(argv[1]); 252 | } 253 | 254 | fprintf(stdout, "Cycles: %u\n", (unsigned int)cycles); 255 | fprintf(stdout, "\n"); 256 | 257 | boxbox(); 258 | cylcyl(); 259 | boxcyl(); 260 | 261 | return 0; 262 | } 263 | -------------------------------------------------------------------------------- /src/testsuites/boxbox.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_BOX 2 | #define BOX_BOX 3 | 4 | #include 5 | 6 | TEST(boxboxSetUp); 7 | TEST(boxboxTearDown); 8 | 9 | TEST(boxboxAlignedX); 10 | TEST(boxboxAlignedY); 11 | TEST(boxboxAlignedZ); 12 | 13 | TEST(boxboxRot); 14 | 15 | TEST(boxboxSeparate); 16 | TEST(boxboxPenetration); 17 | 18 | TEST_SUITE(TSBoxBox) { 19 | TEST_ADD(boxboxSetUp), 20 | 21 | TEST_ADD(boxboxAlignedX), 22 | TEST_ADD(boxboxAlignedY), 23 | TEST_ADD(boxboxAlignedZ), 24 | TEST_ADD(boxboxRot), 25 | TEST_ADD(boxboxSeparate), 26 | TEST_ADD(boxboxPenetration), 27 | 28 | TEST_ADD(boxboxTearDown), 29 | TEST_SUITE_CLOSURE 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/testsuites/boxcyl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "support.h" 5 | 6 | 7 | #define TOSVT() \ 8 | svtObjPen(&box, &cyl, stdout, "Pen 1", depth, &dir, &pos); \ 9 | ccdVec3Scale(&dir, depth); \ 10 | ccdVec3Add(&cyl.pos, &dir); \ 11 | svtObjPen(&box, &cyl, stdout, "Pen 1", depth, &dir, &pos) 12 | 13 | 14 | TEST(boxcylIntersect) 15 | { 16 | ccd_t ccd; 17 | CCD_BOX(box); 18 | CCD_CYL(cyl); 19 | int res; 20 | ccd_vec3_t axis; 21 | 22 | box.x = 0.5; 23 | box.y = 1.; 24 | box.z = 1.5; 25 | cyl.radius = 0.4; 26 | cyl.height = 0.7; 27 | 28 | CCD_INIT(&ccd); 29 | ccd.support1 = ccdSupport; 30 | ccd.support2 = ccdSupport; 31 | 32 | ccdVec3Set(&cyl.pos, 0.1, 0., 0.); 33 | res = ccdGJKIntersect(&box, &cyl, &ccd); 34 | assertTrue(res); 35 | 36 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 37 | res = ccdGJKIntersect(&box, &cyl, &ccd); 38 | assertTrue(res); 39 | 40 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 41 | res = ccdGJKIntersect(&box, &cyl, &ccd); 42 | assertTrue(res); 43 | 44 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 45 | res = ccdGJKIntersect(&box, &cyl, &ccd); 46 | assertTrue(res); 47 | 48 | ccdVec3Set(&axis, 0., 1., 0.); 49 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 50 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 51 | res = ccdGJKIntersect(&box, &cyl, &ccd); 52 | assertTrue(res); 53 | 54 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 55 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 56 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 57 | res = ccdGJKIntersect(&box, &cyl, &ccd); 58 | assertTrue(res); 59 | 60 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 61 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 62 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 63 | ccdVec3Set(&axis, 1., 1., 0.); 64 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 65 | ccdVec3Set(&box.pos, .6, 0., 0.5); 66 | res = ccdGJKIntersect(&box, &cyl, &ccd); 67 | assertTrue(res); 68 | 69 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 70 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 71 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 72 | ccdVec3Set(&axis, 1., 1., 0.); 73 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 74 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 75 | res = ccdGJKIntersect(&box, &cyl, &ccd); 76 | assertTrue(res); 77 | } 78 | 79 | 80 | TEST(boxcylPenEPA) 81 | { 82 | ccd_t ccd; 83 | CCD_BOX(box); 84 | CCD_CYL(cyl); 85 | int res; 86 | ccd_vec3_t axis; 87 | ccd_real_t depth; 88 | ccd_vec3_t dir, pos; 89 | 90 | box.x = 0.5; 91 | box.y = 1.; 92 | box.z = 1.5; 93 | cyl.radius = 0.4; 94 | cyl.height = 0.7; 95 | 96 | CCD_INIT(&ccd); 97 | ccd.support1 = ccdSupport; 98 | ccd.support2 = ccdSupport; 99 | 100 | ccdVec3Set(&cyl.pos, 0.1, 0., 0.); 101 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 102 | assertTrue(res == 0); 103 | recPen(depth, &dir, &pos, stdout, "Pen 1"); 104 | //TOSVT(); 105 | 106 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 107 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 108 | assertTrue(res == 0); 109 | recPen(depth, &dir, &pos, stdout, "Pen 2"); 110 | //TOSVT(); <<< 111 | 112 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 113 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 114 | assertTrue(res == 0); 115 | recPen(depth, &dir, &pos, stdout, "Pen 3"); 116 | //TOSVT(); 117 | 118 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 119 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 120 | assertTrue(res == 0); 121 | recPen(depth, &dir, &pos, stdout, "Pen 4"); 122 | //TOSVT(); 123 | 124 | ccdVec3Set(&axis, 0., 1., 0.); 125 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 126 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 127 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 128 | assertTrue(res == 0); 129 | recPen(depth, &dir, &pos, stdout, "Pen 5"); 130 | //TOSVT(); 131 | 132 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 133 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 134 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 135 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 136 | assertTrue(res == 0); 137 | recPen(depth, &dir, &pos, stdout, "Pen 6"); 138 | //TOSVT(); 139 | 140 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 141 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 142 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 143 | ccdVec3Set(&axis, 1., 1., 0.); 144 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 145 | ccdVec3Set(&box.pos, .6, 0., 0.5); 146 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 147 | assertTrue(res == 0); 148 | recPen(depth, &dir, &pos, stdout, "Pen 7"); 149 | //TOSVT(); 150 | 151 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 152 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 153 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 154 | ccdVec3Set(&axis, 1., 1., 0.); 155 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 156 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 157 | res = ccdGJKPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 158 | assertTrue(res == 0); 159 | recPen(depth, &dir, &pos, stdout, "Pen 8"); 160 | //TOSVT(); 161 | } 162 | 163 | -------------------------------------------------------------------------------- /src/testsuites/boxcyl.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_BOXCYL_H 2 | #define TEST_BOXCYL_H 3 | 4 | #include 5 | 6 | TEST(boxcylIntersect); 7 | TEST(boxcylPenEPA); 8 | 9 | TEST_SUITE(TSBoxCyl){ 10 | TEST_ADD(boxcylIntersect), 11 | TEST_ADD(boxcylPenEPA), 12 | 13 | TEST_SUITE_CLOSURE 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/testsuites/common.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "support.h" 5 | 6 | static void svtCyl(ccd_cyl_t *c, FILE *out, const char *color, const char *name) 7 | { 8 | ccd_vec3_t v[32]; 9 | ccd_quat_t rot; 10 | ccd_vec3_t axis, vpos, vpos2; 11 | ccd_real_t angle, x, y; 12 | int i; 13 | 14 | ccdVec3Set(&axis, 0., 0., 1.); 15 | ccdVec3Set(&vpos, 0., c->radius, 0.); 16 | angle = 0.; 17 | for (i = 0; i < 16; i++){ 18 | angle = (ccd_real_t)i * (2. * M_PI / 16.); 19 | 20 | ccdQuatSetAngleAxis(&rot, angle, &axis); 21 | ccdVec3Copy(&vpos2, &vpos); 22 | ccdQuatRotVec(&vpos2, &rot); 23 | x = ccdVec3X(&vpos2); 24 | y = ccdVec3Y(&vpos2); 25 | 26 | ccdVec3Set(&v[i], x, y, c->height / 2.); 27 | ccdVec3Set(&v[i + 16], x, y, -c->height / 2.); 28 | } 29 | 30 | for (i = 0; i < 32; i++){ 31 | ccdQuatRotVec(&v[i], &c->quat); 32 | ccdVec3Add(&v[i], &c->pos); 33 | } 34 | 35 | fprintf(out, "-----\n"); 36 | if (name) 37 | fprintf(out, "Name: %s\n", name); 38 | 39 | fprintf(out, "Face color: %s\n", color); 40 | fprintf(out, "Edge color: %s\n", color); 41 | fprintf(out, "Point color: %s\n", color); 42 | fprintf(out, "Points:\n"); 43 | for (i = 0; i < 32; i++){ 44 | fprintf(out, "%lf %lf %lf\n", ccdVec3X(&v[i]), ccdVec3Y(&v[i]), ccdVec3Z(&v[i])); 45 | } 46 | 47 | fprintf(out, "Edges:\n"); 48 | fprintf(out, "0 16\n"); 49 | fprintf(out, "0 31\n"); 50 | for (i = 1; i < 16; i++){ 51 | fprintf(out, "0 %d\n", i); 52 | fprintf(out, "16 %d\n", i + 16); 53 | if (i != 0){ 54 | fprintf(out, "%d %d\n", i - 1, i); 55 | fprintf(out, "%d %d\n", i + 16 - 1, i + 16); 56 | } 57 | 58 | fprintf(out, "%d %d\n", i, i + 16); 59 | fprintf(out, "%d %d\n", i, i + 16 - 1); 60 | } 61 | 62 | fprintf(out, "Faces:\n"); 63 | for (i = 2; i < 16; i++){ 64 | fprintf(out, "0 %d %d\n", i, i -1); 65 | fprintf(out, "16 %d %d\n", i + 16, i + 16 -1); 66 | 67 | } 68 | fprintf(out, "0 16 31\n"); 69 | fprintf(out, "0 31 15\n"); 70 | for (i = 1; i < 16; i++){ 71 | fprintf(out, "%d %d %d\n", i, i + 16, i + 16 - 1); 72 | fprintf(out, "%d %d %d\n", i, i + 16 - 1, i - 1); 73 | } 74 | fprintf(out, "-----\n"); 75 | } 76 | 77 | static void svtBox(ccd_box_t *b, FILE *out, const char *color, const char *name) 78 | { 79 | ccd_vec3_t v[8]; 80 | size_t i; 81 | 82 | ccdVec3Set(&v[0], b->x * 0.5, b->y * 0.5, b->z * 0.5); 83 | ccdVec3Set(&v[1], b->x * 0.5, b->y * -0.5, b->z * 0.5); 84 | ccdVec3Set(&v[2], b->x * 0.5, b->y * 0.5, b->z * -0.5); 85 | ccdVec3Set(&v[3], b->x * 0.5, b->y * -0.5, b->z * -0.5); 86 | ccdVec3Set(&v[4], b->x * -0.5, b->y * 0.5, b->z * 0.5); 87 | ccdVec3Set(&v[5], b->x * -0.5, b->y * -0.5, b->z * 0.5); 88 | ccdVec3Set(&v[6], b->x * -0.5, b->y * 0.5, b->z * -0.5); 89 | ccdVec3Set(&v[7], b->x * -0.5, b->y * -0.5, b->z * -0.5); 90 | 91 | for (i = 0; i < 8; i++){ 92 | ccdQuatRotVec(&v[i], &b->quat); 93 | ccdVec3Add(&v[i], &b->pos); 94 | } 95 | 96 | fprintf(out, "-----\n"); 97 | if (name) 98 | fprintf(out, "Name: %s\n", name); 99 | fprintf(out, "Face color: %s\n", color); 100 | fprintf(out, "Edge color: %s\n", color); 101 | fprintf(out, "Point color: %s\n", color); 102 | fprintf(out, "Points:\n"); 103 | for (i = 0; i < 8; i++){ 104 | fprintf(out, "%lf %lf %lf\n", ccdVec3X(&v[i]), ccdVec3Y(&v[i]), ccdVec3Z(&v[i])); 105 | } 106 | 107 | fprintf(out, "Edges:\n"); 108 | fprintf(out, "0 1\n 0 2\n2 3\n3 1\n1 2\n6 2\n1 7\n1 5\n"); 109 | fprintf(out, "5 0\n0 4\n4 2\n6 4\n6 5\n5 7\n6 7\n7 2\n7 3\n4 5\n"); 110 | 111 | fprintf(out, "Faces:\n"); 112 | fprintf(out, "0 2 1\n1 2 3\n6 2 4\n4 2 0\n4 0 5\n5 0 1\n"); 113 | fprintf(out, "5 1 7\n7 1 3\n6 4 5\n6 5 7\n2 6 7\n2 7 3\n"); 114 | fprintf(out, "-----\n"); 115 | } 116 | 117 | 118 | void svtObj(void *_o, FILE *out, const char *color, const char *name) 119 | { 120 | ccd_obj_t *o = (ccd_obj_t *)_o; 121 | 122 | if (o->type == CCD_OBJ_CYL){ 123 | svtCyl((ccd_cyl_t *)o, out, color, name); 124 | }else if (o->type == CCD_OBJ_BOX){ 125 | svtBox((ccd_box_t *)o, out, color, name); 126 | } 127 | } 128 | 129 | void svtObjPen(void *o1, void *o2, 130 | FILE *out, const char *name, 131 | ccd_real_t depth, const ccd_vec3_t *dir, const ccd_vec3_t *pos) 132 | { 133 | ccd_vec3_t sep; 134 | char oname[500]; 135 | 136 | ccdVec3Copy(&sep, dir); 137 | ccdVec3Scale(&sep, depth); 138 | ccdVec3Add(&sep, pos); 139 | 140 | fprintf(out, "------\n"); 141 | if (name) 142 | fprintf(out, "Name: %s\n", name); 143 | fprintf(out, "Point color: 0.1 0.1 0.9\n"); 144 | fprintf(out, "Points:\n%lf %lf %lf\n", ccdVec3X(pos), ccdVec3Y(pos), ccdVec3Z(pos)); 145 | fprintf(out, "------\n"); 146 | fprintf(out, "Point color: 0.1 0.9 0.9\n"); 147 | fprintf(out, "Edge color: 0.1 0.9 0.9\n"); 148 | fprintf(out, "Points:\n%lf %lf %lf\n", ccdVec3X(pos), ccdVec3Y(pos), ccdVec3Z(pos)); 149 | fprintf(out, "%lf %lf %lf\n", ccdVec3X(&sep), ccdVec3Y(&sep), ccdVec3Z(&sep)); 150 | fprintf(out, "Edges: 0 1\n"); 151 | 152 | oname[0] = 0x0; 153 | if (name) 154 | sprintf(oname, "%s o1", name); 155 | svtObj(o1, out, "0.9 0.1 0.1", oname); 156 | 157 | oname[0] = 0x0; 158 | if (name) 159 | sprintf(oname, "%s o1", name); 160 | svtObj(o2, out, "0.1 0.9 0.1", oname); 161 | } 162 | 163 | 164 | void recPen(ccd_real_t depth, const ccd_vec3_t *dir, const ccd_vec3_t *pos, 165 | FILE *out, const char *note) 166 | { 167 | if (!note) 168 | note = ""; 169 | 170 | fprintf(out, "# %s: depth: %lf\n", note, depth); 171 | fprintf(out, "# %s: dir: [%lf %lf %lf]\n", note, ccdVec3X(dir), ccdVec3Y(dir), ccdVec3Z(dir)); 172 | fprintf(out, "# %s: pos: [%lf %lf %lf]\n", note, ccdVec3X(pos), ccdVec3Y(pos), ccdVec3Z(pos)); 173 | fprintf(out, "#\n"); 174 | } 175 | -------------------------------------------------------------------------------- /src/testsuites/common.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_COMMON 2 | #define TEST_COMMON 3 | 4 | #include 5 | #include 6 | 7 | void svtObj(void *o, FILE *out, const char *color, const char *name); 8 | void svtObjPen(void *o1, void *o2, 9 | FILE *out, const char *name, 10 | ccd_real_t depth, const ccd_vec3_t *dir, const ccd_vec3_t *pos); 11 | void recPen(ccd_real_t depth, const ccd_vec3_t *dir, const ccd_vec3_t *pos, 12 | FILE *out, const char *note); 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /src/testsuites/cu/.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/cu/.dir -------------------------------------------------------------------------------- /src/testsuites/cu/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | *.a 4 | tmp.* 5 | test 6 | 7 | -------------------------------------------------------------------------------- /src/testsuites/cu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(CU_SOURCES 2 | cu.c 3 | cu.h) 4 | 5 | add_library(cu STATIC ${CU_SOURCES}) 6 | 7 | if(NOT APPLE) 8 | target_compile_definitions(cu PUBLIC CU_ENABLE_TIMER) 9 | find_library(LIBRT_LIBRARY NAMES rt) 10 | if(NOT LIBRT_LIBRARY) 11 | message(FATAL_ERROR "Could NOT find required library librt") 12 | endif() 13 | target_link_libraries(cu "${LIBRT_LIBRARY}") 14 | endif() 15 | 16 | get_filename_component(CU_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY) 17 | target_include_directories(cu PUBLIC "${CU_INCLUDE_DIR}") 18 | -------------------------------------------------------------------------------- /src/testsuites/cu/COPYING.LESSER: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /src/testsuites/cu/Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | CFLAGS = -g -Wall -pedantic 3 | 4 | ENABLE_TIMER ?= no 5 | 6 | ifeq '$(ENABLE_TIMER)' 'yes' 7 | CFLAGS += -DCU_ENABLE_TIMER 8 | endif 9 | 10 | TARGETS = libcu.a 11 | 12 | TEST_OBJS = test.o test2.o 13 | 14 | all: $(TARGETS) 15 | 16 | libcu.a: cu.o 17 | ar cr $@ $^ 18 | ranlib $@ 19 | cu.o: cu.c cu.h 20 | $(CC) $(CFLAGS) -c -o $@ $< 21 | 22 | test: $(TEST_OBJS) libcu.a 23 | $(CC) $(CFLAGS) -o $@ $(TEST_OBJS) -L./ -lcu 24 | test-segfault: test-segfault.c libcu.a 25 | $(CC) $(CFLAGS) -o $@ $^ -L./ -lcu 26 | 27 | %.o: %.c 28 | $(CC) $(CFLAGS) -c -o $@ $< 29 | 30 | check: test test-segfault 31 | mkdir -p regressions 32 | touch regressions/testSuiteName{,2}.{out,err} 33 | touch regressions/testSuiteTest2.{out,err} 34 | -./test 35 | -cd regressions && ../check-regressions 36 | @echo "" 37 | @echo "======= SEGFAULT: =========" 38 | @echo "" 39 | -./test-segfault 40 | 41 | clean: 42 | rm -f *.o 43 | rm -f test 44 | rm -f test-segfault 45 | rm -f $(TARGETS) 46 | rm -f tmp.* 47 | rm -rf regressions 48 | 49 | .PHONY: all clean check 50 | -------------------------------------------------------------------------------- /src/testsuites/cu/Makefile.am: -------------------------------------------------------------------------------- 1 | AM_CPPFLAGS = -DCU_ENABLE_TIMER 2 | 3 | check_LTLIBRARIES = libcu.la 4 | 5 | libcu_la_SOURCES = cu.c cu.h 6 | 7 | -------------------------------------------------------------------------------- /src/testsuites/cu/cu.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * CU - C unit testing framework 3 | * --------------------------------- 4 | * Copyright (c)2007,2008,2009 Daniel Fiser 5 | * 6 | * 7 | * This file is part of CU. 8 | * 9 | * CU is free software; you can redistribute it and/or modify 10 | * it under the terms of the GNU Lesser General Public License as 11 | * published by the Free Software Foundation; either version 3 of 12 | * the License, or (at your option) any later version. 13 | * 14 | * CU is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | * GNU Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public License 20 | * along with this program. If not, see . 21 | */ 22 | 23 | #ifndef _CU_H_ 24 | #define _CU_H_ 25 | 26 | #ifdef CU_ENABLE_TIMER 27 | # include 28 | #endif /* CU_ENABLE_TIMER */ 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif /* __cplusplus */ 33 | 34 | /***** PUBLIC API *****/ 35 | /** 36 | * Define test 37 | */ 38 | #define TEST(name) \ 39 | void name(void) 40 | 41 | /** 42 | * Define testsuite 43 | */ 44 | #define TEST_SUITE(name) \ 45 | cu_test_suite_t test_suite_##name[] = 46 | /** 47 | * Must be on the end of list of tests. 48 | */ 49 | #define TEST_SUITE_CLOSURE \ 50 | { NULL, NULL } 51 | 52 | #define TEST_SUITES \ 53 | cu_test_suites_t cu_test_suites[] = 54 | #define TEST_SUITES_CLOSURE \ 55 | { NULL, NULL } 56 | #define TEST_SUITE_ADD(name) \ 57 | { #name, test_suite_##name } 58 | 59 | /** 60 | * Add test to testsuite 61 | */ 62 | #define TEST_ADD(name) \ 63 | { #name, name } 64 | 65 | #define CU_RUN(argc, argv) \ 66 | cu_run(argc, argv) 67 | 68 | /** 69 | * Set prefix for files printed out. Must contain trailing /. 70 | */ 71 | #define CU_SET_OUT_PREFIX(str) \ 72 | cu_set_out_prefix(str) 73 | 74 | /** 75 | * Assertations 76 | * Assertations with suffix 'M' (e.g. assertTrueM) is variation of macro 77 | * where is possible to specify error message. 78 | */ 79 | #define assertTrueM(a, message) \ 80 | if (a){ \ 81 | cu_success_assertation(); \ 82 | }else{ \ 83 | cu_fail_assertation(__FILE__, __LINE__, message); \ 84 | } 85 | #define assertTrue(a) \ 86 | assertTrueM((a), #a " is not true") 87 | 88 | #define assertFalseM(a, message) \ 89 | assertTrueM(!(a), message) 90 | #define assertFalse(a) \ 91 | assertFalseM((a), #a " is not false") 92 | 93 | #define assertEqualsM(a,b,message) \ 94 | assertTrueM((a) == (b), message) 95 | #define assertEquals(a,b) \ 96 | assertEqualsM((a), (b), #a " not equals " #b) 97 | 98 | #define assertNotEqualsM(a,b,message) \ 99 | assertTrueM((a) != (b), message) 100 | #define assertNotEquals(a,b) \ 101 | assertNotEqualsM((a), (b), #a " equals " #b) 102 | /***** PUBLIC API END *****/ 103 | 104 | 105 | #include 106 | 107 | #define CU_MAX_NAME_LENGTH 30 108 | 109 | typedef void (*cu_test_func_t)(void); 110 | typedef struct _cu_test_suite_t { 111 | const char *name; 112 | cu_test_func_t func; 113 | } cu_test_suite_t; 114 | typedef struct _cu_test_suites_t { 115 | const char *name; 116 | cu_test_suite_t *test_suite; 117 | } cu_test_suites_t; 118 | 119 | extern cu_test_suites_t cu_test_suites[]; 120 | 121 | extern const char *cu_current_test; 122 | extern const char *cu_current_test_suite; 123 | 124 | extern int cu_success_test_suites; 125 | extern int cu_fail_test_suites; 126 | extern int cu_success_tests; 127 | extern int cu_fail_tests; 128 | extern int cu_success_checks; 129 | extern int cu_fail_checks; 130 | 131 | #define CU_OUT_PREFIX_LENGTH 30 132 | extern char cu_out_prefix[CU_OUT_PREFIX_LENGTH+1]; 133 | 134 | void cu_run(int argc, char *argv[]); 135 | void cu_success_assertation(void); 136 | void cu_fail_assertation(const char *file, int line, const char *msg); 137 | void cu_set_out_prefix(const char *str); 138 | 139 | /** Timer **/ 140 | #ifdef CU_ENABLE_TIMER 141 | extern struct timespec __cu_timer; 142 | 143 | /** 144 | * Returns value of timer. (as timespec struct) 145 | */ 146 | const struct timespec *cuTimer(void); 147 | 148 | /** 149 | * Starts timer. 150 | */ 151 | void cuTimerStart(void); 152 | 153 | /** 154 | * Stops timer and record elapsed time from last call of cuTimerStart(). 155 | * Returns current value of timer. 156 | */ 157 | const struct timespec *cuTimerStop(void); 158 | #endif /* CU_ENABLE_TIMER */ 159 | 160 | #ifdef __cplusplus 161 | } 162 | #endif /* __cplusplus */ 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /src/testsuites/cu/latest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | repo=http://git.danfis.cz/cu.git 4 | files="COPYING \ 5 | COPYING.LESSER \ 6 | cu.h \ 7 | cu.c \ 8 | Makefile \ 9 | check-regressions \ 10 | .gitignore \ 11 | " 12 | rm -rf cu 13 | git clone $repo 14 | for file in $files; do 15 | mv cu/"$file" . 16 | done; 17 | rm -rf cu 18 | -------------------------------------------------------------------------------- /src/testsuites/cylcyl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "support.h" 5 | #include "common.h" 6 | 7 | 8 | TEST(cylcylSetUp) 9 | { 10 | } 11 | 12 | TEST(cylcylTearDown) 13 | { 14 | } 15 | 16 | 17 | TEST(cylcylAlignedX) 18 | { 19 | ccd_t ccd; 20 | CCD_CYL(c1); 21 | CCD_CYL(c2); 22 | size_t i; 23 | int res; 24 | 25 | CCD_INIT(&ccd); 26 | ccd.support1 = ccdSupport; 27 | ccd.support2 = ccdSupport; 28 | 29 | c1.radius = 0.35; 30 | c1.height = 0.5; 31 | c2.radius = 0.5; 32 | c2.height = 1.; 33 | 34 | ccdVec3Set(&c1.pos, -5., 0., 0.); 35 | for (i = 0; i < 100; i++){ 36 | res = ccdGJKIntersect(&c1, &c2, &ccd); 37 | 38 | if (i < 42 || i > 58){ 39 | assertFalse(res); 40 | }else{ 41 | assertTrue(res); 42 | } 43 | 44 | c1.pos.v[0] += 0.1; 45 | } 46 | } 47 | 48 | TEST(cylcylAlignedY) 49 | { 50 | ccd_t ccd; 51 | CCD_CYL(c1); 52 | CCD_CYL(c2); 53 | size_t i; 54 | int res; 55 | 56 | CCD_INIT(&ccd); 57 | ccd.support1 = ccdSupport; 58 | ccd.support2 = ccdSupport; 59 | 60 | c1.radius = 0.35; 61 | c1.height = 0.5; 62 | c2.radius = 0.5; 63 | c2.height = 1.; 64 | 65 | ccdVec3Set(&c1.pos, 0., -5., 0.); 66 | for (i = 0; i < 100; i++){ 67 | res = ccdGJKIntersect(&c1, &c2, &ccd); 68 | 69 | if (i < 42 || i > 58){ 70 | assertFalse(res); 71 | }else{ 72 | assertTrue(res); 73 | } 74 | 75 | c1.pos.v[1] += 0.1; 76 | } 77 | } 78 | 79 | TEST(cylcylAlignedZ) 80 | { 81 | ccd_t ccd; 82 | CCD_CYL(c1); 83 | CCD_CYL(c2); 84 | size_t i; 85 | int res; 86 | 87 | CCD_INIT(&ccd); 88 | ccd.support1 = ccdSupport; 89 | ccd.support2 = ccdSupport; 90 | 91 | c1.radius = 0.35; 92 | c1.height = 0.5; 93 | c2.radius = 0.5; 94 | c2.height = 1.; 95 | 96 | ccdVec3Set(&c1.pos, 0., 0., -5.); 97 | for (i = 0; i < 100; i++){ 98 | res = ccdGJKIntersect(&c1, &c2, &ccd); 99 | 100 | if (i < 43 || i > 57){ 101 | assertFalse(res); 102 | }else{ 103 | assertTrue(res); 104 | } 105 | 106 | c1.pos.v[2] += 0.1; 107 | } 108 | } 109 | 110 | #define TOSVT() \ 111 | svtObjPen(&cyl1, &cyl2, stdout, "Pen 1", depth, &dir, &pos); \ 112 | ccdVec3Scale(&dir, depth); \ 113 | ccdVec3Add(&cyl2.pos, &dir); \ 114 | svtObjPen(&cyl1, &cyl2, stdout, "Pen 1", depth, &dir, &pos) 115 | 116 | TEST(cylcylPenetrationEPA) 117 | { 118 | ccd_t ccd; 119 | CCD_CYL(cyl1); 120 | CCD_CYL(cyl2); 121 | int res; 122 | ccd_vec3_t axis; 123 | ccd_real_t depth; 124 | ccd_vec3_t dir, pos; 125 | 126 | fprintf(stderr, "\n\n\n---- cylcylPenetration ----\n\n\n"); 127 | 128 | cyl1.radius = 0.35; 129 | cyl1.height = 0.5; 130 | cyl2.radius = 0.5; 131 | cyl2.height = 1.; 132 | 133 | CCD_INIT(&ccd); 134 | ccd.support1 = ccdSupport; 135 | ccd.support2 = ccdSupport; 136 | 137 | ccdVec3Set(&cyl2.pos, 0., 0., 0.3); 138 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 139 | assertTrue(res == 0); 140 | recPen(depth, &dir, &pos, stdout, "Pen 1"); 141 | //TOSVT(); 142 | 143 | ccdVec3Set(&cyl1.pos, 0.3, 0.1, 0.1); 144 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 145 | assertTrue(res == 0); 146 | recPen(depth, &dir, &pos, stdout, "Pen 2"); 147 | //TOSVT(); <<< 148 | 149 | ccdVec3Set(&axis, 0., 1., 1.); 150 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 151 | ccdVec3Set(&cyl2.pos, 0., 0., 0.); 152 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 153 | assertTrue(res == 0); 154 | recPen(depth, &dir, &pos, stdout, "Pen 3"); 155 | //TOSVT(); 156 | 157 | ccdVec3Set(&axis, 0., 1., 1.); 158 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 159 | ccdVec3Set(&cyl2.pos, -0.2, 0.7, 0.2); 160 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 161 | assertTrue(res == 0); 162 | recPen(depth, &dir, &pos, stdout, "Pen 4"); 163 | //TOSVT(); 164 | 165 | ccdVec3Set(&axis, 0.567, 1.2, 1.); 166 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 167 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 168 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 169 | assertTrue(res == 0); 170 | recPen(depth, &dir, &pos, stdout, "Pen 5"); 171 | //TOSVT(); 172 | 173 | ccdVec3Set(&axis, -4.567, 1.2, 0.); 174 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 3., &axis); 175 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 176 | res = ccdGJKPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 177 | assertTrue(res == 0); 178 | recPen(depth, &dir, &pos, stdout, "Pen 6"); 179 | //TOSVT(); 180 | } 181 | -------------------------------------------------------------------------------- /src/testsuites/cylcyl.h: -------------------------------------------------------------------------------- 1 | #ifndef CYL_CYL 2 | #define CYL_CYL 3 | 4 | #include 5 | 6 | TEST(cylcylSetUp); 7 | TEST(cylcylTearDown); 8 | 9 | TEST(cylcylAlignedX); 10 | TEST(cylcylAlignedY); 11 | TEST(cylcylAlignedZ); 12 | 13 | TEST(cylcylPenetrationEPA); 14 | 15 | TEST_SUITE(TSCylCyl) { 16 | TEST_ADD(cylcylSetUp), 17 | 18 | TEST_ADD(cylcylAlignedX), 19 | TEST_ADD(cylcylAlignedY), 20 | TEST_ADD(cylcylAlignedZ), 21 | 22 | TEST_ADD(cylcylPenetrationEPA), 23 | 24 | TEST_ADD(cylcylTearDown), 25 | TEST_SUITE_CLOSURE 26 | }; 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /src/testsuites/main.c: -------------------------------------------------------------------------------- 1 | #include "vec3.h" 2 | #include "polytope.h" 3 | #include "boxbox.h" 4 | #include "spheresphere.h" 5 | #include "cylcyl.h" 6 | #include "boxcyl.h" 7 | 8 | #include "mpr_boxbox.h" 9 | #include "mpr_cylcyl.h" 10 | #include "mpr_boxcyl.h" 11 | 12 | TEST_SUITES { 13 | TEST_SUITE_ADD(TSVec3), 14 | TEST_SUITE_ADD(TSPt), 15 | TEST_SUITE_ADD(TSBoxBox), 16 | TEST_SUITE_ADD(TSSphereSphere), 17 | TEST_SUITE_ADD(TSCylCyl), 18 | TEST_SUITE_ADD(TSBoxCyl), 19 | 20 | TEST_SUITE_ADD(TSMPRBoxBox), 21 | TEST_SUITE_ADD(TSMPRCylCyl), 22 | TEST_SUITE_ADD(TSMPRBoxCyl), 23 | 24 | TEST_SUITES_CLOSURE 25 | }; 26 | int main(int argc, char *argv[]) 27 | { 28 | CU_SET_OUT_PREFIX("regressions/"); 29 | CU_RUN(argc, argv); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /src/testsuites/mpr_boxbox.h: -------------------------------------------------------------------------------- 1 | #ifndef MPR_BOX_BOX 2 | #define MPR_BOX_BOX 3 | 4 | #include 5 | 6 | TEST(mprBoxboxAlignedX); 7 | TEST(mprBoxboxAlignedY); 8 | TEST(mprBoxboxAlignedZ); 9 | 10 | TEST(mprBoxboxRot); 11 | 12 | TEST(mprBoxboxSeparate); 13 | TEST(mprBoxboxPenetration); 14 | 15 | TEST_SUITE(TSMPRBoxBox) { 16 | TEST_ADD(mprBoxboxAlignedX), 17 | TEST_ADD(mprBoxboxAlignedY), 18 | TEST_ADD(mprBoxboxAlignedZ), 19 | TEST_ADD(mprBoxboxRot), 20 | TEST_ADD(mprBoxboxSeparate), 21 | TEST_ADD(mprBoxboxPenetration), 22 | 23 | TEST_SUITE_CLOSURE 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/testsuites/mpr_boxcyl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "common.h" 4 | #include "support.h" 5 | 6 | #define TOSVT() \ 7 | svtObjPen(&box, &cyl, stdout, "Pen 1", depth, &dir, &pos); \ 8 | ccdVec3Scale(&dir, depth); \ 9 | ccdVec3Add(&cyl.pos, &dir); \ 10 | svtObjPen(&box, &cyl, stdout, "Pen 1", depth, &dir, &pos) 11 | 12 | TEST(mprBoxcylIntersect) 13 | { 14 | ccd_t ccd; 15 | CCD_BOX(box); 16 | CCD_CYL(cyl); 17 | int res; 18 | ccd_vec3_t axis; 19 | 20 | box.x = 0.5; 21 | box.y = 1.; 22 | box.z = 1.5; 23 | cyl.radius = 0.4; 24 | cyl.height = 0.7; 25 | 26 | CCD_INIT(&ccd); 27 | ccd.support1 = ccdSupport; 28 | ccd.support2 = ccdSupport; 29 | ccd.center1 = ccdObjCenter; 30 | ccd.center2 = ccdObjCenter; 31 | 32 | ccdVec3Set(&cyl.pos, 0.1, 0., 0.); 33 | res = ccdMPRIntersect(&box, &cyl, &ccd); 34 | assertTrue(res); 35 | 36 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 37 | res = ccdMPRIntersect(&box, &cyl, &ccd); 38 | assertTrue(res); 39 | 40 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 41 | res = ccdMPRIntersect(&box, &cyl, &ccd); 42 | assertTrue(res); 43 | 44 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 45 | res = ccdMPRIntersect(&box, &cyl, &ccd); 46 | assertTrue(res); 47 | 48 | ccdVec3Set(&axis, 0., 1., 0.); 49 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 50 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 51 | res = ccdMPRIntersect(&box, &cyl, &ccd); 52 | assertTrue(res); 53 | 54 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 55 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 56 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 57 | res = ccdMPRIntersect(&box, &cyl, &ccd); 58 | assertTrue(res); 59 | 60 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 61 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 62 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 63 | ccdVec3Set(&axis, 1., 1., 0.); 64 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 65 | ccdVec3Set(&box.pos, .6, 0., 0.5); 66 | res = ccdMPRIntersect(&box, &cyl, &ccd); 67 | assertTrue(res); 68 | 69 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 70 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 71 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 72 | ccdVec3Set(&axis, 1., 1., 0.); 73 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 74 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 75 | res = ccdMPRIntersect(&box, &cyl, &ccd); 76 | assertTrue(res); 77 | } 78 | 79 | 80 | 81 | TEST(mprBoxcylPen) 82 | { 83 | ccd_t ccd; 84 | CCD_BOX(box); 85 | CCD_CYL(cyl); 86 | int res; 87 | ccd_vec3_t axis; 88 | ccd_real_t depth; 89 | ccd_vec3_t dir, pos; 90 | 91 | box.x = 0.5; 92 | box.y = 1.; 93 | box.z = 1.5; 94 | cyl.radius = 0.4; 95 | cyl.height = 0.7; 96 | 97 | CCD_INIT(&ccd); 98 | ccd.support1 = ccdSupport; 99 | ccd.support2 = ccdSupport; 100 | ccd.center1 = ccdObjCenter; 101 | ccd.center2 = ccdObjCenter; 102 | 103 | ccdVec3Set(&cyl.pos, 0.1, 0., 0.); 104 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 105 | assertTrue(res == 0); 106 | recPen(depth, &dir, &pos, stdout, "Pen 1"); 107 | //TOSVT(); 108 | 109 | ccdVec3Set(&cyl.pos, .6, 0., 0.); 110 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 111 | assertTrue(res == 0); 112 | recPen(depth, &dir, &pos, stdout, "Pen 2"); 113 | //TOSVT(); 114 | 115 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.); 116 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 117 | assertTrue(res == 0); 118 | recPen(depth, &dir, &pos, stdout, "Pen 3"); 119 | //TOSVT(); 120 | 121 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 122 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 123 | assertTrue(res == 0); 124 | recPen(depth, &dir, &pos, stdout, "Pen 4"); 125 | //TOSVT(); 126 | 127 | ccdVec3Set(&axis, 0., 1., 0.); 128 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 3., &axis); 129 | ccdVec3Set(&cyl.pos, .6, 0.6, 0.5); 130 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 131 | assertTrue(res == 0); 132 | recPen(depth, &dir, &pos, stdout, "Pen 5"); 133 | //TOSVT(); 134 | 135 | ccdVec3Set(&axis, 0.67, 1.1, 0.12); 136 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 4., &axis); 137 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 138 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 139 | assertTrue(res == 0); 140 | recPen(depth, &dir, &pos, stdout, "Pen 6"); 141 | //TOSVT(); 142 | 143 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 144 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 145 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 146 | ccdVec3Set(&axis, 1., 1., 0.); 147 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 148 | ccdVec3Set(&box.pos, .6, 0., 0.5); 149 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 150 | assertTrue(res == 0); 151 | recPen(depth, &dir, &pos, stdout, "Pen 7"); 152 | //TOSVT(); 153 | 154 | ccdVec3Set(&axis, -0.1, 2.2, -1.); 155 | ccdQuatSetAngleAxis(&cyl.quat, M_PI / 5., &axis); 156 | ccdVec3Set(&cyl.pos, .6, 0., 0.5); 157 | ccdVec3Set(&axis, 1., 1., 0.); 158 | ccdQuatSetAngleAxis(&box.quat, -M_PI / 4., &axis); 159 | ccdVec3Set(&box.pos, .9, 0.8, 0.5); 160 | res = ccdMPRPenetration(&box, &cyl, &ccd, &depth, &dir, &pos); 161 | assertTrue(res == 0); 162 | recPen(depth, &dir, &pos, stdout, "Pen 8"); 163 | //TOSVT(); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /src/testsuites/mpr_boxcyl.h: -------------------------------------------------------------------------------- 1 | #ifndef MPR_TEST_BOXCYL_H 2 | #define MPR_TEST_BOXCYL_H 3 | 4 | #include 5 | 6 | TEST(mprBoxcylIntersect); 7 | TEST(mprBoxcylPen); 8 | 9 | TEST_SUITE(TSMPRBoxCyl){ 10 | TEST_ADD(mprBoxcylIntersect), 11 | TEST_ADD(mprBoxcylPen), 12 | 13 | TEST_SUITE_CLOSURE 14 | }; 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /src/testsuites/mpr_cylcyl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "support.h" 5 | #include "common.h" 6 | 7 | 8 | TEST(mprCylcylAlignedX) 9 | { 10 | ccd_t ccd; 11 | CCD_CYL(c1); 12 | CCD_CYL(c2); 13 | size_t i; 14 | int res; 15 | 16 | CCD_INIT(&ccd); 17 | ccd.support1 = ccdSupport; 18 | ccd.support2 = ccdSupport; 19 | ccd.center1 = ccdObjCenter; 20 | ccd.center2 = ccdObjCenter; 21 | 22 | c1.radius = 0.35; 23 | c1.height = 0.5; 24 | c2.radius = 0.5; 25 | c2.height = 1.; 26 | 27 | ccdVec3Set(&c1.pos, -5., 0., 0.); 28 | for (i = 0; i < 100; i++){ 29 | res = ccdMPRIntersect(&c1, &c2, &ccd); 30 | 31 | if (i < 42 || i > 58){ 32 | assertFalse(res); 33 | }else{ 34 | assertTrue(res); 35 | } 36 | 37 | c1.pos.v[0] += 0.1; 38 | } 39 | } 40 | 41 | TEST(mprCylcylAlignedY) 42 | { 43 | ccd_t ccd; 44 | CCD_CYL(c1); 45 | CCD_CYL(c2); 46 | size_t i; 47 | int res; 48 | 49 | CCD_INIT(&ccd); 50 | ccd.support1 = ccdSupport; 51 | ccd.support2 = ccdSupport; 52 | ccd.center1 = ccdObjCenter; 53 | ccd.center2 = ccdObjCenter; 54 | 55 | c1.radius = 0.35; 56 | c1.height = 0.5; 57 | c2.radius = 0.5; 58 | c2.height = 1.; 59 | 60 | ccdVec3Set(&c1.pos, 0., -5., 0.); 61 | for (i = 0; i < 100; i++){ 62 | res = ccdMPRIntersect(&c1, &c2, &ccd); 63 | 64 | if (i < 42 || i > 58){ 65 | assertFalse(res); 66 | }else{ 67 | assertTrue(res); 68 | } 69 | 70 | c1.pos.v[1] += 0.1; 71 | } 72 | } 73 | 74 | TEST(mprCylcylAlignedZ) 75 | { 76 | ccd_t ccd; 77 | CCD_CYL(c1); 78 | CCD_CYL(c2); 79 | size_t i; 80 | int res; 81 | 82 | CCD_INIT(&ccd); 83 | ccd.support1 = ccdSupport; 84 | ccd.support2 = ccdSupport; 85 | ccd.center1 = ccdObjCenter; 86 | ccd.center2 = ccdObjCenter; 87 | 88 | c1.radius = 0.35; 89 | c1.height = 0.5; 90 | c2.radius = 0.5; 91 | c2.height = 1.; 92 | 93 | ccdVec3Set(&c1.pos, 0., 0., -5.); 94 | for (i = 0; i < 100; i++){ 95 | res = ccdMPRIntersect(&c1, &c2, &ccd); 96 | 97 | if (i < 43 || i > 57){ 98 | assertFalse(res); 99 | }else{ 100 | assertTrue(res); 101 | } 102 | 103 | c1.pos.v[2] += 0.1; 104 | } 105 | } 106 | 107 | #define TOSVT() \ 108 | svtObjPen(&cyl1, &cyl2, stdout, "Pen 1", depth, &dir, &pos); \ 109 | ccdVec3Scale(&dir, depth); \ 110 | ccdVec3Add(&cyl2.pos, &dir); \ 111 | svtObjPen(&cyl1, &cyl2, stdout, "Pen 1", depth, &dir, &pos) 112 | 113 | TEST(mprCylcylPenetration) 114 | { 115 | ccd_t ccd; 116 | CCD_CYL(cyl1); 117 | CCD_CYL(cyl2); 118 | int res; 119 | ccd_vec3_t axis; 120 | ccd_real_t depth; 121 | ccd_vec3_t dir, pos; 122 | 123 | fprintf(stderr, "\n\n\n---- mprCylcylPenetration ----\n\n\n"); 124 | 125 | cyl1.radius = 0.35; 126 | cyl1.height = 0.5; 127 | cyl2.radius = 0.5; 128 | cyl2.height = 1.; 129 | 130 | CCD_INIT(&ccd); 131 | ccd.support1 = ccdSupport; 132 | ccd.support2 = ccdSupport; 133 | ccd.center1 = ccdObjCenter; 134 | ccd.center2 = ccdObjCenter; 135 | 136 | ccdVec3Set(&cyl2.pos, 0., 0., 0.3); 137 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 138 | assertTrue(res == 0); 139 | recPen(depth, &dir, &pos, stdout, "Pen 1"); 140 | //TOSVT(); 141 | 142 | ccdVec3Set(&cyl1.pos, 0.3, 0.1, 0.1); 143 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 144 | assertTrue(res == 0); 145 | recPen(depth, &dir, &pos, stdout, "Pen 2"); 146 | //TOSVT(); 147 | 148 | ccdVec3Set(&axis, 0., 1., 1.); 149 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 150 | ccdVec3Set(&cyl2.pos, 0., 0., 0.); 151 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 152 | assertTrue(res == 0); 153 | recPen(depth, &dir, &pos, stdout, "Pen 3"); 154 | //TOSVT(); 155 | 156 | ccdVec3Set(&axis, 0., 1., 1.); 157 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 158 | ccdVec3Set(&cyl2.pos, -0.2, 0.7, 0.2); 159 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 160 | assertTrue(res == 0); 161 | recPen(depth, &dir, &pos, stdout, "Pen 4"); 162 | //TOSVT(); 163 | 164 | ccdVec3Set(&axis, 0.567, 1.2, 1.); 165 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 4., &axis); 166 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 167 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 168 | assertTrue(res == 0); 169 | recPen(depth, &dir, &pos, stdout, "Pen 5"); 170 | //TOSVT(); 171 | 172 | ccdVec3Set(&axis, -4.567, 1.2, 0.); 173 | ccdQuatSetAngleAxis(&cyl2.quat, M_PI / 3., &axis); 174 | ccdVec3Set(&cyl2.pos, 0.6, -0.7, 0.2); 175 | res = ccdMPRPenetration(&cyl1, &cyl2, &ccd, &depth, &dir, &pos); 176 | assertTrue(res == 0); 177 | recPen(depth, &dir, &pos, stdout, "Pen 6"); 178 | //TOSVT(); 179 | } 180 | -------------------------------------------------------------------------------- /src/testsuites/mpr_cylcyl.h: -------------------------------------------------------------------------------- 1 | #ifndef MPR_CYL_CYL 2 | #define MPR_CYL_CYL 3 | 4 | #include 5 | 6 | TEST(mprCylcylAlignedX); 7 | TEST(mprCylcylAlignedY); 8 | TEST(mprCylcylAlignedZ); 9 | 10 | TEST(mprCylcylPenetration); 11 | 12 | TEST_SUITE(TSMPRCylCyl) { 13 | TEST_ADD(mprCylcylAlignedX), 14 | TEST_ADD(mprCylcylAlignedY), 15 | TEST_ADD(mprCylcylAlignedZ), 16 | 17 | TEST_ADD(mprCylcylPenetration), 18 | 19 | TEST_SUITE_CLOSURE 20 | }; 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /src/testsuites/polytope.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_POLYTOPE_H 2 | #define TEST_POLYTOPE_H 3 | 4 | #include 5 | 6 | TEST(ptSetUp); 7 | TEST(ptTearDown); 8 | 9 | TEST(ptCreate1); 10 | TEST(ptCreate2); 11 | TEST(ptNearest); 12 | 13 | TEST_SUITE(TSPt) { 14 | TEST_ADD(ptSetUp), 15 | 16 | TEST_ADD(ptCreate1), 17 | TEST_ADD(ptCreate2), 18 | TEST_ADD(ptNearest), 19 | 20 | TEST_ADD(ptTearDown), 21 | TEST_SUITE_CLOSURE 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/testsuites/regressions/.dir: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/.dir -------------------------------------------------------------------------------- /src/testsuites/regressions/TSBoxBox.err: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ---- boxboxSeparate ---- 5 | 6 | 7 | 8 | 9 | 10 | ---- boxboxPenetration ---- 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSBoxBox.out: -------------------------------------------------------------------------------- 1 | # box1.pos: [-0.500000 0.000000 0.000000] 2 | # box1->quat: [0.000000 0.000000 0.382683 0.923880] 3 | # box2->pos: [0.000000 0.000000 0.000000] 4 | # box2->quat: [0.000000 0.000000 0.000000 1.000000] 5 | # sep: [0.707107 0.000000 0.000000] 6 | # 7 | # box1.pos: [-0.500000 0.100000 0.400000] 8 | # box1->quat: [0.000000 0.270598 0.270598 0.923880] 9 | # box2->pos: [0.000000 0.000000 0.000000] 10 | # box2->quat: [0.000000 0.000000 0.000000 1.000000] 11 | # sep: [0.633939 0.000000 -0.371353] 12 | # 13 | # Pen 1: depth: 0.650000 14 | # Pen 1: dir: [1.000000 0.000000 0.000000] 15 | # Pen 1: pos: [0.096875 0.000000 0.000000] 16 | # 17 | # Pen 2: depth: 0.250000 18 | # Pen 2: dir: [-0.000000 0.000000 -1.000000] 19 | # Pen 2: pos: [-0.058333 0.250000 0.583333] 20 | # 21 | # Pen 3: depth: 0.900000 22 | # Pen 3: dir: [0.000000 0.000000 -1.000000] 23 | # Pen 3: pos: [0.111506 0.000000 0.050000] 24 | # 25 | # Pen 4: depth: 0.607107 26 | # Pen 4: dir: [1.000000 0.000000 0.000000] 27 | # Pen 4: pos: [-0.153585 0.000000 0.000000] 28 | # 29 | # Pen 5: depth: 0.429289 30 | # Pen 5: dir: [0.707107 -0.707107 0.000000] 31 | # Pen 5: pos: [-0.167157 0.379289 0.000000] 32 | # 33 | # Pen 6: depth: 0.648412 34 | # Pen 6: dir: [0.862856 0.000000 -0.505449] 35 | # Pen 6: pos: [-0.148223 0.055362 0.319638] 36 | # 37 | # Pen 7: depth: 0.622622 38 | # Pen 7: dir: [1.000000 0.000000 -0.000000] 39 | # Pen 7: pos: [-0.095997 0.063593 0.067678] 40 | # 41 | # Pen 8: depth: 0.053553 42 | # Pen 8: dir: [1.000000 0.000000 0.000000] 43 | # Pen 8: pos: [-0.523223 -0.073223 0.020711] 44 | # 45 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSBoxCyl.err: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSBoxCyl.err -------------------------------------------------------------------------------- /src/testsuites/regressions/TSBoxCyl.out: -------------------------------------------------------------------------------- 1 | # Pen 1: depth: 0.549996 2 | # Pen 1: dir: [0.999992 -0.003902 0.000000] 3 | # Pen 1: pos: [0.020284 0.000000 0.000000] 4 | # 5 | # Pen 2: depth: 0.050000 6 | # Pen 2: dir: [0.999992 -0.003902 0.000000] 7 | # Pen 2: pos: [0.253480 0.000000 0.025000] 8 | # 9 | # Pen 3: depth: 0.030994 10 | # Pen 3: dir: [0.950248 0.311493 0.000000] 11 | # Pen 3: pos: [0.246546 0.420744 0.000000] 12 | # 13 | # Pen 4: depth: 0.033436 14 | # Pen 4: dir: [0.976101 0.217308 0.001900] 15 | # Pen 4: pos: [0.243648 0.480401 0.450000] 16 | # 17 | # Pen 5: depth: 0.142160 18 | # Pen 5: dir: [0.968442 0.249235 0.001146] 19 | # Pen 5: pos: [0.190887 0.421462 0.605496] 20 | # 21 | # Pen 6: depth: 0.179282 22 | # Pen 6: dir: [0.999995 0.001057 0.002913] 23 | # Pen 6: pos: [0.176026 0.036944 0.488189] 24 | # 25 | # Pen 7: depth: 0.750000 26 | # Pen 7: dir: [-0.853795 -0.143509 -0.500438] 27 | # Pen 7: pos: [0.572744 0.014828 0.562324] 28 | # 29 | # Pen 8: depth: 0.142666 30 | # Pen 8: dir: [-0.475515 -0.841074 0.257839] 31 | # Pen 8: pos: [0.824886 0.230213 0.463136] 32 | # 33 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSCylCyl.err: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ---- cylcylPenetration ---- 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSCylCyl.out: -------------------------------------------------------------------------------- 1 | # Pen 1: depth: 0.750000 2 | # Pen 1: dir: [0.000000 0.000000 1.000000] 3 | # Pen 1: pos: [0.004079 -0.012238 0.009615] 4 | # 5 | # Pen 2: depth: 0.531931 6 | # Pen 2: dir: [-0.926428 -0.376463 -0.002666] 7 | # Pen 2: pos: [0.218566 0.072232 0.025000] 8 | # 9 | # Pen 3: depth: 0.645740 10 | # Pen 3: dir: [-0.500000 -0.146447 -0.853553] 11 | # Pen 3: pos: [0.177594 0.070484 0.186987] 12 | # 13 | # Pen 4: depth: 0.104445 14 | # Pen 4: dir: [-0.482095 0.866317 0.130685] 15 | # Pen 4: pos: [0.123724 0.348390 0.269312] 16 | # 17 | # Pen 5: depth: 0.093082 18 | # Pen 5: dir: [0.034600 -0.999228 -0.018627] 19 | # Pen 5: pos: [0.311257 -0.203923 -0.064270] 20 | # 21 | # Pen 6: depth: 0.198749 22 | # Pen 6: dir: [0.411370 -0.911372 0.013223] 23 | # Pen 6: pos: [0.405836 -0.130066 0.121441] 24 | # 25 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRBoxBox.err: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ---- boxboxSeparate ---- 5 | 6 | 7 | 8 | 9 | 10 | ---- boxboxPenetration ---- 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRBoxBox.out: -------------------------------------------------------------------------------- 1 | # box1.pos: [-0.500000 0.000000 0.000000] 2 | # box1->quat: [0.000000 0.000000 0.382683 0.923880] 3 | # box2->pos: [0.000000 0.000000 0.000000] 4 | # box2->quat: [0.000000 0.000000 0.000000 1.000000] 5 | # sep: [0.707107 0.000000 0.000000] 6 | # 7 | # box1.pos: [-0.500000 0.100000 0.400000] 8 | # box1->quat: [0.000000 0.270598 0.270598 0.923880] 9 | # box2->pos: [0.000000 0.000000 0.000000] 10 | # box2->quat: [0.000000 0.000000 0.000000 1.000000] 11 | # sep: [0.633939 0.000000 -0.371353] 12 | # 13 | # Pen 1: depth: 0.650000 14 | # Pen 1: dir: [1.000000 0.000000 0.000000] 15 | # Pen 1: pos: [0.175000 0.000000 0.000000] 16 | # 17 | # Pen 2: depth: 0.250000 18 | # Pen 2: dir: [-0.000000 0.000000 -1.000000] 19 | # Pen 2: pos: [-0.033333 0.250000 0.600000] 20 | # 21 | # Pen 3: depth: 0.900000 22 | # Pen 3: dir: [0.000000 0.000000 -1.000000] 23 | # Pen 3: pos: [0.100000 0.000000 0.050000] 24 | # 25 | # Pen 4: depth: 0.607107 26 | # Pen 4: dir: [1.000000 0.000000 0.000000] 27 | # Pen 4: pos: [-0.096447 0.000000 0.000000] 28 | # 29 | # Pen 5: depth: 0.429289 30 | # Pen 5: dir: [0.707107 -0.707107 0.000000] 31 | # Pen 5: pos: [-0.222183 0.322183 0.000000] 32 | # 33 | # Pen 6: depth: 0.648412 34 | # Pen 6: dir: [0.862856 0.000000 -0.505449] 35 | # Pen 6: pos: [-0.163060 0.012676 0.263060] 36 | # 37 | # Pen 7: depth: 0.622928 38 | # Pen 7: dir: [0.999509 0.028016 -0.014008] 39 | # Pen 7: pos: [-0.145374 0.170833 0.176732] 40 | # 41 | # Pen 8: depth: 0.053553 42 | # Pen 8: dir: [1.000000 0.000000 0.000000] 43 | # Pen 8: pos: [-0.480217 -0.140652 0.000000] 44 | # 45 | # Pen 9: depth: 0.020000 46 | # Pen 9: dir: [0.000000 1.000000 0.000000] 47 | # Pen 9: pos: [0.000000 0.490000 0.000000] 48 | # 49 | # Pen 10: depth: 0.012000 50 | # Pen 10: dir: [-0.000000 1.000000 0.000000] 51 | # Pen 10: pos: [0.200000 0.492000 0.000000] 52 | # 53 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRBoxCyl.err: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSMPRBoxCyl.err -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRBoxCyl.out: -------------------------------------------------------------------------------- 1 | # Pen 1: depth: 0.550000 2 | # Pen 1: dir: [1.000000 0.000000 0.000000] 3 | # Pen 1: pos: [-0.025000 0.000000 0.000000] 4 | # 5 | # Pen 2: depth: 0.050000 6 | # Pen 2: dir: [1.000000 0.000000 0.000000] 7 | # Pen 2: pos: [0.225000 0.000000 0.000000] 8 | # 9 | # Pen 3: depth: 0.038532 10 | # Pen 3: dir: [0.788956 0.614450 0.000000] 11 | # Pen 3: pos: [0.238587 0.477175 0.000000] 12 | # 13 | # Pen 4: depth: 0.038654 14 | # Pen 4: dir: [0.779134 0.626832 -0.005696] 15 | # Pen 4: pos: [0.238603 0.477206 0.340909] 16 | # 17 | # Pen 5: depth: 0.166653 18 | # Pen 5: dir: [0.734126 0.679013 -0.000000] 19 | # Pen 5: pos: [0.208320 0.416640 0.595113] 20 | # 21 | # Pen 6: depth: 0.180673 22 | # Pen 6: dir: [1.000000 0.000003 -0.000000] 23 | # Pen 6: pos: [0.192142 0.009404 0.479162] 24 | # 25 | # Pen 7: depth: 1.321922 26 | # Pen 7: dir: [-0.897996 -0.063457 0.435403] 27 | # Pen 7: pos: [0.531929 -0.046446 0.867546] 28 | # 29 | # Pen 8: depth: 0.142813 30 | # Pen 8: dir: [-0.476782 -0.840534 0.257259] 31 | # Pen 8: pos: [0.776128 0.285646 0.436629] 32 | # 33 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRCylCyl.err: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ---- mprCylcylPenetration ---- 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSMPRCylCyl.out: -------------------------------------------------------------------------------- 1 | # Pen 1: depth: 0.450000 2 | # Pen 1: dir: [0.000000 0.000000 1.000000] 3 | # Pen 1: pos: [0.000000 0.000000 0.025000] 4 | # 5 | # Pen 2: depth: 0.533732 6 | # Pen 2: dir: [-0.952492 -0.304562 0.000000] 7 | # Pen 2: pos: [0.176471 0.058824 0.166667] 8 | # 9 | # Pen 3: depth: 0.720933 10 | # Pen 3: dir: [-0.947406 -0.320033 0.000085] 11 | # Pen 3: pos: [0.198747 0.066309 0.050800] 12 | # 13 | # Pen 4: depth: 0.106076 14 | # Pen 4: dir: [-0.524820 0.835278 0.163936] 15 | # Pen 4: pos: [0.138692 0.362418 0.320024] 16 | # 17 | # Pen 5: depth: 0.103863 18 | # Pen 5: dir: [0.291494 -0.956567 -0.003314] 19 | # Pen 5: pos: [0.337721 -0.209314 -0.094587] 20 | # 21 | # Pen 6: depth: 0.202625 22 | # Pen 6: dir: [0.347225 -0.937782 -0.000000] 23 | # Pen 6: pos: [0.399554 -0.164780 0.199941] 24 | # 25 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSPt.err: -------------------------------------------------------------------------------- 1 | ptCreate1 :: ------ 2 | ptCreate1 :: e[0]->dist: 0.200000 3 | ptCreate1 :: ->witness: [0.200000 -0.400000 0.000000] 4 | ptCreate1 :: e[1]->dist: 0.500000 5 | ptCreate1 :: ->witness: [0.500000 0.000000 0.500000] 6 | ptCreate1 :: e[2]->dist: 0.666667 7 | ptCreate1 :: ->witness: [-0.333333 -0.333333 0.666667] 8 | ptCreate1 :: f->dist: 0.166667 9 | ptCreate1 :: ->witness: [0.166667 -0.333333 0.166667] 10 | ptCreate2 :: ------ 11 | ptCreate2 :: v[0]->dist: 2.000000 12 | ptCreate2 :: ->witness: [-1.000000 -1.000000 0.000000] 13 | ptCreate2 :: v[1]->dist: 1.000000 14 | ptCreate2 :: ->witness: [1.000000 0.000000 0.000000] 15 | ptCreate2 :: v[2]->dist: 1.000000 16 | ptCreate2 :: ->witness: [0.000000 0.000000 1.000000] 17 | ptCreate2 :: v[3]->dist: 1.000000 18 | ptCreate2 :: ->witness: [0.000000 1.000000 0.000000] 19 | ptCreate2 :: e[0]->dist: 0.200000 20 | ptCreate2 :: ->witness: [0.200000 -0.400000 0.000000] 21 | ptCreate2 :: e[1]->dist: 0.500000 22 | ptCreate2 :: ->witness: [0.500000 0.000000 0.500000] 23 | ptCreate2 :: e[2]->dist: 0.666667 24 | ptCreate2 :: ->witness: [-0.333333 -0.333333 0.666667] 25 | ptCreate2 :: e[3]->dist: 0.200000 26 | ptCreate2 :: ->witness: [-0.400000 0.200000 0.000000] 27 | ptCreate2 :: e[4]->dist: 0.500000 28 | ptCreate2 :: ->witness: [0.500000 0.500000 0.000000] 29 | ptCreate2 :: e[5]->dist: 0.500000 30 | ptCreate2 :: ->witness: [0.000000 0.500000 0.500000] 31 | ptCreate2 :: f[0]->dist: 0.166667 32 | ptCreate2 :: ->witness: [0.166667 -0.333333 0.166667] 33 | ptCreate2 :: f[1]->dist: 0.000000 34 | ptCreate2 :: ->witness: [0.000000 0.000000 0.000000] 35 | ptCreate2 :: f[2]->dist: 0.333333 36 | ptCreate2 :: ->witness: [0.333333 0.333333 0.333333] 37 | ptCreate2 :: f[3]->dist: 0.166667 38 | ptCreate2 :: ->witness: [-0.333333 0.166667 0.166667] 39 | ptNearest :: ------ 40 | -------------------------------------------------------------------------------- /src/testsuites/regressions/TSPt.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSPt.out -------------------------------------------------------------------------------- /src/testsuites/regressions/TSSphereSphere.err: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSSphereSphere.err -------------------------------------------------------------------------------- /src/testsuites/regressions/TSSphereSphere.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSSphereSphere.out -------------------------------------------------------------------------------- /src/testsuites/regressions/TSVec3.err: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSVec3.err -------------------------------------------------------------------------------- /src/testsuites/regressions/TSVec3.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danfis/libccd/7931e764a19ef6b21b443376c699bbc9c6d4fba8/src/testsuites/regressions/TSVec3.out -------------------------------------------------------------------------------- /src/testsuites/spheresphere.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "support.h" 5 | 6 | TEST(spheresphereSetUp) 7 | { 8 | } 9 | 10 | TEST(spheresphereTearDown) 11 | { 12 | } 13 | 14 | TEST(spheresphereAlignedX) 15 | { 16 | ccd_t ccd; 17 | CCD_SPHERE(s1); 18 | CCD_SPHERE(s2); 19 | size_t i; 20 | int res; 21 | 22 | CCD_INIT(&ccd); 23 | ccd.support1 = ccdSupport; 24 | ccd.support2 = ccdSupport; 25 | 26 | s1.radius = 0.35; 27 | s2.radius = .5; 28 | 29 | ccdVec3Set(&s1.pos, -5., 0., 0.); 30 | for (i = 0; i < 100; i++){ 31 | res = ccdGJKIntersect(&s1, &s2, &ccd); 32 | 33 | if (i < 42 || i > 58){ 34 | assertFalse(res); 35 | }else{ 36 | assertTrue(res); 37 | } 38 | 39 | s1.pos.v[0] += 0.1; 40 | } 41 | } 42 | 43 | TEST(spheresphereAlignedY) 44 | { 45 | ccd_t ccd; 46 | CCD_SPHERE(s1); 47 | CCD_SPHERE(s2); 48 | size_t i; 49 | int res; 50 | 51 | CCD_INIT(&ccd); 52 | ccd.support1 = ccdSupport; 53 | ccd.support2 = ccdSupport; 54 | 55 | s1.radius = 0.35; 56 | s2.radius = .5; 57 | 58 | ccdVec3Set(&s1.pos, 0., -5., 0.); 59 | for (i = 0; i < 100; i++){ 60 | res = ccdGJKIntersect(&s1, &s2, &ccd); 61 | 62 | if (i < 42 || i > 58){ 63 | assertFalse(res); 64 | }else{ 65 | assertTrue(res); 66 | } 67 | 68 | s1.pos.v[1] += 0.1; 69 | } 70 | } 71 | 72 | TEST(spheresphereAlignedZ) 73 | { 74 | ccd_t ccd; 75 | CCD_SPHERE(s1); 76 | CCD_SPHERE(s2); 77 | size_t i; 78 | int res; 79 | 80 | CCD_INIT(&ccd); 81 | ccd.support1 = ccdSupport; 82 | ccd.support2 = ccdSupport; 83 | 84 | s1.radius = 0.35; 85 | s2.radius = .5; 86 | 87 | ccdVec3Set(&s1.pos, 0., 0., -5.); 88 | for (i = 0; i < 100; i++){ 89 | res = ccdGJKIntersect(&s1, &s2, &ccd); 90 | 91 | if (i < 42 || i > 58){ 92 | assertFalse(res); 93 | }else{ 94 | assertTrue(res); 95 | } 96 | 97 | s1.pos.v[2] += 0.1; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/testsuites/spheresphere.h: -------------------------------------------------------------------------------- 1 | #ifndef SPHERE_SPHERE 2 | #define SPHERE_SPHERE 3 | 4 | #include 5 | 6 | TEST(spheresphereSetUp); 7 | TEST(spheresphereTearDown); 8 | 9 | TEST(spheresphereAlignedX); 10 | TEST(spheresphereAlignedY); 11 | TEST(spheresphereAlignedZ); 12 | 13 | TEST_SUITE(TSSphereSphere) { 14 | TEST_ADD(spheresphereSetUp), 15 | 16 | TEST_ADD(spheresphereAlignedX), 17 | TEST_ADD(spheresphereAlignedY), 18 | TEST_ADD(spheresphereAlignedZ), 19 | 20 | TEST_ADD(spheresphereTearDown), 21 | TEST_SUITE_CLOSURE 22 | }; 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/testsuites/support.c: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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 | #include 19 | #include 20 | #include "support.h" 21 | 22 | void ccdSupport(const void *_obj, const ccd_vec3_t *_dir, 23 | ccd_vec3_t *v) 24 | { 25 | // Support function is made according to Gino van den Bergen's paper 26 | // A Fast and Robust CCD Implementation for Collision Detection of 27 | // Convex Objects 28 | 29 | ccd_obj_t *obj = (ccd_obj_t *)_obj; 30 | ccd_vec3_t dir; 31 | ccd_quat_t qinv; 32 | 33 | ccdVec3Copy(&dir, _dir); 34 | ccdQuatInvert2(&qinv, &obj->quat); 35 | 36 | ccdQuatRotVec(&dir, &qinv); 37 | 38 | if (obj->type == CCD_OBJ_BOX){ 39 | ccd_box_t *box = (ccd_box_t *)obj; 40 | ccdVec3Set(v, ccdSign(ccdVec3X(&dir)) * box->x * CCD_REAL(0.5), 41 | ccdSign(ccdVec3Y(&dir)) * box->y * CCD_REAL(0.5), 42 | ccdSign(ccdVec3Z(&dir)) * box->z * CCD_REAL(0.5)); 43 | }else if (obj->type == CCD_OBJ_SPHERE){ 44 | ccd_sphere_t *sphere = (ccd_sphere_t *)obj; 45 | ccd_real_t len; 46 | 47 | len = ccdVec3Len2(&dir); 48 | if (len - CCD_EPS > CCD_ZERO){ 49 | ccdVec3Copy(v, &dir); 50 | ccdVec3Scale(v, sphere->radius / CCD_SQRT(len)); 51 | }else{ 52 | ccdVec3Set(v, CCD_ZERO, CCD_ZERO, CCD_ZERO); 53 | } 54 | }else if (obj->type == CCD_OBJ_CYL){ 55 | ccd_cyl_t *cyl = (ccd_cyl_t *)obj; 56 | ccd_real_t zdist, rad; 57 | 58 | zdist = dir.v[0] * dir.v[0] + dir.v[1] * dir.v[1]; 59 | zdist = CCD_SQRT(zdist); 60 | if (ccdIsZero(zdist)){ 61 | ccdVec3Set(v, CCD_ZERO, CCD_ZERO, 62 | ccdSign(ccdVec3Z(&dir)) * cyl->height * CCD_REAL(0.5)); 63 | }else{ 64 | rad = cyl->radius / zdist; 65 | 66 | ccdVec3Set(v, rad * ccdVec3X(&dir), 67 | rad * ccdVec3Y(&dir), 68 | ccdSign(ccdVec3Z(&dir)) * cyl->height * CCD_REAL(0.5)); 69 | } 70 | } 71 | 72 | // transform support vertex 73 | ccdQuatRotVec(v, &obj->quat); 74 | ccdVec3Add(v, &obj->pos); 75 | } 76 | 77 | void ccdObjCenter(const void *_obj, ccd_vec3_t *center) 78 | { 79 | ccd_obj_t *obj = (ccd_obj_t *)_obj; 80 | 81 | ccdVec3Set(center, CCD_ZERO, CCD_ZERO, CCD_ZERO); 82 | // rotation is not needed 83 | ccdVec3Add(center, &obj->pos); 84 | } 85 | -------------------------------------------------------------------------------- /src/testsuites/support.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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 | /*** 19 | * Some support() functions for some convex shapes. 20 | */ 21 | 22 | #ifndef __CCD_SUPPORT_H__ 23 | #define __CCD_SUPPORT_H__ 24 | 25 | #include 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif /* __cplusplus */ 30 | 31 | #define CCD_OBJ_BOX 1 32 | #define CCD_OBJ_SPHERE 2 33 | #define CCD_OBJ_CYL 3 34 | 35 | #define __CCD_OBJ__ \ 36 | int type; \ 37 | ccd_vec3_t pos; \ 38 | ccd_quat_t quat; 39 | 40 | struct _ccd_obj_t { 41 | __CCD_OBJ__ 42 | }; 43 | typedef struct _ccd_obj_t ccd_obj_t; 44 | 45 | struct _ccd_box_t { 46 | __CCD_OBJ__ 47 | ccd_real_t x, y, z; //!< Lengths of box's edges 48 | }; 49 | typedef struct _ccd_box_t ccd_box_t; 50 | 51 | struct _ccd_sphere_t { 52 | __CCD_OBJ__ 53 | ccd_real_t radius; 54 | }; 55 | typedef struct _ccd_sphere_t ccd_sphere_t; 56 | 57 | struct _ccd_cyl_t { 58 | __CCD_OBJ__ 59 | ccd_real_t radius; 60 | ccd_real_t height; 61 | }; 62 | typedef struct _ccd_cyl_t ccd_cyl_t; 63 | 64 | 65 | #define CCD_BOX(name) \ 66 | ccd_box_t name = { .type = CCD_OBJ_BOX, \ 67 | .pos = { .v = { 0., 0., 0. } }, \ 68 | .quat = { .q = { 0., 0., 0., 1. } }, \ 69 | .x = 0., \ 70 | .y = 0., \ 71 | .z = 0. } 72 | 73 | #define CCD_SPHERE(name) \ 74 | ccd_sphere_t name = { .type = CCD_OBJ_SPHERE, \ 75 | .pos = { .v = { 0., 0., 0. } }, \ 76 | .quat = { .q = { 0., 0., 0., 1. } }, \ 77 | .radius = 0. } 78 | 79 | #define CCD_CYL(name) \ 80 | ccd_cyl_t name = { .type = CCD_OBJ_CYL, \ 81 | .pos = { .v = { 0., 0., 0. } }, \ 82 | .quat = { .q = { 0., 0., 0., 1. } }, \ 83 | .radius = 0., \ 84 | .height = 0. } 85 | 86 | /** 87 | * Returns supporting vertex via v. 88 | * Supporting vertex is fathest vertex from object in direction dir. 89 | */ 90 | void ccdSupport(const void *obj, const ccd_vec3_t *dir, 91 | ccd_vec3_t *v); 92 | 93 | /** 94 | * Returns center of object. 95 | */ 96 | void ccdObjCenter(const void *obj, ccd_vec3_t *center); 97 | 98 | #ifdef __cplusplus 99 | } /* extern "C" */ 100 | #endif /* __cplusplus */ 101 | 102 | #endif /* __CCD_SUPPORT_H__ */ 103 | -------------------------------------------------------------------------------- /src/testsuites/vec3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | TEST(vec3SetUp) 6 | { 7 | } 8 | 9 | TEST(vec3TearDown) 10 | { 11 | } 12 | 13 | 14 | TEST(vec3PointSegmentDist) 15 | { 16 | ccd_vec3_t P, a, b, w, ew; 17 | ccd_real_t dist; 18 | 19 | ccdVec3Set(&a, 0., 0., 0.); 20 | ccdVec3Set(&b, 1., 0., 0.); 21 | 22 | // extereme w == a 23 | ccdVec3Set(&P, -1., 0., 0.); 24 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 25 | assertTrue(ccdEq(dist, 1.)); 26 | assertTrue(ccdVec3Eq(&w, &a)); 27 | 28 | ccdVec3Set(&P, -0.5, 0., 0.); 29 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 30 | assertTrue(ccdEq(dist, 0.5 * 0.5)); 31 | assertTrue(ccdVec3Eq(&w, &a)); 32 | 33 | ccdVec3Set(&P, -0.1, 0., 0.); 34 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 35 | assertTrue(ccdEq(dist, .1 * .1)); 36 | assertTrue(ccdVec3Eq(&w, &a)); 37 | 38 | ccdVec3Set(&P, 0., 0., 0.); 39 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 40 | assertTrue(ccdEq(dist, 0.)); 41 | assertTrue(ccdVec3Eq(&w, &a)); 42 | 43 | ccdVec3Set(&P, -1., 1., 0.); 44 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 45 | assertTrue(ccdEq(dist, 2.)); 46 | assertTrue(ccdVec3Eq(&w, &a)); 47 | 48 | ccdVec3Set(&P, -0.5, 0.5, 0.); 49 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 50 | assertTrue(ccdEq(dist, 0.5)); 51 | assertTrue(ccdVec3Eq(&w, &a)); 52 | 53 | ccdVec3Set(&P, -0.1, -1., 2.); 54 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 55 | assertTrue(ccdEq(dist, 5.01)); 56 | assertTrue(ccdVec3Eq(&w, &a)); 57 | 58 | 59 | // extereme w == b 60 | ccdVec3Set(&P, 2., 0., 0.); 61 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 62 | assertTrue(ccdEq(dist, 1.)); 63 | assertTrue(ccdVec3Eq(&w, &b)); 64 | 65 | ccdVec3Set(&P, 1.5, 0., 0.); 66 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 67 | assertTrue(ccdEq(dist, 0.5 * 0.5)); 68 | assertTrue(ccdVec3Eq(&w, &b)); 69 | 70 | ccdVec3Set(&P, 1.1, 0., 0.); 71 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 72 | assertTrue(ccdEq(dist, .1 * .1)); 73 | assertTrue(ccdVec3Eq(&w, &b)); 74 | 75 | ccdVec3Set(&P, 1., 0., 0.); 76 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 77 | assertTrue(ccdEq(dist, 0.)); 78 | assertTrue(ccdVec3Eq(&w, &b)); 79 | 80 | ccdVec3Set(&P, 2., 1., 0.); 81 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 82 | assertTrue(ccdEq(dist, 2.)); 83 | assertTrue(ccdVec3Eq(&w, &b)); 84 | 85 | ccdVec3Set(&P, 1.5, 0.5, 0.); 86 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 87 | assertTrue(ccdEq(dist, 0.5)); 88 | assertTrue(ccdVec3Eq(&w, &b)); 89 | 90 | ccdVec3Set(&P, 1.1, -1., 2.); 91 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 92 | assertTrue(ccdEq(dist, 5.01)); 93 | assertTrue(ccdVec3Eq(&w, &b)); 94 | 95 | // inside segment 96 | ccdVec3Set(&P, .5, 0., 0.); 97 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 98 | assertTrue(ccdEq(dist, 0.)); 99 | assertTrue(ccdVec3Eq(&w, &P)); 100 | 101 | ccdVec3Set(&P, .9, 0., 0.); 102 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 103 | assertTrue(ccdEq(dist, 0.)); 104 | assertTrue(ccdVec3Eq(&w, &P)); 105 | 106 | ccdVec3Set(&P, .5, 1., 0.); 107 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 108 | assertTrue(ccdEq(dist, 1.)); 109 | ccdVec3Set(&ew, 0.5, 0., 0.); 110 | assertTrue(ccdVec3Eq(&w, &ew)); 111 | 112 | ccdVec3Set(&P, .5, 1., 1.); 113 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 114 | assertTrue(ccdEq(dist, 2.)); 115 | ccdVec3Set(&ew, 0.5, 0., 0.); 116 | assertTrue(ccdVec3Eq(&w, &ew)); 117 | 118 | 119 | 120 | ccdVec3Set(&a, -.5, 2., 1.); 121 | ccdVec3Set(&b, 1., 1.5, 0.5); 122 | 123 | // extereme w == a 124 | ccdVec3Set(&P, -10., 0., 0.); 125 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 126 | assertTrue(ccdEq(dist, 9.5 * 9.5 + 2. * 2. + 1.)); 127 | assertTrue(ccdVec3Eq(&w, &a)); 128 | 129 | ccdVec3Set(&P, -10., 9.2, 3.4); 130 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 131 | assertTrue(ccdEq(dist, 9.5 * 9.5 + 7.2 * 7.2 + 2.4 * 2.4)); 132 | assertTrue(ccdVec3Eq(&w, &a)); 133 | 134 | // extereme w == b 135 | ccdVec3Set(&P, 10., 0., 0.); 136 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 137 | assertTrue(ccdEq(dist, 9. * 9. + 1.5 * 1.5 + 0.5 * 0.5)); 138 | assertTrue(ccdVec3Eq(&w, &b)); 139 | 140 | ccdVec3Set(&P, 10., 9.2, 3.4); 141 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 142 | assertTrue(ccdEq(dist, 9. * 9. + 7.7 * 7.7 + 2.9 * 2.9)); 143 | assertTrue(ccdVec3Eq(&w, &b)); 144 | 145 | // inside ab 146 | ccdVec3Set(&a, -.1, 1., 1.); 147 | ccdVec3Set(&b, 1., 1., 1.); 148 | ccdVec3Set(&P, 0., 0., 0.); 149 | dist = ccdVec3PointSegmentDist2(&P, &a, &b, &w); 150 | assertTrue(ccdEq(dist, 2.)); 151 | ccdVec3Set(&ew, 0., 1., 1.); 152 | assertTrue(ccdVec3Eq(&w, &ew)); 153 | } 154 | 155 | 156 | TEST(vec3PointTriDist) 157 | { 158 | ccd_vec3_t P, a, b, c, w, P0; 159 | ccd_real_t dist; 160 | 161 | ccdVec3Set(&a, -1., 0., 0.); 162 | ccdVec3Set(&b, 0., 1., 1.); 163 | ccdVec3Set(&c, -1., 0., 1.); 164 | 165 | ccdVec3Set(&P, -1., 0., 0.); 166 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 167 | assertTrue(ccdEq(dist, 0.)); 168 | assertTrue(ccdVec3Eq(&w, &a)); 169 | 170 | ccdVec3Set(&P, 0., 1., 1.); 171 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 172 | assertTrue(ccdEq(dist, 0.)); 173 | assertTrue(ccdVec3Eq(&w, &b)); 174 | 175 | ccdVec3Set(&P, -1., 0., 1.); 176 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 177 | assertTrue(ccdEq(dist, 0.)); 178 | assertTrue(ccdVec3Eq(&w, &c)); 179 | 180 | ccdVec3Set(&P, 0., 0., 0.); 181 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, NULL); 182 | assertTrue(ccdEq(dist, 2./3.)); 183 | 184 | 185 | // region 4 186 | ccdVec3Set(&P, -2., 0., 0.); 187 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 188 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &a))); 189 | assertTrue(ccdVec3Eq(&w, &a)); 190 | ccdVec3Set(&P, -2., 0.2, -1.); 191 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 192 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &a))); 193 | assertTrue(ccdVec3Eq(&w, &a)); 194 | 195 | // region 2 196 | ccdVec3Set(&P, -1.3, 0., 1.2); 197 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 198 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &c))); 199 | assertTrue(ccdVec3Eq(&w, &c)); 200 | ccdVec3Set(&P, -1.2, 0.2, 1.1); 201 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 202 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &c))); 203 | assertTrue(ccdVec3Eq(&w, &c)); 204 | 205 | // region 6 206 | ccdVec3Set(&P, 0.3, 1., 1.); 207 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 208 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &b))); 209 | assertTrue(ccdVec3Eq(&w, &b)); 210 | ccdVec3Set(&P, .1, 1., 1.); 211 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 212 | assertTrue(ccdEq(dist, ccdVec3Dist2(&P, &b))); 213 | assertTrue(ccdVec3Eq(&w, &b)); 214 | 215 | // region 1 216 | ccdVec3Set(&P, 0., 1., 2.); 217 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 218 | assertTrue(ccdEq(dist, 1.)); 219 | assertTrue(ccdVec3Eq(&w, &b)); 220 | ccdVec3Set(&P, -1., 0., 2.); 221 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 222 | assertTrue(ccdEq(dist, 1.)); 223 | assertTrue(ccdVec3Eq(&w, &c)); 224 | ccdVec3Set(&P, -0.5, 0.5, 2.); 225 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 226 | assertTrue(ccdEq(dist, 1.)); 227 | ccdVec3Set(&P0, -0.5, 0.5, 1.); 228 | assertTrue(ccdVec3Eq(&w, &P0)); 229 | 230 | // region 3 231 | ccdVec3Set(&P, -2., -1., 0.7); 232 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 233 | assertTrue(ccdEq(dist, 2.)); 234 | ccdVec3Set(&P0, -1., 0., 0.7); 235 | assertTrue(ccdVec3Eq(&w, &P0)); 236 | 237 | // region 5 238 | ccdVec3Set(&P, 0., 0., 0.); 239 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 240 | assertTrue(ccdEq(dist, 2./3.)); 241 | ccdVec3Set(&P0, -2./3., 1./3., 1./3.); 242 | assertTrue(ccdVec3Eq(&w, &P0)); 243 | 244 | // region 0 245 | ccdVec3Set(&P, -0.5, 0.5, 0.5); 246 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 247 | assertTrue(ccdEq(dist, 0.)); 248 | assertTrue(ccdVec3Eq(&w, &P)); 249 | ccdVec3Set(&P, -0.5, 0.5, 0.7); 250 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 251 | assertTrue(ccdEq(dist, 0.)); 252 | assertTrue(ccdVec3Eq(&w, &P)); 253 | ccdVec3Set(&P, -0.5, 0.5, 0.9); 254 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 255 | assertTrue(ccdEq(dist, 0.)); 256 | assertTrue(ccdVec3Eq(&w, &P)); 257 | 258 | ccdVec3Set(&P, 0., 0., 0.5); 259 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 260 | assertTrue(ccdEq(dist, 0.5)); 261 | ccdVec3Set(&P0, -.5, .5, .5); 262 | assertTrue(ccdVec3Eq(&w, &P0)); 263 | 264 | ccdVec3Set(&a, -1., 0., 0.); 265 | ccdVec3Set(&b, 0., 1., -1.); 266 | ccdVec3Set(&c, 0., 1., 1.); 267 | ccdVec3Set(&P, 0., 0., 0.); 268 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 269 | assertTrue(ccdEq(dist, 0.5)); 270 | ccdVec3Set(&P0, -.5, .5, 0.); 271 | assertTrue(ccdVec3Eq(&w, &P0)); 272 | //fprintf(stderr, "dist: %lf\n", dist); 273 | 274 | ccdVec3Set(&a, -0.36715889, 0.288464308, 0.000100158155); 275 | ccdVec3Set(&b, -0.0222680569, 0.0171524286, -2.88337469e-06); 276 | ccdVec3Set(&c, 3.0792e-01, -2.4249e-01, 3.8363e-05); 277 | ccdVec3Set(&P, 0., 0., 0.); 278 | dist = ccdVec3PointTriDist2(&P, &a, &b, &c, &w); 279 | assertTrue(dist < 0.0000001); 280 | } 281 | -------------------------------------------------------------------------------- /src/testsuites/vec3.h: -------------------------------------------------------------------------------- 1 | #ifndef TEST_VEC3_H 2 | #define TEST_VEC3_H 3 | 4 | #include 5 | 6 | TEST(vec3SetUp); 7 | TEST(vec3TearDown); 8 | TEST(vec3PointSegmentDist); 9 | TEST(vec3PointTriDist); 10 | 11 | TEST_SUITE(TSVec3) { 12 | TEST_ADD(vec3SetUp), 13 | 14 | TEST_ADD(vec3PointSegmentDist), 15 | TEST_ADD(vec3PointTriDist), 16 | 17 | TEST_ADD(vec3TearDown), 18 | TEST_SUITE_CLOSURE 19 | }; 20 | #endif 21 | -------------------------------------------------------------------------------- /src/vec3.c: -------------------------------------------------------------------------------- 1 | /*** 2 | * libccd 3 | * --------------------------------- 4 | * Copyright (c)2010 Daniel Fiser 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 | * . 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 | #include 19 | #include 20 | #include "dbg.h" 21 | 22 | static CCD_VEC3(__ccd_vec3_origin, CCD_ZERO, CCD_ZERO, CCD_ZERO); 23 | ccd_vec3_t *ccd_vec3_origin = &__ccd_vec3_origin; 24 | 25 | static ccd_vec3_t points_on_sphere[] = { 26 | CCD_VEC3_STATIC(CCD_REAL( 0.000000), CCD_REAL(-0.000000), CCD_REAL(-1.000000)), 27 | CCD_VEC3_STATIC(CCD_REAL( 0.723608), CCD_REAL(-0.525725), CCD_REAL(-0.447219)), 28 | CCD_VEC3_STATIC(CCD_REAL(-0.276388), CCD_REAL(-0.850649), CCD_REAL(-0.447219)), 29 | CCD_VEC3_STATIC(CCD_REAL(-0.894426), CCD_REAL(-0.000000), CCD_REAL(-0.447216)), 30 | CCD_VEC3_STATIC(CCD_REAL(-0.276388), CCD_REAL( 0.850649), CCD_REAL(-0.447220)), 31 | CCD_VEC3_STATIC(CCD_REAL( 0.723608), CCD_REAL( 0.525725), CCD_REAL(-0.447219)), 32 | CCD_VEC3_STATIC(CCD_REAL( 0.276388), CCD_REAL(-0.850649), CCD_REAL( 0.447220)), 33 | CCD_VEC3_STATIC(CCD_REAL(-0.723608), CCD_REAL(-0.525725), CCD_REAL( 0.447219)), 34 | CCD_VEC3_STATIC(CCD_REAL(-0.723608), CCD_REAL( 0.525725), CCD_REAL( 0.447219)), 35 | CCD_VEC3_STATIC(CCD_REAL( 0.276388), CCD_REAL( 0.850649), CCD_REAL( 0.447219)), 36 | CCD_VEC3_STATIC(CCD_REAL( 0.894426), CCD_REAL( 0.000000), CCD_REAL( 0.447216)), 37 | CCD_VEC3_STATIC(CCD_REAL(-0.000000), CCD_REAL( 0.000000), CCD_REAL( 1.000000)), 38 | CCD_VEC3_STATIC(CCD_REAL( 0.425323), CCD_REAL(-0.309011), CCD_REAL(-0.850654)), 39 | CCD_VEC3_STATIC(CCD_REAL(-0.162456), CCD_REAL(-0.499995), CCD_REAL(-0.850654)), 40 | CCD_VEC3_STATIC(CCD_REAL( 0.262869), CCD_REAL(-0.809012), CCD_REAL(-0.525738)), 41 | CCD_VEC3_STATIC(CCD_REAL( 0.425323), CCD_REAL( 0.309011), CCD_REAL(-0.850654)), 42 | CCD_VEC3_STATIC(CCD_REAL( 0.850648), CCD_REAL(-0.000000), CCD_REAL(-0.525736)), 43 | CCD_VEC3_STATIC(CCD_REAL(-0.525730), CCD_REAL(-0.000000), CCD_REAL(-0.850652)), 44 | CCD_VEC3_STATIC(CCD_REAL(-0.688190), CCD_REAL(-0.499997), CCD_REAL(-0.525736)), 45 | CCD_VEC3_STATIC(CCD_REAL(-0.162456), CCD_REAL( 0.499995), CCD_REAL(-0.850654)), 46 | CCD_VEC3_STATIC(CCD_REAL(-0.688190), CCD_REAL( 0.499997), CCD_REAL(-0.525736)), 47 | CCD_VEC3_STATIC(CCD_REAL( 0.262869), CCD_REAL( 0.809012), CCD_REAL(-0.525738)), 48 | CCD_VEC3_STATIC(CCD_REAL( 0.951058), CCD_REAL( 0.309013), CCD_REAL( 0.000000)), 49 | CCD_VEC3_STATIC(CCD_REAL( 0.951058), CCD_REAL(-0.309013), CCD_REAL( 0.000000)), 50 | CCD_VEC3_STATIC(CCD_REAL( 0.587786), CCD_REAL(-0.809017), CCD_REAL( 0.000000)), 51 | CCD_VEC3_STATIC(CCD_REAL( 0.000000), CCD_REAL(-1.000000), CCD_REAL( 0.000000)), 52 | CCD_VEC3_STATIC(CCD_REAL(-0.587786), CCD_REAL(-0.809017), CCD_REAL( 0.000000)), 53 | CCD_VEC3_STATIC(CCD_REAL(-0.951058), CCD_REAL(-0.309013), CCD_REAL(-0.000000)), 54 | CCD_VEC3_STATIC(CCD_REAL(-0.951058), CCD_REAL( 0.309013), CCD_REAL(-0.000000)), 55 | CCD_VEC3_STATIC(CCD_REAL(-0.587786), CCD_REAL( 0.809017), CCD_REAL(-0.000000)), 56 | CCD_VEC3_STATIC(CCD_REAL(-0.000000), CCD_REAL( 1.000000), CCD_REAL(-0.000000)), 57 | CCD_VEC3_STATIC(CCD_REAL( 0.587786), CCD_REAL( 0.809017), CCD_REAL(-0.000000)), 58 | CCD_VEC3_STATIC(CCD_REAL( 0.688190), CCD_REAL(-0.499997), CCD_REAL( 0.525736)), 59 | CCD_VEC3_STATIC(CCD_REAL(-0.262869), CCD_REAL(-0.809012), CCD_REAL( 0.525738)), 60 | CCD_VEC3_STATIC(CCD_REAL(-0.850648), CCD_REAL( 0.000000), CCD_REAL( 0.525736)), 61 | CCD_VEC3_STATIC(CCD_REAL(-0.262869), CCD_REAL( 0.809012), CCD_REAL( 0.525738)), 62 | CCD_VEC3_STATIC(CCD_REAL( 0.688190), CCD_REAL( 0.499997), CCD_REAL( 0.525736)), 63 | CCD_VEC3_STATIC(CCD_REAL( 0.525730), CCD_REAL( 0.000000), CCD_REAL( 0.850652)), 64 | CCD_VEC3_STATIC(CCD_REAL( 0.162456), CCD_REAL(-0.499995), CCD_REAL( 0.850654)), 65 | CCD_VEC3_STATIC(CCD_REAL(-0.425323), CCD_REAL(-0.309011), CCD_REAL( 0.850654)), 66 | CCD_VEC3_STATIC(CCD_REAL(-0.425323), CCD_REAL( 0.309011), CCD_REAL( 0.850654)), 67 | CCD_VEC3_STATIC(CCD_REAL( 0.162456), CCD_REAL( 0.499995), CCD_REAL( 0.850654)) 68 | }; 69 | ccd_vec3_t *ccd_points_on_sphere = points_on_sphere; 70 | size_t ccd_points_on_sphere_len = sizeof(points_on_sphere) / sizeof(ccd_vec3_t); 71 | 72 | 73 | _ccd_inline ccd_real_t __ccdVec3PointSegmentDist2(const ccd_vec3_t *P, 74 | const ccd_vec3_t *x0, 75 | const ccd_vec3_t *b, 76 | ccd_vec3_t *witness) 77 | { 78 | // The computation comes from solving equation of segment: 79 | // S(t) = x0 + t.d 80 | // where - x0 is initial point of segment 81 | // - d is direction of segment from x0 (|d| > 0) 82 | // - t belongs to <0, 1> interval 83 | // 84 | // Than, distance from a segment to some point P can be expressed: 85 | // D(t) = |x0 + t.d - P|^2 86 | // which is distance from any point on segment. Minimization 87 | // of this function brings distance from P to segment. 88 | // Minimization of D(t) leads to simple quadratic equation that's 89 | // solving is straightforward. 90 | // 91 | // Bonus of this method is witness point for free. 92 | 93 | ccd_real_t dist, t; 94 | ccd_vec3_t d, a; 95 | 96 | // direction of segment 97 | ccdVec3Sub2(&d, b, x0); 98 | 99 | // precompute vector from P to x0 100 | ccdVec3Sub2(&a, x0, P); 101 | 102 | t = -CCD_REAL(1.) * ccdVec3Dot(&a, &d); 103 | t /= ccdVec3Len2(&d); 104 | 105 | if (t < CCD_ZERO || ccdIsZero(t)){ 106 | dist = ccdVec3Dist2(x0, P); 107 | if (witness) 108 | ccdVec3Copy(witness, x0); 109 | }else if (t > CCD_ONE || ccdEq(t, CCD_ONE)){ 110 | dist = ccdVec3Dist2(b, P); 111 | if (witness) 112 | ccdVec3Copy(witness, b); 113 | }else{ 114 | if (witness){ 115 | ccdVec3Copy(witness, &d); 116 | ccdVec3Scale(witness, t); 117 | ccdVec3Add(witness, x0); 118 | dist = ccdVec3Dist2(witness, P); 119 | }else{ 120 | // recycling variables 121 | ccdVec3Scale(&d, t); 122 | ccdVec3Add(&d, &a); 123 | dist = ccdVec3Len2(&d); 124 | } 125 | } 126 | 127 | return dist; 128 | } 129 | 130 | ccd_real_t ccdVec3PointSegmentDist2(const ccd_vec3_t *P, 131 | const ccd_vec3_t *x0, const ccd_vec3_t *b, 132 | ccd_vec3_t *witness) 133 | { 134 | return __ccdVec3PointSegmentDist2(P, x0, b, witness); 135 | } 136 | 137 | ccd_real_t ccdVec3PointTriDist2(const ccd_vec3_t *P, 138 | const ccd_vec3_t *x0, const ccd_vec3_t *B, 139 | const ccd_vec3_t *C, 140 | ccd_vec3_t *witness) 141 | { 142 | // Computation comes from analytic expression for triangle (x0, B, C) 143 | // T(s, t) = x0 + s.d1 + t.d2, where d1 = B - x0 and d2 = C - x0 and 144 | // Then equation for distance is: 145 | // D(s, t) = | T(s, t) - P |^2 146 | // This leads to minimization of quadratic function of two variables. 147 | // The solution from is taken only if s is between 0 and 1, t is 148 | // between 0 and 1 and t + s < 1, otherwise distance from segment is 149 | // computed. 150 | 151 | ccd_vec3_t d1, d2, a; 152 | ccd_real_t u, v, w, p, q, r, d; 153 | ccd_real_t s, t, dist, dist2; 154 | ccd_vec3_t witness2; 155 | 156 | ccdVec3Sub2(&d1, B, x0); 157 | ccdVec3Sub2(&d2, C, x0); 158 | ccdVec3Sub2(&a, x0, P); 159 | 160 | u = ccdVec3Dot(&a, &a); 161 | v = ccdVec3Dot(&d1, &d1); 162 | w = ccdVec3Dot(&d2, &d2); 163 | p = ccdVec3Dot(&a, &d1); 164 | q = ccdVec3Dot(&a, &d2); 165 | r = ccdVec3Dot(&d1, &d2); 166 | 167 | d = w * v - r * r; 168 | if (ccdIsZero(d)){ 169 | // To avoid division by zero for zero (or near zero) area triangles 170 | s = t = -1.; 171 | }else{ 172 | s = (q * r - w * p) / d; 173 | t = (-s * r - q) / w; 174 | } 175 | 176 | if ((ccdIsZero(s) || s > CCD_ZERO) 177 | && (ccdEq(s, CCD_ONE) || s < CCD_ONE) 178 | && (ccdIsZero(t) || t > CCD_ZERO) 179 | && (ccdEq(t, CCD_ONE) || t < CCD_ONE) 180 | && (ccdEq(t + s, CCD_ONE) || t + s < CCD_ONE)){ 181 | 182 | if (witness){ 183 | ccdVec3Scale(&d1, s); 184 | ccdVec3Scale(&d2, t); 185 | ccdVec3Copy(witness, x0); 186 | ccdVec3Add(witness, &d1); 187 | ccdVec3Add(witness, &d2); 188 | 189 | dist = ccdVec3Dist2(witness, P); 190 | }else{ 191 | dist = s * s * v; 192 | dist += t * t * w; 193 | dist += CCD_REAL(2.) * s * t * r; 194 | dist += CCD_REAL(2.) * s * p; 195 | dist += CCD_REAL(2.) * t * q; 196 | dist += u; 197 | } 198 | }else{ 199 | dist = __ccdVec3PointSegmentDist2(P, x0, B, witness); 200 | 201 | dist2 = __ccdVec3PointSegmentDist2(P, x0, C, &witness2); 202 | if (dist2 < dist){ 203 | dist = dist2; 204 | if (witness) 205 | ccdVec3Copy(witness, &witness2); 206 | } 207 | 208 | dist2 = __ccdVec3PointSegmentDist2(P, B, C, &witness2); 209 | if (dist2 < dist){ 210 | dist = dist2; 211 | if (witness) 212 | ccdVec3Copy(witness, &witness2); 213 | } 214 | } 215 | 216 | return dist; 217 | } 218 | --------------------------------------------------------------------------------