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 | |
Keir Mierle | fdeb577 | 2012-05-09 07:38:07 -0700 | [diff] [blame] | 41 | #include "ceres/types.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 42 | #include "ceres/cost_function.h" |
Sameer Agarwal | a1eaa26 | 2013-05-09 10:02:24 -0700 | [diff] [blame] | 43 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 44 | |
| 45 | namespace ceres { |
| 46 | |
| 47 | template<int kNumResiduals, |
Keir Mierle | f1e67cc | 2012-10-19 10:50:02 -0700 | [diff] [blame] | 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> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 50 | class SizedCostFunction : public CostFunction { |
| 51 | public: |
| 52 | SizedCostFunction() { |
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 | |
Keir Mierle | f1e67cc | 2012-10-19 10:50:02 -0700 | [diff] [blame] | 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) || |
| 60 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && !N4 && !N5 && !N6 && !N7 && !N8 && !N9) || |
| 61 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && !N5 && !N6 && !N7 && !N8 && !N9) || |
| 62 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && !N6 && !N7 && !N8 && !N9) || |
| 63 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && !N7 && !N8 && !N9) || |
| 64 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && !N8 && !N9) || |
| 65 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && (N8 > 0) && !N9) || |
| 66 | ((N1 > 0) && (N2 > 0) && (N3 > 0) && (N4 > 0) && (N5 > 0) && (N6 > 0) && (N7 > 0) && (N8 > 0) && (N9 > 0))) |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 67 | << "Zero block cannot precede a non-zero block. Block sizes are " |
| 68 | << "(ignore trailing 0s): " << N0 << ", " << N1 << ", " << N2 << ", " |
Keir Mierle | f1e67cc | 2012-10-19 10:50:02 -0700 | [diff] [blame] | 69 | << N3 << ", " << N4 << ", " << N5 << ", " << N6 << ", " << N7 << ", " |
| 70 | << N8 << ", " << N9; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 71 | |
| 72 | set_num_residuals(kNumResiduals); |
| 73 | |
| 74 | #define ADD_PARAMETER_BLOCK(N) \ |
| 75 | if (N) mutable_parameter_block_sizes()->push_back(N); |
| 76 | ADD_PARAMETER_BLOCK(N0); |
| 77 | ADD_PARAMETER_BLOCK(N1); |
| 78 | ADD_PARAMETER_BLOCK(N2); |
| 79 | ADD_PARAMETER_BLOCK(N3); |
| 80 | ADD_PARAMETER_BLOCK(N4); |
| 81 | ADD_PARAMETER_BLOCK(N5); |
Keir Mierle | f1e67cc | 2012-10-19 10:50:02 -0700 | [diff] [blame] | 82 | ADD_PARAMETER_BLOCK(N6); |
| 83 | ADD_PARAMETER_BLOCK(N7); |
| 84 | ADD_PARAMETER_BLOCK(N8); |
| 85 | ADD_PARAMETER_BLOCK(N9); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 86 | #undef ADD_PARAMETER_BLOCK |
| 87 | } |
| 88 | |
| 89 | virtual ~SizedCostFunction() { } |
| 90 | |
| 91 | // Subclasses must implement Evaluate(). |
| 92 | }; |
| 93 | |
| 94 | } // namespace ceres |
| 95 | |
| 96 | #endif // CERES_PUBLIC_SIZED_COST_FUNCTION_H_ |