| 1 |
// Copyright(C) 1999-2020, 2023, 2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include <cmath> |
| 8 |
|
| 9 |
#include "vector3d.h" |
| 10 |
|
| 11 |
//---------------------------------------------------------------------------- |
| 12 |
vector3d::vector3d(double X, double Y, double Z) : x(X), y(Y), z(Z) {} |
| 13 |
|
| 14 |
//---------------------------------------------------------------------------- |
| 15 |
void vector3d::set(double X, double Y, double Z) |
| 16 |
{ |
| 17 |
x = X; |
| 18 |
y = Y; |
| 19 |
z = Z; |
| 20 |
} |
| 21 |
|
| 22 |
void vector3d::set(const double location[3]) |
| 23 |
{ |
| 24 |
x = location[0]; |
| 25 |
y = location[1]; |
| 26 |
z = location[2]; |
| 27 |
} |
| 28 |
|
| 29 |
vector3d &vector3d::reverse() |
| 30 |
{ |
| 31 |
x = -x; |
| 32 |
y = -y; |
| 33 |
z = -z; |
| 34 |
return *this; |
| 35 |
} |
| 36 |
|
| 37 |
bool vector3d::operator==(const vector3d &from) const |
| 38 |
{ |
| 39 |
return (x == from.x && y == from.y && z == from.z); |
| 40 |
} |
| 41 |
|
| 42 |
bool vector3d::operator!=(const vector3d &from) const |
| 43 |
{ |
| 44 |
return (x != from.x || y != from.y || z != from.z); |
| 45 |
} |
| 46 |
|
| 47 |
vector3d operator+(const vector3d &lhs, const vector3d &rhs) |
| 48 |
{ |
| 49 |
vector3d tmp(lhs); |
| 50 |
return tmp += rhs; |
| 51 |
} |
| 52 |
|
| 53 |
vector3d operator-(const vector3d &lhs, const vector3d &rhs) |
| 54 |
{ |
| 55 |
vector3d tmp(lhs); |
| 56 |
return tmp -= rhs; |
| 57 |
} |
| 58 |
|
| 59 |
vector3d operator*(const vector3d &lhs, double scalar) |
| 60 |
{ |
| 61 |
vector3d tmp(lhs); |
| 62 |
return tmp *= scalar; |
| 63 |
} |
| 64 |
|
| 65 |
vector3d vector3d::operator-() const |
| 66 |
{ |
| 67 |
vector3d tmp(x, y, z); |
| 68 |
return tmp *= -1.0; |
| 69 |
} |
| 70 |
|
| 71 |
vector3d operator*(double scalar, const vector3d &from) |
| 72 |
{ |
| 73 |
vector3d tmp(from); |
| 74 |
return tmp *= scalar; |
| 75 |
} |
| 76 |
|
| 77 |
vector3d operator/(const vector3d &lhs, double scalar) |
| 78 |
{ |
| 79 |
if (scalar != 0.0) { |
| 80 |
vector3d tmp(lhs); |
| 81 |
return tmp /= scalar; |
| 82 |
} |
| 83 |
return {HUGE_VAL, HUGE_VAL, HUGE_VAL}; |
| 84 |
} |
| 85 |
|
| 86 |
vector3d &vector3d::operator/=(double scalar) |
| 87 |
{ |
| 88 |
if (scalar != 0.0) { |
| 89 |
x /= scalar; |
| 90 |
y /= scalar; |
| 91 |
z /= scalar; |
| 92 |
} |
| 93 |
else { |
| 94 |
x = HUGE_VAL; |
| 95 |
y = HUGE_VAL; |
| 96 |
z = HUGE_VAL; |
| 97 |
} |
| 98 |
|
| 99 |
return *this; |
| 100 |
} |
| 101 |
|
| 102 |
double vector3d::length() const { return std::sqrt(x * x + y * y + z * z); } |
| 103 |
|
| 104 |
double vector3d::normalize(double tolerance) |
| 105 |
{ |
| 106 |
double mylength = length(); |
| 107 |
|
| 108 |
if (mylength > tolerance) { |
| 109 |
x /= mylength; |
| 110 |
y /= mylength; |
| 111 |
z /= mylength; |
| 112 |
} |
| 113 |
else { |
| 114 |
mylength = 0.0; |
| 115 |
} |
| 116 |
|
| 117 |
return mylength; |
| 118 |
} |
| 119 |
|
| 120 |
vector3d vector3d::plane_normal(const vector3d &v1, const vector3d &v2, const vector3d &v3) |
| 121 |
{ |
| 122 |
vector3d v32 = v3; |
| 123 |
v32 -= v2; |
| 124 |
vector3d v12 = v1; |
| 125 |
v12 -= v2; |
| 126 |
return v32.cross(v12); |
| 127 |
} |