Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2023 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: jodebo_beck@gmx.de (Johannes Beck) |
| 30 | // |
| 31 | |
| 32 | #ifndef CERES_PUBLIC_LINE_MANIFOLD_H_ |
| 33 | #define CERES_PUBLIC_LINE_MANIFOLD_H_ |
| 34 | |
| 35 | #include <Eigen/Core> |
| 36 | #include <algorithm> |
| 37 | #include <array> |
| 38 | #include <memory> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "ceres/internal/disable_warnings.h" |
| 42 | #include "ceres/internal/export.h" |
| 43 | #include "ceres/internal/householder_vector.h" |
| 44 | #include "ceres/internal/sphere_manifold_functions.h" |
| 45 | #include "ceres/manifold.h" |
| 46 | #include "ceres/types.h" |
| 47 | #include "glog/logging.h" |
| 48 | |
| 49 | namespace ceres { |
| 50 | // This provides a manifold for lines, where the line is |
| 51 | // over-parameterized by an origin point and a direction vector. So the |
| 52 | // parameter vector size needs to be two times the ambient space dimension, |
| 53 | // where the first half is interpreted as the origin point and the second half |
| 54 | // as the direction. |
| 55 | // |
| 56 | // The plus operator for the line direction is the same as for the |
| 57 | // SphereManifold. The update of the origin point is |
| 58 | // perpendicular to the line direction before the update. |
| 59 | // |
| 60 | // This manifold is a special case of the affine Grassmannian |
| 61 | // manifold (see https://en.wikipedia.org/wiki/Affine_Grassmannian_(manifold)) |
| 62 | // for the case Graff_1(R^n). |
| 63 | // |
| 64 | // The class works with dynamic and static ambient space dimensions. If the |
| 65 | // ambient space dimensions is known at compile time use |
| 66 | // |
| 67 | // LineManifold<3> manifold; |
| 68 | // |
| 69 | // If the ambient space dimensions is not known at compile time the template |
| 70 | // parameter needs to be set to ceres::DYNAMIC and the actual dimension needs |
| 71 | // to be provided as a constructor argument: |
| 72 | // |
| 73 | // LineManifold<ceres::DYNAMIC> manifold(ambient_dim); |
| 74 | // |
| 75 | template <int AmbientSpaceDimension> |
| 76 | class LineManifold final : public Manifold { |
| 77 | public: |
| 78 | static_assert(AmbientSpaceDimension == DYNAMIC || AmbientSpaceDimension >= 2, |
| 79 | "The ambient space must be at least 2."); |
| 80 | static_assert(ceres::DYNAMIC == Eigen::Dynamic, |
| 81 | "ceres::DYNAMIC needs to be the same as Eigen::Dynamic."); |
| 82 | |
| 83 | LineManifold(); |
| 84 | explicit LineManifold(int size); |
| 85 | |
| 86 | int AmbientSize() const override { return 2 * size_; } |
| 87 | int TangentSize() const override { return 2 * (size_ - 1); } |
| 88 | bool Plus(const double* x, |
| 89 | const double* delta, |
| 90 | double* x_plus_delta) const override; |
| 91 | bool PlusJacobian(const double* x, double* jacobian) const override; |
| 92 | bool Minus(const double* y, |
| 93 | const double* x, |
| 94 | double* y_minus_x) const override; |
| 95 | bool MinusJacobian(const double* x, double* jacobian) const override; |
| 96 | |
| 97 | private: |
| 98 | static constexpr bool IsDynamic = (AmbientSpaceDimension == ceres::DYNAMIC); |
| 99 | static constexpr int TangentSpaceDimension = |
| 100 | IsDynamic ? ceres::DYNAMIC : AmbientSpaceDimension - 1; |
| 101 | |
| 102 | static constexpr int DAmbientSpaceDimension = |
| 103 | IsDynamic ? ceres::DYNAMIC : 2 * AmbientSpaceDimension; |
| 104 | static constexpr int DTangentSpaceDimension = |
| 105 | IsDynamic ? ceres::DYNAMIC : 2 * TangentSpaceDimension; |
| 106 | |
| 107 | using AmbientVector = Eigen::Matrix<double, AmbientSpaceDimension, 1>; |
| 108 | using TangentVector = Eigen::Matrix<double, TangentSpaceDimension, 1>; |
| 109 | using MatrixPlusJacobian = Eigen::Matrix<double, |
| 110 | DAmbientSpaceDimension, |
| 111 | DTangentSpaceDimension, |
| 112 | Eigen::RowMajor>; |
| 113 | using MatrixMinusJacobian = Eigen::Matrix<double, |
| 114 | DTangentSpaceDimension, |
| 115 | DAmbientSpaceDimension, |
| 116 | Eigen::RowMajor>; |
| 117 | |
| 118 | const int size_{AmbientSpaceDimension}; |
| 119 | }; |
| 120 | |
| 121 | template <int AmbientSpaceDimension> |
| 122 | LineManifold<AmbientSpaceDimension>::LineManifold() |
| 123 | : size_{AmbientSpaceDimension} { |
| 124 | static_assert( |
| 125 | AmbientSpaceDimension != Eigen::Dynamic, |
| 126 | "The size is set to dynamic. Please call the constructor with a size."); |
| 127 | } |
| 128 | |
| 129 | template <int AmbientSpaceDimension> |
| 130 | LineManifold<AmbientSpaceDimension>::LineManifold(int size) : size_{size} { |
| 131 | if (AmbientSpaceDimension != Eigen::Dynamic) { |
| 132 | CHECK_EQ(AmbientSpaceDimension, size) |
| 133 | << "Specified size by template parameter differs from the supplied " |
| 134 | "one."; |
| 135 | } else { |
| 136 | CHECK_GT(size_, 1) |
| 137 | << "The size of the manifold needs to be greater than 1."; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | template <int AmbientSpaceDimension> |
| 142 | bool LineManifold<AmbientSpaceDimension>::Plus(const double* x_ptr, |
| 143 | const double* delta_ptr, |
| 144 | double* x_plus_delta_ptr) const { |
| 145 | // We seek a box plus operator of the form |
| 146 | // |
| 147 | // [o*, d*] = Plus([o, d], [delta_o, delta_d]) |
| 148 | // |
| 149 | // where o is the origin point, d is the direction vector, delta_o is |
| 150 | // the delta of the origin point and delta_d the delta of the direction and |
| 151 | // o* and d* is the updated origin point and direction. |
| 152 | // |
| 153 | // We separate the Plus operator into the origin point and directional part |
| 154 | // d* = Plus_d(d, delta_d) |
| 155 | // o* = Plus_o(o, d, delta_o) |
| 156 | // |
| 157 | // The direction update function Plus_d is the same as as the SphereManifold: |
| 158 | // |
| 159 | // d* = H_{v(d)} [sinc(|delta_d|) delta_d, cos(|delta_d|)]^T |
| 160 | // |
| 161 | // where H is the householder matrix |
| 162 | // H_{v} = I - (2 / |v|^2) v v^T |
| 163 | // and |
| 164 | // v(d) = d - sign(d_n) |d| e_n. |
| 165 | // |
| 166 | // The origin point update function Plus_o is defined as |
| 167 | // |
| 168 | // o* = o + H_{v(d)} [delta_o, 0]^T. |
| 169 | |
| 170 | Eigen::Map<const AmbientVector> o(x_ptr, size_); |
| 171 | Eigen::Map<const AmbientVector> d(x_ptr + size_, size_); |
| 172 | |
| 173 | Eigen::Map<const TangentVector> delta_o(delta_ptr, size_ - 1); |
| 174 | Eigen::Map<const TangentVector> delta_d(delta_ptr + size_ - 1, size_ - 1); |
| 175 | Eigen::Map<AmbientVector> o_plus_delta(x_plus_delta_ptr, size_); |
| 176 | Eigen::Map<AmbientVector> d_plus_delta(x_plus_delta_ptr + size_, size_); |
| 177 | |
| 178 | const double norm_delta_d = delta_d.norm(); |
| 179 | |
| 180 | o_plus_delta = o; |
| 181 | |
| 182 | // Shortcut for zero delta direction. |
| 183 | if (norm_delta_d == 0.0) { |
| 184 | d_plus_delta = d; |
| 185 | |
| 186 | if (delta_o.isZero(0.0)) { |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Calculate the householder transformation which is needed for f_d and f_o. |
| 192 | AmbientVector v(size_); |
| 193 | double beta; |
| 194 | |
| 195 | // NOTE: The explicit template arguments are needed here because |
| 196 | // ComputeHouseholderVector is templated and some versions of MSVC |
| 197 | // have trouble deducing the type of v automatically. |
| 198 | internal::ComputeHouseholderVector<Eigen::Map<const AmbientVector>, |
| 199 | double, |
| 200 | AmbientSpaceDimension>(d, &v, &beta); |
| 201 | |
| 202 | if (norm_delta_d != 0.0) { |
| 203 | internal::ComputeSphereManifoldPlus( |
| 204 | v, beta, d, delta_d, norm_delta_d, &d_plus_delta); |
| 205 | } |
| 206 | |
| 207 | // The null space is in the direction of the line, so the tangent space is |
| 208 | // perpendicular to the line direction. This is achieved by using the |
| 209 | // householder matrix of the direction and allow only movements |
| 210 | // perpendicular to e_n. |
| 211 | AmbientVector y(size_); |
| 212 | y << delta_o, 0; |
| 213 | o_plus_delta += internal::ApplyHouseholderVector(y, v, beta); |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | template <int AmbientSpaceDimension> |
| 219 | bool LineManifold<AmbientSpaceDimension>::PlusJacobian( |
| 220 | const double* x_ptr, double* jacobian_ptr) const { |
| 221 | Eigen::Map<const AmbientVector> d(x_ptr + size_, size_); |
| 222 | Eigen::Map<MatrixPlusJacobian> jacobian( |
| 223 | jacobian_ptr, 2 * size_, 2 * (size_ - 1)); |
| 224 | |
| 225 | // Clear the Jacobian as only half of the matrix is not zero. |
| 226 | jacobian.setZero(); |
| 227 | |
| 228 | auto jacobian_d = |
| 229 | jacobian |
| 230 | .template topLeftCorner<AmbientSpaceDimension, TangentSpaceDimension>( |
| 231 | size_, size_ - 1); |
| 232 | auto jacobian_o = jacobian.template bottomRightCorner<AmbientSpaceDimension, |
| 233 | TangentSpaceDimension>( |
| 234 | size_, size_ - 1); |
| 235 | internal::ComputeSphereManifoldPlusJacobian(d, &jacobian_d); |
| 236 | jacobian_o = jacobian_d; |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | template <int AmbientSpaceDimension> |
| 241 | bool LineManifold<AmbientSpaceDimension>::Minus(const double* y_ptr, |
| 242 | const double* x_ptr, |
| 243 | double* y_minus_x) const { |
| 244 | Eigen::Map<const AmbientVector> y_o(y_ptr, size_); |
| 245 | Eigen::Map<const AmbientVector> y_d(y_ptr + size_, size_); |
| 246 | Eigen::Map<const AmbientVector> x_o(x_ptr, size_); |
| 247 | Eigen::Map<const AmbientVector> x_d(x_ptr + size_, size_); |
| 248 | |
| 249 | Eigen::Map<TangentVector> y_minus_x_o(y_minus_x, size_ - 1); |
| 250 | Eigen::Map<TangentVector> y_minus_x_d(y_minus_x + size_ - 1, size_ - 1); |
| 251 | |
| 252 | AmbientVector v(size_); |
| 253 | double beta; |
| 254 | |
| 255 | // NOTE: The explicit template arguments are needed here because |
| 256 | // ComputeHouseholderVector is templated and some versions of MSVC |
| 257 | // have trouble deducing the type of v automatically. |
| 258 | internal::ComputeHouseholderVector<Eigen::Map<const AmbientVector>, |
| 259 | double, |
| 260 | AmbientSpaceDimension>(x_d, &v, &beta); |
| 261 | |
| 262 | internal::ComputeSphereManifoldMinus(v, beta, x_d, y_d, &y_minus_x_d); |
| 263 | |
| 264 | AmbientVector delta_o = y_o - x_o; |
| 265 | const AmbientVector h_delta_o = |
| 266 | internal::ApplyHouseholderVector(delta_o, v, beta); |
| 267 | y_minus_x_o = h_delta_o.template head<TangentSpaceDimension>(size_ - 1); |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | template <int AmbientSpaceDimension> |
| 273 | bool LineManifold<AmbientSpaceDimension>::MinusJacobian( |
| 274 | const double* x_ptr, double* jacobian_ptr) const { |
| 275 | Eigen::Map<const AmbientVector> d(x_ptr + size_, size_); |
| 276 | Eigen::Map<MatrixMinusJacobian> jacobian( |
| 277 | jacobian_ptr, 2 * (size_ - 1), 2 * size_); |
| 278 | |
| 279 | // Clear the Jacobian as only half of the matrix is not zero. |
| 280 | jacobian.setZero(); |
| 281 | |
| 282 | auto jacobian_d = |
| 283 | jacobian |
| 284 | .template topLeftCorner<TangentSpaceDimension, AmbientSpaceDimension>( |
| 285 | size_ - 1, size_); |
| 286 | auto jacobian_o = jacobian.template bottomRightCorner<TangentSpaceDimension, |
| 287 | AmbientSpaceDimension>( |
| 288 | size_ - 1, size_); |
| 289 | internal::ComputeSphereManifoldMinusJacobian(d, &jacobian_d); |
| 290 | jacobian_o = jacobian_d; |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | } // namespace ceres |
| 296 | |
| 297 | // clang-format off |
| 298 | #include "ceres/internal/reenable_warnings.h" |
| 299 | // clang-format on |
| 300 | |
| 301 | #endif // CERES_PUBLIC_LINE_MANIFOLD_H_ |