blob: b7cf93518cffd343f51faa33ad23262abd5216b2 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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#ifndef CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
32#define CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
33
34#include <array>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035#include <utility>
Austin Schuh70cc9552019-01-21 19:46:48 -080036
Austin Schuh70cc9552019-01-21 19:46:48 -080037#include "ceres/internal/integer_sequence_algorithm.h"
38
Austin Schuh3de38b02024-06-25 18:25:10 -070039namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080040
41// Helper class that represents the parameter dimensions. The parameter
42// dimensions are either dynamic or the sizes are known at compile time. It is
43// used to pass parameter block dimensions around (e.g. between functions or
44// classes).
45//
46// As an example if one have three parameter blocks with dimensions (2, 4, 1),
47// one would use 'StaticParameterDims<2, 4, 1>' which is a synonym for
48// 'ParameterDims<false, 2, 4, 1>'.
49// For dynamic parameter dims, one would just use 'DynamicParameterDims', which
50// is a synonym for 'ParameterDims<true>'.
51template <bool IsDynamic, int... Ns>
52class ParameterDims {
53 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080054 using Parameters = std::integer_sequence<int, Ns...>;
Austin Schuh70cc9552019-01-21 19:46:48 -080055
56 // The parameter dimensions are only valid if all parameter block dimensions
57 // are greater than zero.
Austin Schuh3de38b02024-06-25 18:25:10 -070058 static constexpr bool kIsValid = ((Ns > 0) && ...);
Austin Schuh70cc9552019-01-21 19:46:48 -080059 static_assert(kIsValid,
60 "Invalid parameter block dimension detected. Each parameter "
61 "block dimension must be bigger than zero.");
62
63 static constexpr bool kIsDynamic = IsDynamic;
64 static constexpr int kNumParameterBlocks = sizeof...(Ns);
65 static_assert(kIsDynamic || kNumParameterBlocks > 0,
66 "At least one parameter block must be specified.");
67
Austin Schuh3de38b02024-06-25 18:25:10 -070068 static constexpr int kNumParameters = (Ns + ... + 0);
Austin Schuh70cc9552019-01-21 19:46:48 -080069
70 static constexpr int GetDim(int dim) { return params_[dim]; }
71
72 // If one has all parameters packed into a single array this function unpacks
73 // the parameters.
74 template <typename T>
75 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
76 T* ptr) {
77 using Offsets = ExclusiveScan<Parameters>;
78 return GetUnpackedParameters(ptr, Offsets());
79 }
80
81 private:
82 template <typename T, int... Indices>
83 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080084 T* ptr, std::integer_sequence<int, Indices...>) {
Austin Schuh70cc9552019-01-21 19:46:48 -080085 return std::array<T*, kNumParameterBlocks>{{ptr + Indices...}};
86 }
87
88 static constexpr std::array<int, kNumParameterBlocks> params_{Ns...};
89};
90
91// Even static constexpr member variables needs to be defined (not only
92// declared). As the ParameterDims class is tempalted this definition must
93// be in the header file.
94template <bool IsDynamic, int... Ns>
95constexpr std::array<int, ParameterDims<IsDynamic, Ns...>::kNumParameterBlocks>
96 ParameterDims<IsDynamic, Ns...>::params_;
97
98// Using declarations for static and dynamic parameter dims. This makes client
99// code easier to read.
100template <int... Ns>
101using StaticParameterDims = ParameterDims<false, Ns...>;
102using DynamicParameterDims = ParameterDims<true>;
103
Austin Schuh3de38b02024-06-25 18:25:10 -0700104} // namespace ceres::internal
Austin Schuh70cc9552019-01-21 19:46:48 -0800105
106#endif // CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_