Program Listing for File sample.hpp

Return to documentation for file (include/tuw_geometry/sample.hpp)

#ifndef TUW_GEOMETRY__SAMPLE_HPP
#define TUW_GEOMETRY__SAMPLE_HPP

#include <tuw_geometry/pose2d.hpp>

namespace tuw
{
template<class T>
class Sample;
template<class T>
using SamplePtr = std::shared_ptr<Sample<T>>;
template<class T>
using SampleConstPtr = std::shared_ptr<Sample<T> const>;

template<class T>
class Sample : public T
{
  double weight_;
  unsigned int idx_;

public:
  Sample()
  : T(), weight_(0), idx_(0) {}
  Sample(const Sample<T> & s)
  : T(s), weight_(s.weight_) {}

  Sample(const T & o, double weight = 0)
  : T(o), weight_(weight) {}

  void set(const T & o, double weight = 0) {T::set(o), weight_ = weight;}

  void set(const T & s) {set(s, s.weight_);}

  void set(const SamplePtr<T> & s) {set(*s, s->weight_);}

  const T & get() const {return *this;}

  T & get() {return *this;}

  const unsigned int & idx() const {return idx_;}
  unsigned int & idx() {return idx_;}
  const double & weight() const {return weight_;}
  double & weight() {return weight_;}
  friend std::ostream & operator<<(std::ostream & os, const Sample<T> & o)
  {
    os << "[" << (Pose2D &)o << ", " << o.weight() << "]";
    return os;
  }

  static bool greater(const SamplePtr<T> & a, const SamplePtr<T> & b)
  {
    return a->weight() > b->weight();
  }

  static bool smaller(const SamplePtr<T> & a, const SamplePtr<T> & b)
  {
    return a->weight() < b->weight();
  }
};

class Pose2D;
using SamplePose2D = Sample<Pose2D>;
using SamplePose2DPtr = std::shared_ptr<SamplePose2D>;
using SamplePose2DConstPtr = std::shared_ptr<SamplePose2D const>;
}  // namespace tuw
#endif  // TUW_GEOMETRY__SAMPLE_HPP