/******************实现功能:判断平面任一点是否在指定多边形内********************/

 #include <string>
#include <vector>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream> using namespace std; typedef struct { int x, y; } Point; //算法说明 file:///D:/ddb/相机标定/Point_In_Polygon.pdf
// a Point is defined by its coordinates {int x, y;}
//=================================================================== // isLeft(): tests if a point is Left|On|Right of an infinite line.
// Input: three points P0, P1, and P2
// Return: >0 for P2 left of the line through P0 and P1
// =0 for P2 on the line
// <0 for P2 right of the line
// See: Algorithm 1 "Area of Triangles and Polygons" inline int
isLeft(Point P0, Point P1, Point P2)
{
return ((P1.x - P0.x) * (P2.y - P0.y)
- (P2.x - P0.x) * (P1.y - P0.y));
} //==================================================================
// cn_PnPoly(): crossing number test for a point in a polygon
// Input: P = a point,
// V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
// Return: 0 = outside, 1 = inside
// This code is patterned after [Franklin, 2000]
int
cn_PnPoly(Point P, Point* V, int n)
{
int cn = ; // the crossing number counter // loop through all edges of the polygon
for (int i = ; i < n; i++)
{
// edge from V[i] to V[i+1]
if (((V[i].y <= P.y) && (V[i + ].y > P.y))||((V[i].y > P.y) && (V[i + ].y <= P.y))) // an upward crossing
{
// a downward crossing
// compute the actual edge-ray intersect x-coordinate
float vt = (float)(P.y - V[i].y) / (V[i + ].y - V[i].y);
if (P.x < V[i].x + vt * (V[i + ].x - V[i].x)) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x
}
}
return (cn & ); // 0 if even (out), and 1 if odd (in)
} // wn_PnPoly(): winding number test for a point in a polygon
// Input: P = a point,
// V[] = vertex points of a polygon V[n+1] with V[n]=V[0]
// Return: wn = the winding number (=0 only when P is outside)
int
wn_PnPoly(Point P, Point* V, int n)
{
int wn = ; // the winding number counter
// loop through all edges of the polygon
for (int i = ; i<n; i++)
{
// edge from V[i] to V[i+1]
if (V[i].y <= P.y)
{
// start y <= P.y
if (V[i + ].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + ], P) > ) // P left of edge
++wn; // have a valid up intersect
}
else
{ // start y > P.y (no test needed)
if (V[i + ].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + ], P) < ) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
} int
wn_PnPoly_ddb(Point& P, const std::vector<Point>& V) // std::vector<Point>& V = vertex points of a polygon V[n + 1] with V[n] = V[0] , 注意多一个点首尾相同
{
int wn = ; // the winding number counter
int n = V.size() - ; // loop through all edges of the polygon
for (int i = ; i<n; i++)
{
// edge from V[i] to V[i+1]
if (V[i].y <= P.y)
{
// start y <= P.y
if (V[i + ].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + ], P) > ) // P left of edge
++wn; // have a valid up intersect
}
else
{ // start y > P.y (no test needed)
if (V[i + ].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + ], P) < ) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
} void
readImageROIpolygen(const std::string& path, std::vector<Point>& pointVec, int i)
{
char name[];
string filename;
Point p2d; int length = sprintf(name, "%d%s", i + , "_ROI.txt");
filename = path + "/" + name;
ifstream infile(filename); string Fline;
if (infile) // 有该文件
{
while (getline(infile, Fline)) // Fline中不包括每行的换行符
{
//cout << Fline << endl;
string buf;
stringstream ss(Fline); // 字符流ss
vector<string> tokens; // vector // 按照逗号分隔
while (getline(ss, buf, ','))
{
tokens.push_back(buf);
} ////stringstream 按空格分开字符输入
//while (ss >> buf)
//{
// tokens.push_back(buf);
//} if (tokens.size()<)
{
continue;
}
else if (tokens.size() == )
{
int j = ;
p2d.x = atof(tokens[j].c_str()); //根据字段顺序做修改 对应图像 u v
j++;
p2d.y = atof(tokens[j].c_str());
pointVec.push_back(p2d);
}
}
pointVec.push_back(pointVec[]);
//std::cout << i << endl;
}
else // 没有该文件
{
cout << "error:: no such file" << endl;
}
infile.close(); } /**************** Test ****************/
void
main()
{
Point detectPoint;
detectPoint.x = ; //1363, 1260 //2896,2276 //2653,390
detectPoint.y = ;
std::string path="矫正后图像"; std::vector<Point> pointVec;
readImageROIpolygen(path, pointVec,);
if (pointVec.size()==)
{
cout << "no ROI!!!" << endl;
return;
} if (wn_PnPoly_ddb(detectPoint, pointVec) == )
{
cout << "detectPoint outside ImageROIpolygen!!!!" << endl;
}
else
{
cout << "detectPoint in ImageROIpolygen!!!!" << endl;
} system("pause");
}

