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.

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

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