2 #ifndef vgl_polygon_hxx_ 3 #define vgl_polygon_hxx_ 12 #include <vnl/vnl_math.h> 21 # include <vcl_msvc_warnings.h> 31 for (
int i = 0; i < n; ++i)
32 sheets_[0][i].set(p[i].x(), p[i].y());
39 for (
int i = 0; i < n; ++i)
47 for (
int i = 0; i < n; ++i)
48 sheets_[0][i].set(x_y[2*i], x_y[2*i+1]);
55 for (
int i = 0; i < n; ++i)
56 s[i].set(p[i].x(), p[i].y());
64 for (
int i = 0; i < n; ++i)
73 for (
int i = 0; i < n; ++i)
74 s[i].set(x_y[2*i], x_y[2*i+1]);
83 sheets_[sheets_.size()-1].push_back(
point_t(x,y));
89 sheets_[sheets_.size()-1].push_back(p);
96 for (
unsigned int s=0; s < sheets_.size(); ++s)
98 sheet_t const& pgon = sheets_[s];
99 int n = int(pgon.size());
100 for (
int i = 0, j = n-1; i < n; j = i++)
106 if ((p_j.
x() - x) * (p_i.
y() - y) == (p_i.
x() - x) * (p_j.
y() - y) &&
107 (((p_i.
x()<=x) && (x<=p_j.
x())) || ((p_j.
x()<=x) && (x<=p_i.
x()))) &&
108 (((p_i.
y()<=y) && (y<=p_j.
y())) || ((p_j.
y()<=y) && (y<=p_i.
y()))))
111 if ((((p_i.
y()<=y) && (y<p_j.
y())) || ((p_j.
y()<=y) && (y<p_i.
y()))) &&
112 (x < (p_j.
x() - p_i.
x()) * (y - p_i.
y()) / (p_j.
y() - p_i.
y()) + p_i.
x()))
122 if (sheets_.size() == 0)
123 os <<
"Empty polygon\n";
125 os <<
"Polygon with " << num_sheets() <<
" sheets:\n";
126 for (
unsigned int s = 0; s < sheets_.size(); ++s)
128 os <<
"Sheet " << s <<
' ';
129 if (sheets_[s].size()==0)
132 os <<
" nverts = " << sheets_[s].size() <<
'\n';
133 for (
unsigned int p = 0; p < sheets_[s].size(); ++p)
134 os <<
"( " << sheets_[s][p].x() <<
" , " << sheets_[s][p].y() <<
" ) ";
145 if(s ==
"Empty polygon")
154 sheets_.resize(n_sheets);
155 for(
unsigned sh = 0; sh<n_sheets; ++sh){
164 for(
unsigned iv = 0; iv<nv; ++iv){
172 sheets_[sh].push_back(pt);
182 n = int(p[0].size());
185 for (
int v = 0;
v < n; ++
v)
198 for (
int v = 0;
v < n; ++
v)
221 std::vector<std::pair<unsigned int,unsigned int> >& e1,
222 std::vector<std::pair<unsigned int,unsigned int> >& e2,
226 std::vector<unsigned int> offsets(p.
num_sheets()+1,0);
227 for (
unsigned int s = 0; s < p.
num_sheets(); ++s) {
228 offsets[s+1] = offsets[s]+(
unsigned int)(p[s].size());
234 typedef std::pair<unsigned int, unsigned int> upair;
235 std::set<upair> isect_set;
236 for (
unsigned int s1 = 0; s1 < p.
num_sheets(); ++s1)
239 if (sheet1.size() < 2)
241 for (
unsigned int i1=(
unsigned int)(sheet1.size()-1), i1n=0; i1n < sheet1.size(); i1=i1n, ++i1n)
247 T cx = v1.
y()-v2.
y();
248 T cy = v2.
x()-v1.
x();
249 T c = v1.
x()*v2.
y()-v2.
x()*v1.
y();
250 T norm = 1/std::sqrt(cx*cx+cy*cy);
255 unsigned int idx1 = offsets[s1]+i1;
258 for (
unsigned int s2 = 0; s2 < p.
num_sheets(); ++s2)
261 const unsigned int size2 = (
unsigned int)(sheet2.size());
264 unsigned int start = 0, end = 0;
266 start = (i1n+1)%size2;
267 end = (i1+size2-1)%size2;
270 T dist = cx*sheet2[start].x()+cy*sheet2[start].y()+c;
271 bool last_sign = dist > 0;
272 bool last_zero = std::abs(dist) <= tol;
274 for (
unsigned int i2=start, i2n=(start+1)%size2; i2!=end || first; i2=i2n, i2n=(i2n+1)%size2)
278 dist = cx*v4.
x()+cy*v4.
y()+c;
279 bool sign = dist > 0;
280 bool zero = std::abs(dist) <= tol;
281 if (sign != last_sign || zero || last_zero)
283 unsigned int idx2 = offsets[s2]+i2;
284 upair pair_idx(idx1,idx2);
285 if (pair_idx.first > pair_idx.second) {
286 pair_idx = upair(idx2,idx1);
288 std::set<upair>::iterator f = isect_set.find(pair_idx);
289 if (f == isect_set.end())
290 isect_set.insert(pair_idx);
306 t -= std::min(std::min(t1,t2),std::min(t3,t4));
307 t -= std::max(std::max(t1,t2),std::max(t3,t4));
311 e1.emplace_back(s2,i2);
312 e2.emplace_back(s1,i1);
331 std::vector<std::pair<unsigned,unsigned> > e1, e2;
332 std::vector<vgl_point_2d<T> > ip;
334 if(!e1.empty() && !e2.empty() && !ip.empty())
338 std::cout<<
"WARNING - self intersecting polygon"<<std::endl;
345 std::vector<vgl_point_2d<T> > verts = p[i];
353 {std::reverse(verts.begin(), verts.end());}
368 for(
size_t x = 1; x <verts.size(); x++)
372 term1 = double(v2.
x())-
double(v1.
x());
373 term2 = double(v2.
y())+
double(v1.
y());
376 v1 = verts.at(verts.size()-1);
378 term1 = double(v2.
x())-
double(v1.
x());
379 term2 = double(v2.
y())+
double(v1.
y());
386 #undef VGL_POLYGON_INSTANTIATE 387 #define VGL_POLYGON_INSTANTIATE(T) \ 388 template class vgl_polygon<T >; \ 389 template struct vgl_polygon_sheet_as_array<T >; \ 390 template std::ostream& operator<<(std::ostream&,vgl_polygon<T > const&); \ 391 template std::istream& operator>>(std::istream&,vgl_polygon<T >&); \ 392 template void vgl_selfintersections(vgl_polygon<T > const& p, \ 393 std::vector<std::pair<unsigned int,unsigned int> >& e1, \ 394 std::vector<std::pair<unsigned int,unsigned int> >& e2, \ 395 std::vector<vgl_point_2d<T > >& ip); \ 396 template vgl_polygon<T> vgl_reorient_polygon(vgl_polygon<T > const &p); \ 397 template bool vgl_polygon_sheet_is_counter_clockwise(std::vector<vgl_point_2d<T > > verts) 399 #endif // vgl_polygon_hxx_ vgl_polygon()=default
Default constructor - constructs an empty polygon with no sheets.
Set of intersection functions.
T dot_product(v const &a, v const &b)
dot product or inner product of two vectors.
bool contains(point_t const &p) const
Returns true if p(x,y) is inside the polygon, else false.
Direction vector in Euclidean 2D space, templated by type of element.
v & normalize(v &a)
Normalise by dividing through by the length, thus returning a length 1 vector.
std::ostream & print(std::ostream &) const
Pretty print.
vgl_polygon< T > vgl_reorient_polygon(vgl_polygon< T > const &p)
void add_contour(point_t const p[], int n)
Add a single sheet to this polygon, specified by the given list of n points.
bool vgl_polygon_sheet_is_counter_clockwise(std::vector< vgl_point_2d< T > > verts)
vgl_point_3d< T > vgl_intersection(const std::vector< vgl_plane_3d< T > > &p)
Return the intersection point of vector of planes.
void vgl_selfintersections(vgl_polygon< T > const &p, std::vector< std::pair< unsigned int, unsigned int > > &e1, std::vector< std::pair< unsigned int, unsigned int > > &e2, std::vector< vgl_point_2d< T > > &ip)
Compute all self-intersections between all edges on all sheets.
vgl_polygon_sheet_as_array(vgl_polygon< T > const &p)
Automatic constructor from a single-sheet polygon.
void push_back(T x, T y)
Add a new point to the last sheet.
~vgl_polygon_sheet_as_array()
Destructor.
bool parallel(v const &a, v const &b, double eps=0.0)
are two vectors parallel, i.e., is one a scalar multiple of the other?.
std::vector< point_t > sheet_t
unsigned int num_sheets() const
std::istream & read(std::istream &)
read this polygon from ascii stream.
std::vector< sheet_t > sheets_