-
Notifications
You must be signed in to change notification settings - Fork 0
/
blVector3dOperations.hpp
379 lines (291 loc) · 11.7 KB
/
blVector3dOperations.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
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
#ifndef BL_VECTOR3DOPERATIONS_HPP
#define BL_VECTOR3DOPERATIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: Useful functions for handling 3d vector
///
/// AUTHOR: Vincenzo Barbato
/// navyenzo@gmail.com
///
/// NOTE: All things in this library are defined within the
/// blMathAPI namespace
///
/// LISENSE: MIT-LICENCE
/// http://www.opensource.org/licenses/mit-license.php
///
///
///
///-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include "blVector3d.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blMathAPI namespace
//-------------------------------------------------------------------
namespace blMathAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the L1-norm of a vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType norm1(const blVector3d<blNumberType>& v)
{
return ( blMathAPI::abs(v.x()) + blMathAPI::abs(v.y()) + blMathAPI::abs(v.z()) );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the L2-norm of a vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType norm2(const blVector3d<blNumberType>& v)
{
return std::sqrt( v.x()*v.x() + v.y()*v.y() + v.z()*v.z() );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the Linf-norm of a vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType normInf(const blVector3d<blNumberType>& v)
{
auto tempMax = std::max(v.x(),v.y());
return std::max(tempMax,v.z());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Calculate the Lp-norm of a vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType normP(const blVector3d<blNumberType>& v,const int& p)
{
return blMathAPI::power(blMathAPI::power(v.x(),p) + blMathAPI::power(v.y(),p)+ blMathAPI::power(v.z(),p),
1.0 / double(p));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to normalize a vector or to get
// a normalized vector from a given vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blVector3d<blNumberType>& normalize(blVector3d<blNumberType>& v)
{
v /= norm2(v);
return v;
}
template<typename blNumberType>
inline blVector3d<blNumberType> getNormalized(const blVector3d<blNumberType>& v)
{
return (v/norm2(v));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to get a vector perpendicular
// to a given vector such that the dot product
// would give zero
//-------------------------------------------------------------------
template<typename blNumberType>
inline blVector3d<blNumberType> getPerpendicularUnitVector(const blVector3d<blNumberType>& v)
{
// Find a component of this
// vector that is non zero
// and if unsuccessfull,
// just return the zero vector
//
// P1*P2 = x1*x2 + y1*y2 + z1*z2 = 0
if(v.x() != 0)
{
if(v.y() == 0)
return blVector3d<blNumberType>(0,1,0);
else
{
if(v.z() == 0)
{
return blVector3d<blNumberType>(0,0,1);
}
else
{
blVector3d<blNumberType> vector( 1, 1, -(v.x() + v.y())/v.z() );
normalize(vector);
return vector;
}
}
}
else if(v.y() != 0 || v.z() != 0)
{
return blVector3d<blNumberType>(1,0,0);
}
// The vector was a zero
// vector, therefore any
// vector would be perpendicular,
// thus we are just going
// to return the zero vector
return blVector3d<blNumberType>(0,0,0);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to return a vector
// whose components are fractional
// inverses of the original vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline blVector3d<blNumberType> getVectorWithFractionalInvertedElements(const blVector3d<blNumberType>& v)
{
return blVector3d<blNumberType>(blNumberType(1)/v.x(),blNumberType(1)/v.y(),blNumberType(1)/v.z());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to take the cross
// product of two vectors
//-------------------------------------------------------------------
template<typename blNumberType>
inline blVector3d<blNumberType> crossProduct(const blVector3d<blNumberType>& v1,
const blVector3d<blNumberType>& v2)
{
return blVector3d<blNumberType>(v1.y()*v2.z() - v1.z()*v2.y(),
-v1.x()*v2.z() + v1.z()*v2.x(),
v1.x()*v2.y() - v1.y()*v2.x());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// output a vector to
// an output stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::ostream& operator<<(std::ostream& os,
const blVector3d<blNumberType>& v)
{
// We simply output the x and y
// components with a space
// separating them
os << v.x() << " " << v.y() << " " << v.z();
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// input a point from
// an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::istream& operator>>(std::istream& is,
blVector3d<blNumberType>& v)
{
if(!(is >> v.x()))
{
// Error -- Cound not read
// the x value
return is;
}
if(!(is >> v.y()))
{
// Error -- Cound not read
// the y value
return is;
}
if(!(is >> v.z()))
{
// Error -- Cound not read
// the z value
return is;
}
return is;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to return a vector
// with the minimum or maximum values
// of each component when comparing
// multiple vector
//-------------------------------------------------------------------
template<typename blNumberType>
inline void min(const blVector3d<blNumberType>& v1,
const blVector3d<blNumberType>& v2,
blVector3d<blNumberType>& minV)
{
minV.x = std::min(v1.x(),v2.x());
minV.y = std::min(v1.y(),v2.y());
minV.z = std::min(v1.z(),v2.z());
}
template<typename blNumberType>
inline void max(const blVector3d<blNumberType>& v1,
const blVector3d<blNumberType>& v2,
blVector3d<blNumberType>& maxV)
{
maxV.x = std::max(v1.x(),v2.x());
maxV.y = std::max(v1.y(),v2.y());
maxV.z = std::max(v1.z(),v2.z());
}
template<typename blNumberType>
inline blVector3d<blNumberType> min(const blVector3d<blNumberType>& v1,
const blVector3d<blNumberType>& v2)
{
blVector3d<blNumberType> minV;
blMathAPI::min(v1,v2,minV);
return minV;
}
template<typename blNumberType>
inline blVector3d<blNumberType> max(const blVector3d<blNumberType>& v1,
const blVector3d<blNumberType>& v2)
{
blVector3d<blNumberType> maxV;
blMathAPI::max(v1,v2,maxV);
return maxV;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// convert a generic
// string to a vector
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType convertStringToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& decimalPointDelimiter,
blVector3d<blNumberType>& convertedNumber,
const int& numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter)
{
// We expect the
// following format:
// P = x y
blStringIteratorType newStringPosition;
// First we try to
// get the x value
newStringPosition = convertStringToNumber(beginIter,
endIter,
decimalPointDelimiter,
convertedNumber.x(),
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the y value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber.y(),
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the y value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber.z(),
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
return newStringPosition;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_VECTOR3DOPERATIONS_HPP