-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVerdictVector.hpp
471 lines (383 loc) · 12.2 KB
/
VerdictVector.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*=========================================================================
Module: VerdictVector.hpp
Copyright 2003,2006,2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
Under the terms of Contract DE-NA0003525 with NTESS,
the U.S. Government retains certain rights in this software.
See LICENSE for details.
=========================================================================*/
/*
*
* VerdictVector.hpp contains declarations of vector operations
*
* This file is part of VERDICT
*
*/
// .SECTION Thanks
// Prior to its inclusion within VTK, this code was developed by the CUBIT
// project at Sandia National Laboratories.
#ifndef VERDICTVECTOR_HPP
#define VERDICTVECTOR_HPP
#include "verdict.h"
#include <cassert>
#include <cmath>
namespace VERDICT_NAMESPACE
{
class VerdictVector
{
public:
//- Heading: Constructors and Destructor
VerdictVector(); //- Default constructor.
VerdictVector(const double x, const double y, const double z);
//- Constructor: create vector from three components
VerdictVector(const double xyz[3]);
//- Constructor: create vector from tuple
VerdictVector(const VerdictVector& tail, const VerdictVector& head);
VerdictVector(const double *tail, const double *head, int dimension);
VerdictVector(const double *tail, const double *head);
//- Constructor for a VerdictVector starting at tail and pointing
//- to head.
template <typename ARG1, typename ARG2, typename ARG3> VerdictVector(ARG1, ARG2, ARG3) = delete;
//- define this template to avoid ambiguity between the (double, double, double) and (double *, double *, int) constructors
VerdictVector(const VerdictVector& copy_from); //- Copy Constructor
//- Heading: Set and Inquire Functions
void set(const double xv, const double yv, const double zv);
//- Change vector components to {x}, {y}, and {z}
void set(const double xyz[3]);
//- Change vector components to xyz[0], xyz[1], xyz[2]
void set(const VerdictVector& tail, const VerdictVector& head);
//- Change vector to go from tail to head.
void set(const VerdictVector& to_copy);
//- Same as operator=(const VerdictVector&)
double x() const; //- Return x component of vector
double y() const; //- Return y component of vector
double z() const; //- Return z component of vector
void get_xyz(double& x, double& y, double& z); //- Get x, y, z components
void get_xyz(double xyz[3]); //- Get xyz tuple
double& r(); //- Return r component of vector, if (r,theta) format
double& theta(); //- Return theta component of vector, if (r,theta) format
void x(const double xv); //- Set x component of vector
void y(const double yv); //- Set y component of vector
void z(const double zv); //- Set z component of vector
void r(const double xv); //- Set r component of vector, if (r,theta) format
void theta(const double yv); //- Set theta component of vector, if (r,theta) format
double normalize();
//- Normalize (set magnitude equal to 1) vector - return the magnitude
VerdictVector& length(const double new_length);
//- Change length of vector to {new_length}. Can be used to move a
//- location a specified distance from the origin in the current
//- orientation.
double length() const;
//- Calculate the length of the vector.
//- Use {length_squared()} if only comparing lengths, not adding.
double length_squared() const;
//- Calculate the squared length of the vector.
//- Faster than {length()} since it eliminates the square root if
//- only comparing other lengths.
double interior_angle(const VerdictVector& otherVector);
//- Calculate the interior angle: acos((a%b)/(|a||b|))
//- Returns angle in degrees.
void perpendicular_z();
//- Transform this vector to a perpendicular one, leaving
//- z-component alone. Rotates clockwise about the z-axis by pi/2.
//- Heading: Operator Overloads *****************************
VerdictVector& operator+=(const VerdictVector& vec);
//- Compound Assignment: addition: {this = this + vec}
VerdictVector& operator-=(const VerdictVector& vec);
//- Compound Assignment: subtraction: {this = this - vec}
VerdictVector& operator*=(const VerdictVector& vec);
//- Compound Assignment: cross product: {this = this * vec},
//- non-commutative
VerdictVector& operator*=(const double scalar);
//- Compound Assignment: multiplication: {this = this * scalar}
VerdictVector& operator/=(const double scalar);
//- Compound Assignment: division: {this = this / scalar}
VerdictVector operator-() const;
//- unary negation.
friend VerdictVector operator~(const VerdictVector& vec);
//- normalize. Returns a new vector which is a copy of {vec},
//- scaled such that {|vec|=1}. Uses overloaded bitwise NOT operator.
friend VerdictVector operator+(const VerdictVector& v1, const VerdictVector& v2);
//- vector addition
friend VerdictVector operator-(const VerdictVector& v1, const VerdictVector& v2);
//- vector subtraction
friend VerdictVector operator*(const VerdictVector& v1, const VerdictVector& v2);
//- vector cross product, non-commutative
friend VerdictVector operator*(const VerdictVector& v1, const double sclr);
//- vector * scalar
friend VerdictVector operator*(const double sclr, const VerdictVector& v1);
//- scalar * vector
friend double operator%(const VerdictVector& v1, const VerdictVector& v2);
//- dot product
static double Dot(const VerdictVector& v1, const VerdictVector& v2);
//- dot product
friend VerdictVector operator/(const VerdictVector& v1, const double sclr);
//- vector / scalar
friend int operator==(const VerdictVector& v1, const VerdictVector& v2);
//- Equality operator
friend int operator!=(const VerdictVector& v1, const VerdictVector& v2);
//- Inequality operator
VerdictVector& operator=(const VerdictVector& from);
private:
double xVal; //- x component of vector.
double yVal; //- y component of vector.
double zVal; //- z component of vector.
};
inline double VerdictVector::x() const
{
return xVal;
}
inline double VerdictVector::y() const
{
return yVal;
}
inline double VerdictVector::z() const
{
return zVal;
}
inline void VerdictVector::get_xyz(double xyz[3])
{
xyz[0] = xVal;
xyz[1] = yVal;
xyz[2] = zVal;
}
inline void VerdictVector::get_xyz(double& xv, double& yv, double& zv)
{
xv = xVal;
yv = yVal;
zv = zVal;
}
inline double& VerdictVector::r()
{
return xVal;
}
inline double& VerdictVector::theta()
{
return yVal;
}
inline void VerdictVector::x(const double xv)
{
xVal = xv;
}
inline void VerdictVector::y(const double yv)
{
yVal = yv;
}
inline void VerdictVector::z(const double zv)
{
zVal = zv;
}
inline void VerdictVector::r(const double xv)
{
xVal = xv;
}
inline void VerdictVector::theta(const double yv)
{
yVal = yv;
}
inline VerdictVector& VerdictVector::operator+=(const VerdictVector& vector)
{
xVal += vector.x();
yVal += vector.y();
zVal += vector.z();
return *this;
}
inline VerdictVector& VerdictVector::operator-=(const VerdictVector& vector)
{
xVal -= vector.x();
yVal -= vector.y();
zVal -= vector.z();
return *this;
}
inline VerdictVector& VerdictVector::operator*=(const VerdictVector& vector)
{
double xcross, ycross, zcross;
xcross = yVal * vector.z() - zVal * vector.y();
ycross = zVal * vector.x() - xVal * vector.z();
zcross = xVal * vector.y() - yVal * vector.x();
xVal = xcross;
yVal = ycross;
zVal = zcross;
return *this;
}
inline VerdictVector::VerdictVector(const VerdictVector& copy_from)
: xVal(copy_from.xVal)
, yVal(copy_from.yVal)
, zVal(copy_from.zVal)
{
}
inline VerdictVector::VerdictVector()
: xVal(0)
, yVal(0)
, zVal(0)
{
}
inline VerdictVector::VerdictVector(const double *tail, const double *head, int dimension)
: xVal{head[0] - tail[0]}
, yVal{head[1] - tail[1]}
, zVal{dimension == 2 ? 0.0 : head[2] - tail[2]}
{
}
inline VerdictVector::VerdictVector(const double *tail, const double *head)
: xVal{head[0] - tail[0]}
, yVal{head[1] - tail[1]}
, zVal{head[2] - tail[2]}
{
}
inline VerdictVector::VerdictVector(const VerdictVector& tail, const VerdictVector& head)
: xVal(head.xVal - tail.xVal)
, yVal(head.yVal - tail.yVal)
, zVal(head.zVal - tail.zVal)
{
}
inline VerdictVector::VerdictVector(const double xIn, const double yIn, const double zIn)
: xVal(xIn)
, yVal(yIn)
, zVal(zIn)
{
}
// This sets the vector to be perpendicular to it's current direction.
// NOTE:
// This is a 2D function. It only works in the XY Plane.
inline void VerdictVector::perpendicular_z()
{
double temp = x();
x(y());
y(-temp);
}
inline void VerdictVector::set(const double xv, const double yv, const double zv)
{
xVal = xv;
yVal = yv;
zVal = zv;
}
inline void VerdictVector::set(const double xyz[3])
{
xVal = xyz[0];
yVal = xyz[1];
zVal = xyz[2];
}
inline void VerdictVector::set(const VerdictVector& tail, const VerdictVector& head)
{
xVal = head.xVal - tail.xVal;
yVal = head.yVal - tail.yVal;
zVal = head.zVal - tail.zVal;
}
inline VerdictVector& VerdictVector::operator=(const VerdictVector& from)
{
xVal = from.xVal;
yVal = from.yVal;
zVal = from.zVal;
return *this;
}
inline void VerdictVector::set(const VerdictVector& to_copy)
{
*this = to_copy;
}
// Scale all values by scalar.
inline VerdictVector& VerdictVector::operator*=(const double scalar)
{
xVal *= scalar;
yVal *= scalar;
zVal *= scalar;
return *this;
}
// Scales all values by 1/scalar
inline VerdictVector& VerdictVector::operator/=(const double scalar)
{
assert(scalar != 0);
xVal /= scalar;
yVal /= scalar;
zVal /= scalar;
return *this;
}
// Returns the normalized 'this'.
inline VerdictVector operator~(const VerdictVector& vec)
{
double mag = std::sqrt(vec.xVal * vec.xVal + vec.yVal * vec.yVal + vec.zVal * vec.zVal);
VerdictVector temp = vec;
if (mag != 0.0)
{
temp /= mag;
}
return temp;
}
// Unary minus. Negates all values in vector.
inline VerdictVector VerdictVector::operator-() const
{
return VerdictVector(-xVal, -yVal, -zVal);
}
inline VerdictVector operator+(const VerdictVector& vector1, const VerdictVector& vector2)
{
double xv = vector1.x() + vector2.x();
double yv = vector1.y() + vector2.y();
double zv = vector1.z() + vector2.z();
return VerdictVector(xv, yv, zv);
// return VerdictVector(vector1) += vector2;
}
inline VerdictVector operator-(const VerdictVector& vector1, const VerdictVector& vector2)
{
double xv = vector1.x() - vector2.x();
double yv = vector1.y() - vector2.y();
double zv = vector1.z() - vector2.z();
return VerdictVector(xv, yv, zv);
// return VerdictVector(vector1) -= vector2;
}
// Cross products.
// vector1 cross vector2
inline VerdictVector operator*(const VerdictVector& vector1, const VerdictVector& vector2)
{
return VerdictVector(vector1) *= vector2;
}
// Returns a scaled vector.
inline VerdictVector operator*(const VerdictVector& vector1, const double scalar)
{
return VerdictVector(vector1) *= scalar;
}
// Returns a scaled vector
inline VerdictVector operator*(const double scalar, const VerdictVector& vector1)
{
return VerdictVector(vector1) *= scalar;
}
// Returns a vector scaled by 1/scalar
inline VerdictVector operator/(const VerdictVector& vector1, const double scalar)
{
return VerdictVector(vector1) /= scalar;
}
inline int operator==(const VerdictVector& v1, const VerdictVector& v2)
{
return (v1.xVal == v2.xVal && v1.yVal == v2.yVal && v1.zVal == v2.zVal);
}
inline int operator!=(const VerdictVector& v1, const VerdictVector& v2)
{
return (v1.xVal != v2.xVal || v1.yVal != v2.yVal || v1.zVal != v2.zVal);
}
inline double VerdictVector::length_squared() const
{
return (xVal * xVal + yVal * yVal + zVal * zVal);
}
inline double VerdictVector::length() const
{
return (std::sqrt(xVal * xVal + yVal * yVal + zVal * zVal));
}
inline double VerdictVector::normalize()
{
double mag = length();
if (mag != 0)
{
xVal = xVal / mag;
yVal = yVal / mag;
zVal = zVal / mag;
}
return mag;
}
// Dot Product.
inline double operator%(const VerdictVector& vector1, const VerdictVector& vector2)
{
return VerdictVector::Dot(vector1, vector2);
}
inline double VerdictVector::Dot(const VerdictVector& vector1, const VerdictVector& vector2)
{
return (vector1.xVal * vector2.xVal + vector1.yVal * vector2.yVal + vector1.zVal * vector2.zVal);
}
} // namespace verdict
#endif