aboutsummaryrefslogtreecommitdiffstats
path: root/src/mbgl/map/transform_state.hpp
blob: c3e1072c98e0d3b467665fee667e0a81e78715b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef MBGL_MAP_TRANSFORM_STATE
#define MBGL_MAP_TRANSFORM_STATE

#include <mbgl/util/geo.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/vec.hpp>
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/vec4.hpp>

#include <cstdint>
#include <array>
#include <limits>

namespace mbgl {

class TileID;
struct box;
struct TileCoordinate;

class TransformState {
    friend class Transform;

public:
    // Matrix
    void matrixFor(mat4& matrix, const TileID& id, const int8_t z) const;
    void getProjMatrix(mat4& matrix) const;
    box cornersToBox(uint32_t z) const;

    // Dimensions
    bool hasSize() const;
    uint16_t getWidth() const;
    uint16_t getHeight() const;

    std::array<float, 2> locationCoordinate(float lon, float lat) const;
    void getLonLat(double &lon, double &lat) const;

    // Position
    const LatLng getLatLng() const;

    // Zoom
    float getNormalizedZoom() const;
    double getZoom() const;
    int32_t getIntegerZoom() const;
    double getZoomFraction() const;
    double getScale() const;
    double getMinZoom() const;
    double getMaxZoom() const;

    // Rotation
    float getAngle() const;

    float getAltitude() const;
    float getPitch() const;

    // Changing
    bool isChanging() const;

    double pixel_x() const;
    double pixel_y() const;

    // Conversion and projection

    vec2<double> latLngToPoint(const LatLng& latLng) const;
    LatLng pointToLatLng(const vec2<double> point) const;

    TileCoordinate latLngToCoordinate(const LatLng& latLng) const;
    LatLng coordinateToLatLng(const TileCoordinate& coord) const;

    vec2<double> coordinateToPoint(const TileCoordinate& coord) const;
    TileCoordinate pointToCoordinate(const vec2<double> point) const;

private:
    void constrain(double& scale, double& y) const;

    // Limit the amount of zooming possible on the map.
    double min_scale = std::pow(2, 0);
    double max_scale = std::pow(2, 18);

    // logical dimensions
    uint16_t width = 0, height = 0;

    double xLng(double x, double worldSize) const;
    double yLat(double y, double worldSize) const;
    double lngX(double lon) const;
    double latY(double lat) const;
    float zoomScale(float zoom) const;
    float worldSize() const;

    mat4 coordinatePointMatrix(float z) const;
    mat4 getPixelMatrix() const;

private:
    // animation state
    bool rotating = false;
    bool scaling = false;
    bool panning = false;
    bool gestureInProgress = false;

    // map position
    double x = 0, y = 0;
    double angle = 0;
    double scale = 1;
    double altitude = 1.5;
    double pitch = 0.0;

    // cache values for spherical mercator math
    double Bc = (scale * util::tileSize) / 360;
    double Cc = (scale * util::tileSize) / util::M2PI;
};

}

#endif