CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/DOTA_devkit/polyiou.cpp
Views: 475
1
2
#include<cstdio>
3
#include<iostream>
4
#include<algorithm>
5
#include<cmath>
6
#include <vector>
7
using namespace std;
8
#define maxn 51
9
const double eps=1E-8;
10
int sig(double d){
11
return(d>eps)-(d<-eps);
12
}
13
struct Point{
14
double x,y; Point(){}
15
Point(double x,double y):x(x),y(y){}
16
bool operator==(const Point&p)const{
17
return sig(x-p.x)==0&&sig(y-p.y)==0;
18
}
19
};
20
double cross(Point o,Point a,Point b){ //叉积
21
return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
22
}
23
double area(Point* ps,int n){
24
ps[n]=ps[0];
25
double res=0;
26
for(int i=0;i<n;i++){
27
res+=ps[i].x*ps[i+1].y-ps[i].y*ps[i+1].x;
28
}
29
return res/2.0;
30
}
31
int lineCross(Point a,Point b,Point c,Point d,Point&p){
32
double s1,s2;
33
s1=cross(a,b,c);
34
s2=cross(a,b,d);
35
if(sig(s1)==0&&sig(s2)==0) return 2;
36
if(sig(s2-s1)==0) return 0;
37
p.x=(c.x*s2-d.x*s1)/(s2-s1);
38
p.y=(c.y*s2-d.y*s1)/(s2-s1);
39
return 1;
40
}
41
//多边形切割
42
//用直线ab切割多边形p,切割后的在向量(a,b)的左侧,并原地保存切割结果
43
//如果退化为一个点,也会返回去,此时n为1
44
//void polygon_cut(Point*p,int&n,Point a,Point b){
45
// static Point pp[maxn];
46
// int m=0;p[n]=p[0];
47
// for(int i=0;i<n;i++){
48
// if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
49
// if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
50
// lineCross(a,b,p[i],p[i+1],pp[m++]);
51
// }
52
// n=0;
53
// for(int i=0;i<m;i++)
54
// if(!i||!(pp[i]==pp[i-1]))
55
// p[n++]=pp[i];
56
// while(n>1&&p[n-1]==p[0])n--;
57
//}
58
void polygon_cut(Point*p,int&n,Point a,Point b, Point* pp){
59
// static Point pp[maxn];
60
int m=0;p[n]=p[0];
61
for(int i=0;i<n;i++){
62
if(sig(cross(a,b,p[i]))>0) pp[m++]=p[i];
63
if(sig(cross(a,b,p[i]))!=sig(cross(a,b,p[i+1])))
64
lineCross(a,b,p[i],p[i+1],pp[m++]);
65
}
66
n=0;
67
for(int i=0;i<m;i++)
68
if(!i||!(pp[i]==pp[i-1]))
69
p[n++]=pp[i];
70
while(n>1&&p[n-1]==p[0])n--;
71
}
72
//---------------华丽的分隔线-----------------//
73
//返回三角形oab和三角形ocd的有向交面积,o是原点//
74
double intersectArea(Point a,Point b,Point c,Point d){
75
Point o(0,0);
76
int s1=sig(cross(o,a,b));
77
int s2=sig(cross(o,c,d));
78
if(s1==0||s2==0)return 0.0;//退化,面积为0
79
if(s1==-1) swap(a,b);
80
if(s2==-1) swap(c,d);
81
Point p[10]={o,a,b};
82
int n=3;
83
Point pp[maxn];
84
polygon_cut(p,n,o,c, pp);
85
polygon_cut(p,n,c,d, pp);
86
polygon_cut(p,n,d,o, pp);
87
double res=fabs(area(p,n));
88
if(s1*s2==-1) res=-res;return res;
89
}
90
//求两多边形的交面积
91
double intersectArea(Point*ps1,int n1,Point*ps2,int n2){
92
if(area(ps1,n1)<0) reverse(ps1,ps1+n1);
93
if(area(ps2,n2)<0) reverse(ps2,ps2+n2);
94
ps1[n1]=ps1[0];
95
ps2[n2]=ps2[0];
96
double res=0;
97
for(int i=0;i<n1;i++){
98
for(int j=0;j<n2;j++){
99
res+=intersectArea(ps1[i],ps1[i+1],ps2[j],ps2[j+1]);
100
}
101
}
102
return res;//assumeresispositive!
103
}
104
105
106
107
108
double iou_poly(vector<double> p, vector<double> q) {
109
Point ps1[maxn],ps2[maxn];
110
int n1 = 4;
111
int n2 = 4;
112
for (int i = 0; i < 4; i++) {
113
ps1[i].x = p[i * 2];
114
ps1[i].y = p[i * 2 + 1];
115
116
ps2[i].x = q[i * 2];
117
ps2[i].y = q[i * 2 + 1];
118
}
119
double inter_area = intersectArea(ps1, n1, ps2, n2);
120
double union_area = fabs(area(ps1, n1)) + fabs(area(ps2, n2)) - inter_area;
121
double iou = inter_area / union_area;
122
123
// cout << "inter_area:" << inter_area << endl;
124
// cout << "union_area:" << union_area << endl;
125
// cout << "iou:" << iou << endl;
126
127
return iou;
128
}
129
//
130
//int main(){
131
// double p[8] = {0, 0, 1, 0, 1, 1, 0, 1};
132
// double q[8] = {0.5, 0.5, 1.5, 0.5, 1.5, 1.5, 0.5, 1.5};
133
// vector<double> P(p, p + 8);
134
// vector<double> Q(q, q + 8);
135
// iou_poly(P, Q);
136
// return 0;
137
//}
138
139
//int main(){
140
// double p[8] = {0, 0, 1, 0, 1, 1, 0, 1};
141
// double q[8] = {0.5, 0.5, 1.5, 0.5, 1.5, 1.5, 0.5, 1.5};
142
// iou_poly(p, q);
143
// return 0;
144
//}
145