restructured some files

This commit is contained in:
aap
2015-08-27 16:45:54 +02:00
parent c99e045e8d
commit fca3327ae2
44 changed files with 22 additions and 238 deletions

93
tools/gl/math/conversion.h Executable file
View File

@ -0,0 +1,93 @@
#ifndef MATH_CONVERSION_H
#define MATH_CONVERSION_H
Vec3::Vec3(const Vec4 &v)
: x(v.x), y(v.y), z(v.z)
{
}
Vec3::Vec3(const Quat &q)
: x(q.x), y(q.y), z(q.z)
{
}
Vec4::Vec4(const Vec3 &v, float w)
: x(v.x), y(v.y), z(v.z), w(w)
{
}
Vec4::Vec4(const Quat &q)
: x(q.x), y(q.y), z(q.z), w(q.w)
{
}
Quat::Quat(const Vec3 &v)
: x(v.x), y(v.y), z(v.z)
{
}
Quat::Quat(float w, const Vec3 &v)
: w(w), x(v.x), y(v.y), z(v.z)
{
}
Quat::Quat(const Vec4 &v)
: w(v.w), x(v.x), y(v.y), z(v.z)
{
}
Mat3::Mat3(const Mat4 &m)
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
this->e[i][j] = m.e[i][j];
}
Mat4::Mat4(const Mat3 &m)
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
this->e[i][j] = m.e[i][j];
this->e[0][3] = 0.0f;
this->e[1][3] = 0.0f;
this->e[2][3] = 0.0f;
this->e[3][3] = 1.0f;
this->e[3][2] = 0.0f;
this->e[3][1] = 0.0f;
this->e[3][0] = 0.0f;
}
Mat4
Mat4::rotation(const Quat &v)
{
Mat4 m(1.0f);
m.e[0][0] = v.w*v.w + v.x*v.x - v.y*v.y - v.z*v.z;
m.e[1][0] = 2*v.x*v.y - 2*v.w*v.z;
m.e[2][0] = 2*v.w*v.y + 2*v.x*v.z;
m.e[0][1] = 2*v.w*v.z + 2*v.x*v.y;
m.e[1][1] = v.w*v.w - v.x*v.x + v.y*v.y - v.z*v.z;
m.e[2][1] = 2*v.y*v.z - 2*v.w*v.x;
m.e[0][2] = 2*v.x*v.z - 2*v.w*v.y;
m.e[1][2] = 2*v.w*v.x + 2*v.y*v.z;
m.e[2][2] = v.w*v.w - v.x*v.x - v.y*v.y + v.z*v.z;
return m;
}
Mat3
Mat3::rotation(const Quat &v)
{
Mat3 m(1.0f);
m.e[0][0] = v.w*v.w + v.x*v.x - v.y*v.y - v.z*v.z;
m.e[1][0] = 2*v.x*v.y - 2*v.w*v.z;
m.e[2][0] = 2*v.w*v.y + 2*v.x*v.z;
m.e[0][1] = 2*v.w*v.z + 2*v.x*v.y;
m.e[1][1] = v.w*v.w - v.x*v.x + v.y*v.y - v.z*v.z;
m.e[2][1] = 2*v.y*v.z - 2*v.w*v.x;
m.e[0][2] = 2*v.x*v.z - 2*v.w*v.y;
m.e[1][2] = 2*v.w*v.x + 2*v.y*v.z;
m.e[2][2] = v.w*v.w - v.x*v.x - v.y*v.y + v.z*v.z;
return m;
}
#endif

45
tools/gl/math/dquat.h Executable file
View File

@ -0,0 +1,45 @@
#ifndef MATH_DQUAT_H
#define MATH_DQUAT_H
#include <iostream>
#include <cmath>
class Vec3;
class Vec4;
class DQuat {
public:
Quat q1, q2;
DQuat(void);
DQuat(const Quat &q1, const Quat &q2);
DQuat operator-(void) const;
DQuat operator+(const DQuat &rhs) const;
DQuat operator-(const DQuat &rhs) const;
DQuat operator*(const DQuat &rhs) const;
DQuat operator*(float rhs) const;
DQuat operator/(float rhs) const;
DQuat &operator+=(const DQuat &rhs);
DQuat &operator-=(const DQuat &rhs);
DQuat &operator*=(const DQuat &rhs);
DQuat &operator*=(float rhs);
DQuat &operator/=(float rhs);
bool operator==(const DQuat &rhs) const;
bool operator!=(const DQuat &rhs) const;
// DQuat inv(void) const;
DQuat K(void) const; /* conjugate */
// DQuat S(void) const; /* scalar */
// DQuat V(void) const; /* vector */
// float T(void) const; /* tensor */
// float N(void) const; /* norm = tensor^2 */
// DQuat U(void) const; /* versor */
// DQuat wedge(const Quat &rhs) const;
// float inner(const Quat &rhs) const;
// DQuat slerp(const Quat &rhs, float t) const;
void print(std::ostream &of) const;
};
#endif

