homog2d library
Macros | Functions
demo_svg_import.cpp File Reference

Demo of reading svg files. Build with $ make demo-import More...

#include "../../homog2d.hpp"
Include dependency graph for demo_svg_import.cpp:

Macros

#define HOMOG2D_USE_SVG_IMPORT
 

Functions

int main (int argc, const char **argv)
 

Detailed Description

Demo of reading svg files. Build with $ make demo-import

Then run with $ BUILD/demo_svg_import misc/test_files/France.svg or $ BUILD/demo_svg_import misc/test_files/France_Normandie.svg

This will generate a svg file in current folder that is a copy of what was read in the file.

Macro Definition Documentation

◆ HOMOG2D_USE_SVG_IMPORT

#define HOMOG2D_USE_SVG_IMPORT

Function Documentation

◆ main()

int main ( int  argc,
const char **  argv 
)
37 {
38  tinyxml2::XMLDocument doc;
39  if( argc < 2 )
40  {
41  std::cout << "arg missing!\n";
42  return 1;
43  }
44  auto err = doc.LoadFile( argv[1] );
45  if( err != tinyxml2::XML_SUCCESS )
46  {
47  std::cout << "...exiting, failed to read file '" << argv[1] << "'\n";
48  return 1;
49  }
50 
51  svg::printFileAttrib( doc );
52 
53  std::pair<double,double> imSize(500.,500.);
54  try
55  {
56  imSize = svg::getImgSize( doc );
57  std::cout << "Input image size: " << imSize.first << " x " << imSize.second << '\n';
58  }
59  catch( const std::exception& error )
60  {
61  std::cout << "input file has no size, size set to 500x500\n -msg=" << error.what() << '\n';
62  }
63 
64  h2d::svg::Visitor visitor;
65  doc.Accept( &visitor );
66  const auto& data = visitor.get();
67  std::cout << argv[0] << ": Read " << data.size() << " shapes in file '" << argv[1] << "'\n";
68  if( !data.size() )
69  {
70  std::cout << "...exiting, no data!\n";
71  return 1;
72  }
73 
74  img::Image<img::SvgImage> out( imSize.first, imSize.second );
75  fct::DrawFunct<img::SvgImage> dfunc( out );
76 
77  auto vcolors = img::genRandomColors( data.size() );
78  PointPair pp_all;
79  size_t c = 0;
80  for( const auto& e: data )
81  {
82  std::visit( dfunc, e ); // draw element
83 
84  auto ptpair = std::visit( fct::PtPairFunct{}, e );
85  pp_all = getMinMax( pp_all, ptpair );
86 
87  std::cout << "Shape " << c << ": " << getString( type(e) )
88  << ", size " << size(e) << "\n";
89 
90  if( type(e) != Type::Segment ) // because no BB is defined
91  {
92  auto bb = std::visit( fct::BBFunct{}, e );
93  bb.draw( out, img::DrawParams().setColor(vcolors[c]) );
94  }
95  c++;
96  }
97  auto s = FRect(pp_all);
98  out.setSize( s.width(), s.height() );
99  std::cout << "min/max=" << s << '\n';
100 
101  out.write( "demo_import.svg" );
102 }
void printFileAttrib(const tinyxml2::XMLDocument &doc)
Definition: homog2d.hpp:13077
FRect_< HOMOG2D_INUMTYPE > FRect
Default rectangle type.
Definition: homog2d.hpp:12395
Segment_< HOMOG2D_INUMTYPE > Segment
Default segment type.
Definition: homog2d.hpp:12388
PointPair_< double > PointPair
Definition: homog2d.hpp:12445
auto getImgSize(const tinyxml2::XMLDocument &doc)
Fetch size of image in SVG file.
Definition: homog2d.hpp:13092
A functor used to draw objects. To use with std::variant and std::visit()
Definition: homog2d.hpp:1068
Type type(const T &elem)
Free function. Returns the type of object or variant.
Definition: homog2d.hpp:10190
Draw parameters, independent of back-end library.
Definition: homog2d.hpp:514
Visitor class, derived from the tinyxml2 visitor class. Used to import SVG data.
Definition: homog2d.hpp:12864
A functor to get the Bounding Box.
Definition: homog2d.hpp:10151
std::vector< img::Color > genRandomColors(size_t nb, int minval=20, int maxval=250)
Helper function, will generate a vector of nb random RGB colors.
Definition: homog2d.hpp:453
auto getMinMax(const PointPair2_< T1, T2 > &pp1, const PointPair2_< T3, T4 > &pp2)
Returns a pair of points holding min and max coordinates of the two input pair of points...
Definition: homog2d.hpp:10398
HOMOG2D_INUMTYPE size(const T &elem)
Returns size of element or variant (free function)
Definition: homog2d.hpp:10253
const std::vector< CommonType_< double > > & get() const
Used to access the data once the file has been read.
Definition: homog2d.hpp:12904
A functor to get pair of points englobing the element.
Definition: homog2d.hpp:10138
Opaque data structure, will hold the image type, depending on back-end library. This type is the one ...
Definition: homog2d.hpp:712
const char * getString(PtStyle t)
Definition: homog2d.hpp:496
Here is the call graph for this function: