nvidia-texture-tools/src/nvmath/Vector.h

138 lines
4.1 KiB
C
Raw Normal View History

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
2020-04-05 19:20:35 +00:00
#include "nvcore/nvcore.h"
2007-04-17 08:49:19 +00:00
namespace nv
{
2020-04-05 19:20:35 +00:00
class Vector2
2010-05-27 23:19:24 +00:00
{
public:
typedef Vector2 const & Arg;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector2();
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
const float * ptr() const;
2007-04-17 08:49:19 +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);
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
union {
struct {
float x, y;
2010-05-27 23:19:24 +00:00
};
float component[2];
2010-05-27 23:19:24 +00:00
};
};
2007-04-17 08:49:19 +00:00
2020-04-05 19:20:35 +00:00
class Vector3
2010-05-27 23:19:24 +00:00
{
public:
typedef Vector3 const & Arg;
2007-04-17 08:49:19 +00:00
2010-05-27 23:19:24 +00:00
Vector3();
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)) {}
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
Vector2 xy() const;
2007-04-17 08:49:19 +00:00
const float * ptr() const;
2007-04-17 08:49:19 +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);
void operator*=(float s);
void operator/=(float s);
2010-05-27 23:19:24 +00:00
void operator*=(Vector3::Arg v);
void operator/=(Vector3::Arg v);
2010-05-27 23:19:24 +00:00
union {
struct {
float x, y, z;
2010-05-27 23:19:24 +00:00
};
float component[3];
2010-05-27 23:19:24 +00:00
};
};
2020-04-05 19:20:35 +00:00
class Vector4
2010-05-27 23:19:24 +00:00
{
public:
typedef Vector4 const & Arg;
Vector4();
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);
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);
Vector2 xy() const;
2011-09-27 17:48:46 +00:00
Vector2 zw() const;
Vector3 xyz() const;
2010-05-27 23:19:24 +00:00
const float * ptr() const;
2010-05-27 23:19:24 +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);
void operator*=(float s);
void operator/=(float s);
2010-05-27 23:19:24 +00:00
void operator*=(Vector4::Arg v);
void operator/=(Vector4::Arg v);
2010-05-27 23:19:24 +00:00
union {
struct {
float x, y, z, w;
2010-05-27 23:19:24 +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