1
0
Fork 0

Initial commit

This commit is contained in:
Henrik Hautakoski 2015-12-22 17:43:51 +01:00
commit edfc5298e1
252 changed files with 93965 additions and 0 deletions

View file

@ -0,0 +1,65 @@
#ifndef SPECTRE_MATH_COLOR_H
#define SPECTRE_MATH_COLOR_H
#include "Vector3.h"
#include "Vector4.h"
// Basic structure representing a color in 32bit RGBA
struct Color
{
public :
unsigned char r, g, b, a;
// Predefined colors
static const Color Black;
static const Color White;
static const Color Red;
static const Color Green;
static const Color Blue;
static const Color Transparant;
Color(unsigned char red = 0, unsigned char green = 0, unsigned char blue = 0, unsigned char alpha = 255);
// Assignment
Color(const Color& color);
Color& operator=(const Color& color);
// Convert to RGB float.
// Each component is clamped to the range [0.0f, 1.0f]
Vector3f toRGBf() const;
// Convert to RGBA float.
// Each component is clamped to the range [0.0f, 1.0f]
Vector4f toRGBAf() const;
};
// ---------
// Compare
// ---------
bool operator ==(const Color& a, const Color& b);
bool operator !=(const Color& a, const Color& b);
// ------------
// Arithmetic
// ------------
Color operator +(const Color& a, const Color& b);
Color operator -(const Color& a, const Color& b);
Color operator /(const Color& a, const Color& b);
Color operator *(const Color& a, const Color& b);
Color& operator +=(Color& a, const Color& b);
Color& operator -=(Color& a, const Color& b);
Color& operator /=(Color& a, const Color& b);
Color& operator *=(Color& a, const Color& b);
// -------------------
// Scalar arithmetic
// -------------------
Color operator +(const Color& a, unsigned int s);
Color operator -(const Color& a, unsigned int s);
Color operator /(const Color& a, unsigned int s);
Color operator *(const Color& a, unsigned int s);
#endif /* SPECTRE_MATH_COLOR_H */

View file

@ -0,0 +1,52 @@
#ifndef SPECTRE_MATH_MATH_H
#define SPECTRE_MATH_MATH_H
#include "Vector3.h"
#include "Vector2.h"
#include "Matrix4.h"
namespace math
{
float rand(float min, float max);
// Convert degrees to radians.
float deg2rad(float deg);
// ------------------------------
// Logarithmic functions.
// ------------------------------
double log(double base, double value);
double log2(double value);
unsigned int nextPowerOfTwo(unsigned int value);
// ------------------------------
// Matrix Projection functions.
// ------------------------------
// Create a orthographic projection matrix.
Matrix4f orthoProjection(float left, float right,
float bottom, float top,
float zNear = -1.0f, float zFar = 1.0f);
// Create a 2D rotation matrix. (rotation around Z-axis)
Matrix4f rotation(float theta);
// Create a 2D translation matrix.
Matrix4f translate(const Vector2f& v);
// Create a 2D scale matrix.
Matrix4f scale(const Vector2f& f);
// Get translation part of a matrix.
Vector3f getTranslate(const Matrix4f matrix);
Vector3f getUpVector(const Matrix4f matrix);
Vector3f getForwardVector(const Matrix4f matrix);
};
#endif /* SPECTRE_MATH_MATH_H */

View file

@ -0,0 +1,79 @@
#ifndef SPECTRE_MATH_MATRIX3_H
#define SPECTRE_MATH_MATRIX3_H
// 3x3 matrix implementation.
#include <iostream>
#include <Spectre/Math/Vector2.h>
template<typename T>
struct Matrix3
{
/* Column-major order are used for the matrices here to be compatible with OpenGL.
_ _
| |
| 0 3 6 |
| 1 4 7 |
| 2 5 8 |
|_ _|
*/
union {
T m[3][3]; // format: [col][row]
T v[9];
};
static Matrix3<T> Identity;
static Matrix3<T> Zero;
inline Matrix3();
inline Matrix3( T a, T b, T c,
T d, T e, T f,
T g, T h, T i);
inline Matrix3(T *a);
// -----------------
// Copy & Assignment.
// -----------------
inline Matrix3<T>& operator=(const Matrix3<T>& mat);
// -----------------
// Compare
// -----------------
inline bool operator==(const Matrix3<T>& mat) const;
inline bool operator!=(const Matrix3<T>& mat) const;
// -----------------
// Arithmetic: Matrix-Matrix
// -----------------
inline Matrix3<T> operator*(const Matrix3<T>& mat) const;
inline Matrix3<T> operator*=(const Matrix3<T>& mat);
// Need scalar, vector arithmetic?
// -----------------
// Named operations.
// -----------------
// Determinant.
float det() const;
// Transpose matrix.
Matrix3<T> transpose() const;
};
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Matrix3<T>& v);
typedef Matrix3<float> Matrix3f;
typedef Matrix3<double> Matrix3d;
typedef Matrix3<int> Matrix3i;
typedef Matrix3<unsigned> Matrix3u;
#include "Matrix3.inl"
#endif /* SPECTRE_MATH_MATRIX3_H */

View file

@ -0,0 +1,129 @@
#include <string>
#include "Matrix3.h"
template <>
Matrix3f Matrix3f::Identity(
1, 0, 0,
0, 1, 0,
0, 0, 1
);
template <>
Matrix3f Matrix3f::Zero(
0, 0, 0,
0, 0, 0,
0, 0, 0
);
template<typename T>
inline Matrix3<T>::Matrix3()
{
v[0] = T(0); v[3] = T(0); v[6] = T(0);
v[1] = T(0); v[4] = T(0); v[7] = T(0);
v[2] = T(0); v[5] = T(0); v[8] = T(0);
}
template<typename T>
inline Matrix3<T>::Matrix3(T a, T b, T c, T d, T e, T f, T g, T h, T i)
{
v[0] = a; v[3] = d; v[6] = g;
v[1] = b; v[4] = e; v[7] = h;
v[2] = c; v[5] = f; v[8] = i;
}
template<typename T>
inline Matrix3<T>::Matrix3(T *a)
{
v[0] = a[0]; v[3] = a[3]; v[6] = a[6];
v[1] = a[1]; v[4] = a[4]; v[7] = a[7];
v[2] = a[2]; v[5] = a[5]; v[8] = a[8];
}
template<typename T>
inline Matrix3<T>& Matrix3<T>::operator=(const Matrix3<T>& mat)
{
if (this != &mat) {
v[0] = mat.v[0]; v[3] = mat.v[3]; v[6] = mat.v[6];
v[1] = mat.v[1]; v[4] = mat.v[4]; v[7] = mat.v[7];
v[2] = mat.v[2]; v[5] = mat.v[5]; v[8] = mat.v[8];
}
return *this;
}
template<typename T>
inline bool Matrix3<T>::operator==(const Matrix3<T>& mat) const
{
for(int i = 0; i < 9; i++) {
if (v[i] != mat.v[i]) {
return false;
}
}
return true;
}
template<typename T>
inline bool Matrix3<T>::operator!=(const Matrix3<T>& mat) const
{
return !(*this == mat);
}
template<typename T>
inline Matrix3<T> Matrix3<T>::operator*(const Matrix3<T>& mat) const
{
Matrix3<T> c;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
T s = T(0);
for(int k = 0; k < 3; k++) {
s += m[k][i] * mat.m[j][k];
}
c.m[j][i] = s;
}
}
return c;
}
template<typename T>
inline Matrix3<T> Matrix3<T>::operator*=(const Matrix3<T>& mat)
{
*this = *this * mat;
return *this;
}
template<typename T>
float Matrix3<T>::det() const
{
float det = 0.0f;
det += v[0] * v[4] * v[8];
det += v[3] * v[7] * v[2];
det += v[6] * v[1] * v[5];
det -= v[6] * v[4] * v[2];
det -= v[3] * v[1] * v[8];
det -= v[0] * v[7] * v[5];
return det;
}
template<typename T>
Matrix3<T> Matrix3<T>::transpose() const
{
Matrix3<T> t;
t.v[0] = v[0]; t.v[3] = v[1]; t.v[6] = v[2];
t.v[1] = v[3]; t.v[4] = v[4]; t.v[7] = v[5];
t.v[2] = v[6]; t.v[5] = v[7]; t.v[8] = v[8];
return t;
}
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Matrix3<T>& mat)
{
return s << mat.v[0] << " " << mat.v[3] << " " << mat.v[6] << std::endl
<< mat.v[1] << " " << mat.v[4] << " " << mat.v[7] << std::endl
<< mat.v[2] << " " << mat.v[5] << " " << mat.v[8] << std::endl;
}

View file

@ -0,0 +1,109 @@
#ifndef SPECTRE_MATH_MATRIX4_H
#define SPECTRE_MATH_MATRIX4_H
// 4x4 matrix implementation.
#include <string>
#include <iostream>
#include <Spectre/Math/Vector3.h>
#include <Spectre/Math/Vector2.h>
template<typename T>
struct Matrix4
{
// Column-major order are used for the matrices here to be compatible with OpenGL.
union {
T m[4][4]; // format: [col][row]
/* Indicies to use for accessing elements.
_ _
| |
| 0 4 8 12 |
| 1 5 9 13 |
| 2 6 10 14 |
| 3 7 11 15 |
|_ _|
*/
T e[16];
};
static const Matrix4<T> Identity;
inline Matrix4();
inline Matrix4(
T a00, T a01, T a02, T a03,
T a10, T a11, T a12, T a13,
T a20, T a21, T a22, T a23,
T a30, T a31, T a32, T a33);
inline Matrix4(T *a);
// -------------------
// Copy & Assignment
// -------------------
template <typename U>
inline Matrix4(const Matrix4<U>& mat);
inline Matrix4<T>& operator=(const Matrix4<T>& mat);
// ------------------
// Named operations
// ------------------
// Transpose matrix.
Matrix4<T> transpose() const;
std::string toString() const;
};
// -----------------
// Compare
// -----------------
template <typename T>
inline bool operator==(const Matrix4<T>& a, const Matrix4<T>& b);
template <typename T>
inline bool operator!=(const Matrix4<T>& a, const Matrix4<T>& b);
// --------------------------
// Arithmetic: Matrix-Matrix
// --------------------------
template <typename T>
inline Matrix4<T> operator*(const Matrix4<T>& a, const Matrix4<T>& b);
template <typename T>
inline Matrix4<T>& operator*=(Matrix4<T>& a, const Matrix4<T>& b);
// --------------------------
// Arithmetic: Matrix-Vector
// --------------------------
template <typename T>
inline Vector3<T> operator*(const Matrix4<T>& mat, const Vector3<T>& vec);
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Matrix4<T>& mat);
// -----------------------------
// Common specialization types
// -----------------------------
typedef Matrix4<float> Matrix4f;
typedef Matrix4<double> Matrix4d;
typedef Matrix4<int> Matrix4i;
typedef Matrix4<unsigned> Matrix4u;
typedef Matrix4<float> mat4f;
typedef Matrix4<double> mat4d;
typedef Matrix4<int> mat4i;
typedef Matrix4<unsigned> mat4u;
// ----------------
// Implementation
// ----------------
#include "Matrix4.inl"
#endif /* SPECTRE_MATH_MATRIX4_H */

