| 1 |
///////////////////////////////////////////////////////////// |
| 2 |
// // |
| 3 |
// Copyright (c) 2007-2017 by The University of Queensland // |
| 4 |
// Centre for Geoscience Computing // |
| 5 |
// http://earth.uq.edu.au/centre-geoscience-computing // |
| 6 |
// // |
| 7 |
// Primary Business: Brisbane, Queensland, Australia // |
| 8 |
// Licensed under the Open Software License version 3.0 // |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 // |
| 10 |
// // |
| 11 |
///////////////////////////////////////////////////////////// |
| 12 |
|
| 13 |
#include "PolygonWithLines2D.h" |
| 14 |
|
| 15 |
//-- system includes -- |
| 16 |
#include <cstdlib> |
| 17 |
//#include <math> |
| 18 |
|
| 19 |
using std::rand; |
| 20 |
|
| 21 |
// --- STL includes --- |
| 22 |
#include <utility> |
| 23 |
#include <map> |
| 24 |
#include <list> |
| 25 |
|
| 26 |
using std::make_pair; |
| 27 |
using std::map; |
| 28 |
using std::list; |
| 29 |
|
| 30 |
//--- IO includes --- |
| 31 |
#include <iostream> |
| 32 |
|
| 33 |
|
| 34 |
/*! |
| 35 |
helper function, return random value between min & max |
| 36 |
*/ |
| 37 |
double PolygonWithLines2D::m_random(double imin,double imax) const |
| 38 |
{ |
| 39 |
return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX))); |
| 40 |
} |
| 41 |
|
| 42 |
PolygonWithLines2D::PolygonWithLines2D() |
| 43 |
{} |
| 44 |
|
| 45 |
/*! |
| 46 |
Construct regular n-sided polygon |
| 47 |
|
| 48 |
\param centre the center postition of the polygon |
| 49 |
\param rad the distance of the corners fron the center, i.e. the radius of the circle into which the polygon is incribed |
| 50 |
\param nsides nr. of sides |
| 51 |
\param add_bounding_lines add fitting lines to sides of polygon (yes/no) |
| 52 |
*/ |
| 53 |
PolygonWithLines2D::PolygonWithLines2D(const Vector3& centre,double rad,int nsides,bool add_bounding_lines) |
| 54 |
: m_centre(centre),m_radius(rad),m_nsides(nsides) |
| 55 |
{ |
| 56 |
Vector3 pmin, pmax; |
| 57 |
int i; |
| 58 |
double px,py,pz,theta,theta0; |
| 59 |
Line2D l; |
| 60 |
|
| 61 |
pmin[0]=m_centre[0]-m_radius; |
| 62 |
pmin[1]=m_centre[1]-m_radius; |
| 63 |
pmin[2]=m_centre[2]; |
| 64 |
pmax[0]=m_centre[0]+m_radius; |
| 65 |
pmax[1]=m_centre[1]+m_radius; |
| 66 |
pmax[2]=m_centre[2]; |
| 67 |
|
| 68 |
m_pmin=(pmin); |
| 69 |
m_pmax=(pmax); |
| 70 |
|
| 71 |
for (i=0;i<m_nsides;i++) { |
| 72 |
theta = 2*3.141591654*(double)(i)/(double)(m_nsides); |
| 73 |
theta0 = 0.5*3.141591654/(double)(m_nsides); |
| 74 |
px = m_centre[0] + m_radius * cos(theta-theta0); |
| 75 |
py = m_centre[1] + m_radius * sin(theta-theta0); |
| 76 |
pz = m_centre[2]; |
| 77 |
m_vertices[i] = Vector3(px,py,pz); |
| 78 |
} |
| 79 |
if (add_bounding_lines == true) { |
| 80 |
for (i=0;i<m_nsides-1;i++) { |
| 81 |
l = Line2D(m_vertices[i],m_vertices[i+1]); |
| 82 |
addLine(l); |
| 83 |
} |
| 84 |
l = Line2D(m_vertices[m_nsides-1],m_vertices[0]); |
| 85 |
addLine(l); |
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
/*! |
| 91 |
Construct polygon from list of corner points |
| 92 |
|
| 93 |
\param corners the list of corner points as Vector3 |
| 94 |
*/ |
| 95 |
PolygonWithLines2D::PolygonWithLines2D(boost::python::list corners) |
| 96 |
{ |
| 97 |
// convert python list to STL-list |
| 98 |
boost::python::stl_input_iterator<Vector3> begin(corners), end; |
| 99 |
list<Vector3> vertex_list=list<Vector3>(begin, end); |
| 100 |
|
| 101 |
|
| 102 |
int idx=0; |
| 103 |
//go though list of corner points |
| 104 |
for(list<Vector3>::iterator iter=vertex_list.begin(); |
| 105 |
iter!=vertex_list.end(); |
| 106 |
iter++){ |
| 107 |
m_vertices[idx]=*iter; |
| 108 |
idx++; |
| 109 |
// update bounding box |
| 110 |
if(idx==1){ |
| 111 |
m_pmin=*iter; |
| 112 |
m_pmax=*iter; |
| 113 |
} |
| 114 |
m_pmin=comp_min(m_pmin,*iter); |
| 115 |
m_pmax=comp_max(m_pmax,*iter); |
| 116 |
} |
| 117 |
// m_vertices[idx]=m_vertices[0]; // closure |
| 118 |
std::cout << m_pmin << " - " << m_pmax << std::endl; |
| 119 |
m_nsides=idx; |
| 120 |
m_centre=0.5*(m_pmin+m_pmax); |
| 121 |
for(int i=0;i<m_nsides;i++){ |
| 122 |
std::cout << m_vertices[i] << std::endl; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/*! |
| 127 |
add a line |
| 128 |
|
| 129 |
\param l the line |
| 130 |
*/ |
| 131 |
void PolygonWithLines2D::addLine(const Line2D& l) |
| 132 |
{ |
| 133 |
m_lines.push_back(l); |
| 134 |
} |
| 135 |
|
| 136 |
/*! |
| 137 |
get bounding box |
| 138 |
*/ |
| 139 |
pair<Vector3,Vector3> PolygonWithLines2D::getBoundingBox() |
| 140 |
{ |
| 141 |
return make_pair(m_pmin,m_pmax); |
| 142 |
} |
| 143 |
|
| 144 |
/*! |
| 145 |
get point inside the box. The Argument is ignored |
| 146 |
*/ |
| 147 |
Vector3 PolygonWithLines2D::getAPoint(int) const |
| 148 |
{ |
| 149 |
double px,py; |
| 150 |
bool found_a_point; |
| 151 |
Vector3 aPoint; |
| 152 |
|
| 153 |
found_a_point = false; |
| 154 |
do { |
| 155 |
px=m_random(m_pmin.x(),m_pmax.x()); |
| 156 |
py=m_random(m_pmin.y(),m_pmax.y()); |
| 157 |
aPoint = Vector3(px,py,0.0); |
| 158 |
found_a_point = isIn(aPoint); |
| 159 |
} while (found_a_point==false); |
| 160 |
|
| 161 |
return Vector3(px,py,0.0); |
| 162 |
} |
| 163 |
|
| 164 |
/*! |
| 165 |
Return closest plane to a given point. Assumes that the list of Planes/Lines is not empty. |
| 166 |
|
| 167 |
\param p the point |
| 168 |
\warning check non-empty list of lines (via hasPlane()) before invoking |
| 169 |
*/ |
| 170 |
Line2D PolygonWithLines2D::getClosestPlane(const Vector3& p) |
| 171 |
{ |
| 172 |
std::cout << "getClosestPlane : " << p << std::endl; |
| 173 |
|
| 174 |
vector<Line2D>::iterator PL=m_lines.begin(); |
| 175 |
double dist=PL->getDist(p); |
| 176 |
|
| 177 |
for(vector<Line2D>::iterator iter=m_lines.begin(); |
| 178 |
iter!=m_lines.end(); |
| 179 |
iter++){ |
| 180 |
double ndist=iter->getDist(p); |
| 181 |
std::cout << "Line: " << *iter << " Distance: " << ndist << std::endl; |
| 182 |
if(ndist<dist){ |
| 183 |
PL=iter; |
| 184 |
dist=ndist; |
| 185 |
} |
| 186 |
} |
| 187 |
std::cout << "closest line: " << *PL << " Distance: " << dist << std::endl; |
| 188 |
|
| 189 |
return (*PL); |
| 190 |
} |
| 191 |
|
| 192 |
const map<double, const Line2D*> PolygonWithLines2D::getClosestPlanes(const Vector3& p,int nmax) const |
| 193 |
{ |
| 194 |
map<double,const Line2D*> res; |
| 195 |
|
| 196 |
for(vector<Line2D>::const_iterator iter=m_lines.begin(); |
| 197 |
iter!=m_lines.end(); |
| 198 |
iter++){ |
| 199 |
double ndist=iter->getDist(p); |
| 200 |
const Line2D *l=&(*iter); |
| 201 |
res.insert(make_pair(ndist,l)); |
| 202 |
} |
| 203 |
|
| 204 |
return res; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/*! |
| 209 |
check if point is inside |
| 210 |
|
| 211 |
\param p the point |
| 212 |
*/ |
| 213 |
bool PolygonWithLines2D::isIn(const Vector3& p) const |
| 214 |
{ |
| 215 |
bool inside,inbox; |
| 216 |
double dp,dv; |
| 217 |
Vector3 cp,cv; |
| 218 |
int i; |
| 219 |
|
| 220 |
inbox = ((p.x()>m_pmin.x()) && (p.x()<m_pmax.x()) && (p.y()>m_pmin.y()) && (p.y()<m_pmax.y())); |
| 221 |
|
| 222 |
if (inbox) { |
| 223 |
inside = true; |
| 224 |
|
| 225 |
for (i=0;i<m_nsides-1;i++) { |
| 226 |
cp = cross(m_vertices[i+1]-m_vertices[i],p-m_vertices[i]); |
| 227 |
cv = cross(m_vertices[i+1]-m_vertices[i],m_centre-m_vertices[i]); |
| 228 |
dp = cp.z(); |
| 229 |
dv = cv.z(); |
| 230 |
if (dp*dv<0.0) inside=false; |
| 231 |
} |
| 232 |
cp = cross(m_vertices[0]-m_vertices[m_nsides-1],p-m_vertices[m_nsides-1]); |
| 233 |
cv = cross(m_vertices[0]-m_vertices[m_nsides-1],m_centre-m_vertices[m_nsides-1]); |
| 234 |
dp = cp.z(); |
| 235 |
dv = cv.z(); |
| 236 |
if (dp*dv<0.0) inside=false; |
| 237 |
} |
| 238 |
|
| 239 |
// if (inside && inbox) std::cout << "inside " << p << std::endl; |
| 240 |
|
| 241 |
|
| 242 |
return (inside && inbox); |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
/*! |
| 247 |
check if a Sphere is inside and doesn't intersect any lines |
| 248 |
|
| 249 |
\param S the Sphere |
| 250 |
*/ |
| 251 |
bool PolygonWithLines2D::isIn(const Sphere& S) |
| 252 |
{ |
| 253 |
double r=S.Radius(); |
| 254 |
Vector3 p=S.Center(); |
| 255 |
|
| 256 |
// std::cout << "PolygonWithLines2D rad: " << r << " pos: " << p << std::endl; |
| 257 |
|
| 258 |
// bool inside=(p.X()>m_pmin.X()+r) && (p.X()<m_pmax.X()-r) && (p.Y()>m_pmin.Y()+r) && (p.Y()<m_pmax.Y()-r); |
| 259 |
bool inside = isIn(p); |
| 260 |
|
| 261 |
vector<Line2D>::iterator iter=m_lines.begin(); |
| 262 |
|
| 263 |
double dist=2*r; |
| 264 |
while(iter!=m_lines.end() && dist>r){ |
| 265 |
dist=iter->getDist(p); |
| 266 |
iter++; |
| 267 |
} |
| 268 |
|
| 269 |
return inside && (dist>r); |
| 270 |
} |
| 271 |
|
| 272 |
ostream& operator<< (ostream& ost, const PolygonWithLines2D& L) |
| 273 |
{ |
| 274 |
ost << " centre: " << L.m_centre << " radius: " << L.m_radius << " number of sides: " << L.m_nsides; |
| 275 |
|
| 276 |
return ost; |
| 277 |
} |
| 278 |
|