///////////////////////////////////////////////////////////// // // // Copyright (c) 2007-2017 by The University of Queensland // // Centre for Geoscience Computing // // http://earth.uq.edu.au/centre-geoscience-computing // // // // Primary Business: Brisbane, Queensland, Australia // // Licensed under the Open Software License version 3.0 // // http://www.apache.org/licenses/LICENSE-2.0 // // // ///////////////////////////////////////////////////////////// #include "PolygonWithLines2D.h" //-- system includes -- #include //#include using std::rand; // --- STL includes --- #include #include #include using std::make_pair; using std::map; using std::list; //--- IO includes --- #include /*! helper function, return random value between min & max */ double PolygonWithLines2D::m_random(double imin,double imax) const { return imin+((imax-imin)*((double)(rand())/(double)(RAND_MAX))); } PolygonWithLines2D::PolygonWithLines2D() {} /*! Construct regular n-sided polygon \param centre the center postition of the polygon \param rad the distance of the corners fron the center, i.e. the radius of the circle into which the polygon is incribed \param nsides nr. of sides \param add_bounding_lines add fitting lines to sides of polygon (yes/no) */ PolygonWithLines2D::PolygonWithLines2D(const Vector3& centre,double rad,int nsides,bool add_bounding_lines) : m_centre(centre),m_radius(rad),m_nsides(nsides) { Vector3 pmin, pmax; int i; double px,py,pz,theta,theta0; Line2D l; pmin[0]=m_centre[0]-m_radius; pmin[1]=m_centre[1]-m_radius; pmin[2]=m_centre[2]; pmax[0]=m_centre[0]+m_radius; pmax[1]=m_centre[1]+m_radius; pmax[2]=m_centre[2]; m_pmin=(pmin); m_pmax=(pmax); for (i=0;i begin(corners), end; list vertex_list=list(begin, end); int idx=0; //go though list of corner points for(list::iterator iter=vertex_list.begin(); iter!=vertex_list.end(); iter++){ m_vertices[idx]=*iter; idx++; // update bounding box if(idx==1){ m_pmin=*iter; m_pmax=*iter; } m_pmin=comp_min(m_pmin,*iter); m_pmax=comp_max(m_pmax,*iter); } // m_vertices[idx]=m_vertices[0]; // closure std::cout << m_pmin << " - " << m_pmax << std::endl; m_nsides=idx; m_centre=0.5*(m_pmin+m_pmax); for(int i=0;i PolygonWithLines2D::getBoundingBox() { return make_pair(m_pmin,m_pmax); } /*! get point inside the box. The Argument is ignored */ Vector3 PolygonWithLines2D::getAPoint(int) const { double px,py; bool found_a_point; Vector3 aPoint; found_a_point = false; do { px=m_random(m_pmin.x(),m_pmax.x()); py=m_random(m_pmin.y(),m_pmax.y()); aPoint = Vector3(px,py,0.0); found_a_point = isIn(aPoint); } while (found_a_point==false); return Vector3(px,py,0.0); } /*! Return closest plane to a given point. Assumes that the list of Planes/Lines is not empty. \param p the point \warning check non-empty list of lines (via hasPlane()) before invoking */ Line2D PolygonWithLines2D::getClosestPlane(const Vector3& p) { std::cout << "getClosestPlane : " << p << std::endl; vector::iterator PL=m_lines.begin(); double dist=PL->getDist(p); for(vector::iterator iter=m_lines.begin(); iter!=m_lines.end(); iter++){ double ndist=iter->getDist(p); std::cout << "Line: " << *iter << " Distance: " << ndist << std::endl; if(ndist PolygonWithLines2D::getClosestPlanes(const Vector3& p,int nmax) const { map res; for(vector::const_iterator iter=m_lines.begin(); iter!=m_lines.end(); iter++){ double ndist=iter->getDist(p); const Line2D *l=&(*iter); res.insert(make_pair(ndist,l)); } return res; } /*! check if point is inside \param p the point */ bool PolygonWithLines2D::isIn(const Vector3& p) const { bool inside,inbox; double dp,dv; Vector3 cp,cv; int i; inbox = ((p.x()>m_pmin.x()) && (p.x()m_pmin.y()) && (p.y()m_pmin.X()+r) && (p.X()m_pmin.Y()+r) && (p.Y()::iterator iter=m_lines.begin(); double dist=2*r; while(iter!=m_lines.end() && dist>r){ dist=iter->getDist(p); iter++; } return inside && (dist>r); } ostream& operator<< (ostream& ost, const PolygonWithLines2D& L) { ost << " centre: " << L.m_centre << " radius: " << L.m_radius << " number of sides: " << L.m_nsides; return ost; }