View file

@ -0,0 +1,149 @@
#include <iomanip>
#include <sstream>
#include <string.h>
#include "Matrix4.h"
const Matrix4f Matrix4f::Identity(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
template<typename T>
inline Matrix4<T>::Matrix4()
{
}
template<typename T>
inline Matrix4<T>::Matrix4(
T a00, T a01, T a02, T a03,
T a10, T a11, T a12, T a13,
T a20, T a21, T a22, T a23,
T a30, T a31, T a32, T a33)
{
e[0] = a00; e[4] = a01; e[8] = a02; e[12] = a03;
e[1] = a10; e[5] = a11; e[9] = a12; e[13] = a13;
e[2] = a20; e[6] = a21; e[10] = a22; e[14] = a23;
e[3] = a30; e[7] = a31; e[11] = a32; e[15] = a33;
}
template<typename T>
inline Matrix4<T>::Matrix4(T *a)
{
// Note. array is passed as row-major
m[0][0] = a[0]; m[1][0] = a[1]; m[2][0] = a[2]; m[3][0] = a[3];
m[0][1] = a[4]; m[1][1] = a[5]; m[2][1] = a[6]; m[3][1] = a[7];
m[0][2] = a[8]; m[1][2] = a[9]; m[2][2] = a[10]; m[3][2] = a[11];
m[0][3] = a[12]; m[1][3] = a[13]; m[2][3] = a[14]; m[3][3] = a[15];
}
template<typename T>
inline Matrix4<T>& Matrix4<T>::operator=(const Matrix4<T>& mat)
{
if (*this != mat) {
memcpy(e, mat.e, sizeof(T) * 16);
}
return *this;
}
template<typename T>
Matrix4<T> Matrix4<T>::transpose() const
{
return Matrix4<T>(
m[0][0], m[1][0], m[2][0], m[3][0],
m[0][1], m[1][1], m[2][1], m[3][1],
m[0][2], m[1][2], m[2][2], m[3][2],
m[0][3], m[1][3], m[2][3], m[3][3]);
}
template <typename T>
std::string Matrix4<T>::toString() const
{
std::stringstream ss;
ss << std::setprecision(2)
<< "[ " << e[0 ] << "\t" << e[1 ] << "\t" << e[2 ] << "\t" << e[3 ] << " ]" << std::endl
<< "| " << e[4 ] << "\t" << e[5 ] << "\t" << e[6 ] << "\t" << e[7 ] << " |" << std::endl
<< "| " << e[8 ] << "\t" << e[9 ] << "\t" << e[10] << "\t" << e[11] << " |" << std::endl
<< "[ " << e[12] << "\t" << e[13] << "\t" << e[14] << "\t" << e[15] << " ]" << std::endl;
return ss.str();
}
template<typename T>
inline bool operator==(const Matrix4<T>& a, const Matrix4<T>& b)
{
return a.e[0 ] == b.e[0 ]
&& a.e[1 ] == b.e[1 ]
&& a.e[2 ] == b.e[2 ]
&& a.e[3 ] == b.e[3 ]
&& a.e[4 ] == b.e[4 ]
&& a.e[5 ] == b.e[5 ]
&& a.e[6 ] == b.e[6 ]
&& a.e[7 ] == b.e[7 ]
&& a.e[8 ] == b.e[8 ]
&& a.e[9 ] == b.e[9 ]
&& a.e[10] == b.e[10]
&& a.e[11] == b.e[11]
&& a.e[12] == b.e[12]
&& a.e[13] == b.e[13]
&& a.e[14] == b.e[14]
&& a.e[15] == b.e[15];
}
template<typename T>
inline bool operator!=(const Matrix4<T>& a, const Matrix4<T>& b)
{
return !(a == b);
}
// -----------------
// Arithmetic: Matrix-Matrix
// -----------------
template<typename T>
inline Matrix4<T> operator*(const Matrix4<T>& a, const Matrix4<T>& b)
{
Matrix4<T> c;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
T s = T(0);
for(int k = 0; k < 4; k++) {
s += a.m[k][i] * b.m[j][k];
}
c.m[j][i] = s;
}
}
return c;
}
template<typename T>
inline Matrix4<T>& operator*=(Matrix4<T>& a, const Matrix4<T>& b)
{
a = a * b;
return a;
}
// -----------------
// Arithmetic: Matrix-Vector
// -----------------
template <typename T>
inline Vector3<T> operator*(const Matrix4<T>& mat, const Vector3<T>& vec)
{
return Vextor3<T>(
mat.m[0][0] * vec.x + mat.m[1][0] * vec.y + mat[2][0] * vec.z,
mat.m[0][1] * vec.x + mat.m[1][1] * vec.y + mat[2][1] * vec.z,
mat.m[0][2] * vec.x + mat.m[1][2] * vec.y + mat[2][2] * vec.z,
);
}
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Matrix4<T>& mat)
{
return s << mat.toString();
}

View file

