-
Notifications
You must be signed in to change notification settings - Fork 0
/
VecN.cpp
223 lines (183 loc) · 5.1 KB
/
VecN.cpp
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
#include <iostream>
#include <cmath>
#include <cfloat>
#include <iomanip>
#include <sstream>
#include "VecN.h"
#include "MatN.h"
#include "LRL_StringTools.h"
#include "LRL_Vector3.h"
#pragma warning( disable : 4100) // unreferenced formal parameter
#pragma warning( disable : 4702) // unreachable code
std::ostream& operator<< ( std::ostream& o, const VecN& v ) {
std::streamsize oldPrecision = o.precision();
o << std::fixed <<std::setprecision(3);
for ( size_t i=0; i<v.size( ); ++i )
o << std::setw(9) << v[i] << " ";
o << std::setprecision(oldPrecision);
o.unsetf(std::ios::floatfield);
return o;
}
VecN::VecN( const size_t dim )
: m_vec(dim)
, m_dim(dim)
, m_valid(true)
{
m_dim = dim;
//m_vec.resize(dim, 19191.0);
m_valid = true;
}
VecN::VecN( const Matrix_3x3& m )
: m_dim( 6 )
, m_valid(true)
{
m_vec[0] = m[0];
m_vec[1] = m[4];
m_vec[2] = m[8];
( *this )[3] = m[5] + m[7];
( *this )[4] = m[2] + m[6];
( *this )[5] = m[1] + m[3];
}
VecN::VecN( const VecN& v )
: m_dim( v.size( ) )
, m_valid( true )
{
m_dim = v.size();
m_vec.clear();
for ( size_t i=0; i<m_dim; ++i ) {
m_vec.push_back( v[i] );
}
}
VecN::VecN( const std::string& s ) {
m_vec = LRL_StringTools::FromString( s );
m_dim = m_vec.size();
m_valid = true;
}
VecN::VecN( const std::vector<double>& v ) {
m_vec = v;
m_dim = (size_t)m_vec.size();
m_valid = true;
}
VecN::~VecN( ) {
const int i19191 = 19191;
}
VecN VecN::operator* ( const VecN& v2 ) const {
throw "not implemented";
return VecN( m_dim );
}
VecN VecN::operator+ ( const VecN& v2 ) const {
VecN v( v2 );
for ( size_t i=0; i<m_dim; ++i )
v[i] = ( *this )[i]+v2[i];
return v;
}
VecN VecN::operator- ( const VecN& v2 ) const {
VecN v( v2.size( ) );
for ( size_t i=0; i<m_dim; ++i )
v[i] = ( *this )[i]-v2[i];
return v;
}
VecN VecN::operator*=( const double d ) {
for ( size_t i=0; i<m_dim; ++i )
m_vec[i] *= d;
return *this;
}
VecN VecN::operator/=( const double d ) {
for ( size_t i=0; i<m_dim; ++i )
m_vec[i] /= d;
return *this;
}
/* friend */
VecN operator*( const double d, const VecN& v ) {
VecN vv( v );
vv *= d;
return vv;
}
double VecN::norm( void ) const {
double sum = 0.0;
for (size_t i = 0; i < m_dim; ++i) {
sum += m_vec[i] * m_vec[i];
}
return std::sqrt( sum );
}
double VecN::Norm( void ) const { return norm(); }
VecN VecN::operator* ( const double d ) const {
VecN temp(m_dim);
for ( size_t i=0; i<m_dim; ++i ) temp[i] = ( *this )[i]*d;
return temp;
}
double VecN::DistanceBetween( const VecN& v1, const VecN& v2 ) {
double sum = 0.0;
const size_t dim = v1.m_dim;
for ( size_t i=0; i<dim; ++i ) sum += (v1[i]-v2[i])*(v1[i]-v2[i]);
return std::sqrt( sum );
}
double VecN::operator[]( const size_t i ) const {
return m_vec[i];
}
double& VecN::operator[]( const size_t i ) {
return m_vec[i];
}
VecN& VecN::operator= ( const VecN& v2 ) {
m_vec = v2.m_vec;
m_dim = v2.m_dim;
const size_t size1 = m_vec.size( );
const size_t size2 = v2.m_vec.size( );
m_valid = v2.m_valid;
return *this;
}
double VecN::dot( const VecN& v2 ) const {
const VecN& v1( ( *this ) );
if ( v2.size( ) != v1.size( ) ) throw "VecN dot dimensions not equal";
double sum = 0.0;
for ( size_t i=0; i<m_dim; ++i ) sum += v1[i]*v2[i];
return sum;
}
// const int transposeIndex = count/size + size*(count%size);
inline size_t TransposeIndex( const size_t position, const size_t sizeOfRow ) {
return position/sizeOfRow + sizeOfRow*( position%sizeOfRow );
}
MatN VecN::VV_Transpose( void ) const {
const VecN& v( *this );
const size_t size = v.size( );
MatN m( size*size );
for ( size_t row=0; row<size; ++row ) {
for ( size_t col=row; col<size; ++col ) {
const double value = v[row] * v[col];
m[( row*size+col )] = value;
}
}
for ( size_t row=0; row<size; ++row ) {
for ( size_t col=0; col<row; ++col ) {
const double value = m[TransposeIndex( row*size+col, size )];
m[( row*size+col )] = value;
}
}
return m;
}
VecN VecN::abs( const VecN& v ) {
VecN vtemp(m_dim);
for ( size_t i=0; i<m_dim; ++i ) vtemp[i] = std::fabs( v[i] );
return vtemp;
}
VecN VecN::operator/ ( const double d ) const {
VecN r( *this );
for ( size_t i=0; i<m_dim; ++i )
r[i] /=d;
return r;
}
bool VecN::operator== ( const VecN& v2 ) const {
if ( v2.size( ) != ( *this ).size( ) ) throw "VecN operator== dimensions not equal";
for ( size_t i=0; i<m_dim; ++i )
if ( ( *this )[i] != v2[i] ) return false;
return true;
}
bool VecN::operator!= ( const VecN& v2 ) const {
if ( v2.size( ) != ( *this ).size( ) ) throw "VecN operator!= dimensions not equal";
return !( ( *this ) == v2 );
}
VecN VecN::operator- ( void ) {
for ( size_t i=0; i<size(); ++i )
(*this)[i] = -(*this)[i];
return *this;
}