Program Listing for File range_cost_functor.hpp
↰ Return to documentation for file (include/fuse_tutorials/range_cost_functor.hpp
)
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2021, Locus Robotics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FUSE_TUTORIALS__RANGE_COST_FUNCTOR_HPP_
#define FUSE_TUTORIALS__RANGE_COST_FUNCTOR_HPP_
#include <ceres/jet.h>
namespace fuse_tutorials
{
class RangeCostFunctor
{
public:
RangeCostFunctor(const double z, const double sigma)
: sigma_(sigma), z_(z) {}
template<typename T>
bool operator()(
const T * const robot_position, const T * const beacon_position,
T * residuals) const
{
// Implement our mathematic measurement model:
// z_hat = sqrt( (x_robot - x_beacon)^2 + (y_robot - y_beacon)^2 )
// error = (z - z_hat) / sigma
// Unfortunately there is a problem when computing the derivatives. The derivative is undefined
// when the radicand is zero. To have a well-defined cost function, we implement an alternative
// cost equation for that case. Thankfully this does not cause any issues with Ceres Solver's
// auto-diff system. See http://ceres-solver.org/automatic_derivatives.html#pitfalls
auto dx = robot_position[0] - beacon_position[0];
auto dy = robot_position[1] - beacon_position[1];
auto norm_sq = dx * dx + dy * dy;
if (norm_sq > 0.0) {
auto z_hat = ceres::sqrt(norm_sq);
residuals[0] = (T(z_) - z_hat) / T(sigma_);
} else {
residuals[0] = T(z_) / T(sigma_);
}
return true;
}
private:
double sigma_;
double z_;
};
} // namespace fuse_tutorials
#endif // FUSE_TUTORIALS__RANGE_COST_FUNCTOR_HPP_