summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/plane.cpp
blob: b54e894c7715442db69bcf6382370dd950353726 (plain)
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
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

/*

standard ODE geometry primitives: public API and pairwise collision functions.

the rule is that only the low level primitive collision functions should set
dContactGeom::g1 and dContactGeom::g2.

*/

#include <ode/common.h>
#include <ode/collision.h>
#include <ode/rotation.h>
#include "config.h"
#include "matrix.h"
#include "odemath.h"
#include "collision_kernel.h"
#include "collision_std.h"
#include "collision_util.h"

#ifdef _MSC_VER
#pragma warning(disable:4291)  // for VC++, no complaints about "no matching operator delete found"
#endif

//****************************************************************************
// plane public API

static void make_sure_plane_normal_has_unit_length (dxPlane *g)
{
    dReal l = g->p[0]*g->p[0] + g->p[1]*g->p[1] + g->p[2]*g->p[2];
    if (l > 0) {
        l = dRecipSqrt(l);
        g->p[0] *= l;
        g->p[1] *= l;
        g->p[2] *= l;
        g->p[3] *= l;
    }
    else {
        g->p[0] = 1;
        g->p[1] = 0;
        g->p[2] = 0;
        g->p[3] = 0;
    }
}


dxPlane::dxPlane (dSpaceID space, dReal a, dReal b, dReal c, dReal d) :
dxGeom (space,0)
{
    type = dPlaneClass;
    p[0] = a;
    p[1] = b;
    p[2] = c;
    p[3] = d;
    make_sure_plane_normal_has_unit_length (this);
}


void dxPlane::computeAABB()
{
    aabb[0] = -dInfinity;
    aabb[1] = dInfinity;
    aabb[2] = -dInfinity;
    aabb[3] = dInfinity;
    aabb[4] = -dInfinity;
    aabb[5] = dInfinity;

    // Planes that have normal vectors aligned along an axis can use a
    // less comprehensive (half space) bounding box.

    if ( p[1] == 0.0f && p[2] == 0.0f ) {
        // normal aligned with x-axis
        aabb[0] = (p[0] > 0) ? -dInfinity : -p[3];
        aabb[1] = (p[0] > 0) ? p[3] : dInfinity;
    } else
        if ( p[0] == 0.0f && p[2] == 0.0f ) {
            // normal aligned with y-axis
            aabb[2] = (p[1] > 0) ? -dInfinity : -p[3];
            aabb[3] = (p[1] > 0) ? p[3] : dInfinity;
        } else
            if ( p[0] == 0.0f && p[1] == 0.0f ) {
                // normal aligned with z-axis
                aabb[4] = (p[2] > 0) ? -dInfinity : -p[3];
                aabb[5] = (p[2] > 0) ? p[3] : dInfinity;
            }
}


dGeomID dCreatePlane (dSpaceID space,
                      dReal a, dReal b, dReal c, dReal d)
{
    return new dxPlane (space,a,b,c,d);
}


void dGeomPlaneSetParams (dGeomID g, dReal a, dReal b, dReal c, dReal d)
{
    dUASSERT (g && g->type == dPlaneClass,"argument not a plane");
    dxPlane *p = (dxPlane*) g;
    p->p[0] = a;
    p->p[1] = b;
    p->p[2] = c;
    p->p[3] = d;
    make_sure_plane_normal_has_unit_length (p);
    dGeomMoved (g);
}


void dGeomPlaneGetParams (dGeomID g, dVector4 result)
{
    dUASSERT (g && g->type == dPlaneClass,"argument not a plane");
    dxPlane *p = (dxPlane*) g;
    result[0] = p->p[0];
    result[1] = p->p[1];
    result[2] = p->p[2];
    result[3] = p->p[3];
}


dReal dGeomPlanePointDepth (dGeomID g, dReal x, dReal y, dReal z)
{
    dUASSERT (g && g->type == dPlaneClass,"argument not a plane");
    dxPlane *p = (dxPlane*) g;
    return p->p[3] - p->p[0]*x - p->p[1]*y - p->p[2]*z;
}