2005-06-14 17:06:59 +02:00
|
|
|
/* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
* Copyright (C) 2005 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* Mapnik 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 2
|
|
|
|
* of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//$Id: geom_util.hpp 39 2005-04-10 20:39:53Z pavlenko $
|
|
|
|
|
|
|
|
#ifndef GEOM_UTIL_HPP
|
|
|
|
#define GEOM_UTIL_HPP
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool clip_test(T p,T q,double& tmin,double& tmax)
|
|
|
|
{
|
|
|
|
double r;
|
|
|
|
bool result=true;
|
|
|
|
if (p<0.0)
|
|
|
|
{
|
|
|
|
r=q/p;
|
|
|
|
if (r>tmax) result=false;
|
|
|
|
else if (r>tmin) tmin=r;
|
|
|
|
}
|
|
|
|
else if (p>0.0)
|
|
|
|
{
|
|
|
|
r=q/p;
|
|
|
|
if (r<tmin) result=false;
|
|
|
|
else if (r<tmax) tmax=r;
|
|
|
|
} else if (q<0.0) result=false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T,typename Image>
|
2006-01-26 17:41:15 +01:00
|
|
|
bool clip_line(T& x0,T& y0,T& x1,T& y1,Envelope<T> const& box)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
double tmin=0.0;
|
|
|
|
double tmax=1.0;
|
|
|
|
double dx=x1-x0;
|
|
|
|
if (clip_test<double>(-dx,x0,tmin,tmax))
|
|
|
|
{
|
2006-01-26 17:41:15 +01:00
|
|
|
if (clip_test<double>(dx,box.width()-x0,tmin,tmax))
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
double dy=y1-y0;
|
|
|
|
if (clip_test<double>(-dy,y0,tmin,tmax))
|
|
|
|
{
|
2006-01-26 17:41:15 +01:00
|
|
|
if (clip_test<double>(dy,box.height()-y0,tmin,tmax))
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
if (tmax<1.0)
|
|
|
|
{
|
|
|
|
x1=static_cast<T>(x0+tmax*dx);
|
|
|
|
y1=static_cast<T>(y0+tmax*dy);
|
|
|
|
}
|
|
|
|
if (tmin>0.0)
|
|
|
|
{
|
|
|
|
x0+=static_cast<T>(tmin*dx);
|
|
|
|
y0+=static_cast<T>(tmin*dy);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Iter>
|
2005-06-17 14:40:51 +02:00
|
|
|
inline bool point_inside_path(double x,double y,Iter start,Iter end)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
bool inside=false;
|
|
|
|
double x0=start->x;
|
|
|
|
double y0=start->y;
|
|
|
|
double x1,y1;
|
|
|
|
while (++start!=end)
|
|
|
|
{
|
|
|
|
if (start->cmd == SEG_MOVETO)
|
|
|
|
{
|
|
|
|
x0=start->x;
|
|
|
|
y0=start->y;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
x1=start->x;
|
|
|
|
y1=start->y;
|
|
|
|
if ((((y1 <= y) && (y < y0)) ||
|
|
|
|
((y0 <= y) && (y < y1))) &&
|
|
|
|
( x < (x0 - x1) * (y - y1)/ (y0 - y1) + x1))
|
|
|
|
inside=!inside;
|
|
|
|
x0=x1;
|
|
|
|
y0=y1;
|
|
|
|
}
|
|
|
|
return inside;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TOL 0.00001
|
|
|
|
|
|
|
|
/*
|
|
|
|
(Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
|
|
|
|
s = -----------------------------
|
|
|
|
L^2
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline bool point_in_circle(double x,double y,double cx,double cy,double r)
|
|
|
|
{
|
|
|
|
double dx = x - cx;
|
|
|
|
double dy = y - cy;
|
|
|
|
double d2 = dx * dx + dy * dy;
|
|
|
|
return (d2 <= r * r);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool point_on_segment(double x,double y,double x0,double y0,double x1,double y1)
|
|
|
|
{
|
|
|
|
double dx = x1 - x0;
|
|
|
|
double dy = y1 - y0;
|
|
|
|
if ( fabs(dx) > TOL || fabs(dy) > TOL )
|
|
|
|
{
|
|
|
|
double s = (y0 - y) * dx - (x0 - x) * dy;
|
|
|
|
return ( fabs (s) < TOL ) ;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool point_on_segment2(double x,double y,double x0,double y0,double x1,double y1)
|
|
|
|
{
|
|
|
|
double d = sqrt ((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
|
|
|
|
double d0 = sqrt ((x0 - x) * (x0 - x) + (y0 - y) * (y0 - y));
|
|
|
|
double d1 = sqrt ((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
|
|
|
|
double d2 = d0 + d1;
|
|
|
|
return ( d2 - d < 0.01);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef TOL
|
2005-06-17 14:40:51 +02:00
|
|
|
template <typename Iter>
|
|
|
|
inline bool point_on_path(double x,double y,Iter start,Iter end)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2005-06-17 14:40:51 +02:00
|
|
|
return false;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2005-06-17 14:40:51 +02:00
|
|
|
|
|
|
|
template <typename Iter>
|
|
|
|
inline bool point_on_points (double x,double y,Iter start,Iter end)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct filter_in_box
|
|
|
|
{
|
|
|
|
Envelope<double> box_;
|
|
|
|
explicit filter_in_box(const Envelope<double>& box)
|
|
|
|
: box_(box) {}
|
|
|
|
|
|
|
|
bool pass(const Envelope<double>& extent) const
|
|
|
|
{
|
|
|
|
return extent.intersects(box_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct filter_at_point
|
|
|
|
{
|
|
|
|
coord2d pt_;
|
|
|
|
explicit filter_at_point(const coord2d& pt)
|
|
|
|
: pt_(pt) {}
|
|
|
|
bool pass(const Envelope<double>& extent) const
|
|
|
|
{
|
|
|
|
return extent.contains(pt_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //GEOM_UTIL_HPP
|