diff options
-rw-r--r-- | include/mbgl/util/mat4.hpp | 8 | ||||
-rw-r--r-- | include/mbgl/util/vec.hpp | 2 | ||||
-rw-r--r-- | src/mbgl/map/transform_state.cpp | 36 | ||||
-rw-r--r-- | src/mbgl/map/transform_state.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/util/mat4.cpp | 38 | ||||
-rw-r--r-- | src/mbgl/util/vec4.cpp | 33 | ||||
-rw-r--r-- | src/mbgl/util/vec4.hpp | 40 |
7 files changed, 52 insertions, 106 deletions
diff --git a/include/mbgl/util/mat4.hpp b/include/mbgl/util/mat4.hpp index 59f6e3ee00..ff8e65224c 100644 --- a/include/mbgl/util/mat4.hpp +++ b/include/mbgl/util/mat4.hpp @@ -23,11 +23,13 @@ #ifndef MBGL_UTIL_MAT4 #define MBGL_UTIL_MAT4 +#include <mbgl/util/vec.hpp> + #include <array> namespace mbgl { -typedef std::array<double, 16> mat4; +using mat4 = std::array<double, 16>; namespace matrix { @@ -43,7 +45,9 @@ void rotate_z(mat4& out, const mat4& a, double rad); void scale(mat4& out, const mat4& a, double x, double y, double z); void multiply(mat4& out, const mat4& a, const mat4& b); +void transformMat4(vec4<double>& out, vec4<double>& a, mat4& m); + } // namespace matrix } // namespace mbgl -#endif +#endif // MBGL_UTIL_MAT4 diff --git a/include/mbgl/util/vec.hpp b/include/mbgl/util/vec.hpp index 6fca71b06c..83530a1798 100644 --- a/include/mbgl/util/vec.hpp +++ b/include/mbgl/util/vec.hpp @@ -122,4 +122,4 @@ struct vec4 { } // namespace mbgl -#endif +#endif // MBGL_UTIL_VEC diff --git a/src/mbgl/map/transform_state.cpp b/src/mbgl/map/transform_state.cpp index 1a0704b0a3..1ac6b5fbfa 100644 --- a/src/mbgl/map/transform_state.cpp +++ b/src/mbgl/map/transform_state.cpp @@ -5,7 +5,7 @@ #include <mbgl/util/interpolate.hpp> #include <mbgl/util/math.hpp> -using namespace mbgl; +namespace mbgl { TransformState::TransformState(ConstrainMode constrainMode_) : constrainMode(constrainMode_) @@ -278,10 +278,10 @@ LatLng TransformState::coordinateToLatLng(const TileCoordinate& coord) const { PrecisionPoint TransformState::coordinateToPoint(const TileCoordinate& coord) const { mat4 mat = coordinatePointMatrix(coord.zoom); - matrix::vec4 p; - matrix::vec4 c = {{ coord.column, coord.row, 0, 1 }}; + vec4<double> p; + vec4<double> c = { coord.column, coord.row, 0, 1 }; matrix::transformMat4(p, c, mat); - return { p[0] / p[3], height - p[1] / p[3] }; + return { p.x / p.w, height - p.y / p.w }; } TileCoordinate TransformState::pointToCoordinate(const PrecisionPoint& point) const { @@ -302,24 +302,24 @@ TileCoordinate TransformState::pointToCoordinate(const PrecisionPoint& point) co // unproject two points to get a line and then find the point on that // line with z=0 - matrix::vec4 coord0; - matrix::vec4 coord1; - matrix::vec4 point0 = {{ point.x, flippedY, 0, 1 }}; - matrix::vec4 point1 = {{ point.x, flippedY, 1, 1 }}; + vec4<double> coord0; + vec4<double> coord1; + vec4<double> point0 = { point.x, flippedY, 0, 1 }; + vec4<double> point1 = { point.x, flippedY, 1, 1 }; matrix::transformMat4(coord0, point0, inverted); matrix::transformMat4(coord1, point1, inverted); - double w0 = coord0[3]; - double w1 = coord1[3]; - double x0 = coord0[0] / w0; - double x1 = coord1[0] / w1; - double y0 = coord0[1] / w0; - double y1 = coord1[1] / w1; - double z0 = coord0[2] / w0; - double z1 = coord1[2] / w1; + double w0 = coord0.w; + double w1 = coord1.w; + double x0 = coord0.x / w0; + double x1 = coord1.x / w1; + double y0 = coord0.y / w0; + double y1 = coord1.y / w1; + double z0 = coord0.z / w0; + double z1 = coord1.z / w1; double t = z0 == z1 ? 0 : (targetZ - z0) / (z1 - z0); - + return { util::interpolate(x0, x1, t), util::interpolate(y0, y1, t), tileZoom }; } @@ -413,3 +413,5 @@ void TransformState::setScalePoint(const double newScale, const PrecisionPoint & Bc = worldSize() / 360; Cc = worldSize() / util::M2PI; } + +} // namespace mbgl diff --git a/src/mbgl/map/transform_state.hpp b/src/mbgl/map/transform_state.hpp index 0fb1f2304b..c57b2b8e1b 100644 --- a/src/mbgl/map/transform_state.hpp +++ b/src/mbgl/map/transform_state.hpp @@ -6,7 +6,6 @@ #include <mbgl/util/constants.hpp> #include <mbgl/util/vec.hpp> #include <mbgl/util/mat4.hpp> -#include <mbgl/util/vec4.hpp> #include <cstdint> #include <array> diff --git a/src/mbgl/util/mat4.cpp b/src/mbgl/util/mat4.cpp index 5f6f9d1b4c..bf8252595d 100644 --- a/src/mbgl/util/mat4.cpp +++ b/src/mbgl/util/mat4.cpp @@ -21,12 +21,15 @@ // 3. This notice may not be removed or altered from any source distribution. #include <mbgl/util/mat4.hpp> +#include <mbgl/util/vec.hpp> #include <cmath> -using namespace mbgl; +namespace mbgl { -void matrix::identity(mat4& out) { +namespace matrix { + +void identity(mat4& out) { out[0] = 1.0f; out[1] = 0.0f; out[2] = 0.0f; @@ -45,7 +48,7 @@ void matrix::identity(mat4& out) { out[15] = 1.0f; } -bool matrix::invert(mat4& out, mat4& a) { +bool invert(mat4& out, mat4& a) { double 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], @@ -92,7 +95,7 @@ bool matrix::invert(mat4& out, mat4& a) { return false; } -void matrix::ortho(mat4& out, double left, double right, double bottom, double top, double near, double far) { +void 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), nf = 1.0f / (near - far); @@ -114,7 +117,7 @@ void matrix::ortho(mat4& out, double left, double right, double bottom, double t out[15] = 1.0f; } -void matrix::perspective(mat4& out, double fovy, double aspect, double near, double far) { +void perspective(mat4& out, double fovy, double aspect, double near, double far) { double f = 1.0f / std::tan(fovy / 2.0f), nf = 1.0f / (near - far); out[0] = f / aspect; @@ -135,7 +138,7 @@ void matrix::perspective(mat4& out, double fovy, double aspect, double near, dou out[15] = 0.0f; } -void matrix::copy(mat4& out, const mat4& a) { +void copy(mat4& out, const mat4& a) { out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; @@ -154,7 +157,7 @@ void matrix::copy(mat4& out, const mat4& a) { out[15] = a[15]; } -void matrix::translate(mat4& out, const mat4& a, double x, double y, double z) { +void translate(mat4& out, const mat4& a, double x, double y, double z) { if (&a == &out) { out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; @@ -180,7 +183,7 @@ void matrix::translate(mat4& out, const mat4& a, double x, double y, double z) { } } -void matrix::rotate_x(mat4& out, const mat4& a, double rad) { +void rotate_x(mat4& out, const mat4& a, double rad) { double s = std::sin(rad), c = std::cos(rad), a10 = a[4], @@ -214,7 +217,7 @@ void matrix::rotate_x(mat4& out, const mat4& a, double rad) { out[11] = a23 * c - a13 * s; } -void matrix::rotate_y(mat4& out, const mat4& a, double rad) { +void rotate_y(mat4& out, const mat4& a, double rad) { double s = std::sin(rad), c = std::cos(rad), a00 = a[0], @@ -248,7 +251,7 @@ void matrix::rotate_y(mat4& out, const mat4& a, double rad) { out[11] = a03 * s + a23 * c; } -void matrix::rotate_z(mat4& out, const mat4& a, double rad) { +void rotate_z(mat4& out, const mat4& a, double rad) { double s = std::sin(rad), c = std::cos(rad), a00 = a[0], @@ -282,7 +285,7 @@ void matrix::rotate_z(mat4& out, const mat4& a, double rad) { out[7] = a13 * c - a03 * s; } -void matrix::scale(mat4& out, const mat4& a, double x, double y, double z) { +void scale(mat4& out, const mat4& a, double x, double y, double z) { out[0] = a[0] * x; out[1] = a[1] * x; out[2] = a[2] * x; @@ -301,7 +304,7 @@ void matrix::scale(mat4& out, const mat4& a, double x, double y, double z) { out[15] = a[15]; } -void matrix::multiply(mat4& out, const mat4& a, const mat4& b) { +void multiply(mat4& out, const mat4& a, const mat4& b) { double 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], @@ -332,3 +335,14 @@ void matrix::multiply(mat4& out, const mat4& a, const mat4& b) { out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; } + +void transformMat4(vec4<double>& out, vec4<double>& a, mat4& m) { + out.x = m[0] * a.x + m[4] * a.y + m[8] * a.z + m[12] * a.w; + out.y = m[1] * a.x + m[5] * a.y + m[9] * a.z + m[13] * a.w; + out.z = m[2] * a.x + m[6] * a.y + m[10] * a.z + m[14] * a.w; + out.w = m[3] * a.x + m[7] * a.y + m[11] * a.z + m[15] * a.w; +} + +} // namespace matrix + +} // namespace mbgl diff --git a/src/mbgl/util/vec4.cpp b/src/mbgl/util/vec4.cpp deleted file mode 100644 index bee4eaf976..0000000000 --- a/src/mbgl/util/vec4.cpp +++ /dev/null @@ -1,33 +0,0 @@ -// 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) { - double 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 deleted file mode 100644 index 8f54f22261..0000000000 --- a/src/mbgl/util/vec4.hpp +++ /dev/null @@ -1,40 +0,0 @@ -// 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); - -} // namespace matrix -} // namespace mbgl - -#endif |