poly_util.cc 4.1 KB
Newer Older
Y
Yipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#ifndef POLY_UTIL_CC_
#define POLY_UTIL_CC_

#include "paddle/fluid/operators/detection/poly_util.h"
18

Y
Yipeng 已提交
19 20 21 22 23 24
#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

using gpc::gpc_free_polygon;
25
using gpc::gpc_polygon_clip;
Y
Yipeng 已提交
26 27

template <class T>
28 29 30
void Array2PointVec(const T* box,
                    const size_t box_size,
                    std::vector<Point_<T>>* vec) {
Y
Yipeng 已提交
31
  size_t pts_num = box_size / 2;
32
  (*vec).resize(pts_num);
Y
Yipeng 已提交
33
  for (size_t i = 0; i < pts_num; i++) {
34 35
    (*vec).at(i).x = box[2 * i];
    (*vec).at(i).y = box[2 * i + 1];
Y
Yipeng 已提交
36 37 38 39
  }
}

template <class T>
40
void Array2Poly(const T* box, const size_t box_size, gpc::gpc_polygon* poly) {
Y
Yipeng 已提交
41
  size_t pts_num = box_size / 2;
42 43 44 45 46 47
  (*poly).num_contours = 1;
  (*poly).hole = reinterpret_cast<int*>(malloc(sizeof(int)));
  (*poly).hole[0] = 0;
  (*poly).contour = (gpc::gpc_vertex_list*)malloc(sizeof(gpc::gpc_vertex_list));
  (*poly).contour->num_vertices = pts_num;
  (*poly).contour->vertex =
Y
Yipeng 已提交
48 49
      (gpc::gpc_vertex*)malloc(sizeof(gpc::gpc_vertex) * pts_num);
  for (size_t i = 0; i < pts_num; ++i) {
50 51
    (*poly).contour->vertex[i].x = box[2 * i];
    (*poly).contour->vertex[i].y = box[2 * i + 1];
Y
Yipeng 已提交
52 53 54 55
  }
}

template <class T>
56
void PointVec2Poly(const std::vector<Point_<T>>& vec, gpc::gpc_polygon* poly) {
Y
Yipeng 已提交
57
  int pts_num = vec.size();
58 59 60 61 62 63
  (*poly).num_contours = 1;
  (*poly).hole = reinterpret_cast<int*>(malloc(sizeof(int)));
  (*poly).hole[0] = 0;
  (*poly).contour = (gpc::gpc_vertex_list*)malloc(sizeof(gpc::gpc_vertex_list));
  (*poly).contour->num_vertices = pts_num;
  (*poly).contour->vertex =
Y
Yipeng 已提交
64 65
      (gpc::gpc_vertex*)malloc(sizeof(gpc::gpc_vertex) * pts_num);
  for (size_t i = 0; i < pts_num; ++i) {
66 67
    (*poly).contour->vertex[i].x = vec[i].x;
    (*poly).contour->vertex[i].y = vec[i].y;
Y
Yipeng 已提交
68 69 70 71 72
  }
}

template <class T>
void Poly2PointVec(const gpc::gpc_vertex_list& contour,
73
                   std::vector<Point_<T>>* vec) {
Y
Yipeng 已提交
74
  int pts_num = contour.num_vertices;
75
  (*vec).resize(pts_num);
Y
Yipeng 已提交
76
  for (int i = 0; i < pts_num; i++) {
77 78
    (*vec).at(i).x = contour.vertex[i].x;
    (*vec).at(i).y = contour.vertex[i].y;
Y
Yipeng 已提交
79 80 81 82
  }
}

template <class T>
83
T GetContourArea(const std::vector<Point_<T>>& vec) {
Y
Yipeng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  size_t pts_num = vec.size();
  if (pts_num < 3) return T(0.);
  T area = T(0.);
  for (size_t i = 0; i < pts_num; ++i) {
    area += vec[i].x * vec[(i + 1) % pts_num].y -
            vec[i].y * vec[(i + 1) % pts_num].x;
  }
  return std::fabs(area / 2.0);
}

template <class T>
T PolyArea(const T* box, const size_t box_size, const bool normalized) {
  // If coordinate values are is invalid
  // if area size <= 0,  return 0.
  std::vector<Point_<T>> vec;
99
  Array2PointVec<T>(box, box_size, &vec);
Y
Yipeng 已提交
100 101 102 103
  return GetContourArea<T>(vec);
}

template <class T>
104 105 106
T PolyOverlapArea(const T* box1,
                  const T* box2,
                  const size_t box_size,
Y
Yipeng 已提交
107 108 109
                  const bool normalized) {
  gpc::gpc_polygon poly1;
  gpc::gpc_polygon poly2;
110 111
  Array2Poly<T>(box1, box_size, &poly1);
  Array2Poly<T>(box2, box_size, &poly2);
Y
Yipeng 已提交
112 113 114 115 116 117 118 119
  gpc::gpc_polygon respoly;
  gpc::gpc_op op = gpc::GPC_INT;
  gpc::gpc_polygon_clip(op, &poly2, &poly1, &respoly);

  T inter_area = T(0.);
  int contour_num = respoly.num_contours;
  for (int i = 0; i < contour_num; ++i) {
    std::vector<Point_<T>> resvec;
120
    Poly2PointVec<T>(respoly.contour[i], &resvec);
Y
Yipeng 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    // inter_area += std::fabs(cv::contourArea(resvec)) + 0.5f *
    // (cv::arcLength(resvec, true));
    inter_area += GetContourArea<T>(resvec);
  }

  gpc::gpc_free_polygon(&poly1);
  gpc::gpc_free_polygon(&poly2);
  gpc::gpc_free_polygon(&respoly);
  return inter_area;
}

}  // namespace operators
}  // namespace paddle

#endif