瀏覽代碼

math: add outer product for matrices and vectors.

undefined
Sam Hocevar 10 年之前
父節點
當前提交
97cdc17ae1
共有 2 個檔案被更改,包括 47 行新增14 行删除
  1. +31
    -0
      src/lol/math/matrix.h
  2. +16
    -14
      src/math/matrix.cpp

+ 31
- 0
src/lol/math/matrix.h 查看文件

@@ -548,6 +548,37 @@ static inline mat_t<T, N, N> &operator *=(mat_t<T, N, N> &a,
return a = a * b;
}

/*
* Matrix-matrix outer product (Kronecker product)
*/

template<typename T, int COLS1, int COLS2, int ROWS1, int ROWS2>
static inline mat_t<T, COLS1 * COLS2, ROWS1 * ROWS2>
outer(mat_t<T, COLS1, ROWS1> const &a, mat_t<T, COLS2, ROWS2> const &b)
{
/* FIXME: could this be optimised somehow? */
mat_t<T, COLS1 * COLS2, ROWS1 * ROWS2> ret;
for (int i1 = 0; i1 < COLS1; ++i1)
for (int j1 = 0; j1 < ROWS1; ++j1)
for (int i2 = 0; i2 < COLS2; ++i2)
for (int j2 = 0; j2 < ROWS2; ++j2)
ret[i1 * COLS2 + i2][j1 * ROWS2 + j2]
= a[i1][j1] * b[i2][j2];
return ret;
}

/*
* Vector-vector outer product
*/

template<typename T, int COLS, int ROWS>
static inline mat_t<T, COLS, ROWS> outer(vec_t<T, ROWS> const &a,
vec_t<T, COLS> const &b)
{
return *reinterpret_cast<mat_t<T, 1, ROWS> const *>(&a)
* *reinterpret_cast<mat_t<T, COLS, 1> const *>(&b);
}

#if !LOL_FEATURE_CXX11_CONSTEXPR
#undef constexpr
#endif


+ 16
- 14
src/math/matrix.cpp 查看文件

@@ -17,17 +17,6 @@
namespace lol
{

template<> mat3 mat3::scale(float x)
{
mat3 ret(1.0f);

ret[0][0] = x;
ret[1][1] = x;
ret[2][2] = x;

return ret;
}

template<> mat3 mat3::scale(float x, float y, float z)
{
mat3 ret(1.0f);
@@ -39,6 +28,11 @@ template<> mat3 mat3::scale(float x, float y, float z)
return ret;
}

template<> mat3 mat3::scale(float x)
{
return scale(x, x, x);
}

template<> mat3 mat3::scale(vec3 v)
{
return scale(v.x, v.y, v.z);
@@ -198,7 +192,10 @@ template<> mat4 mat4::frustum(float left, float right, float bottom,
return ret;
}

//Returns a standard perspective matrix
/*
* Return a standard perspective matrix
*/

template<> mat4 mat4::perspective(float fov_y, float width,
float height, float near, float far)
{
@@ -208,9 +205,14 @@ template<> mat4 mat4::perspective(float fov_y, float width,
return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
}

//Returns a perspective matrix with the camera location shifted to be on the near plane
/*
* Return a perspective matrix with the camera location shifted to be on
* the near plane
*/

template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size,
float screen_ratio_yx, float near, float far)
float screen_ratio_yx,
float near, float far)
{
float tan_y = tanf(radians(fov_y) * .5f);
ASSERT(tan_y > 0.000001f);


Loading…
取消
儲存