blob: 240210614162beceea4466f2ea30fdf92a5ced0e [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2018 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#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
39namespace ceres {
40namespace internal {
41
42// Checks, whether the given parameter block sizes are valid. Valid means every
43// dimension is bigger than zero.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080044constexpr bool IsValidParameterDimensionSequence(std::integer_sequence<int>) {
Austin Schuh70cc9552019-01-21 19:46:48 -080045 return true;
46}
47
48template <int N, int... Ts>
49constexpr bool IsValidParameterDimensionSequence(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080050 std::integer_sequence<int, N, Ts...>) {
Austin Schuh70cc9552019-01-21 19:46:48 -080051 return (N <= 0) ? false
52 : IsValidParameterDimensionSequence(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080053 std::integer_sequence<int, Ts...>());
Austin Schuh70cc9552019-01-21 19:46:48 -080054}
55
56// Helper class that represents the parameter dimensions. The parameter
57// dimensions are either dynamic or the sizes are known at compile time. It is
58// used to pass parameter block dimensions around (e.g. between functions or
59// classes).
60//
61// As an example if one have three parameter blocks with dimensions (2, 4, 1),
62// one would use 'StaticParameterDims<2, 4, 1>' which is a synonym for
63// 'ParameterDims<false, 2, 4, 1>'.
64// For dynamic parameter dims, one would just use 'DynamicParameterDims', which
65// is a synonym for 'ParameterDims<true>'.
66template <bool IsDynamic, int... Ns>
67class ParameterDims {
68 public:
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080069 using Parameters = std::integer_sequence<int, Ns...>;
Austin Schuh70cc9552019-01-21 19:46:48 -080070
71 // The parameter dimensions are only valid if all parameter block dimensions
72 // are greater than zero.
73 static constexpr bool kIsValid =
74 IsValidParameterDimensionSequence(Parameters());
75 static_assert(kIsValid,
76 "Invalid parameter block dimension detected. Each parameter "
77 "block dimension must be bigger than zero.");
78
79 static constexpr bool kIsDynamic = IsDynamic;
80 static constexpr int kNumParameterBlocks = sizeof...(Ns);
81 static_assert(kIsDynamic || kNumParameterBlocks > 0,
82 "At least one parameter block must be specified.");
83
84 static constexpr int kNumParameters =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080085 Sum<std::integer_sequence<int, Ns...>>::Value;
Austin Schuh70cc9552019-01-21 19:46:48 -080086
87 static constexpr int GetDim(int dim) { return params_[dim]; }
88
89 // If one has all parameters packed into a single array this function unpacks
90 // the parameters.
91 template <typename T>
92 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
93 T* ptr) {
94 using Offsets = ExclusiveScan<Parameters>;
95 return GetUnpackedParameters(ptr, Offsets());
96 }
97
98 private:
99 template <typename T, int... Indices>
100 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800101 T* ptr, std::integer_sequence<int, Indices...>) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800102 return std::array<T*, kNumParameterBlocks>{{ptr + Indices...}};
103 }
104
105 static constexpr std::array<int, kNumParameterBlocks> params_{Ns...};
106};
107
108// Even static constexpr member variables needs to be defined (not only
109// declared). As the ParameterDims class is tempalted this definition must
110// be in the header file.
111template <bool IsDynamic, int... Ns>
112constexpr std::array<int, ParameterDims<IsDynamic, Ns...>::kNumParameterBlocks>
113 ParameterDims<IsDynamic, Ns...>::params_;
114
115// Using declarations for static and dynamic parameter dims. This makes client
116// code easier to read.
117template <int... Ns>
118using StaticParameterDims = ParameterDims<false, Ns...>;
119using DynamicParameterDims = ParameterDims<true>;
120
121} // namespace internal
122} // namespace ceres
123
124#endif // CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_