g2o
Loading...
Searching...
No Matches
parameter_dims.h
Go to the documentation of this file.
1// 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 G2O_CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
32#define G2O_CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
33
34#include <array>
35#include <utility>
36
38
39namespace g2o {
40namespace ceres {
41namespace internal {
42
43// Checks, whether the given parameter block sizes are valid. Valid means every
44// dimension is bigger than zero.
45constexpr bool IsValidParameterDimensionSequence(std::integer_sequence<int>) {
46 return true;
47}
48
49template <int N, int... Ts>
51 std::integer_sequence<int, N, Ts...>) {
52 return (N <= 0) ? false
54 std::integer_sequence<int, Ts...>());
55}
56
57// Helper class that represents the parameter dimensions. The parameter
58// dimensions are either dynamic or the sizes are known at compile time. It is
59// used to pass parameter block dimensions around (e.g. between functions or
60// classes).
61//
62// As an example if one have three parameter blocks with dimensions (2, 4, 1),
63// one would use 'StaticParameterDims<2, 4, 1>' which is a synonym for
64// 'ParameterDims<false, 2, 4, 1>'.
65// For dynamic parameter dims, one would just use 'DynamicParameterDims', which
66// is a synonym for 'ParameterDims<true>'.
67template <bool IsDynamic, int... Ns>
69 public:
70 using Parameters = std::integer_sequence<int, Ns...>;
71
72 // The parameter dimensions are only valid if all parameter block dimensions
73 // are greater than zero.
74 static constexpr bool kIsValid =
76 static_assert(kIsValid,
77 "Invalid parameter block dimension detected. Each parameter "
78 "block dimension must be bigger than zero.");
79
80 static constexpr bool kIsDynamic = IsDynamic;
81 static constexpr int kNumParameterBlocks = sizeof...(Ns);
82 static_assert(kIsDynamic || kNumParameterBlocks > 0,
83 "At least one parameter block must be specified.");
84
85 static constexpr int kNumParameters =
86 Sum<std::integer_sequence<int, Ns...>>::Value;
87
88 static constexpr int GetDim(int dim) { return params_[dim]; }
89
90 // If one has all parameters packed into a single array this function unpacks
91 // the parameters.
92 template <typename T>
93 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
94 T* ptr) {
95 using Offsets = ExclusiveScan<Parameters>;
96 return GetUnpackedParameters(ptr, Offsets());
97 }
98
99 private:
100 template <typename T, int... Indices>
101 static inline std::array<T*, kNumParameterBlocks> GetUnpackedParameters(
102 T* ptr, std::integer_sequence<int, Indices...>) {
103 return std::array<T*, kNumParameterBlocks>{{ptr + Indices...}};
104 }
105
106 static constexpr std::array<int, kNumParameterBlocks> params_{Ns...};
107};
108
109// Even static constexpr member variables needs to be defined (not only
110// declared). As the ParameterDims class is tempalted this definition must
111// be in the header file.
112template <bool IsDynamic, int... Ns>
113constexpr std::array<int, ParameterDims<IsDynamic, Ns...>::kNumParameterBlocks>
114 ParameterDims<IsDynamic, Ns...>::params_;
115
116// Using declarations for static and dynamic parameter dims. This makes client
117// code easier to read.
118template <int... Ns>
119using StaticParameterDims = ParameterDims<false, Ns...>;
121
122} // namespace internal
123} // namespace ceres
124} // namespace g2o
125
126#endif // G2O_CERES_PUBLIC_INTERNAL_PARAMETER_DIMS_H_
static constexpr std::array< int, kNumParameterBlocks > params_
static constexpr int GetDim(int dim)
static constexpr int kNumParameterBlocks
std::integer_sequence< int, Ns... > Parameters
static std::array< T *, kNumParameterBlocks > GetUnpackedParameters(T *ptr)
static std::array< T *, kNumParameterBlocks > GetUnpackedParameters(T *ptr, std::integer_sequence< int, Indices... >)
constexpr bool IsValidParameterDimensionSequence(std::integer_sequence< int >)
typename ExclusiveScanT< Seq >::Type ExclusiveScan
ParameterDims< false, Ns... > StaticParameterDims
ParameterDims< true > DynamicParameterDims