Skip to content
Snippets Groups Projects
Commit 641334ec authored by Philip Trettner's avatar Philip Trettner
Browse files

determinants up to 3x3, submatrix/subvector

parent 8e17d883
No related branches found
No related tags found
No related merge requests found
#pragma once
#include "../types/vec.hh"
namespace tg
{
namespace detail
{
template <class ScalarT>
constexpr ScalarT& csubscript(vec<1, ScalarT>& v, int i)
{
return v.x;
}
template <class ScalarT>
constexpr ScalarT const& csubscript(vec<1, ScalarT> const& v, int i)
{
return v.x;
}
template <class ScalarT>
constexpr ScalarT const& csubscript(vec<2, ScalarT> const& v, int i)
{
if (i == 0)
return v.x;
else
return v.y;
}
template <class ScalarT>
constexpr ScalarT& csubscript(vec<2, ScalarT>& v, int i)
{
if (i == 0)
return v.x;
else
return v.y;
}
template <class ScalarT>
constexpr ScalarT const& csubscript(vec<3, ScalarT> const& v, int i)
{
if (i == 0)
return v.x;
else if (i == 1)
return v.y;
else
return v.z;
}
template <class ScalarT>
constexpr ScalarT& csubscript(vec<3, ScalarT>& v, int i)
{
if (i == 0)
return v.x;
else if (i == 1)
return v.y;
else
return v.z;
}
template <class ScalarT>
constexpr ScalarT& csubscript(vec<4, ScalarT>& v, int i)
{
if (i == 0)
return v.x;
else if (i == 1)
return v.y;
else if (i == 2)
return v.z;
else
return v.w;
}
template <class ScalarT>
constexpr ScalarT const& csubscript(vec<4, ScalarT> const& v, int i)
{
if (i == 0)
return v.x;
else if (i == 1)
return v.y;
else if (i == 2)
return v.z;
else
return v.w;
}
} // namespace detail
} // namespace tg
......@@ -9,6 +9,7 @@
#include "functions/coordinates.hh"
#include "functions/cross.hh"
#include "functions/data_ptr.hh"
#include "functions/determinant.hh"
#include "functions/diag.hh"
#include "functions/direction.hh"
#include "functions/distance.hh"
......@@ -21,5 +22,7 @@
#include "functions/project.hh"
#include "functions/rasterize.hh"
#include "functions/round.hh"
#include "functions/submatrix.hh"
#include "functions/subvector.hh"
#include "functions/uniform.hh"
#include "functions/volume.hh"
#pragma once
#include "../../types/mat.hh"
#include "submatrix.hh"
namespace tg
{
template <class ScalarT>
constexpr ScalarT determinant(mat<1, 1, ScalarT> const& a)
{
return a[0].x;
}
template <class ScalarT>
constexpr ScalarT determinant(mat<2, 2, ScalarT> const& a)
{
return a[0].x * a[1].y - a[0].y * a[1].x;
}
template <class ScalarT>
constexpr ScalarT determinant(mat<3, 3, ScalarT> const& a)
{
return a[0].x * (a[1].y * a[2].z - a[2].y * a[1].z) + //
a[1].x * (a[2].y * a[0].z - a[0].y * a[2].z) + //
a[2].x * (a[0].y * a[1].z - a[0].z * a[1].y);
}
// template <class ScalarT>
// constexpr ScalarT determinant(mat<4, 4, ScalarT> const& a)
// {
// // TODO
// }
} // namespace tg
#pragma once
#include "../../types/mat.hh"
#include "subvector.hh"
namespace tg
{
template <int SX, int SY, int SC, int SR, int C, int R, class ScalarT>
constexpr mat<SC, SR, ScalarT> submatrix(mat<C, R, ScalarT> const& a)
{
static_assert(SC <= C && SR <= R, "submatrix can only make matrices smaller");
static_assert(SX >= 0 && SY >= 0, "submatrix must start at least at zero");
static_assert(SX + SC <= C && SY + SR <= R, "submatrix must be contained");
mat<SC, SR, ScalarT> m;
for (auto c = 0; c < SC; ++c)
m[c] = subvector<SY, SR>(a[c + SX]);
return m;
}
} // namespace tg
#pragma once
#include "../../types/vec.hh"
#include "../constexpr_helper.hh"
namespace tg
{
template <int SX, int SD, int D, class ScalarT>
constexpr vec<SD, ScalarT> subvector(vec<D, ScalarT> const& v)
{
static_assert(SD <= D, "subvector cannot be larger");
static_assert(SX >= 0, "subvector must start at least at 0");
static_assert(SD + SX <= D, "subvector must be contained");
vec<SD, ScalarT> r;
for (auto i = 0; i < SD; ++i)
detail::csubscript(r, i) = detail::csubscript(v, i + SX);
return r;
}
} // namespace tg
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment