core, android: add support for camera roll (#3011)
This commit is contained in:
@@ -14,20 +14,6 @@ using namespace std::numbers;
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
namespace {
|
||||
double vec2Len(const vec2& v) noexcept {
|
||||
return std::sqrt(v[0] * v[0] + v[1] * v[1]);
|
||||
};
|
||||
|
||||
double vec2Dot(const vec2& a, const vec2& b) noexcept {
|
||||
return a[0] * b[0] + a[1] * b[1];
|
||||
};
|
||||
|
||||
vec2 vec2Scale(const vec2& v, double s) noexcept {
|
||||
return vec2{{v[0] * s, v[1] * s}};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
namespace util {
|
||||
namespace {
|
||||
|
||||
@@ -66,12 +52,13 @@ vec3 toMercator(const LatLng& location, double altitudeMeters) noexcept {
|
||||
altitudeMeters * pixelsPerMeter / worldSize}};
|
||||
}
|
||||
|
||||
Quaternion orientationFromPitchBearing(double pitch, double bearing) noexcept {
|
||||
Quaternion orientationFromRollPitchBearing(double roll, double pitch, double bearing) noexcept {
|
||||
// Both angles have to be negated to achieve CW rotation around the axis of rotation
|
||||
const Quaternion rotBearing = Quaternion::fromAxisAngle({{0.0, 0.0, 1.0}}, -bearing);
|
||||
const Quaternion rotPitch = Quaternion::fromAxisAngle({{1.0, 0.0, 0.0}}, -pitch);
|
||||
Quaternion rotBearing = Quaternion::fromAxisAngle({{0.0, 0.0, 1.0}}, -bearing);
|
||||
Quaternion rotPitch = Quaternion::fromAxisAngle({{1.0, 0.0, 0.0}}, -pitch);
|
||||
Quaternion rotRoll = Quaternion::fromAxisAngle({{0.0, 0.0, 1.0}}, roll);
|
||||
|
||||
return rotBearing.multiply(rotPitch);
|
||||
return rotBearing.multiply(rotPitch).multiply(rotRoll);
|
||||
}
|
||||
|
||||
void updateTransform(mat4& transform, const Quaternion& orientation) noexcept {
|
||||
@@ -170,16 +157,24 @@ vec3 Camera::up() const noexcept {
|
||||
return {{-column[0], -column[1], -column[2]}};
|
||||
}
|
||||
|
||||
void Camera::getOrientation(double& pitch, double& bearing) const noexcept {
|
||||
void Camera::getOrientation(double& roll, double& pitch, double& bearing) const noexcept {
|
||||
const vec3 f = forward();
|
||||
const vec3 r = right();
|
||||
const vec3 u = up();
|
||||
|
||||
bearing = std::atan2(-r[1], r[0]);
|
||||
pitch = std::atan2(std::sqrt(f[0] * f[0] + f[1] * f[1]), -f[2]);
|
||||
const double f01 = std::sqrt(f[0] * f[0] + f[1] * f[1]);
|
||||
pitch = std::atan2(f01, -f[2]);
|
||||
if (f01 > 1.0e-3) {
|
||||
bearing = -std::atan2(f[0], -f[1]);
|
||||
roll = -std::atan2(r[2], u[2]);
|
||||
} else {
|
||||
bearing = std::atan2(-r[1], r[0]);
|
||||
roll = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::setOrientation(double pitch, double bearing) noexcept {
|
||||
orientation = orientationFromPitchBearing(pitch, bearing);
|
||||
void Camera::setOrientation(double roll, double pitch, double bearing) noexcept {
|
||||
orientation = orientationFromRollPitchBearing(roll, pitch, bearing);
|
||||
updateTransform(transform, orientation);
|
||||
}
|
||||
|
||||
@@ -193,32 +188,27 @@ void Camera::setPosition(const vec3& position) noexcept {
|
||||
}
|
||||
|
||||
std::optional<Quaternion> Camera::orientationFromFrame(const vec3& forward, const vec3& up) noexcept {
|
||||
vec3 upVector = up;
|
||||
|
||||
const vec2 xyForward = {{forward[0], forward[1]}};
|
||||
vec2 xyUp = {{up[0], up[1]}};
|
||||
const double epsilon = 1e-15;
|
||||
|
||||
// Remove roll-component of the resulting orientation by projecting
|
||||
// the up-vector to the forward vector on xy-plane
|
||||
if (vec2Len(xyForward) >= epsilon) {
|
||||
const vec2 xyDir = vec2Scale(xyForward, 1.0 / vec2Len(xyForward));
|
||||
|
||||
xyUp = vec2Scale(xyDir, vec2Dot(xyUp, xyDir));
|
||||
upVector[0] = xyUp[0];
|
||||
upVector[1] = xyUp[1];
|
||||
}
|
||||
|
||||
const vec3 right = vec3Cross(upVector, forward);
|
||||
const vec3 right = vec3Cross(up, forward);
|
||||
|
||||
if (vec3Length(right) < epsilon) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const double bearing = std::atan2(-right[1], right[0]);
|
||||
const double pitch = std::atan2(std::sqrt(forward[0] * forward[0] + forward[1] * forward[1]), -forward[2]);
|
||||
const double f01 = std::sqrt(forward[0] * forward[0] + forward[1] * forward[1]);
|
||||
const double pitch = std::atan2(f01, -forward[2]);
|
||||
double bearing;
|
||||
double roll;
|
||||
if (f01 > 1.0e-3) {
|
||||
bearing = -std::atan2(forward[0], -forward[1]);
|
||||
roll = -std::atan2(right[2], up[2]);
|
||||
} else {
|
||||
bearing = std::atan2(-right[1], right[0]);
|
||||
roll = 0.0;
|
||||
}
|
||||
|
||||
return util::orientationFromPitchBearing(pitch, bearing);
|
||||
return util::orientationFromRollPitchBearing(roll, pitch, bearing);
|
||||
}
|
||||
} // namespace util
|
||||
|
||||
@@ -258,14 +248,16 @@ void FreeCameraOptions::lookAtPoint(const LatLng& location, const std::optional<
|
||||
// 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;
|
||||
auto q = util::Camera::orientationFromFrame(forward, up);
|
||||
|
||||
if (q) {
|
||||
orientation = q->m;
|
||||
}
|
||||
}
|
||||
|
||||
void FreeCameraOptions::setPitchBearing(double pitch, double bearing) noexcept {
|
||||
orientation = util::orientationFromPitchBearing(util::deg2rad(pitch), util::deg2rad(bearing)).m;
|
||||
void FreeCameraOptions::setRollPitchBearing(double roll, double pitch, double bearing) noexcept {
|
||||
orientation =
|
||||
util::orientationFromRollPitchBearing(util::deg2rad(roll), util::deg2rad(pitch), util::deg2rad(bearing)).m;
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -25,14 +25,13 @@ public:
|
||||
vec3 up() const noexcept;
|
||||
|
||||
const Quaternion& getOrientation() const noexcept { return orientation; }
|
||||
void getOrientation(double& pitch, double& bearing) const noexcept;
|
||||
void setOrientation(double pitch, double bearing) noexcept;
|
||||
void getOrientation(double& roll, double& pitch, double& bearing) const noexcept;
|
||||
void setOrientation(double roll, double pitch, double bearing) noexcept;
|
||||
void setOrientation(const Quaternion& orientation_) noexcept;
|
||||
void setPosition(const vec3& position) noexcept;
|
||||
|
||||
// Computes orientation using forward and up vectors of the provided
|
||||
// coordinate frame. Only bearing and pitch components will be used. Does
|
||||
// not return a value if input is invalid
|
||||
// coordinate frame. Does not return a value if input is invalid
|
||||
static std::optional<Quaternion> orientationFromFrame(const vec3& forward, const vec3& up) noexcept;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user