@ -0,0 +1,78 @@
#ifndef SPECTRE_MATH_TRANSFORM_H
#define SPECTRE_MATH_TRANSFORM_H
#include "Vector2.h"
#include "Matrix4.h"
// Class representing a transformation matrix.
class Transform
{
public :
// Constructors
Transform();
Transform(const Matrix4f& matrix);
Transform(float a00, float a01, float a02,
float a10, float a11, float a12,
float a20, float a21, float a22);
// Shorthand for applying translate/rotate/scale.
// Order of operations:
// 1. translate
// 2. rotate
// 3. scale
void set(Vector2f translate, float rotate, Vector2f scale);
void setMatrix(const Matrix4f& matrix);
// Reset the transformation (Identity matrix).
void reset();
// Axis.
Vector2f getUpVector() const;
Vector2f getRightVector() const;
// Translation
Transform& translate(float x, float y);
Transform& translate(Vector2f offset);
// Rotation
Transform& rotate(float theta);
// Scale
Transform& scale(float x, float y);
Transform& scale(Vector2f offset);
Transform& scale(float s);
Transform& multiply(const Transform& other);
Vector2f transformPoint(float x, float y) const;
Vector2f transformPoint(const Vector2f& v) const;
// Get the transformation as a 4x4 matrix.
Matrix4f& getMatrix();
const Matrix4f& getMatrix() const;
Matrix4f getRotationMatrix() const;
float* getRawMatrix();
const float* getRawMatrix() const;
protected :
Matrix4f m_matrix;
};
Transform operator*(const Transform& a, const Transform& b);
Transform& operator*=(Transform& a, const Transform& b);
Vector2f operator*(const Transform& t, const Vector2f& v);
#endif /* SPECTRE_MATH_TRANSFORM_H */

View file

