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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
// CVector
// A one-dimensional array including basic vector operations
//
// Author: Thomas Brox
// Last change: 23.05.2005
//-------------------------------------------------------------------------
#ifndef CVECTOR_H
#define CVECTOR_H
#include <iostream>
#include <fstream>
template <class T> class CMatrix;
template <class T> class CTensor;
template <class T>
class CVector {
public:
// constructor
inline CVector();
// constructor
inline CVector(const int aSize);
// copy constructor
CVector(const CVector<T>& aCopyFrom);
// constructor (from array)
CVector(const T* aPointer, const int aSize);
// constructor with implicit filling
CVector(const int aSize, const T aFillValue);
// destructor
virtual ~CVector();
// Changes the size of the vector (data is lost)
void setSize(int aSize);
// Fills the vector with the specified value (see also operator=)
void fill(const T aValue);
// Appends the values of another vector
void append(CVector<T>& aVector);
// Normalizes the length of the vector to 1
void normalize();
// Normalizes the component sum to 1
void normalizeSum();
// Reads values from a text file
void readFromTXT(const char* aFilename);
// Writes values to a text file
void writeToTXT(char* aFilename);
// Returns the sum of all values
T sum();
// Returns the minimum value
T min();
// Returns the maximum value
T max();
// Returns the Euclidean norm
T norm();
// Converts vector to homogeneous coordinates, i.e., all components are divided by last component
CVector<T>& homogen();
// Remove the last component
inline void homogen_nD();
// Computes the cross product between this vector and aVector
void cross(CVector<T>& aVector);
// Gives full access to the vector's values
inline T& operator()(const int aIndex) const;
inline T& operator[](const int aIndex) const;
// Fills the vector with the specified value (equivalent to fill)
inline CVector<T>& operator=(const T aValue);
// Copies a vector into this vector (size might change)
CVector<T>& operator=(const CVector<T>& aCopyFrom);
// Copies values from a matrix to the vector (size might change)
CVector<T>& operator=(const CMatrix<T>& aCopyFrom);
// Copies values from a tensor to the vector (size might change)
CVector<T>& operator=(const CTensor<T>& aCopyFrom);
// Adds another vector
CVector<T>& operator+=(const CVector<T>& aVector);
// Substracts another vector
CVector<T>& operator-=(const CVector<T>& aVector);
// Multiplies the vector with a scalar
CVector<T>& operator*=(const T aValue);
// Scalar product
T operator*=(const CVector<T>& aVector);
// Checks (non-)equivalence to another vector
bool operator==(const CVector<T>& aVector);
inline bool operator!=(const CVector<T>& aVector);
// Gives access to the vector's size
inline int size() const;
// Gives access to the internal data representation
inline T* data() const {return mData;}
protected:
int mSize;
T* mData;
};
// Adds two vectors
template <class T> CVector<T> operator+(const CVector<T>& vec1, const CVector<T>& vec2);
// Substracts two vectors
template <class T> CVector<T> operator-(const CVector<T>& vec1, const CVector<T>& vec2);
// Multiplies vector with a scalar
template <class T> CVector<T> operator*(const CVector<T>& aVector, const T aValue);
template <class T> CVector<T> operator*(const T aValue, const CVector<T>& aVector);
// Computes the scalar product of two vectors
template <class T> T operator*(const CVector<T>& vec1, const CVector<T>& vec2);
// Computes cross product of two vectors
template <class T> CVector<T> operator/(const CVector<T>& vec1, const CVector<T>& vec2);
// Sends the vector to an output stream
template <class T> std::ostream& operator<<(std::ostream& aStream, const CVector<T>& aVector);
// Exceptions thrown by CVector--------------------------------------------
// Thrown if one tries to access an element of a vector which is out of
// the vector's bounds
struct EVectorRangeOverflow {
EVectorRangeOverflow(const int aIndex) {
using namespace std;
cerr << "Exception EVectorRangeOverflow: Index = " << aIndex << endl;
}
};
struct EVectorIncompatibleSize {
EVectorIncompatibleSize(int aSize1, int aSize2) {
using namespace std;
cerr << "Exception EVectorIncompatibleSize: " << aSize1 << " <> " << aSize2 << endl;
}
};
// I M P L E M E N T A T I O N --------------------------------------------
//
// You might wonder why there is implementation code in a header file.
// The reason is that not all C++ compilers yet manage separate compilation
// of templates. Inline functions cannot be compiled separately anyway.
// So in this case the whole implementation code is added to the header
// file.
// Users of CVector should ignore everything that's beyond this line.
// ------------------------------------------------------------------------
// P U B L I C ------------------------------------------------------------
// constructor
template <class T>
inline CVector<T>::CVector() : mSize(0) {
mData = new T[0];
}
// constructor
template <class T>
inline CVector<T>::CVector(const int aSize)
: mSize(aSize) {
mData = new T[aSize];
}
// copy constructor
template <class T>
CVector<T>::CVector(const CVector<T>& aCopyFrom)
: mSize(aCopyFrom.mSize) {
mData = new T[mSize];
for (int i = 0; i < mSize; i++)
mData[i] = aCopyFrom.mData[i];
}
// constructor (from array)
template <class T>
CVector<T>::CVector(const T* aPointer, const int aSize)
: mSize(aSize) {
mData = new T[mSize];
for (int i = 0; i < mSize; i++)
mData[i] = aPointer[i];
}
// constructor with implicit filling
template <class T>
CVector<T>::CVector(const int aSize, const T aFillValue)
: mSize(aSize) {
mData = new T[aSize];
fill(aFillValue);
}
// destructor
template <class T>
CVector<T>::~CVector() {
delete[] mData;
}
// setSize
template <class T>
void CVector<T>::setSize(int aSize) {
if (mData != 0) delete[] mData;
mData = new T[aSize];
mSize = aSize;
}
// fill
template <class T>
void CVector<T>::fill(const T aValue) {
for (register int i = 0; i < mSize; i++)
mData[i] = aValue;
}
// append
template <class T>
void CVector<T>::append(CVector<T>& aVector) {
T* aNewData = new T[mSize+aVector.size()];
for (int i = 0; i < mSize; i++)
aNewData[i] = mData[i];
for (int i = 0; i < aVector.size(); i++)
aNewData[i+mSize] = aVector(i);
mSize += aVector.size();
delete[] mData;
mData = aNewData;
}
// normalize
template <class T>
void CVector<T>::normalize() {
T aSum = 0;
for (register int i = 0; i < mSize; i++)
aSum += mData[i]*mData[i];
if (aSum == 0) return;
aSum = 1.0/sqrt(aSum);
for (register int i = 0; i < mSize; i++)
mData[i] *= aSum;
}
// normalizeSum
template <class T>
void CVector<T>::normalizeSum() {
T aSum = 0;
for (register int i = 0; i < mSize; i++)
aSum += mData[i];
if (aSum == 0) return;
aSum = 1.0/aSum;
for (register int i = 0; i < mSize; i++)
mData[i] *= aSum;
}
// readFromTXT
template<class T>
void CVector<T>::readFromTXT(const char* aFilename) {
std::ifstream aStream(aFilename);
mSize = 0;
float aDummy;
while (!aStream.eof()) {
aStream >> aDummy;
mSize++;
}
aStream.close();
std::ifstream aStream2(aFilename);
delete mData;
mData = new T[mSize];
for (int i = 0; i < mSize; i++)
aStream2 >> mData[i];
}
// writeToTXT
template<class T>
void CVector<T>::writeToTXT(char* aFilename) {
std::ofstream aStream(aFilename);
for (int i = 0; i < mSize; i++)
aStream << mData[i] << std::endl;
}
// sum
template <class T>
T CVector<T>::sum() {
T val = mData[0];
for (int i = 1; i < mSize; i++)
val += mData[i];
return val;
}
// min
template <class T>
T CVector<T>::min() {
T bestValue = mData[0];
for (int i = 1; i < mSize; i++)
if (mData[i] < bestValue) bestValue = mData[i];
return bestValue;
}
// max
template <class T>
T CVector<T>::max() {
T bestValue = mData[0];
for (int i = 1; i < mSize; i++)
if (mData[i] > bestValue) bestValue = mData[i];
return bestValue;
}
// norm
template <class T>
T CVector<T>::norm() {
T aSum = 0.0;
for (int i = 0; i < mSize; i++)
aSum += mData[i]*mData[i];
return sqrt(aSum);
}
// homogen
template <class T>
CVector<T>& CVector<T>::homogen() {
if (mSize > 1 && mData[mSize-1] != 0) {
T invVal = 1.0/mData[mSize-1];
for (int i = 0; i < mSize; i++)
mData[i] *= invVal;
}
return (*this);
}
// homogen_nD
template <class T>
inline void CVector<T>::homogen_nD() {
mSize--;
}
// cross
template <class T>
void CVector<T>::cross(CVector<T>& aVector) {
T aHelp0 = aVector(2)*mData[1] - aVector(1)*mData[2];
T aHelp1 = aVector(0)*mData[2] - aVector(2)*mData[0];
T aHelp2 = aVector(1)*mData[0] - aVector(0)*mData[1];
mData[0] = aHelp0;
mData[1] = aHelp1;
mData[2] = aHelp2;
}
// operator()
template <class T>
inline T& CVector<T>::operator()(const int aIndex) const {
#ifdef _DEBUG
if (aIndex >= mSize || aIndex < 0)
throw EVectorRangeOverflow(aIndex);
#endif
return mData[aIndex];
}
// operator[]
template <class T>
inline T& CVector<T>::operator[](const int aIndex) const {
return operator()(aIndex);
}
// operator=
template <class T>
inline CVector<T>& CVector<T>::operator=(const T aValue) {
fill(aValue);
return *this;
}
template <class T>
CVector<T>& CVector<T>::operator=(const CVector<T>& aCopyFrom) {
if (this != &aCopyFrom) {
if (mSize != aCopyFrom.size()) {
delete[] mData;
mSize = aCopyFrom.size();
mData = new T[mSize];
}
for (register int i = 0; i < mSize; i++)
mData[i] = aCopyFrom.mData[i];
}
return *this;
}
template <class T>
CVector<T>& CVector<T>::operator=(const CMatrix<T>& aCopyFrom) {
if (mSize != aCopyFrom.size()) {
delete[] mData;
mSize = aCopyFrom.size();
mData = new T[mSize];
}
for (register int i = 0; i < mSize; i++)
mData[i] = aCopyFrom.data()[i];
return *this;
}
template <class T>
CVector<T>& CVector<T>::operator=(const CTensor<T>& aCopyFrom) {
if (mSize != aCopyFrom.size()) {
delete[] mData;
mSize = aCopyFrom.size();
mData = new T[mSize];
}
for (register int i = 0; i < mSize; i++)
mData[i] = aCopyFrom.data()[i];
return *this;
}
// operator +=
template <class T>
CVector<T>& CVector<T>::operator+=(const CVector<T>& aVector) {
#ifdef _DEBUG
if (mSize != aVector.size()) throw EVectorIncompatibleSize(mSize,aVector.size());
#endif
for (int i = 0; i < mSize; i++)
mData[i] += aVector(i);
return *this;
}
// operator -=
template <class T>
CVector<T>& CVector<T>::operator-=(const CVector<T>& aVector) {
#ifdef _DEBUG
if (mSize != aVector.size()) throw EVectorIncompatibleSize(mSize,aVector.size());
#endif
for (int i = 0; i < mSize; i++)
mData[i] -= aVector(i);
return *this;
}
// operator *=
template <class T>
CVector<T>& CVector<T>::operator*=(const T aValue) {
for (int i = 0; i < mSize; i++)
mData[i] *= aValue;
return *this;
}
template <class T>
T CVector<T>::operator*=(const CVector<T>& aVector) {
#ifdef _DEBUG
if (mSize != aVector.size()) throw EVectorIncompatibleSize(mSize,aVector.size());
#endif
T aSum = 0.0;
for (int i = 0; i < mSize; i++)
aSum += mData[i]*aVector(i);
return aSum;
}
// operator ==
template <class T>
bool CVector<T>::operator==(const CVector<T>& aVector) {
if (mSize != aVector.size()) return false;
int i = 0;
while (i < mSize && aVector(i) == mData[i])
i++;
return (i == mSize);
}
// operator !=
template <class T>
inline bool CVector<T>::operator!=(const CVector<T>& aVector) {
return !((*this)==aVector);
}
// size
template <class T>
inline int CVector<T>::size() const {
return mSize;
}
// N O N - M E M B E R F U N C T I O N S -------------------------------------
// operator +
template <class T>
CVector<T> operator+(const CVector<T>& vec1, const CVector<T>& vec2) {
#ifdef _DEBUG
if (vec1.size() != vec2.size()) throw EVectorIncompatibleSize(vec1.size(),vec2.size());
#endif
CVector<T> result(vec1.size());
for (int i = 0; i < vec1.size(); i++)
result(i) = vec1[i]+vec2[i];
return result;
}
// operator -
template <class T>
CVector<T> operator-(const CVector<T>& vec1, const CVector<T>& vec2) {
#ifdef _DEBUG
if (vec1.size() != vec2.size()) throw EVectorIncompatibleSize(vec1.size(),vec2.size());
#endif
CVector<T> result(vec1.size());
for (int i = 0; i < vec1.size(); i++)
result(i) = vec1(i)-vec2(i);
return result;
}
// operator *
template <class T>
CVector<T> operator*(const T aValue, const CVector<T>& aVector) {
CVector<T> result(aVector.size());
for (int i = 0; i < aVector.size(); i++)
result(i) = aValue*aVector(i);
return result;
}
template <class T>
CVector<T> operator*(const CVector<T>& aVector, const T aValue) {
return operator*(aValue,aVector);
}
template <class T>
T operator*(const CVector<T>& vec1, const CVector<T>& vec2) {
#ifdef _DEBUG
if (vec1.size() != vec2.size()) throw EVectorIncompatibleSize(vec1.size(),vec2.size());
#endif
T aSum = 0.0;
for (int i = 0; i < vec1.size(); i++)
aSum += vec1(i)*vec2(i);
return aSum;
}
// operator /
template <class T>
CVector<T> operator/(const CVector<T>& vec1, const CVector<T>& vec2) {
CVector<T> result(3);
result[0]=vec1[1]*vec2[2] - vec1[2]*vec2[1];
result[1]=vec1[2]*vec2[0] - vec1[0]*vec2[2];
result[2]=vec1[0]*vec2[1] - vec1[1]*vec2[0];
return result;
}
// operator <<
template <class T>
std::ostream& operator<<(std::ostream& aStream, const CVector<T>& aVector) {
for (int i = 0; i < aVector.size(); i++)
aStream << aVector(i) << '|';
aStream << std::endl;
return aStream;
}
#endif
|