#pragma once /* #ifdef USE_BULLET #include "LinearMath\btVector3.h" #endif */ #include "shared.hpp" #ifndef _WIN32 #define sinf(x) sin(x) #define cosf(x) cos(x) #define acosf(x) acos(x) #endif namespace ace { template T acos(T n) { return -1; } template T cos(T n) { return -1; } template T sin(T n) { return -1; } template class vector3 { public: vector3() : _x(0), _y(0), _z(0) { } vector3(const T x_, const T y_, const T z_) : _x(x_), _y(y_), _z(z_) { } vector3(std::istream & read_) { // Constructor to read from a stream read_.read((char *)&_x, sizeof(T)); read_.read((char *)&_y, sizeof(T)); read_.read((char *)&_z, sizeof(T)); } vector3(const float *buffer) { _x = buffer[0]; _y = buffer[1]; _z = buffer[2]; } vector3 & operator= (const vector3& other) { _x = other.x(); _y = other.y(); _z = other.z(); return *this; } /*#ifdef _WIN32 && _DIRECTX vector3 & operator= (const XMFLOAT3& Float3) { _x = Float3.x; _y = Float3.y; _z = Float3.z; return *this; } #endif #ifdef USE_BULLET vector3 & operator= (const btVector3& bt_vec) { _x = bt_vec.x(); _y = bt_vec.y(); _z = bt_vec.z(); return *this; } #endif */ vector3 operator * (const T &val) const { return vector3(_x * val, _y * val, _z * val); } vector3 operator / (const T &val) const { T invVal = T(1) / val; return vector3(_x * invVal, _y * invVal, _z * invVal); } vector3 operator + (const vector3 &v) const { return vector3(_x + v.x(), _y + v.y(), _z + v.z()); } vector3 operator / (const vector3 &v) const { return vector3(_x / v.x(), _y / v.y(), _z / v.z()); } vector3 operator * (const vector3 &v) const { return vector3(_x * v.x(), _y * v.y(), _z * v.z()); } vector3 operator - (const vector3 &v) const { return vector3(_x - v.x(), _y - v.y(), _z - v.z()); } vector3 operator - () const { return vector3(-_x, -_y, -_z); } bool operator == (const vector3 &r) const { return (_x == r.x() && _y == r.y() && _z == r.z()); } bool operator > (const vector3 &r) const { throw 1; } bool operator < (const vector3 &r) const { throw 1; } bool operator <= (const vector3 &r) const { throw 1; } bool operator >= (const vector3 &r) const { throw 1; } T magnitude() const { return sqrt(_x * _x + _y * _y + _z * _z); } T dot(const vector3 &v) const { return (_x * v.x() + _y * v.y() + _z * v.z()); } T distance(const vector3 &v) const { vector3 dist = (*this - v); dist = dist * dist; return sqrt(dist.x() + dist.y() + dist.z()); } vector3 cross(const vector3 &v) const { return vector3(_y * v.z() - _z * v.y(), _z * v.x() - _x * v.z(), _x * v.y() - _y * v.x()); } vector3 normalize(void) const { return (*this / abs(magnitude())); }; bool zero_distance() { return ((_x == 0.0f && _y == 0.0f && _z == 0.0f) ? true : false ); } static float clamp(T x, T a, T b) { return x < a ? a : (x > b ? b : x); } static vector3 lerp(const vector3& A, const vector3& B, const T t) { return A*t + B*(1.f - t); } vector3 lerp(const vector3& B, const T t) { return vector3::lerp(*this, B, t); } static vector3 slerp(vector3 start, vector3 end, T percent) { T dot = start.dot(end); dot = vector3::clamp(dot, -1.0f, 1.0f); T theta = acos(dot) * percent; vector3 relative = end - start*dot; relative.normalize(); return ((start * cos(theta)) + (relative*sin(theta))); } vector3 slerp(const vector3& B, const T p) { return vector3::slerp(*this, B, p); } const T & x() const { return _x; } const T & y() const { return _y; } const T & z() const { return _z; } void x(const T val) { _x = val; } void y(const T val) { _y = val; } void z(const T val) { _z = val; } protected: T _x; T _y; T _z; }; template class vector { public: vector() : _values(std::vector(N)) { } std::vector _values; }; template class spatial { public: spatial() : position(vector3()), orientation(vector3()) { } explicit spatial(const vector3 & position_, const vector3 & orientation_) : position(position_), orientation(orientation_) {} spatial & operator= (const spatial & other) { position = other.position; orientation = other.orientation; return *this; } vector3 position; vector3 orientation; }; template class pair { public: pair() : _x(0), _y(0) { } pair(const T x_, const T y_, const T z_) : _x(x_), _y(y_) { } pair(const float *buffer) { _x = buffer[0]; _y = buffer[1]; } pair(std::istream & read_) { // Constructor to read from a stream read_.read((char *)&_x, sizeof(T)); read_.read((char *)&_y, sizeof(T)); } pair & operator= (const pair& other) { _x = other.x(); _y = other.y(); return *this; } const T & x() const { return _x; } const T & y() const { return _y; } void x(const T val) { _x = val; } void y(const T val) { _y = val; } protected: T _x; T _y; }; }