|
| 1 | +// This file is part of the AliceVision project. |
| 2 | +// Copyright (c) 2024 AliceVision contributors. |
| 3 | +// This Source Code Form is subject to the terms of the Mozilla Public License, |
| 4 | +// v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +#include "Equirectangular.hpp" |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <cmath> |
| 11 | + |
| 12 | +namespace aliceVision { |
| 13 | +namespace camera { |
| 14 | + |
| 15 | +std::shared_ptr<Equirectangular> Equirectangular::cast(std::shared_ptr<IntrinsicBase> sptr) |
| 16 | +{ |
| 17 | + return std::dynamic_pointer_cast<Equirectangular>(sptr); |
| 18 | +} |
| 19 | + |
| 20 | +Vec2 Equirectangular::transformProject(const Eigen::Matrix4d& pose, const Vec4& pt, bool applyDistortion) const |
| 21 | +{ |
| 22 | + Vec4 X = pose * pt; |
| 23 | + Vec3 spherical = X.head(3).normalized(); |
| 24 | + |
| 25 | + Vec2 angles; |
| 26 | + angles.x() = atan2(spherical(0), spherical(2)); |
| 27 | + angles.y() = asin(spherical(1)); |
| 28 | + |
| 29 | + Vec2 imapt = cam2ima(angles); |
| 30 | + |
| 31 | + return imapt; |
| 32 | +} |
| 33 | + |
| 34 | +Vec2 Equirectangular::project(const Vec4& pt, bool applyDistortion) const |
| 35 | +{ |
| 36 | + Vec3 spherical = pt.head(3).normalized(); |
| 37 | + |
| 38 | + Vec2 angles; |
| 39 | + angles.x() = atan2(spherical(0), spherical(2)); |
| 40 | + angles.y() = asin(spherical(1)); |
| 41 | + |
| 42 | + Vec2 imapt = cam2ima(angles); |
| 43 | + |
| 44 | + return imapt; |
| 45 | +} |
| 46 | + |
| 47 | +Eigen::Matrix<double, 2, 3> Equirectangular::getDerivativeTransformProjectWrtPoint3(const Eigen::Matrix4d& T, const Vec4& pt) const |
| 48 | +{ |
| 49 | + Vec3 spherical = pt.head(3).normalized(); |
| 50 | + |
| 51 | + double sx = spherical(0); |
| 52 | + double sy = spherical(1); |
| 53 | + double sz = spherical(2); |
| 54 | + double len = sx*sx + sy*sy; |
| 55 | + |
| 56 | + double norm = pt.norm(); |
| 57 | + double normsq = norm * norm; |
| 58 | + double invnormsq = 1.0 / normsq; |
| 59 | + |
| 60 | + double d_norm_d_x = pt.x() / norm; |
| 61 | + double d_norm_d_y = pt.y() / norm; |
| 62 | + double d_norm_d_z = pt.z() / norm; |
| 63 | + |
| 64 | + // x / norm; y / norm; z / norm |
| 65 | + Eigen::Matrix<double, 3, 3> d_spherical_d_pt3; |
| 66 | + d_spherical_d_pt3(0, 0) = (norm * 1.0 - pt.x() * d_norm_d_x) * invnormsq; |
| 67 | + d_spherical_d_pt3(0, 1) = (norm * 0.0 - pt.x() * d_norm_d_y) * invnormsq; |
| 68 | + d_spherical_d_pt3(0, 2) = (norm * 0.0 - pt.x() * d_norm_d_z) * invnormsq; |
| 69 | + d_spherical_d_pt3(1, 0) = (norm * 0.0 - pt.y() * d_norm_d_x) * invnormsq; |
| 70 | + d_spherical_d_pt3(1, 1) = (norm * 1.0 - pt.y() * d_norm_d_y) * invnormsq; |
| 71 | + d_spherical_d_pt3(1, 2) = (norm * 0.0 - pt.y() * d_norm_d_z) * invnormsq; |
| 72 | + d_spherical_d_pt3(2, 0) = (norm * 0.0 - pt.z() * d_norm_d_x) * invnormsq; |
| 73 | + d_spherical_d_pt3(2, 1) = (norm * 0.0 - pt.z() * d_norm_d_y) * invnormsq; |
| 74 | + d_spherical_d_pt3(2, 2) = (norm * 1.0 - pt.z() * d_norm_d_z) * invnormsq; |
| 75 | + |
| 76 | + |
| 77 | + //atan2(spherical(0), spherical(2)) ; asin(spherical(1)); |
| 78 | + Eigen::Matrix<double, 2, 3> d_coords_d_spherical = Eigen::Matrix<double, 2, 3>::Zero(); |
| 79 | + d_coords_d_spherical(0, 0) = sz / len; |
| 80 | + d_coords_d_spherical(0, 2) = - sx / len; |
| 81 | + d_coords_d_spherical(1, 1) = 1.0 / sqrt(1.0 - (sy*sy)); |
| 82 | + |
| 83 | + |
| 84 | + return getDerivativeCam2ImaWrtPoint() * d_coords_d_spherical * d_spherical_d_pt3; |
| 85 | +} |
| 86 | + |
| 87 | +Eigen::Matrix<double, 2, 3> Equirectangular::getDerivativeTransformProjectWrtDisto(const Eigen::Matrix4d& pose, const Vec4& pt) const |
| 88 | +{ |
| 89 | + return Eigen::Matrix<double, 2, 3>::Zero(); |
| 90 | +} |
| 91 | + |
| 92 | +Eigen::Matrix<double, 2, 2> Equirectangular::getDerivativeTransformProjectWrtScale(const Eigen::Matrix4d& pose, const Vec4& pt) const |
| 93 | +{ |
| 94 | + Vec3 spherical = pt.head(3).normalized(); |
| 95 | + |
| 96 | + Vec2 angles; |
| 97 | + angles.x() = atan2(spherical(0), spherical(2)); |
| 98 | + angles.y() = asin(spherical(1)); |
| 99 | + |
| 100 | + return getDerivativeCam2ImaWrtScale(angles); |
| 101 | +} |
| 102 | + |
| 103 | +Eigen::Matrix<double, 2, 2> Equirectangular::getDerivativeTransformProjectWrtPrincipalPoint(const Eigen::Matrix4d& pose, const Vec4& pt) const |
| 104 | +{ |
| 105 | + return getDerivativeCam2ImaWrtPrincipalPoint(); |
| 106 | +} |
| 107 | + |
| 108 | +Eigen::Matrix<double, 2, Eigen::Dynamic> Equirectangular::getDerivativeTransformProjectWrtParams(const Eigen::Matrix4d& pose, const Vec4& pt3D) const |
| 109 | +{ |
| 110 | + Eigen::Matrix<double, 2, Eigen::Dynamic> ret(2, getParams().size()); |
| 111 | + |
| 112 | + ret.block<2, 2>(0, 0) = getDerivativeTransformProjectWrtScale(pose, pt3D); |
| 113 | + ret.block<2, 2>(0, 2) = getDerivativeTransformProjectWrtPrincipalPoint(pose, pt3D); |
| 114 | + |
| 115 | + return ret; |
| 116 | +} |
| 117 | + |
| 118 | +Vec3 Equirectangular::toUnitSphere(const Vec2& pt) const |
| 119 | +{ |
| 120 | + const double latitude = pt(1); |
| 121 | + const double longitude = pt(0); |
| 122 | + |
| 123 | + Vec3 spherical; |
| 124 | + spherical.x() = cos(latitude) * sin(longitude); |
| 125 | + spherical.y() = sin(latitude); |
| 126 | + spherical.z() = cos(latitude) * cos(longitude); |
| 127 | + |
| 128 | + return spherical; |
| 129 | +} |
| 130 | + |
| 131 | +Eigen::Matrix<double, 3, 2> Equirectangular::getDerivativetoUnitSphereWrtPoint(const Vec2& pt) const |
| 132 | +{ |
| 133 | + const double latitude = pt(1); |
| 134 | + const double longitude = pt(0); |
| 135 | + |
| 136 | + Vec3 spherical; |
| 137 | + spherical.x() = cos(latitude) * sin(longitude); |
| 138 | + spherical.y() = sin(latitude); |
| 139 | + spherical.z() = cos(latitude) * cos(longitude); |
| 140 | + |
| 141 | + Eigen::Matrix<double, 3, 2> d_spherical_d_pt; |
| 142 | + d_spherical_d_pt(0, 0) = cos(latitude) * cos(longitude); |
| 143 | + d_spherical_d_pt(0, 1) = -sin(latitude) * sin(longitude); |
| 144 | + d_spherical_d_pt(1, 0) = 0; |
| 145 | + d_spherical_d_pt(1, 1) = cos(latitude); |
| 146 | + d_spherical_d_pt(0, 0) = cos(latitude) * -sin(longitude); |
| 147 | + d_spherical_d_pt(0, 1) = -sin(latitude) * cos(longitude); |
| 148 | + |
| 149 | + return d_spherical_d_pt; |
| 150 | +} |
| 151 | + |
| 152 | +Eigen::Matrix<double, 3, Eigen::Dynamic> Equirectangular::getDerivativeBackProjectUnitWrtParams(const Vec2& pt2D) const |
| 153 | +{ |
| 154 | + const Vec2 ptMeters = ima2cam(pt2D); |
| 155 | + const Vec3 ptSphere = toUnitSphere(ptMeters); |
| 156 | + |
| 157 | + Eigen::Matrix<double, 3, Eigen::Dynamic> ret(3, 4); |
| 158 | + |
| 159 | + Eigen::Matrix<double, 3, 2> J = getDerivativetoUnitSphereWrtPoint(ptMeters); |
| 160 | + |
| 161 | + ret.block<3, 2>(0, 0) = J * getDerivativeIma2CamWrtScale(pt2D); |
| 162 | + ret.block<3, 2>(0, 2) = J * getDerivativeIma2CamWrtPrincipalPoint(); |
| 163 | + |
| 164 | + return ret; |
| 165 | +} |
| 166 | + |
| 167 | +double Equirectangular::imagePlaneToCameraPlaneError(double value) const { return 0.0; } |
| 168 | + |
| 169 | +bool Equirectangular::isVisibleRay(const Vec3& ray) const |
| 170 | +{ |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +EINTRINSIC Equirectangular::getType() const |
| 175 | +{ |
| 176 | + return EINTRINSIC::EQUIRECTANGULAR_CAMERA; |
| 177 | +} |
| 178 | + |
| 179 | +double Equirectangular::getHorizontalFov() const |
| 180 | +{ |
| 181 | + return 2.0 * M_PI; |
| 182 | +} |
| 183 | + |
| 184 | +double Equirectangular::getVerticalFov() const { return M_PI; } |
| 185 | + |
| 186 | +double Equirectangular::pixelProbability() const |
| 187 | +{ |
| 188 | + return 1.0 / double(w()); |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | +} // namespace camera |
| 194 | +} // namespace aliceVision |
0 commit comments