ObjectDataMap.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Created by wrede on 22.04.16.
  3. //
  4. #include "ObjectDataMap.h"
  5. #include "../util/MyMath.h"
  6. namespace core
  7. {
  8. void ObjectDataMap::Print(std::ostream& os) const
  9. {
  10. os << "ObjectDataMap{frame:" << GetFrameIndex();
  11. for (auto it = value_weight_map_.begin(); it != value_weight_map_.end(); ++it)
  12. {
  13. os << "," << it->first
  14. << ":" << it->second.first
  15. << "*" << it->second.second;
  16. }
  17. os << "}";
  18. }
  19. ObjectDataMap::ObjectDataMap(size_t frame_index)
  20. : ObjectData(frame_index)
  21. {
  22. value_weight_map_ = std::unordered_map<std::string, std::pair<double, double>>();
  23. }
  24. ObjectDataMap::ObjectDataMap(size_t frame_index,
  25. std::vector<std::string> keys,
  26. std::vector<double> value_list)
  27. : ObjectDataMap(frame_index)
  28. {
  29. double weight = 1.0 / value_list.size();
  30. for (size_t i = 0; i < value_list.size() && i < keys.size(); ++i)
  31. {
  32. value_weight_map_[keys[i]] = std::make_pair(value_list[i], weight);
  33. }
  34. }
  35. ObjectDataMap::ObjectDataMap(size_t frame_index,
  36. std::vector<std::string> keys,
  37. std::vector<double> value_list,
  38. std::vector<double> weight_list)
  39. : ObjectDataMap(frame_index)
  40. {
  41. for (size_t i = 0;
  42. i < value_list.size() && i < keys.size() && i < weight_list.size();
  43. ++i)
  44. {
  45. value_weight_map_[keys[i]] = std::make_pair(value_list[i], weight_list[i]);
  46. }
  47. }
  48. ObjectDataMap::ObjectDataMap(size_t frame_index,
  49. std::vector<std::string> keys,
  50. std::vector<std::pair<double, double>> value_weight_list)
  51. : ObjectDataMap(frame_index)
  52. {
  53. for (size_t i = 0; i < value_weight_list.size() && i < keys.size(); ++i)
  54. {
  55. value_weight_map_[keys[i]] = value_weight_list[i];
  56. }
  57. }
  58. double ObjectDataMap::GetValue(std::string key)
  59. {
  60. return value_weight_map_[key].first;
  61. }
  62. double ObjectDataMap::GetWeight(std::string key)
  63. {
  64. return value_weight_map_[key].second;
  65. }
  66. void ObjectDataMap::Put(std::string key, double value,
  67. double weight)
  68. {
  69. value_weight_map_[key] = std::make_pair(value, weight);
  70. }
  71. void ObjectDataMap::Put(std::string key,
  72. std::pair<double, double> value_weight)
  73. {
  74. value_weight_map_[key] = value_weight;
  75. }
  76. double ObjectDataMap::CompareTo(ObjectDataPtr obj) const
  77. {
  78. ObjectDataMapPtr obj_map = std::static_pointer_cast<ObjectDataMap>(obj);
  79. double diff = 0.0;
  80. for (auto it = value_weight_map_.begin(); it != value_weight_map_.end(); ++it)
  81. {
  82. // |other_value - this_value| * this_weight;
  83. diff += fabs(obj_map->value_weight_map_[it->first].first - it->second.first)
  84. * it->second.second;
  85. }
  86. return diff;
  87. }
  88. ObjectDataPtr ObjectDataMap::Interpolate(ObjectDataPtr obj,
  89. double fraction) const
  90. {
  91. ObjectDataMapPtr obj_map = std::static_pointer_cast<ObjectDataMap>(obj);
  92. fraction = util::MyMath::Clamp(0.0, 1.0, fraction);
  93. double frame_index = util::MyMath::Lerp(GetFrameIndex(),
  94. obj_map->GetFrameIndex(),
  95. fraction);
  96. ObjectDataMapPtr obj_out(new ObjectDataMap(static_cast<size_t>(fabs(frame_index))));
  97. // Interpolate all values but leave the weights untouched
  98. for (auto iter = value_weight_map_.begin();
  99. iter != value_weight_map_.end();
  100. ++iter)
  101. {
  102. obj_out->Put(
  103. iter->first,
  104. util::MyMath::Lerp(iter->second.first,
  105. obj_map->value_weight_map_[iter->first].first,
  106. fraction),
  107. iter->second.second
  108. );
  109. }
  110. return obj_out;
  111. }
  112. void ObjectDataMap::Visualize(cv::Mat& image, cv::Scalar& color) const
  113. {
  114. ObjectData::Visualize(image, color);
  115. }
  116. }