File: examples.rst

package info (click to toggle)
libccd 2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: ansic: 5,295; makefile: 376; python: 319; sh: 165
file content (185 lines) | stat: -rw-r--r-- 6,269 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
Example of Usage
=================

1. GJK - Intersection Test
---------------------------
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.

Procedure is very simple (and similar to the usage of the rest of the
library):

#. Include ``<ccd/ccd.h>`` file.
#. Implement support function for specific shapes. Support function is
   function that returns furthest point from object (shape) in specified
   direction.
#. Set up ``ccd_t`` structure.
#. Run ``ccdGJKIntersect()`` function on desired objects.


Here is a skeleton of simple program:

.. code-block:: c

    #include <ccd/ccd.h>
    #include <ccd/quat.h> // for work with quaternions

    /** Support function for box */
    void support(const void *obj, const ccd_vec3_t *dir,
                 ccd_vec3_t *vec)
    {
        // assume that obj_t is user-defined structure that holds info about
        // object (in this case box: x, y, z, pos, quat - dimensions of box,
        // position and rotation)
        obj_t *obj = (obj_t *)_obj;
        ccd_vec3_t dir;
        ccd_quat_t qinv;

        // apply rotation on direction vector
        ccdVec3Copy(&dir, _dir);
        ccdQuatInvert2(&qinv, &obj->quat);
        ccdQuatRotVec(&dir, &qinv);

        // compute support point in specified direction
        ccdVec3Set(v, ccdSign(ccdVec3X(&dir)) * box->x * CCD_REAL(0.5),
                      ccdSign(ccdVec3Y(&dir)) * box->y * CCD_REAL(0.5),
                      ccdSign(ccdVec3Z(&dir)) * box->z * CCD_REAL(0.5));

        // transform support point according to position and rotation of object
        ccdQuatRotVec(v, &obj->quat);
        ccdVec3Add(v, &obj->pos);
    }


    int main(int argc, char *argv[])
    {
        ...

        ccd_t ccd;
        CCD_INIT(&ccd); // initialize ccd_t struct
    
        // set up ccd_t struct
        ccd.support1       = support; // support function for first object
        ccd.support2       = support; // support function for second object
        ccd.max_iterations = 100;     // maximal number of iterations

        int intersect = ccdGJKIntersect(obj1, obj2, &ccd);
        // now intersect holds true if obj1 and obj2 intersect, false otherwise
    }




2. GJK + EPA - Penetration Of Two Objects
------------------------------------------

If you want to obtain also penetration info about two intersection objects
``ccdGJKPenetration()`` function can be used. 

Procedure is almost the same as for the previous case:

.. code-block:: c

    #include <ccd/ccd.h>
    #include <ccd/quat.h> // for work with quaternions

    /** Support function is same as in previous case */

    int main(int argc, char *argv[])
    {
        ...
        ccd_t ccd;
        CCD_INIT(&ccd); // initialize ccd_t struct

        // set up ccd_t struct
        ccd.support1       = support; // support function for first object
        ccd.support2       = support; // support function for second object
        ccd.max_iterations = 100;     // maximal number of iterations
        ccd.epa_tolerance  = 0.0001;  // maximal tolerance fro EPA part

        ccd_real_t depth;
        ccd_vec3_t dir, pos;
        int intersect = ccdGJKPenetration(obj1, obj2, &ccd, &depth, &dir, &pos);
        // now intersect holds true if obj1 and obj2 intersect, false otherwise
        // in depth, dir and pos is stored penetration depth, direction of
        // separation vector and position in global coordinate system
    }


3. MPR - Intersection Test
---------------------------

**libccd** also provides *MPR* - Minkowski Portal Refinement algorithm that
can be used for testing if two objects intersects.

Procedure is similar to the one used for GJK algorithm. Support function is
the same but also function that returns a center (or any point near center)
of a given object must be implemented:

.. code-block:: c

    #include <ccd/ccd.h>
    #include <ccd/quat.h> // for work with quaternions

    /** Support function is same as in previous case */

    /** Center function - returns center of object */
    void center(const void *_obj, ccd_vec3_t *center)
    {
        obj_t *obj = (obj_t *)_obj;
        ccdVec3Copy(center, &obj->pos);
    }

    int main(int argc, char *argv[])
    {
        ...
        ccd_t ccd;
        CCD_INIT(&ccd); // initialize ccd_t struct

        // set up ccd_t struct
        ccd.support1       = support; // support function for first object
        ccd.support2       = support; // support function for second object
        ccd.center1        = center;  // center function for first object
        ccd.center2        = center;  // center function for second object
        ccd.mpr_tolerance  = 0.0001;  // maximal tolerance

        int intersect = ccdMPRIntersect(obj1, obj2, &ccd);
        // now intersect holds true if obj1 and obj2 intersect, false otherwise
    }


4. MPR - Penetration Of Two Objects
------------------------------------

Using MPR algorithm for obtaining penetration info about two intersection
objects is equally easy as in the previous case instead but
``ccdMPRPenetration()`` function is used:

.. code-block:: c

    #include <ccd/ccd.h>
    #include <ccd/quat.h> // for work with quaternions

    /** Support function is same as in previous case */
    /** Center function is same as in prevous case */

    int main(int argc, char *argv[])
    {
        ...
        ccd_t ccd;
        CCD_INIT(&ccd); // initialize ccd_t struct

        // set up ccd_t struct
        ccd.support1       = support; // support function for first object
        ccd.support2       = support; // support function for second object
        ccd.center1        = center;  // center function for first object
        ccd.center2        = center;  // center function for second object
        ccd.mpr_tolerance  = 0.0001;  // maximal tolerance

        ccd_real_t depth;
        ccd_vec3_t dir, pos;
        int intersect = ccdMPRPenetration(obj1, obj2, &ccd, &depth, &dir, &pos);
        // now intersect holds true if obj1 and obj2 intersect, false otherwise
        // in depth, dir and pos is stored penetration depth, direction of
        // separation vector and position in global coordinate system
    }