diff options
author | Mikko Pulkki <mikko.pulkki@mapbox.com> | 2020-04-27 13:28:46 +0300 |
---|---|---|
committer | Mikko Pulkki <55925868+mpulkki-mapbox@users.noreply.github.com> | 2020-05-02 17:07:02 +0300 |
commit | 83c06c1f99bf82fb1bc695b8b38fe1ea1dd91dde (patch) | |
tree | dbe6a4d917458b67787ad0b2d35e1bf23db062fb /src/mbgl/util/camera.cpp | |
parent | 57b4b2829e8033d6cf3f7bd48c1fe511e00b830c (diff) | |
download | qtlocation-mapboxgl-83c06c1f99bf82fb1bc695b8b38fe1ea1dd91dde.tar.gz |
Add FreeCameraOptions to the Map class
Diffstat (limited to 'src/mbgl/util/camera.cpp')
-rw-r--r-- | src/mbgl/util/camera.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/mbgl/util/camera.cpp b/src/mbgl/util/camera.cpp index f64e24ab2d..34d27620ac 100644 --- a/src/mbgl/util/camera.cpp +++ b/src/mbgl/util/camera.cpp @@ -212,4 +212,51 @@ optional<Quaternion> Camera::orientationFromFrame(const vec3& forward, const vec return util::orientationFromPitchBearing(pitch, bearing); } } // namespace util + +void FreeCameraOptions::setLocation(const LatLngAltitude& location) { + position = util::toMercator(location.location, location.altitude); +} + +optional<LatLngAltitude> FreeCameraOptions::getLocation() const { + if (!position) { + return nullopt; + } + + const vec3 positionValue = position.value(); + if (positionValue[1] < 0.0 || positionValue[1] > 1.0) { + return nullopt; + } + + const LatLng location = {util::latFromMercatorY(positionValue[1]), util::lngFromMercatorX(positionValue[0])}; + + const double metersPerPixel = Projection::getMetersPerPixelAtLatitude(location.latitude(), 0.0); + const double worldSize = Projection::worldSize(std::pow(2.0, 0.0)); + const double altitude = positionValue[2] * worldSize * metersPerPixel; + + return LatLngAltitude{location, altitude}; +} + +void FreeCameraOptions::lookAtPoint(const LatLng& location, const optional<vec3>& upVector) { + orientation = nullopt; + if (!position) { + return; + } + + const vec3 target = util::toMercator(location, 0.0); + const vec3 forward = vec3Sub(target, position.value()); + vec3 up = upVector ? upVector.value() : vec3{{0.0, 0.0, 1.0}}; + + // Flip z-component of the up vector if it's pointing downwards + up[2] = std::abs(up[2]); + + const auto newOrientation = util::Camera::orientationFromFrame(forward, up); + if (newOrientation) { + orientation = newOrientation.value().m; + } +} + +void FreeCameraOptions::setPitchBearing(double pitch, double bearing) { + orientation = util::orientationFromPitchBearing(pitch * util::DEG2RAD, bearing * util::DEG2RAD).m; +} + } // namespace mbgl
\ No newline at end of file |