parameter_dims.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
  31. #define CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
  32. #include <array>
  33. #include <utility>
  34. #include "ceres/internal/integer_sequence_algorithm.h"
  35. namespace ceres::internal {
  36. // Helper class that represents the parameter dimensions. The parameter
  37. // dimensions are either dynamic or the sizes are known at compile time. It is
  38. // used to pass parameter block dimensions around (e.g. between functions or
  39. // classes).
  40. //
  41. // As an example if one have three parameter blocks with dimensions (2, 4, 1),
  42. // one would use 'StaticParameterDims<2, 4, 1>' which is a synonym for
  43. // 'ParameterDims<false, 2, 4, 1>'.
  44. // For dynamic parameter dims, one would just use 'DynamicParameterDims', which
  45. // is a synonym for 'ParameterDims<true>'.
  46. template <bool IsDynamic, int... Ns>
  47. class ParameterDims {
  48. public:
  49. using Parameters = std::integer_sequence<int, Ns...>;
  50. // The parameter dimensions are only valid if all parameter block dimensions
  51. // are greater than zero.
  52. static constexpr bool kIsValid = ((Ns > 0) && ...);
  53. static_assert(kIsValid,
  54. "Invalid parameter block dimension detected. Each parameter "
  55. "block dimension must be bigger than zero.");
  56. static constexpr bool kIsDynamic = IsDynamic;
  57. static constexpr int kNumParameterBlocks = sizeof...(Ns);
  58. static_assert(kIsDynamic || kNumParameterBlocks > 0,
  59. "At least one parameter block must be specified.");
  60. static constexpr int kNumParameters = (Ns + ... + 0);
  61. static constexpr int GetDim(int dim) { return params_[dim]; }
  62. // If one has all parameters packed into a single array this function unpacks
  63. // the parameters.
  64. template <typename T>
  65. static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
  66. T* ptr) {
  67. using Offsets = ExclusiveScan<Parameters>;
  68. return GetUnpackedParameters(ptr, Offsets());
  69. }
  70. private:
  71. template <typename T, int... Indices>
  72. static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
  73. T* ptr, std::integer_sequence<int, Indices...>) {
  74. return std::array<T*, kNumParameterBlocks>{{ptr + Indices...}};
  75. }
  76. static constexpr std::array<int, kNumParameterBlocks> params_{Ns...};
  77. };
  78. // Even static constexpr member variables needs to be defined (not only
  79. // declared). As the ParameterDims class is tempalted this definition must
  80. // be in the header file.
  81. template <bool IsDynamic, int... Ns>
  82. constexpr std::array<int, ParameterDims<IsDynamic, Ns...>::kNumParameterBlocks>
  83. ParameterDims<IsDynamic, Ns...>::params_;
  84. // Using declarations for static and dynamic parameter dims. This makes client
  85. // code easier to read.
  86. template <int... Ns>
  87. using StaticParameterDims = ParameterDims<false, Ns...>;
  88. using DynamicParameterDims = ParameterDims<true>;
  89. } // namespace ceres::internal
  90. #endif // CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_