homog2d library
Public Types | Public Member Functions | Friends | List of all members
h2d::FRect_< FPT > Class Template Reference

A Flat Rectangle, modeled by its two opposite points. More...

#include <homog2d.hpp>

Inheritance diagram for h2d::FRect_< FPT >:
Inheritance graph
[legend]
Collaboration diagram for h2d::FRect_< FPT >:
Collaboration graph
[legend]

Public Types

using FType = FPT
 
using SType = typ::T_FRect
 

Public Member Functions

void draw (img::Image< cv::Mat > &, img::DrawParams dp=img::DrawParams()) const
 Draw FRect (Opencv implementation) More...
 
void draw (img::Image< img::SvgImage > &, img::DrawParams dp=img::DrawParams()) const
 Draw FRect (SVG implementation) More...
 
std::array< Point2d_< FPT >, 4 > get4Pts () const
 Returns the 4 points of the rectangle, starting from "smallest" one, and in clockwise order. More...
 
std::pair< Segment_< FPT >, Segment_< FPT > > getDiagonals () const
 
FRect_< FPT > getExtended () const
 
std::array< Segment_< FPT >, 4 > getSegs () const
 Returns the 4 segments of the rectangle, starting with the first vertical one. More...
 
template<typename FPT1 , typename FPT2 >
void set (const Point2d_< FPT1 > &pa, const Point2d_< FPT2 > &pb)
 Assigns points pa and pb to rectangle. More...
 
template<typename T >
void set (T x1, T y1, T x2, T y2)
 Assigns points (x1,y1) and (x2,y2) to rectangle. More...
 
Type type () const
 
Constructors
 FRect_ ()
 Default constructor, initialize rectangle to (0,0)-(1,1) More...
 
template<typename FPT2 >
 FRect_ (const Point2d_< FPT2 > &pa, const Point2d_< FPT2 > &pb)
 Constructor from 2 points. More...
 
template<typename FPT2 >
 FRect_ (const PointPair_< FPT2 > &ppts)
 Constructor from pair of points. More...
 
template<typename FPT2 , typename T1 , typename T2 >
 FRect_ (const Point2d_< FPT2 > &p0, T1 w, T2 h)
 Constructor from center point, width and height. More...
 
template<typename T1 , typename T2 , typename T3 , typename T4 >
 FRect_ (T1 x1, T2 y1, T3 x2, T4 y2)
 Constructor from x1, y1, x2, y2. More...
 
template<typename FPT2 >
 FRect_ (const FRect_< FPT2 > &other)
 Copy-Constructor. More...
 
Attributes access
HOMOG2D_INUMTYPE height () const
 
HOMOG2D_INUMTYPE width () const
 
HOMOG2D_INUMTYPE area () const
 
HOMOG2D_INUMTYPE length () const
 
constexpr size_t size () const
 
FRect_< FPT > getBB () const
 get BB of rectangle. Needed for getBB( pair of objects ) More...
 
PointPair_< FPT > getPts () const
 Returns the 2 major points of the rectangle. More...
 
Point2d_< FPT > getCenter () const
 Returns center of rectangle. More...
 
Circle_< FPT > getBoundingCircle () const
 Return circle passing through 4 points of flat rectangle. More...
 
Circle_< FPT > getInscribedCircle () const
 Return circle inscribed in rectangle. More...
 
Modifying functions
template<typename TX , typename TY >
void translate (TX dx, TY dy)
 Translate FRect. More...
 
template<typename T1 , typename T2 >
void translate (const std::pair< T1, T2 > &pa)
 Translate FRect. More...
 
template<typename TX , typename TY >
void moveTo (TX x, TY y)
 Move FRect to other location. More...
 
template<typename T1 >
void moveTo (const Point2d_< T1 > &pt)
 Move FRect to other location, given by pt. More...
 
template<typename FPT2 >
void rotate (Rotate, const Point2d_< FPT2 > &)
 Rotates the rectangle by either 90°, 180°, 270° (-90°) at point refpt. More...
 
void rotate (Rotate)
 Rotates the rectangle by either 90°, 180°, 270° (-90°) at point (0,0) More...
 
Union/intersection area
template<typename FPT2 >
CPolyline_< FPT > unionArea (const FRect_< FPT2 > &other) const
 Computes the CPolyline of the union of two rectangles. More...
 
template<typename FPT2 >
detail::RectArea< FPT > intersectArea (const FRect_< FPT2 > &other) const
 Returns Rectangle of the intersection area of two rectangles. More...
 
template<typename FPT2 >
CPolyline_< FPT > operator| (const FRect_< FPT2 > &other) const
 
template<typename FPT2 >
detail::RectArea< FPT > operator& (const FRect_< FPT2 > &other) const
 
Enclosing functions
template<typename T >
bool isInside (const T &shape) const
 Returns true if rectangle is inside shape (Circle_ or FRect_ or base::Polyline) More...
 
template<typename FPT2 >
constexpr bool isInside (const OPolyline_< FPT2 > &) const
 A FRect is never inside an open polyline. More...
 
template<typename FPT2 >
bool isInside (const CPolyline_< FPT2 > &poly) const
 For a rectangle to be inside a closed Polyline, two conditions are necessary: More...
 
Intersection functions
template<typename FPT2 >
detail::IntersectM< FPT > intersects (const Line2d_< FPT2 > &line) const
 FRect/Line intersection. More...
 
template<typename FPT2 >
detail::IntersectM< FPT > intersects (const Segment_< FPT2 > &seg) const
 FRect/Segment intersection. More...
 
template<typename FPT2 >
detail::IntersectM< FPT > intersects (const Circle_< FPT2 > &circle) const
 FRect/Circle intersection. More...
 
template<typename PLT2 , typename FPT2 >
detail::IntersectM< FPT > intersects (const base::PolylineBase< PLT2, FPT2 > &pl) const
 FRect/Polyline intersection. More...
 
template<typename FPT2 >
detail::IntersectM< FPT > intersects (const FRect_< FPT2 > &rect) const
 FRect/FRect intersection. More...
 
Operators
template<typename FPT2 >
bool operator== (const FRect_< FPT2 > &other) const
 
template<typename FPT2 >
bool operator!= (const FRect_< FPT2 > &other) const
 
- Public Member Functions inherited from h2d::detail::Common< FPT >
std::pair< int, int > dsize () const
 Get data size expressed as number of bits for, respectively, mantissa and exponent. More...
 
Dtype dtype () const
 Get numerical data type as a Dtype value, can be stringified with h2d::getString(Dtype) More...
 
template<typename T >
constexpr bool isInside (const Common< T > &) const
 This function is a fallback for all sub-classes that do not provide such a method. More...
 
size_t size () const
 
- Public Member Functions inherited from h2d::rtp::Root
virtual ~Root ()
 

Friends

template<typename T >
class FRect_
 
template<typename T >
std::ostream & operator<< (std::ostream &f, const FRect_< T > &r)
 

Detailed Description

template<typename FPT>
class h2d::FRect_< FPT >

A Flat Rectangle, modeled by its two opposite points.

Member Typedef Documentation

◆ FType

template<typename FPT>
using h2d::FRect_< FPT >::FType = FPT

◆ SType

template<typename FPT>
using h2d::FRect_< FPT >::SType = typ::T_FRect

Constructor & Destructor Documentation

◆ FRect_() [1/6]

template<typename FPT>
h2d::FRect_< FPT >::FRect_ ( )
inline

Default constructor, initialize rectangle to (0,0)-(1,1)

2650  {
2651  _ptR2.set( 1., 1. );
2652  }

◆ FRect_() [2/6]

template<typename FPT>
template<typename FPT2 >
h2d::FRect_< FPT >::FRect_ ( const Point2d_< FPT2 > &  pa,
const Point2d_< FPT2 > &  pb 
)
inline

Constructor from 2 points.

2656  {
2657  set( pa, pb );
2658  }

◆ FRect_() [3/6]

template<typename FPT>
template<typename FPT2 >
h2d::FRect_< FPT >::FRect_ ( const PointPair_< FPT2 > &  ppts)
inline

Constructor from pair of points.

2663  {
2664  set( ppts.first, ppts.second );
2665  }

◆ FRect_() [4/6]

template<typename FPT>
template<typename FPT2 , typename T1 , typename T2 >
h2d::FRect_< FPT >::FRect_ ( const Point2d_< FPT2 > &  p0,
T1  w,
T2  h 
)
inline

Constructor from center point, width and height.

2670  {
2673  set(
2674  Point2d_<FPT>( p0.getX()-0.5L*w, p0.getY()-0.5L*h ),
2675  Point2d_<FPT>( p0.getX()+0.5L*w, p0.getY()+0.5L*h )
2676  );
2677  }
#define HOMOG2D_CHECK_IS_NUMBER(T)
Definition: homog2d.hpp:144
Here is the call graph for this function:

◆ FRect_() [5/6]

template<typename FPT>
template<typename T1 , typename T2 , typename T3 , typename T4 >
h2d::FRect_< FPT >::FRect_ ( T1  x1,
T2  y1,
T3  x2,
T4  y2 
)
inline

Constructor from x1, y1, x2, y2.

2682  {
2687  set( Point2d_<FPT>(x1,y1), Point2d_<FPT>(x2,y2) );
2688  }
#define HOMOG2D_CHECK_IS_NUMBER(T)
Definition: homog2d.hpp:144

◆ FRect_() [6/6]

template<typename FPT>
template<typename FPT2 >
h2d::FRect_< FPT >::FRect_ ( const FRect_< FPT2 > &  other)
inline

Copy-Constructor.

2693  : _ptR1(other._ptR1),_ptR2(other._ptR2)
2694  {}
Here is the call graph for this function:

Member Function Documentation

◆ area()

template<typename FPT>
HOMOG2D_INUMTYPE h2d::FRect_< FPT >::area ( ) const
inlinevirtual

Implements h2d::rtp::Root.

2747 { return height() * width(); }
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2746
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2745
Here is the call graph for this function:
Here is the caller graph for this function:

◆ draw() [1/2]

template<typename FPT >
void h2d::FRect_< FPT >::draw ( img::Image< cv::Mat > &  im,
img::DrawParams  dp = img::DrawParams() 
) const
virtual

Draw FRect (Opencv implementation)

Implements h2d::rtp::Root.

11846 {
11847  cv::rectangle(
11848  im.getReal(),
11849  _ptR1.getCvPti(),
11850  _ptR2.getCvPti(),
11851  dp.cvColor(),
11852  dp._dpValues._lineThickness,
11853  dp._dpValues._lineType==1?cv::LINE_AA:cv::LINE_8
11854  );
11855  if( dp._dpValues._showPoints )
11856  {
11857  _ptR1.draw( im, dp );
11858  _ptR2.draw( im, dp );
11859  }
11860 }
img::Image< img::SvgImage > im(300, 400)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ draw() [2/2]

template<typename FPT >
void h2d::FRect_< FPT >::draw ( img::Image< img::SvgImage > &  im,
img::DrawParams  dp = img::DrawParams() 
) const
virtual

Draw FRect (SVG implementation)

Implements h2d::rtp::Root.

12115 {
12116  im.getReal()._svgString << "<rect x=\""
12117  << getPts().first.getX()
12118  << "\" y=\""
12119  << getPts().first.getY()
12120  << "\" width=\""
12121  << width()
12122  << "\" height=\""
12123  << height()
12124  << "\" stroke=\""
12125  << dp.getSvgRgbColor()
12126  << "\" stroke-width=\"" << dp._dpValues._lineThickness << "\" ";
12127  if( !dp.holdsFill() )
12128  im.getReal()._svgString << "fill=\"none\" ";
12129  im.getReal()._svgString << dp.getAttrString() << "/>\n";
12130 }
img::Image< img::SvgImage > im(300, 400)
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2746
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2745
PointPair_< FPT > getPts() const
Returns the 2 major points of the rectangle.
Definition: homog2d.hpp:2767
Here is the call graph for this function:

◆ get4Pts()

template<typename FPT>
std::array<Point2d_<FPT>,4> h2d::FRect_< FPT >::get4Pts ( ) const
inline

Returns the 4 points of the rectangle, starting from "smallest" one, and in clockwise order.

 p1 +------+ p2
    |      |
    |      |
    |      |
 p0 +------+ p3
See also
get4Pts( const FRect_<FPT>& )
2911  {
2912  std::array<Point2d_<FPT>,4> arr;
2913  arr[0] = _ptR1;
2914  arr[1] = Point2d_<FPT>( _ptR1.getX(), _ptR2.getY() );
2915  arr[2] = _ptR2;
2916  arr[3] = Point2d_<FPT>( _ptR2.getX(), _ptR1.getY() );
2917  return arr;
2918  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBB()

template<typename FPT>
FRect_<FPT> h2d::FRect_< FPT >::getBB ( ) const
inline

get BB of rectangle. Needed for getBB( pair of objects )

Todo:
20250205: fix this so that it return a "full res" rectangle
2758  { //FRect_<HOMOG2D_INUMTYPE> out;
2759 // out = *this;
2760  return *this;
2761 // return out;
2762  }
Here is the caller graph for this function:

◆ getBoundingCircle()

template<typename FPT>
Circle_<FPT> h2d::FRect_< FPT >::getBoundingCircle ( ) const
inline

Return circle passing through 4 points of flat rectangle.

See also
h2d::getBoundingCircle()
2784  {
2785  auto middle_pt = getCenter();
2786  return Circle_<FPT>( middle_pt, middle_pt.distTo( _ptR1 ) );
2787  }
Point2d_< FPT > getCenter() const
Returns center of rectangle.
Definition: homog2d.hpp:2773
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getCenter()

template<typename FPT>
Point2d_<FPT> h2d::FRect_< FPT >::getCenter ( ) const
inline

Returns center of rectangle.

2774  {
2775  return Point2d_<FPT>(
2776  (static_cast<HOMOG2D_INUMTYPE>(_ptR1.getX() ) + _ptR2.getX() ) * 0.5,
2777  (static_cast<HOMOG2D_INUMTYPE>(_ptR1.getY() ) + _ptR2.getY() ) * 0.5
2778  );
2779  }
#define HOMOG2D_INUMTYPE
Definition: homog2d.hpp:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDiagonals()

template<typename FPT>
std::pair<Segment_<FPT>,Segment_<FPT> > h2d::FRect_< FPT >::getDiagonals ( ) const
inline
2865  {
2866  auto x1 = _ptR1.getX();
2867  auto y1 = _ptR1.getY();
2868  auto x2 = _ptR2.getX();
2869  auto y2 = _ptR2.getY();
2870 
2871  return std::make_pair(
2872  Segment_<FPT>(x1,y1,x2,y2),
2873  Segment_<FPT>(x1,y2,x2,y1)
2874  );
2875  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getExtended()

template<typename FPT>
FRect_<FPT> h2d::FRect_< FPT >::getExtended ( ) const
inline
2853  {
2854  auto x1 = _ptR1.getX() * 2. - _ptR2.getX();
2855  auto y1 = _ptR1.getY() * 2. - _ptR2.getY();
2856 
2857  auto x2 = _ptR2.getX() * 2. - _ptR1.getX();
2858  auto y2 = _ptR2.getY() * 2. - _ptR1.getY();
2859 
2860  return FRect_<FPT>( x1, y1, x2, y2 );
2861  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getInscribedCircle()

template<typename FPT>
Circle_<FPT> h2d::FRect_< FPT >::getInscribedCircle ( ) const
inline

Return circle inscribed in rectangle.

See also
h2d::getInscribedCircle()
2792  {
2793  auto segs = getSegs();
2794  auto center = getCenter();
2795  Circle_<FPT> cir( center );
2796  cir.radius() = std::min(
2797  center.distTo( segs[0] ),
2798  center.distTo( segs[1] )
2799  );
2800  return cir;
2801  }
Point2d_< FPT > getCenter() const
Returns center of rectangle.
Definition: homog2d.hpp:2773
Point2d_< FPT > & center(Circle_< FPT > &cir)
Returns reference on center of circle (free function), non-const version.
Definition: homog2d.hpp:10934
std::array< Segment_< FPT >, 4 > getSegs() const
Returns the 4 segments of the rectangle, starting with the first vertical one.
Definition: homog2d.hpp:2934
Circle cir
Definition: homog2d_test.cpp:4036
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPts()

template<typename FPT>
PointPair_<FPT> h2d::FRect_< FPT >::getPts ( ) const
inline

Returns the 2 major points of the rectangle.

See also
getPts( const FRect_<FPT>& )
2768  {
2769  return std::make_pair( _ptR1, _ptR2 );
2770  }
Here is the caller graph for this function:

◆ getSegs()

template<typename FPT>
std::array<Segment_<FPT>,4> h2d::FRect_< FPT >::getSegs ( ) const
inline

Returns the 4 segments of the rectangle, starting with the first vertical one.

      s1
   +------+p2
   |      |
s0 |      | s2
   |      |
 p1+------+
      s3
See also
h2d::getSegs( const FRect_& )
2935  {
2936  auto pts = get4Pts();
2937  std::array<Segment_<FPT>,4> out;
2938  out[0] = Segment_<FPT>( pts[0], pts[1] );
2939  out[1] = Segment_<FPT>( pts[1], pts[2] );
2940  out[2] = Segment_<FPT>( pts[2], pts[3] );
2941  out[3] = Segment_<FPT>( pts[3], pts[0] );
2942  return out;
2943  }
std::array< Point2d_< FPT >, 4 > get4Pts() const
Returns the 4 points of the rectangle, starting from "smallest" one, and in clockwise order...
Definition: homog2d.hpp:2910
Here is the call graph for this function:
Here is the caller graph for this function:

◆ height()

template<typename FPT>
HOMOG2D_INUMTYPE h2d::FRect_< FPT >::height ( ) const
inline
2745 { return _ptR2.getY() - _ptR1.getY(); }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ intersectArea()

template<typename FPT >
template<typename FPT2 >
detail::RectArea< FPT > h2d::FRect_< FPT >::intersectArea ( const FRect_< FPT2 > &  other) const

Returns Rectangle of the intersection area of two rectangles.

Returns
An object of type detail::RectArea, that can be checked for success using the () operator, and if success, will hold the resulting FRect_

Algorithm:

4 situations need to be considered, depending on the number of intersection points:

  • A: 2 intersection points on same segment => we have 2 points inside.
      +------+                   +------+
      |      |                   |      |
      |   +--+-----+          +--+----+ |
      |   |  |     |          |  |    | |
      |   |  |     |    or    |  |    | |  or ...
      |   +--+-----+          +--+----+ |
      |      |                   |      |
      +------+                   +------+
    
  • B: 2 intersection points on different segments => for each rectangle, 1 point is inside the other.
      +------+
      |      |
      |      |
      |   +--+-----+
      |   |  |     |   or ...
      +---+--+     |
          |        |
          +--------+
    
  • C: 3 points
      +------+-----+
      |      |     |
      |      |     |
      +------+-----+
      |      |
      +------+
    
  • D: 4 intersection points: the intersection rectangle is made of these 4 points
         +------+
         |      |
      +--+------+-----+
      |  |      |     |
      |  |      |     |
      +--+------+-----+
         |      |
         +------+
    
7714 {
7715  if( *this == other ) // if same rectangles, then
7716  return detail::RectArea<FPT>(other); // the intersection area is the rectangle
7717 
7718  auto inter = this->intersects( other );
7719 
7720  if( !inter() ) // rectangles do not intersect
7721  {
7722  if( this->isInside( other ) )
7723  return detail::RectArea<FPT>(*this);
7724  if( other.isInside( *this ) )
7725  return detail::RectArea<FPT>(other);
7726  return detail::RectArea<FPT>();
7727  }
7728 
7729  if( inter.size() < 2 ) // only one intersection point
7730  return detail::RectArea<FPT>(); // => no intersection area!
7731 
7732  if( inter.size() == 4 ) // 4 intersection points => case "D"
7733  return detail::RectArea<FPT>(FRect_<FPT>( inter.get().at(0), inter.get().at(3) ) );
7734 
7735  if( inter.size() == 3 ) // 3 intersection points => case "C"
7736  {
7737  auto v = inter.get();
7738 
7739  auto xmin = std::min( v[0].getX(), std::min( v[1].getX(),v[2].getX() ) );
7740  auto ymin = std::min( v[0].getY(), std::min( v[1].getY(),v[2].getY() ) );
7741  auto xmax = std::max( v[0].getX(), std::max( v[1].getX(),v[2].getX() ) );
7742  auto ymax = std::max( v[0].getY(), std::max( v[1].getY(),v[2].getY() ) );
7743 #ifndef HOMOG2D_DEBUGMODE
7744  assert( xmax-xmin > thr::nullOrthogDistance() );
7745  assert( ymax-ymin > thr::nullOrthogDistance() );
7746 #else
7748  ( ( xmax-xmin > thr::nullOrthogDistance() )
7749  &&
7750  ( ymax-ymin > thr::nullOrthogDistance() )) ,
7751  std::scientific
7752  << "this=" << *this << " other=" << other
7753  << "\nxmax=" << xmax << " xmin=" << xmin
7754  << "\nymax=" << ymax << " ymin=" << ymin
7755  << "\nnod=" << thr::nullOrthogDistance()
7756  );
7757 #endif
7758  return detail::RectArea<FPT>( FRect_<FPT>( xmin, ymin, xmax,ymax ) );
7759  }
7760 /*------------------------
7761  If we get to this point, we have 2 intersection points
7762 ------------------------*/
7763 #ifndef HOMOG2D_DEBUGMODE
7764  assert( inter.size() == 2 );
7765 #else
7766 /* HOMOG2D_DEBUG_ASSERT(
7767  inter.size() == 2,
7768  "inter.size()=" << inter.size() << "\n-this=" << *this << "\n-other=" << other
7769  <<"\n-inter:" << inter
7770  );*/
7771 #endif
7772  const auto& r1 = *this;
7773  const auto& r2 = other;
7774 // HOMOG2D_LOG( "r1="<<r1 << " r2="<<r2 );
7775  auto v1 = r1.p_pointsInside( r2 );
7776  auto v2 = r2.p_pointsInside( r1 );
7777  auto c1 = v1.size();
7778  auto c2 = v2.size();
7779 // HOMOG2D_LOG( "c1=" << c1 << " c2=" << c2 );
7780 
7781  if( c1==0 && c2==0 ) // unable, rectangles share a segment
7782  return detail::RectArea<FPT>();
7783 
7784  assert(
7785  ( c1==1 && c2==1 )
7786  ||
7787  ( c1==0 && c2==2 )
7788  ||
7789  ( c2==0 && c1==2 )
7790  );
7791  if( c1==1 || c2==1 ) // only 1 point inside => the rectangle is defined by the intersection points
7792  return detail::RectArea<FPT>( FRect_<FPT>( inter.get().at(0), inter.get().at(1) ) );
7793 
7794 // here: 2 points inside, then build rectangle using the 4 points:
7795 // the 2 inside, and the two intersection points
7796  assert( c1 == 2 || c2 == 2 );
7797 
7798  if( c1 == 2 )
7799  return detail::RectArea<FPT>(
7800  FRect_<FPT>(
7801  inter.get().at(0),
7802  inter.get().at(1),
7803  v1.at(0),
7804  v1.at(1)
7805  )
7806  );
7807  return detail::RectArea<FPT>(
7808  FRect_<FPT>(
7809  inter.get().at(0),
7810  inter.get().at(1),
7811  v2.at(0),
7812  v2.at(1)
7813  )
7814  );
7815 }
FPT getY(const Point2d_< FPT > &pt)
Definition: homog2d.hpp:9823
detail::IntersectM< FPT > intersects(const Line2d_< FPT2 > &line) const
FRect/Line intersection.
Definition: homog2d.hpp:3012
bool isInside(const T &shape) const
Returns true if rectangle is inside shape (Circle_ or FRect_ or base::Polyline)
Definition: homog2d.hpp:2972
#define HOMOG2D_DEBUG_ASSERT(a, b)
Assert debug macro, used internally if HOMOG2D_DEBUGMODE is defined.
Definition: homog2d.hpp:126
static HOMOG2D_INUMTYPE & nullOrthogDistance()
Definition: homog2d.hpp:1200
FPT getX(const Point2d_< FPT > &pt)
Definition: homog2d.hpp:9820
Here is the call graph for this function:
Here is the caller graph for this function:

◆ intersects() [1/5]

template<typename FPT>
template<typename FPT2 >
detail::IntersectM<FPT> h2d::FRect_< FPT >::intersects ( const Line2d_< FPT2 > &  line) const
inline

FRect/Line intersection.

3013  {
3014  return line.intersects( *this );
3015  }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ intersects() [2/5]

template<typename FPT>
template<typename FPT2 >
detail::IntersectM<FPT> h2d::FRect_< FPT >::intersects ( const Segment_< FPT2 > &  seg) const
inline

FRect/Segment intersection.

3020  {
3021  detail::IntersectM<FPT> out;
3022  for( const auto& rseg: getSegs() )
3023  {
3024  auto inters = rseg.intersects( seg ); // call of Segment/Segment
3025  if( inters() )
3026  {
3027  auto pt = inters.get();
3028  bool addPoint = true;
3029  if( out.size() == 1 ) // if we have already one
3030  if( out.get()[0] == pt )
3031  addPoint = false;
3032  if( addPoint )
3033  out.add( pt );
3034  if( out.size() == 2 )
3035  break;
3036  }
3037  }
3038  return out;
3039  }
Segment seg
Definition: homog2d_test.cpp:4033
std::array< Segment_< FPT >, 4 > getSegs() const
Returns the 4 segments of the rectangle, starting with the first vertical one.
Definition: homog2d.hpp:2934
Point2d pt
Definition: homog2d_test.cpp:4034
Here is the call graph for this function:

◆ intersects() [3/5]

template<typename FPT>
template<typename FPT2 >
detail::IntersectM<FPT> h2d::FRect_< FPT >::intersects ( const Circle_< FPT2 > &  circle) const
inline

FRect/Circle intersection.

3044  {
3045 // HOMOG2D_START;
3046  return p_intersects_R_C( circle );
3047  }

◆ intersects() [4/5]

template<typename FPT>
template<typename PLT2 , typename FPT2 >
detail::IntersectM<FPT> h2d::FRect_< FPT >::intersects ( const base::PolylineBase< PLT2, FPT2 > &  pl) const
inline

FRect/Polyline intersection.

3052  {
3053 // HOMOG2D_START;
3054  return pl.intersects( *this );
3055  }
Here is the call graph for this function:

◆ intersects() [5/5]

template<typename FPT>
template<typename FPT2 >
detail::IntersectM<FPT> h2d::FRect_< FPT >::intersects ( const FRect_< FPT2 > &  rect) const
inline

FRect/FRect intersection.

3060  {
3061 // HOMOG2D_START;
3062  if( *this == rect ) // if rectangles are the same,
3063  return detail::IntersectM<FPT>(); // no intersection
3064  return p_intersects_R_C( rect );
3065  }
FRect rect
Definition: homog2d_test.cpp:4038

◆ isInside() [1/3]

template<typename FPT>
template<typename T >
bool h2d::FRect_< FPT >::isInside ( const T &  shape) const
inline

Returns true if rectangle is inside shape (Circle_ or FRect_ or base::Polyline)

Todo:
add some SFINAE to enable only for allowed types?
2973  {
2974  HOMOG2D_START;
2975  for( const auto& pt: get4Pts() )
2976  if( !pt.isInside( shape ) )
2977  return false;
2978  return true;
2979  }
std::array< Point2d_< FPT >, 4 > get4Pts() const
Returns the 4 points of the rectangle, starting from "smallest" one, and in clockwise order...
Definition: homog2d.hpp:2910
#define HOMOG2D_START
Definition: homog2d.hpp:106
Point2d pt
Definition: homog2d_test.cpp:4034
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isInside() [2/3]

template<typename FPT>
template<typename FPT2 >
constexpr bool h2d::FRect_< FPT >::isInside ( const OPolyline_< FPT2 > &  ) const
inline

A FRect is never inside an open polyline.

2984  {
2985  return false;
2986  }

◆ isInside() [3/3]

template<typename FPT>
template<typename FPT2 >
bool h2d::FRect_< FPT >::isInside ( const CPolyline_< FPT2 > &  poly) const
inline

For a rectangle to be inside a closed Polyline, two conditions are necessary:

  • all the points must be inside
  • no intersections
2995  {
2996  for( const auto& seg: getSegs() )
2997  if( seg.intersects(poly)() )
2998  return false;
2999 
3000  for( const auto& pt: get4Pts() )
3001  if( !pt.isInside( poly ) )
3002  return false;
3003  return true;
3004  }
std::array< Point2d_< FPT >, 4 > get4Pts() const
Returns the 4 points of the rectangle, starting from "smallest" one, and in clockwise order...
Definition: homog2d.hpp:2910
Segment seg
Definition: homog2d_test.cpp:4033
std::array< Segment_< FPT >, 4 > getSegs() const
Returns the 4 segments of the rectangle, starting with the first vertical one.
Definition: homog2d.hpp:2934
Point2d pt
Definition: homog2d_test.cpp:4034
Here is the call graph for this function:

◆ length()

template<typename FPT>
HOMOG2D_INUMTYPE h2d::FRect_< FPT >::length ( ) const
inlinevirtual

Implements h2d::rtp::Root.

2748 { return 2.*height() + 2.*width(); }
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2746
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2745
Here is the call graph for this function:
Here is the caller graph for this function:

◆ moveTo() [1/2]

template<typename FPT>
template<typename TX , typename TY >
void h2d::FRect_< FPT >::moveTo ( TX  x,
TY  y 
)
inline

Move FRect to other location.

2828  {
2831 
2832  auto s = size();
2833  _ptR1.set(x,y);
2834  _ptR2.set( _ptR1.getX() + s.first, _ptR1.getY() + s.second );
2835  }
#define HOMOG2D_CHECK_IS_NUMBER(T)
Definition: homog2d.hpp:144
constexpr size_t size() const
Definition: homog2d.hpp:2750
Here is the call graph for this function:

◆ moveTo() [2/2]

template<typename FPT>
template<typename T1 >
void h2d::FRect_< FPT >::moveTo ( const Point2d_< T1 > &  pt)
inline

Move FRect to other location, given by pt.

2840  {
2841  auto s = size();
2842  _ptR1 = pt;
2843  _ptR2.set( _ptR1.getX() + s.first, _ptR1.getY() + s.second );
2844  }
constexpr size_t size() const
Definition: homog2d.hpp:2750
Point2d pt
Definition: homog2d_test.cpp:4034
Here is the call graph for this function:

◆ operator!=()

template<typename FPT>
template<typename FPT2 >
bool h2d::FRect_< FPT >::operator!= ( const FRect_< FPT2 > &  other) const
inline
3081  {
3082  return !( *this == other );
3083  }
Here is the call graph for this function:

◆ operator&()

template<typename FPT>
template<typename FPT2 >
detail::RectArea<FPT> h2d::FRect_< FPT >::operator & ( const FRect_< FPT2 > &  other) const
inline
2891  {
2892  return this->intersectArea( other );
2893  }
detail::RectArea< FPT > intersectArea(const FRect_< FPT2 > &other) const
Returns Rectangle of the intersection area of two rectangles.
Definition: homog2d.hpp:7713
Here is the call graph for this function:

◆ operator==()

template<typename FPT>
template<typename FPT2 >
bool h2d::FRect_< FPT >::operator== ( const FRect_< FPT2 > &  other) const
inline
3072  {
3073  if( _ptR1 != other._ptR1 )
3074  return false;
3075  if( _ptR2 != other._ptR2 )
3076  return false;
3077  return true;
3078  }

◆ operator|()

template<typename FPT>
template<typename FPT2 >
CPolyline_<FPT> h2d::FRect_< FPT >::operator| ( const FRect_< FPT2 > &  other) const
inline
2886  {
2887  return this->unionArea( other );
2888  }
CPolyline_< FPT > unionArea(const FRect_< FPT2 > &other) const
Computes the CPolyline of the union of two rectangles.
Definition: homog2d.hpp:8085
Here is the call graph for this function:

◆ rotate() [1/2]

template<typename FPT >
template<typename FPT2 >
void h2d::FRect_< FPT >::rotate ( Rotate  rot,
const Point2d_< FPT2 > &  refpt 
)

Rotates the rectangle by either 90°, 180°, 270° (-90°) at point refpt.

For an arbitrary angle alpha (rad.), you can write:

auto r2 = Homogr(alpha) * rect;
See also
FRect_<FPT>::rotate( Rotate )
h2d::rotate()
7624 {
7625  translate( -refpt.getX(), -refpt.getY() );
7626  this->rotate( rot );
7627  translate( refpt.getX(), refpt.getY() );
7628 }
void rotate(Rotate, const Point2d_< FPT2 > &)
Rotates the rectangle by either 90°, 180°, 270° (-90°) at point refpt.
Definition: homog2d.hpp:7623
void translate(TX dx, TY dy)
Translate FRect.
Definition: homog2d.hpp:2810
Here is the call graph for this function:

◆ rotate() [2/2]

template<typename FPT >
void h2d::FRect_< FPT >::rotate ( Rotate  rot)

Rotates the rectangle by either 90°, 180°, 270° (-90°) at point (0,0)

For an arbitrary angle alpha (rad.), you can write:

auto r2 = Homogr(alpha) * rect;
See also
FRect_<FPT>::rotate( Rotate, const Point2d_<FPT2>& )
h2d::rotate()
Note
This function converts the FRect to a CPolyline_, rotates it, and builds the output rectangle by getting the bounding box. This is because we cannot directly rotate the points, because the two points of a FRect_ are switched so that the smallest point is always in _ptR1.
7648 {
7649  CPolyline_<HOMOG2D_INUMTYPE> pol( *this );
7650  pol.rotate( rot );
7651  *this = pol.getBB();
7652 }
CPolyline pol(5, 5u)
Here is the call graph for this function:

◆ set() [1/2]

template<typename FPT>
template<typename FPT1 , typename FPT2 >
void h2d::FRect_< FPT >::set ( const Point2d_< FPT1 > &  pa,
const Point2d_< FPT2 > &  pb 
)
inline

Assigns points pa and pb to rectangle.

2731  {
2732  auto ppts = detail::getCorrectPoints( pa, pb );
2733  _ptR1 = ppts.first;
2734  _ptR2 = ppts.second;
2735  }
PointPair_< FPT > getCorrectPoints(const Point2d_< FPT > &p0, const Point2d_< FPT > &p1)
Private free function, get top-left and bottom-right points from two arbitrary points.
Definition: homog2d.hpp:1312
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set() [2/2]

template<typename FPT>
template<typename T >
void h2d::FRect_< FPT >::set ( x1,
y1,
x2,
y2 
)
inline

Assigns points (x1,y1) and (x2,y2) to rectangle.

2739  {
2740  set( Point2d_<T>(x1,y1), Point2d_<T>(x2,y2) );
2741  }

◆ size()

template<typename FPT>
constexpr size_t h2d::FRect_< FPT >::size ( ) const
inlinevirtual

Implements h2d::rtp::Root.

2751  {
2752  return 4;
2753  }
Here is the caller graph for this function:

◆ translate() [1/2]

template<typename FPT>
template<typename TX , typename TY >
void h2d::FRect_< FPT >::translate ( TX  dx,
TY  dy 
)
inline

Translate FRect.

2811  {
2814  _ptR1.set( _ptR1.getX() + dx, _ptR1.getY() + dy );
2815  _ptR2.set( _ptR2.getX() + dx, _ptR2.getY() + dy );
2816  }
#define HOMOG2D_CHECK_IS_NUMBER(T)
Definition: homog2d.hpp:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ translate() [2/2]

template<typename FPT>
template<typename T1 , typename T2 >
void h2d::FRect_< FPT >::translate ( const std::pair< T1, T2 > &  pa)
inline

Translate FRect.

2821  {
2822  this->translate( pa.first, pa.second );
2823  }
void translate(TX dx, TY dy)
Translate FRect.
Definition: homog2d.hpp:2810
Here is the call graph for this function:

◆ type()

template<typename FPT>
Type h2d::FRect_< FPT >::type ( ) const
inlinevirtual

Implements h2d::rtp::Root.

2636  {
2637  return Type::FRect;
2638  }

◆ unionArea()

template<typename FPT >
template<typename FPT2 >
CPolyline_< FPT > h2d::FRect_< FPT >::unionArea ( const FRect_< FPT2 > &  other) const

Computes the CPolyline of the union of two rectangles.

Algorithm:

  1. build vectors of x and y coordinates (4 elements)
  2. build table x-y (4x4), with corners tagged
  3. parse the table by turning right at each corner, and left if position is not one the outside row/col
  4. convert back indexes to real coordinates, to build the final CPolyline object

Two examples:

9      +----+              +-------+
       |    |              |       |
8  +---+----+---+      +---+---+   |
   |   |    |   |      |   |   |   |
7  +---+----+---+      +---+---+   |
       |    |              |       |
6      +----+              +-------+
   1   2    3   4      1   2   3   4

Step 1 will build (for both situations above):

  • vx: 1,2,3,4
  • vy: 6,7,8,9

Step 2 will build a table showing where the corners are:

  | 0 1 2 3             | 0 1 2 3
--|---------|         --|---------|
0 | . F F . |          0| . F . F
1 | F F F F |          1| F F . .
2 | F F F F |          2| F F . .
3 | . F F . |          3| . F . F

Step 3: parse that table and turn on each corner:

  • left if "inner" corner (row and col = 1 or = 2)
  • right if "outer" corner (row and col = 0 or = 3)

Final step: convert indexes to real coordinates

Special note: if the rectangles have an identical coordinate, as in this example:

9      +----+
       |    |
8  +---+----+
   |   |    |
7  +---+----+
       |    |
6      +----+
   1   2    3

Then the vectors are:

  • vx: 1,2,3,3
  • vy: 6,7,8,9

(notice the duped coordinate)

This will produce a Polyline_ with 2 extra points:
(1,7)-(1,8)-(2,8)-(2,9)-(3,9)-(3,8)-(3,7)-(3,6)-(2,6)-(2,7)
instead of:
(1,7)-(1,8)-(2,8)-(2,9)-(3,9)-(3,6)-(2,6)-(2,7)
We solve this by proceeding an extra Polyline minimization, see PolylineBase::minimize()

8086 {
8087  using namespace priv::runion;
8088 
8089  if( *this == other ) // if same rectangles, then
8090  return CPolyline_<FPT>( other ); // returns one of them
8091 
8092  if( !this->intersects(other)() ) // if no intersection,
8093  {
8094  if( this->isInside( other ) )
8095  return CPolyline_<FPT>(other);
8096  if( other.isInside( *this ) )
8097  return CPolyline_<FPT>(*this);
8098  return CPolyline_<FPT>(); // return empty polygon
8099  }
8100 
8101 /* step 0: make sure the rect with highest x is first.
8102  This is needed to avoid this kind of situation in table:
8103 
8104  F F . .
8105  . . F F
8106  . . F F
8107  F F . .
8108 
8109  That would happen if we have an identical x in the two rect
8110  (thus, they DO intersect), because when sorting, the rect with smallest index
8111  (1 or 2) is placed first in the vector of coordinates */
8112  const auto* pr1 = this;
8113  const auto* pr2 = &other;
8114  if( pr1->getPts().first.getX() < pr2->getPts().first.getX() )
8115  std::swap( pr1, pr2 );
8116  const auto& r1 = *pr1;
8117  const auto& r2 = *pr2;
8118 
8119 // step 1: build vectors of coordinates and sort them
8120  std::array<Index<FPT>,4> vx, vy;
8121  int i=0;
8122  vx[i++] = Index<FPT>( r1.getPts().first.getX(), 1 );
8123  vx[i++] = Index<FPT>( r1.getPts().second.getX(), 1 );
8124  vx[i++] = Index<FPT>( r2.getPts().first.getX(), 2 );
8125  vx[i++] = Index<FPT>( r2.getPts().second.getX(), 2 );
8126 
8127  i=0;
8128  vy[i++] = Index<FPT>( r1.getPts().first.getY(), 1 );
8129  vy[i++] = Index<FPT>( r1.getPts().second.getY(), 1 );
8130  vy[i++] = Index<FPT>( r2.getPts().first.getY(), 2 );
8131  vy[i++] = Index<FPT>( r2.getPts().second.getY(), 2 );
8132 
8133  std::sort( vx.begin(), vx.end() );
8134  std::sort( vy.begin(), vy.end() );
8135 // priv::printArray( vx, "vx" ); priv::printArray( vy, "vy");
8136 
8137 // step 2: fill table\n";
8138  Table table;
8139  for( uint8_t r=0;r<4; r++ )
8140  for( uint8_t c=0;c<4; c++ )
8141  table[r][c] = Cell( vx[r], vy[c] );
8142 // printTable( table, "after step 2" );
8143 
8144 // step 3: parse table
8145  auto vpts = parseTable( table );
8146 // priv::printVectorPairs( vpts );
8147 
8148 // step 4: convert back vector of coordinates indexes into vector of coordinates
8149  auto res1 = convertToCoord( vpts, vx, vy );
8150  res1.minimize(); // remove unecessary points
8151  return res1;
8152 }
std::vector< PCoord > parseTable(Table &table)
Helper function for FRect_<FPT>::unionArea()
Definition: homog2d.hpp:7929
std::array< std::array< Cell, 4 >, 4 > Table
Definition: homog2d.hpp:7872
detail::IntersectM< FPT > intersects(const Line2d_< FPT2 > &line) const
FRect/Line intersection.
Definition: homog2d.hpp:3012
bool isInside(const T &shape) const
Returns true if rectangle is inside shape (Circle_ or FRect_ or base::Polyline)
Definition: homog2d.hpp:2972
CPolyline_< FPT > convertToCoord(const std::vector< PCoord > &v_coord, const std::array< Index< FPT >, 4 > &v_x, const std::array< Index< FPT >, 4 > &v_y)
Helper function, used in FRect_<FPT>::unionArea()
Definition: homog2d.hpp:7990
Here is the call graph for this function:
Here is the caller graph for this function:

◆ width()

template<typename FPT>
HOMOG2D_INUMTYPE h2d::FRect_< FPT >::width ( ) const
inline
2746 { return _ptR2.getX() - _ptR1.getX(); }
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ FRect_

template<typename FPT>
template<typename T >
friend class FRect_
friend

◆ operator<<

template<typename FPT>
template<typename T >
std::ostream& operator<< ( std::ostream &  f,
const FRect_< T > &  r 
)
friend
8412 {
8413  f << "pt1: " << r._ptR1 << " pt2: " << r._ptR2;
8414  return f;
8415 }

The documentation for this class was generated from the following file: