1#ifndef MECACELL_VECTOR3D_H
2#define MECACELL_VECTOR3D_H
8#include "../utilities/config.hpp"
9#include "../utilities/exportable.hpp"
10#include "../utilities/utils.hpp"
29 inline explicit Vector3D(
double a) : coords{{a, a, a}} {}
30 inline explicit Vector3D(std::array<double, 3> c) : coords(c) {}
42 if (&other ==
this)
return *
this;
76 inline double &
xRef() {
return coords[0]; }
82 inline double &
yRef() {
return coords[1]; }
88 inline double &
zRef() {
return coords[2]; }
94 inline double x()
const {
return coords[0]; }
100 inline double y()
const {
return coords[1]; }
106 inline double z()
const {
return coords[2]; }
113 inline void setX(
const double f) { coords[0] = f; }
119 inline void setY(
const double f) { coords[1] = f; }
125 inline void setZ(
const double f) { coords[2] = f; }
132 std::normal_distribution<double> nDist(0.0, 1.0);
133 coords = {{nDist(Config::globalRand()), nDist(Config::globalRand()),
134 nDist(Config::globalRand())}};
158 std::normal_distribution<double> nDist(0.0, amount);
159 return Vector3D(coords[0] + nDist(Config::globalRand()),
160 coords[1] + nDist(Config::globalRand()),
161 coords[2] + nDist(Config::globalRand()))
178 return (coords[0] == 0.0 && coords[1] == 0.0 && coords[2] == 0.0);
258 return sqrt(coords[0] * coords[0] + coords[1] * coords[1] + coords[2] * coords[2]);
266 return coords[0] * coords[0] + coords[1] * coords[1] + coords[2] * coords[2];
278 double halfangle = angle * 0.5;
281 return *
this +
cos(halfangle) * vcV + v.
cross(vcV);
292 double halfangle = r.
teta * 0.5;
295 return *
this +
cos(halfangle) * vcV + v.
cross(vcV);
306 double dTeta = v.
length();
324 res.
teta =
acos(min<double>(1.0, max<double>(-1.0, v0.
dot(v1))));
348 double nr = n.
dot(r);
349 return (nr == 0) ? 0 : n.
dot(o - p) / nr;
364 return p - (n.
dot(p - o) * n);
391 return q2.toAxisAngle();
411 return qres.toAxisAngle();
425 return getRotation(b0.
X, b0.
Y, b1.
X, b1.
Y);
454 return l > 0 ? *
this / l : zero();
460 s <<
"(" << coords[0] <<
" , " << coords[1] <<
", " << coords[2] <<
")";
471 inline int getHash(
const int a,
const int b)
const {
472 unsigned int A = (
unsigned int)(a >= 0 ? 2 * a : -2 * a - 1);
473 unsigned int B = (
unsigned int)(b >= 0 ? 2 * b : -2 * b - 1);
474 int C = ((A >= B ? A * A + A + B : A + B * B) / 2);
475 return (a < 0 && b < 0) || (a >= 0 && b >= 0) ? C : -C - 1;
485 static_cast<int>(
floor(coords[0])),
486 getHash(
static_cast<int>(
floor(coords[1])),
static_cast<int>(
floor(coords[2]))));
499 const std::function<
void(
const Vector3D &)> &fun,
500 const double inc = 1)
const {
505 for (
auto i = base.
x(); i <= nxt.
x(); i += inc) {
506 for (
auto j = base.
y(); j <= nxt.
y(); j += inc) {
507 for (
auto k = base.
z(); k <= nxt.
z(); k += inc) {
520 if (coords[1] == 0 && coords[0] == 0) {
523 return Vector3D(-coords[1], coords[0], 0.0);
527 if ((v - *
this).sqlength() > 0.000000000001) {
529 if (res.
sqlength() > 0.0000000000001)
return res;
general purpose 3D vector/point class.
void random()
sets the current vector as a random normalized one. Uniform direction distribution on all directions ...
Vector3D & operator/=(double d)
scalar division operator
Vector3D & operator*=(double d)
scalar multiplication operator
static Vector3D getProjectionOnPlane(const Vector3D &o, const Vector3D &n, const Vector3D &p)
project a point on a plane
Vector3D & operator=(const Vector3D &other)
assignment operator for copy constructing a vector
static void addAsAngularVelocity(const Vector3D &v, Rotation< Vector3D > &r)
adds a vector representing an angular Velocity (or any rotation coded on only one vector) to an exist...
double length() const
compute the length of the vector
static Vector3D getProjection(const Vector3D &A, const Vector3D &B, const Vector3D &P)
computes the orthogonal projection of a point onto a segment AB
static const int dimension
static Rotation< Vector3D > getRotation(const Vector3D &v0, const Vector3D &v1)
computes the rotation from one vector to another
std::array< double, 3 > coords
Vector3D(std::array< double, 3 > c)
Vector3D rotated(const Rotation< Vector3D > &r) const
gives a rotated copy of the current vector
Vector3D(const Vector3D &v)
int getHash() const
fast hash method
void setX(const double f)
setter for x coordinate
double sqlength() const
compute the square length of the current vector (faster than length)
static Vector3D zero()
constructs a zero vector
Vector3D ortho() const
deterministic generation of an orthogonal vector
void setY(const double f)
setter for y coordinate
Vector3D rotated(double angle, const Vector3D &vec) const
gives a rotated copy of the current vector
Vector3D & operator-=(const Vector3D &v)
substract operator
void setZ(const double f)
setter for z coordinate
static Rotation< Vector3D > rotateRotation(const Rotation< Vector3D > &start, const Rotation< Vector3D > &offset)
rotates the axis of rotation of a rotation
static Rotation< Vector3D > getRotation(const Vector3D &X0, const Vector3D &Y0, const Vector3D &X1, const Vector3D &Y1)
computes the rotation transform from basis X0,Y0 to X1,Y1
static double rayCast(const Vector3D &o, const Vector3D &n, const Vector3D &p, const Vector3D &r)
simple raycasting on a plane
Vector3D & operator+=(const Vector3D &v)
addition operator
int getHash(const int a, const int b) const
fast hash for two ints
const Vector3D cross(const Vector3D &v) const
cross product calculation
Vector3D ortho(const Vector3D &v) const
static Rotation< Vector3D > getRotation(const Basis< Vector3D > &b0, const Basis< Vector3D > &b1)
computes the rotation transform from basis b0 to b1
double dot(const Vector3D &v) const
dot product calculation
void normalize()
normalizes the vector
static Vector3D randomUnit()
creates a random normalized vector. Uniform direction distribution on all directions of a sphere.
Vector3D(double a, double b, double c)
bool isZero() const
returns true if all vector coordinates are equal to zero
EXPORTABLE(Vector3D, KV(coords))
Vector3D normalized() const
returns a normalized copy of the current vector
std::string toString() const
Vector3D deltaDirection(double amount)
returns a vector randomly tilted relatively to the original one
static Rotation< Vector3D > addRotations(const Rotation< Vector3D > &R0, const Rotation< Vector3D > &R1)
adds two rotations
void iterateTo(const Vector3D &v, const std::function< void(const Vector3D &)> &fun, const double inc=1) const
helper method to iterates over a 3D rectangular cuboid bounded by two corner vectors
A simple vector class template.
this file contains various miscellanious utility functions & helpers *
Vector3D operator/(const Vector3D &v, const double f)
bool operator!=(const Vector3D &v1, const Vector3D &v2)
Vector3D operator*(const Vector3D &v, const double f)
Vector3D operator-(const Vector3D &v)
ostream & operator<<(ostream &out, const Vector3D &v)
Vector3D operator+(const Vector3D &v, const double f)
bool operator==(const Vector3D &v1, const Vector3D &v2)
Provides common mathematical functions and vector operations.
double floor(double x)
Computes the floor of a number.
double acos(double x)
Computes the arc cosine of a number.
double sin(double x)
Computes the sine of a number.
double sqrt(double x)
Computes the square root of a number.
double max(double a, double b)
Computes the maximum of two numbers.
double min(double a, double b)
Computes the minimum of two numbers.
double ceil(double x)
Computes the ceiling of a number.
double cos(double x)
Computes the cosine of a number.
int operator()(const MecaCell::Vector3D &v) const