ObjectData2D.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // Created by wrede on 02.06.16.
  3. //
  4. #include "ObjectData2D.h"
  5. #include "../util/MyMath.h"
  6. namespace core
  7. {
  8. ObjectData2D::ObjectData2D(size_t frame_index, cv::Point2d position)
  9. : ObjectData(frame_index),
  10. position_(position),
  11. temporal_weight_(1.0),
  12. spatial_weight_(1.0)
  13. {
  14. }
  15. void ObjectData2D::SetTemporalWeight(double weight)
  16. {
  17. temporal_weight_ = weight;
  18. }
  19. void ObjectData2D::SetSpatialWeight(double weight)
  20. {
  21. spatial_weight_ = weight;
  22. }
  23. cv::Point2d ObjectData2D::GetPosition() const
  24. {
  25. return position_;
  26. }
  27. double ObjectData2D::GetTemporalWeight() const
  28. {
  29. return temporal_weight_;
  30. }
  31. double ObjectData2D::GetSpatialWeight() const
  32. {
  33. return spatial_weight_;
  34. }
  35. double ObjectData2D::CompareTo(ObjectDataPtr obj) const
  36. {
  37. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  38. double d_temp = obj_2d->GetFrameIndex() - GetFrameIndex();
  39. double d_spat = util::MyMath::EuclideanDistance(position_, obj_2d->position_);
  40. return d_temp * temporal_weight_ + d_spat * spatial_weight_;
  41. }
  42. ObjectDataPtr ObjectData2D::Interpolate(ObjectDataPtr obj,
  43. double fraction) const
  44. {
  45. ObjectDataPtr obj_in = ObjectData::Interpolate(obj, fraction);
  46. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  47. double x = util::MyMath::Lerp(position_.x, obj_2d->position_.x, fraction);
  48. double y = util::MyMath::Lerp(position_.y, obj_2d->position_.y, fraction);
  49. ObjectData2DPtr obj_out(
  50. new ObjectData2D(obj_in->GetFrameIndex(), cv::Point2d(x, y)));
  51. return obj_out;
  52. }
  53. void ObjectData2D::Print(std::ostream& os) const
  54. {
  55. os << "ObjectData2D{"
  56. << "frame: " << GetFrameIndex() << ","
  57. << "x: " << position_.x << ","
  58. << "y: " << position_.y << "}";
  59. }
  60. void ObjectData2D::Visualize(cv::Mat& image, cv::Scalar& color) const
  61. {
  62. double x = position_.x * image.cols;
  63. double y = position_.y * image.rows;
  64. int r = (int) (0.005 * (image.rows + image.cols) * 0.5);
  65. cv::circle(image, cv::Point2d(x, y), r, color);
  66. }
  67. }