43
tools/gl/math/mat3.h Executable file
View File

@ -0,0 +1,43 @@
#ifndef MATH_MATRIX3_H
#define MATH_MATRIX3_H
#include <iostream>
#include <cmath>
class Mat4;
class Mat3 {
public:
union {
float e[3][3];
float cr[9];
};
Mat3(void);
Mat3(float f);
Mat3(float *f);
Mat3(float e00, float e10, float e20,
float e01, float e11, float e21,
float e02, float e12, float e22);
Mat3(const Mat4 &m);
float *ptr(void);
static Mat3 rotation(float theta, const Vec3 &v);
static Mat3 rotation(const Quat &v);
static Mat3 scale(const Vec3 &v);
Mat3 transpose(void) const;
Mat3 operator+(const Mat3 &rhs) const;
Mat3 operator-(const Mat3 &rhs) const;
Mat3 operator*(float rhs) const;
Mat3 operator/(float rhs) const;
Mat3 operator*(const Mat3 &rhs) const;
Vec3 operator*(const Vec3 &rhs) const;
Mat3 &operator+=(const Mat3 &rhs);
Mat3 &operator-=(const Mat3 &rhs);
Mat3 &operator*=(float rhs);
Mat3 &operator/=(float rhs);
Mat3 &operator*=(const Mat3 &rhs);
void print(std::ostream &of) const;
};
#endif

52
tools/gl/math/mat4.h Executable file
View File

@ -0,0 +1,52 @@
#ifndef MATH_MATRIX4_H
#define MATH_MATRIX4_H
#include <iostream>
#include <cmath>
class Mat3;
class Mat4 {
public:
union {
float e[4][4];
float cr[16];
};
Mat4(void);
Mat4(float f);
Mat4(float *f);
Mat4(float e00, float e10, float e20, float e30,
float e01, float e11, float e21, float e31,
float e02, float e12, float e22, float e32,
float e03, float e13, float e23, float e33);
Mat4(const Mat3 &m);
float *ptr(void);
static Mat4 perspective(float fov, float aspect, float n, float f);
static Mat4 frustum(float l, float r, float b, float t,
float n, float f);
static Mat4 ortho(float l, float r, float b, float t,
float n, float f);
static Mat4 lookat(const Vec3 &pos, const Vec3 &target, const Vec3 &up);
static Mat4 translation(const Vec3 &v);
static Mat4 rotation(float theta, const Vec3 &v);
static Mat4 rotation(const Quat &q);
static Mat4 transrot(const DQuat &q);
static Mat4 scale(const Vec3 &v);
Mat4 transpose(void) const;
Mat4 operator+(const Mat4 &rhs) const;
Mat4 operator-(const Mat4 &rhs) const;
Mat4 operator*(float rhs) const;
Mat4 operator/(float rhs) const;
Mat4 operator*(const Mat4 &rhs) const;
Vec4 operator*(const Vec4 &rhs) const;
Mat4 &operator+=(const Mat4 &rhs);
Mat4 &operator-=(const Mat4 &rhs);
Mat4 &operator*=(float rhs);
Mat4 &operator/=(float rhs);
Mat4 &operator*=(const Mat4 &rhs);
void print(std::ostream &of) const;
};
#endif

21
tools/gl/math/math.h Executable file
View File

@ -0,0 +1,21 @@
#ifndef MATH_H
#define MATH_H
#include "vec3.h"
#include "vec4.h"
#include "quat.h"
#include "dquat.h"
#include "mat3.h"
#include "mat4.h"
std::ostream &operator<<(std::ostream& of, const Vec3 &v);
std::ostream &operator<<(std::ostream& of, const Vec4 &v);
std::ostream &operator<<(std::ostream& of, const Quat &v);
std::ostream &operator<<(std::ostream& of, const DQuat &v);
std::ostream &operator<<(std::ostream& of, const Mat3 &v);
std::ostream &operator<<(std::ostream& of, const Mat4 &v);
#define PI 3.14159265359f
#endif

