Project
Loading...
Searching...
No Matches
SVGWriter.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef O2_MCH_CONTOUR_SVGWRITER_H
16#define O2_MCH_CONTOUR_SVGWRITER_H
17
18#include "Contour.h"
19#include "BBox.h"
20#include <ostream>
21#include <sstream>
22#include "ContourCreator.h"
23#include <utility>
24#include <boost/format.hpp>
25#include <cmath>
26
27namespace o2
28{
29namespace mch
30{
31namespace contour
32{
33
35{
36
37 public:
39 : mSVGBuffer{},
40 mStyleBuffer{},
41 mWidth{size},
42 mHeight{static_cast<int>(std::round(size * viewingBox.height() / viewingBox.width()))},
43 mViewingBox{viewingBox}
44 {
45 }
46
47 template <typename T>
49 {
50 mSVGBuffer << a;
51 return *this;
52 }
53
54 void svgGroupStart(const std::string& classname)
55 {
56 mSVGBuffer << R"(<g class=")" << classname << R"(">)"
57 << "\n";
58 }
59
60 void svgGroupEnd() { mSVGBuffer << "</g>\n"; }
61
62 void box(double x, double y, double w, double h)
63 {
64 mSVGBuffer << boost::format(R"(<rect x="%f" y="%f" width="%f" height="%f"/>)") % x % y % w % h << "\n";
65 }
66
67 void text(const std::string& text, double x, double y)
68 {
69 mSVGBuffer << boost::format(R"(<text x="%f" y="%f">%s</text>)") % x % y % text.c_str() << "\n";
70 }
71
72 template <typename T>
74 {
75 mSVGBuffer << R"(<polygon points=")";
76 auto vertices = getVertices(p);
77 for (auto j = 0; j < vertices.size(); ++j) {
78 auto v = vertices[j];
79 mSVGBuffer << v.x << "," << v.y << ' ';
80 }
81 mSVGBuffer << R"("/>)"
82 << "\n";
83 }
84
85 template <typename T>
87 {
88 for (auto& p : c.getPolygons()) {
89 polygon(p);
90 }
91 }
92
93 template <typename T>
94 void polygon(const o2::mch::contour::Polygon<T>& p, const std::string& fillColor)
95 {
96 // modification
97 mSVGBuffer << R"(<polygon fill=")" << fillColor << R"(" points=")";
98 auto vertices = getVertices(p);
99 for (auto j = 0; j < vertices.size(); ++j) {
100 auto v = vertices[j];
101 mSVGBuffer << v.x << "," << v.y << ' ';
102 }
103 mSVGBuffer << R"("/>)"
104 << "\n";
105 }
106
107 template <typename T>
108 void contour(const o2::mch::contour::Contour<T>& c, const std::string& color)
109 {
110
111 for (auto& p : c.getPolygons()) {
112 polygon(p, color);
113 }
114 }
115
116 void points(const std::vector<std::pair<double, double>>& pts, double radius = 0.05)
117 {
118 for (auto& p : pts) {
119 mSVGBuffer << boost::format(R"(<circle cx="%f" cy="%f" r="%f"/>)") % p.first % p.second % radius << "\n";
120 }
121 }
122
123 void addStyle(const std::string& style) { mStyleBuffer << style << "\n"; }
124
125 void writeStyle(std::ostream& os) { os << "<style>\n"
126 << mStyleBuffer.str() << "</style>\n"; }
127
128 void writeSVG(std::ostream& os)
129 {
130 os << boost::format(R"(<svg width="%d" height="%d" viewBox="%f %f %f %f">)") % mWidth %
131 mHeight % mViewingBox.xmin() % mViewingBox.ymin() % mViewingBox.width() % mViewingBox.height();
132
133 os << mSVGBuffer.str();
134
135 os << "</svg>";
136 }
137
138 void writeHTML(std::ostream& os)
139 {
140 os << "<html>\n";
141 writeStyle(os);
142 os << "<body>\n";
143 writeSVG(os);
144 os << "</body>\n";
145 os << "</html>\n";
146 }
147
148 private:
149 std::stringstream mSVGBuffer;
150 std::stringstream mStyleBuffer;
151 int mWidth;
152 int mHeight;
154};
155
156} // namespace contour
157} // namespace mch
158} // namespace o2
159
160#endif
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
Class for time synchronization of RawReader instances.
T xmin() const
Definition BBox.h:40
T ymin() const
Definition BBox.h:44
T height() const
Definition BBox.h:54
T width() const
Definition BBox.h:52
void points(const std::vector< std::pair< double, double > > &pts, double radius=0.05)
Definition SVGWriter.h:116
void polygon(const o2::mch::contour::Polygon< T > &p)
Definition SVGWriter.h:73
SVGWriter & operator<<(T a)
Definition SVGWriter.h:48
void box(double x, double y, double w, double h)
Definition SVGWriter.h:62
void addStyle(const std::string &style)
Definition SVGWriter.h:123
void polygon(const o2::mch::contour::Polygon< T > &p, const std::string &fillColor)
Definition SVGWriter.h:94
SVGWriter(o2::mch::contour::BBox< double > viewingBox, int size=1024)
Definition SVGWriter.h:38
void contour(const o2::mch::contour::Contour< T > &c, const std::string &color)
Definition SVGWriter.h:108
void writeStyle(std::ostream &os)
Definition SVGWriter.h:125
void contour(const o2::mch::contour::Contour< T > &c)
Definition SVGWriter.h:86
void svgGroupStart(const std::string &classname)
Definition SVGWriter.h:54
void writeSVG(std::ostream &os)
Definition SVGWriter.h:128
void writeHTML(std::ostream &os)
Definition SVGWriter.h:138
void text(const std::string &text, double x, double y)
Definition SVGWriter.h:67
GLint GLenum GLint x
Definition glcorearb.h:403
GLsizeiptr size
Definition glcorearb.h:659
GLuint color
Definition glcorearb.h:1272
const GLdouble * v
Definition glcorearb.h:832
GLint GLsizei GLsizei height
Definition glcorearb.h:270
GLint GLsizei width
Definition glcorearb.h:270
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
std::vector< o2::mch::contour::Vertex< T > > getVertices(const std::vector< o2::mch::contour::Polygon< T > > &polygons)
Definition Contour.h:31
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Defining DataPointCompositeObject explicitly as copiable.