#ifndef __DARKRL__VECTOR_HPP__ #define __DARKRL__VECTOR_HPP__ #include #include #include #include #include "Math.hpp" template struct Vector2 { Vector2() : x( 0 ), y( 0 ) {} Vector2( T v ) : x( v ), y( v ) {} Vector2( T _x, T _y ) : x( _x ), y( _y ) {} bool operator==( const Vector2& rhs ) const { return x == rhs.x && y == rhs.y; } bool operator!=( const Vector2& rhs ) const { return !( *this == rhs ); } Vector2& operator+=( const Vector2& rhs ) { x += rhs.x; y += rhs.y; return *this; } Vector2& operator-=( const Vector2& rhs ) { x -= rhs.x; y -= rhs.y; return *this; } Vector2& operator*=( const Vector2& rhs ) { x *= rhs.x; y *= rhs.y; return *this; } T x, y; }; template Vector2 operator+( const Vector2& lhs, const Vector2& rhs ) { return Vector2( lhs.x + rhs.x, lhs.y + rhs.y ); } template Vector2 operator-( const Vector2& lhs, const Vector2& rhs ) { return Vector2( lhs.x - rhs.x, lhs.y - rhs.y ); } template Vector2 operator*( const Vector2& lhs, const float& rhs ) { return Vector2( lhs.x * rhs, lhs.y * rhs ); } template Vector2 operator/( const Vector2& lhs, const T& rhs ) { return Vector2( lhs.x / rhs, lhs.y / rhs ); } typedef Vector2 v2i; typedef Vector2 v2f; template struct Vector3 { Vector3() : x( 0 ), y( 0 ), z( 0 ) {} Vector3( T v ) : x( v ), y( v ), z( v ) {} Vector3( T _x, T _y, T _z ) : x( _x ), y( _y ), z( _z ) {} template Vector3( const Vector3& v ) : x( T( v.x ) ), y( T( v.y ) ), z( T( v.z ) ) {} T Luminance() const { return T( x * 0.3f + y * 0.59f + z * 0.11f ); } void Clamp() { x = std::min( T(1), std::max( T(0), x ) ); y = std::min( T(1), std::max( T(0), y ) ); z = std::min( T(1), std::max( T(0), z ) ); } bool operator==( const Vector3& rhs ) const { return x == rhs.x && y == rhs.y && z == rhs.z; } bool operator!=( const Vector2& rhs ) const { return !( *this == rhs ); } T& operator[]( unsigned int idx ) { assert( idx < 3 ); return ((T*)this)[idx]; } const T& operator[]( unsigned int idx ) const { assert( idx < 3 ); return ((T*)this)[idx]; } Vector3 operator+=( const Vector3& rhs ) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; } Vector3 operator*=( const Vector3& rhs ) { x *= rhs.x; y *= rhs.y; z *= rhs.z; return *this; } Vector3 operator*=( const float& rhs ) { x *= rhs; y *= rhs; z *= rhs; return *this; } T x, y, z; T padding; }; template Vector3 operator+( const Vector3& lhs, const Vector3& rhs ) { return Vector3( lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z ); } template Vector3 operator-( const Vector3& lhs, const Vector3& rhs ) { return Vector3( lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z ); } template Vector3 operator*( const Vector3& lhs, const Vector3& rhs ) { return Vector3( lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z ); } template Vector3 operator*( const Vector3& lhs, const float& rhs ) { return Vector3( T( lhs.x * rhs ), T( lhs.y * rhs ), T( lhs.z * rhs ) ); } template Vector3 operator/( const Vector3& lhs, const T& rhs ) { return Vector3( lhs.x / rhs, lhs.y / rhs, lhs.z / rhs ); } template bool operator<( const Vector3& lhs, const Vector3& rhs ) { return lhs.Luminance() < rhs.Luminance(); } typedef Vector3 v3i; typedef Vector3 v3f; typedef Vector3 v3b; static inline v3b v3f_to_v3b( const v3f& v ) { return v3b( uint8_t( std::min( 1.f, v.x ) * 255 ), uint8_t( std::min( 1.f, v.y ) * 255 ), uint8_t( std::min( 1.f, v.z ) * 255 ) ); } template Vector3 Mix( const Vector3& v1, const Vector3& v2, float amount ) { return v1 + ( v2 - v1 ) * amount; } template<> inline v3b Mix( const v3b& v1, const v3b& v2, float amount ) { return v3b( v3f( v1 ) + ( v3f( v2 ) - v3f( v1 ) ) * amount ); } template Vector3 Desaturate( const Vector3& v ) { T l = v.Luminance(); return Vector3( l, l, l ); } template Vector3 Desaturate( const Vector3& v, float mul ) { T l = T( v.Luminance() * mul ); return Vector3( l, l, l ); } template Vector3 pow( const Vector3& base, float exponent ) { return Vector3( pow( base.x, exponent ), pow( base.y, exponent ), pow( base.z, exponent ) ); } template Vector3 sRGB2linear( const Vector3& v ) { return Vector3( sRGB2linear( v.x ), sRGB2linear( v.y ), sRGB2linear( v.z ) ); } template Vector3 linear2sRGB( const Vector3& v ) { return Vector3( linear2sRGB( v.x ), linear2sRGB( v.y ), linear2sRGB( v.z ) ); } #endif