Debian Codegraph @

avogadro/qtplugins/svg/svg.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "svg.h"
7
8 #include <QDir>
9 #include <QFile>
10 #include <QFileDialog>
11 #include <QSvgGenerator>
12 #include <QAction>
13 #include <avogadro/qtopengl/glwidget.h>
14 #include <avogadro/rendering/camera.h>
15 #include <avogadro/rendering/scene.h>
16 #include <iostream>
17 #include <string>
18
19 namespace Avogadro::QtPlugins {
20
21 const float SVG::DEFAULT_RADIUS = 35.0f;
22 const float SVG::DEFAULT_PEN_WIDTH_MOL = 1.0f;
23 const float SVG::DEFAULT_PEN_WIDTH_BOND = 4.0f;
24 const float SVG::DEFAULT_OFF_SET_PARALEL = 7.0f;
25 const float SVG::DEFAULT_IMAGE_PADDING = 1.0f;
26 const Vector3ub SVG::DEFAULT_BOND_COLOR = Vector3ub(125, 125, 125);
27
28 SVG::SVG(QObject* parent_)
29 : Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr),
30 m_scene(nullptr), m_camera(nullptr),
31 m_action(new QAction(tr("&SVG…"), this))
32 {
33 connect(m_action, SIGNAL(triggered()), SLOT(render()));
34 }
35
36 SVG::~SVG() {}
37
38 QList<QAction*> SVG::actions() const
39 {
40 QList<QAction*> result;
41 return result << m_action;
42 }
43
44 QStringList SVG::menuPath(QAction*) const
45 {
46 return QStringList() << tr("&File") << tr("&Export");
47 }
48
49 void SVG::setMolecule(QtGui::Molecule* mol)
50 {
51 m_molecule = mol;
52 }
53
54 void SVG::setScene(Rendering::Scene* scene)
55 {
56 m_scene = scene;
57 }
58
59 void SVG::setCamera(Rendering::Camera* camera)
60 {
61 m_camera = camera;
62 }
63
64 bool SVG::defaultChecks()
65 {
66 if ((m_molecule == nullptr) || (m_camera == nullptr))
67 return true;
68
69 // Check for 3D coordinates - it's useless to consider the camera otherwise
70 if (m_molecule->atomPositions3d().size() != m_molecule->atomCount())
71 return true;
72
73 // no need to create SVG when there are no atoms
74 if (m_molecule->atomCount() == 0)
75 return true;
76
77 return false;
78 }
79
80 void SVG::setOptions()
81 {
82 m_radius = DEFAULT_RADIUS;
83 m_penWidthMol = DEFAULT_PEN_WIDTH_MOL;
84 m_penWidthBond = DEFAULT_PEN_WIDTH_BOND;
85 m_offSetParalel = DEFAULT_OFF_SET_PARALEL;
86 m_ImagePadding = DEFAULT_IMAGE_PADDING;
87 m_BondColor = DEFAULT_BOND_COLOR;
88 }
89
90 // calculate the camera frustrum
91 void SVG::calculateCamera()
92 {
93 Eigen::Matrix4f m = (m_camera->projection() * m_camera->modelView()).matrix();
94
95 m_frustrum = std::vector<Eigen::Vector4f>(6);
96 // right
97 m_frustrum[0] = Eigen::Vector4f(m(0, 3) + m(0, 0), m(1, 3) + m(1, 0),
98 m(2, 3) + m(2, 0), m(3, 3) + m(3, 0));
99 // left
100 m_frustrum[1] = Eigen::Vector4f(m(0, 3) - m(0, 0), m(1, 3) - m(1, 0),
101 m(2, 3) - m(2, 0), m(3, 3) - m(3, 0));
102 // top
103 m_frustrum[2] = Eigen::Vector4f(m(0, 3) - m(0, 1), m(1, 3) - m(1, 1),
104 m(2, 3) - m(2, 1), m(3, 3) - m(3, 1));
105 // bottom
106 m_frustrum[3] = Eigen::Vector4f(m(0, 3) + m(0, 1), m(1, 3) + m(1, 1),
107 m(2, 3) + m(2, 1), m(3, 3) + m(3, 1));
108 // far
109 m_frustrum[4] = Eigen::Vector4f(m(0, 2), m(1, 2), m(2, 2), m(3, 2));
110 // near
111 m_frustrum[5] = Eigen::Vector4f(m(0, 3) - m(0, 2), m(1, 3) - m(1, 2),
112 m(2, 3) - m(2, 2), m(3, 3) - m(3, 2));
113 }
114
115 // conservative frustrum culling
116 bool SVG::frustrumCulling(const SVGAtom& atom)
117 {
118 return true;
119 for (const auto& p : m_frustrum) {
120 Eigen::Vector4f pos_model;
121 pos_model << atom.pos_model, 1.0f;
122 if (p.dot(pos_model) + atom.radius <= 0)
123 return true;
124 }
125 return false;
126 }
127
128 // transform view position to 2d SVG image
129 Eigen::Vector3f SVG::posToSVGImage(const SVGAtom& atom)
130 {
131 Eigen::Vector3f result = m_camera->project(atom.pos_model);
132 float scale = 1.0f + ((atom.pos_view - m_min).norm() / m_max.norm());
133 float r_scale = 2.0f - (1.0f / atom.radius);
134
135 result[1] = ((result[1] - (m_height / 2.0f)) * -1.0f) + (m_height / 2.0f);
136 result[2] = m_radius * r_scale * scale;
137 return result;
138 }
139
140 // get all model, view, image positions sort them and save id to vector index
141 void SVG::getPositions()
142 {
143 m_atoms.clear();
144 calculateCamera();
145 m_max[0] = m_max[1] = m_max[2] = Eigen::Infinity;
146 m_min[0] = m_min[1] = m_min[2] = Eigen::Infinity * -1.0f;
147
148 const Core::Array<Vector3> atomPositions = m_molecule->atomPositions3d();
149 for (unsigned int i = 0; i < atomPositions.size(); ++i) {
150 Eigen::Vector3f pos =
151 m_camera->modelView() * atomPositions[i].cast<float>();
152 pos[1] *= -1.0f;
153 SVGAtom atom(atomPositions[i].cast<float>(), pos, i,
154 m_molecule->bonds(i).size());
155 m_atoms.push_back(atom);
156 if (frustrumCulling(atom)) {
157 for (unsigned int j = 0; j < 3; ++j) {
158 if (m_min[j] > pos[j])
159 m_min[j] = pos[j];
160 if (m_max[j] < pos[j])
161 m_max[j] = pos[j];
162 }
163 }
164 }
165 m_min[0] -= m_ImagePadding;
166 m_min[1] -= m_ImagePadding;
167 m_max[0] += m_ImagePadding;
168 m_max[1] += m_ImagePadding;
169 m_max -= m_min;
170 std::sort(m_atoms.begin(), m_atoms.end(),
171 [&](const SVGAtom& a, const SVGAtom& b) {
172 // sort using the distance to the camera
173 return a.pos_view.norm() > b.pos_view.norm();
174 });
175 int i = 0;
176 for (auto& m : m_atoms) {
177 m_idToindex[m.id] = i;
178 m.pos_image = posToSVGImage(m);
179 ++i;
180 }
181 }
182
183 // paint all bondings only once
184 void SVG::paintBonds(QPainter& painter, const SVGAtom& atom, unsigned int i,
185 const NeighborListType& bonds)
186 {
187 painter.setPen(QPen(QColor(m_BondColor[0], m_BondColor[1], m_BondColor[2]),
188 m_penWidthBond));
189 for (auto bond : bonds) {
190 unsigned int j = bond.atom2().index();
191 if (m_idToindex[j] == i) {
192 j = bond.atom1().index();
193 }
194 if (m_idToindex[j] <= i) {
195 continue;
196 }
197 // calculate the inicial and final position, considering the atom middle
198 // and the middle of the pen width
199 auto mol_to = m_atoms[m_idToindex[j]].pos_image;
200 Eigen::Vector2f from(atom.pos_image[0] - (m_penWidthBond / 2.0f),
201 atom.pos_image[1] - (m_penWidthBond / 2.0f));
202 Eigen::Vector2f to(mol_to[0] - (m_penWidthBond / 2.0f),
203 mol_to[1] - (m_penWidthBond / 2.0f));
204
205 float L = std::hypot(from[0] - to[0], from[1] - to[1]);
206 float offsetX = (to[1] - from[1]) / L;
207 float offsetY = (from[0] - to[0]) / L;
208 unsigned int order = int(bond.order());
209 // for each bound offset it following the orthogonal direction
210 for (unsigned int o = 0; o < order; ++o) {
211 // if there is only one bond, don't displace
212 float x = 0, y = 0;
213 if (order > 1) {
214 x = (float(o) - (order / 2.0f)) * m_offSetParalel * offsetX;
215 y = (float(o) - (order / 2.0f)) * m_offSetParalel * offsetY;
216 }
217 QLineF line(from[0] + x, from[1] + y, to[0] + x, to[1] + y);
218 painter.drawLine(line);
219 }
220 }
221 }
222
223 void SVG::paintCore(QPainter& painter, const SVGAtom& atom)
224 {
225 Vector3ub atom_color = m_molecule->color(atom.id);
226 QColor color(atom_color[0], atom_color[1], atom_color[2]);
227 painter.setPen(QPen(QColor(0, 0, 0), m_penWidthMol));
228 painter.setBrush(color);
229 painter.drawEllipse(atom.pos_image[0] - atom.pos_image[2] / 2.0f,
230 atom.pos_image[1] - atom.pos_image[2] / 2.0f,
231 atom.pos_image[2], atom.pos_image[2]);
232 }
233
234 void SVG::paintSVG(QPainter& painter)
235 {
236 const Vector4ub background = m_scene->backgroundColor();
237 QColor backgroundColor(background[0], background[1], background[2],
238 background[3]);
239 painter.fillRect(QRect(0, 0, m_width, m_height), backgroundColor);
240 getPositions();
241 for (unsigned int i = 0; i < m_atoms.size(); ++i) {
242 auto atom = m_atoms[i];
243 const NeighborListType bonds = m_molecule->bonds(atom.id);
244 if (frustrumCulling(atom)) {
245 paintBonds(painter, atom, i, bonds);
246 paintCore(painter, atom);
247 }
248 }
249 }
250
251 void SVG::render()
252 {
253 setOptions();
254 if (defaultChecks())
255 return;
256 QString filename = QFileDialog::getSaveFileName(
257 qobject_cast<QWidget*>(parent()), tr("Save File"), QDir::homePath(),
258 tr("SVG (*.svg)"));
259 if (!filename.endsWith(".svg", Qt::CaseInsensitive))
260 filename.append(".svg");
261
262 QFile file(filename);
263 if (!file.open(QIODevice::WriteOnly))
264 return;
265
266 m_width = m_camera->width();
267 m_height = m_camera->height();
268
269 QSvgGenerator generator;
270 generator.setFileName(filename);
271 generator.setSize(QSize(m_width, m_height));
272 generator.setViewBox(QRect(0, 0, m_width, m_height));
273
274 QPainter painter;
275 painter.begin(&generator);
276 paintSVG(painter);
277 painter.end();
278 }
279
280 } // namespace Avogadro::QtPlugins