2007-04-17 08:49:19 +00:00
// This code is in the public domain -- castanyo@yahoo.es
2010-05-27 23:19:24 +00:00
# pragma once
2007-04-17 08:49:19 +00:00
# ifndef NV_MATH_VECTOR_H
# define NV_MATH_VECTOR_H
2010-05-27 23:19:24 +00:00
# include "nvmath.h"
2007-04-17 08:49:19 +00:00
namespace nv
{
2010-05-27 23:19:24 +00:00
class NVMATH_CLASS Vector2
{
public :
typedef Vector2 const & Arg ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector2 ( ) ;
2012-01-02 08:49:13 +00:00
explicit Vector2 ( float f ) ;
Vector2 ( float x , float y ) ;
2010-05-27 23:19:24 +00:00
Vector2 ( Vector2 : : Arg v ) ;
2007-04-17 08:49:19 +00:00
2013-06-07 17:53:55 +00:00
//template <typename T> explicit Vector2(const T & v) : x(v.x), y(v.y) {}
//template <typename T> operator T() const { return T(x, y); }
2012-07-20 16:19:03 +00:00
2010-05-27 23:19:24 +00:00
const Vector2 & operator = ( Vector2 : : Arg v ) ;
2007-04-17 08:49:19 +00:00
2012-01-02 08:49:13 +00:00
const float * ptr ( ) const ;
2007-04-17 08:49:19 +00:00
2012-01-02 08:49:13 +00:00
void set ( float x , float y ) ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector2 operator - ( ) const ;
void operator + = ( Vector2 : : Arg v ) ;
void operator - = ( Vector2 : : Arg v ) ;
2012-01-02 08:49:13 +00:00
void operator * = ( float s ) ;
2010-05-27 23:19:24 +00:00
void operator * = ( Vector2 : : Arg v ) ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
friend bool operator = = ( Vector2 : : Arg a , Vector2 : : Arg b ) ;
friend bool operator ! = ( Vector2 : : Arg a , Vector2 : : Arg b ) ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
union {
struct {
2012-01-02 08:49:13 +00:00
float x , y ;
2010-05-27 23:19:24 +00:00
} ;
2012-01-02 08:49:13 +00:00
float component [ 2 ] ;
2010-05-27 23:19:24 +00:00
} ;
} ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
class NVMATH_CLASS Vector3
{
public :
typedef Vector3 const & Arg ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector3 ( ) ;
2012-01-02 08:49:13 +00:00
explicit Vector3 ( float x ) ;
2013-06-07 17:53:55 +00:00
//explicit Vector3(int x) : x(float(x)), y(float(x)), z(float(x)) {}
2012-01-02 08:49:13 +00:00
Vector3 ( float x , float y , float z ) ;
Vector3 ( Vector2 : : Arg v , float z ) ;
2010-05-27 23:19:24 +00:00
Vector3 ( Vector3 : : Arg v ) ;
2007-04-17 08:49:19 +00:00
2013-06-07 17:53:55 +00:00
//template <typename T> explicit Vector3(const T & v) : x(v.x), y(v.y), z(v.z) {}
//template <typename T> operator T() const { return T(x, y, z); }
2012-07-20 16:19:03 +00:00
2010-05-27 23:19:24 +00:00
const Vector3 & operator = ( Vector3 : : Arg v ) ;
2007-04-17 08:49:19 +00:00
2010-10-06 02:56:35 +00:00
Vector2 xy ( ) const ;
2007-04-17 08:49:19 +00:00
2012-01-02 08:49:13 +00:00
const float * ptr ( ) const ;
2007-04-17 08:49:19 +00:00
2012-01-02 08:49:13 +00:00
void set ( float x , float y , float z ) ;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector3 operator - ( ) const ;
void operator + = ( Vector3 : : Arg v ) ;
void operator - = ( Vector3 : : Arg v ) ;
2012-01-02 08:49:13 +00:00
void operator * = ( float s ) ;
void operator / = ( float s ) ;
2010-05-27 23:19:24 +00:00
void operator * = ( Vector3 : : Arg v ) ;
2013-12-07 02:17:08 +00:00
void operator / = ( Vector3 : : Arg v ) ;
2010-05-27 23:19:24 +00:00
friend bool operator = = ( Vector3 : : Arg a , Vector3 : : Arg b ) ;
friend bool operator ! = ( Vector3 : : Arg a , Vector3 : : Arg b ) ;
union {
struct {
2012-01-02 08:49:13 +00:00
float x , y , z ;
2010-05-27 23:19:24 +00:00
} ;
2012-01-02 08:49:13 +00:00
float component [ 3 ] ;
2010-05-27 23:19:24 +00:00
} ;
} ;
class NVMATH_CLASS Vector4
{
public :
typedef Vector4 const & Arg ;
Vector4 ( ) ;
2012-01-02 08:49:13 +00:00
explicit Vector4 ( float x ) ;
Vector4 ( float x , float y , float z , float w ) ;
Vector4 ( Vector2 : : Arg v , float z , float w ) ;
2011-09-27 17:48:46 +00:00
Vector4 ( Vector2 : : Arg v , Vector2 : : Arg u ) ;
2012-01-02 08:49:13 +00:00
Vector4 ( Vector3 : : Arg v , float w ) ;
2010-05-27 23:19:24 +00:00
Vector4 ( Vector4 : : Arg v ) ;
// Vector4(const Quaternion & v);
2013-06-07 17:53:55 +00:00
//template <typename T> explicit Vector4(const T & v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
//template <typename T> operator T() const { return T(x, y, z, w); }
2012-07-20 16:19:03 +00:00
2010-05-27 23:19:24 +00:00
const Vector4 & operator = ( Vector4 : : Arg v ) ;
2010-10-06 02:56:35 +00:00
Vector2 xy ( ) const ;
2011-09-27 17:48:46 +00:00
Vector2 zw ( ) const ;
2010-10-06 02:56:35 +00:00
Vector3 xyz ( ) const ;
2010-05-27 23:19:24 +00:00
2012-01-02 08:49:13 +00:00
const float * ptr ( ) const ;
2010-05-27 23:19:24 +00:00
2012-01-02 08:49:13 +00:00
void set ( float x , float y , float z , float w ) ;
2010-05-27 23:19:24 +00:00
Vector4 operator - ( ) const ;
void operator + = ( Vector4 : : Arg v ) ;
void operator - = ( Vector4 : : Arg v ) ;
2012-01-02 08:49:13 +00:00
void operator * = ( float s ) ;
2013-12-07 02:17:08 +00:00
void operator / = ( float s ) ;
2010-05-27 23:19:24 +00:00
void operator * = ( Vector4 : : Arg v ) ;
2013-12-07 02:17:08 +00:00
void operator / = ( Vector4 : : Arg v ) ;
2010-05-27 23:19:24 +00:00
friend bool operator = = ( Vector4 : : Arg a , Vector4 : : Arg b ) ;
friend bool operator ! = ( Vector4 : : Arg a , Vector4 : : Arg b ) ;
union {
struct {
2012-01-02 08:49:13 +00:00
float x , y , z , w ;
2010-05-27 23:19:24 +00:00
} ;
2012-01-02 08:49:13 +00:00
float component [ 4 ] ;
2010-05-27 23:19:24 +00:00
} ;
} ;
2007-04-17 08:49:19 +00:00
} // nv namespace
2013-06-07 17:53:55 +00:00
// If we had these functions, they would be ambiguous, the compiler would not know which one to pick:
//template <typename T> Vector2 to(const T & v) { return Vector2(v.x, v.y); }
//template <typename T> Vector3 to(const T & v) { return Vector3(v.x, v.y, v.z); }
//template <typename T> Vector4 to(const T & v) { return Vector4(v.x, v.y, v.z, v.z); }
// We could use a cast operator so that we could infer the expected type, but that doesn't work the same way in all compilers and produces horrible error messages.
// Instead we simply have explicit casts:
template < typename T > T to ( const nv : : Vector2 & v ) { NV_COMPILER_CHECK ( sizeof ( T ) = = sizeof ( nv : : Vector2 ) ) ; return T ( v . x , v . y ) ; }
template < typename T > T to ( const nv : : Vector3 & v ) { NV_COMPILER_CHECK ( sizeof ( T ) = = sizeof ( nv : : Vector3 ) ) ; return T ( v . x , v . y , v . z ) ; }
2014-11-04 17:49:29 +00:00
template < typename T > T to ( const nv : : Vector4 & v ) { NV_COMPILER_CHECK ( sizeof ( T ) = = sizeof ( nv : : Vector4 ) ) ; return T ( v . x , v . y , v . z , v . w ) ; }
2013-06-07 17:53:55 +00:00
2007-04-17 08:49:19 +00:00
# endif // NV_MATH_VECTOR_H