blob: 47ff6b18fa02e139eb6962db5ca7609d0a208b97 [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>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080039#include <utility>
Austin Schuh70cc9552019-01-21 19:46:48 -080040
41#include "ceres/cost_function.h"
42#include "ceres/internal/parameter_dims.h"
43
44namespace ceres {
45namespace internal {
46
47// For fixed size cost functors
48template <typename Functor, typename T, int... Indices>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049inline bool VariadicEvaluateImpl(const Functor& functor,
50 T const* const* input,
51 T* output,
52 std::false_type /*is_dynamic*/,
53 std::integer_sequence<int, Indices...>) {
Austin Schuh70cc9552019-01-21 19:46:48 -080054 static_assert(sizeof...(Indices),
55 "Invalid number of parameter blocks. At least one parameter "
56 "block must be specified.");
57 return functor(input[Indices]..., output);
58}
59
60// For dynamic sized cost functors
61template <typename Functor, typename T>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080062inline bool VariadicEvaluateImpl(const Functor& functor,
63 T const* const* input,
64 T* output,
65 std::true_type /*is_dynamic*/,
66 std::integer_sequence<int>) {
Austin Schuh70cc9552019-01-21 19:46:48 -080067 return functor(input, output);
68}
69
70// For ceres cost functors (not ceres::CostFunction)
71template <typename ParameterDims, typename Functor, typename T>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080072inline bool VariadicEvaluateImpl(const Functor& functor,
73 T const* const* input,
74 T* output,
75 const void* /* NOT USED */) {
Austin Schuh70cc9552019-01-21 19:46:48 -080076 using ParameterBlockIndices =
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077 std::make_integer_sequence<int, ParameterDims::kNumParameterBlocks>;
Austin Schuh70cc9552019-01-21 19:46:48 -080078 using IsDynamic = std::integral_constant<bool, ParameterDims::kIsDynamic>;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080079 return VariadicEvaluateImpl(
80 functor, input, output, IsDynamic(), ParameterBlockIndices());
Austin Schuh70cc9552019-01-21 19:46:48 -080081}
82
83// For ceres::CostFunction
84template <typename ParameterDims, typename Functor, typename T>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080085inline bool VariadicEvaluateImpl(const Functor& functor,
86 T const* const* input,
Austin Schuh70cc9552019-01-21 19:46:48 -080087 T* output,
88 const CostFunction* /* NOT USED */) {
89 return functor.Evaluate(input, output, nullptr);
90}
91
92// Variadic evaluate is a helper function to evaluate ceres cost function or
93// functors using an input, output and the parameter dimensions. There are
94// several ways different possibilities:
95// 1) If the passed functor is a 'ceres::CostFunction' its evaluate method is
96// called.
97// 2) If the functor is not a 'ceres::CostFunction' and the specified parameter
98// dims is dynamic, the functor must have the following signature
99// 'bool(T const* const* input, T* output)'.
100// 3) If the functor is not a 'ceres::CostFunction' and the specified parameter
101// dims is not dynamic, the input is expanded by using the number of parameter
102// blocks. The signature of the functor must have the following signature
103// 'bool()(const T* i_1, const T* i_2, ... const T* i_n, T* output)'.
104template <typename ParameterDims, typename Functor, typename T>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105inline bool VariadicEvaluate(const Functor& functor,
106 T const* const* input,
Austin Schuh70cc9552019-01-21 19:46:48 -0800107 T* output) {
108 return VariadicEvaluateImpl<ParameterDims>(functor, input, output, &functor);
109}
110
111} // namespace internal
112} // namespace ceres
113
114#endif // CERES_PUBLIC_INTERNAL_VARIADIC_EVALUATE_H_