From 0922e71f98e4da8bcaee510874e05f887eead35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 20 Jan 2014 11:57:24 +0100 Subject: [PATCH] transform => Transform --- include/llmr/map/map.hpp | 2 +- include/llmr/map/transform.hpp | 14 ++++----- include/llmr/renderer/painter.hpp | 5 +-- src/map/map.cpp | 51 +++++++++++++++---------------- src/map/transform.cpp | 36 +++++++++++----------- src/renderer/painter.cpp | 10 +++--- 6 files changed, 57 insertions(+), 61 deletions(-) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index caedac941..95bf42bd2 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -50,7 +50,7 @@ private: private: Settings& settings; - transform *transform; + Transform transform; Painter painter; int32_t min_zoom; diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp index 2f78b0775..b16ea5a6d 100644 --- a/include/llmr/map/transform.hpp +++ b/include/llmr/map/transform.hpp @@ -12,17 +12,15 @@ namespace llmr { struct box; -class tile; - -class transform { +class Transform { public: - transform(); + Transform(); // Make noncopyable - transform(const transform&) = delete; - transform(const transform&&) = delete; - transform &operator=(const transform&) = delete; - transform &operator=(const transform&&) = delete; + Transform(const Transform&) = delete; + Transform(const Transform&&) = delete; + Transform &operator=(const Transform&) = delete; + Transform &operator=(const Transform&&) = delete; // Animations bool needsAnimation() const; diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 88ab4e0c2..687f86889 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -2,6 +2,7 @@ #define LLMR_RENDERER_PAINTER #include "../map/settings.hpp" +#include "../map/transform.hpp" #include "shader-fill.hpp" #include "shader-line.hpp" #include "shader-outline.hpp" @@ -13,7 +14,7 @@ class tile; class Painter { public: - Painter(class transform *transform, Settings& settings); + Painter(Transform& transform, Settings& settings); // Make noncopyable Painter(const Painter&) = delete; @@ -39,7 +40,7 @@ private: public: private: - transform *transform; + Transform& transform; Settings& settings; float matrix[16]; diff --git a/src/map/map.cpp b/src/map/map.cpp index 82054afd1..81ba185de 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -12,14 +12,13 @@ using namespace llmr; Map::Map(Settings& settings) : settings(settings), - transform(new class transform()), + transform(), painter(transform, settings), min_zoom(0), max_zoom(14) { } Map::~Map() { - delete transform; } void Map::setup() { @@ -27,60 +26,60 @@ void Map::setup() { } void Map::loadSettings() { - transform->setAngle(settings.angle); - transform->setScale(settings.scale); - transform->setLonLat(settings.longitude, settings.latitude); + transform.setAngle(settings.angle); + transform.setScale(settings.scale); + transform.setLonLat(settings.longitude, settings.latitude); update(); } void Map::resize(uint32_t width, uint32_t height) { - transform->width = width; - transform->height = height; + transform.width = width; + transform.height = height; update(); } void Map::moveBy(double dx, double dy) { - transform->moveBy(dx, dy); + transform.moveBy(dx, dy); update(); - transform->getLonLat(settings.longitude, settings.latitude); + transform.getLonLat(settings.longitude, settings.latitude); settings.save(); } void Map::scaleBy(double ds, double cx, double cy) { - transform->scaleBy(ds, cx, cy); + transform.scaleBy(ds, cx, cy); update(); - transform->getLonLat(settings.longitude, settings.latitude); - settings.scale = transform->getScale(); + transform.getLonLat(settings.longitude, settings.latitude); + settings.scale = transform.getScale(); settings.save(); } void Map::rotateBy(double cx, double cy, double sx, double sy, double ex, double ey) { - transform->rotateBy(cx, cy, sx, sy, ex, ey); + transform.rotateBy(cx, cy, sx, sy, ex, ey); update(); - settings.angle = transform->getAngle(); + settings.angle = transform.getAngle(); settings.save(); } void Map::resetNorth() { - transform->setAngle(0, 0.5); // 500 ms + transform.setAngle(0, 0.5); // 500 ms update(); - settings.angle = transform->getAngle(); + settings.angle = transform.getAngle(); settings.save(); } void Map::resetPosition() { - transform->setAngle(0); - transform->setLonLat(0, 0); - transform->setZoom(0); + transform.setAngle(0); + transform.setLonLat(0, 0); + transform.setZoom(0); update(); - transform->getLonLat(settings.longitude, settings.latitude); - settings.scale = transform->getScale(); - settings.angle = transform->getAngle(); + transform.getLonLat(settings.longitude, settings.latitude); + settings.scale = transform.getScale(); + settings.angle = transform.getAngle(); settings.save(); } @@ -177,7 +176,7 @@ bool Map::findLoadedParent(const tile_id& id, int32_t minCoveringZoom, std::forw void Map::updateTiles() { // Figure out what tiles we need to load - int32_t zoom = transform->getZoom(); + int32_t zoom = transform.getZoom(); if (zoom > max_zoom) zoom = max_zoom; if (zoom < min_zoom) zoom = min_zoom; @@ -192,7 +191,7 @@ void Map::updateTiles() { // Map four viewport corners to pixel coordinates box box; - transform->mapCornersToBox(zoom, box); + transform.mapCornersToBox(zoom, box); vec2 tl, br; tl.x = fmax(0, floor(fmin(box.tl.x, box.bl.x))); @@ -261,7 +260,7 @@ void Map::updateTiles() { } bool Map::render() { - transform->updateAnimations(); + transform.updateAnimations(); painter.clear(); @@ -272,7 +271,7 @@ bool Map::render() { } } - return transform->needsAnimation(); + return transform.needsAnimation(); } void Map::tileLoaded(tile::ptr tile) { diff --git a/src/map/transform.cpp b/src/map/transform.cpp index 941fa0c3d..cf0139cb3 100644 --- a/src/map/transform.cpp +++ b/src/map/transform.cpp @@ -14,7 +14,7 @@ const double M2PI = 2 * M_PI; const double A = 6378137; -transform::transform() +Transform::Transform() : width(0), height(0), @@ -28,22 +28,22 @@ transform::transform() setAngle(angle); } -bool transform::needsAnimation() const { +bool Transform::needsAnimation() const { return !animations.empty(); } -void transform::updateAnimations() { +void Transform::updateAnimations() { animations.remove_if([](const util::animation& animation) { return animation.update() == util::animation::complete; }); } -void transform::moveBy(double dx, double dy) { +void Transform::moveBy(double dx, double dy) { x += cos(angle) * dx + sin(angle) * dy; y += cos(angle) * dy + sin(-angle) * dx; } -void transform::scaleBy(double ds, double cx, double cy) { +void Transform::scaleBy(double ds, double cx, double cy) { // clamp scale to min/max values double new_scale = scale * ds; if (new_scale < min_scale) { @@ -64,7 +64,7 @@ void transform::scaleBy(double ds, double cx, double cy) { } -void transform::rotateBy(double anchor_x, double anchor_y, double start_x, double start_y, double end_x, double end_y) { +void Transform::rotateBy(double anchor_x, double anchor_y, double start_x, double start_y, double end_x, double end_y) { double center_x = width / 2, center_y = height / 2; const double begin_center_x = start_x - center_x; @@ -91,7 +91,7 @@ void transform::rotateBy(double anchor_x, double anchor_y, double start_x, doubl setAngle(ang); } -void transform::setAngle(double new_angle, double duration) { +void Transform::setAngle(double new_angle, double duration) { while (new_angle > M_PI) new_angle -= M2PI; while (new_angle <= -M_PI) new_angle += M2PI; @@ -102,7 +102,7 @@ void transform::setAngle(double new_angle, double duration) { } } -void transform::setScale(double new_scale) { +void Transform::setScale(double new_scale) { if (new_scale < min_scale) { new_scale = min_scale; } else if (new_scale > max_scale) { @@ -120,32 +120,32 @@ void transform::setScale(double new_scale) { Cc = s / (2 * M_PI); } -void transform::setZoom(double zoom) { +void Transform::setZoom(double zoom) { setScale(pow(2.0, zoom)); } -void transform::setLonLat(double lon, double lat) { +void Transform::setLonLat(double lon, double lat) { const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999); x = -round(lon * Bc); y = round(0.5 * Cc * log((1 + f) / (1 - f))); } -void transform::getLonLat(double &lon, double &lat) const { +void Transform::getLonLat(double &lon, double &lat) const { lon = -x / Bc; lat = R2D * (2 * atan(exp(y / Cc)) - 0.5 * M_PI); } -double transform::pixel_x() const { +double Transform::pixel_x() const { const double center = (width - scale * size) / 2; return center + x; } -double transform::pixel_y() const { +double Transform::pixel_y() const { const double center = (height - scale * size) / 2; return center + y; } -void transform::matrixFor(float matrix[16], const vec3& id) const { +void Transform::matrixFor(float matrix[16], const vec3& id) const { const double tile_scale = pow(2, id.z); const double tile_size = scale * size / tile_scale; @@ -164,19 +164,19 @@ void transform::matrixFor(float matrix[16], const vec3& id) const { mat4::translate(matrix, matrix, 0, 0, -1); } -int32_t transform::getZoom() const { +int32_t Transform::getZoom() const { return floor(log(scale) / M_LN2); } -double transform::getScale() const { +double Transform::getScale() const { return scale; } -double transform::getAngle() const { +double Transform::getAngle() const { return angle; } -void transform::mapCornersToBox(uint32_t z, box& b) const { +void Transform::mapCornersToBox(uint32_t z, box& b) const { const double ref_scale = pow(2, z); // Keep the map as upright as possible. diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 0031c9a6e..b0c49d802 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -27,7 +27,7 @@ GLshort tile_border_vertices[] = { 0, 0 }; -Painter::Painter(class transform *transform, Settings& settings) +Painter::Painter(Transform& transform, Settings& settings) : transform(transform), settings(settings), currentShader(NULL), @@ -84,14 +84,12 @@ void Painter::teardown() { } void Painter::changeMatrix(const tile::ptr& tile) { - assert(transform); - // Initialize projection matrix float projMatrix[16]; - mat4::ortho(projMatrix, 0, transform->width, transform->height, 0, 1, 10); + mat4::ortho(projMatrix, 0, transform.width, transform.height, 0, 1, 10); float mvMatrix[16]; - transform->matrixFor(mvMatrix, tile->id); + transform.matrixFor(mvMatrix, tile->id); mat4::multiply(matrix, projMatrix, mvMatrix); } @@ -156,7 +154,7 @@ void Painter::render(const tile::ptr& tile) { tile->lineVertex.bind(); glVertexAttribPointer(outlineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0)); glUniform4f(outlineShader->u_color, 0.0f, 0.0f, 0.0f, 1.0f); - glUniform2f(outlineShader->u_world, transform->width, transform->height); + glUniform2f(outlineShader->u_world, transform.width, transform.height); glLineWidth(2.0f); glDrawArrays(GL_LINE_STRIP, 0, tile->lineVertex.length());