2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cmath>
|
2014-03-16 21:29:22 +00:00
|
|
|
|
2017-08-31 15:13:18 +00:00
|
|
|
#include "Common/Common.h"
|
2017-08-31 15:24:34 +00:00
|
|
|
#include "Core/Util/AudioFormat.h" // for clamp_u8
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Math/fast/fast_matrix.h"
|
2014-03-22 08:26:28 +00:00
|
|
|
|
2014-03-16 21:29:22 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
#include <emmintrin.h>
|
2018-01-31 07:38:11 +00:00
|
|
|
#if _M_SSE >= 0x401
|
|
|
|
#include <smmintrin.h>
|
|
|
|
#endif
|
2014-03-16 21:29:22 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-29 21:32:55 +00:00
|
|
|
namespace Math3D {
|
|
|
|
|
2013-10-05 20:05:32 +00:00
|
|
|
// Helper for Vec classes to clamp values.
|
|
|
|
template<typename T>
|
|
|
|
inline static T VecClamp(const T &v, const T &low, const T &high)
|
|
|
|
{
|
|
|
|
if (v > high)
|
|
|
|
return high;
|
|
|
|
if (v < low)
|
|
|
|
return low;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
class Vec2
|
|
|
|
{
|
|
|
|
public:
|
2014-03-16 21:29:22 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x,y;
|
|
|
|
};
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i ivec;
|
|
|
|
__m128 vec;
|
|
|
|
#endif
|
2013-07-25 12:58:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
T* AsArray() { return &x; }
|
2013-11-17 22:10:57 +00:00
|
|
|
const T* AsArray() const { return &x; }
|
2013-07-25 12:58:40 +00:00
|
|
|
|
|
|
|
Vec2() {}
|
|
|
|
Vec2(const T a[2]) : x(a[0]), y(a[1]) {}
|
|
|
|
Vec2(const T& _x, const T& _y) : x(_x), y(_y) {}
|
2014-03-16 21:29:22 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
Vec2(const __m128 &_vec) : vec(_vec) {}
|
|
|
|
Vec2(const __m128i &_ivec) : ivec(_ivec) {}
|
|
|
|
#endif
|
2013-07-25 12:58:40 +00:00
|
|
|
|
2013-07-25 13:37:55 +00:00
|
|
|
template<typename T2>
|
|
|
|
Vec2<T2> Cast() const
|
|
|
|
{
|
|
|
|
return Vec2<T2>((T2)x, (T2)y);
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:58:40 +00:00
|
|
|
static Vec2 AssignToAll(const T& f)
|
|
|
|
{
|
|
|
|
return Vec2<T>(f, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(T a[2])
|
|
|
|
{
|
|
|
|
a[0] = x; a[1] = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 operator +(const Vec2& other) const
|
|
|
|
{
|
|
|
|
return Vec2(x+other.x, y+other.y);
|
|
|
|
}
|
|
|
|
void operator += (const Vec2 &other)
|
|
|
|
{
|
|
|
|
x+=other.x; y+=other.y;
|
|
|
|
}
|
|
|
|
Vec2 operator -(const Vec2& other) const
|
|
|
|
{
|
|
|
|
return Vec2(x-other.x, y-other.y);
|
|
|
|
}
|
|
|
|
void operator -= (const Vec2& other)
|
|
|
|
{
|
|
|
|
x-=other.x; y-=other.y;
|
|
|
|
}
|
|
|
|
Vec2 operator -() const
|
|
|
|
{
|
|
|
|
return Vec2(-x,-y);
|
|
|
|
}
|
2013-07-15 13:23:10 +00:00
|
|
|
Vec2 operator * (const Vec2& other) const
|
2013-07-25 12:58:40 +00:00
|
|
|
{
|
|
|
|
return Vec2(x*other.x, y*other.y);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
Vec2 operator * (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec2(x*f,y*f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator *= (const V& f)
|
|
|
|
{
|
|
|
|
x*=f; y*=f;
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
Vec2 operator / (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec2(x/f,y/f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator /= (const V& f)
|
|
|
|
{
|
|
|
|
*this = *this / f;
|
|
|
|
}
|
|
|
|
|
|
|
|
T Length2() const
|
|
|
|
{
|
|
|
|
return x*x + y*y;
|
|
|
|
}
|
|
|
|
|
2013-10-05 20:05:32 +00:00
|
|
|
Vec2 Clamp(const T &l, const T &h) const
|
|
|
|
{
|
|
|
|
return Vec2(VecClamp(x, l, h), VecClamp(y, l, h));
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:58:40 +00:00
|
|
|
// Only implemented for T=float
|
|
|
|
float Length() const;
|
|
|
|
void SetLength(const float l);
|
|
|
|
Vec2 WithLength(const float l) const;
|
|
|
|
float Distance2To(Vec2 &other);
|
|
|
|
Vec2 Normalized() const;
|
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
|
|
|
|
|
|
|
T& operator [] (int i) //allow vector[1] = 3 (vector.y=3)
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
T operator [] (const int i) const
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetZero()
|
|
|
|
{
|
|
|
|
x=0; y=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common aliases: UV (texel coordinates), ST (texture coordinates)
|
|
|
|
T& u() { return x; }
|
|
|
|
T& v() { return y; }
|
|
|
|
T& s() { return x; }
|
|
|
|
T& t() { return y; }
|
|
|
|
|
|
|
|
const T& u() const { return x; }
|
|
|
|
const T& v() const { return y; }
|
|
|
|
const T& s() const { return x; }
|
|
|
|
const T& t() const { return y; }
|
2013-07-25 13:13:21 +00:00
|
|
|
|
|
|
|
// swizzlers - create a subvector of specific components
|
2014-08-17 16:37:32 +00:00
|
|
|
const Vec2 yx() const { return Vec2(y, x); }
|
|
|
|
const Vec2 vu() const { return Vec2(y, x); }
|
|
|
|
const Vec2 ts() const { return Vec2(y, x); }
|
2013-07-25 12:58:40 +00:00
|
|
|
};
|
|
|
|
|
2015-02-22 21:07:28 +00:00
|
|
|
template<typename T>
|
|
|
|
class Vec3Packed;
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename T>
|
2012-11-01 15:19:01 +00:00
|
|
|
class Vec3
|
|
|
|
{
|
|
|
|
public:
|
2014-03-16 21:29:22 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x,y,z;
|
|
|
|
};
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i ivec;
|
|
|
|
__m128 vec;
|
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
T* AsArray() { return &x; }
|
2013-11-17 22:10:57 +00:00
|
|
|
const T* AsArray() const { return &x; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
Vec3() {}
|
2013-07-25 12:46:42 +00:00
|
|
|
Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {}
|
|
|
|
Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
|
2013-11-23 21:07:07 +00:00
|
|
|
Vec3(const Vec2<T>& _xy, const T& _z) : x(_xy.x), y(_xy.y), z(_z) {}
|
2014-03-16 21:29:22 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
Vec3(const __m128 &_vec) : vec(_vec) {}
|
|
|
|
Vec3(const __m128i &_ivec) : ivec(_ivec) {}
|
2015-02-22 21:07:28 +00:00
|
|
|
Vec3(const Vec3Packed<T> &_xyz) {
|
2015-02-26 03:22:48 +00:00
|
|
|
vec = _mm_loadu_ps(_xyz.AsArray());
|
2015-02-22 21:07:28 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
Vec3(const Vec3Packed<T> &_xyz) : x(_xyz.x), y(_xyz.y), z(_xyz.z) {}
|
2014-03-16 21:29:22 +00:00
|
|
|
#endif
|
2013-07-13 09:46:44 +00:00
|
|
|
|
2013-07-25 13:37:55 +00:00
|
|
|
template<typename T2>
|
|
|
|
Vec3<T2> Cast() const
|
|
|
|
{
|
|
|
|
return Vec3<T2>((T2)x, (T2)y, (T2)z);
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
// Only implemented for T=int and T=float
|
|
|
|
static Vec3 FromRGB(unsigned int rgb);
|
2013-07-18 07:48:02 +00:00
|
|
|
unsigned int ToRGB() const; // alpha bits set to zero
|
2013-07-13 09:46:44 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
static Vec3 AssignToAll(const T& f)
|
2013-07-13 09:46:44 +00:00
|
|
|
{
|
2013-07-25 12:46:42 +00:00
|
|
|
return Vec3<T>(f, f, f);
|
2013-07-13 09:46:44 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
void Write(T a[3])
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
a[0] = x; a[1] = y; a[2] = z;
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
Vec3 operator +(const Vec3 &other) const
|
|
|
|
{
|
|
|
|
return Vec3(x+other.x, y+other.y, z+other.z);
|
|
|
|
}
|
|
|
|
void operator += (const Vec3 &other)
|
|
|
|
{
|
|
|
|
x+=other.x; y+=other.y; z+=other.z;
|
|
|
|
}
|
|
|
|
Vec3 operator -(const Vec3 &other) const
|
|
|
|
{
|
|
|
|
return Vec3(x-other.x, y-other.y, z-other.z);
|
|
|
|
}
|
|
|
|
void operator -= (const Vec3 &other)
|
|
|
|
{
|
|
|
|
x-=other.x; y-=other.y; z-=other.z;
|
|
|
|
}
|
|
|
|
Vec3 operator -() const
|
|
|
|
{
|
|
|
|
return Vec3(-x,-y,-z);
|
|
|
|
}
|
2013-07-15 13:23:10 +00:00
|
|
|
Vec3 operator * (const Vec3 &other) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return Vec3(x*other.x, y*other.y, z*other.z);
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename V>
|
|
|
|
Vec3 operator * (const V& f) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return Vec3(x*f,y*f,z*f);
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename V>
|
|
|
|
void operator *= (const V& f)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
x*=f; y*=f; z*=f;
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename V>
|
|
|
|
Vec3 operator / (const V& f) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-07-25 12:46:42 +00:00
|
|
|
return Vec3(x/f,y/f,z/f);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename V>
|
|
|
|
void operator /= (const V& f)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
*this = *this / f;
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
|
|
|
|
T Length2() const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return x*x + y*y + z*z;
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
|
2013-10-05 20:05:32 +00:00
|
|
|
Vec3 Clamp(const T &l, const T &h) const
|
|
|
|
{
|
|
|
|
return Vec3(VecClamp(x, l, h), VecClamp(y, l, h), VecClamp(z, l, h));
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
// Only implemented for T=float
|
|
|
|
float Length() const;
|
|
|
|
void SetLength(const float l);
|
|
|
|
Vec3 WithLength(const float l) const;
|
|
|
|
float Distance2To(Vec3 &other);
|
2018-01-31 07:38:11 +00:00
|
|
|
Vec3 Normalized(bool useSSE4 = false) const;
|
2021-02-28 07:51:45 +00:00
|
|
|
Vec3 NormalizedOr001(bool useSSE4 = false) const;
|
2013-07-25 12:46:42 +00:00
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
2021-02-28 07:51:45 +00:00
|
|
|
float NormalizeOr001();
|
2013-07-25 12:46:42 +00:00
|
|
|
|
|
|
|
T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
T operator [] (const int i) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
2013-07-25 12:46:42 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void SetZero()
|
|
|
|
{
|
2013-07-25 12:46:42 +00:00
|
|
|
x=0; y=0; z=0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-07-13 09:35:48 +00:00
|
|
|
|
|
|
|
// Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
|
2013-07-25 12:46:42 +00:00
|
|
|
T& u() { return x; }
|
|
|
|
T& v() { return y; }
|
|
|
|
T& w() { return z; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
T& r() { return x; }
|
|
|
|
T& g() { return y; }
|
|
|
|
T& b() { return z; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
T& s() { return x; }
|
|
|
|
T& t() { return y; }
|
|
|
|
T& q() { return z; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
const T& u() const { return x; }
|
|
|
|
const T& v() const { return y; }
|
|
|
|
const T& w() const { return z; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
const T& r() const { return x; }
|
|
|
|
const T& g() const { return y; }
|
|
|
|
const T& b() const { return z; }
|
2013-07-13 09:35:48 +00:00
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
const T& s() const { return x; }
|
|
|
|
const T& t() const { return y; }
|
|
|
|
const T& q() const { return z; }
|
2013-07-25 13:13:21 +00:00
|
|
|
|
|
|
|
// swizzlers - create a subvector of specific components
|
|
|
|
// e.g. Vec2 uv() { return Vec2(x,y); }
|
|
|
|
// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
|
2014-08-17 16:37:32 +00:00
|
|
|
#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
|
2013-07-25 13:13:21 +00:00
|
|
|
#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a##b); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a3##b3); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a4##b4); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b##a); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b2##a2); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b3##a3); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b4##a4);
|
|
|
|
|
|
|
|
DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
|
|
|
|
DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
|
|
|
|
DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
|
|
|
|
#undef DEFINE_SWIZZLER2
|
|
|
|
#undef _DEFINE_SWIZZLER2
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2014-03-17 13:58:50 +00:00
|
|
|
template<typename T>
|
|
|
|
class Vec3Packed
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x,y,z;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
T* AsArray() { return &x; }
|
|
|
|
const T* AsArray() const { return &x; }
|
|
|
|
|
|
|
|
Vec3Packed() {}
|
|
|
|
Vec3Packed(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {}
|
|
|
|
Vec3Packed(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
|
|
|
|
Vec3Packed(const Vec2<T>& _xy, const T& _z) : x(_xy.x), y(_xy.y), z(_z) {}
|
2015-02-26 03:22:48 +00:00
|
|
|
Vec3Packed(const Vec3<T>& _xyz) {
|
|
|
|
memcpy(&x, _xyz.AsArray(), sizeof(float) * 3);
|
|
|
|
}
|
2014-03-17 13:58:50 +00:00
|
|
|
|
|
|
|
template<typename T2>
|
|
|
|
Vec3Packed<T2> Cast() const
|
|
|
|
{
|
|
|
|
return Vec3Packed<T2>((T2)x, (T2)y, (T2)z);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only implemented for T=int and T=float
|
|
|
|
static Vec3Packed FromRGB(unsigned int rgb);
|
|
|
|
unsigned int ToRGB() const; // alpha bits set to zero
|
|
|
|
|
|
|
|
static Vec3Packed AssignToAll(const T& f)
|
|
|
|
{
|
|
|
|
return Vec3Packed<T>(f, f, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(T a[3])
|
|
|
|
{
|
|
|
|
a[0] = x; a[1] = y; a[2] = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3Packed operator +(const Vec3Packed &other) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(x+other.x, y+other.y, z+other.z);
|
|
|
|
}
|
|
|
|
void operator += (const Vec3Packed &other)
|
|
|
|
{
|
|
|
|
x+=other.x; y+=other.y; z+=other.z;
|
|
|
|
}
|
|
|
|
Vec3Packed operator -(const Vec3Packed &other) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(x-other.x, y-other.y, z-other.z);
|
|
|
|
}
|
|
|
|
void operator -= (const Vec3Packed &other)
|
|
|
|
{
|
|
|
|
x-=other.x; y-=other.y; z-=other.z;
|
|
|
|
}
|
|
|
|
Vec3Packed operator -() const
|
|
|
|
{
|
|
|
|
return Vec3Packed(-x,-y,-z);
|
|
|
|
}
|
|
|
|
Vec3Packed operator * (const Vec3Packed &other) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(x*other.x, y*other.y, z*other.z);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
Vec3Packed operator * (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(x*f,y*f,z*f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator *= (const V& f)
|
|
|
|
{
|
|
|
|
x*=f; y*=f; z*=f;
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
Vec3Packed operator / (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(x/f,y/f,z/f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator /= (const V& f)
|
|
|
|
{
|
|
|
|
*this = *this / f;
|
|
|
|
}
|
|
|
|
|
|
|
|
T Length2() const
|
|
|
|
{
|
|
|
|
return x*x + y*y + z*z;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3Packed Clamp(const T &l, const T &h) const
|
|
|
|
{
|
|
|
|
return Vec3Packed(VecClamp(x, l, h), VecClamp(y, l, h), VecClamp(z, l, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only implemented for T=float
|
|
|
|
float Length() const;
|
|
|
|
void SetLength(const float l);
|
|
|
|
Vec3Packed WithLength(const float l) const;
|
|
|
|
float Distance2To(Vec3Packed &other);
|
|
|
|
Vec3Packed Normalized() const;
|
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
|
|
|
|
|
|
|
T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
T operator [] (const int i) const
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetZero()
|
|
|
|
{
|
|
|
|
x=0; y=0; z=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
|
|
|
|
T& u() { return x; }
|
|
|
|
T& v() { return y; }
|
|
|
|
T& w() { return z; }
|
|
|
|
|
|
|
|
T& r() { return x; }
|
|
|
|
T& g() { return y; }
|
|
|
|
T& b() { return z; }
|
|
|
|
|
|
|
|
T& s() { return x; }
|
|
|
|
T& t() { return y; }
|
|
|
|
T& q() { return z; }
|
|
|
|
|
|
|
|
const T& u() const { return x; }
|
|
|
|
const T& v() const { return y; }
|
|
|
|
const T& w() const { return z; }
|
|
|
|
|
|
|
|
const T& r() const { return x; }
|
|
|
|
const T& g() const { return y; }
|
|
|
|
const T& b() const { return z; }
|
|
|
|
|
|
|
|
const T& s() const { return x; }
|
|
|
|
const T& t() const { return y; }
|
|
|
|
const T& q() const { return z; }
|
|
|
|
|
|
|
|
// swizzlers - create a subvector of specific components
|
|
|
|
// e.g. Vec2 uv() { return Vec2(x,y); }
|
|
|
|
// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
|
2014-08-17 16:37:32 +00:00
|
|
|
#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
|
2014-03-17 13:58:50 +00:00
|
|
|
#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a##b); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a3##b3); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a4##b4); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b##a); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b2##a2); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b3##a3); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b4##a4);
|
|
|
|
|
|
|
|
DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
|
|
|
|
DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
|
|
|
|
DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
|
|
|
|
#undef DEFINE_SWIZZLER2
|
|
|
|
#undef _DEFINE_SWIZZLER2
|
|
|
|
};
|
|
|
|
|
2013-07-25 12:54:53 +00:00
|
|
|
template<typename T>
|
|
|
|
class Vec4
|
|
|
|
{
|
|
|
|
public:
|
2014-03-16 21:29:22 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x,y,z,w;
|
|
|
|
};
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i ivec;
|
|
|
|
__m128 vec;
|
|
|
|
#endif
|
2013-07-25 12:54:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
T* AsArray() { return &x; }
|
2013-11-17 22:10:57 +00:00
|
|
|
const T* AsArray() const { return &x; }
|
2013-07-25 12:54:53 +00:00
|
|
|
|
|
|
|
Vec4() {}
|
|
|
|
Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {}
|
|
|
|
Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {}
|
2013-11-23 21:07:07 +00:00
|
|
|
Vec4(const Vec2<T>& _xy, const T& _z, const T& _w) : x(_xy.x), y(_xy.y), z(_z), w(_w) {}
|
|
|
|
Vec4(const Vec3<T>& _xyz, const T& _w) : x(_xyz.x), y(_xyz.y), z(_xyz.z), w(_w) {}
|
2014-03-16 21:29:22 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
Vec4(const __m128 &_vec) : vec(_vec) {}
|
|
|
|
Vec4(const __m128i &_ivec) : ivec(_ivec) {}
|
|
|
|
#endif
|
2013-07-25 12:54:53 +00:00
|
|
|
|
2013-07-25 13:37:55 +00:00
|
|
|
template<typename T2>
|
|
|
|
Vec4<T2> Cast() const
|
|
|
|
{
|
|
|
|
return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w);
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:54:53 +00:00
|
|
|
// Only implemented for T=int and T=float
|
|
|
|
static Vec4 FromRGBA(unsigned int rgba);
|
2015-02-28 10:35:07 +00:00
|
|
|
static Vec4 FromRGBA(const u8 *rgba);
|
2013-07-18 07:48:02 +00:00
|
|
|
unsigned int ToRGBA() const;
|
2015-02-28 10:35:07 +00:00
|
|
|
void ToRGBA(u8 *rgba) const;
|
2013-07-25 12:54:53 +00:00
|
|
|
|
|
|
|
static Vec4 AssignToAll(const T& f)
|
|
|
|
{
|
|
|
|
return Vec4<T>(f, f, f, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Write(T a[4])
|
|
|
|
{
|
|
|
|
a[0] = x; a[1] = y; a[2] = z; a[3] = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec4 operator +(const Vec4& other) const
|
|
|
|
{
|
|
|
|
return Vec4(x+other.x, y+other.y, z+other.z, w+other.w);
|
|
|
|
}
|
|
|
|
void operator += (const Vec4& other)
|
|
|
|
{
|
|
|
|
x+=other.x; y+=other.y; z+=other.z; w+=other.w;
|
|
|
|
}
|
|
|
|
Vec4 operator -(const Vec4 &other) const
|
|
|
|
{
|
|
|
|
return Vec4(x-other.x, y-other.y, z-other.z, w-other.w);
|
|
|
|
}
|
|
|
|
void operator -= (const Vec4 &other)
|
|
|
|
{
|
|
|
|
x-=other.x; y-=other.y; z-=other.z; w-=other.w;
|
|
|
|
}
|
|
|
|
Vec4 operator -() const
|
|
|
|
{
|
|
|
|
return Vec4(-x,-y,-z,-w);
|
|
|
|
}
|
2013-07-15 13:23:10 +00:00
|
|
|
Vec4 operator * (const Vec4 &other) const
|
2013-07-25 12:54:53 +00:00
|
|
|
{
|
|
|
|
return Vec4(x*other.x, y*other.y, z*other.z, w*other.w);
|
|
|
|
}
|
2017-04-23 14:18:32 +00:00
|
|
|
Vec4 operator | (const Vec4 &other) const
|
|
|
|
{
|
|
|
|
return Vec4(x | other.x, y | other.y, z | other.z, w | other.w);
|
|
|
|
}
|
2013-07-25 12:54:53 +00:00
|
|
|
template<typename V>
|
|
|
|
Vec4 operator * (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec4(x*f,y*f,z*f,w*f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator *= (const V& f)
|
|
|
|
{
|
|
|
|
x*=f; y*=f; z*=f; w*=f;
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
Vec4 operator / (const V& f) const
|
|
|
|
{
|
|
|
|
return Vec4(x/f,y/f,z/f,w/f);
|
|
|
|
}
|
|
|
|
template<typename V>
|
|
|
|
void operator /= (const V& f)
|
|
|
|
{
|
|
|
|
*this = *this / f;
|
|
|
|
}
|
|
|
|
|
2019-10-22 21:46:26 +00:00
|
|
|
bool operator ==(const Vec4 &other) const {
|
|
|
|
return x == other.x && y == other.y && z == other.z && w == other.w;
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:54:53 +00:00
|
|
|
T Length2() const
|
|
|
|
{
|
|
|
|
return x*x + y*y + z*z + w*w;
|
|
|
|
}
|
|
|
|
|
2013-10-05 20:05:32 +00:00
|
|
|
Vec4 Clamp(const T &l, const T &h) const
|
|
|
|
{
|
|
|
|
return Vec4(VecClamp(x, l, h), VecClamp(y, l, h), VecClamp(z, l, h), VecClamp(w, l, h));
|
|
|
|
}
|
|
|
|
|
2017-04-23 17:39:58 +00:00
|
|
|
Vec4 Reciprocal() const
|
|
|
|
{
|
|
|
|
const T one = 1.0f;
|
|
|
|
return Vec4(one / x, one / y, one / z, one / w);
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:54:53 +00:00
|
|
|
// Only implemented for T=float
|
|
|
|
float Length() const;
|
|
|
|
void SetLength(const float l);
|
|
|
|
Vec4 WithLength(const float l) const;
|
|
|
|
float Distance2To(Vec4 &other);
|
|
|
|
Vec4 Normalized() const;
|
|
|
|
float Normalize(); // returns the previous length, which is often useful
|
|
|
|
|
|
|
|
T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
T operator [] (const int i) const
|
|
|
|
{
|
|
|
|
return *((&x) + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetZero()
|
|
|
|
{
|
2014-12-13 18:35:16 +00:00
|
|
|
x=0; y=0; z=0; w=0;
|
2013-07-25 12:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common alias: RGBA (colors)
|
|
|
|
T& r() { return x; }
|
|
|
|
T& g() { return y; }
|
|
|
|
T& b() { return z; }
|
|
|
|
T& a() { return w; }
|
|
|
|
|
|
|
|
const T& r() const { return x; }
|
|
|
|
const T& g() const { return y; }
|
|
|
|
const T& b() const { return z; }
|
|
|
|
const T& a() const { return w; }
|
2013-07-25 13:13:21 +00:00
|
|
|
|
|
|
|
// swizzlers - create a subvector of specific components
|
|
|
|
// e.g. Vec2 uv() { return Vec2(x,y); }
|
|
|
|
// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
|
2014-08-17 16:37:32 +00:00
|
|
|
#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
|
2013-07-25 13:13:21 +00:00
|
|
|
#define DEFINE_SWIZZLER2(a, b, a2, b2) \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a##b); \
|
|
|
|
_DEFINE_SWIZZLER2(a, b, a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b##a); \
|
|
|
|
_DEFINE_SWIZZLER2(b, a, b2##a2);
|
|
|
|
|
|
|
|
DEFINE_SWIZZLER2(x, y, r, g);
|
|
|
|
DEFINE_SWIZZLER2(x, z, r, b);
|
|
|
|
DEFINE_SWIZZLER2(x, w, r, a);
|
|
|
|
DEFINE_SWIZZLER2(y, z, g, b);
|
|
|
|
DEFINE_SWIZZLER2(y, w, g, a);
|
|
|
|
DEFINE_SWIZZLER2(z, w, b, a);
|
|
|
|
#undef DEFINE_SWIZZLER2
|
|
|
|
#undef _DEFINE_SWIZZLER2
|
|
|
|
|
2014-08-17 16:37:32 +00:00
|
|
|
#define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3<T> name() const { return Vec3<T>(a, b, c); }
|
2013-07-25 13:13:21 +00:00
|
|
|
#define DEFINE_SWIZZLER3(a, b, c, a2, b2, c2) \
|
|
|
|
_DEFINE_SWIZZLER3(a, b, c, a##b##c); \
|
|
|
|
_DEFINE_SWIZZLER3(a, c, b, a##c##b); \
|
|
|
|
_DEFINE_SWIZZLER3(b, a, c, b##a##c); \
|
|
|
|
_DEFINE_SWIZZLER3(b, c, a, b##c##a); \
|
|
|
|
_DEFINE_SWIZZLER3(c, a, b, c##a##b); \
|
|
|
|
_DEFINE_SWIZZLER3(c, b, a, c##b##a); \
|
|
|
|
_DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
|
|
|
|
_DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
|
|
|
|
_DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
|
|
|
|
_DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
|
|
|
|
_DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
|
|
|
|
_DEFINE_SWIZZLER3(c, b, a, c2##b2##a2);
|
|
|
|
|
|
|
|
DEFINE_SWIZZLER3(x, y, z, r, g, b);
|
|
|
|
DEFINE_SWIZZLER3(x, y, w, r, g, a);
|
|
|
|
DEFINE_SWIZZLER3(x, z, w, r, b, a);
|
|
|
|
DEFINE_SWIZZLER3(y, z, w, g, b, a);
|
|
|
|
#undef DEFINE_SWIZZLER3
|
|
|
|
#undef _DEFINE_SWIZZLER3
|
2013-07-25 12:54:53 +00:00
|
|
|
};
|
|
|
|
|
2013-07-29 20:25:42 +00:00
|
|
|
|
|
|
|
template<typename BaseType>
|
|
|
|
class Mat3x3
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Convention: first three values = first column
|
|
|
|
Mat3x3(const BaseType values[])
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 3*3; ++i)
|
|
|
|
{
|
|
|
|
this->values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat3x3(BaseType _00, BaseType _01, BaseType _02, BaseType _10, BaseType _11, BaseType _12, BaseType _20, BaseType _21, BaseType _22)
|
|
|
|
{
|
|
|
|
values[0] = _00;
|
|
|
|
values[1] = _01;
|
|
|
|
values[2] = _02;
|
|
|
|
values[3] = _10;
|
|
|
|
values[4] = _11;
|
|
|
|
values[5] = _12;
|
|
|
|
values[6] = _20;
|
|
|
|
values[7] = _21;
|
|
|
|
values[8] = _22;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-17 21:27:33 +00:00
|
|
|
Vec3<T> operator * (const Vec3<T>& vec) const
|
2013-07-29 20:25:42 +00:00
|
|
|
{
|
|
|
|
Vec3<T> ret;
|
|
|
|
ret.x = values[0]*vec.x + values[3]*vec.y + values[6]*vec.z;
|
|
|
|
ret.y = values[1]*vec.x + values[4]*vec.y + values[7]*vec.z;
|
|
|
|
ret.z = values[2]*vec.x + values[5]*vec.y + values[8]*vec.z;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-17 21:27:33 +00:00
|
|
|
Mat3x3 Inverse() const
|
2013-07-29 20:25:42 +00:00
|
|
|
{
|
|
|
|
float a = values[0];
|
|
|
|
float b = values[1];
|
|
|
|
float c = values[2];
|
|
|
|
float d = values[3];
|
|
|
|
float e = values[4];
|
|
|
|
float f = values[5];
|
|
|
|
float g = values[6];
|
|
|
|
float h = values[7];
|
|
|
|
float i = values[8];
|
|
|
|
return Mat3x3(e*i-f*h, f*g-d*i, d*h-e*g,
|
|
|
|
c*h-b*i, a*i-c*g, b*g-a*h,
|
|
|
|
b*f-c*e, c*d-a*f, a*e-b*d) / Det();
|
|
|
|
}
|
|
|
|
|
2013-11-17 21:27:33 +00:00
|
|
|
BaseType Det() const
|
2013-07-29 20:25:42 +00:00
|
|
|
{
|
|
|
|
return values[0]*values[4]*values[8] + values[3]*values[7]*values[2] +
|
|
|
|
values[6]*values[1]*values[5] - values[2]*values[4]*values[6] -
|
|
|
|
values[5]*values[7]*values[0] - values[8]*values[1]*values[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat3x3 operator / (const BaseType& val) const
|
|
|
|
{
|
|
|
|
return Mat3x3(values[0]/val, values[1]/val, values[2]/val,
|
|
|
|
values[3]/val, values[4]/val, values[5]/val,
|
|
|
|
values[6]/val, values[7]/val, values[8]/val);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BaseType values[3*3];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-29 20:49:19 +00:00
|
|
|
template<typename BaseType>
|
|
|
|
class Mat4x4
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Convention: first four values in arrow = first column
|
|
|
|
Mat4x4(const BaseType values[])
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < 4*4; ++i)
|
|
|
|
{
|
|
|
|
this->values[i] = values[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-17 21:27:33 +00:00
|
|
|
Vec4<T> operator * (const Vec4<T>& vec) const
|
2013-07-29 20:49:19 +00:00
|
|
|
{
|
|
|
|
Vec4<T> ret;
|
|
|
|
ret.x = values[0]*vec.x + values[4]*vec.y + values[8]*vec.z + values[12]*vec.w;
|
|
|
|
ret.y = values[1]*vec.x + values[5]*vec.y + values[9]*vec.z + values[13]*vec.w;
|
|
|
|
ret.z = values[2]*vec.x + values[6]*vec.y + values[10]*vec.z + values[14]*vec.w;
|
|
|
|
ret.w = values[3]*vec.x + values[7]*vec.y + values[11]*vec.z + values[15]*vec.w;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BaseType values[4*4];
|
|
|
|
};
|
|
|
|
|
2013-12-29 21:32:55 +00:00
|
|
|
}; // namespace Math3D
|
|
|
|
|
2018-01-31 00:03:13 +00:00
|
|
|
typedef Math3D::Vec2<float> Vec2f;
|
2013-12-29 21:32:55 +00:00
|
|
|
typedef Math3D::Vec3<float> Vec3f;
|
2014-03-17 13:58:50 +00:00
|
|
|
typedef Math3D::Vec3Packed<float> Vec3Packedf;
|
2013-12-29 21:32:55 +00:00
|
|
|
typedef Math3D::Vec4<float> Vec4f;
|
|
|
|
|
2018-03-02 12:03:16 +00:00
|
|
|
// v and vecOut must point to different memory.
|
2017-08-17 20:51:20 +00:00
|
|
|
inline void Vec3ByMatrix43(float vecOut[3], const float v[3], const float m[12]) {
|
2012-11-01 15:19:01 +00:00
|
|
|
vecOut[0] = v[0] * m[0] + v[1] * m[3] + v[2] * m[6] + m[9];
|
|
|
|
vecOut[1] = v[0] * m[1] + v[1] * m[4] + v[2] * m[7] + m[10];
|
|
|
|
vecOut[2] = v[0] * m[2] + v[1] * m[5] + v[2] * m[8] + m[11];
|
|
|
|
}
|
|
|
|
|
2013-09-24 11:58:14 +00:00
|
|
|
inline void Vec3ByMatrix44(float vecOut[4], const float v[3], const float m[16])
|
|
|
|
{
|
2013-11-14 10:01:31 +00:00
|
|
|
vecOut[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + m[12];
|
|
|
|
vecOut[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + m[13];
|
|
|
|
vecOut[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + m[14];
|
2013-11-17 22:10:57 +00:00
|
|
|
vecOut[3] = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + m[15];
|
2013-09-24 11:58:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 10:01:31 +00:00
|
|
|
inline void Vec4ByMatrix44(float vecOut[4], const float v[4], const float m[16])
|
|
|
|
{
|
|
|
|
vecOut[0] = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12];
|
|
|
|
vecOut[1] = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13];
|
|
|
|
vecOut[2] = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14];
|
|
|
|
vecOut[3] = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
inline void Norm3ByMatrix43(float vecOut[3], const float v[3], const float m[12])
|
|
|
|
{
|
|
|
|
vecOut[0] = v[0] * m[0] + v[1] * m[3] + v[2] * m[6];
|
|
|
|
vecOut[1] = v[0] * m[1] + v[1] * m[4] + v[2] * m[7];
|
|
|
|
vecOut[2] = v[0] * m[2] + v[1] * m[5] + v[2] * m[8];
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:01:31 +00:00
|
|
|
inline void Matrix4ByMatrix4(float out[16], const float a[16], const float b[16]) {
|
2014-03-22 08:26:28 +00:00
|
|
|
fast_matrix_mul_4x4(out, b, a);
|
2013-11-14 10:01:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 21:32:55 +00:00
|
|
|
inline void ConvertMatrix4x3To4x4(float *m4x4, const float *m4x3) {
|
|
|
|
m4x4[0] = m4x3[0];
|
|
|
|
m4x4[1] = m4x3[1];
|
|
|
|
m4x4[2] = m4x3[2];
|
|
|
|
m4x4[3] = 0.0f;
|
|
|
|
m4x4[4] = m4x3[3];
|
|
|
|
m4x4[5] = m4x3[4];
|
|
|
|
m4x4[6] = m4x3[5];
|
|
|
|
m4x4[7] = 0.0f;
|
|
|
|
m4x4[8] = m4x3[6];
|
|
|
|
m4x4[9] = m4x3[7];
|
|
|
|
m4x4[10] = m4x3[8];
|
|
|
|
m4x4[11] = 0.0f;
|
|
|
|
m4x4[12] = m4x3[9];
|
|
|
|
m4x4[13] = m4x3[10];
|
|
|
|
m4x4[14] = m4x3[11];
|
|
|
|
m4x4[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:22:05 +00:00
|
|
|
inline void ConvertMatrix4x3To4x4Transposed(float *m4x4, const float *m4x3) {
|
|
|
|
m4x4[0] = m4x3[0];
|
|
|
|
m4x4[1] = m4x3[3];
|
|
|
|
m4x4[2] = m4x3[6];
|
|
|
|
m4x4[3] = m4x3[9];
|
|
|
|
m4x4[4] = m4x3[1];
|
|
|
|
m4x4[5] = m4x3[4];
|
|
|
|
m4x4[6] = m4x3[7];
|
|
|
|
m4x4[7] = m4x3[10];
|
|
|
|
m4x4[8] = m4x3[2];
|
|
|
|
m4x4[9] = m4x3[5];
|
|
|
|
m4x4[10] = m4x3[8];
|
|
|
|
m4x4[11] = m4x3[11];
|
|
|
|
m4x4[12] = 0.0f;
|
|
|
|
m4x4[13] = 0.0f;
|
|
|
|
m4x4[14] = 0.0f;
|
|
|
|
m4x4[15] = 1.0f;
|
|
|
|
}
|
|
|
|
|
2017-08-17 20:51:20 +00:00
|
|
|
// 0369
|
|
|
|
// 147A
|
|
|
|
// 258B
|
|
|
|
// ->>-
|
|
|
|
// 0123
|
|
|
|
// 4567
|
|
|
|
// 89AB
|
|
|
|
// Don't see a way to SIMD that. Should be pretty fast anyway.
|
2014-09-18 21:08:46 +00:00
|
|
|
inline void ConvertMatrix4x3To3x4Transposed(float *m4x4, const float *m4x3) {
|
|
|
|
m4x4[0] = m4x3[0];
|
|
|
|
m4x4[1] = m4x3[3];
|
|
|
|
m4x4[2] = m4x3[6];
|
|
|
|
m4x4[3] = m4x3[9];
|
|
|
|
m4x4[4] = m4x3[1];
|
|
|
|
m4x4[5] = m4x3[4];
|
|
|
|
m4x4[6] = m4x3[7];
|
|
|
|
m4x4[7] = m4x3[10];
|
|
|
|
m4x4[8] = m4x3[2];
|
|
|
|
m4x4[9] = m4x3[5];
|
|
|
|
m4x4[10] = m4x3[8];
|
|
|
|
m4x4[11] = m4x3[11];
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:22:05 +00:00
|
|
|
inline void Transpose4x4(float out[16], const float in[16]) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
out[i * 4 + j] = in[j * 4 + i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 10:01:31 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
inline float Vec3Dot(const float v1[3], const float v2[3])
|
|
|
|
{
|
|
|
|
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
|
|
|
|
}
|
2013-07-13 09:29:09 +00:00
|
|
|
|
2013-12-29 21:32:55 +00:00
|
|
|
namespace Math3D {
|
|
|
|
|
2013-07-25 12:58:40 +00:00
|
|
|
template<typename T>
|
|
|
|
inline T Dot(const Vec2<T>& a, const Vec2<T>& b)
|
|
|
|
{
|
|
|
|
return a.x*b.x + a.y*b.y;
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename T>
|
|
|
|
inline T Dot(const Vec3<T>& a, const Vec3<T>& b)
|
2013-07-13 09:29:09 +00:00
|
|
|
{
|
|
|
|
return a.x*b.x + a.y*b.y + a.z*b.z;
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:54:53 +00:00
|
|
|
template<typename T>
|
|
|
|
inline T Dot(const Vec4<T>& a, const Vec4<T>& b)
|
|
|
|
{
|
|
|
|
return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
|
|
|
|
}
|
|
|
|
|
2013-07-25 12:46:42 +00:00
|
|
|
template<typename T>
|
|
|
|
inline Vec3<T> Cross(const Vec3<T>& a, const Vec3<T>& b)
|
2013-07-13 09:29:09 +00:00
|
|
|
{
|
2013-07-25 12:46:42 +00:00
|
|
|
return Vec3<T>(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
|
2013-07-13 09:29:09 +00:00
|
|
|
}
|
2013-07-25 13:39:55 +00:00
|
|
|
|
2014-03-17 13:58:50 +00:00
|
|
|
template<typename T>
|
|
|
|
inline Vec3Packed<T> Cross(const Vec3Packed<T>& a, const Vec3Packed<T>& b)
|
|
|
|
{
|
|
|
|
return Vec3Packed<T>(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:03:04 +00:00
|
|
|
template<>
|
|
|
|
inline Vec3<float> Vec3<float>::FromRGB(unsigned int rgb)
|
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i z = _mm_setzero_si128();
|
|
|
|
__m128i c = _mm_cvtsi32_si128(rgb);
|
|
|
|
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
|
|
|
|
return Vec3<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
|
|
|
|
#else
|
|
|
|
return Vec3((rgb & 0xFF) * (1.0f/255.0f),
|
|
|
|
((rgb >> 8) & 0xFF) * (1.0f/255.0f),
|
|
|
|
((rgb >> 16) & 0xFF) * (1.0f/255.0f));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec3<int> Vec3<int>::FromRGB(unsigned int rgb)
|
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i z = _mm_setzero_si128();
|
|
|
|
__m128i c = _mm_cvtsi32_si128(rgb);
|
|
|
|
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
|
|
|
|
return Vec3<int>(c);
|
|
|
|
#else
|
|
|
|
return Vec3(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2014-03-23 19:09:17 +00:00
|
|
|
__forceinline unsigned int Vec3<float>::ToRGB() const
|
2014-03-18 06:03:04 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
|
|
|
|
__m128i c16 = _mm_packs_epi32(c, c);
|
|
|
|
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
|
|
|
|
#else
|
2014-10-31 16:07:54 +00:00
|
|
|
return (clamp_u8((int)(r() * 255.f)) << 0) |
|
2014-10-31 16:31:42 +00:00
|
|
|
(clamp_u8((int)(g() * 255.f)) << 8) |
|
2014-10-31 16:07:54 +00:00
|
|
|
(clamp_u8((int)(b() * 255.f)) << 16);
|
2014-03-18 06:03:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2014-03-23 19:09:17 +00:00
|
|
|
__forceinline unsigned int Vec3<int>::ToRGB() const
|
2014-03-18 06:03:04 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i c16 = _mm_packs_epi32(ivec, ivec);
|
|
|
|
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
|
|
|
|
#else
|
2014-10-31 16:07:54 +00:00
|
|
|
return clamp_u8(r()) | (clamp_u8(g()) << 8) | (clamp_u8(b()) << 16);
|
2014-03-18 06:03:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec4<float> Vec4<float>::FromRGBA(unsigned int rgba)
|
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i z = _mm_setzero_si128();
|
|
|
|
__m128i c = _mm_cvtsi32_si128(rgba);
|
|
|
|
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
|
|
|
|
return Vec4<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
|
|
|
|
#else
|
|
|
|
return Vec4((rgba & 0xFF) * (1.0f/255.0f),
|
|
|
|
((rgba >> 8) & 0xFF) * (1.0f/255.0f),
|
|
|
|
((rgba >> 16) & 0xFF) * (1.0f/255.0f),
|
|
|
|
((rgba >> 24) & 0xFF) * (1.0f/255.0f));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-28 10:35:07 +00:00
|
|
|
template<typename T>
|
|
|
|
inline Vec4<T> Vec4<T>::FromRGBA(const u8 *rgba)
|
|
|
|
{
|
|
|
|
return Vec4<T>::FromRGBA(*(unsigned int *)rgba);
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:03:04 +00:00
|
|
|
template<>
|
|
|
|
inline Vec4<int> Vec4<int>::FromRGBA(unsigned int rgba)
|
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i z = _mm_setzero_si128();
|
|
|
|
__m128i c = _mm_cvtsi32_si128(rgba);
|
|
|
|
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
|
|
|
|
return Vec4<int>(c);
|
|
|
|
#else
|
|
|
|
return Vec4(rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2014-03-23 19:09:17 +00:00
|
|
|
__forceinline unsigned int Vec4<float>::ToRGBA() const
|
2014-03-18 06:03:04 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
|
|
|
|
__m128i c16 = _mm_packs_epi32(c, c);
|
|
|
|
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
|
|
|
|
#else
|
2014-10-31 16:07:54 +00:00
|
|
|
return (clamp_u8((int)(r() * 255.f)) << 0) |
|
|
|
|
(clamp_u8((int)(g() * 255.f)) << 8) |
|
|
|
|
(clamp_u8((int)(b() * 255.f)) << 16) |
|
|
|
|
(clamp_u8((int)(a() * 255.f)) << 24);
|
2014-03-18 06:03:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2014-03-23 19:09:17 +00:00
|
|
|
__forceinline unsigned int Vec4<int>::ToRGBA() const
|
2014-03-18 06:03:04 +00:00
|
|
|
{
|
|
|
|
#if defined(_M_SSE)
|
|
|
|
__m128i c16 = _mm_packs_epi32(ivec, ivec);
|
|
|
|
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
|
|
|
|
#else
|
2014-10-31 16:07:54 +00:00
|
|
|
return clamp_u8(r()) | (clamp_u8(g()) << 8) | (clamp_u8(b()) << 16) | (clamp_u8(a()) << 24);
|
2014-03-18 06:03:04 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-02-28 10:35:07 +00:00
|
|
|
template<typename T>
|
|
|
|
__forceinline void Vec4<T>::ToRGBA(u8 *rgba) const
|
|
|
|
{
|
|
|
|
*(u32 *)rgba = ToRGBA();
|
|
|
|
}
|
|
|
|
|
2018-01-31 07:38:11 +00:00
|
|
|
#if defined(_M_SSE)
|
|
|
|
// Specialized for SIMD optimization
|
|
|
|
|
|
|
|
// Vec3<float> operation
|
|
|
|
template<>
|
|
|
|
inline void Vec3<float>::operator += (const Vec3<float> &other)
|
|
|
|
{
|
|
|
|
vec = _mm_add_ps(vec, other.vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec3<float> Vec3<float>::operator + (const Vec3 &other) const
|
|
|
|
{
|
|
|
|
return Vec3<float>(_mm_add_ps(vec, other.vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec3<float> Vec3<float>::operator * (const Vec3 &other) const
|
|
|
|
{
|
|
|
|
return Vec3<float>(_mm_mul_ps(vec, other.vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> template<>
|
|
|
|
inline Vec3<float> Vec3<float>::operator * (const float &other) const
|
|
|
|
{
|
|
|
|
return Vec3<float>(_mm_mul_ps(vec, _mm_set_ps1(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vec4<float> operation
|
|
|
|
template<>
|
|
|
|
inline void Vec4<float>::operator += (const Vec4<float> &other)
|
|
|
|
{
|
|
|
|
vec = _mm_add_ps(vec, other.vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec4<float> Vec4<float>::operator + (const Vec4 &other) const
|
|
|
|
{
|
|
|
|
return Vec4<float>(_mm_add_ps(vec, other.vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline Vec4<float> Vec4<float>::operator * (const Vec4 &other) const
|
|
|
|
{
|
|
|
|
return Vec4<float>(_mm_mul_ps(vec, other.vec));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> template<>
|
|
|
|
inline Vec4<float> Vec4<float>::operator * (const float &other) const
|
|
|
|
{
|
|
|
|
return Vec4<float>(_mm_mul_ps(vec, _mm_set_ps1(other)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vec3<float> cross product
|
|
|
|
template<>
|
|
|
|
inline Vec3<float> Cross(const Vec3<float> &a, const Vec3<float> &b)
|
|
|
|
{
|
|
|
|
const __m128 left = _mm_mul_ps(_mm_shuffle_ps(a.vec, a.vec, _MM_SHUFFLE(3, 0, 2, 1)), _mm_shuffle_ps(b.vec, b.vec, _MM_SHUFFLE(3, 1, 0, 2)));
|
|
|
|
const __m128 right = _mm_mul_ps(_mm_shuffle_ps(a.vec, a.vec, _MM_SHUFFLE(3, 1, 0, 2)), _mm_shuffle_ps(b.vec, b.vec, _MM_SHUFFLE(3, 0, 2, 1)));
|
|
|
|
return _mm_sub_ps(left, right);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-29 21:32:55 +00:00
|
|
|
}; // namespace Math3D
|
|
|
|
|
2013-07-25 13:39:55 +00:00
|
|
|
// linear interpolation via float: 0.0=begin, 1.0=end
|
|
|
|
template<typename X>
|
|
|
|
inline X Lerp(const X& begin, const X& end, const float t)
|
|
|
|
{
|
|
|
|
return begin*(1.f-t) + end*t;
|
|
|
|
}
|
|
|
|
|
|
|
|
// linear interpolation via int: 0=begin, base=end
|
|
|
|
template<typename X, int base>
|
|
|
|
inline X LerpInt(const X& begin, const X& end, const int t)
|
|
|
|
{
|
|
|
|
return (begin*(base-t) + end*t) / base;
|
|
|
|
}
|