detect——point_in_polygon的更多相关文章

  1. Microsoft Windows* SDK May 2010 或较新版本(兼容 2010 年 6 月 DirectX SDK)GPU Detect

    原文链接 下载代码样本 特性/描述 日期: 2016 年 5 月 5 日 GPU Detect 是一种简短的示例,演示了检测系统中主要显卡硬件(包括第六代智能英特尔® 酷睿™ 处理器产品家族)的方式. ...

  2. 【maven】pom.xml报错:Cannot detect Web Project version.

    新建的maven项目 报错如下: Cannot detect Web Project version. Please specify version of Web Project through &l ...

  3. JS: How to detect my browser version and operating system using JavaScript?

    Example: 1. for IE 11,  navigator.userAgent  returns "Mozilla/5.0 (Windows NT 6.1; WOW64; Tride ...

  4. python flask detect browser language

    python flask detect browser language   No problem. We won't show you that ad again. Why didn't you l ...

  5. javascript: detect mobile devices or browser

    http://detectmobilebrowsers.com/ http://hgoebl.github.io/mobile-detect.js/ http://www.hand-interacti ...

  6. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  7. Leetcode: Graph Valid Tree && Summary: Detect cycle in undirected graph

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  8. linux安装phpstorm出现 Startup Error: Unable to detect graphics environment

    在centos6.2下在安装phpstorm下遇到 Startup Error: Unable to detect graphics environment 其中jdk安装的版本是 1.7.0_79  ...

  9. GoldenGate: Extract Abend with Detect Inconsistency in Pdata (文档 ID 1355067.1)

    APPLIES TO: Oracle GoldenGate - Version 10.4.0.0 and laterInformation in this document applies to an ...

随机推荐

  1. 【11】react 之 flux

    Flux 是 Facebook 使用的一套前端应用的架构模式.React 标榜自己是 MVC 里面 V 的部分,那么 Flux 就相当于添加 M 和 C 的部分. 1.1.  Flux介绍 Flux并 ...

  2. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  3. [HNOI2012]矿场搭建(tarjan求点双)

    题目 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无 ...

  4. 【转】linux之shfit

    位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shift命令相当于shift 1. 非常 ...

  5. 网页内容切换效果实现的15个jQuery插件

    原文发布时间为:2010-02-01 -- 来源于本人的百度文章 [由搬家工具导入] http://www.webjx.com/javascript/jsajax-15550.html

  6. mysql数据库存放位置

    在mysql 命令行里执行 show variables like '%datadir%';

  7. hdu 4738 无向图缩点断桥 // 细节坑题

    Caocao's Bridges 题意:给个无向图,求出边权最小的桥. 一看,直接缩点,若无桥,输出-1,有桥,遍历下边,更新最小..分分钟搞定,以为IA的..一交wa... 坑点:1:若原图不连通, ...

  8. EasyUI左边树菜单和datagrid分页

    //这个页面是Home.html 1 <!DOCTYPE html> <html> <head> <meta http-equiv="Content ...

  9. autofac 用法总结

    autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...

  10. Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)

    题目链接  Power Tower 题意  给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$  对m取模的值 根据这个公式 每次 ...