oomph::PolygonHelper Namespace Reference

Namespace with helper function for polygons. More...

Functions

bool point_is_in_polygon (const Vector< double > &point, const Vector< Vector< double > > &polygon_vertex)
 

Detailed Description

Namespace with helper function for polygons.

///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////

Function Documentation

◆ point_is_in_polygon()

bool oomph::PolygonHelper::point_is_in_polygon ( const Vector< double > &  point,
const Vector< Vector< double > > &  polygon_vertex 
)

Check if point in inside polygon. Reference: http://paulbourke.net/geometry/insidepoly/

434  {
435 
436  // Total number of vertices
437  unsigned nvertex=polygon_vertex.size();
438 
439 // faire: obviously polygons should ALWAYS be designed to be closed, so
440 // such testing should only need to be done once upon implementation of
441 // the polygon, not everytime we want to check number of intersections
442 // hierher #ifdef PARANOID
443 
444  // Make sure the polygon closes exactly:
445  double d0=polygon_vertex[nvertex-1][0]-polygon_vertex[0][0];
446  double d1=polygon_vertex[nvertex-1][1]-polygon_vertex[0][1];
447  if (sqrt(d0*d0+d1*d1)>0.0)
448  {
449  std::stringstream junk;
450  junk << "First and last point of polygon don't coincide!\n"
451  << "First point at: "
452  << polygon_vertex[0][0] << " "
453  << polygon_vertex[0][1] << "\n"
454  << "Last point at: "
455  << polygon_vertex[nvertex-1][0] << " "
456  << polygon_vertex[nvertex-1][1] << "\n";
457  throw OomphLibError(
458  junk.str(),
461  }
462 
463 // #endif
464 
465  // Counter for number of intersections
466  unsigned intersect_counter=0;
467 
468  //Get first vertex
469  Vector<double> p1=polygon_vertex[0];
470  for (unsigned i=1;i<=nvertex;i++)
471  {
472  // Get second vertex by wrap-around
473  Vector<double> p2 = polygon_vertex[i%nvertex];
474 
475  if (point[1] > std::min(p1[1],p2[1]))
476  {
477  if (point[1] <= std::max(p1[1],p2[1]))
478  {
479  if (point[0] <= std::max(p1[0],p2[0]))
480  {
481  if (p1[1] != p2[1])
482  {
483  double xintersect =
484  (point[1]-p1[1])*(p2[0]-p1[0])/
485  (p2[1]-p1[1])+p1[0];
486  if ( (p1[0] == p2[0]) ||
487  (point[0] <= xintersect) )
488  {
489  intersect_counter++;
490  }
491  }
492  }
493  }
494  }
495  p1 = p2;
496  }
497 
498  // Even number of intersections: outside
499  if (intersect_counter%2==0)
500  {
501  return false;
502  }
503  return true;
504  }
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Vector3f p1
Definition: MatrixBase_all.cpp:2
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
#define OOMPH_EXCEPTION_LOCATION
Definition: oomph_definitions.h:61
#define OOMPH_CURRENT_FUNCTION
Definition: oomph_definitions.h:86

References i, max, min, OOMPH_CURRENT_FUNCTION, OOMPH_EXCEPTION_LOCATION, p1, and sqrt().