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)

2685  {
2686  _ptR2.set( 1., 1. );
2687  }

◆ 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.

2691  {
2692  set( pa, pb );
2693  }

◆ FRect_() [3/6]

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

Constructor from pair of points.

2698  {
2699  set( ppts.first, ppts.second );
2700  }

◆ 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.

2705  {
2708  set(
2709  Point2d_<FPT>( p0.getX()-0.5L*w, p0.getY()-0.5L*h ),
2710  Point2d_<FPT>( p0.getX()+0.5L*w, p0.getY()+0.5L*h )
2711  );
2712  }
#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.

2717  {
2722  set( Point2d_<FPT>(x1,y1), Point2d_<FPT>(x2,y2) );
2723  }
#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.

2728  : _ptR1(other._ptR1),_ptR2(other._ptR2)
2729  {}
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.

2782 { return height() * width(); }
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2781
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2780
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.

11847 {
11848  cv::rectangle(
11849  im.getReal(),
11850  _ptR1.getCvPti(),
11851  _ptR2.getCvPti(),
11852  dp.cvColor(),
11853  dp._dpValues._lineThickness,
11854  dp._dpValues._lineType==1?cv::LINE_AA:cv::LINE_8
11855  );
11856  if( dp._dpValues._showPoints )
11857  {
11858  _ptR1.draw( im, dp );
11859  _ptR2.draw( im, dp );
11860  }
11861 }
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.

12116 {
12117  im.getReal()._svgString << "<rect x=\""
12118  << getPts().first.getX()
12119  << "\" y=\""
12120  << getPts().first.getY()
12121  << "\" width=\""
12122  << width()
12123  << "\" height=\""
12124  << height()
12125  << "\" stroke=\""
12126  << dp.getSvgRgbColor()
12127  << "\" stroke-width=\"" << dp._dpValues._lineThickness << "\" ";
12128  if( !dp.holdsFill() )
12129  im.getReal()._svgString << "fill=\"none\" ";
12130  im.getReal()._svgString << dp.getAttrString() << "/>\n";
12131 }
img::Image< img::SvgImage > im(300, 400)
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2781
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2780
PointPair_< FPT > getPts() const
Returns the 2 major points of the rectangle.
Definition: homog2d.hpp:2802
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>& )
2946  {
2947  std::array<Point2d_<FPT>,4> arr;
2948  arr[0] = _ptR1;
2949  arr[1] = Point2d_<FPT>( _ptR1.getX(), _ptR2.getY() );
2950  arr[2] = _ptR2;
2951  arr[3] = Point2d_<FPT>( _ptR2.getX(), _ptR1.getY() );
2952  return arr;
2953  }
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
2793  { //FRect_<HOMOG2D_INUMTYPE> out;
2794 // out = *this;
2795  return *this;
2796 // return out;
2797  }
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()
2819  {
2820  auto middle_pt = getCenter();
2821  return Circle_<FPT>( middle_pt, middle_pt.distTo( _ptR1 ) );
2822  }
Point2d_< FPT > getCenter() const
Returns center of rectangle.
Definition: homog2d.hpp:2808
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.

2809  {
2810  return Point2d_<FPT>(
2811  (static_cast<HOMOG2D_INUMTYPE>(_ptR1.getX() ) + _ptR2.getX() ) * 0.5,
2812  (static_cast<HOMOG2D_INUMTYPE>(_ptR1.getY() ) + _ptR2.getY() ) * 0.5
2813  );
2814  }
#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
2900  {
2901  auto x1 = _ptR1.getX();
2902  auto y1 = _ptR1.getY();
2903  auto x2 = _ptR2.getX();
2904  auto y2 = _ptR2.getY();
2905 
2906  return std::make_pair(
2907  Segment_<FPT>(x1,y1,x2,y2),
2908  Segment_<FPT>(x1,y2,x2,y1)
2909  );
2910  }
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
2888  {
2889  auto x1 = _ptR1.getX() * 2. - _ptR2.getX();
2890  auto y1 = _ptR1.getY() * 2. - _ptR2.getY();
2891 
2892  auto x2 = _ptR2.getX() * 2. - _ptR1.getX();
2893  auto y2 = _ptR2.getY() * 2. - _ptR1.getY();
2894 
2895  return FRect_<FPT>( x1, y1, x2, y2 );
2896  }
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()
2827  {
2828  auto segs = getSegs();
2829  auto center = getCenter();
2830  Circle_<FPT> cir( center );
2831  cir.radius() = std::min(
2832  center.distTo( segs[0] ),
2833  center.distTo( segs[1] )
2834  );
2835  return cir;
2836  }
Point2d_< FPT > getCenter() const
Returns center of rectangle.
Definition: homog2d.hpp:2808
Point2d_< FPT > & center(Circle_< FPT > &cir)
Returns reference on center of circle (free function), non-const version.
Definition: homog2d.hpp:10935
std::array< Segment_< FPT >, 4 > getSegs() const
Returns the 4 segments of the rectangle, starting with the first vertical one.
Definition: homog2d.hpp:2969
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>& )
2803  {
2804  return std::make_pair( _ptR1, _ptR2 );
2805  }
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_& )
2970  {
2971  auto pts = get4Pts();
2972  std::array<Segment_<FPT>,4> out;
2973  out[0] = Segment_<FPT>( pts[0], pts[1] );
2974  out[1] = Segment_<FPT>( pts[1], pts[2] );
2975  out[2] = Segment_<FPT>( pts[2], pts[3] );
2976  out[3] = Segment_<FPT>( pts[3], pts[0] );
2977  return out;
2978  }
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:2945
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
2780 { 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
         +------+
         |      |
      +--+------+-----+
      |  |      |     |
      |  |      |     |
      +--+------+-----+
         |      |
         +------+
    
7715 {
7716  if( *this == other ) // if same rectangles, then
7717  return detail::RectArea<FPT>(other); // the intersection area is the rectangle
7718 
7719  auto inter = this->intersects( other );
7720 
7721  if( !inter() ) // rectangles do not intersect
7722  {
7723  if( this->isInside( other ) )
7724  return detail::RectArea<FPT>(*this);
7725  if( other.isInside( *this ) )
7726  return detail::RectArea<FPT>(other);
7727  return detail::RectArea<FPT>();
7728  }
7729 
7730  if( inter.size() < 2 ) // only one intersection point
7731  return detail::RectArea<FPT>(); // => no intersection area!
7732 
7733  if( inter.size() == 4 ) // 4 intersection points => case "D"
7734  return detail::RectArea<FPT>(FRect_<FPT>( inter.get().at(0), inter.get().at(3) ) );
7735 
7736  if( inter.size() == 3 ) // 3 intersection points => case "C"
7737  {
7738  auto v = inter.get();
7739 
7740  auto xmin = std::min( v[0].getX(), std::min( v[1].getX(),v[2].getX() ) );
7741  auto ymin = std::min( v[0].getY(), std::min( v[1].getY(),v[2].getY() ) );
7742  auto xmax = std::max( v[0].getX(), std::max( v[1].getX(),v[2].getX() ) );
7743  auto ymax = std::max( v[0].getY(), std::max( v[1].getY(),v[2].getY() ) );
7744 #ifndef HOMOG2D_DEBUGMODE
7745  assert( xmax-xmin > thr::nullOrthogDistance() );
7746  assert( ymax-ymin > thr::nullOrthogDistance() );
7747 #else
7749  ( ( xmax-xmin > thr::nullOrthogDistance() )
7750  &&
7751  ( ymax-ymin > thr::nullOrthogDistance() )) ,
7752  std::scientific
7753  << "this=" << *this << " other=" << other
7754  << "\nxmax=" << xmax << " xmin=" << xmin
7755  << "\nymax=" << ymax << " ymin=" << ymin
7756  << "\nnod=" << thr::nullOrthogDistance()
7757  );
7758 #endif
7759  return detail::RectArea<FPT>( FRect_<FPT>( xmin, ymin, xmax,ymax ) );
7760  }
7761 /*------------------------
7762  If we get to this point, we have 2 intersection points
7763 ------------------------*/
7764 #ifndef HOMOG2D_DEBUGMODE
7765  assert( inter.size() == 2 );
7766 #else
7767 /* HOMOG2D_DEBUG_ASSERT(
7768  inter.size() == 2,
7769  "inter.size()=" << inter.size() << "\n-this=" << *this << "\n-other=" << other
7770  <<"\n-inter:" << inter
7771  );*/
7772 #endif
7773  const auto& r1 = *this;
7774  const auto& r2 = other;
7775 // HOMOG2D_LOG( "r1="<<r1 << " r2="<<r2 );
7776  auto v1 = r1.p_pointsInside( r2 );
7777  auto v2 = r2.p_pointsInside( r1 );
7778  auto c1 = v1.size();
7779  auto c2 = v2.size();
7780 // HOMOG2D_LOG( "c1=" << c1 << " c2=" << c2 );
7781 
7782  if( c1==0 && c2==0 ) // unable, rectangles share a segment
7783  return detail::RectArea<FPT>();
7784 
7785  assert(
7786  ( c1==1 && c2==1 )
7787  ||
7788  ( c1==0 && c2==2 )
7789  ||
7790  ( c2==0 && c1==2 )
7791  );
7792  if( c1==1 || c2==1 ) // only 1 point inside => the rectangle is defined by the intersection points
7793  return detail::RectArea<FPT>( FRect_<FPT>( inter.get().at(0), inter.get().at(1) ) );
7794 
7795 // here: 2 points inside, then build rectangle using the 4 points:
7796 // the 2 inside, and the two intersection points
7797  assert( c1 == 2 || c2 == 2 );
7798 
7799  if( c1 == 2 )
7800  return detail::RectArea<FPT>(
7801  FRect_<FPT>(
7802  inter.get().at(0),
7803  inter.get().at(1),
7804  v1.at(0),
7805  v1.at(1)
7806  )
7807  );
7808  return detail::RectArea<FPT>(
7809  FRect_<FPT>(
7810  inter.get().at(0),
7811  inter.get().at(1),
7812  v2.at(0),
7813  v2.at(1)
7814  )
7815  );
7816 }
FPT getY(const Point2d_< FPT > &pt)
Definition: homog2d.hpp:9824
detail::IntersectM< FPT > intersects(const Line2d_< FPT2 > &line) const
FRect/Line intersection.
Definition: homog2d.hpp:3047
bool isInside(const T &shape) const
Returns true if rectangle is inside shape (Circle_ or FRect_ or base::Polyline)
Definition: homog2d.hpp:3007
#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:1234
FPT getX(const Point2d_< FPT > &pt)
Definition: homog2d.hpp:9821
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.

3048  {
3049  return line.intersects( *this );
3050  }
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.

3055  {
3056  detail::IntersectM<FPT> out;
3057  for( const auto& rseg: getSegs() )
3058  {
3059  auto inters = rseg.intersects( seg ); // call of Segment/Segment
3060  if( inters() )
3061  {
3062  auto pt = inters.get();
3063  bool addPoint = true;
3064  if( out.size() == 1 ) // if we have already one
3065  if( out.get()[0] == pt )
3066  addPoint = false;
3067  if( addPoint )
3068  out.add( pt );
3069  if( out.size() == 2 )
3070  break;
3071  }
3072  }
3073  return out;
3074  }
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:2969
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.

3079  {
3080 // HOMOG2D_START;
3081  return p_intersects_R_C( circle );
3082  }

◆ 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.

3087  {
3088 // HOMOG2D_START;
3089  return pl.intersects( *this );
3090  }
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.

3095  {
3096 // HOMOG2D_START;
3097  if( *this == rect ) // if rectangles are the same,
3098  return detail::IntersectM<FPT>(); // no intersection
3099  return p_intersects_R_C( rect );
3100  }
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?
3008  {
3009  HOMOG2D_START;
3010  for( const auto& pt: get4Pts() )
3011  if( !pt.isInside( shape ) )
3012  return false;
3013  return true;
3014  }
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:2945
#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.

3019  {
3020  return false;
3021  }

◆ 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
3030  {
3031  for( const auto& seg: getSegs() )
3032  if( seg.intersects(poly)() )
3033  return false;
3034 
3035  for( const auto& pt: get4Pts() )
3036  if( !pt.isInside( poly ) )
3037  return false;
3038  return true;
3039  }
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:2945
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:2969
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.

2783 { return 2.*height() + 2.*width(); }
HOMOG2D_INUMTYPE width() const
Definition: homog2d.hpp:2781
HOMOG2D_INUMTYPE height() const
Definition: homog2d.hpp:2780
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.

2863  {
2866 
2867  auto s = size();
2868  _ptR1.set(x,y);
2869  _ptR2.set( _ptR1.getX() + s.first, _ptR1.getY() + s.second );
2870  }
#define HOMOG2D_CHECK_IS_NUMBER(T)
Definition: homog2d.hpp:144
constexpr size_t size() const
Definition: homog2d.hpp:2785
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.

2875  {
2876  auto s = size();
2877  _ptR1 = pt;
2878  _ptR2.set( _ptR1.getX() + s.first, _ptR1.getY() + s.second );
2879  }
constexpr size_t size() const
Definition: homog2d.hpp:2785
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
3116  {
3117  return !( *this == other );
3118  }
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
2926  {
2927  return this->intersectArea( other );
2928  }
detail::RectArea< FPT > intersectArea(const FRect_< FPT2 > &other) const
Returns Rectangle of the intersection area of two rectangles.
Definition: homog2d.hpp:7714
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
3107  {
3108  if( _ptR1 != other._ptR1 )
3109  return false;
3110  if( _ptR2 != other._ptR2 )
3111  return false;
3112  return true;
3113  }

◆ operator|()

template<typename FPT>
template<typename FPT2 >
CPolyline_<FPT> h2d::FRect_< FPT >::operator| ( const FRect_< FPT2 > &  other) const
inline
2921  {
2922  return this->unionArea( other );
2923  }
CPolyline_< FPT > unionArea(const FRect_< FPT2 > &other) const
Computes the CPolyline of the union of two rectangles.
Definition: homog2d.hpp:8086
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()
7625 {
7626  translate( -refpt.getX(), -refpt.getY() );
7627  this->rotate( rot );
7628  translate( refpt.getX(), refpt.getY() );
7629 }
void rotate(Rotate, const Point2d_< FPT2 > &)
Rotates the rectangle by either 90°, 180°, 270° (-90°) at point refpt.
Definition: homog2d.hpp:7624
void translate(TX dx, TY dy)
Translate FRect.
Definition: homog2d.hpp:2845
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.
7649 {
7650  CPolyline_<HOMOG2D_INUMTYPE> pol( *this );
7651  pol.rotate( rot );
7652  *this = pol.getBB();
7653 }
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.

2766  {
2767  auto ppts = detail::getCorrectPoints( pa, pb );
2768  _ptR1 = ppts.first;
2769  _ptR2 = ppts.second;
2770  }
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:1346
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.

2774  {
2775  set( Point2d_<T>(x1,y1), Point2d_<T>(x2,y2) );
2776  }

◆ size()

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

Implements h2d::rtp::Root.

2786  {
2787  return 4;
2788  }
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.

2846  {
2849  _ptR1.set( _ptR1.getX() + dx, _ptR1.getY() + dy );
2850  _ptR2.set( _ptR2.getX() + dx, _ptR2.getY() + dy );
2851  }
#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.

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

◆ type()

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

Implements h2d::rtp::Root.

2671  {
2672  return Type::FRect;
2673  }

◆ 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()

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

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