blob: 26428d07add68042573f884809599f24477de5a7 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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: sameeragarwal@google.com (Sameer Agarwal)
30// mierle@gmail.com (Keir Mierle)
31// jodebo_beck@gmx.de (Johannes Beck)
32
33#ifndef CERES_PUBLIC_INTERNAL_VARIADIC_EVALUATE_H_
34#define CERES_PUBLIC_INTERNAL_VARIADIC_EVALUATE_H_
35
36#include <stddef.h>
37
38#include <type_traits>
39
40#include "ceres/cost_function.h"
41#include "ceres/internal/parameter_dims.h"
42
43namespace ceres {
44namespace internal {
45
46// For fixed size cost functors
47template <typename Functor, typename T, int... Indices>
48inline bool VariadicEvaluateImpl(const Functor& functor, T const* const* input,
49 T* output, std::false_type /*is_dynamic*/,
50 integer_sequence<int, Indices...>) {
51 static_assert(sizeof...(Indices),
52 "Invalid number of parameter blocks. At least one parameter "
53 "block must be specified.");
54 return functor(input[Indices]..., output);
55}
56
57// For dynamic sized cost functors
58template <typename Functor, typename T>
59inline bool VariadicEvaluateImpl(const Functor& functor, T const* const* input,
60 T* output, std::true_type /*is_dynamic*/,
61 integer_sequence<int>) {
62 return functor(input, output);
63}
64
65// For ceres cost functors (not ceres::CostFunction)
66template <typename ParameterDims, typename Functor, typename T>
67inline bool VariadicEvaluateImpl(const Functor& functor, T const* const* input,
68 T* output, const void* /* NOT USED */) {
69 using ParameterBlockIndices =
70 make_integer_sequence<int, ParameterDims::kNumParameterBlocks>;
71 using IsDynamic = std::integral_constant<bool, ParameterDims::kIsDynamic>;
72 return VariadicEvaluateImpl(functor, input, output, IsDynamic(),
73 ParameterBlockIndices());
74}
75
76// For ceres::CostFunction
77template <typename ParameterDims, typename Functor, typename T>
78inline bool VariadicEvaluateImpl(const Functor& functor, T const* const* input,
79 T* output,
80 const CostFunction* /* NOT USED */) {
81 return functor.Evaluate(input, output, nullptr);
82}
83
84// Variadic evaluate is a helper function to evaluate ceres cost function or
85// functors using an input, output and the parameter dimensions. There are
86// several ways different possibilities:
87// 1) If the passed functor is a 'ceres::CostFunction' its evaluate method is
88// called.
89// 2) If the functor is not a 'ceres::CostFunction' and the specified parameter
90// dims is dynamic, the functor must have the following signature
91// 'bool(T const* const* input, T* output)'.
92// 3) If the functor is not a 'ceres::CostFunction' and the specified parameter
93// dims is not dynamic, the input is expanded by using the number of parameter
94// blocks. The signature of the functor must have the following signature
95// 'bool()(const T* i_1, const T* i_2, ... const T* i_n, T* output)'.
96template <typename ParameterDims, typename Functor, typename T>
97inline bool VariadicEvaluate(const Functor& functor, T const* const* input,
98 T* output) {
99 return VariadicEvaluateImpl<ParameterDims>(functor, input, output, &functor);
100}
101
102} // namespace internal
103} // namespace ceres
104
105#endif // CERES_PUBLIC_INTERNAL_VARIADIC_EVALUATE_H_