SVGLoader.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef _GAZEBO_SVGLOADER_HH_
19 #define _GAZEBO_SVGLOADER_HH_
20 
21 #include <stdexcept>
22 #include <string>
23 #include <vector>
24 
25 #include <ignition/math/Vector2.hh>
26 #include <ignition/math/Matrix3.hh>
27 
28 #include "gazebo/common/Console.hh"
29 
30 class TiXmlElement;
31 class TiXmlNode;
32 
33 namespace gazebo
34 {
35  namespace common
36  {
37  class SVGLoaderPrivate;
38 
40  class GZ_COMMON_VISIBLE SvgError: public std::runtime_error
41  {
44  public: explicit SvgError(const std::string &_what);
45  };
46 
48  class GZ_COMMON_VISIBLE SVGCommand
49  {
51  public: SVGCommand() : cmd(' ') {}
52 
54  public: virtual ~SVGCommand() = default;
55 
57  public: char cmd;
58 
60  public: std::vector<double> numbers;
61  };
62 
64  struct GZ_COMMON_VISIBLE SVGPath
65  {
67  std::string id;
68 
70  std::string style;
71 
73  ignition::math::Matrix3d transform;
74 
76  std::vector< std::vector<SVGCommand> > subpaths;
77 
79  std::vector< std::vector<ignition::math::Vector2d> > polylines;
80  };
81 
83  class GZ_COMMON_VISIBLE SVGLoader
84  {
87  public: explicit SVGLoader(unsigned int _samples);
88 
90  public: ~SVGLoader();
91 
96  public: bool Parse(const std::string &_filename,
97  std::vector<SVGPath> &_paths);
98 
104  public: static void PathsToClosedPolylines(
105  const std::vector<common::SVGPath> &_paths,
106  double _tol,
107  std::vector< std::vector<ignition::math::Vector2d> > &_closedPolys,
108  std::vector< std::vector<ignition::math::Vector2d> > &_openPolys);
109 
113  public: void DumpPaths(const std::vector<SVGPath> &_paths,
114  std::ostream &_out) const;
115 
119  private: void GetPathCommands(const std::vector<std::string> &_tokens,
120  SVGPath &_path);
121 
125  private: void GetPathAttribs(TiXmlElement *_pElement, SVGPath &_path);
126 
130  private: void GetSvgPaths(TiXmlNode *_pParent,
131  std::vector<SVGPath> &_paths);
132 
136  private: void ExpandCommands(
137  const std::vector< std::vector<SVGCommand> > &_subpaths,
138  SVGPath& _path);
139 
144  private: void SplitSubpaths(const std::vector<SVGCommand> &_cmds,
145  std::vector< std::vector<SVGCommand> > &_subpaths);
146 
151  private: void PathToPoints(const SVGPath &_path,
152  double _resolution,
153  std::vector< std::vector<ignition::math::Vector2d> > &_polys);
154 
160  private: ignition::math::Vector2d SubpathToPolyline(
161  const std::vector<SVGCommand> &_subpath,
162  ignition::math::Vector2d _last,
163  std::vector<ignition::math::Vector2d> &_polyline);
164 
167  private: SVGLoaderPrivate *dataPtr;
168  };
169  }
170 }
171 
172 #endif
std::vector< std::vector< ignition::math::Vector2d > > polylines
The polylines described by the commands.
Definition: SVGLoader.hh:79
SVG command data structure.
Definition: SVGLoader.hh:48
Handles errors during SVG parsing.
Definition: SVGLoader.hh:40
ignition::math::Matrix3d transform
A 2D transform (or a list of transforms)
Definition: SVGLoader.hh:73
Forward declarations for the common classes.
Definition: Animation.hh:26
std::string style
The style (i.e. stroke style, color, thickness etc)
Definition: SVGLoader.hh:70
A loader for SVG files.
Definition: SVGLoader.hh:83
SVGCommand()
Constructor.
Definition: SVGLoader.hh:51
std::vector< std::vector< SVGCommand > > subpaths
A list of subpaths (as lists of commands)
Definition: SVGLoader.hh:76
std::vector< double > numbers
Coordinates for the command.
Definition: SVGLoader.hh:60
std::string id
An id or name.
Definition: SVGLoader.hh:67
An SVG path element data structure.
Definition: SVGLoader.hh:64
char cmd
A letter that describe the segment.
Definition: SVGLoader.hh:57