Added Basis.h and stub Quat.h

pull/7/head
Karroffel 2017-03-03 03:14:34 +01:00
parent 0cd08a93ec
commit 5d543692eb
2 changed files with 815 additions and 0 deletions

644
include/godot/core/Basis.h Normal file
View File

@ -0,0 +1,644 @@
#ifndef BASIS_H
#define BASIS_H
#include "Vector3.h"
#include <algorithm>
typedef float real_t; // @Todo move this to a global Godot.h
#define CMP_EPSILON 0.00001 // @Todo move this somewhere more global
#define CMP_EPSILON2 (CMP_EPSILON*CMP_EPSILON) // @Todo same as above
#define Math_PI 3.14159265358979323846 // I feel like I'm talking to myself
namespace godot {
class Quat;
class Basis {
public:
Vector3 elements[3];
Basis(const Quat& p_quat); // euler
Basis(const Vector3& p_euler); // euler
Basis(const Vector3& p_axis, real_t p_phi);
Basis(const Vector3& row0, const Vector3& row1, const Vector3& row2)
{
elements[0]=row0;
elements[1]=row1;
elements[2]=row2;
}
Basis(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
set(xx, xy, xz, yx, yy, yz, zx, zy, zz);
}
Basis() {
elements[0][0]=1;
elements[0][1]=0;
elements[0][2]=0;
elements[1][0]=0;
elements[1][1]=1;
elements[1][2]=0;
elements[2][0]=0;
elements[2][1]=0;
elements[2][2]=1;
}
const Vector3& operator[](int axis) const {
return elements[axis];
}
Vector3& operator[](int axis) {
return elements[axis];
}
#define cofac(row1,col1, row2, col2)\
(elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
void invert()
{
real_t co[3]={
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
};
real_t det = elements[0][0] * co[0]+
elements[0][1] * co[1]+
elements[0][2] * co[2];
if ( det != 0 ) {
// WTF
__builtin_trap(); // WTF WTF WTF
// I shouldn't do this
// @Todo @Fixme @Todo @Todo
}
real_t s = 1.0/det;
set( co[0]*s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
co[1]*s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
co[2]*s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s );
}
#undef cofac
void transpose()
{
std::swap(elements[0][1],elements[1][0]);
std::swap(elements[0][2],elements[2][0]);
std::swap(elements[1][2],elements[2][1]);
}
Basis inverse() const
{
Basis b = *this;
b.invert();
return b;
}
Basis transposed() const
{
Basis b = *this;
b.transpose();
return b;
}
real_t determinant() const
{
return elements[0][0]*(elements[1][1]*elements[2][2] - elements[2][1]*elements[1][2]) -
elements[1][0]*(elements[0][1]*elements[2][2] - elements[2][1]*elements[0][2]) +
elements[2][0]*(elements[0][1]*elements[1][2] - elements[1][1]*elements[0][2]);
}
Vector3 get_axis(int p_axis) const {
// get actual basis axis (elements is transposed for performance)
return Vector3( elements[0][p_axis], elements[1][p_axis], elements[2][p_axis] );
}
void set_axis(int p_axis, const Vector3& p_value) {
// get actual basis axis (elements is transposed for performance)
elements[0][p_axis]=p_value.x;
elements[1][p_axis]=p_value.y;
elements[2][p_axis]=p_value.z;
}
void rotate(const Vector3& p_axis, real_t p_phi)
{
*this = rotated(p_axis, p_phi);
}
Basis rotated(const Vector3& p_axis, real_t p_phi) const
{
return Basis(p_axis, p_phi) * (*this);
}
Vector3 get_rotation() const; // need?!
void scale( const Vector3& p_scale )
{
elements[0][0]*=p_scale.x;
elements[0][1]*=p_scale.x;
elements[0][2]*=p_scale.x;
elements[1][0]*=p_scale.y;
elements[1][1]*=p_scale.y;
elements[1][2]*=p_scale.y;
elements[2][0]*=p_scale.z;
elements[2][1]*=p_scale.z;
elements[2][2]*=p_scale.z;
}
Basis scaled( const Vector3& p_scale ) const
{
Basis b = *this;
b.scale(p_scale);
return b;
}
Vector3 get_scale() const
{
// We are assuming M = R.S, and performing a polar decomposition to extract R and S.
// FIXME: We eventually need a proper polar decomposition.
// As a cheap workaround until then, to ensure that R is a proper rotation matrix with determinant +1
// (such that it can be represented by a Quat or Euler angles), we absorb the sign flip into the scaling matrix.
// As such, it works in conjuction with get_rotation().
real_t det_sign = determinant() > 0 ? 1 : -1;
return det_sign*Vector3(
Vector3(elements[0][0],elements[1][0],elements[2][0]).length(),
Vector3(elements[0][1],elements[1][1],elements[2][1]).length(),
Vector3(elements[0][2],elements[1][2],elements[2][2]).length()
);
}
Vector3 get_euler() const
{
// Euler angles in XYZ convention.
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
//
// rot = cy*cz -cy*sz sy
// cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
// -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
Vector3 euler;
if (is_rotation() == false)
return euler;
euler.y = ::asin(elements[0][2]);
if ( euler.y < Math_PI*0.5) {
if ( euler.y > -Math_PI*0.5) {
euler.x = ::atan2(-elements[1][2],elements[2][2]);
euler.z = ::atan2(-elements[0][1],elements[0][0]);
} else {
real_t r = ::atan2(elements[1][0],elements[1][1]);
euler.z = 0.0;
euler.x = euler.z - r;
}
} else {
real_t r = ::atan2(elements[0][1],elements[1][1]);
euler.z = 0;
euler.x = r - euler.z;
}
return euler;
}
void set_euler(const Vector3& p_euler)
{
real_t c, s;
c = ::cos(p_euler.x);
s = ::sin(p_euler.x);
Basis xmat(1.0,0.0,0.0,0.0,c,-s,0.0,s,c);
c = ::cos(p_euler.y);
s = ::sin(p_euler.y);
Basis ymat(c,0.0,s,0.0,1.0,0.0,-s,0.0,c);
c = ::cos(p_euler.z);
s = ::sin(p_euler.z);
Basis zmat(c,-s,0.0,s,c,0.0,0.0,0.0,1.0);
//optimizer will optimize away all this anyway
*this = xmat*(ymat*zmat);
}
// transposed dot products
real_t tdotx(const Vector3& v) const {
return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2];
}
real_t tdoty(const Vector3& v) const {
return elements[0][1] * v[0] + elements[1][1] * v[1] + elements[2][1] * v[2];
}
real_t tdotz(const Vector3& v) const {
return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2];
}
bool isequal_approx(const Basis& a, const Basis& b) const; // need?
bool operator==(const Basis& p_matrix) const
{
for (int i=0;i<3;i++) {
for (int j=0;j<3;j++) {
if (elements[i][j] != p_matrix.elements[i][j])
return false;
}
}
return true;
}
bool operator!=(const Basis& p_matrix) const
{
return (!(*this==p_matrix));
}
Vector3 xform(const Vector3& p_vector) const {
return Vector3(
elements[0].dot(p_vector),
elements[1].dot(p_vector),
elements[2].dot(p_vector)
);
}
Vector3 xform_inv(const Vector3& p_vector) const {
return Vector3(
(elements[0][0]*p_vector.x ) + ( elements[1][0]*p_vector.y ) + ( elements[2][0]*p_vector.z ),
(elements[0][1]*p_vector.x ) + ( elements[1][1]*p_vector.y ) + ( elements[2][1]*p_vector.z ),
(elements[0][2]*p_vector.x ) + ( elements[1][2]*p_vector.y ) + ( elements[2][2]*p_vector.z )
);
}
void operator*=(const Basis& p_matrix)
{
set(
p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]));
}
Basis operator*(const Basis& p_matrix) const
{
return Basis(
p_matrix.tdotx(elements[0]), p_matrix.tdoty(elements[0]), p_matrix.tdotz(elements[0]),
p_matrix.tdotx(elements[1]), p_matrix.tdoty(elements[1]), p_matrix.tdotz(elements[1]),
p_matrix.tdotx(elements[2]), p_matrix.tdoty(elements[2]), p_matrix.tdotz(elements[2]) );
}
void operator+=(const Basis& p_matrix) {
elements[0] += p_matrix.elements[0];
elements[1] += p_matrix.elements[1];
elements[2] += p_matrix.elements[2];
}
Basis operator+(const Basis& p_matrix) const {
Basis ret(*this);
ret += p_matrix;
return ret;
}
void operator-=(const Basis& p_matrix) {
elements[0] -= p_matrix.elements[0];
elements[1] -= p_matrix.elements[1];
elements[2] -= p_matrix.elements[2];
}
Basis operator-(const Basis& p_matrix) const {
Basis ret(*this);
ret -= p_matrix;
return ret;
}
void operator*=(real_t p_val) {
elements[0]*=p_val;
elements[1]*=p_val;
elements[2]*=p_val;
}
Basis operator*(real_t p_val) const {
Basis ret(*this);
ret *= p_val;
return ret;
}
int get_orthogonal_index() const; // down below
void set_orthogonal_index(int p_index); // down below
bool is_orthogonal() const; // need?
bool is_rotation() const; // need?
operator String() const;
void get_axis_and_angle(Vector3 &r_axis,real_t& r_angle) const;
/* create / set */
void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) {
elements[0][0]=xx;
elements[0][1]=xy;
elements[0][2]=xz;
elements[1][0]=yx;
elements[1][1]=yy;
elements[1][2]=yz;
elements[2][0]=zx;
elements[2][1]=zy;
elements[2][2]=zz;
}
Vector3 get_column(int i) const {
return Vector3(elements[0][i],elements[1][i],elements[2][i]);
}
Vector3 get_row(int i) const {
return Vector3(elements[i][0],elements[i][1],elements[i][2]);
}
Vector3 get_main_diagonal() const {
return Vector3(elements[0][0],elements[1][1],elements[2][2]);
}
void set_row(int i, const Vector3& p_row) {
elements[i][0]=p_row.x;
elements[i][1]=p_row.y;
elements[i][2]=p_row.z;
}
Basis transpose_xform(const Basis& m) const
{
return Basis(
elements[0].x * m[0].x + elements[1].x * m[1].x + elements[2].x * m[2].x,
elements[0].x * m[0].y + elements[1].x * m[1].y + elements[2].x * m[2].y,
elements[0].x * m[0].z + elements[1].x * m[1].z + elements[2].x * m[2].z,
elements[0].y * m[0].x + elements[1].y * m[1].x + elements[2].y * m[2].x,
elements[0].y * m[0].y + elements[1].y * m[1].y + elements[2].y * m[2].y,
elements[0].y * m[0].z + elements[1].y * m[1].z + elements[2].y * m[2].z,
elements[0].z * m[0].x + elements[1].z * m[1].x + elements[2].z * m[2].x,
elements[0].z * m[0].y + elements[1].z * m[1].y + elements[2].z * m[2].y,
elements[0].z * m[0].z + elements[1].z * m[1].z + elements[2].z * m[2].z);
}
void orthonormalize()
{
if (determinant() != 0) {
// not this crap again
__builtin_trap(); // WTF WTF WTF
// somebody please complain some day
// so I can fix this
// need propert error reporting here.
}
// Gram-Schmidt Process
Vector3 x=get_axis(0);
Vector3 y=get_axis(1);
Vector3 z=get_axis(2);
x.normalize();
y = (y-x*(x.dot(y)));
y.normalize();
z = (z-x*(x.dot(z))-y*(y.dot(z)));
z.normalize();
set_axis(0,x);
set_axis(1,y);
set_axis(2,z);
}
Basis orthonormalized() const
{
Basis b = *this;
b.orthonormalize();
return b;
}
bool is_symmetric() const
{
if (::fabs(elements[0][1] - elements[1][0]) > CMP_EPSILON)
return false;
if (::fabs(elements[0][2] - elements[2][0]) > CMP_EPSILON)
return false;
if (::fabs(elements[1][2] - elements[2][1]) > CMP_EPSILON)
return false;
return true;
}
Basis diagonalize()
{
// I love copy paste
if (!is_symmetric())
return Basis();
const int ite_max = 1024;
real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
int ite = 0;
Basis acc_rot;
while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max ) {
real_t el01_2 = elements[0][1] * elements[0][1];
real_t el02_2 = elements[0][2] * elements[0][2];
real_t el12_2 = elements[1][2] * elements[1][2];
// Find the pivot element
int i, j;
if (el01_2 > el02_2) {
if (el12_2 > el01_2) {
i = 1;
j = 2;
} else {
i = 0;
j = 1;
}
} else {
if (el12_2 > el02_2) {
i = 1;
j = 2;
} else {
i = 0;
j = 2;
}
}
// Compute the rotation angle
real_t angle;
if (::fabs(elements[j][j] - elements[i][i]) < CMP_EPSILON) {
angle = Math_PI / 4;
} else {
angle = 0.5 * ::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
}
// Compute the rotation matrix
Basis rot;
rot.elements[i][i] = rot.elements[j][j] = ::cos(angle);
rot.elements[i][j] = - (rot.elements[j][i] = ::sin(angle));
// Update the off matrix norm
off_matrix_norm_2 -= elements[i][j] * elements[i][j];
// Apply the rotation
*this = rot * *this * rot.transposed();
acc_rot = rot * acc_rot;
}
return acc_rot;
}
operator Quat() const;
};
static const Basis _ortho_bases[24]={
Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
};
int Basis::get_orthogonal_index() const
{
//could be sped up if i come up with a way
Basis orth=*this;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
real_t v = orth[i][j];
if (v>0.5)
v=1.0;
else if (v<-0.5)
v=-1.0;
else
v=0;
orth[i][j]=v;
}
}
for(int i=0;i<24;i++) {
if (_ortho_bases[i]==orth)
return i;
}
return 0;
}
void Basis::set_orthogonal_index(int p_index){
//there only exist 24 orthogonal bases in r3
if (p_index >= 24) {
__builtin_trap(); // kiiiiill me
// I don't want to do shady stuff like that
// @Todo WTF WTF
}
*this=_ortho_bases[p_index];
}
Basis::Basis(const Vector3& p_euler) {
set_euler( p_euler );
}
}
#include "Quat.h"
namespace godot {
Basis::Basis(const Quat& p_quat) {
real_t d = p_quat.length_squared();
real_t s = 2.0 / d;
real_t xs = p_quat.x * s, ys = p_quat.y * s, zs = p_quat.z * s;
real_t wx = p_quat.w * xs, wy = p_quat.w * ys, wz = p_quat.w * zs;
real_t xx = p_quat.x * xs, xy = p_quat.x * ys, xz = p_quat.x * zs;
real_t yy = p_quat.y * ys, yz = p_quat.y * zs, zz = p_quat.z * zs;
set( 1.0 - (yy + zz), xy - wz, xz + wy,
xy + wz, 1.0 - (xx + zz), yz - wx,
xz - wy, yz + wx, 1.0 - (xx + yy)) ;
}
Basis::Basis(const Vector3& p_axis, real_t p_phi) {
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
Vector3 axis_sq(p_axis.x*p_axis.x,p_axis.y*p_axis.y,p_axis.z*p_axis.z);
real_t cosine= ::cos(p_phi);
real_t sine= ::sin(p_phi);
elements[0][0] = axis_sq.x + cosine * ( 1.0 - axis_sq.x );
elements[0][1] = p_axis.x * p_axis.y * ( 1.0 - cosine ) - p_axis.z * sine;
elements[0][2] = p_axis.z * p_axis.x * ( 1.0 - cosine ) + p_axis.y * sine;
elements[1][0] = p_axis.x * p_axis.y * ( 1.0 - cosine ) + p_axis.z * sine;
elements[1][1] = axis_sq.y + cosine * ( 1.0 - axis_sq.y );
elements[1][2] = p_axis.y * p_axis.z * ( 1.0 - cosine ) - p_axis.x * sine;
elements[2][0] = p_axis.z * p_axis.x * ( 1.0 - cosine ) - p_axis.y * sine;
elements[2][1] = p_axis.y * p_axis.z * ( 1.0 - cosine ) + p_axis.x * sine;
elements[2][2] = axis_sq.z + cosine * ( 1.0 - axis_sq.z );
}
}
#endif // BASIS_H

