SVGLoader.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2016 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 #include <gazebo/math/Vector2d.hh>
30 #include <gazebo/math/Matrix3.hh>
31 
32 class TiXmlElement;
33 class TiXmlNode;
34 
35 namespace gazebo
36 {
37  namespace common
38  {
39  class SVGLoaderPrivate;
40 
42  class GZ_COMMON_VISIBLE SvgError: public std::runtime_error
43  {
46  public: SvgError(const std::string &_what);
47  };
48 
51  {
53  public: SVGCommand() : cmd(' ') {}
54 
56  public: virtual ~SVGCommand() = default;
57 
59  public: char cmd;
60 
62  public: std::vector<double> numbers;
63  };
64 
67  {
69  std::string id;
70 
72  std::string style;
73 
75  ignition::math::Matrix3d transform;
76 
78  std::vector< std::vector<SVGCommand> > subpaths;
79 
81  std::vector< std::vector<ignition::math::Vector2d> > polylines;
82  };
83 
86  {
89  public: SVGLoader(unsigned int _samples);
90 
92  public: ~SVGLoader();
93 
98  public: bool Parse(const std::string &_filename,
99  std::vector<SVGPath> &_paths);
100 
106  public: static void PathsToClosedPolylines(
107  const std::vector<common::SVGPath> &_paths,
108  double _tol,
109  std::vector< std::vector<ignition::math::Vector2d> > &_closedPolys,
110  std::vector< std::vector<ignition::math::Vector2d> > &_openPolys);
111 
115  public: void DumpPaths(const std::vector<SVGPath> &_paths,
116  std::ostream &_out) const;
117 
121  private: void GetPathCommands(const std::vector<std::string> &_tokens,
122  SVGPath &_path);
123 
127  private: void GetPathAttribs(TiXmlElement *_pElement, SVGPath &_path);
128 
132  private: void GetSvgPaths(TiXmlNode *_pParent,
133  std::vector<SVGPath> &_paths);
134 
138  private: void ExpandCommands(
139  const std::vector< std::vector<SVGCommand> > &_subpaths,
140  SVGPath& _path);
141 
146  private: void SplitSubpaths(const std::vector<SVGCommand> &_cmds,
147  std::vector< std::vector<SVGCommand> > &_subpaths);
148 
153  private: void PathToPoints(const SVGPath &_path,
154  double _resolution,
155  std::vector< std::vector<ignition::math::Vector2d> > &_polys);
156 
162  private: ignition::math::Vector2d SubpathToPolyline(
163  const std::vector<SVGCommand> &_subpath,
164  ignition::math::Vector2d _last,
165  std::vector<ignition::math::Vector2d> &_polyline);
166 
169  private: SVGLoaderPrivate *dataPtr;
170  };
171  }
172 }
173 
174 #endif
SVG command data structure.
Definition: SVGLoader.hh:50
Handles errors during SVG parsing.
Definition: SVGLoader.hh:42
Definition: SVGLoaderPrivate.hh:33
#define GZ_COMMON_VISIBLE
Definition: system.hh:84
A loader for SVG files.
Definition: SVGLoader.hh:85
std::vector< std::vector< SVGCommand > > subpaths
A list of subpaths (as lists of commands)
Definition: SVGLoader.hh:78
SVGCommand()
Constructor.
Definition: SVGLoader.hh:53
std::vector< double > numbers
Coordinates for the command.
Definition: SVGLoader.hh:62
std::string style
The style (i.e. stroke style, color, thickness etc)
Definition: SVGLoader.hh:72
std::vector< std::vector< ignition::math::Vector2d > > polylines
The polylines described by the commands.
Definition: SVGLoader.hh:81
std::string id
An id or name.
Definition: SVGLoader.hh:69
char cmd
A letter that describe the segment.
Definition: SVGLoader.hh:59
An SVG path element data structure.
Definition: SVGLoader.hh:66
ignition::math::Matrix3d transform
A 2D transform (or a list of transforms)
Definition: SVGLoader.hh:75