|
@@ -1,45 +1,46 @@
|
1
|
1
|
#include <math.h>
|
2
|
2
|
|
3
|
|
-#define tmpl template<unsigned X>
|
|
3
|
+#define tmplx template<unsigned X>
|
|
4
|
+#define tmpl template<unsigned X, class T>
|
4
|
5
|
|
5
|
|
-tmpl VectorXD<X>::VectorXD()
|
|
6
|
+tmplx VectorXD<X>::VectorXD()
|
6
|
7
|
{
|
7
|
8
|
for (unsigned i = 0; i < X; ++i) {
|
8
|
9
|
_scalars[i] = 0;
|
9
|
10
|
}
|
10
|
11
|
}
|
11
|
12
|
|
12
|
|
-tmpl VectorXD<X>::VectorXD(const double scalars[X])
|
|
13
|
+tmplx VectorXD<X>::VectorXD(const double scalars[X])
|
13
|
14
|
{
|
14
|
15
|
for (unsigned i = 0; i < X; ++i) {
|
15
|
16
|
_scalars[i] = scalars[i];
|
16
|
17
|
}
|
17
|
18
|
}
|
18
|
19
|
|
19
|
|
-tmpl VectorXD<X>::VectorXD(const VectorXD<X>& other)
|
|
20
|
+tmplx VectorXD<X>::VectorXD(const VectorXD<X>& other)
|
20
|
21
|
{
|
21
|
22
|
for (unsigned i = 0; i < X; ++i) {
|
22
|
23
|
_scalars[i] = other._scalars[i];
|
23
|
24
|
}
|
24
|
25
|
}
|
25
|
26
|
|
26
|
|
-tmpl VectorXD<X>& VectorXD<X>::setScalar(unsigned i, double value)
|
|
27
|
+tmpl T& GenericVector<X, T>::setScalar(unsigned i, double value)
|
27
|
28
|
{
|
28
|
29
|
_scalars[i] = value;
|
29
|
|
- return *this;
|
|
30
|
+ return *getThis();
|
30
|
31
|
}
|
31
|
32
|
|
32
|
|
-tmpl double VectorXD<X>::getScalar(unsigned i) const
|
|
33
|
+tmpl double GenericVector<X, T>::getScalar(unsigned i) const
|
33
|
34
|
{
|
34
|
35
|
return _scalars[i];
|
35
|
36
|
}
|
36
|
37
|
|
37
|
|
-tmpl bool VectorXD<X>::isNull() const
|
|
38
|
+tmpl bool GenericVector<X, T>::isNull() const
|
38
|
39
|
{
|
39
|
|
- return equal(VectorXD<X>());
|
|
40
|
+ return equal(T());
|
40
|
41
|
}
|
41
|
42
|
|
42
|
|
-tmpl bool VectorXD<X>::equal(const VectorXD<X> &other) const
|
|
43
|
+tmpl bool GenericVector<X, T>::equal(const T &other) const
|
43
|
44
|
{
|
44
|
45
|
for (unsigned i = 0; i < X; ++i) {
|
45
|
46
|
if (_scalars[i] != other._scalars[i]) {
|
|
@@ -49,94 +50,94 @@ tmpl bool VectorXD<X>::equal(const VectorXD<X> &other) const
|
49
|
50
|
return true;
|
50
|
51
|
}
|
51
|
52
|
|
52
|
|
-tmpl VectorXD<X> &VectorXD<X>::add(double k)
|
|
53
|
+tmpl T &GenericVector<X, T>::add(double k)
|
53
|
54
|
{
|
54
|
55
|
double scalars[X];
|
55
|
56
|
for (unsigned i = 0; i < X; ++i) {
|
56
|
57
|
scalars[i] = k;
|
57
|
58
|
}
|
58
|
|
- return add(VectorXD<X>(scalars));
|
|
59
|
+ return add(T(scalars));
|
59
|
60
|
}
|
60
|
61
|
|
61
|
|
-tmpl VectorXD<X> &VectorXD<X>::add(double scalars[X])
|
|
62
|
+tmpl T &GenericVector<X, T>::add(double scalars[X])
|
62
|
63
|
{
|
63
|
|
- return add(VectorXD<X>(scalars));
|
|
64
|
+ return add(T(scalars));
|
64
|
65
|
}
|
65
|
66
|
|
66
|
|
-tmpl VectorXD<X>& VectorXD<X>::add(const VectorXD<X> &other)
|
|
67
|
+tmpl T& GenericVector<X, T>::add(const T &other)
|
67
|
68
|
{
|
68
|
69
|
for (unsigned i = 0; i < X; ++i) {
|
69
|
70
|
_scalars[i] += other._scalars[i];
|
70
|
71
|
}
|
71
|
|
- return *this;
|
|
72
|
+ return *getThis();
|
72
|
73
|
}
|
73
|
74
|
|
74
|
|
-tmpl VectorXD<X> &VectorXD<X>::sub(double k)
|
|
75
|
+tmpl T &GenericVector<X, T>::sub(double k)
|
75
|
76
|
{
|
76
|
77
|
double scalars[X];
|
77
|
78
|
for (unsigned i = 0; i < X; ++i) {
|
78
|
79
|
scalars[i] = k;
|
79
|
80
|
}
|
80
|
|
- return sub(VectorXD<X>(scalars));
|
|
81
|
+ return sub(T(scalars));
|
81
|
82
|
}
|
82
|
83
|
|
83
|
|
-tmpl VectorXD<X> &VectorXD<X>::sub(double scalars[X])
|
|
84
|
+tmpl T &GenericVector<X, T>::sub(double scalars[X])
|
84
|
85
|
{
|
85
|
|
- return sub(VectorXD<X>(scalars));
|
|
86
|
+ return sub(T(scalars));
|
86
|
87
|
}
|
87
|
88
|
|
88
|
|
-tmpl VectorXD<X>& VectorXD<X>::sub(const VectorXD<X> &other)
|
|
89
|
+tmpl T& GenericVector<X, T>::sub(const T &other)
|
89
|
90
|
{
|
90
|
91
|
for (unsigned i = 0; i < X; ++i) {
|
91
|
92
|
_scalars[i] -= other._scalars[i];
|
92
|
93
|
}
|
93
|
|
- return *this;
|
|
94
|
+ return *getThis();
|
94
|
95
|
}
|
95
|
96
|
|
96
|
|
-tmpl VectorXD<X> &VectorXD<X>::mult(double k)
|
|
97
|
+tmpl T &GenericVector<X, T>::mult(double k)
|
97
|
98
|
{
|
98
|
99
|
for (unsigned i = 0; i < X; ++i) {
|
99
|
100
|
_scalars[i] *= k;
|
100
|
101
|
}
|
101
|
|
- return *this;
|
|
102
|
+ return *getThis();
|
102
|
103
|
}
|
103
|
104
|
|
104
|
|
-tmpl VectorXD<X> &VectorXD<X>::mult(double scalars[X])
|
|
105
|
+tmpl T &GenericVector<X, T>::mult(double scalars[X])
|
105
|
106
|
{
|
106
|
|
- return mult(VectorXD<X>(scalars));
|
|
107
|
+ return mult(T(scalars));
|
107
|
108
|
}
|
108
|
109
|
|
109
|
|
-tmpl VectorXD<X> &VectorXD<X>::mult(const VectorXD<X> &other)
|
|
110
|
+tmpl T &GenericVector<X, T>::mult(const T &other)
|
110
|
111
|
{
|
111
|
112
|
for (unsigned i = 0; i < X; ++i) {
|
112
|
113
|
_scalars[i] *= other._scalars[i];
|
113
|
114
|
}
|
114
|
|
- return *this;
|
|
115
|
+ return *getThis();
|
115
|
116
|
}
|
116
|
117
|
|
117
|
|
-tmpl VectorXD<X> &VectorXD<X>::div(double k)
|
|
118
|
+tmpl T &GenericVector<X, T>::div(double k)
|
118
|
119
|
{
|
119
|
120
|
double scalars[X];
|
120
|
121
|
for (unsigned i = 0; i < X; ++i) {
|
121
|
122
|
scalars[i] = k;
|
122
|
123
|
}
|
123
|
|
- return div(VectorXD<X>(scalars));
|
|
124
|
+ return div(T(scalars));
|
124
|
125
|
}
|
125
|
126
|
|
126
|
|
-tmpl VectorXD<X> &VectorXD<X>::div(double scalars[X])
|
|
127
|
+tmpl T &GenericVector<X, T>::div(double scalars[X])
|
127
|
128
|
{
|
128
|
|
- return div(VectorXD<X>(scalars));
|
|
129
|
+ return div(T(scalars));
|
129
|
130
|
}
|
130
|
131
|
|
131
|
|
-tmpl VectorXD<X> &VectorXD<X>::div(const VectorXD<X> &other)
|
|
132
|
+tmpl T &GenericVector<X, T>::div(const T &other)
|
132
|
133
|
{
|
133
|
134
|
for (unsigned i = 0; i < X; ++i) {
|
134
|
135
|
_scalars[i] /= other._scalars[i];
|
135
|
136
|
}
|
136
|
|
- return *this;
|
|
137
|
+ return *getThis();
|
137
|
138
|
}
|
138
|
139
|
|
139
|
|
-tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
|
|
140
|
+tmpl double GenericVector<X, T>::dotProduct(const T &other) const
|
140
|
141
|
{
|
141
|
142
|
double total = 0;
|
142
|
143
|
for (unsigned i = 0; i < X; ++i) {
|
|
@@ -145,23 +146,23 @@ tmpl double VectorXD<X>::dotProduct(const VectorXD<X> &other) const
|
145
|
146
|
return total;
|
146
|
147
|
}
|
147
|
148
|
|
148
|
|
-tmpl VectorXD<X>& VectorXD<X>::crossProduct(const VectorXD<X>& other)
|
|
149
|
+tmpl T& GenericVector<X, T>::crossProduct(const T& other)
|
149
|
150
|
{
|
150
|
|
- VectorXD<X> t = *this;
|
|
151
|
+ T t = *getThis();
|
151
|
152
|
for (unsigned i = 0; i < X; ++i) {
|
152
|
153
|
unsigned j = (i + 1) % X;
|
153
|
154
|
unsigned k = (i + 2) % X;
|
154
|
155
|
_scalars[i] = (t._scalars[j] * other._scalars[k]) - (t._scalars[k] * other._scalars[j]);
|
155
|
156
|
}
|
156
|
|
- return *this;
|
|
157
|
+ return *getThis();
|
157
|
158
|
}
|
158
|
159
|
|
159
|
|
-tmpl VectorXD<X> VectorXD<X>::crossProduct(const VectorXD<X> &v1, const VectorXD<X> &v2)
|
|
160
|
+tmpl T GenericVector<X, T>::crossProduct(const T &v1, const T &v2)
|
160
|
161
|
{
|
161
|
|
- return VectorXD<X>(v1).crossProduct(v2);
|
|
162
|
+ return T(v1).crossProduct(v2);
|
162
|
163
|
}
|
163
|
164
|
|
164
|
|
-tmpl double VectorXD<X>::norm() const
|
|
165
|
+tmpl double GenericVector<X, T>::norm() const
|
165
|
166
|
{
|
166
|
167
|
double total = 0;
|
167
|
168
|
for (unsigned i = 0; i < X; ++i) {
|
|
@@ -170,117 +171,123 @@ tmpl double VectorXD<X>::norm() const
|
170
|
171
|
return sqrt(total);
|
171
|
172
|
}
|
172
|
173
|
|
173
|
|
-tmpl VectorXD<X> VectorXD<X>::operator+() const
|
|
174
|
+tmpl T GenericVector<X, T>::operator+() const
|
174
|
175
|
{
|
175
|
|
- return *this;
|
|
176
|
+ return *getThis();
|
176
|
177
|
}
|
177
|
178
|
|
178
|
|
-tmpl VectorXD<X> VectorXD<X>::operator+(const double &k) const
|
|
179
|
+tmpl T GenericVector<X, T>::operator+(const double &k) const
|
179
|
180
|
{
|
180
|
|
- return VectorXD(*this).add(k);
|
|
181
|
+ return T(*getThis()).add(k);
|
181
|
182
|
}
|
182
|
183
|
|
183
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator+=(const double &k)
|
|
184
|
+tmpl T &GenericVector<X, T>::operator+=(const double &k)
|
184
|
185
|
{
|
185
|
186
|
return add(k);
|
186
|
187
|
}
|
187
|
188
|
|
188
|
|
-tmpl VectorXD<X> VectorXD<X>::operator+(const VectorXD<X> &other) const
|
|
189
|
+tmpl T GenericVector<X, T>::operator+(const T &other) const
|
189
|
190
|
{
|
190
|
|
- return VectorXD<X>(*this).add(other);
|
|
191
|
+ T v = *getThis();
|
|
192
|
+ return v.add(other);
|
191
|
193
|
}
|
192
|
194
|
|
193
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator+=(const VectorXD<X> &other)
|
|
195
|
+tmpl T &GenericVector<X, T>::operator+=(const T &other)
|
194
|
196
|
{
|
195
|
197
|
return add(other);
|
196
|
198
|
}
|
197
|
199
|
|
198
|
|
-tmpl VectorXD<X> VectorXD<X>::operator-() const
|
|
200
|
+tmpl T GenericVector<X, T>::operator-() const
|
199
|
201
|
{
|
200
|
202
|
double scalars[X];
|
201
|
203
|
for (unsigned i = 0; i < X; ++i) {
|
202
|
204
|
scalars[i] = -_scalars[i];
|
203
|
205
|
}
|
204
|
|
- return VectorXD<X>(scalars);
|
|
206
|
+ return T(scalars);
|
205
|
207
|
}
|
206
|
208
|
|
207
|
|
-tmpl VectorXD<X> VectorXD<X>::operator-(const double &k) const
|
|
209
|
+tmpl T GenericVector<X, T>::operator-(const double &k) const
|
208
|
210
|
{
|
209
|
|
- return VectorXD(*this).sub(k);
|
|
211
|
+ return T(*getThis()).sub(k);
|
210
|
212
|
}
|
211
|
213
|
|
212
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator-=(const double &k)
|
|
214
|
+tmpl T &GenericVector<X, T>::operator-=(const double &k)
|
213
|
215
|
{
|
214
|
216
|
return sub(k);
|
215
|
217
|
}
|
216
|
218
|
|
217
|
|
-tmpl VectorXD<X> VectorXD<X>::operator-(const VectorXD<X> &other) const
|
|
219
|
+tmpl T GenericVector<X, T>::operator-(const T &other) const
|
218
|
220
|
{
|
219
|
|
- return VectorXD(*this).sub(other);
|
|
221
|
+ return T(*getThis()).sub(other);
|
220
|
222
|
}
|
221
|
223
|
|
222
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator-=(const VectorXD<X> &other)
|
|
224
|
+tmpl T &GenericVector<X, T>::operator-=(const T &other)
|
223
|
225
|
{
|
224
|
226
|
return sub(other);
|
225
|
227
|
}
|
226
|
228
|
|
227
|
|
-tmpl VectorXD<X> VectorXD<X>::operator*(const double &k) const
|
|
229
|
+tmpl T GenericVector<X, T>::operator*(const double &k) const
|
228
|
230
|
{
|
229
|
|
- return VectorXD<X>(*this).mult(k);
|
|
231
|
+ return T(*getThis()).mult(k);
|
230
|
232
|
}
|
231
|
233
|
|
232
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator*=(const double &k)
|
|
234
|
+tmpl T &GenericVector<X, T>::operator*=(const double &k)
|
233
|
235
|
{
|
234
|
236
|
return mult(k);
|
235
|
237
|
}
|
236
|
238
|
|
237
|
|
-tmpl VectorXD<X> VectorXD<X>::operator*(const VectorXD<X> &other) const
|
|
239
|
+tmpl T GenericVector<X, T>::operator*(const T &other) const
|
238
|
240
|
{
|
239
|
|
- return VectorXD<X>(*this).mult(other);
|
|
241
|
+ return T(*getThis()).mult(other);
|
240
|
242
|
}
|
241
|
243
|
|
242
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator*=(const VectorXD<X> &other)
|
|
244
|
+tmpl T &GenericVector<X, T>::operator*=(const T &other)
|
243
|
245
|
{
|
244
|
246
|
dotProduct(other);
|
245
|
|
- return *this;
|
|
247
|
+ return *getThis();
|
246
|
248
|
}
|
247
|
249
|
|
248
|
|
-tmpl VectorXD<X> VectorXD<X>::operator/(const double &k) const
|
|
250
|
+tmpl T GenericVector<X, T>::operator/(const double &k) const
|
249
|
251
|
{
|
250
|
|
- return VectorXD<X>(*this).div(k);
|
|
252
|
+ return T(*getThis()).div(k);
|
251
|
253
|
}
|
252
|
254
|
|
253
|
|
-tmpl VectorXD<X> &VectorXD<X>::operator/=(const double &k)
|
|
255
|
+tmpl T &GenericVector<X, T>::operator/=(const double &k)
|
254
|
256
|
{
|
255
|
257
|
return div(k);
|
256
|
258
|
}
|
257
|
259
|
|
258
|
|
-tmpl bool VectorXD<X>::operator==(const VectorXD<X> &other) const
|
|
260
|
+tmpl bool GenericVector<X, T>::operator==(const T &other) const
|
259
|
261
|
{
|
260
|
262
|
return equal(other);
|
261
|
263
|
}
|
262
|
264
|
|
263
|
|
-tmpl bool VectorXD<X>::operator!=(const VectorXD<X> &other) const
|
|
265
|
+tmpl bool GenericVector<X, T>::operator!=(const T &other) const
|
264
|
266
|
{
|
265
|
267
|
return !equal(other);
|
266
|
268
|
}
|
267
|
269
|
|
268
|
|
-tmpl double VectorXD<X>::operator[](unsigned i) const
|
|
270
|
+tmpl double GenericVector<X, T>::operator[](unsigned i) const
|
269
|
271
|
{
|
270
|
272
|
return getScalar(i);
|
271
|
273
|
}
|
272
|
274
|
|
273
|
|
-tmpl bool VectorXD<X>::operator!() const
|
|
275
|
+tmpl bool GenericVector<X, T>::operator!() const
|
274
|
276
|
{
|
275
|
277
|
return isNull();
|
276
|
278
|
}
|
277
|
279
|
|
278
|
|
-tmpl VectorXD<X>::operator bool() const
|
|
280
|
+tmpl GenericVector<X, T>::operator bool() const
|
279
|
281
|
{
|
280
|
282
|
return !isNull();
|
281
|
283
|
}
|
282
|
284
|
|
283
|
|
-tmpl QDebug operator<<(QDebug dbg, const VectorXD<X> &v)
|
|
285
|
+tmpl T* GenericVector<X, T>::getThis() const
|
|
286
|
+{
|
|
287
|
+ return (T*)this;
|
|
288
|
+}
|
|
289
|
+
|
|
290
|
+tmpl QDebug operator<<(QDebug dbg, const T &v)
|
284
|
291
|
{
|
285
|
292
|
return dbg.nospace() << "(" << v.getX() << ", " << v.getY() << ", " << v.getZ() << ")";
|
286
|
293
|
}
|