@ -0,0 +1,195 @@
#ifndef SPECTRE_MATH_VECTOR2_H
#define SPECTRE_MATH_VECTOR2_H
#include <string>
#include <iostream>
template <typename T>
struct Vector2
{
union {
struct {
T x, y;
};
T v[2];
};
inline Vector2();
inline Vector2(T s);
inline Vector2(T x, T y);
template <typename U>
inline Vector2(const Vector2<U>& vec);
// -----------------
// Named operations.
// -----------------
inline float length() const;
inline Vector2<T>& normalize();
// Dot product
inline T dot(const Vector2<T>& vec) const;
inline Vector2<T>& reflect(const Vector2<T>& n);
std::string toString() const;
};
// ------------
// Compare
// ------------
template<typename T>
inline bool operator==(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline bool operator!=(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline bool operator<(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline bool operator<=(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline bool operator>(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline bool operator>=(const Vector2<T>& a, const Vector2<T>& b);
// ------------------
// Unary arithmetic
// ------------------
template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v);
template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v);
// ------------
// Arithmetic
// ------------
template<typename T>
inline Vector2<T> operator+(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T> operator-(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T> operator/(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T> operator*(const Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T>& operator+=(Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T>& operator-=(Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T>& operator/=(Vector2<T>& a, const Vector2<T>& b);
template<typename T>
inline Vector2<T>& operator*=(Vector2<T>& a, const Vector2<T>& b);
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v, T s);
template <typename T>
inline Vector2<T> operator+(T s, const Vector2<T>& v);
template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v,T s);
template <typename T>
inline Vector2<T> operator-(T s, const Vector2<T>& v);
template <typename T>
inline Vector2<T> operator/(const Vector2<T>& v, T s);
template <typename T>
inline Vector2<T> operator/(T s, const Vector2<T>& v);
template <typename T>
inline Vector2<T> operator*(const Vector2<T>& v, T s);
template <typename T>
inline Vector2<T> operator*(T s, const Vector2<T>& v);
template <typename T>
inline Vector2<T>& operator+=(Vector2<T>& v, T s);
template <typename T>
inline Vector2<T>& operator-=(Vector2<T>& v,T s);
template <typename T>
inline Vector2<T>& operator/=(Vector2<T>& v, T s);
template <typename T>
inline Vector2<T>& operator*=(Vector2<T>& v, T s);
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector2<T>& v, T s);
template <typename T>
inline bool operator<(T s, const Vector2<T>& v);
template <typename T>
inline bool operator<=(const Vector2<T>& v, T s);
template <typename T>
inline bool operator<=(T s, const Vector2<T>& v);
template <typename T>
inline bool operator>(const Vector2<T>& v, T s);
template <typename T>
inline bool operator>(T s, const Vector2<T>& v);
template <typename T>
inline bool operator>=(const Vector2<T>& v, T s);
template <typename T>
inline bool operator>=(T s, const Vector2<T>& v);
// -------------------
// Stream operations
// -------------------
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector2<T>& v);
// -----------------------------
// Common specialization types
// -----------------------------
typedef Vector2<float> Vector2f;
typedef Vector2<int> Vector2i;
typedef Vector2<unsigned> Vector2u;
typedef Vector2<unsigned char> Vector2b;
typedef Vector2<float> vec2f;
typedef Vector2<int> vec2i;
typedef Vector2<unsigned> vec2u;
typedef Vector2<unsigned char> vec2b;
#include "Vector2.inl"
#endif /* SPECTRE_MATH_VECTOR2_H */

View file

@ -0,0 +1,316 @@
#include <Spectre/Core/String.h>
#include <math.h>
#include "Vector2.h"
template <typename T>
Vector2<T>::Vector2()
{
x = T(0); y = T(0);
}
template <typename T>
Vector2<T>::Vector2(T x, T y)
{
v[0] = x; v[1] = y;
}
template <typename T>
Vector2<T>::Vector2(T s)
{
x = s; y = s;
}
template <typename T>
template <typename U>
inline Vector2<T>::Vector2(const Vector2<U>& vec)
{
x = T(vec.x); y = T(vec.y);
}
template <typename T>
inline float Vector2<T>::length() const
{
return std::sqrt((x * x) + (y * y));
}
template <typename T>
inline Vector2<T>& Vector2<T>::normalize()
{
float il = 1.0f / length();
x *= il; y *= il;
return *this;
}
template <typename T>
inline T Vector2<T>::dot(const Vector2<T>& vec) const
{
return x * vec.x + y * vec.y;
}
template <typename T>
inline Vector2<T>& Vector2<T>::reflect(const Vector2<T>& n)
{
*this = T(2) * n * dot(n) - *this;
return *this;
}
template <typename T>
std::string Vector2<T>::toString() const
{
return core::to_string(x) + ", " + core::to_string(y);
}
// ---------
// Compare
// ---------
template <typename T>
inline bool operator==(const Vector2<T>& a, const Vector2<T>& b)
{
return a.x == b.x && a.y == b.y;
}
template <typename T>
inline bool operator!=(const Vector2<T>& a, const Vector2<T>& b)
{
return !(a == b);
}
template<typename T>
inline bool operator<(const Vector2<T>& a, const Vector2<T>& b)
{
return a.x < b.x && a.y < b.y;
}
template<typename T>
inline bool operator<=(const Vector2<T>& a, const Vector2<T>& b)
{
return a.x =< b.x && a.y <= b.y;
}
template<typename T>
inline bool operator>(const Vector2<T>& a, const Vector2<T>& b)
{
return a.x > b.x && a.y > b.y;
}
template<typename T>
inline bool operator>=(const Vector2<T>& a, const Vector2<T>& b)
{
return a.x >= b.x && a.y >= b.y
}
// ------------------
// Unary arithmetic
// ------------------
template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v)
{
return v;
}
template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v)
{
return Vector2<T>(-v.x, -v.y);
}
// ------------
// Arithmetic
// ------------
template<typename T>
inline Vector2<T> operator+(const Vector2<T>& a, const Vector2<T>& b)
{
return Vector2<T>(a.x + b.x, a.y + b.y);
}
template <typename T>
inline Vector2<T> operator-(const Vector2<T>& a, const Vector2<T>& b)
{
return Vector2<T>(a.x - b.x, a.y - b.y);
}
template <typename T>
inline Vector2<T> operator/(const Vector2<T>& a, const Vector2<T>& b)
{
return Vector2<T>(a.x / b.x, a.y / b.y);
}
template <typename T>
inline Vector2<T> operator*(const Vector2<T>& a, const Vector2<T>& b)
{
return Vector2<T>(a.x * b.x, a.y * b.y);
}
template <typename T>
inline Vector2<T>& operator+=(Vector2<T>& a, const Vector2<T>& b)
{
a.x += b.x; a.y += b.y;
return a;
}
template <typename T>
inline Vector2<T>& operator-=(Vector2<T>& a, const Vector2<T>& b)
{
a.x -= b.x; a.y -= b.y;
return a;
}
template <typename T>
inline Vector2<T>& operator/=(Vector2<T>& a, const Vector2<T>& b)
{
a.x /= b.x; a.y /= b.y;
return a;
};
template <typename T>
inline Vector2<T>& operator*=(Vector2<T>& a, const Vector2<T>& b)
{
a.x *= b.x; a.y *= b.y;
return a;
}
// ------------
// Scalar Arithmetic
// ------------
template <typename T>
inline Vector2<T> operator+(const Vector2<T>& v, T s)
{
return Vector2<T>(v.x + s, v.y + s);
}
template <typename T>
inline Vector2<T> operator+(T s, const Vector2<T>& v)
{
return v + s;
}
template <typename T>
inline Vector2<T> operator-(const Vector2<T>& v, T s)
{
return Vector2<T>(v.x - s, v.y - s);
}
template <typename T>
inline Vector2<T> operator-(T s, const Vector2<T>& v)
{
return Vector2<T>(s - v.x, s - v.y);
}
template <typename T>
inline Vector2<T> operator/(const Vector2<T>& v, T s)
{
return Vector2<T>(v.x / s, v.y / s);
}
template <typename T>
inline Vector2<T> operator/(T s, const Vector2<T>& v)
{
return Vector2<T>(s / v.x, s / v.y);
}
template <typename T>
inline Vector2<T> operator*(const Vector2<T>& v, T s)
{
return Vector2<T>(v.x * s, v.y * s);
}
template <typename T>
inline Vector2<T> operator*(T s, const Vector2<T>& v)
{
return v * s;
}
template <typename T>
inline Vector2<T>& operator+=(Vector2<T>& v, T s)
{
v.x += s; v.y += s;
return v;
}
template <typename T>
inline Vector2<T>& operator-=(Vector2<T>& v,T s)
{
v.x -= s; v.y -= s;
return v;
}
template <typename T>
inline Vector2<T>& operator/=(Vector2<T>& v, T s)
{
v.x /= s; v.y /= s;
return v;
}
template <typename T>
inline Vector2<T>& operator*=(Vector2<T>& v, T s)
{
v.x *= s; v.y *= s;
return v;
}
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector2<T>& v, T s)
{
return v.x < s && v.y < s;
}
template <typename T>
inline bool operator<(T s, const Vector2<T>& v)
{
return s < v.x && s < v.y;
}
template <typename T>
inline bool operator<=(const Vector2<T> v, T s)
{
return v.x <= s && v.y <= s;
}
template <typename T>
inline bool operator<=(T s, const Vector2<T>& v)
{
return s <= v.x && s <= v.y;
}
template <typename T>
inline bool operator>(const Vector2<T> v, T s)
{
return x > s && y > s;
}
template <typename T>
inline bool operator>(T s, const Vector2<T>& v)
{
return s < v.x && s < v.y;
}
template <typename T>
inline bool operator>=(const Vector2<T> v, T s)
{
return v.x >= s && v.y >= s;
}
template <typename T>
inline bool operator>=(T s, const Vector2<T>& v)
{
return s >= v.x && s >= v.y;
}
// -------------------
// Stream operations
// -------------------
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector2<T>& v)
{
return s << "(" << v.x << "," << v.y << ")";
}

View file

@ -0,0 +1,172 @@
#ifndef SPECTRE_MATH_VECTOR3_H
#define SPECTRE_MATH_VECTOR3_H
#include "Vector2.h"
#include <iostream>
template <typename T>
struct Vector3
{
union {
struct {
T x, y, z;
};
T v[3];
};
inline Vector3();
inline Vector3(T s);
inline Vector3(T x, T y);
inline Vector3(T x, T y, T z);
template <typename U>
inline Vector3(const Vector3<U>& vec);
template <typename U>
inline Vector3(const Vector2<U>& vec);
// -----------------
// Named operations.
// -----------------
inline float length() const;
inline Vector3<T>& normalize();
inline Vector2<T> toVec2();
};
// ---------
// Compare
// ---------
template <typename T>
inline bool operator==(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline bool operator!=(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline bool operator<(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline bool operator<=(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline bool operator>(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline bool operator>=(const Vector3<T>& a, const Vector3<T>& b);
// ------------
// Arithmetic
// ------------
template <typename T>
inline Vector3<T> operator+(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T> operator-(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T> operator/(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T> operator*(const Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T>& operator+=(Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T>& operator-=(Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T>& operator/=(Vector3<T>& a, const Vector3<T>& b);
template <typename T>
inline Vector3<T>& operator*=(Vector3<T>& a, const Vector3<T>& b);
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector3<T> operator+(const Vector3<T>& v, T s);
template <typename T>
inline Vector3<T> operator+(T s, const Vector3<T>& v);
template <typename T>
inline Vector3<T> operator-(const Vector3<T>& v, T s);
template <typename T>
inline Vector3<T> operator-(T s, const Vector3<T>& v);
template <typename T>
inline Vector3<T> operator/(const Vector3<T>& v, T s);
template <typename T>
inline Vector3<T> operator/(T s, const Vector3<T>& v);
template <typename T>
inline Vector3<T> operator*(const Vector3<T>& v, T s);
template <typename T>
inline Vector3<T> operator*(T s, const Vector3<T>& v);
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector3<T>& v, T s);
template <typename T>
inline bool operator<(T s, const Vector3<T>& v);
template <typename T>
inline bool operator<=(const Vector3<T>& v, T s);
template <typename T>
inline bool operator<=(T s, const Vector3<T>& v);
template <typename T>
inline bool operator>(const Vector3<T>& v, T s);
template <typename T>
inline bool operator>(T s, const Vector3<T>& v);
template <typename T>
inline bool operator>=(const Vector3<T>& v, T s);
template <typename T>
inline bool operator>=(T s, const Vector3<T>& v);
// -------------------
// Stream operations
// -------------------
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector3<T>& v);
// -----------------------------
// Common specialization types
// -----------------------------
typedef Vector3<float> Vector3f;
typedef Vector3<int> Vector3i;
typedef Vector3<unsigned> Vector3u;
typedef Vector3<unsigned char> Vector3b;
typedef Vector3<float> vec3f;
typedef Vector3<int> vec3i;
typedef Vector3<unsigned> vec3u;
typedef Vector3<unsigned char> vec3b;
#include "Vector3.inl"
#endif /* SPECTRE_MATH_VECTOR3_H */

View file

@ -0,0 +1,267 @@
#include <math.h>
#include "Vector3.h"
template <typename T>
Vector3<T>::Vector3()
{
x = y = z = T(0);
}
template <typename T>
Vector3<T>::Vector3(T x, T y, T z)
{
v[0] = x; v[1] = y; v[2] = z;
}
template <typename T>
Vector3<T>::Vector3(T x, T y)
{
v[0] = x; v[1] = y; v[2] = T(0);
}
template <typename T>
Vector3<T>::Vector3(T s)
{
x = y = z = s;
}
template <typename T>
template <typename U>
inline Vector3<T>::Vector3(const Vector3<U>& vec)
{
x = T(vec.x); y = T(vec.y); z = T(vec.z);
}
template <typename T>
template <typename U>
inline Vector3<T>::Vector3(const Vector2<U>& vec)
{
x = T(vec.x); y = T(vec.y); z = T(0);
}
template <typename T>
inline float Vector3<T>::length() const
{
return std::sqrt((x * x) + (y * y) + (z * z));
}
template <typename T>
Vector2<T> Vector3<T>::toVec2()
{
return Vector2<T>(x, y);
}
template <typename T>
inline Vector3<T>& Vector3<T>::normalize()
{
float il = 1.0f / length();
x *= il; y *= il; z *= il;
return *this;
}
// ---------
// Compare
// ---------
template <typename T>
inline bool operator==(const Vector3<T>& a, const Vector3<T>& b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
template <typename T>
inline bool operator!=(const Vector3<T>& a, const Vector3<T>& b)
{
return !(a == b);
}
template <typename T>
inline bool operator<(const Vector3<T>& a, const Vector3<T>& b)
{
return a.x < b.x && a.y < b.y && a.z < b.z;
}
template <typename T>
inline bool operator<=(const Vector3<T>& a, const Vector3<T>& b)
{
return !(a > b);
}
template <typename T>
inline bool operator>(const Vector3<T>& a, const Vector3<T>& b)
{
return a.x > b.x && a.y > b.y && a.z > b.z;
}
template <typename T>
inline bool operator>=(const Vector3<T>& a, const Vector3<T>& b)
{
return !(a < b);
}
// ------------
// Arithmetic
// ------------
template <typename T>
inline Vector3<T> operator+(const Vector3<T>& a, const Vector3<T>& b)
{
return Vector3<T>(a.x + b.x, a.y + b.y, a.z + b.z);
}
template <typename T>
inline Vector3<T> operator-(const Vector3<T>& a, const Vector3<T>& b)
{
return Vector3<T>(a.x - b.x, a.y - b.y, a.z - b.z);
}
template <typename T>
inline Vector3<T> operator/(const Vector3<T>& a, const Vector3<T>& b)
{
return Vector3<T>(a.x / b.x, a.y / b.y, a.z / b.z);
}
template <typename T>
inline Vector3<T> operator*(const Vector3<T>& a, const Vector3<T>& b)
{
return Vector3<T>(a.x * b.x, a.y * b.y, a.z * b.z);
}
template <typename T>
inline Vector3<T>& operator+=(Vector3<T>& a, const Vector3<T>& b)
{
a.x += b.x; a.y += b.y; a.z += b.z;
return a;
}
template <typename T>
inline Vector3<T>& operator-=(Vector3<T>& a, const Vector3<T>& b)
{
a.x -= b.x; a.y -= b.y; a.z -= b.z;
return a;
}
template <typename T>
inline Vector3<T>& operator/=(Vector3<T>& a, const Vector3<T>& b)
{
a.x /= b.x; a.y /= b.y; a.z /= b.z;
return a;
};
template <typename T>
inline Vector3<T>& operator*=(Vector3<T>& a, const Vector3<T>& b)
{
a.x *= b.x; a.y *= b.y; a.z *= b.z;
return a;
}
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector3<T> operator+(const Vector3<T>& v, T s)
{
return Vector3<T>(v.x + s, v.y + s, v.z + s);
}
template <typename T>
inline Vector3<T> operator+(T s, const Vector3<T>& v)
{
return v + s;
}
template <typename T>
inline Vector3<T> operator-(const Vector3<T>& v, T s)
{
return Vector3<T>(v.x - s, v.y - s, v.z - s);
}
template <typename T>
inline Vector3<T> operator-(T s, const Vector3<T>& v)
{
return Vector3<T>(s - v.x, s - v.y, s - v.z);
}
template <typename T>
inline Vector3<T> operator/(const Vector3<T>& v, T s)
{
return Vector3<T>(v.x / s, v.y / s, v.z / s);
}
template <typename T>
inline Vector3<T> operator/(T s, const Vector3<T>& v)
{
return Vector3<T>(s / v.x, s / v.y, s / v.z);
}
template <typename T>
inline Vector3<T> operator*(const Vector3<T>& v, T s)
{
return Vector3<T>(v.x * s, v.y * s, v.z * s);
}
template <typename T>
inline Vector3<T> operator*(T s, const Vector3<T>& v)
{
return v * s;
}
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector3<T>& v, T s)
{
return v.x < s && v.y < s && v.z < s;
}
template <typename T>
inline bool operator<(T s, const Vector3<T>& v)
{
return s < v.x && s < v.y && s < v.z;
}
template <typename T>
inline bool operator<=(const Vector3<T>& v, T s)
{
return v.x <= s && v.y <= s && v.z <= s;
}
template <typename T>
inline bool operator<=(T s, const Vector3<T>& v)
{
return s <= v.x && s <= v.y && s <= v.z;
}
template <typename T>
inline bool operator>(const Vector3<T>& v, T s)
{
return v.x > s && v.y > s && v.z > s;
}
template <typename T>
inline bool operator>(T s, const Vector3<T>& v)
{
return s > v.x && s > v.y && s > v.z;
}
template <typename T>
inline bool operator>=(const Vector3<T>& v, T s)
{
return v.x >= s && v.y >= s && v.z >= s;
}
template <typename T>
inline bool operator>=(T s, const Vector3<T>& v)
{
return s >= v.x && s >= v.y && s >= v.z;
}
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector3<T>& v)
{
return s << "(" << v.x << "," << v.y << "," << v.z << ")";
}

View file

@ -0,0 +1,158 @@
#ifndef SPECTRE_MATH_VECTOR4_H
#define SPECTRE_MATH_VECTOR4_H
#include <iostream>
template <typename T>
struct Vector4
{
union {
struct {
T x, y, z, w;
};
T v[4];
};
inline Vector4();
inline Vector4(T s);
inline Vector4(T x, T y, T z);
inline Vector4(T x, T y, T z, T w);
template <typename U>
inline Vector4(const Vector4<U>& vec);
// Named operations
inline float length() const;
inline Vector4<T>& normalize();
};
// ---------
// Compare
// ---------
template <typename T>
inline bool operator==(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline bool operator!=(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline bool operator<(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline bool operator<=(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline bool operator>(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline bool operator>=(const Vector4<T>& a, const Vector4<T>& b);
// ------------
// Arithmetic
// ------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T>& operator+=(Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T>& operator-=(Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T>& operator/=(Vector4<T>& a, const Vector4<T>& b);
template <typename T>
inline Vector4<T>& operator*=(Vector4<T>& a, const Vector4<T>& b);
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& v, T s);
template <typename T>
inline Vector4<T> operator+(T s, const Vector4<T>& v);
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& v, T s);
template <typename T>
inline Vector4<T> operator-(T s, const Vector4<T>& v);
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& v, T s);
template <typename T>
inline Vector4<T> operator/(T s, const Vector4<T>& v);
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& v, T s);
template <typename T>
inline Vector4<T> operator*(T s, const Vector4<T>& v);
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector4<T>& v, T s);
template <typename T>
inline bool operator<(T s, const Vector4<T>& v);
template <typename T>
inline bool operator<=(const Vector4<T>& v, T s);
template <typename T>
inline bool operator<=(T s, const Vector4<T>& v);
template <typename T>
inline bool operator>(const Vector4<T>& v, T s);
template <typename T>
inline bool operator>(T s, const Vector4<T>& v);
template <typename T>
inline bool operator>=(const Vector4<T>& v, T s);
template <typename T>
inline bool operator>=(T s, const Vector4<T>& v);
// Stream
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector4<T>& v);
typedef Vector4<float> Vector4f;
typedef Vector4<int> Vector4i;
typedef Vector4<unsigned> Vector4u;
typedef Vector4<unsigned char> Vector4b;
typedef Vector4<float> vec4f;
typedef Vector4<int> vec4i;
typedef Vector4<unsigned> vec4u;
typedef Vector4<unsigned char> vec4b;
#include "Vector4.inl"
#endif /* SPECTRE_MATH_VECTOR4_H */

View file

@ -0,0 +1,254 @@
#include <math.h>
#include "Vector4.h"
template <typename T>
Vector4<T>::Vector4()
{
x = y = z = w = T(0);
}
template <typename T>
Vector4<T>::Vector4(T s)
{
x = y = z = w = s;
}
template <typename T>
Vector4<T>::Vector4(T _x, T _y, T _z)
{
x = _x; y = _y; z = _z; w = T(1);
}
template <typename T>
Vector4<T>::Vector4(T _x, T _y, T _z, T _w)
{
x = _x; y = _y; z = _z; w = _w;
}
template <typename T>
template <typename U>
Vector4<T>::Vector4(const Vector4<U>& vec)
{
x = T(vec.x); y = T(vec.y); z = T(vec.z); w = T(vec.w);
}
template <typename T>
float Vector4<T>::length() const
{
return std::sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
template <typename T>
Vector4<T>& Vector4<T>::normalize()
{
float il = 1.0f / length();
x *= il; y *= il; z *= il; w *= il;
return *this;
}
// ---------
// Compare
// ---------
template <typename T>
bool operator==(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
}
template <typename T>
bool operator!=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a == b);
}
template <typename T>
bool operator<(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w;
}
template <typename T>
bool operator<=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a > b);
}
template <typename T>
bool operator>(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w;
}
template <typename T>
bool operator>=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a < b);
}
// ------------
// Arithmetic
// ------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
}
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
template <typename T>
inline Vector4<T>& operator+=(Vector4<T>& a, const Vector4<T>& b)
{
a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator-=(Vector4<T>& a, const Vector4<T>& b)
{
a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator/=(Vector4<T>& a, const Vector4<T>& b)
{
a.x /= b.x; a.y /= b.y; a.z /= b.z; a.w /= b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator*=(Vector4<T>& a, const Vector4<T>& b)
{
a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w;
return a;
}
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x + s, v.y + s, v.z + s, v.w + s);
}
template <typename T>
inline Vector4<T> operator+(T s, const Vector4<T>& v)
{
return v + s;
}
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x - s, v.y - s, v.z - s, v.w - s);
}
template <typename T>
inline Vector4<T> operator-(T s, const Vector4<T>& v)
{
return Vector4<T>(s - v.x, s - v.y, s - v.z, s - v.w);
}
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x / s, v.y / s, v.z / s, v.w / s);
}
template <typename T>
inline Vector4<T> operator/(T s, const Vector4<T>& v)
{
return Vector4<T>(s / v.x, s / v.y, s / v.z, s / v.w);
}
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x * s, v.y * s, v.z * s, v.w * s);
}
template <typename T>
inline Vector4<T> operator*(T s, const Vector4<T>& v)
{
return v * s;
}
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector4<T>& v, T s)
{
return v.x < s && v.y < s && v.z < s && v.w < s;
}
template <typename T>
inline bool operator<(T s, const Vector4<T>& v)
{
return s < v.x && s < v.y && s < v.z && s < v.w;
}
template <typename T>
inline bool operator<=(const Vector4<T>& v, T s)
{
return v.x <= s && v.y <= s && v.z <= s && v.w <= s;
}
template <typename T>
inline bool operator<=(T s, const Vector4<T>& v)
{
return s <= v.x && s <= v.y && s <= v.z && s <= v.w;
}
template <typename T>
inline bool operator>(const Vector4<T>& v, T s)
{
return v.x > s && v.y > s && v.z > s && v.w > s;
}
template <typename T>
inline bool operator>(T s, const Vector4<T>& v)
{
return s > v.x && s > v.y && s > v.z && s > v.w;
}
template <typename T>
inline bool operator>=(const Vector4<T>& v, T s)
{
return v.x >= s && v.y >= s && v.z >= s && v.w >= s;
}
template <typename T>
inline bool operator>=(T s, const Vector4<T>& v)
{
return s >= v.x && s >= v.y && s >= v.z && s >= v.w;
}
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector4<T>& v)
{
return s << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")";
}