utils.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "dl_api.h"
  2. #define EUCLIDEAN_DISTANCE(x1, y1, x2, y2) (sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2)))
  3. // >0 -> Clockwise orientation || <0 -> Counterclockwise orientation || =0 -> Collinear orientation
  4. #define POINT_ON_SIDE(p1, p2, p3) ((p2.y - p1.y) * (p3.x - p2.x) - (p2.x - p1.x) * (p3.y - p2.y))
  5. #define CALC_ANGLE(p1, p2, p3) (std::acos(((p2.x - p1.x) * (p3.x - p1.x) + (p2.y - p1.y) * (p3.y - p1.y)) / (std::sqrt((std::pow(p2.x - p1.x, 2) + std::pow(p2.y - p1.y, 2))) * std::sqrt((std::pow(p3.x - p1.x, 2) + std::pow(p3.y - p1.y, 2))))) * 180 / M_PI)
  6. #define LINE_ANGLE(point1, point2) (std::atan2((point2.y) - (point1.y), (point2.x) - (point1.x)) * 180 / M_PI)
  7. #define IOU(rect1, rect2) \
  8. ({ \
  9. float xmin1 = rect1.x; \
  10. float ymin1 = rect1.y; \
  11. float xmax1 = rect1.x + rect1.w; \
  12. float ymax1 = rect1.y + rect1.h; \
  13. float xmin2 = rect2.x; \
  14. float ymin2 = rect2.y; \
  15. float xmax2 = rect2.x + rect2.w; \
  16. float ymax2 = rect2.y + rect2.h; \
  17. float interArea = \
  18. (std::min(xmax1, xmax2) - std::max(xmin1, xmin2)) * \
  19. (std::min(ymax1, ymax2) - std::max(ymin1, ymin2)); \
  20. float rect1Area = rect1.w * rect1.h; \
  21. float rect2Area = rect2.w * rect2.h; \
  22. float iou = interArea / (rect1Area + rect2Area - interArea); \
  23. iou; \
  24. })
  25. static bool isPointInRect(sampleRunJoint_POINT_S p, sampleRunJoint_RECT_S r) {
  26. return (p.x >= r.x && p.x <= r.x + r.w && p.y >= r.y && p.y <= r.y + r.h);
  27. }
  28. static bool isLineIntersectRect(sampleRunJoint_POINT_S l1, sampleRunJoint_POINT_S l2, sampleRunJoint_RECT_S r) {
  29. if (isPointInRect(l1, r) || isPointInRect(l2, r)) {
  30. return true;
  31. }
  32. if (l1.x > r.x + r.w && l2.x > r.x + r.w) {
  33. return false;
  34. }
  35. if (l1.x < r.x && l2.x < r.x) {
  36. return false;
  37. }
  38. if (l1.y > r.y + r.h && l2.y > r.y + r.h) {
  39. return false;
  40. }
  41. if (l1.y < r.y && l2.y < r.y) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. static sampleRunJoint_POINT_S calculateCentroid(const sampleRunJoint_POINT_S& p1, const sampleRunJoint_POINT_S& p2) {
  47. sampleRunJoint_POINT_S centroid;
  48. centroid.x = (p1.x + p2.x) / 2;
  49. centroid.y = (p1.y + p2.y) / 2;
  50. return centroid;
  51. }