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.

11450 {
11451  auto pmin = h2d::priv::getBmPoint_helper( in );
11452  return static_cast<size_t>( pmin - in.begin() );
11453 }
auto getBmPoint_helper(const T &t)
Return iterator on Bottom-most point of container holding points.
Definition: homog2d.hpp:6879
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
11501 {
11502  HOMOG2D_INUMTYPE px = p.getX();
11503  HOMOG2D_INUMTYPE py = p.getY();
11504  HOMOG2D_INUMTYPE qx = q.getX();
11505  HOMOG2D_INUMTYPE qy = q.getY();
11506  HOMOG2D_INUMTYPE rx = r.getX();
11507  HOMOG2D_INUMTYPE ry = r.getY();
11508 
11509  auto val = (qy - py) * (rx - qx) - (qx - px) * (ry - qy);
11510 
11511  if( homog2d_abs(val) < HOMOG2D_THR_ZERO_DETER )
11512  return 0; // collinear
11513  return (val > 0 ? 1 : -1 ); // clock or counterclock wise
11514 }
#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.

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