Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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/ |
| 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: keir@google.com (Keir Mierle) |
| 30 | // |
| 31 | // A convenience class for cost functions which are statically sized. |
| 32 | // Compared to the dynamically-sized base class, this reduces boilerplate. |
Keir Mierle | fdeb577 | 2012-05-09 07:38:07 -0700 | [diff] [blame] | 33 | // |
| 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. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 37 | |
| 38 | #ifndef CERES_PUBLIC_SIZED_COST_FUNCTION_H_ |
| 39 | #define CERES_PUBLIC_SIZED_COST_FUNCTION_H_ |
| 40 | |
| 41 | #include <glog/logging.h> |
Keir Mierle | fdeb577 | 2012-05-09 07:38:07 -0700 | [diff] [blame] | 42 | #include "ceres/types.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 43 | #include "ceres/cost_function.h" |
| 44 | |
| 45 | namespace ceres { |
| 46 | |
| 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 { |
| 50 | public: |
| 51 | SizedCostFunction() { |
| 52 | // Sanity checking. |
Keir Mierle | fdeb577 | 2012-05-09 07:38:07 -0700 | [diff] [blame] | 53 | CHECK(kNumResiduals > 0 || kNumResiduals == DYNAMIC) |
| 54 | << "Cost functions must have at least one residual block."; |
| 55 | |
| 56 | CHECK_GT(N0, 0) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 57 | << "Cost functions must have at least one parameter block."; |
Keir Mierle | fdeb577 | 2012-05-09 07:38:07 -0700 | [diff] [blame] | 58 | CHECK((!N1 && !N2 && !N3 && !N4 && !N5) || |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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; |
| 67 | |
| 68 | set_num_residuals(kNumResiduals); |
| 69 | |
| 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 |
| 79 | } |
| 80 | |
| 81 | virtual ~SizedCostFunction() { } |
| 82 | |
| 83 | // Subclasses must implement Evaluate(). |
| 84 | }; |
| 85 | |
| 86 | } // namespace ceres |
| 87 | |
| 88 | #endif // CERES_PUBLIC_SIZED_COST_FUNCTION_H_ |