Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / include / ceres / conditioned_cost_function.h
1 // 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: wjr@google.com (William Rucklidge)
30 //
31 // This file contains a cost function that can apply a transformation to
32 // each residual value before they are square-summed.
33
34 #ifndef CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
35 #define CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
36
37 #include <vector>
38
39 #include "ceres/cost_function.h"
40 #include "ceres/internal/scoped_ptr.h"
41 #include "ceres/types.h"
42 #include "ceres/internal/disable_warnings.h"
43
44 namespace ceres {
45
46 // This class allows you to apply different conditioning to the residual
47 // values of a wrapped cost function. An example where this is useful is
48 // where you have an existing cost function that produces N values, but you
49 // want the total cost to be something other than just the sum of these
50 // squared values - maybe you want to apply a different scaling to some
51 // values, to change their contribution to the cost.
52 //
53 // Usage:
54 //
55 //   // my_cost_function produces N residuals
56 //   CostFunction* my_cost_function = ...
57 //   CHECK_EQ(N, my_cost_function->num_residuals());
58 //   vector<CostFunction*> conditioners;
59 //
60 //   // Make N 1x1 cost functions (1 parameter, 1 residual)
61 //   CostFunction* f_1 = ...
62 //   conditioners.push_back(f_1);
63 //   ...
64 //   CostFunction* f_N = ...
65 //   conditioners.push_back(f_N);
66 //   ConditionedCostFunction* ccf =
67 //     new ConditionedCostFunction(my_cost_function, conditioners);
68 //
69 // Now ccf's residual i (i=0..N-1) will be passed though the i'th conditioner.
70 //
71 //   ccf_residual[i] = f_i(my_cost_function_residual[i])
72 //
73 // and the Jacobian will be affected appropriately.
74 class CERES_EXPORT ConditionedCostFunction : public CostFunction {
75  public:
76   // Builds a cost function based on a wrapped cost function, and a
77   // per-residual conditioner. Takes ownership of all of the wrapped cost
78   // functions, or not, depending on the ownership parameter. Conditioners
79   // may be NULL, in which case the corresponding residual is not modified.
80   ConditionedCostFunction(CostFunction* wrapped_cost_function,
81                           const std::vector<CostFunction*>& conditioners,
82                           Ownership ownership);
83   virtual ~ConditionedCostFunction();
84
85   virtual bool Evaluate(double const* const* parameters,
86                         double* residuals,
87                         double** jacobians) const;
88
89  private:
90   internal::scoped_ptr<CostFunction> wrapped_cost_function_;
91   std::vector<CostFunction*> conditioners_;
92   Ownership ownership_;
93 };
94
95 }  // namespace ceres
96
97 #include "ceres/internal/reenable_warnings.h"
98
99 #endif  // CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_