Belle II Software development
SVGPrimitivePlotter Class Reference

A concrete plotter that can draw primitive objects to standalone SVG files. More...

#include <SVGPrimitivePlotter.h>

Inheritance diagram for SVGPrimitivePlotter:
PrimitivePlotter

Public Types

using AttributeMap = Belle2::TrackFindingCDC::AttributeMap
 A map type for attributes names to values for additional drawing information.
 

Public Member Functions

 SVGPrimitivePlotter ()
 Default constructor for ROOT compatibility.
 
 SVGPrimitivePlotter (const AttributeMap &svgAttributes)
 Constructor taking additional attributes that go into the toplevel svg tag.
 
 SVGPrimitivePlotter (const SVGPrimitivePlotter &plotter)
 Copy constructor.
 
std::unique_ptr< PrimitivePlotterclone () const override
 Returns a newly created plotter instance containing all information of this.
 
void drawLine (float startX, float startY, float endX, float endY, const AttributeMap &attributeMap=AttributeMap()) override
 Adds a line to the plot.
 
void drawArrow (float startX, float startY, float endX, float endY, const AttributeMap &attributeMap=AttributeMap()) override
 Adds an arrow to the plot.
 
void drawCircle (float centerX, float centerY, float radius, const AttributeMap &attributeMap=AttributeMap()) override
 Adds a circle to the plot.
 
void drawCircleArc (float startX, float startY, float endX, float endY, float radius, bool longArc, bool sweepFlag, const AttributeMap &attributeMap=AttributeMap()) override
 Adds a circle arc to the plot.
 
void drawCurve (const std::vector< std::array< float, 2 > > &points, const std::vector< std::array< float, 2 > > &tangents, const AttributeMap &attributeMap=AttributeMap()) override
 Adds a smooth curve to the plot.
 
void startGroup (const AttributeMap &attributeMap=AttributeMap()) override
 Indicates the start of a group of drawn elements.
 
void endGroup () override
 Indicates the end of a group of drawn elements.
 
const std::string save (const std::string &fileName) override
 Saves the current plot state to a file.
 
void clear () override
 Clears all drawn elements from the plotter.
 
void clearBoundingBox ()
 Clears the current bounding box. Only following draw calls contribute to the bounding box.
 
const BoundingBoxgetBoundingBox () const
 Getter for the bounding box of all drawn objects.
 
void setBoundingBox (const BoundingBox &boundingBox)
 Setter for the bounding box of all drawn objects.
 
float getCanvasWidth ()
 Getter for the canvas width in pixels.
 
float getCanvasHeight ()
 Getter for the canvas height in pixels.
 
void setCanvasWidth (float width)
 Setter for the canvas width in pixels.
 
void setCanvasHeight (float height)
 Setter for the canvas height in pixels The canvas height denotes the size of the image being produced.
 

Static Public Attributes

static const float s_defaultCanvasWidth = 1120.0
 The default width of the canvas to be plotted into.
 
static const float s_defaultCanvasHeight = 1120.0
 The default height of the canvas to be plotted into.
 

Private Member Functions

void indent ()
 Increases the current indentation by one.
 
void dedent ()
 Decreases the current indentation by one.
 
void writeSVGHeader (std::ostream &outputStream)
 Writes the xml header that indicates that this document will be a SVG document to the given output stream.
 
void writeSVGDefs (std::ostream &outputStream)
 Writes a preamble of definitions that define an arrow cap which can be referenced by lines.
 
void writeOpeningTag (std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
 Writes an opening xml tag to the given output stream taking attributes from two sources.
 
void writeStandAloneTag (std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
 Writes a stand alone xml tag to the given output stream taking attributes from two sources.
 
void writeTagIntern (std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
 Writes part that belongs between the <, > brackets.
 
void writeAttributes (std::ostream &outputStream, const AttributeMap &attributeMap)
 Writes attribute specification that belongs between the <, > brackets after the tag name.
 
void writeClosingTag (std::ostream &outputStream, const std::string &tagName)
 Writes a closing xml tag to the given output stream.
 

Private Attributes

std::ostringstream m_svgContentStream
 Memory for the plotted elements. This contains only the fragment that is in-between the svg tags and comes after the global svg definitions.
 
int m_nIndentationSpaces
 Memory for the number of spaces that shall be prepended to each line.
 
AttributeMap m_svgAttributes
 Memory for additional attributes to the top-level svg element.
 
BoundingBox m_boundingBox
 Bounding box of the currently drawn objects.
 
float m_canvasWidth
 Memory for the width of the SVG drawing in pixels.
 
float m_canvasHeight
 Memory for the height of the SVG drawing in pixels.
 

Static Private Attributes

static const int s_defaultNIndentationSpaces = 2
 Constant for the number of indentation space to be used within the svg block.
 
static const int s_addtionalNIndentationSpaces = 2
 Constant for the additional number of space to be prepended with each open tag group.
 

Detailed Description

A concrete plotter that can draw primitive objects to standalone SVG files.

Definition at line 21 of file SVGPrimitivePlotter.h.

Member Typedef Documentation

◆ AttributeMap

using AttributeMap = Belle2::TrackFindingCDC::AttributeMap
inherited

A map type for attributes names to values for additional drawing information.

Definition at line 29 of file PrimitivePlotter.h.

Constructor & Destructor Documentation

◆ SVGPrimitivePlotter() [1/3]

Default constructor for ROOT compatibility.

Definition at line 20 of file SVGPrimitivePlotter.cc.

25{
26}
PrimitivePlotter()
Default constructor for ROOT compatibility.
std::ostringstream m_svgContentStream
Memory for the plotted elements. This contains only the fragment that is in-between the svg tags and ...
int m_nIndentationSpaces
Memory for the number of spaces that shall be prepended to each line.
static const int s_defaultNIndentationSpaces
Constant for the number of indentation space to be used within the svg block.
AttributeMap m_svgAttributes
Memory for additional attributes to the top-level svg element.

◆ SVGPrimitivePlotter() [2/3]

SVGPrimitivePlotter ( const AttributeMap svgAttributes)
explicit

Constructor taking additional attributes that go into the toplevel svg tag.

Parameters
svgAttributesA map of attributes that goes into the toplevel svg tag.

Definition at line 28 of file SVGPrimitivePlotter.cc.

◆ SVGPrimitivePlotter() [3/3]

Copy constructor.

Definition at line 36 of file SVGPrimitivePlotter.cc.

37 : PrimitivePlotter(plotter)
38 , m_svgContentStream(plotter.m_svgContentStream.str(), std::ostringstream::ate)
39 , m_nIndentationSpaces(plotter.m_nIndentationSpaces)
40 , m_svgAttributes(plotter.m_svgAttributes)
41{
42}

Member Function Documentation

◆ clear()

void clear ( )
overridevirtual

Clears all drawn elements from the plotter.

Reimplemented from PrimitivePlotter.

Definition at line 296 of file SVGPrimitivePlotter.cc.

297{
299 m_svgContentStream.str("");
301}
virtual void clear()
Clears all drawn elements from the plotter.

◆ clearBoundingBox()

void clearBoundingBox ( )
inherited

Clears the current bounding box. Only following draw calls contribute to the bounding box.

Definition at line 115 of file PrimitivePlotter.cc.

116{
118}
void clear()
Clears all bounds to NAN.
Definition: BoundingBox.h:87
BoundingBox m_boundingBox
Bounding box of the currently drawn objects.

◆ clone()

std::unique_ptr< PrimitivePlotter > clone ( ) const
overridevirtual

Returns a newly created plotter instance containing all information of this.

The new object is created on the heap. The ownership is to the caller who has the responsibility to destroy it.

Reimplemented from PrimitivePlotter.

Definition at line 44 of file SVGPrimitivePlotter.cc.

45{
46 return std::make_unique<SVGPrimitivePlotter>(*this);
47}

◆ dedent()

void dedent ( )
private

Decreases the current indentation by one.

Definition at line 308 of file SVGPrimitivePlotter.cc.

309{
311}
static const int s_addtionalNIndentationSpaces
Constant for the additional number of space to be prepended with each open tag group.

◆ drawArrow()

void drawArrow ( float  startX,
float  startY,
float  endX,
float  endY,
const AttributeMap attributeMap = AttributeMap() 
)
overridevirtual

Adds an arrow to the plot.

Parameters
startXx coordinate where the arrow starts.
startYy coordinate where the arrow starts.
endXx coordinate where the arrow ends.
endYy coordinate where the arrow ends.
attributeMapA map of string keys and values that describe the drawing properties of the line.

Reimplemented from PrimitivePlotter.

Definition at line 69 of file SVGPrimitivePlotter.cc.

74{
75 PrimitivePlotter::drawArrow(startX, startY, endX, endY, attributeMap);
76
77 AttributeMap geometryAttributeMap {
78 {"x1", std::to_string(startX)},
79 {"x2", std::to_string(endX)},
80 {"y1", std::to_string(startY)},
81 {"y2", std::to_string(endY)},
82 {"marker-end", "url(#endArrow)"}
83 };
84
85 writeStandAloneTag(m_svgContentStream, "line", geometryAttributeMap, attributeMap);
86}
virtual void drawArrow(float startX, float startY, float endX, float endY, const AttributeMap &attributeMap=AttributeMap())
Adds an arrow to the plot.
void writeStandAloneTag(std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
Writes a stand alone xml tag to the given output stream taking attributes from two sources.

◆ drawCircle()

void drawCircle ( float  centerX,
float  centerY,
float  radius,
const AttributeMap attributeMap = AttributeMap() 
)
overridevirtual

Adds a circle to the plot.

Parameters
centerXx coordinate of the circle center.
centerYy coordinate of the circle center.
radiusradius of the circle
attributeMapA map of string keys and values that describe the drawing properties of the line.

Reimplemented from PrimitivePlotter.

Definition at line 89 of file SVGPrimitivePlotter.cc.

93{
94 PrimitivePlotter::drawCircle(centerX, centerY, radius, attributeMap);
95
96 AttributeMap geometryAttributeMap {
97 {"cx", std::to_string(centerX)},
98 {"cy", std::to_string(centerY)},
99 {"r", std::to_string(std::fabs(radius))}
100 };
101
102 writeStandAloneTag(m_svgContentStream, "circle", geometryAttributeMap, attributeMap);
103
104}
virtual void drawCircle(float centerX, float centerY, float radius, const AttributeMap &attributeMap=AttributeMap())
Adds a circle to the plot.

◆ drawCircleArc()

void drawCircleArc ( float  startX,
float  startY,
float  endX,
float  endY,
float  radius,
bool  longArc,
bool  sweepFlag,
const AttributeMap attributeMap = AttributeMap() 
)
overridevirtual

Adds a circle arc to the plot.

Parameters
startXx coordinate where the line starts.
startYy coordinate where the line starts.
endXx coordinate where the line end.
endYy coordinate where the line end.
radiusRadius of the circle
longArcBoolean indicator if the long arc or the short arc is traversed from one point to the other.
sweepFlagBoolean indicator related to the curvature perceived one the circle arc. False means negative curvature. True mean positive curvature.
attributeMapA map of string keys and values that describe the drawing properties of the line.

Reimplemented from PrimitivePlotter.

Definition at line 107 of file SVGPrimitivePlotter.cc.

115{
117 startY,
118 endX,
119 endY,
120 radius,
121 longArc,
122 sweepFlag,
123 attributeMap);
124
125 // Compile the path for a ellipses arc
126 // spelling out all the parts in case somebody want adopt
127 // although only the simpler circle arc case is needed.
128
129 const float radiusX = std::fabs(radius);
130 const float radiusY = std::fabs(radius);
131 const float rotationAngle = 0;
132
133 std::ostringstream pathStream;
134
135 pathStream << "M" << ' ';
136 pathStream << std::to_string(startX) << ' ';
137 pathStream << std::to_string(startY) << ' ';
138 pathStream << "A" << ' ';
139 pathStream << std::to_string(radiusX) << ' ';
140 pathStream << std::to_string(radiusY) << ' ';
141 pathStream << std::to_string(rotationAngle) << ' ';
142 pathStream << std::to_string(longArc) << ' ';
143 pathStream << std::to_string(sweepFlag) << ' ';
144 pathStream << std::to_string(endX) << ' ';
145 pathStream << std::to_string(endY);
146
147 AttributeMap geometryAttributeMap{{"d", pathStream.str()}};
148
149 writeStandAloneTag(m_svgContentStream, "path", geometryAttributeMap, attributeMap);
150}
virtual void drawCircleArc(float startX, float startY, float endX, float endY, float radius, bool longArc, bool sweepFlag, const AttributeMap &attributeMap=AttributeMap())
Adds a circle arc to the plot.

◆ drawCurve()

void drawCurve ( const std::vector< std::array< float, 2 > > &  points,
const std::vector< std::array< float, 2 > > &  tangents,
const AttributeMap attributeMap = AttributeMap() 
)
overridevirtual

Adds a smooth curve to the plot.

Current implementation approximates circle arcs between the points with cubic splines.

Parameters
pointsX,Y points on the curve
tangentsTangent vector at each point. Length of tangents does not matter.
attributeMapA map of string keys and values that describe the drawing properties of the line.

Reimplemented from PrimitivePlotter.

Definition at line 152 of file SVGPrimitivePlotter.cc.

155{
156 // Magic number for circle approximation with splines
157 static const double k = 4.0 / 3.0 * (std::sqrt(2.0) - 1.0);
158
159 B2ASSERT("Expect number of points and tangents to be the same", points.size() == tangents.size());
160 if (points.size() < 2) return;
161
162 PrimitivePlotter::drawCurve(points, tangents, attributeMap);
163
164 std::ostringstream pathStream;
165
166 // Move to point
167 float startX = std::get<0>(points[0]);
168 float startY = std::get<1>(points[0]);
169
170 pathStream << "M";
171 pathStream << ' ' << std::to_string(startX);
172 pathStream << ' ' << std::to_string(startY);
173
174 for (size_t iCurrent = 0, iNext = 1; iNext < points.size(); iCurrent = iNext, ++iNext) {
175
176 float currentTX = std::get<0>(tangents[iCurrent]);
177 float currentTY = std::get<1>(tangents[iCurrent]);
178
179 float nextTX = std::get<0>(tangents[iNext]);
180 float nextTY = std::get<1>(tangents[iNext]);
181
182 float currentT = std::hypot(currentTX, currentTY);
183 float nextT = std::hypot(nextTX, nextTY);
184
185 currentTX /= currentT;
186 currentTY /= currentT;
187 nextTX /= nextT;
188 nextTY /= nextT;
189
190 float currentX = std::get<0>(points[iCurrent]);
191 float currentY = std::get<1>(points[iCurrent]);
192
193 float nextX = std::get<0>(points[iNext]);
194 float nextY = std::get<1>(points[iNext]);
195
196 // Circle arc angle
197 float alpha = std::atan2(currentTX * nextTY - currentTY * nextTX,
198 currentTX * nextTX + currentTY * nextTY);
199
200 float distance = std::hypot(currentX - nextX, currentY - nextY);
201 float controlLength = k * distance / 2 / std::cos(alpha / 2);
202
203 float currentControlX = currentX + currentTX * controlLength;
204 float currentControlY = currentY + currentTY * controlLength;
205
206 float nextControlX = nextX - nextTX * controlLength;
207 float nextControlY = nextY - nextTY * controlLength;
208
209 pathStream << ' ' << "C";
210
211 pathStream << ' ' << std::to_string(currentControlX);
212 pathStream << ' ' << std::to_string(currentControlY);
213
214 pathStream << ' ' << std::to_string(nextControlX);
215 pathStream << ' ' << std::to_string(nextControlY);
216
217 pathStream << ' ' << std::to_string(nextX);
218 pathStream << ' ' << std::to_string(nextY);
219 }
220
221 AttributeMap geometryAttributeMap{{"d", pathStream.str()}};
222
223 writeStandAloneTag(m_svgContentStream, "path", geometryAttributeMap, attributeMap);
224}
virtual void drawCurve(const std::vector< std::array< float, 2 > > &points, const std::vector< std::array< float, 2 > > &tangents, const AttributeMap &attributeMap=AttributeMap())
Adds a smooth curve to the plot.

◆ drawLine()

void drawLine ( float  startX,
float  startY,
float  endX,
float  endY,
const AttributeMap attributeMap = AttributeMap() 
)
overridevirtual

Adds a line to the plot.

Parameters
startXx coordinate where the line starts.
startYy coordinate where the line starts.
endXx coordinate where the line ends.
endYy coordinate where the line ends.
attributeMapA map of string keys and values that describe the drawing properties of the line.

Reimplemented from PrimitivePlotter.

Definition at line 49 of file SVGPrimitivePlotter.cc.

54{
55 PrimitivePlotter::drawLine(startX, startY, endX, endY, attributeMap);
56
57 AttributeMap geometryAttributeMap {
58 {"x1", std::to_string(startX)},
59 {"x2", std::to_string(endX)},
60 {"y1", std::to_string(startY)},
61 {"y2", std::to_string(endY)}
62 };
63
64 writeStandAloneTag(m_svgContentStream, "line", geometryAttributeMap, attributeMap);
65
66}
virtual void drawLine(float startX, float startY, float endX, float endY, const AttributeMap &attributeMap=AttributeMap())
Adds a line to the plot.

◆ endGroup()

void endGroup ( )
overridevirtual

Indicates the end of a group of drawn elements.

Meaning depends on the actual implementation.

Reimplemented from PrimitivePlotter.

Definition at line 231 of file SVGPrimitivePlotter.cc.

232{
234}
void writeClosingTag(std::ostream &outputStream, const std::string &tagName)
Writes a closing xml tag to the given output stream.

◆ getBoundingBox()

const BoundingBox & getBoundingBox ( ) const
inlineinherited

Getter for the bounding box of all drawn objects.

Definition at line 189 of file PrimitivePlotter.h.

190 {
191 return m_boundingBox;
192 }

◆ getCanvasHeight()

float getCanvasHeight ( )
inlineinherited

Getter for the canvas height in pixels.

Definition at line 208 of file PrimitivePlotter.h.

209 {
210 return m_canvasHeight;
211 }
float m_canvasHeight
Memory for the height of the SVG drawing in pixels.

◆ getCanvasWidth()

float getCanvasWidth ( )
inlineinherited

Getter for the canvas width in pixels.

Definition at line 202 of file PrimitivePlotter.h.

203 {
204 return m_canvasWidth;
205 }
float m_canvasWidth
Memory for the width of the SVG drawing in pixels.

◆ indent()

void indent ( )
private

Increases the current indentation by one.

Definition at line 303 of file SVGPrimitivePlotter.cc.

◆ save()

const std::string save ( const std::string &  fileName)
overridevirtual

Saves the current plot state to a file.

Deriving instances should implement the appropriate thing here and may return a modified string indicating the file name to which the plot as been written. It is allowed to append or change the file extension if the concrete implementation decides to do so.

Parameters
fileNamefileName where the plot shall be saved
Returns
Potentially modified file name where the file has actually been written to.

Reimplemented from PrimitivePlotter.

Definition at line 236 of file SVGPrimitivePlotter.cc.

237{
238 // Check indentation
240 B2WARNING("Mismatching calls to startGroup and endGroup detected. "
241 << "Proceeding to write the illforamed result.");
242 }
243
244 int savedNIndentationSpaces = m_nIndentationSpaces;
246
247 std::ofstream outputFileStream;
248 outputFileStream.open(fileName);
249
250 writeSVGHeader(outputFileStream);
251
252 AttributeMap standardAttributeMap {
253 {"baseProfile", "full"},
254 {"ev", "http://www.w3.org/2001/xml-events"},
255 {"version", "1.1"},
256 {"xlink", "http://www.w3.org/1999/xlink"},
257 {"xmlns", "http://www.w3.org/2000/svg"}
258 };
259
260
261 // Combine the viewbox specification from the bounding box
262 // Format "{left} {bottom} {width} {height}"
263 std::ostringstream viewBoxStringStream;
264
265 viewBoxStringStream << getBoundingBox().getLeft();
266 viewBoxStringStream << " ";
267 viewBoxStringStream << getBoundingBox().getBottom();
268 viewBoxStringStream << " ";
269 viewBoxStringStream << getBoundingBox().getWidth();
270 viewBoxStringStream << " ";
271 viewBoxStringStream << getBoundingBox().getHeight();
272
273 AttributeMap variableAttributeMap{
274 {"height", std::to_string(getCanvasHeight())},
275 {"width", std::to_string(getCanvasWidth())},
276 {"viewBox", viewBoxStringStream.str()},
277 };
278
279 variableAttributeMap.insert(m_svgAttributes.begin(), m_svgAttributes.end());
280
281 writeOpeningTag(outputFileStream, "svg", standardAttributeMap, variableAttributeMap);
282
283 writeSVGDefs(outputFileStream);
284
285 // Copy the stream the output and rewind it to its original position.
286 outputFileStream << m_svgContentStream.str();
287
288 writeClosingTag(outputFileStream, "svg");
289
290 outputFileStream.close();
291 m_nIndentationSpaces = savedNIndentationSpaces;
292
293 return fileName;
294}
float getBottom() const
Getter for the location of the bottom of the bounding box rectangle (lower y bound)....
Definition: BoundingBox.h:75
float getWidth() const
Getter for the width of the bounding box rectangle.
Definition: BoundingBox.h:63
float getLeft() const
Getter for the location of the left of the bounding box rectangle (lower x bound)....
Definition: BoundingBox.h:71
float getHeight() const
Getter for the height of the bounding box rectangle.
Definition: BoundingBox.h:67
float getCanvasWidth()
Getter for the canvas width in pixels.
const BoundingBox & getBoundingBox() const
Getter for the bounding box of all drawn objects.
float getCanvasHeight()
Getter for the canvas height in pixels.
void writeSVGDefs(std::ostream &outputStream)
Writes a preamble of definitions that define an arrow cap which can be referenced by lines.
void writeSVGHeader(std::ostream &outputStream)
Writes the xml header that indicates that this document will be a SVG document to the given output st...
void writeOpeningTag(std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
Writes an opening xml tag to the given output stream taking attributes from two sources.

◆ setBoundingBox()

void setBoundingBox ( const BoundingBox boundingBox)
inlineinherited

Setter for the bounding box of all drawn objects.

Definition at line 195 of file PrimitivePlotter.h.

196 {
197 m_boundingBox = boundingBox;
198 }

◆ setCanvasHeight()

void setCanvasHeight ( float  height)
inlineinherited

Setter for the canvas height in pixels The canvas height denotes the size of the image being produced.

The coordinates space that is visible in the picture is a separate concept which is stored in the bounding box (getBoundingBox()).

Definition at line 230 of file PrimitivePlotter.h.

231 {
232 m_canvasHeight = height;
233 }

◆ setCanvasWidth()

void setCanvasWidth ( float  width)
inlineinherited

Setter for the canvas width in pixels.

The canvas height denotes the size of the image being produced. The coordinates space that is visible in the picture is a separate concept which is stored in the bounding box (getBoundingBox()).

Definition at line 219 of file PrimitivePlotter.h.

220 {
221 m_canvasWidth = width;
222 }

◆ startGroup()

void startGroup ( const AttributeMap attributeMap = AttributeMap())
overridevirtual

Indicates the start of a group of drawn elements.

Meaning depends on the actual implementation.

Reimplemented from PrimitivePlotter.

Definition at line 226 of file SVGPrimitivePlotter.cc.

227{
228 writeOpeningTag(m_svgContentStream, "g", attributeMap);
229}

◆ writeAttributes()

void writeAttributes ( std::ostream &  outputStream,
const AttributeMap attributeMap 
)
private

Writes attribute specification that belongs between the <, > brackets after the tag name.

Parameters
[out]outputStreamThe output stream to which the result is written.
[in]attributeMapA map of attribute keys to attribute values that shall be attached to the tag.

Definition at line 435 of file SVGPrimitivePlotter.cc.

437{
438 bool first = true;
439 for (const std::pair<std::string, std::string> attribute : attributeMap) {
440
441 const std::string& key = attribute.first;
442 const std::string& value = attribute.second;
443
444 // Skip special attribute
445 if ('_' == key[0]) {
446 continue;
447 }
448
449 // Introduce a space between every attribute
450 if (first) {
451 first = false;
452 } else {
453 outputStream << ' ';
454 }
455
456 outputStream << key;
457 outputStream << '=' << '"';
458 outputStream << value;
459 outputStream << '"';
460 }
461}

◆ writeClosingTag()

void writeClosingTag ( std::ostream &  outputStream,
const std::string &  tagName 
)
private

Writes a closing xml tag to the given output stream.

Parameters
[out]outputStreamThe output stream to which the result is written.
[in]tagNameName of the xml tag to be written

Definition at line 463 of file SVGPrimitivePlotter.cc.

464{
465 dedent();
466
467 // Indentation
468 outputStream << std::string(m_nIndentationSpaces, ' ');
469
470 // Opening bracket
471 outputStream << '<' << '/';
472
473 // Tag name
474 outputStream << tagName;
475
476 // Closing bracket
477 outputStream << '>';
478
479 // New line
480 outputStream << std::endl;
481}
void dedent()
Decreases the current indentation by one.

◆ writeOpeningTag()

void writeOpeningTag ( std::ostream &  outputStream,
const std::string &  tagName,
const AttributeMap geometryAttributeMap = AttributeMap(),
const AttributeMap styleAttributeMap = AttributeMap() 
)
private

Writes an opening xml tag to the given output stream taking attributes from two sources.

Parameters
[out]outputStreamThe output stream to which the result is written.
[in]tagNameName of the xml tag to be written
[in]geometryAttributeMapA first map of attribute keys to attribute values that shall be attached to the tag. Usually this gives geometrical information but gets essentially treated the same way as the second attribute map
[in]styleAttributeMapA second map of attribute keys to attribute values that shall be attached to the tag. Usually this gives styling information but gets essentially treated the same way as the first attribute map

Definition at line 348 of file SVGPrimitivePlotter.cc.

352{
353 // Indentation
354 outputStream << std::string(m_nIndentationSpaces, ' ');
355
356 // Opening bracket
357 outputStream << '<';
358
359 // Write contained part
360 writeTagIntern(outputStream, tagName, geometryAttributeMap, styleAttributeMap);
361
362 // Closing bracket
363 outputStream << '>';
364
365 // New line
366 outputStream << std::endl;
367
368 indent();
369
370 if (geometryAttributeMap.count("_showAt") + styleAttributeMap.count("_showAt")) {
371 const std::string showAt = geometryAttributeMap.count("_showAt")
372 ? geometryAttributeMap.at("_showAt")
373 : styleAttributeMap.at("_showAt");
374
375 AttributeMap setAttributeMap{
376 {"attributeName", "visibility"},
377 {"to", "hidden"},
378 {"begin", "0s"},
379 {"end", showAt}
380 };
381
382 writeStandAloneTag(outputStream, "set", setAttributeMap);
383 }
384}
void indent()
Increases the current indentation by one.
void writeTagIntern(std::ostream &outputStream, const std::string &tagName, const AttributeMap &geometryAttributeMap=AttributeMap(), const AttributeMap &styleAttributeMap=AttributeMap())
Writes part that belongs between the <, > brackets.

◆ writeStandAloneTag()

void writeStandAloneTag ( std::ostream &  outputStream,
const std::string &  tagName,
const AttributeMap geometryAttributeMap = AttributeMap(),
const AttributeMap styleAttributeMap = AttributeMap() 
)
private

Writes a stand alone xml tag to the given output stream taking attributes from two sources.

Parameters
[out]outputStreamThe output stream to which the result is written.
[in]tagNameName of the xml tag to be written
[in]geometryAttributeMapA first map of attribute keys to attribute values that shall be attached to the tag. Usually this gives geometrical information but gets essentially treated the same way as the second attribute map
[in]styleAttributeMapA second map of attribute keys to attribute values that shall be attached to the tag. Usually this gives styling information but gets essentially treated the same way as the first attribute map

Definition at line 386 of file SVGPrimitivePlotter.cc.

390{
391 // Special handling for _showAt attribute, which introduces a contained <set> tag
392 if (geometryAttributeMap.count("_showAt") + styleAttributeMap.count("_showAt")) {
393 writeOpeningTag(outputStream, tagName, geometryAttributeMap, styleAttributeMap);
394 writeClosingTag(outputStream, tagName);
395 } else {
396
397 // Indentation
398 outputStream << std::string(m_nIndentationSpaces, ' ');
399
400 // Opening bracket
401 outputStream << '<';
402
403 // Write contained part
404 writeTagIntern(outputStream, tagName, geometryAttributeMap, styleAttributeMap);
405
406 // Closing bracket
407 outputStream << '/' << '>';
408
409 // New line
410 outputStream << std::endl;
411 }
412}

◆ writeSVGDefs()

void writeSVGDefs ( std::ostream &  outputStream)
private

Writes a preamble of definitions that define an arrow cap which can be referenced by lines.

Definition at line 319 of file SVGPrimitivePlotter.cc.

320{
321 writeOpeningTag(outputStream, "defs");
322
323 AttributeMap markerAttributes {
324 { "id", "endArrow"},
325 { "viewBox", "0 0 10 10"},
326 { "refX", "1"},
327 { "refY", "5"},
328 { "markerUnits", "strokeWidth"},
329 { "markerWidth", "4"},
330 { "markerHeight", "3"},
331 {"orient", "auto"}
332 };
333
334 writeOpeningTag(outputStream, "marker", markerAttributes);
335
336
337 AttributeMap polylineAttributes {
338 {"points", "0,0 10,5 0,10 1,5"},
339 {"fill", "black"}
340 };
341
342 writeStandAloneTag(outputStream, "polyline", polylineAttributes);
343
344 writeClosingTag(outputStream, "marker");
345 writeClosingTag(outputStream, "defs");
346}

◆ writeSVGHeader()

void writeSVGHeader ( std::ostream &  outputStream)
private

Writes the xml header that indicates that this document will be a SVG document to the given output stream.

Parameters
[out]outputStreamThe output stream to which the result is written.

Definition at line 313 of file SVGPrimitivePlotter.cc.

314{
315 outputStream << "<?xml version=\"1.0\" ?>" << std::endl;
316 outputStream << "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>" << std::endl;
317}

◆ writeTagIntern()

void writeTagIntern ( std::ostream &  outputStream,
const std::string &  tagName,
const AttributeMap geometryAttributeMap = AttributeMap(),
const AttributeMap styleAttributeMap = AttributeMap() 
)
private

Writes part that belongs between the <, > brackets.

Used by writeOpeningTag and writeClosing.

Used by writeOpeningTag and writeClosing. Factored to avoid code duplication.

Parameters
[out]outputStreamThe output stream to which the result is written.
[in]tagNameName of the xml tag to be written
[in]geometryAttributeMapA first map of attribute keys to attribute values that shall be attached to the tag. Usually this gives geometrical information but gets essentially treated the same way as the second attribute map
[in]styleAttributeMapA second map of attribute keys to attribute values that shall be attached to the tag. Usually this gives styling information but gets essentially treated the same way as the first attribute map

Definition at line 414 of file SVGPrimitivePlotter.cc.

418{
419 // Tag name
420 outputStream << tagName;
421
422 // First attribute map
423 if (not geometryAttributeMap.empty()) {
424 outputStream << ' ';
425 writeAttributes(outputStream, geometryAttributeMap);
426 }
427
428 // Second attribute map
429 if (not styleAttributeMap.empty()) {
430 outputStream << ' ';
431 writeAttributes(outputStream, styleAttributeMap);
432 }
433}
void writeAttributes(std::ostream &outputStream, const AttributeMap &attributeMap)
Writes attribute specification that belongs between the <, > brackets after the tag name.

Member Data Documentation

◆ m_boundingBox

BoundingBox m_boundingBox
privateinherited

Bounding box of the currently drawn objects.

Definition at line 237 of file PrimitivePlotter.h.

◆ m_canvasHeight

float m_canvasHeight
privateinherited

Memory for the height of the SVG drawing in pixels.

Definition at line 243 of file PrimitivePlotter.h.

◆ m_canvasWidth

float m_canvasWidth
privateinherited

Memory for the width of the SVG drawing in pixels.

Definition at line 240 of file PrimitivePlotter.h.

◆ m_nIndentationSpaces

int m_nIndentationSpaces
private

Memory for the number of spaces that shall be prepended to each line.

Definition at line 242 of file SVGPrimitivePlotter.h.

◆ m_svgAttributes

AttributeMap m_svgAttributes
private

Memory for additional attributes to the top-level svg element.

Definition at line 245 of file SVGPrimitivePlotter.h.

◆ m_svgContentStream

std::ostringstream m_svgContentStream
private

Memory for the plotted elements. This contains only the fragment that is in-between the svg tags and comes after the global svg definitions.

Definition at line 239 of file SVGPrimitivePlotter.h.

◆ s_addtionalNIndentationSpaces

const int s_addtionalNIndentationSpaces = 2
staticprivate

Constant for the additional number of space to be prepended with each open tag group.

Definition at line 28 of file SVGPrimitivePlotter.h.

◆ s_defaultCanvasHeight

const float s_defaultCanvasHeight = 1120.0
staticinherited

The default height of the canvas to be plotted into.

Definition at line 36 of file PrimitivePlotter.h.

◆ s_defaultCanvasWidth

const float s_defaultCanvasWidth = 1120.0
staticinherited

The default width of the canvas to be plotted into.

Definition at line 33 of file PrimitivePlotter.h.

◆ s_defaultNIndentationSpaces

const int s_defaultNIndentationSpaces = 2
staticprivate

Constant for the number of indentation space to be used within the svg block.

Definition at line 25 of file SVGPrimitivePlotter.h.


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