Loading [MathJax]/extensions/tex2jax.js
homog2d library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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.

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

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.

12905  {
12906  return _vecVar;
12907  }
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.

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

◆ 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

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

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