1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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 "ceres/types.h"
42 #include "ceres/cost_function.h"
43 #include "glog/logging.h"
47 template<int kNumResiduals,
48 int N0 = 0, int N1 = 0, int N2 = 0, int N3 = 0, int N4 = 0,
49 int N5 = 0, int N6 = 0, int N7 = 0, int N8 = 0, int N9 = 0>
50 class SizedCostFunction : public CostFunction {
53 CHECK(kNumResiduals > 0 || kNumResiduals == DYNAMIC)
54 << "Cost functions must have at least one residual block.";
56 // This block breaks the 80 column rule to keep it somewhat readable.
57 CHECK((!N1 && !N2 && !N3 && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) ||
58 ((N1 > 0) && !N2 && !N3 && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) ||
59 ((N1 > 0) && (N2 > 0) && !N3 && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) || // NOLINT
60 ((N1 > 0) && (N2 > 0) && (N3 > 0) && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) || // NOLINT
61 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && !N5 && !N6 && !N7 && !N8 && !N9) || // NOLINT
62 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && !N6 && !N7 && !N8 && !N9) || // NOLINT
63 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && !N7 && !N8 && !N9) || // NOLINT
64 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && !N8 && !N9) || // NOLINT
65 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && (N8 > 0) && !N9) || // NOLINT
66 ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && (N8 > 0) && (N9 > 0))) // NOLINT
67 << "Zero block cannot precede a non-zero block. Block sizes are "
68 << "(ignore trailing 0s): " << N0 << ", " << N1 << ", " << N2 << ", "
69 << N3 << ", " << N4 << ", " << N5 << ", " << N6 << ", " << N7 << ", "
72 set_num_residuals(kNumResiduals);
74 #define CERES_ADD_PARAMETER_BLOCK(N) \
75 if (N) mutable_parameter_block_sizes()->push_back(N);
76 CERES_ADD_PARAMETER_BLOCK(N0);
77 CERES_ADD_PARAMETER_BLOCK(N1);
78 CERES_ADD_PARAMETER_BLOCK(N2);
79 CERES_ADD_PARAMETER_BLOCK(N3);
80 CERES_ADD_PARAMETER_BLOCK(N4);
81 CERES_ADD_PARAMETER_BLOCK(N5);
82 CERES_ADD_PARAMETER_BLOCK(N6);
83 CERES_ADD_PARAMETER_BLOCK(N7);
84 CERES_ADD_PARAMETER_BLOCK(N8);
85 CERES_ADD_PARAMETER_BLOCK(N9);
86 #undef CERES_ADD_PARAMETER_BLOCK
89 virtual ~SizedCostFunction() { }
91 // Subclasses must implement Evaluate().
96 #endif // CERES_PUBLIC_SIZED_COST_FUNCTION_H_