homog2d library
Classes | Functions
h2d::priv::chull Namespace Reference

Holds convex hull code. More...

Classes

struct  Mystack
 Inherits std::stack<> and adds a member function to fetch the underlying std::vector. Used in h2d::convexHull() More...
 

Functions

template<typename FPT >
size_t getPivotPoint (const std::vector< Point2d_< FPT >> &in)
 Used int the convex hull algorithm. More...
 
template<typename T >
int orientation (Point2d_< T > p, Point2d_< T > q, Point2d_< T > r)
 To find orientation of ordered triplet of points (p, q, r). More...
 
template<typename FPT >
std::vector< size_t > sortPoints (const std::vector< Point2d_< FPT >> &in, size_t piv_idx)
 Sorts points by angle between the lines with horizontal axis. More...
 

Detailed Description

Holds convex hull code.

Function Documentation

◆ getPivotPoint()

template<typename FPT >
size_t h2d::priv::chull::getPivotPoint ( const std::vector< Point2d_< FPT >> &  in)

Used int the convex hull algorithm.

11453 {
11454  auto pmin = h2d::priv::getBmPoint_helper( in );
11455  return static_cast<size_t>( pmin - in.begin() );
11456 }
auto getBmPoint_helper(const T &t)
Return iterator on Bottom-most point of container holding points.
Definition: homog2d.hpp:6882
Here is the call graph for this function:
Here is the caller graph for this function:

◆ orientation()

template<typename T >
int h2d::priv::chull::orientation ( Point2d_< T >  p,
Point2d_< T >  q,
Point2d_< T >  r 
)

To find orientation of ordered triplet of points (p, q, r).

The function returns following values

  • 0 –> p, q and r are colinear
  • 1 –> Clockwise
  • 2 –> Counterclockwise
Todo:
20240326: this is subject to numerical instability, as it is based on differences.
Todo:
20230212: replace const value HOMOG2D_THR_ZERO_DETER with related static function
11504 {
11505  HOMOG2D_INUMTYPE px = p.getX();
11506  HOMOG2D_INUMTYPE py = p.getY();
11507  HOMOG2D_INUMTYPE qx = q.getX();
11508  HOMOG2D_INUMTYPE qy = q.getY();
11509  HOMOG2D_INUMTYPE rx = r.getX();
11510  HOMOG2D_INUMTYPE ry = r.getY();
11511 
11512  auto val = (qy - py) * (rx - qx) - (qx - px) * (ry - qy);
11513 
11514  if( homog2d_abs(val) < HOMOG2D_THR_ZERO_DETER )
11515  return 0; // collinear
11516  return (val > 0 ? 1 : -1 ); // clock or counterclock wise
11517 }
#define HOMOG2D_INUMTYPE
Definition: homog2d.hpp:66
#define homog2d_abs
Definition: homog2d.hpp:69
#define HOMOG2D_THR_ZERO_DETER
Definition: homog2d.hpp:221
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sortPoints()

template<typename FPT >
std::vector<size_t> h2d::priv::chull::sortPoints ( const std::vector< Point2d_< FPT >> &  in,
size_t  piv_idx 
)

Sorts points by angle between the lines with horizontal axis.

11463 {
11464  assert( in.size()>3 );
11465 
11466 // step 1: create new vector holding the indexes of the points, including the pivot point (will be in first position)
11467  std::vector<size_t> out( in.size() );
11468  std::iota( out.begin(), out.end(), 0 ); // fill vector: [0,1,2,...]
11469  std::swap( out[piv_idx], out[0] );
11470  auto pt0 = in[piv_idx];
11471 
11472 // step 2: sort points by angle of lines between the current point and pivot point
11473  std::sort(
11474  out.begin()+1,
11475  out.end(),
11476  [&] // lambda
11477  ( size_t i1, size_t i2 )
11478  {
11479  auto pt1 = in[i1];
11480  auto pt2 = in[i2];
11481  auto dx1 = pt1.getX() - pt0.getX();
11482  auto dy1 = pt1.getY() - pt0.getY();
11483  auto dx2 = pt2.getX() - pt0.getX();
11484  auto dy2 = pt2.getY() - pt0.getY();
11485  return ((dx1 * dy2 - dx2 * dy1) > 0);
11486  }
11487  );
11488  return out;
11489 }
Here is the call graph for this function:
Here is the caller graph for this function: