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