-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamplePoint.cpp
123 lines (109 loc) · 3.45 KB
/
samplePoint.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright (C) 2016 by Rui Huang
I cowrote part of this file with Xiaokang Zhan.
This file is part of DenseFace3D.
DenseFace3D is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DenseFace3D is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DenseFace3D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "samplePoint.h"
extern cv::Subdiv2D delaunay;
extern cv::Rect rect;
//void Work::procsee(std::vector<dpoint> &ptPolygon,int wx,int wy,int nCount)
//{
// double minx=0,maxx=0,miny=0,maxy=0;
// for(int i=0;i<nCount;i++)
// {
// minx=std::min(minx,ptPolygon[i].x());
// maxx=std::max(maxx,ptPolygon[i].x());
// miny=std::min(miny,ptPolygon[i].y());
// maxy=std::max(maxy,ptPolygon[i].y());
// }
// int dx=(maxx-minx)/wx;
// int dy=(maxy-miny)/wy;
// for(int i=1;i<=wx;i++)
// {
// double pointx=minx+i*dx;
// int sy,ey = wy;
// for(int j=1;j<=wy;j++)
// {
// double pointy=miny+j*dy;
// if(check(pointx,pointy,ptPolygon,nCount))
// {
// sy=j;
// break;
// }
// }
// for(int j=wy;j>=1;j--)
// {
// double pointy=miny+j*dy;
// if(check(pointx,pointy,ptPolygon,nCount))
// {
// ey=j;
// break;
// }
// }
// //cout«"ey is "«ey«"sy is "«sy«endl;
// for(int j=sy;j<=ey;j++)
// RPoints.push_back(dpoint(pointx,(miny+j*dy)));
// }
//}
//generate sample points and insert them into delaunay class
void Work::procsee(std::vector<dpoint> &tem, int wx, int wy, int nCount)
{
double minx, maxx, miny, maxy;
minx = miny = numeric_limits<double>::max();
maxx = maxy = numeric_limits<double>::min();
for (int i = 0; i < tem.size(); i++)
{
minx = std::min(minx, tem[i].x());
maxx = std::max(maxx, tem[i].x());
miny = std::min(miny, tem[i].y());
maxy = std::max(maxy, tem[i].y());
}
//scanning steps
double dx = (maxx - minx) / wx;
double dy = (maxy - miny) / wy;
double currentX = minx, currentY = miny;
rect = cv::Rect(minx, miny, maxx - minx, maxy - miny);
delaunay = cv::Subdiv2D(rect);
for (int i = 0; i < wx; i++) {
for (int j = 0; j < wy; j++) {
if (check(currentX, currentY, tem, nCount)) {
delaunay.insert(cv::Point2f(currentX, currentY));
//RPoints.push_back(dpoint(currentX, currentY));
}
currentY += dy;
}
currentX += dx;
currentY = miny;
}
}
//check whether the point is inside the polygon
bool Work::check(double px, double py, std::vector<dpoint> &ptPolygon, int nCount)
{
int nCross = 0;
for (int i = 0; i < nCount; i++)
{
dpoint p1 = ptPolygon[i];
dpoint p2 = ptPolygon[(i + 1) % nCount];
if (p1.y() == p2.y())
continue;
if (py < std::min(p1.y(), p2.y()))
continue;
if (py >= std::max(p1.y(), p2.y()))
continue;
double x = (double)(py - p1.y()) * (double)(p2.x() - p1.x()) / (double)(p2.y() - p1.y()) + p1.x();
if (x > px)
nCross++;
}
return (nCross % 2 == 1);
}