10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: wjr@google.com (William Rucklidge)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This file contains a cost function that can apply a transformation to
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// each residual value before they are square-summed.
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <vector>
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/cost_function.h"
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/scoped_ptr.h"
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h"
4279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/disable_warnings.h"
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This class allows you to apply different conditioning to the residual
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// values of a wrapped cost function. An example where this is useful is
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// where you have an existing cost function that produces N values, but you
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// want the total cost to be something other than just the sum of these
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// squared values - maybe you want to apply a different scaling to some
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// values, to change their contribution to the cost.
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Usage:
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   // my_cost_function produces N residuals
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   CostFunction* my_cost_function = ...
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   CHECK_EQ(N, my_cost_function->num_residuals());
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   vector<CostFunction*> conditioners;
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   // Make N 1x1 cost functions (1 parameter, 1 residual)
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   CostFunction* f_1 = ...
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   conditioners.push_back(f_1);
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   ...
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   CostFunction* f_N = ...
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   conditioners.push_back(f_N);
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   ConditionedCostFunction* ccf =
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//     new ConditionedCostFunction(my_cost_function, conditioners);
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Now ccf's residual i (i=0..N-1) will be passed though the i'th conditioner.
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   ccf_residual[i] = f_i(my_cost_function_residual[i])
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and the Jacobian will be affected appropriately.
7479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass CERES_EXPORT ConditionedCostFunction : public CostFunction {
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Builds a cost function based on a wrapped cost function, and a
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // per-residual conditioner. Takes ownership of all of the wrapped cost
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // functions, or not, depending on the ownership parameter. Conditioners
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // may be NULL, in which case the corresponding residual is not modified.
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  ConditionedCostFunction(CostFunction* wrapped_cost_function,
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                          const vector<CostFunction*>& conditioners,
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                          Ownership ownership);
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual ~ConditionedCostFunction();
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual bool Evaluate(double const* const* parameters,
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                        double* residuals,
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                        double** jacobians) const;
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private:
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  internal::scoped_ptr<CostFunction> wrapped_cost_function_;
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<CostFunction*> conditioners_;
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Ownership ownership_;
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
9779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/reenable_warnings.h"
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_PUBLIC_CONDITIONED_COST_FUNCTION_H_
100