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.

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

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.

12909  {
12910  return _vecVar;
12911  }
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.

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

◆ 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

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

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