ObjectData2D.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Created by wrede on 02.06.16.
  3. //
  4. #include "ObjectData2D.h"
  5. #include "../util/MyMath.h"
  6. namespace core
  7. {
  8. const std::string ObjectData2D::CONSTRAINT_DISTANCE_EUCLID = "distance_euclid";
  9. ObjectData2D::ObjectData2D(size_t frame_index, cv::Point2d position)
  10. : ObjectData(frame_index),
  11. position_(position),
  12. temporal_weight_(1.0),
  13. spatial_weight_(1.0)
  14. {
  15. }
  16. void ObjectData2D::SetTemporalWeight(double weight)
  17. {
  18. temporal_weight_ = weight;
  19. }
  20. void ObjectData2D::SetSpatialWeight(double weight)
  21. {
  22. spatial_weight_ = weight;
  23. }
  24. cv::Point2d ObjectData2D::GetPosition() const
  25. {
  26. return position_;
  27. }
  28. double ObjectData2D::GetTemporalWeight() const
  29. {
  30. return temporal_weight_;
  31. }
  32. double ObjectData2D::GetSpatialWeight() const
  33. {
  34. return spatial_weight_;
  35. }
  36. double ObjectData2D::CompareTo(ObjectDataPtr obj) const
  37. {
  38. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  39. double d_temp = obj_2d->GetFrameIndex() - GetFrameIndex();
  40. double d_spat = util::MyMath::EuclideanDistance(position_, obj_2d->position_);
  41. return d_temp * temporal_weight_ + d_spat * spatial_weight_;
  42. }
  43. bool ObjectData2D::IsWithinConstraints(ObjectDataPtr obj,
  44. std::unordered_map<std::string, double> & constraints)
  45. const
  46. {
  47. if (!ObjectData::IsWithinConstraints(obj, constraints))
  48. return false;
  49. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  50. if (constraints.count(CONSTRAINT_DISTANCE_EUCLID) > 0) {
  51. double distance_euclid = util::MyMath::EuclideanDistance(position_, obj_2d->position_);
  52. if (distance_euclid > constraints[CONSTRAINT_DISTANCE_EUCLID])
  53. return false;
  54. }
  55. return true;
  56. }
  57. ObjectDataPtr ObjectData2D::Interpolate(ObjectDataPtr obj,
  58. double fraction) const
  59. {
  60. ObjectDataPtr obj_in = ObjectData::Interpolate(obj, fraction);
  61. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  62. double x = util::MyMath::Lerp(position_.x, obj_2d->position_.x, fraction);
  63. double y = util::MyMath::Lerp(position_.y, obj_2d->position_.y, fraction);
  64. ObjectData2DPtr obj_out(
  65. new ObjectData2D(obj_in->GetFrameIndex(), cv::Point2d(x, y)));
  66. return obj_out;
  67. }
  68. void ObjectData2D::Print(std::ostream& os) const
  69. {
  70. os << "ObjectData2D{"
  71. << "frame: " << GetFrameIndex() << ","
  72. << "x: " << position_.x << ","
  73. << "y: " << position_.y << "}";
  74. }
  75. void ObjectData2D::Visualize(cv::Mat& image, cv::Scalar& color) const
  76. {
  77. double x = position_.x;
  78. double y = position_.y;
  79. int r = (int) (0.005 * (image.rows + image.cols) * 0.5);
  80. cv::circle(image, cv::Point2d(x, y), r, color);
  81. }
  82. std::string ObjectData2D::ToString(char delimiter) const
  83. {
  84. return ObjectData::ToString(delimiter) + delimiter +
  85. std::to_string(position_.x) + delimiter + std::to_string(position_.y);
  86. }
  87. }