28#define RZ_DEFAULT_COMPARE_RELATIVE_ERROR 1e-9
29#define RZ_URANDSIGN (2 * (static_cast<Real>(rand()) / RAND_MAX - .5))
33 typedef std::complex<Real> Complex;
40 isZero(Real a, Real precision = 1e-9)
42 return fabs(a) < precision;
46 releq(Real a, Real b, Real precision = 1e-9)
48 if (isZero(b, precision))
49 return isZero(a, precision);
51 return fabs(a - b) / fabs(b) < precision;
55 fabsmin(Real a, Real b)
57 return fabs(a) < fabs(b) ? a : b;
61 fabsmax(Real a, Real b)
63 return fabs(a) > fabs(b) ? a : b;
76 inline Vec3(Real x, Real y, Real z) : x(x), y(y), z(z) { }
77 inline Vec3(
const Real coords[3]) :
78 x(coords ==
nullptr ? 0 : coords[0]),
79 y(coords ==
nullptr ? 0 : coords[1]),
80 z(coords ==
nullptr ? 0 : coords[2]) {}
100 return Vec3(0, 1, 0);
107 return Vec3(0, 0, 1);
112 operator * (
Vec3 const &v)
const
114 return x * v.x + y * v.y + z * v.z;
119 operator + (
Vec3 const &v)
const
121 return Vec3(x + v.x, y + v.y, z + v.z);
126 operator - (
Vec3 const &v)
const
128 return Vec3(x - v.x, y - v.y, z - v.z);
133 operator * (Real k)
const
135 return Vec3(k * x, k * y, k * z);
140 operator / (Real k)
const
142 return *
this * (1. / k);
147 operator += (
const Vec3 &vec) {
157 operator -= (
const Vec3 &vec) {
167 cross(
Vec3 const &v)
const
179 return sqrt(x * x + y * y + z * z);
193 isNull(Real tol = 1e-9)
const
195 return isZero(x, tol) && isZero(y, tol) && isZero(z, tol);
199 compare(
Vec3 const &other, Real dist)
const
201 return (other - *
this).norm() < dist;
205 compare(
Vec3 const &other)
const
207 return this->compare(other, std::numeric_limits<Real>::epsilon());
211 operator== (
Vec3 const &other)
const
213 Real norm = this->norm();
214 if (norm < std::numeric_limits<Real>::epsilon())
215 return other.norm() < std::numeric_limits<Real>::epsilon();
217 return (other - *
this).norm() / norm < RZ_DEFAULT_COMPARE_RELATIVE_ERROR;
221 operator!= (
Vec3 const &other)
const
223 return !(*
this == other);
229 return Vec3(-this->x, -this->y, -this->z);
233 copyToArray(Real *dest)
const
235 memcpy(dest, this->coords, 3 *
sizeof(Real));
239 setFromArray(
const Real *coords)
241 memcpy(this->coords, coords, 3 *
sizeof(Real));
248 "(" + std::to_string(this->x)
249 +
"," + std::to_string(this->y)
250 +
"," + std::to_string(this->z)
255 operator=(RZ:: Real v)
257 coords[0] = coords[1] = coords[2] = v;
269 expandBox(Vec3 &p1, Vec3 &p2, Vec3
const &newPoint)
271 p1.x = fmin(p1.x, newPoint.x);
272 p1.y = fmin(p1.y, newPoint.y);
273 p1.z = fmin(p1.z, newPoint.z);
275 p2.x = fmax(p2.x, newPoint.x);
276 p2.y = fmax(p2.y, newPoint.y);
277 p2.z = fmax(p2.z, newPoint.z);
282operator<<(std::ostream& os,
const RZ::Vec3& vec)
284 os << vec.toString();