Blender  V3.3
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"
11 #include "btAlignedObjectArray.h"
12 #include <stdio.h>
13 #include <vector>
14 #include <algorithm>
15 struct TwoInts
16 {
17  int a,b;
18 };
19 inline 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 {
28 public:
31  int m_sz; // all m_indices value < m_sz
32 public:
34  {
35  m_indices.resize(0);
36  m_vecs.resize(0);
37  m_indices.clear();
38  m_vecs.clear();
39  }
40 
41  btReducedVector(int sz): m_sz(sz)
42  {
43  m_indices.resize(0);
44  m_vecs.resize(0);
45  m_indices.clear();
46  m_vecs.clear();
47  }
48 
50  {
51  }
52 
53  void simplify()
54  {
57  m_indices.resize(0);
58  m_vecs.resize(0);
59  m_indices.clear();
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  {
79  ret.m_indices.push_back(m_indices[i]);
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  {
98  ret.m_indices.push_back(m_indices[i]);
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  {
117  ret.m_indices.push_back(m_indices[i]);
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  {
132  ret.m_indices.push_back(m_indices[i]);
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  {
151  ret.m_indices.push_back(m_indices[i]);
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;
194  m_vecs.copyFromArray(other.m_vecs);
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 (int 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 */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
SIMD_FORCE_INLINE btReducedVector & operator/=(btReducedVector &v, btScalar s)
SIMD_FORCE_INLINE btReducedVector operator*(const btReducedVector &v, btScalar s)
bool operator<(const TwoInts &A, const TwoInts &B)
SIMD_FORCE_INLINE btReducedVector & operator-=(btReducedVector &v1, const btReducedVector &v2)
SIMD_FORCE_INLINE btReducedVector & operator+=(btReducedVector &v1, const btReducedVector &v2)
SIMD_FORCE_INLINE btReducedVector operator/(const btReducedVector &v, btScalar s)
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:280
#define SIMD_EPSILON
Definition: btScalar.h:543
SIMD_FORCE_INLINE void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0),...
void copyFromArray(const btAlignedObjectArray &otherArray)
SIMD_FORCE_INLINE int size() const
return the number of elements in the array
SIMD_FORCE_INLINE void resize(int newsize, const T &fillData=T())
SIMD_FORCE_INLINE void push_back(const T &_Val)
btReducedVector & operator=(const btReducedVector &other)
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)
ccl_gpu_kernel_postfix int ccl_global int * indices
#define B
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
return ret