forked from robosam2003/Vector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
187 lines (154 loc) · 4.75 KB
/
Vector.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
//
// Created by robosam2003 2022
//
#ifndef VECTOR_LIBRARY_H
#define VECTOR_LIBRARY_H
// External
#include "initializer_list.h" // for avr devices /:)
#include <math.h>
template<typename T, unsigned int s>
class Vector {
public:
Vector();
// constructors
// explicit Vector(int size);
Vector(std::initializer_list<T> lst);
int getSize() const;
// Copy
/**
* @brief Copy constructor for Vector, note that Vector is a resource handler, so a copy constructor is needed such that copying is not a memberwise copy
* @param a
*/
Vector(const Vector& a); // copy constructor
/**
* @brief Copy assignment constructor for Vector. Usage: Vector<int> v1 {1, 2, 3}; Vector<int> v2 = v1;
* @param a
* @return
*/
Vector& operator=(const Vector& a); // copy assignment
// Move
/**
* @brief Move constructor for Vector.
* @param a - an rvalue reference to a Vector
*/
Vector(Vector&& a) noexcept ; // move constructor
/**
* @brief Move assignment constructor for Vector. Usage: Vector<int> v1 {1, 2, 3}; Vector<int> v2 = std::move(v1);
* @param a
* @return
*/
Vector& operator=(Vector&& a) noexcept ; // move assignment
// operators
/**
* @brief operator []
* @param i
* @return the i-th element of the vector
*/
T& operator[](size_t i); /// const specifier means "Do not modify in this scope" - i.e. we do not modify member variables in this scope
const T& operator[](size_t i) const;
/**
* @brief operator / : divide all elements by a scalar
* @param scalar
* @return the vector divided by the scalar
*/
Vector<T, s> operator/(double scalar) const;
/**
* @brief operator /= : divide equals all elements by a scalar
* @param scalar
* @return reference to the caller vector
*/
Vector<T, s> & operator/=(double scalar);
/**
* @brief operator * : multiply all elements by a scalar
* @param scalar
* @return the vector multiplied by the scalar
*/
Vector<T, s> operator*(double scalar) const;
/**
* @brief operator *= : multiply equals all elements by a scalar
* @param scalar
* @return reference to the caller vector
*/
Vector<T, s>& operator*=(double scalar);
/**
* @brief operator += : add equals two vectors of the same size together
* @param v
* @return reference to the caller vector
*/
Vector<T, s>& operator+=(Vector<T, s> v);
/**
* @brief operator -= : subtract v from caller vector
* @param v
* @return reference to the caller vector
*/
Vector<T, s>& operator-=(Vector<T, s> v);
/**
* @brief operator + : add two vectors of the same size together
* @param v
* @return the result of the sum
*/
Vector<T, s> operator+(Vector<T, s> v) const;
/**
* @brief operator - : subtract v from caller vector
* @param v
* @return the result of the sum
*/
Vector<T, s>operator-(Vector<T, s> v);
/**
* @return the size of the Vector
*/
unsigned int size();
// Casting operators
/**
* @brief C-style cast to a different vector type, or size, or both!
* @return
*/
template<typename D, unsigned int newSize>
explicit operator Vector<D, newSize>() const;
// TODO: iterators? begin functions for range-for?
// Vector operations
/**
* @brief calculate the norm of a vector
* @return the norm of the caller vector
*/
double norm() const;
/**
* @brief calculate the magnitude of a vector
* @return the magnitude of the caller vector
*/
double magnitude() const;
/**
* @brief calculate the dot product of the caller vector and v
* @param v
* @return the dot (scalar) product of the caller vector and v
*/
double dot(Vector<T, s> v) const;
/**
* @brief normalise the caller vector
* @return reference to the normalised caller vector
*/
Vector<T, s>& normalize();
/**
* @brief normalise the caller vector
* @return the normalised caller vector.
*/
Vector<T, s> normalized() const;
/**
* @brief calculate the cross product of the caller vector and v
* @param v
* @return the cross product of the caller vector and v
*/
Vector<T, s> cross(Vector<T, s> v) const;
/**
* @brief calculate the direction cosines triad of the caller vector
* @param v
* @return the direction cosines triad of the caller vector
*/
Vector<T, s> directionCosines() const;
// TODO: Rotation matrices etc
~Vector() = default; // destructor - implicit definition
protected:
T elem[s];
};
#include "Vector.tpp"
#endif //VECTOR_LIBRARY_H