Program Listing for File orientation_3d_stamped.hpp

Return to documentation for file (/tmp/ws/src/fuse/fuse_variables/include/fuse_variables/orientation_3d_stamped.hpp)

/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2019, 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_VARIABLES__ORIENTATION_3D_STAMPED_HPP_
#define FUSE_VARIABLES__ORIENTATION_3D_STAMPED_HPP_

#include <ceres/rotation.h>

#include <ostream>

#include <fuse_core/local_parameterization.hpp>
#include <fuse_core/serialization.hpp>
#include <fuse_core/util.hpp>
#include <fuse_core/uuid.hpp>
#include <fuse_core/variable.hpp>
#include <fuse_variables/fixed_size_variable.hpp>
#include <fuse_variables/stamped.hpp>

#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>

#include <rclcpp/time.hpp>

namespace fuse_variables
{

class Orientation3DLocalParameterization : public fuse_core::LocalParameterization
{
public:
  template<typename T> inline
  static void QuaternionInverse(const T in[4], T out[4])
  {
    out[0] = in[0];
    out[1] = -in[1];
    out[2] = -in[2];
    out[3] = -in[3];
  }

  int GlobalSize() const override
  {
    return 4;
  }

  int LocalSize() const override
  {
    return 3;
  }

  bool Plus(
    const double * x,
    const double * delta,
    double * x_plus_delta) const override
  {
    double q_delta[4];
    ceres::AngleAxisToQuaternion(delta, q_delta);
    ceres::QuaternionProduct(x, q_delta, x_plus_delta);
    return true;
  }

  bool ComputeJacobian(
    const double * x,
    double * jacobian) const override
  {
    double x0 = x[0] / 2;
    double x1 = x[1] / 2;
    double x2 = x[2] / 2;
    double x3 = x[3] / 2;
    /* *INDENT-OFF* */
    jacobian[0] = -x1; jacobian[1]  = -x2; jacobian[2]  = -x3;  // NOLINT
    jacobian[3] =  x0; jacobian[4]  = -x3; jacobian[5]  =  x2;  // NOLINT
    jacobian[6] =  x3; jacobian[7]  =  x0; jacobian[8]  = -x1;  // NOLINT
    jacobian[9] = -x2; jacobian[10] =  x1; jacobian[11] =  x0;  // NOLINT
    /* *INDENT-ON* */
    return true;
  }

  bool Minus(
    const double * x1,
    const double * x2,
    double * delta) const override
  {
    double x1_inverse[4];
    QuaternionInverse(x1, x1_inverse);
    double q_delta[4];
    ceres::QuaternionProduct(x1_inverse, x2, q_delta);
    ceres::QuaternionToAngleAxis(q_delta, delta);
    return true;
  }

  bool ComputeMinusJacobian(
    const double * x,
    double * jacobian) const override
  {
    double x0 = x[0] * 2;
    double x1 = x[1] * 2;
    double x2 = x[2] * 2;
    double x3 = x[3] * 2;
    /* *INDENT-OFF* */
    jacobian[0] = -x1; jacobian[1] =  x0; jacobian[2]  = x3;  jacobian[3]  = -x2;  // NOLINT
    jacobian[4] = -x2; jacobian[5] = -x3; jacobian[6]  = x0;  jacobian[7]  =  x1;  // NOLINT
    jacobian[8] = -x3; jacobian[9] =  x2; jacobian[10] = -x1; jacobian[11] =  x0;  // NOLINT
    /* *INDENT-ON* */
    return true;
  }

private:
  // Allow Boost Serialization access to private methods
  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive & archive, const unsigned int /* version */)
  {
    archive & boost::serialization::base_object<fuse_core::LocalParameterization>(*this);
  }
};

class Orientation3DStamped : public FixedSizeVariable<4>, public Stamped
{
public:
  FUSE_VARIABLE_DEFINITIONS(Orientation3DStamped)


  enum : size_t
  {
    W = 0,
    X = 1,
    Y = 2,
    Z = 3,
  };

  enum class Euler : size_t
  {
    ROLL = 4,
    PITCH = 5,
    YAW = 6
  };

  Orientation3DStamped() = default;

  explicit Orientation3DStamped(
    const rclcpp::Time & stamp,
    const fuse_core::UUID & device_id = fuse_core::uuid::NIL);

  double & w() {return data_[W];}

  const double & w() const {return data_[W];}

  double & x() {return data_[X];}

  const double & x() const {return data_[X];}

  double & y() {return data_[Y];}

  const double & y() const {return data_[Y];}

  double & z() {return data_[Z];}

  const double & z() const {return data_[Z];}

  double roll() {return fuse_core::getRoll(w(), x(), y(), z());}

  double pitch() {return fuse_core::getPitch(w(), x(), y(), z());}

  double yaw() {return fuse_core::getYaw(w(), x(), y(), z());}

  void print(std::ostream & stream = std::cout) const override;

  size_t localSize() const override {return 3u;}

  fuse_core::LocalParameterization * localParameterization() const override;

private:
  // Allow Boost Serialization access to private methods
  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive & archive, const unsigned int /* version */)
  {
    archive & boost::serialization::base_object<FixedSizeVariable<SIZE>>(*this);
    archive & boost::serialization::base_object<Stamped>(*this);
  }
};

}  // namespace fuse_variables

BOOST_CLASS_EXPORT_KEY(fuse_variables::Orientation3DLocalParameterization);
BOOST_CLASS_EXPORT_KEY(fuse_variables::Orientation3DStamped);

#endif  // FUSE_VARIABLES__ORIENTATION_3D_STAMPED_HPP_