171
include/godot/core/Quat.h Normal file
View File

@ -0,0 +1,171 @@
#ifndef QUAT_H
#define QUAT_H
#include <cmath>
#include "Vector3.h"
namespace godot {
#define CMP_EPSILON 0.00001
typedef float real_t;
class Quat{
public:
real_t x,y,z,w;
real_t length_squared() const;
real_t length() const;
void normalize();
Quat normalized() const;
Quat inverse() const;
real_t dot(const Quat& q) const;
void set_euler(const Vector3& p_euler);
Vector3 get_euler() const;
Quat slerp(const Quat& q, const real_t& t) const;
Quat slerpni(const Quat& q, const real_t& t) const;
Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
r_angle = 2 * ::acos(w);
r_axis.x = x / ::sqrt(1-w*w);
r_axis.y = y / ::sqrt(1-w*w);
r_axis.z = z / ::sqrt(1-w*w);
}
void operator*=(const Quat& q);
Quat operator*(const Quat& q) const;
Quat operator*(const Vector3& v) const
{
return Quat( w * v.x + y * v.z - z * v.y,
w * v.y + z * v.x - x * v.z,
w * v.z + x * v.y - y * v.x,
-x * v.x - y * v.y - z * v.z);
}
Vector3 xform(const Vector3& v) const {
Quat q = *this * v;
q *= this->inverse();
return Vector3(q.x,q.y,q.z);
}
void operator+=(const Quat& q);
void operator-=(const Quat& q);
void operator*=(const real_t& s);
void operator/=(const real_t& s);
Quat operator+(const Quat& q2) const;
Quat operator-(const Quat& q2) const;
Quat operator-() const;
Quat operator*(const real_t& s) const;
Quat operator/(const real_t& s) const;
bool operator==(const Quat& p_quat) const;
bool operator!=(const Quat& p_quat) const;
operator String() const;
inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
x=p_x; y=p_y; z=p_z; w=p_w;
}
inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
x=p_x; y=p_y; z=p_z; w=p_w;
}
Quat(const Vector3& axis, const real_t& angle);
Quat(const Vector3& v0, const Vector3& v1) // shortest arc
{
Vector3 c = v0.cross(v1);
real_t d = v0.dot(v1);
if (d < -1.0 + CMP_EPSILON) {
x=0;
y=1;
z=0;
w=0;
} else {
real_t s = ::sqrt((1.0 + d) * 2.0);
real_t rs = 1.0 / s;
x=c.x*rs;
y=c.y*rs;
z=c.z*rs;
w=s * 0.5;
}
}
inline Quat() {x=y=z=0; w=1; }
};
real_t Quat::dot(const Quat& q) const {
return x * q.x+y * q.y+z * q.z+w * q.w;
}
real_t Quat::length_squared() const {
return dot(*this);
}
void Quat::operator+=(const Quat& q) {
x += q.x; y += q.y; z += q.z; w += q.w;
}
void Quat::operator-=(const Quat& q) {
x -= q.x; y -= q.y; z -= q.z; w -= q.w;
}
void Quat::operator*=(const real_t& s) {
x *= s; y *= s; z *= s; w *= s;
}
void Quat::operator/=(const real_t& s) {
*this *= 1.0 / s;
}
Quat Quat::operator+(const Quat& q2) const {
const Quat& q1 = *this;
return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
}
Quat Quat::operator-(const Quat& q2) const {
const Quat& q1 = *this;
return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
}
Quat Quat::operator-() const {
const Quat& q2 = *this;
return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
}
Quat Quat::operator*(const real_t& s) const {
return Quat(x * s, y * s, z * s, w * s);
}
Quat Quat::operator/(const real_t& s) const {
return *this * (1.0 / s);
}
bool Quat::operator==(const Quat& p_quat) const {
return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
}
bool Quat::operator!=(const Quat& p_quat) const {
return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
}
}
#endif // QUAT_H