~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/bullet/src/BulletCollision/CollisionShapes/btConvexPolyhedron.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Bullet Continuous Collision Detection and Physics Library
 
3
Copyright (c) 2011 Advanced Micro Devices, Inc.  http://bulletphysics.org
 
4
 
 
5
This software is provided 'as-is', without any express or implied warranty.
 
6
In no event will the authors be held liable for any damages arising from the use of this software.
 
7
Permission is granted to anyone to use this software for any purpose, 
 
8
including commercial applications, and to alter it and redistribute it freely, 
 
9
subject to the following restrictions:
 
10
 
 
11
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
 
12
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
 
13
3. This notice may not be removed or altered from any source distribution.
 
14
*/
 
15
 
 
16
 
 
17
///This file was written by Erwin Coumans
 
18
///Separating axis rest based on work from Pierre Terdiman, see
 
19
///And contact clipping based on work from Simon Hobbs
 
20
 
 
21
 
 
22
#include "btConvexPolyhedron.h"
 
23
#include "LinearMath/btHashMap.h"
 
24
 
 
25
btConvexPolyhedron::btConvexPolyhedron()
 
26
{
 
27
 
 
28
}
 
29
btConvexPolyhedron::~btConvexPolyhedron()
 
30
{
 
31
 
 
32
}
 
33
 
 
34
 
 
35
inline bool IsAlmostZero(const btVector3& v)
 
36
{
 
37
        if(fabsf(v.x())>1e-6 || fabsf(v.y())>1e-6 || fabsf(v.z())>1e-6) return false;
 
38
        return true;
 
39
}
 
40
 
 
41
struct btInternalVertexPair
 
42
{
 
43
        btInternalVertexPair(short int v0,short int v1)
 
44
                :m_v0(v0),
 
45
                m_v1(v1)
 
46
        {
 
47
                if (m_v1>m_v0)
 
48
                        btSwap(m_v0,m_v1);
 
49
        }
 
50
        short int m_v0;
 
51
        short int m_v1;
 
52
        int getHash() const
 
53
        {
 
54
                return m_v0+(m_v1<<16);
 
55
        }
 
56
        bool equals(const btInternalVertexPair& other) const
 
57
        {
 
58
                return m_v0==other.m_v0 && m_v1==other.m_v1;
 
59
        }
 
60
};
 
61
 
 
62
struct btInternalEdge
 
63
{
 
64
        btInternalEdge()
 
65
                :m_face0(-1),
 
66
                m_face1(-1)
 
67
        {
 
68
        }
 
69
        short int m_face0;
 
70
        short int m_face1;
 
71
};
 
72
 
 
73
//
 
74
 
 
75
void    btConvexPolyhedron::initialize()
 
76
{
 
77
        btHashMap<btInternalVertexPair,btInternalEdge> edges;
 
78
 
 
79
        float TotalArea = 0.0f;
 
80
        
 
81
        m_localCenter.setValue(0, 0, 0);
 
82
        for(int i=0;i<m_faces.size();i++)
 
83
        {
 
84
                int numVertices = m_faces[i].m_indices.size();
 
85
                int NbTris = numVertices;
 
86
                for(int j=0;j<NbTris;j++)
 
87
                {
 
88
                        int k = (j+1)%numVertices;
 
89
                        btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
 
90
                        btInternalEdge* edptr = edges.find(vp);
 
91
                        btVector3 edge = m_vertices[vp.m_v1]-m_vertices[vp.m_v0];
 
92
                        edge.normalize();
 
93
 
 
94
                        bool found = false;
 
95
 
 
96
                        for (int p=0;p<m_uniqueEdges.size();p++)
 
97
                        {
 
98
                                
 
99
                                if (IsAlmostZero(m_uniqueEdges[p]-edge) || 
 
100
                                        IsAlmostZero(m_uniqueEdges[p]+edge))
 
101
                                {
 
102
                                        found = true;
 
103
                                        break;
 
104
                                }
 
105
                        }
 
106
 
 
107
                        if (!found)
 
108
                        {
 
109
                                m_uniqueEdges.push_back(edge);
 
110
                        }
 
111
 
 
112
                        if (edptr)
 
113
                        {
 
114
                                btAssert(edptr->m_face0>=0);
 
115
                                btAssert(edptr->m_face1<0);
 
116
                                edptr->m_face1 = i;
 
117
                        } else
 
118
                        {
 
119
                                btInternalEdge ed;
 
120
                                ed.m_face0 = i;
 
121
                                edges.insert(vp,ed);
 
122
                        }
 
123
                }
 
124
        }
 
125
 
 
126
        for(int i=0;i<m_faces.size();i++)
 
127
        {
 
128
                int numVertices = m_faces[i].m_indices.size();
 
129
                m_faces[i].m_connectedFaces.resize(numVertices);
 
130
 
 
131
                for(int j=0;j<numVertices;j++)
 
132
                {
 
133
                        int k = (j+1)%numVertices;
 
134
                        btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
 
135
                        btInternalEdge* edptr = edges.find(vp);
 
136
                        btAssert(edptr);
 
137
                        btAssert(edptr->m_face0>=0);
 
138
                        btAssert(edptr->m_face1>=0);
 
139
 
 
140
                        int connectedFace = (edptr->m_face0==i)?edptr->m_face1:edptr->m_face0;
 
141
                        m_faces[i].m_connectedFaces[j] = connectedFace;
 
142
                }
 
143
        }
 
144
 
 
145
        for(int i=0;i<m_faces.size();i++)
 
146
        {
 
147
                int numVertices = m_faces[i].m_indices.size();
 
148
                int NbTris = numVertices-2;
 
149
                
 
150
                const btVector3& p0 = m_vertices[m_faces[i].m_indices[0]];
 
151
                for(int j=1;j<=NbTris;j++)
 
152
                {
 
153
                        int k = (j+1)%numVertices;
 
154
                        const btVector3& p1 = m_vertices[m_faces[i].m_indices[j]];
 
155
                        const btVector3& p2 = m_vertices[m_faces[i].m_indices[k]];
 
156
                        float Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
 
157
                        btVector3 Center = (p0+p1+p2)/3.0f;
 
158
                        m_localCenter += Area * Center;
 
159
                        TotalArea += Area;
 
160
                }
 
161
        }
 
162
        m_localCenter /= TotalArea;
 
163
 
 
164
}
 
165
 
 
166
 
 
167
void btConvexPolyhedron::project(const btTransform& trans, const btVector3& dir, float& min, float& max) const
 
168
{
 
169
        min = FLT_MAX;
 
170
        max = -FLT_MAX;
 
171
        int numVerts = m_vertices.size();
 
172
        for(int i=0;i<numVerts;i++)
 
173
        {
 
174
                btVector3 pt = trans * m_vertices[i];
 
175
                float dp = pt.dot(dir);
 
176
                if(dp < min)    min = dp;
 
177
                if(dp > max)    max = dp;
 
178
        }
 
179
        if(min>max)
 
180
        {
 
181
                float tmp = min;
 
182
                min = max;
 
183
                max = tmp;
 
184
        }
 
185
}
 
 
b'\\ No newline at end of file'