54
tools/gl/math/quat.h Executable file
View File

@ -0,0 +1,54 @@
#ifndef MATH_QUAT_H
#define MATH_QUAT_H
#include <iostream>
#include <cmath>
class Vec3;
class Vec4;
class Mat3;
/* Hamilton style */
class Quat {
public:
float w, x, y, z;
Quat(void);
Quat(float w);
Quat(float x, float y, float z);
Quat(float w, float x, float y, float z);
Quat(float w, const Vec3 &v);
Quat(const Vec3 &v);
Quat(const Vec4 &v);
Quat(const Mat3 &m);
float *ptr(void);
Quat operator-(void) const;
Quat operator+(const Quat &rhs) const;
Quat operator-(const Quat &rhs) const;
Quat operator*(const Quat &rhs) const;
Quat operator*(float rhs) const;
Quat operator/(float rhs) const;
Quat &operator+=(const Quat &rhs);
Quat &operator-=(const Quat &rhs);
Quat &operator*=(const Quat &rhs);
Quat &operator*=(float rhs);
Quat &operator/=(float rhs);
bool operator==(const Quat &rhs) const;
bool operator!=(const Quat &rhs) const;
Quat inv(void) const;
Quat K(void) const; /* conjugate */
Quat S(void) const; /* scalar */
Quat V(void) const; /* vector */
float T(void) const; /* tensor */
float N(void) const; /* norm = tensor^2 */
Quat U(void) const; /* versor */
Quat wedge(const Quat &rhs) const;
float inner(const Quat &rhs) const;
Quat lerp(const Quat &rhs, float t) const;
Quat slerp(const Quat &rhs, float t) const;
void print(std::ostream &of) const;
};
#endif

40
tools/gl/math/vec3.h Executable file
View File

@ -0,0 +1,40 @@
#ifndef MATH_VECTOR3_H
#define MATH_VECTOR3_H
#include <iostream>
#include <cmath>
class Vec4;
class Quat;
class Vec3 {
public:
float x, y, z;
Vec3(void);
Vec3(float x, float y, float z);
Vec3(float *v);
Vec3(const Vec4 &v);
Vec3(const Quat &q);
float *ptr(void);
Vec3 operator-(void) const;
Vec3 operator+(const Vec3 &rhs) const;
Vec3 operator-(const Vec3 &rhs) const;
Vec3 operator*(float rhs) const;
Vec3 operator/(float rhs) const;
Vec3 &operator+=(const Vec3 &rhs);
Vec3 &operator-=(const Vec3 &rhs);
Vec3 &operator*=(float rhs);
Vec3 &operator/=(float rhs);
bool operator==(const Vec3 &rhs) const;
bool operator!=(const Vec3 &rhs) const;
float norm(void) const;
float normsq(void) const;
Vec3 normalized(void) const;
float dot(const Vec3 &rhs) const;
Vec3 cross(const Vec3 &rhs) const;
void print(std::ostream &of) const;
};
#endif

39
tools/gl/math/vec4.h Executable file
View File

@ -0,0 +1,39 @@
#ifndef MATH_VECTOR4_H
#define MATH_VECTOR4_H
#include <iostream>
#include <cmath>
class Vec3;
class Quat;
class Vec4 {
public:
float x, y, z, w;
Vec4(void);
Vec4(float x, float y, float z, float w);
Vec4(float *v);
Vec4(const Vec3 &v, float w = 0.0f);
Vec4(const Quat &w);
float *ptr(void);
Vec4 operator-(void) const;
Vec4 operator+(const Vec4 &rhs) const;
Vec4 operator-(const Vec4 &rhs) const;
Vec4 operator*(float rhs) const;
Vec4 operator/(float rhs) const;
Vec4 &operator+=(const Vec4 &rhs);
Vec4 &operator-=(const Vec4 &rhs);
Vec4 &operator*=(float rhs);
Vec4 &operator/=(float rhs);
bool operator==(const Vec4 &rhs) const;
bool operator!=(const Vec4 &rhs) const;
float norm(void) const;
float normsq(void) const;
Vec4 normalized(void) const;
float dot(const Vec4 &rhs) const;
void print(std::ostream &of) const;
};
#endif