-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.hpp
326 lines (289 loc) · 11 KB
/
vec.hpp
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
#ifndef vec_HPP_
#define vec_HPP_
/////////////////////////////////////////////////
// vec.hpp
/////////////////////////////////////////////////
// Copyright (c) 2013, Harrison Leadlay
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1) Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2) Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/////////////////////////////////////////////////
//General design comments
//=============================================//
//
//
//
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/// \mainpage Sumbeard's Tools (sbt)
///
/// \version 0.1a
///
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/// \namespace sbt Sumbeard's Tools
/// \brief Sumbeard's tools
/// Currently only includes a vector template class
/////////////////////////////////////////////////
namespace sbt
{
//=============================================//
// Classes
//=============================================//
/////////////////////////////////////////////////
// Virtual base class for vec<T, L> that includes all methods common to all vec
// classes (e.g., vec<bool, L>, vec<T, L> have the same constructors, so those
// are inherited from vec_base
/////////////////////////////////////////////////
template <typename T, unsigned int L>
class vec_base
{
private:
// all vec classes have len = length/dimensions/#-components
// d[] = array of data (components)
const static unsigned int len = L;
T d[L];
public:
vec_base ();
vec_base (const T value);
vec_base (const vec_base<T, L>& v);
vec_base (const T v[L]);
/////////////////////////////////////////////////
/// \brief Constructor specific to 2-d vectors
///
/// \param component values
///
/////////////////////////////////////////////////
vec_base (T c0, T c1);
/////////////////////////////////////////////////
/// \brief Constructor specific to 3-d vectors
///
/// \param component values
///
/////////////////////////////////////////////////
vec_base (T c0, T c1, T c2);
/////////////////////////////////////////////////
/// \brief Constructor specific to 4-d vectors
///
/// \param component values
///
/////////////////////////////////////////////////
vec_base (T c0, T c1, T c2, T c3);
/////////////////////////////////////////////////
/// \brief Access component directly (via reference).
/// \param [in] index index of component (starting from 0)
/// \return Reference to component at index
/// \warning This method does not check the index to be in bounds
///
/////////////////////////////////////////////////
T& operator[] (const unsigned int index);
/////////////////////////////////////////////////
/// \brief Access component directly (via reference) read-only.
/// \param [in] index index of component (starting from 0)
/// \return Reference to component at index
/// \warning This method does not check the index to be in bounds
///
/////////////////////////////////////////////////
const T& operator[] (const unsigned int index) const;
/////////////////////////////////////////////////
/// \brief Return a copy of component at index.
/// \param index index of component (starting from 0)
///
/// \return Copy of component at index
/// \exception out_of_bounds if index is too large
///
/////////////////////////////////////////////////
T get (const unsigned int index) const;
/////////////////////////////////////////////////
/// \brief Copy vector by component
/// \param v vector to copy
/// \return vector with value of v
///
/////////////////////////////////////////////////
const vec_base& operator= (const vec_base& v);
/////////////////////////////////////////////////
/// \brief Copy array to vector by component
///
/// \param v array to copy
/// \return vector with value of
///
/////////////////////////////////////////////////
const vec_base& operator= (const T v[L]);
/////////////////////////////////////////////////
/// \brief Equals comparison
///
/// \return returns `true` if vectors are equal component-wise
/// `false` if any one set of components are not equal
/// * <1, 2, 3> == <1, 2, 3> == true
/// <2, 1, 3> == <1, 2, 3> == false
///
/// \warning Consider floating-point error when using such types of vectors
///
/////////////////////////////////////////////////
bool operator== (const vec_base<T, L>& v) const;
/////////////////////////////////////////////////
/// Returns the number of components/dimensions of the vector
/// \return length
///
/////////////////////////////////////////////////
const unsigned int length () const;
}; // class vec_base
/////////////////////////////////////////////////
/// \brief A vector (as in mathematics and physics) template class
///
/////////////////////////////////////////////////
template <typename T, unsigned int L>
class vec : public vec_base<T, L>
{
public:
// Inherited constructors (could use this in c++11 to inherit constructors):
// using vec_base<T, L>::vec_base;
// (or something similar) would work
// otherwise( ! c++11):
vec() : vec_base<T, L> () {}
vec(const T value) : vec_base<T, L> (value) {}
vec(const vec_base<T, L>& v) : vec_base<T, L>(v) {}
vec(const T v[L]): vec_base<T, L> (v) {}
vec(T c0, T c1) : vec_base<T, L> (c0, c1) {}
vec(T c0, T c1, T c2) : vec_base<T, L> (c0, c1, c2) {}
vec(T c0, T c1, T c2, T c3) : vec_base<T, L> (c0, c1, c2, c3) {}
/////////////////////////////////////////////////
/// \brief Vector addition
///
/// \param vec to add
/// \return sum vector
///
/////////////////////////////////////////////////
vec operator+ (const vec<T, L>& v) const;
/////////////////////////////////////////////////
/// \brief Vector substraction
///
/// \param vec to subtract
/// \return difference vector
///
/////////////////////////////////////////////////
vec operator- (const vec<T, L>& v) const;
/////////////////////////////////////////////////
/// \brief vector multiplication
///
/// \param v vec to multiply
/// \return product vector
///
/////////////////////////////////////////////////
vec operator* (const vec<T, L>& v) const;
/////////////////////////////////////////////////
/// \brief Multiply vector by scalar
///
/// \param scalar
/// \return product vector
///
/////////////////////////////////////////////////
vec operator* (const T& s) const;
/////////////////////////////////////////////////
/// \brief Vector negation
///
/// \return negated vector
///
/////////////////////////////////////////////////
vec operator- () const;
/////////////////////////////////////////////////
/// \brief Calculates the norm/magnitude of the vector
///
/// \return norm of vector
///
/////////////////////////////////////////////////
T norm() const;
/////////////////////////////////////////////////
/// \brief Calculates a unit vector in the direction of v
///
/// \return unit normal vector
///
/////////////////////////////////////////////////
vec<T, L> normalize() const;
/////////////////////////////////////////////////
/// \brief Calculates a vector that is the difference of vectors `this` and `b`
///
/// \return difference vector
/////////////////////////////////////////////////
vec<T, L> diff( vec<T, L> b) const;
/////////////////////////////////////////////////
/// \brief Calculates a vector that spans half-way from `this` to `b`
/// (B - A) * 0.5
///
/// \return midpoint
/////////////////////////////////////////////////
vec<T, L> mid( vec<T, L> b) const;
/////////////////////////////////////////////////
/// \brief Destructor
///
/////////////////////////////////////////////////
~vec();
//=============================================//
// STATIC FUNCTIONS
//=============================================//
/////////////////////////////////////////////////
/// \brief Dot product
///
/// \param a left vec argument
/// \param b right vec argument
/// \return dot product
///
/////////////////////////////////////////////////
static T dot(const vec<T, L>& a, const vec<T, L>& b);
/////////////////////////////////////////////////
/// \brief Cross product
///
/// \param a left vec<T, 3u> argument
/// \param b right vec<T, 3u> argument
/// \return vec<T, 3u> orthogonal to a and b
///
/////////////////////////////////////////////////
static vec<T, 3u> cross(const vec<T, 3u>& a, const vec<T, 3u>& b);
}; //class vec
//=============================================//
// Class Specializations
//=============================================//
template <unsigned int L>
class vec<bool, L> : public vec_base<bool, L>
{
// Inherited constructors (could use this in c++11 to inherit constructors):
// using vec_base<bool, L>::vec_base;
// (or something similar) would work.
// Otherwise:
vec() : vec_base<bool, L> () {}
vec(const bool value) : vec_base<bool, L> (value) {}
vec(const vec_base<bool, L>& v) : vec_base<bool, L>(v) {}
vec(const bool v[L]): vec_base<bool, L> (v) {}
vec(bool c0, bool c1) : vec_base<bool, L> (c0, c1) {}
vec(bool c0, bool c1, bool c2) : vec_base<bool, L> (c0, c1, c2) {}
vec(bool c0, bool c1, bool c2, bool c3) : vec_base<bool, L> (c0, c1, c2, c3) {}
/////////////////////////////////////////////////
/// \brief logical negation
///
/// \return component-wise negated boolean vector
///
/////////////////////////////////////////////////
vec<bool, L> operator! () const;
};
} //namespace sbt
#endif //vec_HPP_