ObjectData2D.cpp 3.8 KB

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