iou3d_cpu.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. 3D Rotated IoU Calculation (CPU)
  16. Written by Shaoshuai Shi
  17. All Rights Reserved 2020.
  18. */
  19. #include "iou3d_cpu.h"
  20. #include <cuda.h>
  21. #include <cuda_runtime_api.h>
  22. #include <math.h>
  23. #include <paddle/extension.h>
  24. #include <stdio.h>
  25. #include <vector>
  26. inline float min(float a, float b) { return a > b ? b : a; }
  27. inline float max(float a, float b) { return a > b ? a : b; }
  28. const float EPS = 1e-8;
  29. struct Point {
  30. float x, y;
  31. __device__ Point() {}
  32. __device__ Point(double _x, double _y) { x = _x, y = _y; }
  33. __device__ void set(float _x, float _y) {
  34. x = _x;
  35. y = _y;
  36. }
  37. __device__ Point operator+(const Point &b) const {
  38. return Point(x + b.x, y + b.y);
  39. }
  40. __device__ Point operator-(const Point &b) const {
  41. return Point(x - b.x, y - b.y);
  42. }
  43. };
  44. inline float cross(const Point &a, const Point &b) {
  45. return a.x * b.y - a.y * b.x;
  46. }
  47. inline float cross(const Point &p1, const Point &p2, const Point &p0) {
  48. return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
  49. }
  50. inline int check_rect_cross(const Point &p1, const Point &p2, const Point &q1,
  51. const Point &q2) {
  52. int ret = min(p1.x, p2.x) <= max(q1.x, q2.x) &&
  53. min(q1.x, q2.x) <= max(p1.x, p2.x) &&
  54. min(p1.y, p2.y) <= max(q1.y, q2.y) &&
  55. min(q1.y, q2.y) <= max(p1.y, p2.y);
  56. return ret;
  57. }
  58. inline int check_in_box2d(const float *box, const Point &p) {
  59. // params: (7) [x, y, z, dx, dy, dz, heading]
  60. const float MARGIN = 1e-2;
  61. float center_x = box[0], center_y = box[1];
  62. float angle_cos = cos(-box[6]),
  63. angle_sin =
  64. sin(-box[6]); // rotate the point in the opposite direction of box
  65. float rot_x = (p.x - center_x) * angle_cos + (p.y - center_y) * (-angle_sin);
  66. float rot_y = (p.x - center_x) * angle_sin + (p.y - center_y) * angle_cos;
  67. return (fabs(rot_x) < box[3] / 2 + MARGIN &&
  68. fabs(rot_y) < box[4] / 2 + MARGIN);
  69. }
  70. inline int intersection(const Point &p1, const Point &p0, const Point &q1,
  71. const Point &q0, Point &ans) {
  72. // fast exclusion
  73. if (check_rect_cross(p0, p1, q0, q1) == 0)
  74. return 0;
  75. // check cross standing
  76. float s1 = cross(q0, p1, p0);
  77. float s2 = cross(p1, q1, p0);
  78. float s3 = cross(p0, q1, q0);
  79. float s4 = cross(q1, p1, q0);
  80. if (!(s1 * s2 > 0 && s3 * s4 > 0))
  81. return 0;
  82. // calculate intersection of two lines
  83. float s5 = cross(q1, p1, p0);
  84. if (fabs(s5 - s1) > EPS) {
  85. ans.x = (s5 * q0.x - s1 * q1.x) / (s5 - s1);
  86. ans.y = (s5 * q0.y - s1 * q1.y) / (s5 - s1);
  87. } else {
  88. float a0 = p0.y - p1.y, b0 = p1.x - p0.x, c0 = p0.x * p1.y - p1.x * p0.y;
  89. float a1 = q0.y - q1.y, b1 = q1.x - q0.x, c1 = q0.x * q1.y - q1.x * q0.y;
  90. float D = a0 * b1 - a1 * b0;
  91. ans.x = (b0 * c1 - b1 * c0) / D;
  92. ans.y = (a1 * c0 - a0 * c1) / D;
  93. }
  94. return 1;
  95. }
  96. inline void rotate_around_center(const Point &center, const float angle_cos,
  97. const float angle_sin, Point &p) {
  98. float new_x =
  99. (p.x - center.x) * angle_cos + (p.y - center.y) * (-angle_sin) + center.x;
  100. float new_y =
  101. (p.x - center.x) * angle_sin + (p.y - center.y) * angle_cos + center.y;
  102. p.set(new_x, new_y);
  103. }
  104. inline int point_cmp(const Point &a, const Point &b, const Point &center) {
  105. return atan2(a.y - center.y, a.x - center.x) >
  106. atan2(b.y - center.y, b.x - center.x);
  107. }
  108. inline float box_overlap(const float *box_a, const float *box_b) {
  109. // params: box_a (7) [x, y, z, dx, dy, dz, heading]
  110. // params: box_b (7) [x, y, z, dx, dy, dz, heading]
  111. // float a_x1 = box_a[0], a_y1 = box_a[1], a_x2 = box_a[2], a_y2 =
  112. // box_a[3], a_angle = box_a[4];
  113. // float b_x1 = box_b[0], b_y1 = box_b[1], b_x2 = box_b[2], b_y2 =
  114. // box_b[3], b_angle = box_b[4];
  115. float a_angle = box_a[6], b_angle = box_b[6];
  116. float a_dx_half = box_a[3] / 2, b_dx_half = box_b[3] / 2,
  117. a_dy_half = box_a[4] / 2, b_dy_half = box_b[4] / 2;
  118. float a_x1 = box_a[0] - a_dx_half, a_y1 = box_a[1] - a_dy_half;
  119. float a_x2 = box_a[0] + a_dx_half, a_y2 = box_a[1] + a_dy_half;
  120. float b_x1 = box_b[0] - b_dx_half, b_y1 = box_b[1] - b_dy_half;
  121. float b_x2 = box_b[0] + b_dx_half, b_y2 = box_b[1] + b_dy_half;
  122. Point center_a(box_a[0], box_a[1]);
  123. Point center_b(box_b[0], box_b[1]);
  124. Point box_a_corners[5];
  125. box_a_corners[0].set(a_x1, a_y1);
  126. box_a_corners[1].set(a_x2, a_y1);
  127. box_a_corners[2].set(a_x2, a_y2);
  128. box_a_corners[3].set(a_x1, a_y2);
  129. Point box_b_corners[5];
  130. box_b_corners[0].set(b_x1, b_y1);
  131. box_b_corners[1].set(b_x2, b_y1);
  132. box_b_corners[2].set(b_x2, b_y2);
  133. box_b_corners[3].set(b_x1, b_y2);
  134. // get oriented corners
  135. float a_angle_cos = cos(a_angle), a_angle_sin = sin(a_angle);
  136. float b_angle_cos = cos(b_angle), b_angle_sin = sin(b_angle);
  137. for (int k = 0; k < 4; k++) {
  138. rotate_around_center(center_a, a_angle_cos, a_angle_sin, box_a_corners[k]);
  139. rotate_around_center(center_b, b_angle_cos, b_angle_sin, box_b_corners[k]);
  140. }
  141. box_a_corners[4] = box_a_corners[0];
  142. box_b_corners[4] = box_b_corners[0];
  143. // get intersection of lines
  144. Point cross_points[16];
  145. Point poly_center;
  146. int cnt = 0, flag = 0;
  147. poly_center.set(0, 0);
  148. for (int i = 0; i < 4; i++) {
  149. for (int j = 0; j < 4; j++) {
  150. flag = intersection(box_a_corners[i + 1], box_a_corners[i],
  151. box_b_corners[j + 1], box_b_corners[j],
  152. cross_points[cnt]);
  153. if (flag) {
  154. poly_center = poly_center + cross_points[cnt];
  155. cnt++;
  156. }
  157. }
  158. }
  159. // check corners
  160. for (int k = 0; k < 4; k++) {
  161. if (check_in_box2d(box_a, box_b_corners[k])) {
  162. poly_center = poly_center + box_b_corners[k];
  163. cross_points[cnt] = box_b_corners[k];
  164. cnt++;
  165. }
  166. if (check_in_box2d(box_b, box_a_corners[k])) {
  167. poly_center = poly_center + box_a_corners[k];
  168. cross_points[cnt] = box_a_corners[k];
  169. cnt++;
  170. }
  171. }
  172. poly_center.x /= cnt;
  173. poly_center.y /= cnt;
  174. // sort the points of polygon
  175. Point temp;
  176. for (int j = 0; j < cnt - 1; j++) {
  177. for (int i = 0; i < cnt - j - 1; i++) {
  178. if (point_cmp(cross_points[i], cross_points[i + 1], poly_center)) {
  179. temp = cross_points[i];
  180. cross_points[i] = cross_points[i + 1];
  181. cross_points[i + 1] = temp;
  182. }
  183. }
  184. }
  185. // get the overlap areas
  186. float area = 0;
  187. for (int k = 0; k < cnt - 1; k++) {
  188. area += cross(cross_points[k] - cross_points[0],
  189. cross_points[k + 1] - cross_points[0]);
  190. }
  191. return fabs(area) / 2.0;
  192. }
  193. inline float iou_bev(const float *box_a, const float *box_b) {
  194. // params: box_a (7) [x, y, z, dx, dy, dz, heading]
  195. // params: box_b (7) [x, y, z, dx, dy, dz, heading]
  196. float sa = box_a[3] * box_a[4];
  197. float sb = box_b[3] * box_b[4];
  198. float s_overlap = box_overlap(box_a, box_b);
  199. return s_overlap / fmaxf(sa + sb - s_overlap, EPS);
  200. }
  201. std::vector<paddle::Tensor>
  202. boxes_iou_bev_cpu(const paddle::Tensor &boxes_a_tensor,
  203. const paddle::Tensor &boxes_b_tensor) {
  204. // params boxes_a_tensor: (N, 7) [x, y, z, dx, dy, dz, heading]
  205. // params boxes_b_tensor: (M, 7) [x, y, z, dx, dy, dz, heading]
  206. // params ans_iou_tensor: (N, M)
  207. int num_boxes_a = boxes_a_tensor.shape()[0];
  208. int num_boxes_b = boxes_b_tensor.shape()[0];
  209. const float *boxes_a = boxes_a_tensor.data<float>();
  210. const float *boxes_b = boxes_b_tensor.data<float>();
  211. auto ans_iou_tensor =
  212. paddle::empty({num_boxes_a, num_boxes_b}, paddle::DataType::FLOAT32,
  213. paddle::CPUPlace());
  214. float *ans_iou = ans_iou_tensor.data<float>();
  215. for (int i = 0; i < num_boxes_a; i++) {
  216. for (int j = 0; j < num_boxes_b; j++) {
  217. ans_iou[i * num_boxes_b + j] = iou_bev(boxes_a + i * 7, boxes_b + j * 7);
  218. }
  219. }
  220. return {ans_iou_tensor};
  221. }