diff options
author | Ansis Brammanis <brammanis@gmail.com> | 2015-08-17 16:22:46 -0400 |
---|---|---|
committer | Ansis Brammanis <brammanis@gmail.com> | 2015-08-24 18:41:51 -0400 |
commit | cef1b254445bdcfb20ada157ecc7f72c27aeacf9 (patch) | |
tree | a4ca0167238a01cb2982d6f9621e9406d1cd1a8a | |
parent | 554425b753c1eee6b6874f1ddde0c266a717c88e (diff) | |
download | qtlocation-mapboxgl-cef1b254445bdcfb20ada157ecc7f72c27aeacf9.tar.gz |
port invert and transformMat4 from gl-matrix
-rw-r--r-- | src/mbgl/util/mat4.cpp | 47 | ||||
-rw-r--r-- | src/mbgl/util/vec4.cpp | 33 | ||||
-rw-r--r-- | src/mbgl/util/vec4.hpp | 40 |
3 files changed, 120 insertions, 0 deletions
diff --git a/src/mbgl/util/mat4.cpp b/src/mbgl/util/mat4.cpp index 59137ded37..123c8464fd 100644 --- a/src/mbgl/util/mat4.cpp +++ b/src/mbgl/util/mat4.cpp @@ -45,6 +45,53 @@ void matrix::identity(mat4& out) { out[15] = 1.0f; } +bool matrix::invert(mat4& out, mat4& a) { + float a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3], + a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7], + a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11], + a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15], + + b00 = a00 * a11 - a01 * a10, + b01 = a00 * a12 - a02 * a10, + b02 = a00 * a13 - a03 * a10, + b03 = a01 * a12 - a02 * a11, + b04 = a01 * a13 - a03 * a11, + b05 = a02 * a13 - a03 * a12, + b06 = a20 * a31 - a21 * a30, + b07 = a20 * a32 - a22 * a30, + b08 = a20 * a33 - a23 * a30, + b09 = a21 * a32 - a22 * a31, + b10 = a21 * a33 - a23 * a31, + b11 = a22 * a33 - a23 * a32, + + // Calculate the determinant + det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return true; + } + det = 1.0 / det; + + out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; + out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; + out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; + out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; + out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; + out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; + out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; + out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; + out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; + out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; + + return false; +} + void matrix::ortho(mat4& out, double left, double right, double bottom, double top, double near, double far) { double lr = 1.0f / (left - right), bt = 1.0f / (bottom - top), diff --git a/src/mbgl/util/vec4.cpp b/src/mbgl/util/vec4.cpp new file mode 100644 index 0000000000..415b15d806 --- /dev/null +++ b/src/mbgl/util/vec4.cpp @@ -0,0 +1,33 @@ +// This is an incomplete port of http://glmatrix.net/ +// +// Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the +// use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not claim +// that you wrote the original software. If you use this software in a +// product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. + +#include <mbgl/util/vec4.hpp> + +using namespace mbgl; + +void matrix::transformMat4(vec4& out, vec4& a, mat4& m) { + float x = a[0], y = a[1], z = a[2], w = a[3]; + out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; + out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; + out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; + out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; +} diff --git a/src/mbgl/util/vec4.hpp b/src/mbgl/util/vec4.hpp new file mode 100644 index 0000000000..c5b4ab1d9e --- /dev/null +++ b/src/mbgl/util/vec4.hpp @@ -0,0 +1,40 @@ +// This is an incomplete port of http://glmatrix.net/ +// +// Copyright (c) 2013 Brandon Jones, Colin MacKenzie IV +// +// This software is provided 'as-is', without any express or implied warranty. +// In no event will the authors be held liable for any damages arising from the +// use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not claim +// that you wrote the original software. If you use this software in a +// product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. + +#ifndef MBGL_UTIL_VEC4 +#define MBGL_UTIL_VEC4 + +#include <array> +#include <mbgl/util/mat4.hpp> + +namespace mbgl { + +namespace matrix { + +typedef std::array<double, 4> vec4; + +void transformMat4(vec4& out, vec4& a, mat4& m); + +} +} + +#endif |