Bullet Collision Detection & Physics Library
btReducedVector.cpp
Go to the documentation of this file.
1//
2// btReducedVector.cpp
3// LinearMath
4//
5// Created by Xuchen Han on 4/4/20.
6//
7#include <stdio.h>
8#include "btReducedVector.h"
9#include <cmath>
10
11// returns the projection of this onto other
13{
15 btScalar other_length2 = other.length2();
16 if (other_length2 < SIMD_EPSILON)
17 {
18 return ret;
19 }
20 return other*(this->dot(other))/other_length2;
21}
22
24{
25 if (this->length2() < SIMD_EPSILON)
26 {
28 m_vecs.clear();
29 return;
30 }
31 *this /= std::sqrt(this->length2());
32}
33
35{
36 int sz = 5;
38 id1.push_back(1);
39 id1.push_back(3);
41 v1.push_back(btVector3(1,0,1));
42 v1.push_back(btVector3(3,1,5));
44 id2.push_back(2);
45 id2.push_back(3);
46 id2.push_back(5);
48 v2.push_back(btVector3(2,3,1));
49 v2.push_back(btVector3(3,4,9));
50 v2.push_back(btVector3(0,4,0));
52 id3.push_back(1);
53 id3.push_back(2);
54 id3.push_back(3);
55 id3.push_back(5);
57 v3.push_back(btVector3(1,0,1));
58 v3.push_back(btVector3(2,3,1));
59 v3.push_back(btVector3(6,5,14));
60 v3.push_back(btVector3(0,4,0));
61 btReducedVector rv1(sz, id1, v1);
62 btReducedVector rv2(sz, id2, v2);
63 btReducedVector ans(sz, id3, v3);
64 bool ret = ((ans == rv1+rv2) && (ans == rv2+rv1));
65 if (!ret)
66 printf("btReducedVector testAdd failed\n");
67 return ret;
68}
69
71{
72 int sz = 5;
74 id1.push_back(1);
75 id1.push_back(3);
77 v1.push_back(btVector3(1,0,1));
78 v1.push_back(btVector3(3,1,5));
80 id2.push_back(2);
81 id2.push_back(3);
82 id2.push_back(5);
84 v2.push_back(btVector3(2,3,1));
85 v2.push_back(btVector3(3,4,9));
86 v2.push_back(btVector3(0,4,0));
88 id3.push_back(1);
89 id3.push_back(2);
90 id3.push_back(3);
91 id3.push_back(5);
93 v3.push_back(btVector3(-1,-0,-1));
94 v3.push_back(btVector3(2,3,1));
95 v3.push_back(btVector3(0,3,4));
96 v3.push_back(btVector3(0,4,0));
97 btReducedVector rv1(sz, id1, v1);
98 btReducedVector rv2(sz, id2, v2);
99 btReducedVector ans(sz, id3, v3);
100 bool ret = (ans == rv2-rv1);
101 if (!ret)
102 printf("btReducedVector testMinus failed\n");
103 return ret;
104}
105
107{
108 int sz = 5;
110 id1.push_back(1);
111 id1.push_back(3);
113 v1.push_back(btVector3(1,0,1));
114 v1.push_back(btVector3(3,1,5));
116 id2.push_back(2);
117 id2.push_back(3);
118 id2.push_back(5);
120 v2.push_back(btVector3(2,3,1));
121 v2.push_back(btVector3(3,4,9));
122 v2.push_back(btVector3(0,4,0));
123 btReducedVector rv1(sz, id1, v1);
124 btReducedVector rv2(sz, id2, v2);
125 btScalar ans = 58;
126 bool ret = (ans == rv2.dot(rv1) && ans == rv1.dot(rv2));
127 ans = 14+16+9+16+81;
128 ret &= (ans==rv2.dot(rv2));
129
130 if (!ret)
131 printf("btReducedVector testDot failed\n");
132 return ret;
133}
134
136{
137 int sz = 5;
139 id1.push_back(1);
140 id1.push_back(3);
142 v1.push_back(btVector3(1,0,1));
143 v1.push_back(btVector3(3,1,5));
144 btScalar s = 2;
145 btReducedVector rv1(sz, id1, v1);
147 id2.push_back(1);
148 id2.push_back(3);
150 v2.push_back(btVector3(2,0,2));
151 v2.push_back(btVector3(6,2,10));
152 btReducedVector ans(sz, id2, v2);
153 bool ret = (ans == rv1*s);
154 if (!ret)
155 printf("btReducedVector testMultiply failed\n");
156 return ret;
157}
158
160{
161 bool ans = testAdd() && testMinus() && testDot() && testMultiply();
162 if (ans)
163 {
164 printf("All tests passed\n");
165 }
166 else
167 {
168 printf("Tests failed\n");
169 }
170}
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_EPSILON
Definition: btScalar.h:543
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 proj(const btReducedVector &other) const
btAlignedObjectArray< btVector3 > m_vecs
btScalar dot(const btReducedVector &other) const
bool testMultiply() const
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82