Skip to content

Instantly share code, notes, and snippets.

@mosra
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosra/bf6d847fecbd8cba4727 to your computer and use it in GitHub Desktop.
Save mosra/bf6d847fecbd8cba4727 to your computer and use it in GitHub Desktop.
Compile-time matrix/vector computations in Magnum

Magnum now has compile-time matrix/vector library. With full C++14 support in Clang 3.4 you can throw out nearly all manual precomputations, just slap constexpr on the result and be done with it:

#include <Magnum/Magnum.h>
#include <Magnum/Math/Matrix4.h>

using namespace Magnum;

int main() {
    constexpr auto a = Matrix4::scaling(Vector3::xScale(3.0f))*
                       Matrix4::translation({1.5f, -0.3f, 1.1f});

    constexpr auto b = a.inverted();

    static_assert((a*b).determinant() > 1.0f - Math::TypeTraits<Float>::epsilon() &&
                  (a*b).determinant() < 1.0f + Math::TypeTraits<Float>::epsilon(),
                  "The inversion went bad!");
}

Because why not. The code will compile without any problem, try to change the comparison to something else to see the difference.

The code is in cpp14 branch of Corrade and Magnum libraries. Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment