homog2d library
Functions
rtp_p.hpp File Reference
This graph shows which files directly or indirectly include this file:

Functions

template<typename IM >
void do_prtp (const std::vector< h2d::Point2d > &vecpts, IM &im)
 Pointer-based runtime polymorphism. More...
 

Function Documentation

◆ do_prtp()

template<typename IM >
void do_prtp ( const std::vector< h2d::Point2d > &  vecpts,
IM &  im 
)

Pointer-based runtime polymorphism.

Pointer-based polymorphism

ONLY TO BE USED WITH THE ASSOCIATED PROGRAMS homog2d_test_rtp_*.cpp

12 {
13  CPolyline cpol( vecpts );
14  OPolyline opol( vecpts );
15 
16  std::vector<std::shared_ptr<rtp::Root>> vec;
17 
18  vec.push_back( std::make_shared<Circle>( Circle() ) );
19  vec.push_back( std::make_shared<Segment>( Segment() ) );
20  vec.push_back( std::make_shared<FRect>( FRect() ) );
21  vec.push_back( std::make_shared<Line2d>( Line2d() ) );
22  vec.push_back( std::make_shared<Point2d>( Point2d() ) );
23  vec.push_back( std::make_shared<Ellipse>( Ellipse() ) );
24  vec.push_back( std::make_shared<CPolyline>( cpol ) );
25  vec.push_back( std::make_shared<OPolyline>( opol ) );
26 
27  for( auto& e: vec ) // demonstration of polymorphic member functions
28  {
29  std::cout << getString(e->type()) << ": " << *e
30  << "\n -area = " << e->area()
31  << "\n -size = " << e->size();
32  if( e->type() != Type::Line2d )
33  std::cout << "\n -length = " << e->length();
34  else
35  std::cout << "\n -length = infinite";
36  std::cout << '\n';
37  e->draw( im );
38 
39  if( e->type() == Type::CPolyline )
40  {
41  auto pl1 = std::dynamic_pointer_cast<CPolyline>( e );
42  std::cout << "pl1 is closed=" << pl1->isClosed() << '\n';
43 
44 // demonstration of bad casting, this will segfault
45 /*
46  auto pl2 = std::dynamic_pointer_cast<OPolyline>( e );
47  std::cout << "pl2 is closed=" << pl2->isClosed() << '\n';
48  std::cout << "pl2 area=" << pl2->area() << '\n';
49 */
50  }
51  }
52 }
OPolyline_< HOMOG2D_INUMTYPE > OPolyline
Definition: homog2d.hpp:12399
FRect_< HOMOG2D_INUMTYPE > FRect
Default rectangle type.
Definition: homog2d.hpp:12395
Segment_< HOMOG2D_INUMTYPE > Segment
Default segment type.
Definition: homog2d.hpp:12388
Circle_< HOMOG2D_INUMTYPE > Circle
Default circle type.
Definition: homog2d.hpp:12392
Line2d_< HOMOG2D_INUMTYPE > Line2d
Default line type, uses double as numerical type.
Definition: homog2d.hpp:12376
Ellipse_< HOMOG2D_INUMTYPE > Ellipse
Default ellipse type.
Definition: homog2d.hpp:12402
CPolyline_< HOMOG2D_INUMTYPE > CPolyline
Default polyline type.
Definition: homog2d.hpp:12398
Point2d_< HOMOG2D_INUMTYPE > Point2d
Default point type, uses double as numerical type.
Definition: homog2d.hpp:12379
img::Image< img::SvgImage > im(300, 400)
CPolyline cpol
Definition: homog2d_test.cpp:4030
const char * getString(PtStyle t)
Definition: homog2d.hpp:496
Here is the call graph for this function:
Here is the caller graph for this function: