-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSpatialVector.h
230 lines (176 loc) · 6.54 KB
/
BSpatialVector.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* BSpatialVector 31/01/2024
$$$$$$$$$$$$$$$$$$$$$$$$
$ BSpatialVector.h $
$$$$$$$$$$$$$$$$$$$$$$$$
by W.B. Yates
Copyright (c) W.B. Yates. All rights reserved.
History:
*/
#ifndef __BSPATIALVECTOR_H__
#define __BSPATIALVECTOR_H__
#ifndef __BSPATIALTYPES_H__
#include "BSpatialTypes.h"
#endif
class BSpatialVector
{
public:
BSpatialVector( void )=default;
BSpatialVector( const std::array<BScalar, 6> &d ): m_data(d) {}
explicit BSpatialVector( BScalar s ): m_data({s, s, s, s, s, s}) {}
explicit BSpatialVector( const std::vector<BScalar> &d ) { assert(d.size() == 6); set(d); }
explicit BSpatialVector( const std::vector<std::vector<BScalar>> &d ) { assert(d.size() == 1 && d[0].size() == 6); set(d[0]); }
BSpatialVector( BScalar s0, BScalar s1, BScalar s2, BScalar s3, BScalar s4, BScalar s5 ): m_data({s0, s1, s2, s3, s4, s5}) {}
explicit BSpatialVector( const BVector3 &h, const BVector3 &t ): m_data({h[0], h[1], h[2], t[0], t[1], t[2]}) {}
~BSpatialVector( void )=default;
void
set( BScalar s ) { m_data[0] = m_data[1] = m_data[2] = m_data[3] = m_data[4] = m_data[5] = s; }
void
set( const std::vector<BScalar> &d )
{
assert(d.size() == 6);
m_data[0] = d[0]; m_data[1] = d[1]; m_data[2] = d[2];
m_data[3] = d[3]; m_data[4] = d[4]; m_data[5] = d[5];
}
void
set( BScalar s0, BScalar s1, BScalar s2, BScalar s3, BScalar s4, BScalar s5 )
{
m_data[0] = s0; m_data[1] = s1; m_data[2] = s2;
m_data[3] = s3; m_data[4] = s4; m_data[5] = s5;
}
void
set( const BVector3 &h, const BVector3 &t )
{
m_data[0] = h[0]; m_data[1] = h[1]; m_data[2] = h[2];
m_data[3] = t[0]; m_data[4] = t[1]; m_data[5] = t[2];
}
// angular - a force or velocity
const BVector3
head( void ) const { return BVector3(m_data[0], m_data[1], m_data[2]); }
// linear - a force or velocity
const BVector3
tail( void ) const { return BVector3(m_data[3], m_data[4], m_data[5]); }
BScalar&
operator[]( int i ) { return m_data[i]; }
const BScalar
operator[]( int i ) const { return m_data[i]; }
size_t
size( void ) const { return 6; }
std::array<BScalar, 6>&
data( void ) { return m_data; }
const std::array<BScalar, 6>&
data( void ) const { return m_data; }
const BSpatialVector
operator+( const BSpatialVector &v ) const
{
return BSpatialVector(m_data[0] + v[0], m_data[1] + v[1], m_data[2] + v[2], m_data[3] + v[3], m_data[4] + v[4], m_data[5] + v[5]);
}
const BSpatialVector
operator-( const BSpatialVector &v ) const
{
return BSpatialVector(m_data[0] - v[0], m_data[1] - v[1], m_data[2] - v[2], m_data[3] - v[3], m_data[4] - v[4], m_data[5] - v[5]);
}
const BSpatialVector
operator-( void ) const
{
return BSpatialVector(-m_data[0], -m_data[1], -m_data[2], -m_data[3], -m_data[4], -m_data[5]);
}
const BSpatialVector
operator/( BScalar s ) const
{
return BSpatialVector(m_data[0] / s, m_data[1] / s, m_data[2] / s, m_data[3] / s, m_data[4] / s, m_data[5] / s);
}
const BSpatialVector
operator*( BScalar s ) const
{
return BSpatialVector(m_data[0] * s, m_data[1] * s, m_data[2] * s, m_data[3] * s, m_data[4] * s, m_data[5] * s);
}
const BSpatialVector&
operator-=( const BSpatialVector &v )
{
m_data[0] -= v[0]; m_data[1] -= v[1]; m_data[2] -= v[2];
m_data[3] -= v[3]; m_data[4] -= v[4]; m_data[5] -= v[5];
return *this;
}
const BSpatialVector&
operator+=( const BSpatialVector &v )
{
m_data[0] += v[0]; m_data[1] += v[1]; m_data[2] += v[2];
m_data[3] += v[3]; m_data[4] += v[4]; m_data[5] += v[5];
return *this;
}
const BSpatialVector&
operator/=( BScalar s )
{
m_data[0] /= s; m_data[1] /= s; m_data[2] /= s;
m_data[3] /= s; m_data[4] /= s; m_data[5] /= s;
return *this;
}
const BSpatialVector&
operator*=( BScalar s )
{
m_data[0] *= s; m_data[1] *= s; m_data[2] *= s;
m_data[3] *= s; m_data[4] *= s; m_data[5] *= s;
return *this;
}
bool
operator==( const BSpatialVector& v ) const { return (m_data == v.m_data); }
bool
operator!=( const BSpatialVector& v ) const { return (m_data != v.m_data); }
private:
std::array<BScalar, 6> m_data;
};
inline const BSpatialVector
operator*( BScalar s, const BSpatialVector &v )
// scalar multiplication
{
return v * s;
}
inline std::ostream&
operator<<( std::ostream& ostr, const BSpatialVector& v )
{
ostr << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3] << ' ' << v[4] << ' ' << v[5] << ' ';
return ostr;
}
inline std::istream&
operator>>( std::istream& istr, BSpatialVector& v )
{
istr >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5];
return istr;
}
//
// Articulated Rigid Body
//
namespace arb
{
// spatial cross products (RBDA, Section 2.9)
// motion
inline const BSpatialVector
crossm(const BSpatialVector &v1, const BSpatialVector &v2)
{
return BSpatialVector( -v1[2] * v2[1] + v1[1] * v2[2],
v1[2] * v2[0] - v1[0] * v2[2],
-v1[1] * v2[0] + v1[0] * v2[1],
-v1[5] * v2[1] + v1[4] * v2[2] - v1[2] * v2[4] + v1[1] * v2[5],
v1[5] * v2[0] - v1[3] * v2[2] + v1[2] * v2[3] - v1[0] * v2[5],
-v1[4] * v2[0] + v1[3] * v2[1] - v1[1] * v2[3] + v1[0] * v2[4] );
}
// force
inline const BSpatialVector
crossf(const BSpatialVector &v1, const BSpatialVector &v2)
{
return BSpatialVector( -v1[2] * v2[1] + v1[1] * v2[2] - v1[5] * v2[4] + v1[4] * v2[5],
v1[2] * v2[0] - v1[0] * v2[2] + v1[5] * v2[3] - v1[3] * v2[5],
-v1[1] * v2[0] + v1[0] * v2[1] - v1[4] * v2[3] + v1[3] * v2[4],
-v1[2] * v2[4] + v1[1] * v2[5],
v1[2] * v2[3] - v1[0] * v2[5],
-v1[1] * v2[3] + v1[0] * v2[4] );
}
// spatial dot product
inline const BScalar
dot(const BSpatialVector &v1, const BSpatialVector &v2)
{
return glm::dot(v1.head(), v2.head()) + glm::dot(v1.tail(), v2.tail());
//return (v1[0] * v2[0]) + (v1[1] * v2[1]) + (v1[2] * v2[2]) + (v1[3] * v2[3]) + (v1[4] * v2[4]) + (v1[5] * v2[5]);
}
}
#endif