iou3d_cpu.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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) return 0;
  74. // check cross standing
  75. float s1 = cross(q0, p1, p0);
  76. float s2 = cross(p1, q1, p0);
  77. float s3 = cross(p0, q1, q0);
  78. float s4 = cross(q1, p1, q0);
  79. if (!(s1 * s2 > 0 && s3 * s4 > 0)) return 0;
  80. // calculate intersection of two lines
  81. float s5 = cross(q1, p1, p0);
  82. if (fabs(s5 - s1) > EPS) {
  83. ans.x = (s5 * q0.x - s1 * q1.x) / (s5 - s1);
  84. ans.y = (s5 * q0.y - s1 * q1.y) / (s5 - s1);
  85. } else {
  86. float a0 = p0.y - p1.y, b0 = p1.x - p0.x, c0 = p0.x * p1.y - p1.x * p0.y;
  87. float a1 = q0.y - q1.y, b1 = q1.x - q0.x, c1 = q0.x * q1.y - q1.x * q0.y;
  88. float D = a0 * b1 - a1 * b0;
  89. ans.x = (b0 * c1 - b1 * c0) / D;
  90. ans.y = (a1 * c0 - a0 * c1) / D;
  91. }
  92. return 1;
  93. }
  94. inline void rotate_around_center(const Point &center, const float angle_cos,
  95. const float angle_sin, Point &p) {
  96. float new_x =
  97. (p.x - center.x) * angle_cos + (p.y - center.y) * (-angle_sin) + center.x;
  98. float new_y =
  99. (p.x - center.x) * angle_sin + (p.y - center.y) * angle_cos + center.y;
  100. p.set(new_x, new_y);
  101. }
  102. inline int point_cmp(const Point &a, const Point &b, const Point &center) {
  103. return atan2(a.y - center.y, a.x - center.x) >
  104. atan2(b.y - center.y, b.x - center.x);
  105. }
  106. inline float box_overlap(const float *box_a, const float *box_b) {
  107. // params: box_a (7) [x, y, z, dx, dy, dz, heading]
  108. // params: box_b (7) [x, y, z, dx, dy, dz, heading]
  109. // float a_x1 = box_a[0], a_y1 = box_a[1], a_x2 = box_a[2], a_y2 =
  110. // box_a[3], a_angle = box_a[4];
  111. // float b_x1 = box_b[0], b_y1 = box_b[1], b_x2 = box_b[2], b_y2 =
  112. // box_b[3], b_angle = box_b[4];
  113. float a_angle = box_a[6], b_angle = box_b[6];
  114. float a_dx_half = box_a[3] / 2, b_dx_half = box_b[3] / 2,
  115. a_dy_half = box_a[4] / 2, b_dy_half = box_b[4] / 2;
  116. float a_x1 = box_a[0] - a_dx_half, a_y1 = box_a[1] - a_dy_half;
  117. float a_x2 = box_a[0] + a_dx_half, a_y2 = box_a[1] + a_dy_half;
  118. float b_x1 = box_b[0] - b_dx_half, b_y1 = box_b[1] - b_dy_half;
  119. float b_x2 = box_b[0] + b_dx_half, b_y2 = box_b[1] + b_dy_half;
  120. Point center_a(box_a[0], box_a[1]);
  121. Point center_b(box_b[0], box_b[1]);
  122. Point box_a_corners[5];
  123. box_a_corners[0].set(a_x1, a_y1);
  124. box_a_corners[1].set(a_x2, a_y1);
  125. box_a_corners[2].set(a_x2, a_y2);
  126. box_a_corners[3].set(a_x1, a_y2);
  127. Point box_b_corners[5];
  128. box_b_corners[0].set(b_x1, b_y1);
  129. box_b_corners[1].set(b_x2, b_y1);
  130. box_b_corners[2].set(b_x2, b_y2);
  131. box_b_corners[3].set(b_x1, b_y2);
  132. // get oriented corners
  133. float a_angle_cos = cos(a_angle), a_angle_sin = sin(a_angle);
  134. float b_angle_cos = cos(b_angle), b_angle_sin = sin(b_angle);
  135. for (int k = 0; k < 4; k++) {
  136. rotate_around_center(center_a, a_angle_cos, a_angle_sin, box_a_corners[k]);
  137. rotate_around_center(center_b, b_angle_cos, b_angle_sin, box_b_corners[k]);
  138. }
  139. box_a_corners[4] = box_a_corners[0];
  140. box_b_corners[4] = box_b_corners[0];
  141. // get intersection of lines
  142. Point cross_points[16];
  143. Point poly_center;
  144. int cnt = 0, flag = 0;
  145. poly_center.set(0, 0);
  146. for (int i = 0; i < 4; i++) {
  147. for (int j = 0; j < 4; j++) {
  148. flag = intersection(box_a_corners[i + 1], box_a_corners[i],
  149. box_b_corners[j + 1], box_b_corners[j],
  150. cross_points[cnt]);
  151. if (flag) {
  152. poly_center = poly_center + cross_points[cnt];
  153. cnt++;
  154. }
  155. }
  156. }
  157. // check corners
  158. for (int k = 0; k < 4; k++) {
  159. if (check_in_box2d(box_a, box_b_corners[k])) {
  160. poly_center = poly_center + box_b_corners[k];
  161. cross_points[cnt] = box_b_corners[k];
  162. cnt++;
  163. }
  164. if (check_in_box2d(box_b, box_a_corners[k])) {
  165. poly_center = poly_center + box_a_corners[k];
  166. cross_points[cnt] = box_a_corners[k];
  167. cnt++;
  168. }
  169. }
  170. poly_center.x /= cnt;
  171. poly_center.y /= cnt;
  172. // sort the points of polygon
  173. Point temp;
  174. for (int j = 0; j < cnt - 1; j++) {
  175. for (int i = 0; i < cnt - j - 1; i++) {
  176. if (point_cmp(cross_points[i], cross_points[i + 1], poly_center)) {
  177. temp = cross_points[i];
  178. cross_points[i] = cross_points[i + 1];
  179. cross_points[i + 1] = temp;
  180. }
  181. }
  182. }
  183. // get the overlap areas
  184. float area = 0;
  185. for (int k = 0; k < cnt - 1; k++) {
  186. area += cross(cross_points[k] - cross_points[0],
  187. cross_points[k + 1] - cross_points[0]);
  188. }
  189. return fabs(area) / 2.0;
  190. }
  191. inline float iou_bev(const float *box_a, const float *box_b) {
  192. // params: box_a (7) [x, y, z, dx, dy, dz, heading]
  193. // params: box_b (7) [x, y, z, dx, dy, dz, heading]
  194. float sa = box_a[3] * box_a[4];
  195. float sb = box_b[3] * box_b[4];
  196. float s_overlap = box_overlap(box_a, box_b);
  197. return s_overlap / fmaxf(sa + sb - s_overlap, EPS);
  198. }
  199. std::vector<paddle::Tensor> boxes_iou_bev_cpu(
  200. const paddle::Tensor &boxes_a_tensor,
  201. const paddle::Tensor &boxes_b_tensor) {
  202. // params boxes_a_tensor: (N, 7) [x, y, z, dx, dy, dz, heading]
  203. // params boxes_b_tensor: (M, 7) [x, y, z, dx, dy, dz, heading]
  204. // params ans_iou_tensor: (N, M)
  205. int num_boxes_a = boxes_a_tensor.shape()[0];
  206. int num_boxes_b = boxes_b_tensor.shape()[0];
  207. const float *boxes_a = boxes_a_tensor.data<float>();
  208. const float *boxes_b = boxes_b_tensor.data<float>();
  209. auto ans_iou_tensor =
  210. paddle::empty({num_boxes_a, num_boxes_b}, paddle::DataType::FLOAT32,
  211. paddle::CPUPlace());
  212. float *ans_iou = ans_iou_tensor.data<float>();
  213. for (int i = 0; i < num_boxes_a; i++) {
  214. for (int j = 0; j < num_boxes_b; j++) {
  215. ans_iou[i * num_boxes_b + j] = iou_bev(boxes_a + i * 7, boxes_b + j * 7);
  216. }
  217. }
  218. return {ans_iou_tensor};
  219. }