1
// Ceres Solver - A fast non-linear least squares minimizer
2
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3
// http://code.google.com/p/ceres-solver/
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are met:
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.
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.
29
// Author: keir@google.com (Keir Mierle)
31
// A convenience class for cost functions which are statically sized.
32
// Compared to the dynamically-sized base class, this reduces boilerplate.
34
// The kNumResiduals template parameter can be a constant such as 2 or 5, or it
35
// can be ceres::DYNAMIC. If kNumResiduals is ceres::DYNAMIC, then subclasses
36
// are responsible for calling set_num_residuals() at runtime.
38
#ifndef CERES_PUBLIC_SIZED_COST_FUNCTION_H_
39
#define CERES_PUBLIC_SIZED_COST_FUNCTION_H_
41
#include <glog/logging.h>
42
#include "ceres/types.h"
43
#include "ceres/cost_function.h"
47
template<int kNumResiduals,
48
int N0 = 0, int N1 = 0, int N2 = 0, int N3 = 0, int N4 = 0, int N5 = 0>
49
class SizedCostFunction : public CostFunction {
53
CHECK(kNumResiduals > 0 || kNumResiduals == DYNAMIC)
54
<< "Cost functions must have at least one residual block.";
57
<< "Cost functions must have at least one parameter block.";
58
CHECK((!N1 && !N2 && !N3 && !N4 && !N5) ||
59
((N1 > 0) && !N2 && !N3 && !N4 && !N5) ||
60
((N1 > 0) && (N2 > 0) && !N3 && !N4 && !N5) ||
61
((N1 > 0) && (N2 > 0) && (N3 > 0) && !N4 && !N5) ||
62
((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && !N5) ||
63
((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0)))
64
<< "Zero block cannot precede a non-zero block. Block sizes are "
65
<< "(ignore trailing 0s): " << N0 << ", " << N1 << ", " << N2 << ", "
66
<< N3 << ", " << N4 << ", " << N5;
68
set_num_residuals(kNumResiduals);
70
#define ADD_PARAMETER_BLOCK(N) \
71
if (N) mutable_parameter_block_sizes()->push_back(N);
72
ADD_PARAMETER_BLOCK(N0);
73
ADD_PARAMETER_BLOCK(N1);
74
ADD_PARAMETER_BLOCK(N2);
75
ADD_PARAMETER_BLOCK(N3);
76
ADD_PARAMETER_BLOCK(N4);
77
ADD_PARAMETER_BLOCK(N5);
78
#undef ADD_PARAMETER_BLOCK
81
virtual ~SizedCostFunction() { }
83
// Subclasses must implement Evaluate().
88
#endif // CERES_PUBLIC_SIZED_COST_FUNCTION_H_