rotation support

This commit is contained in:
Konstantin Käfer
2014-01-08 17:31:35 +01:00
parent 7526d681e2
commit 04d9d6b9f9
7 changed files with 113 additions and 34 deletions

View File

@@ -10,6 +10,7 @@ using namespace llmr;
const double MAXEXTENT = 20037508.34;
const double D2R = M_PI / 180.0;
const double R2D = 180.0 / M_PI;
const double M2PI = 2 * M_PI;
const double A = 6378137;
@@ -22,22 +23,35 @@ transform::transform()
angle(0.0),
scale(1.0) {
setScale(scale);
setAngle(angle);
}
void transform::moveBy(double dx, double dy) {
x += dx;
y += dy;
x += cos(angle) * dx + sin(angle) * dy;
y += cos(angle) * dy + sin(-angle) * dx;
}
void transform::scaleBy(double ds, double cx, double cy) {
const double dx = (cx - width / 2) * (1.0 - ds);
const double dy = (cy - height / 2) * (1.0 - ds);
const double fx = cos(angle) * dx + sin(angle) * dy;
const double fy = cos(angle) * dy + sin(-angle) * dx;
scale *= ds;
x = x * ds + (cx - width / 2) * (1.0 - ds);
y = y * ds + (cy - height / 2) * (1.0 - ds);
x = (x * ds) + fx;
y = (y * ds) + fy;
}
// void transform::setAngle(double _angle) {
// angle = _angle;
// }
void transform::rotateBy(double cx, double cy, double dx, double dy) {
const double distance = copysign(sqrt(dx * dx + dy * dy), dx);
setAngle(angle + D2R * distance);
}
void transform::setAngle(double new_angle) {
angle = new_angle;
while (angle > M_PI) angle -= M2PI;
while (angle <= -M_PI) angle += M2PI;
}
void transform::setScale(double new_scale) {
const double factor = new_scale / scale;
@@ -59,8 +73,6 @@ 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)));
fprintf(stderr, "pos: %f/%f\n", x, y);
}
void transform::getLonLat(double& lon, double& lat) const {
@@ -83,9 +95,20 @@ void transform::matrixFor(float matrix[16], uint32_t tile_z, uint32_t tile_x, ui
const double tile_size = scale * size / tile_scale;
mat4_identity(matrix);
mat4_translate(matrix, matrix, pixel_x(), pixel_y(), -1);
mat4_translate(matrix, matrix, 0.5f * (float)width, 0.5f * (float)height, 0);
mat4_rotate_z(matrix, matrix, angle);
mat4_translate(matrix, matrix, -0.5f * (float)width, -0.5f * (float)height, 0);
mat4_translate(matrix, matrix, pixel_x(), pixel_y(), 0);
mat4_translate(matrix, matrix, tile_x * tile_size, tile_y * tile_size, 0);
// TODO: Get rid of the 8 (scaling from 4096 to 512 px tile size);
mat4_scale(matrix, matrix, scale / tile_scale / 8, scale / tile_scale / 8, 1);
// Clipping plane
mat4_translate(matrix, matrix, 0, 0, -1);
}