homog2d library
Public Member Functions | List of all members
h2d::svg::Visitor Class Reference

Visitor class, derived from the tinyxml2 visitor class. Used to import SVG data. More...

#include <homog2d.hpp>

Inheritance diagram for h2d::svg::Visitor:
Inheritance graph
[legend]
Collaboration diagram for h2d::svg::Visitor:
Collaboration graph
[legend]

Public Member Functions

const std::vector< CommonType_< double > > & get () const
 Used to access the data once the file has been read. More...
 
SvgType getSvgType (std::string s) const
 Returns the type as a member of enum SvgType, so the type can be used in a switch. More...
 
bool VisitExit (const tinyxml2::XMLElement &) override
 This is the place where actual SVG data is converted and stored into vector. More...
 
 Visitor ()
 Constructor, populates the table giving type from svg string. More...
 

Detailed Description

Visitor class, derived from the tinyxml2 visitor class. Used to import SVG data.

Holds the imported data through std::variant

Constructor & Destructor Documentation

◆ Visitor()

h2d::svg::Visitor::Visitor ( )
inline

Constructor, populates the table giving type from svg string.

12883  { // svg name local type id
12884  _svgTypesTable["circle"] = T_circle;
12885  _svgTypesTable["rect"] = T_rect;
12886  _svgTypesTable["line"] = T_line;
12887  _svgTypesTable["polyline"] = T_polyline;
12888  _svgTypesTable["polygon"] = T_polygon;
12889  _svgTypesTable["ellipse"] = T_ellipse;
12890  _svgTypesTable["path"] = T_path;
12891  }

Member Function Documentation

◆ get()

const std::vector<CommonType_<double> >& h2d::svg::Visitor::get ( ) const
inline

Used to access the data once the file has been read.

12906  {
12907  return _vecVar;
12908  }
Here is the caller graph for this function:

◆ getSvgType()

SvgType h2d::svg::Visitor::getSvgType ( std::string  s) const
inline

Returns the type as a member of enum SvgType, so the type can be used in a switch.

12896  {
12897  auto it = _svgTypesTable.find( s );
12898  if( it == std::cend(_svgTypesTable) )
12899  return T_other;
12900  return it->second;
12901  }

◆ VisitExit()

bool h2d::svg::Visitor::VisitExit ( const tinyxml2::XMLElement &  e)
inlineoverride

This is the place where actual SVG data is converted and stored into vector.

(see manual, section "SVG import")

Overload of the root class VisitExit() member function

12968 {
12969  std::string n = e.Name();
12970 // std::cout << "element name:" << n << '\n';
12971  try
12972  {
12973  switch( getSvgType( n ) )
12974  {
12975  case T_circle:
12976  _vecVar.emplace_back(
12977  CircleD(
12978  svgp::getAttribValue( e, "cx", n ),
12979  svgp::getAttribValue( e, "cy", n ),
12980  svgp::getAttribValue( e, "r", n )
12981  )
12982  );
12983  break;
12984 
12985  case T_rect:
12986  {
12987  auto x1 = svgp::getAttribValue( e, "x", n );
12988  auto y1 = svgp::getAttribValue( e, "y", n );
12989  auto w = svgp::getAttribValue( e, "width", n );
12990  auto h = svgp::getAttribValue( e, "height", n );
12991  _vecVar.emplace_back( FRectD( x1, y1, x1+w, y1+h ) );
12992  }
12993  break;
12994 
12995  case T_line:
12996  _vecVar.emplace_back(
12997  SegmentD(
12998  svgp::getAttribValue( e, "x1", n ),
12999  svgp::getAttribValue( e, "y1", n ),
13000  svgp::getAttribValue( e, "x2", n ),
13001  svgp::getAttribValue( e, "y2", n )
13002  )
13003  );
13004  break;
13005 
13006  case T_polygon:
13007  {
13008  auto vpts = svgp::importSvgPoints( e );
13009  _vecVar.emplace_back( CPolylineD(vpts) );
13010  }
13011  break;
13012 
13013  case T_polyline:
13014  {
13015  auto vpts = svgp::importSvgPoints( e );
13016  _vecVar.emplace_back( OPolylineD(vpts) );
13017  }
13018  break;
13019 
13020  case T_path: // a path can hold multiple polygons (because of the 'L' command)
13021  {
13022  auto pts_str = svgp::getAttribString( "d", e );
13023  try
13024  {
13025  auto parse_res = svgp::parsePath( pts_str );
13026  const auto& vec_vec_pts = parse_res.first; //
13027  for( auto vec_pts: vec_vec_pts ) // we need a copy so we may remove last point if equal to first
13028 // for( const auto& vec_pts: vec_vec_pts ) // we need a copy so we may remove last point if equal to first
13029  {
13030  if( vec_pts.front() == vec_pts.back() ) // if first point equal to last
13031  vec_pts.pop_back(); // point, remove last point
13032 
13033  if( parse_res.second == true )
13034  _vecVar.emplace_back( CPolylineD(vec_pts) );
13035  else
13036  _vecVar.emplace_back( OPolylineD(vec_pts) );
13037  }
13038  }
13039  catch( std::exception& err ) // an unhandled path command will just get the whole path command ignored
13040  {
13041  HOMOG2D_LOG_WARNING( "Unable to import SVG path command\n -msg="
13042  << err.what() << "\n -input string=" << pts_str
13043  );
13044  }
13045  }
13046  break;
13047 
13048  case T_ellipse:
13049  {
13050  auto x = svgp::getAttribValue( e, "cx", n );
13051  auto y = svgp::getAttribValue( e, "cy", n );
13052  auto rx = svgp::getAttribValue( e, "rx", n );
13053  auto ry = svgp::getAttribValue( e, "ry", n );
13054  auto rot = svgp::getEllipseRotateAttr( svgp::getAttribString( "transform", e ) );
13055  auto ell = EllipseD( x, y, rx, ry );
13056 
13057  auto H = Homogr().addTranslation(-x,-y).addRotation(rot.second).addTranslation(x,y);
13058  _vecVar.push_back( H * ell );
13059  }
13060  break;
13061 
13062  default: // for T_other elements
13063  if( n != "svg" ) // because that one will be always there, so no need to show a warning
13064  HOMOG2D_LOG_WARNING( "found SVG element '" << n << "' in SVG file, left unprocessed" );
13065  break;
13066  }
13067  }
13068  catch( std::string& msg )
13069  {
13070  HOMOG2D_THROW_ERROR_1( "h2d: Tinyxml read error: " << msg );
13071  return false; // to avoid a compile warning
13072  }
13073  return true;
13074 }
Homogr_< HOMOG2D_INUMTYPE > Homogr
Default homography (3x3 matrix) type, uses double as numerical type.
Definition: homog2d.hpp:12383
Segment_< double > SegmentD
Definition: homog2d.hpp:12419
double getAttribValue(const tinyxml2::XMLElement &e, const char *str, std::string e_name)
Fetch attribute from XML element. Tag e_name is there just in case of trouble.
Definition: homog2d.hpp:12918
SvgType getSvgType(std::string s) const
Returns the type as a member of enum SvgType, so the type can be used in a switch.
Definition: homog2d.hpp:12895
auto parsePath(const char *s)
Parse a SVG "path" string and convert it to a vector holding a set (vector) of points.
Definition: homog2d.hpp:12797
const char * getAttribString(const char *attribName, const tinyxml2::XMLElement &e)
Helper function for SVG import.
Definition: homog2d.hpp:12933
Circle_< double > CircleD
Definition: homog2d.hpp:12421
#define HOMOG2D_LOG_WARNING(a)
Definition: homog2d.hpp:151
std::pair< Point2d_< HOMOG2D_INUMTYPE >, HOMOG2D_INUMTYPE > getEllipseRotateAttr(const char *rot_str)
Importing rotated ellipse from SVG data.
Definition: homog2d.hpp:12486
CPolyline_< double > CPolylineD
Definition: homog2d.hpp:12436
Ellipse ell
Definition: homog2d_test.cpp:4037
FRect_< double > FRectD
Definition: homog2d.hpp:12422
#define HOMOG2D_THROW_ERROR_1(msg)
Error throw wrapper macro.
Definition: homog2d.hpp:181
Ellipse_< double > EllipseD
Definition: homog2d.hpp:12423
std::vector< Point2d > importSvgPoints(const tinyxml2::XMLElement &e)
Helper function called by Visitor::VisitExit() to process Polyline/Polygons.
Definition: homog2d.hpp:12944
OPolyline_< double > OPolylineD
Definition: homog2d.hpp:12440
Here is the call graph for this function:

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