2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2013-08-29 02:34:34 -03:00
|
|
|
// Copyright 2010 Michael Smith, all rights reserved.
|
2010-09-08 05:21:46 -03:00
|
|
|
|
|
|
|
// Derived closely from:
|
|
|
|
/****************************************
|
2012-08-17 03:20:14 -03:00
|
|
|
* 3D Vector Classes
|
|
|
|
* By Bill Perone (billperone@yahoo.com)
|
|
|
|
* Original: 9-16-2002
|
|
|
|
* Revised: 19-11-2003
|
|
|
|
* 11-12-2003
|
|
|
|
* 18-12-2003
|
|
|
|
* 06-06-2004
|
|
|
|
*
|
2018-09-11 06:54:56 -03:00
|
|
|
* Copyright 2003, This code is provided "as is" and you can use it freely as long as
|
2012-08-17 03:20:14 -03:00
|
|
|
* credit is given to Bill Perone in the application it is used in
|
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* if a*b = 0 then a & b are orthogonal
|
|
|
|
* a%b = -b%a
|
|
|
|
* a*(b%c) = (a%b)*c
|
|
|
|
* a%b = a(cast to matrix)*b
|
|
|
|
* (a%b).length() = area of parallelogram formed by a & b
|
|
|
|
* (a%b).length() = a.length()*b.length() * sin(angle between a & b)
|
|
|
|
* (a%b).length() = 0 if angle between a & b = 0 or a.length() = 0 or b.length() = 0
|
|
|
|
* a * (b%c) = volume of parallelpiped formed by a, b, c
|
|
|
|
* vector triple product: a%(b%c) = b*(a*c) - c*(a*b)
|
|
|
|
* scalar triple product: a*(b%c) = c*(a%b) = b*(c%a)
|
|
|
|
* vector quadruple product: (a%b)*(c%d) = (a*c)*(b*d) - (a*d)*(b*c)
|
|
|
|
* if a is unit vector along b then a%b = -b%a = -b(cast to matrix)*a = 0
|
2016-05-12 14:02:03 -03:00
|
|
|
* vectors a1...an are linearly dependent if there exists a vector of scalars (b) where a1*b1 + ... + an*bn = 0
|
2012-08-17 03:20:14 -03:00
|
|
|
* or if the matrix (A) * b = 0
|
|
|
|
*
|
|
|
|
****************************************/
|
2016-02-17 21:25:33 -04:00
|
|
|
#pragma once
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2020-11-05 19:28:44 -04:00
|
|
|
#ifndef MATH_CHECK_INDEXES
|
|
|
|
#define MATH_CHECK_INDEXES 0
|
|
|
|
#endif
|
|
|
|
|
2016-03-31 18:43:36 -03:00
|
|
|
#include <cmath>
|
2015-05-02 02:07:45 -03:00
|
|
|
#include <float.h>
|
2010-12-19 21:52:23 -04:00
|
|
|
#include <string.h>
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
2010-09-08 05:21:46 -03:00
|
|
|
|
2016-05-02 18:39:03 -03:00
|
|
|
#include "rotations.h"
|
|
|
|
|
2021-05-04 08:12:23 -03:00
|
|
|
#include "ftype.h"
|
|
|
|
|
2013-07-19 19:13:34 -03:00
|
|
|
template <typename T>
|
|
|
|
class Matrix3;
|
|
|
|
|
2021-06-12 04:46:22 -03:00
|
|
|
template <typename T>
|
|
|
|
class Vector2;
|
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
template <typename T>
|
|
|
|
class Vector3
|
|
|
|
{
|
2013-07-19 19:13:34 -03:00
|
|
|
|
2010-09-08 05:21:46 -03:00
|
|
|
public:
|
2012-08-17 03:20:14 -03:00
|
|
|
T x, y, z;
|
|
|
|
|
|
|
|
// trivial ctor
|
2016-04-22 09:59:24 -03:00
|
|
|
constexpr Vector3<T>()
|
|
|
|
: x(0)
|
|
|
|
, y(0)
|
|
|
|
, z(0) {}
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// setting ctor
|
2016-04-22 09:59:24 -03:00
|
|
|
constexpr Vector3<T>(const T x0, const T y0, const T z0)
|
|
|
|
: x(x0)
|
|
|
|
, y(y0)
|
|
|
|
, z(z0) {}
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2021-06-17 03:36:03 -03:00
|
|
|
//Create a Vector3 from a Vector2 with z
|
|
|
|
constexpr Vector3<T>(const Vector2<T> &v0, const T z0)
|
|
|
|
: x(v0.x)
|
|
|
|
, y(v0.y)
|
|
|
|
, z(z0) {}
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// test for equality
|
2013-04-11 21:04:18 -03:00
|
|
|
bool operator ==(const Vector3<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// test for inequality
|
2013-04-11 21:04:18 -03:00
|
|
|
bool operator !=(const Vector3<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// negation
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator -(void) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// addition
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator +(const Vector3<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// subtraction
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator -(const Vector3<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator *(const T num) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator /(const T num) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// addition
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> &operator +=(const Vector3<T> &v);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// subtraction
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> &operator -=(const Vector3<T> &v);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> &operator *=(const T num);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
|
|
|
// uniform scaling
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> &operator /=(const T num);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2018-09-28 14:40:15 -03:00
|
|
|
// non-uniform scaling
|
|
|
|
Vector3<T> &operator *=(const Vector3<T> &v) {
|
|
|
|
x *= v.x; y *= v.y; z *= v.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-29 03:37:14 -04:00
|
|
|
// allow a vector3 to be used as an array, 0 indexed
|
|
|
|
T & operator[](uint8_t i) {
|
|
|
|
T *_v = &x;
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
assert(i >= 0 && i < 3);
|
|
|
|
#endif
|
2013-12-29 22:24:33 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const T & operator[](uint8_t i) const {
|
|
|
|
const T *_v = &x;
|
2016-02-14 18:22:23 -04:00
|
|
|
#if MATH_CHECK_INDEXES
|
2013-12-29 22:24:33 -04:00
|
|
|
assert(i >= 0 && i < 3);
|
|
|
|
#endif
|
2013-12-29 03:37:14 -04:00
|
|
|
return _v[i];
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// dot product
|
2013-04-11 21:04:18 -03:00
|
|
|
T operator *(const Vector3<T> &v) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2020-05-07 18:23:36 -03:00
|
|
|
// dot product for Lua
|
|
|
|
T dot(const Vector3<T> &v) const {
|
|
|
|
return *this * v;
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:13:34 -03:00
|
|
|
// multiply a row vector by a matrix, to give a row vector
|
|
|
|
Vector3<T> operator *(const Matrix3<T> &m) const;
|
|
|
|
|
|
|
|
// multiply a column vector by a row vector, returning a 3x3 matrix
|
|
|
|
Matrix3<T> mul_rowcol(const Vector3<T> &v) const;
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// cross product
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> operator %(const Vector3<T> &v) const;
|
|
|
|
|
2020-05-07 18:23:36 -03:00
|
|
|
// cross product for Lua
|
|
|
|
Vector3<T> cross(const Vector3<T> &v) const {
|
|
|
|
return *this % v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale a vector3
|
2021-05-04 08:12:23 -03:00
|
|
|
Vector3<T> scale(const T v) const {
|
2020-05-07 18:23:36 -03:00
|
|
|
return *this * v;
|
|
|
|
}
|
|
|
|
|
2013-04-11 21:04:18 -03:00
|
|
|
// computes the angle between this vector and another vector
|
2021-05-04 08:12:23 -03:00
|
|
|
T angle(const Vector3<T> &v2) const;
|
2013-04-11 21:04:18 -03:00
|
|
|
|
|
|
|
// check if any elements are NAN
|
2019-07-24 20:25:06 -03:00
|
|
|
bool is_nan(void) const WARN_IF_UNUSED;
|
2013-04-11 21:04:18 -03:00
|
|
|
|
|
|
|
// check if any elements are infinity
|
2019-07-24 20:25:06 -03:00
|
|
|
bool is_inf(void) const WARN_IF_UNUSED;
|
2013-04-11 21:04:18 -03:00
|
|
|
|
2013-12-08 18:49:44 -04:00
|
|
|
// check if all elements are zero
|
2019-07-24 20:25:06 -03:00
|
|
|
bool is_zero(void) const WARN_IF_UNUSED {
|
|
|
|
return (fabsf(x) < FLT_EPSILON) &&
|
|
|
|
(fabsf(y) < FLT_EPSILON) &&
|
|
|
|
(fabsf(z) < FLT_EPSILON);
|
|
|
|
}
|
2015-05-02 02:07:45 -03:00
|
|
|
|
2013-12-08 18:49:44 -04:00
|
|
|
|
2013-04-11 21:04:18 -03:00
|
|
|
// rotate by a standard rotation
|
|
|
|
void rotate(enum Rotation rotation);
|
2015-04-21 00:37:35 -03:00
|
|
|
void rotate_inverse(enum Rotation rotation);
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2021-05-19 11:13:06 -03:00
|
|
|
// rotate vector by angle in radians in xy plane leaving z untouched
|
2021-05-04 08:12:23 -03:00
|
|
|
void rotate_xy(T rotation_rad);
|
2021-05-14 18:20:13 -03:00
|
|
|
|
2021-06-21 04:25:51 -03:00
|
|
|
// return xy components of a vector3 as a vector2.
|
|
|
|
// this returns a reference to the original vector3 xy data
|
|
|
|
const Vector2<T> &xy() const {
|
|
|
|
return *(const Vector2<T> *)this;
|
|
|
|
}
|
|
|
|
Vector2<T> &xy() {
|
|
|
|
return *(Vector2<T> *)this;
|
|
|
|
}
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// gets the length of this vector squared
|
2013-04-11 21:04:18 -03:00
|
|
|
T length_squared() const
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
return (T)(*this * *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the length of this vector
|
2021-05-04 08:12:23 -03:00
|
|
|
T length(void) const;
|
2012-08-17 03:20:14 -03:00
|
|
|
|
2021-03-11 08:04:12 -04:00
|
|
|
// limit xy component vector to a given length. returns true if vector was limited
|
2021-05-04 08:12:23 -03:00
|
|
|
bool limit_length_xy(T max_length);
|
2021-03-11 08:04:12 -04:00
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
// normalizes this vector
|
2013-04-11 21:04:18 -03:00
|
|
|
void normalize()
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
2013-04-11 21:04:18 -03:00
|
|
|
*this /= length();
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// zero the vector
|
2013-04-11 21:04:18 -03:00
|
|
|
void zero()
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
2013-10-29 03:43:38 -03:00
|
|
|
x = y = z = 0;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the normalized version of this vector
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> normalized() const
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
return *this/length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reflects this vector about n
|
2013-04-11 21:04:18 -03:00
|
|
|
void reflect(const Vector3<T> &n)
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
Vector3<T> orig(*this);
|
|
|
|
project(n);
|
2013-04-11 21:04:18 -03:00
|
|
|
*this = *this*2 - orig;
|
2012-08-17 03:20:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// projects this vector onto v
|
2013-04-11 21:04:18 -03:00
|
|
|
void project(const Vector3<T> &v)
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
*this= v * (*this * v)/(v*v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns this vector projected onto v
|
2013-04-11 21:04:18 -03:00
|
|
|
Vector3<T> projected(const Vector3<T> &v) const
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
return v * (*this * v)/(v*v);
|
|
|
|
}
|
|
|
|
|
2017-08-26 01:53:43 -03:00
|
|
|
// distance from the tip of this vector to another vector squared (so as to avoid the sqrt calculation)
|
2021-05-04 08:12:23 -03:00
|
|
|
T distance_squared(const Vector3<T> &v) const {
|
|
|
|
const T dist_x = x-v.x;
|
|
|
|
const T dist_y = y-v.y;
|
|
|
|
const T dist_z = z-v.z;
|
2017-08-26 01:53:43 -03:00
|
|
|
return (dist_x*dist_x + dist_y*dist_y + dist_z*dist_z);
|
|
|
|
}
|
|
|
|
|
2017-08-30 06:25:18 -03:00
|
|
|
// distance from the tip of this vector to a line segment specified by two vectors
|
2021-05-04 08:12:23 -03:00
|
|
|
T distance_to_segment(const Vector3<T> &seg_start, const Vector3<T> &seg_end) const;
|
2017-08-30 06:25:18 -03:00
|
|
|
|
2020-12-06 08:16:43 -04:00
|
|
|
// extrapolate position given bearing and pitch (in degrees) and distance
|
2021-05-04 08:12:23 -03:00
|
|
|
void offset_bearing(T bearing, T pitch, T distance);
|
2021-06-17 22:17:50 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
conversion to/from double
|
|
|
|
*/
|
2021-06-17 22:18:41 -03:00
|
|
|
Vector3<float> tofloat() const {
|
|
|
|
return Vector3<float>{float(x),float(y),float(z)};
|
|
|
|
}
|
|
|
|
Vector3<double> todouble() const {
|
|
|
|
return Vector3<double>{x,y,z};
|
|
|
|
}
|
2021-06-17 22:17:50 -03:00
|
|
|
|
2016-06-19 20:30:35 -03:00
|
|
|
// given a position p1 and a velocity v1 produce a vector
|
|
|
|
// perpendicular to v1 maximising distance from p1. If p1 is the
|
|
|
|
// zero vector the return from the function will always be the
|
|
|
|
// zero vector - that should be checked for.
|
|
|
|
static Vector3<T> perpendicular(const Vector3<T> &p1, const Vector3<T> &v1)
|
|
|
|
{
|
2018-09-11 05:57:33 -03:00
|
|
|
const T d = p1 * v1;
|
2016-06-19 20:30:35 -03:00
|
|
|
if (fabsf(d) < FLT_EPSILON) {
|
|
|
|
return p1;
|
|
|
|
}
|
2018-09-11 05:57:33 -03:00
|
|
|
const Vector3<T> parallel = (v1 * d) / v1.length_squared();
|
2016-06-19 20:30:35 -03:00
|
|
|
Vector3<T> perpendicular = p1 - parallel;
|
|
|
|
|
|
|
|
return perpendicular;
|
|
|
|
}
|
2012-03-09 22:44:36 -04:00
|
|
|
|
2020-06-08 04:00:27 -03:00
|
|
|
// Shortest distance between point(p) to a point contained in the line segment defined by w1,w2
|
2021-05-04 08:12:23 -03:00
|
|
|
static T closest_distance_between_line_and_point(const Vector3<T> &w1, const Vector3<T> &w2, const Vector3<T> &p);
|
2020-12-06 08:17:06 -04:00
|
|
|
|
|
|
|
// Point in the line segment defined by w1,w2 which is closest to point(p)
|
|
|
|
static Vector3<T> point_on_line_closest_to_other_point(const Vector3<T> &w1, const Vector3<T> &w2, const Vector3<T> &p);
|
|
|
|
|
|
|
|
// This implementation is borrowed from: http://geomalgorithms.com/a07-_distance.html
|
|
|
|
// INPUT: 4 points corresponding to start and end of two line segments
|
2021-05-04 08:12:23 -03:00
|
|
|
|
2021-05-26 16:03:22 -03:00
|
|
|
// OUTPUT: closest point on segment 2, from segment 1, gets passed on reference as "closest_point"
|
|
|
|
static void segment_to_segment_closest_point(const Vector3<T>& seg1_start, const Vector3<T>& seg1_end, const Vector3<T>& seg2_start, const Vector3<T>& seg2_end, Vector3<T>& closest_point);
|
|
|
|
|
|
|
|
// Returns true if the passed 3D segment passes through a plane defined by plane normal, and a point on the plane
|
|
|
|
static bool segment_plane_intersect(const Vector3<T>& seg_start, const Vector3<T>& seg_end, const Vector3<T>& plane_normal, const Vector3<T>& plane_point);
|
2010-09-08 05:21:46 -03:00
|
|
|
};
|
|
|
|
|
2012-08-17 03:20:14 -03:00
|
|
|
typedef Vector3<int16_t> Vector3i;
|
|
|
|
typedef Vector3<uint16_t> Vector3ui;
|
|
|
|
typedef Vector3<int32_t> Vector3l;
|
|
|
|
typedef Vector3<uint32_t> Vector3ul;
|
|
|
|
typedef Vector3<float> Vector3f;
|
2015-11-03 09:46:39 -04:00
|
|
|
typedef Vector3<double> Vector3d;
|