-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathqbVector.h
More file actions
329 lines (268 loc) · 10.1 KB
/
qbVector.h
File metadata and controls
329 lines (268 loc) · 10.1 KB
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
// This file is part of the qbLinAlg linear algebra library.
/*
MIT License
Copyright (c) 2023 Michael Bennett
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef QBVECTOR_H
#define QBVECTOR_H
/* *************************************************************************************************
qbVector
Class to provide capability to handle vectors.
Created as part of the qbLinAlg linear algebra library, which is intended to be primarily for
educational purposes. For more details, see the corresponding videos on the QuantitativeBytes
YouTube channel at:
www.youtube.com/c/QuantitativeBytes
************************************************************************************************* */
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <vector>
template <class T>
class qbVector
{
public:
// Define the various constructors.
// Default.
qbVector();
// With a single integer specifying the number of dimensions.
qbVector(int numDims);
// With input data (std::vector).
qbVector(std::vector<T> inputData);
// And the destructor.
~qbVector();
// Functions to return parameters of the vector.
int GetNumDims() const;
// Functions to handle elements of the vector.
T GetElement(int index) const;
void SetElement(int index, T value);
// Functions to perform computations on the vector.
// Return the length of the vector.
T norm();
// Return a normalized copy of the vector.
qbVector<T> Normalized();
// Normalize the vector in place.
void Normalize();
// Overloaded operators.
qbVector<T> operator+ (const qbVector<T> &rhs) const;
qbVector<T> operator- (const qbVector<T> &rhs) const;
qbVector<T> operator* (const T &rhs) const;
// Friend functions.
template <class U> friend qbVector<U> operator* (const U &lhs, const qbVector<U> &rhs);
// Static functions.
static T dot(const qbVector<T> &a, const qbVector<T> &b);
static qbVector<T> cross(const qbVector<T> &a, const qbVector<T> &b);
private:
std::vector<T> m_vectorData;
int m_nDims;
};
/* **************************************************************************************************
CONSTRUCTOR / DESTRUCTOR FUNCTIONS
/* *************************************************************************************************/
// The default constructor.
template <class T>
qbVector<T>::qbVector()
{
m_nDims = 0;
m_vectorData = std::vector<T>();
}
template <class T>
qbVector<T>::qbVector(int numDims)
{
m_nDims = numDims;
m_vectorData = std::vector<T>(numDims, static_cast<T>(0.0));
}
template <class T>
qbVector<T>::qbVector(std::vector<T> inputData)
{
m_nDims = inputData.size();
m_vectorData = inputData;
}
template <class T>
qbVector<T>::~qbVector()
{
// For now, we don't need to do anything in the destructor.
}
/* **************************************************************************************************
FUNCTIONS TO RETURN PARAMETERS
/* *************************************************************************************************/
template <class T>
int qbVector<T>::GetNumDims() const
{
return m_nDims;
}
/* **************************************************************************************************
FUNCTIONS TO HANDLE ELEMENTS OF THE VECTOR
/* *************************************************************************************************/
template <class T>
T qbVector<T>::GetElement(int index) const
{
return m_vectorData[index];
}
template <class T>
void qbVector<T>::SetElement(int index, T value)
{
m_vectorData[index] = value;
}
/* **************************************************************************************************
FUNCTIONS TO PERFORM COMPUTATIONS ON THE VECTOR
/* *************************************************************************************************/
// Compute the length of the vector,known as the 'norm'.
template <class T>
T qbVector<T>::norm()
{
T cumulativeSum = static_cast<T>(0.0);
for (int i=0; i<m_nDims; ++i)
cumulativeSum += (m_vectorData.at(i) * m_vectorData.at(i));
return sqrt(cumulativeSum);
}
// Return a normalized copy of the vector.
template <class T>
qbVector<T> qbVector<T>::Normalized()
{
// Compute the vector norm.
T vecNorm = this->norm();
// Compute the normalized version of the vector.
qbVector<T> result(m_vectorData);
return result * (static_cast<T>(1.0) / vecNorm);
}
// Normalize the vector in place.
template <class T>
void qbVector<T>::Normalize()
{
// Compute the vector norm.
T vecNorm = this->norm();
// Compute the elements of the normalized version of the vector.
for (int i=0; i<m_nDims; ++i)
{
T temp = m_vectorData.at(i) * (static_cast<T>(1.0) / vecNorm);
m_vectorData.at(i) = temp;
}
}
/* **************************************************************************************************
OVERLOADED OPERATORS
Note the changes that have been made since the videos about this first code were made. The original
code has been left, but commented out. Ultimately it was necessary to make some changes to
improve the performance of these functions in terms of run time.
See this episode of my ray tracing in C++ series for further information:
https://youtu.be/-5kLk7_bs0U
/* *************************************************************************************************/
template <class T>
qbVector<T> qbVector<T>::operator+ (const qbVector<T> &rhs) const
{
// Check that the number of dimensions match.
if (m_nDims != rhs.m_nDims)
throw std::invalid_argument("Vector dimensions do not match.");
/*
std::vector<T> resultData;
for (int i=0; i<m_nDims; ++i)
resultData.push_back(m_vectorData.at(i) + rhs.m_vectorData.at(i));
qbVector<T> result(resultData);
return result;
*/
qbVector<T> resultData (m_nDims);
for (int i=0; i<m_nDims; ++i)
resultData.SetElement(i, (m_vectorData.at(i) + rhs.m_vectorData.at(i)));
return resultData;
}
template <class T>
qbVector<T> qbVector<T>::operator- (const qbVector<T> &rhs) const
{
// Check that the number of dimensions match.
if (m_nDims != rhs.m_nDims)
throw std::invalid_argument("Vector dimensions do not match.");
/*
std::vector<T> resultData;
for (int i=0; i<m_nDims; ++i)
resultData.push_back(m_vectorData.at(i) - rhs.m_vectorData.at(i));
qbVector<T> result(resultData);
return result;
*/
qbVector<T> resultData (m_nDims);
for (int i=0; i<m_nDims; ++i)
resultData.SetElement(i, (m_vectorData.at(i) - rhs.m_vectorData.at(i)));
return resultData;
}
template <class T>
qbVector<T> qbVector<T>::operator* (const T &rhs) const
{
// Perform scalar multiplication.
/*
std::vector<T> resultData;
for (int i=0; i<m_nDims; ++i)
resultData.push_back(m_vectorData.at(i) * rhs);
qbVector<T> result(resultData);
return result;
*/
qbVector<T> resultData (m_nDims);
for (int i=0; i<m_nDims; ++i)
resultData.SetElement(i, (m_vectorData.at(i) * rhs));
return resultData;
}
/* **************************************************************************************************
FRIEND FUNCTIONS
/* *************************************************************************************************/
template <class T>
qbVector<T> operator* (const T &lhs, const qbVector<T> &rhs)
{
// Perform scalar multiplication.
/*
std::vector<T> resultData;
for (int i=0; i<rhs.m_nDims; ++i)
resultData.push_back(lhs * rhs.m_vectorData.at(i));
qbVector<T> result(resultData);
return result;
*/
std::vector<T> resultData (rhs.m_nDims);
for (int i=0; i<rhs.m_nDims; ++i)
resultData.at(i) = (lhs * rhs.m_vectorData.at(i));
qbVector<T> result(resultData);
return result;
}
/* **************************************************************************************************
STATIC FUNCTIONS
/* *************************************************************************************************/
template <class T>
T qbVector<T>::dot(const qbVector<T> &a, const qbVector<T> &b)
{
// Check that the number of dimensions match.
if (a.m_nDims != b.m_nDims)
throw std::invalid_argument("Vector dimensions must match for the dot-product to be computed.");
// Compute the dot product.
T cumulativeSum = static_cast<T>(0.0);
for (int i=0; i<a.m_nDims; ++i)
cumulativeSum += a.m_vectorData.at(i) * b.m_vectorData.at(i);
return cumulativeSum;
}
template <class T>
qbVector<T> qbVector<T>::cross(const qbVector<T> &a, const qbVector<T> &b)
{
// Check that the number of dimensions match.
if (a.m_nDims != b.m_nDims)
throw std::invalid_argument("Vector dimensions must match for the cross-product to be computed.");
// Check that the number of dimensions is 3.
/* Although the cross-product is also defined for 7 dimensions, we are
not going to consider that case at this time. */
if (a.m_nDims != 3)
throw std::invalid_argument("The cross-product can only be computed for three-dimensional vectors.");
// Compute the cross product.
std::vector<T> resultData;
resultData.push_back((a.m_vectorData.at(1) * b.m_vectorData.at(2)) - (a.m_vectorData.at(2) * b.m_vectorData.at(1)));
resultData.push_back(-((a.m_vectorData.at(0) * b.m_vectorData.at(2)) - (a.m_vectorData.at(2) * b.m_vectorData.at(0))));
resultData.push_back((a.m_vectorData.at(0) * b.m_vectorData.at(1)) - (a.m_vectorData.at(1) * b.m_vectorData.at(0)));
qbVector<T> result(resultData);
return result;
}
#endif