Bullet Collision Detection & Physics Library
btReducedVector.h
Go to the documentation of this file.
1//
2// btReducedVectors.h
3// BulletLinearMath
4//
5// Created by Xuchen Han on 4/4/20.
6//
7#ifndef btReducedVectors_h
8#define btReducedVectors_h
9#include "btVector3.h"
10#include "btMatrix3x3.h"
12#include <stdio.h>
13#include <vector>
14#include <algorithm>
15struct TwoInts
16{
17 int a,b;
18};
19inline bool operator<(const TwoInts& A, const TwoInts& B)
20{
21 return A.b < B.b;
22}
23
24
25// A helper vector type used for CG projections
27{
28public:
31 int m_sz; // all m_indices value < m_sz
32public:
34 {
36 m_vecs.resize(0);
38 m_vecs.clear();
39 }
40
41 btReducedVector(int sz): m_sz(sz)
42 {
44 m_vecs.resize(0);
46 m_vecs.clear();
47 }
48
49 btReducedVector(int sz, const btAlignedObjectArray<int>& indices, const btAlignedObjectArray<btVector3>& vecs): m_sz(sz), m_indices(indices), m_vecs(vecs)
50 {
51 }
52
53 void simplify()
54 {
58 m_vecs.resize(0);
60 m_vecs.clear();
61 for (int i = 0; i < old_indices.size(); ++i)
62 {
63 if (old_vecs[i].length2() > SIMD_EPSILON)
64 {
65 m_indices.push_back(old_indices[i]);
66 m_vecs.push_back(old_vecs[i]);
67 }
68 }
69 }
70
72 {
74 int i=0, j=0;
75 while (i < m_indices.size() && j < other.m_indices.size())
76 {
77 if (m_indices[i] < other.m_indices[j])
78 {
80 ret.m_vecs.push_back(m_vecs[i]);
81 ++i;
82 }
83 else if (m_indices[i] > other.m_indices[j])
84 {
85 ret.m_indices.push_back(other.m_indices[j]);
86 ret.m_vecs.push_back(other.m_vecs[j]);
87 ++j;
88 }
89 else
90 {
91 ret.m_indices.push_back(other.m_indices[j]);
92 ret.m_vecs.push_back(m_vecs[i] + other.m_vecs[j]);
93 ++i; ++j;
94 }
95 }
96 while (i < m_indices.size())
97 {
99 ret.m_vecs.push_back(m_vecs[i]);
100 ++i;
101 }
102 while (j < other.m_indices.size())
103 {
104 ret.m_indices.push_back(other.m_indices[j]);
105 ret.m_vecs.push_back(other.m_vecs[j]);
106 ++j;
107 }
108 ret.simplify();
109 return ret;
110 }
111
113 {
115 for (int i = 0; i < m_indices.size(); ++i)
116 {
118 ret.m_vecs.push_back(-m_vecs[i]);
119 }
120 ret.simplify();
121 return ret;
122 }
123
125 {
127 int i=0, j=0;
128 while (i < m_indices.size() && j < other.m_indices.size())
129 {
130 if (m_indices[i] < other.m_indices[j])
131 {
133 ret.m_vecs.push_back(m_vecs[i]);
134 ++i;
135 }
136 else if (m_indices[i] > other.m_indices[j])
137 {
138 ret.m_indices.push_back(other.m_indices[j]);
139 ret.m_vecs.push_back(-other.m_vecs[j]);
140 ++j;
141 }
142 else
143 {
144 ret.m_indices.push_back(other.m_indices[j]);
145 ret.m_vecs.push_back(m_vecs[i] - other.m_vecs[j]);
146 ++i; ++j;
147 }
148 }
149 while (i < m_indices.size())
150 {
152 ret.m_vecs.push_back(m_vecs[i]);
153 ++i;
154 }
155 while (j < other.m_indices.size())
156 {
157 ret.m_indices.push_back(other.m_indices[j]);
158 ret.m_vecs.push_back(-other.m_vecs[j]);
159 ++j;
160 }
161 ret.simplify();
162 return ret;
163 }
164
165 bool operator==(const btReducedVector& other) const
166 {
167 if (m_sz != other.m_sz)
168 return false;
169 if (m_indices.size() != other.m_indices.size())
170 return false;
171 for (int i = 0; i < m_indices.size(); ++i)
172 {
173 if (m_indices[i] != other.m_indices[i] || m_vecs[i] != other.m_vecs[i])
174 {
175 return false;
176 }
177 }
178 return true;
179 }
180
181 bool operator!=(const btReducedVector& other) const
182 {
183 return !(*this == other);
184 }
185
187 {
188 if (this == &other)
189 {
190 return *this;
191 }
192 m_sz = other.m_sz;
195 return *this;
196 }
197
198 btScalar dot(const btReducedVector& other) const
199 {
200 btScalar ret = 0;
201 int j = 0;
202 for (int i = 0; i < m_indices.size(); ++i)
203 {
204 while (j < other.m_indices.size() && other.m_indices[j] < m_indices[i])
205 {
206 ++j;
207 }
208 if (j < other.m_indices.size() && other.m_indices[j] == m_indices[i])
209 {
210 ret += m_vecs[i].dot(other.m_vecs[j]);
211// ++j;
212 }
213 }
214 return ret;
215 }
216
218 {
219 btScalar ret = 0;
220 for (int i = 0; i < m_indices.size(); ++i)
221 {
222 ret += m_vecs[i].dot(other[m_indices[i]]);
223 }
224 return ret;
225 }
226
228 {
229 return this->dot(*this);
230 }
231
232 void normalize();
233
234 // returns the projection of this onto other
235 btReducedVector proj(const btReducedVector& other) const;
236
237 bool testAdd() const;
238
239 bool testMinus() const;
240
241 bool testDot() const;
242
243 bool testMultiply() const;
244
245 void test() const;
246
247 void print() const
248 {
249 for (int i = 0; i < m_indices.size(); ++i)
250 {
251 printf("%d: (%f, %f, %f)/", m_indices[i], m_vecs[i][0],m_vecs[i][1],m_vecs[i][2]);
252 }
253 printf("\n");
254 }
255
256
257 void sort()
258 {
259 std::vector<TwoInts> tuples;
260 for (int i = 0; i < m_indices.size(); ++i)
261 {
262 TwoInts ti;
263 ti.a = i;
264 ti.b = m_indices[i];
265 tuples.push_back(ti);
266 }
267 std::sort(tuples.begin(), tuples.end());
268 btAlignedObjectArray<int> new_indices;
270 for (size_t i = 0; i < tuples.size(); ++i)
271 {
272 new_indices.push_back(tuples[i].b);
273 new_vecs.push_back(m_vecs[tuples[i].a]);
274 }
275 m_indices = new_indices;
276 m_vecs = new_vecs;
277 }
278};
279
281{
282 btReducedVector ret(v.m_sz);
283 for (int i = 0; i < v.m_indices.size(); ++i)
284 {
285 ret.m_indices.push_back(v.m_indices[i]);
286 ret.m_vecs.push_back(s*v.m_vecs[i]);
287 }
288 ret.simplify();
289 return ret;
290}
291
293{
294 return v*s;
295}
296
298{
299 return v * (1.0/s);
300}
301
303{
304 v = v/s;
305 return v;
306}
307
309{
310 v1 = v1+v2;
311 return v1;
312}
313
315{
316 v1 = v1-v2;
317 return v1;
318}
319
320#endif /* btReducedVectors_h */
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
btReducedVector operator/(const btReducedVector &v, btScalar s)
btReducedVector & operator/=(btReducedVector &v, btScalar s)
btReducedVector & operator-=(btReducedVector &v1, const btReducedVector &v2)
btReducedVector operator*(const btReducedVector &v, btScalar s)
bool operator<(const TwoInts &A, const TwoInts &B)
btReducedVector & operator+=(btReducedVector &v1, const btReducedVector &v2)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
#define SIMD_EPSILON
Definition: btScalar.h:543
int size() const
return the number of elements in the array
void copyFromArray(const btAlignedObjectArray &otherArray)
void resize(int newsize, const T &fillData=T())
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0),...
void push_back(const T &_Val)
bool testAdd() const
bool testDot() const
bool testMinus() const
btScalar length2() const
btAlignedObjectArray< int > m_indices
btReducedVector operator-(const btReducedVector &other)
btReducedVector proj(const btReducedVector &other) const
btReducedVector operator+(const btReducedVector &other)
bool operator==(const btReducedVector &other) const
btReducedVector(int sz, const btAlignedObjectArray< int > &indices, const btAlignedObjectArray< btVector3 > &vecs)
btScalar dot(const btAlignedObjectArray< btVector3 > &other) const
btAlignedObjectArray< btVector3 > m_vecs
btReducedVector operator-()
void print() const
bool operator!=(const btReducedVector &other) const
btScalar dot(const btReducedVector &other) const
bool testMultiply() const
btReducedVector(int sz)
btReducedVector & operator=(const btReducedVector &other)