FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
export_svg.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/geometry/conformal_mesh.hpp>
12#include <kernel/util/math.hpp>
13
14// includes, system
15#include <iostream>
16#include <fstream>
17
18namespace FEAT
19{
20 namespace Geometry
21 {
35 {
36 public:
55 template<typename Shape_, typename DataType_>
56 static void write(
57 std::ostream& os,
59 const double width = 100.0,
60 const double height = 100.0,
61 const double stroke = 0.1,
62 const double extra = 0.0)
63 {
64 XASSERTM(width >= 1.0, "invalid bounding box width");
65 XASSERTM(height >= 1.0, "invalid bounding box height");
66 XASSERTM(stroke > 0.01, "invalid stroke width");
67 XASSERTM(extra >= 0.0, "invalid bounding box extra");
68
69 // get vertices-at-edge
70 const auto& vtx = mesh.get_vertex_set();
71 const auto& idx = mesh.template get_index_set<1,0>();
72
73 // compute minimal and maximal x/y coords
74 DataType_ x_min, x_max, y_min, y_max;
75 x_min = x_max = vtx[0][0];
76 y_min = y_max = vtx[0][1];
77 for(Index i(1); i < vtx.get_num_vertices(); ++i)
78 {
79 const auto& v = vtx[i];
80 Math::minimax(v[0], x_min, x_max);
81 Math::minimax(v[1], y_min, y_max);
82 }
83
84 const double x_sc = width / double(x_max - x_min);
85 const double y_sc = height / double(y_max - y_min);
86
87 // compute scaling factor
88 const double scale = Math::min(x_sc, y_sc);
89
90 // write SVG header
91 os << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
92 os << "<svg version=\"1.1\" id=\"svg2\" ";
93
94 const double X = double(x_max - x_min);
95 const double Y = double(y_max - y_min);
96
97 const double adjusted_width = scale * X + 2.0 * extra;
98 const double adjusted_height = scale * Y + 2.0 * extra;
99
100 // write dimensions
101 os << "width=\"" << adjusted_width << "mm\" height=\"" << adjusted_height << "mm\" ";
102
103 // write viewbox
104 os << "viewBox=\"" << (-extra/scale) << " " << (-extra/scale) << " ";
105 os << (x_max - x_min + 2.0*extra/scale) << " " << (y_max - y_min + 2.0*extra/scale);
106 os << "\" xmlns=\"http://www.w3.org/2000/svg\">\n";
107
108 // start path and set stroke width
109 os << "<path style=\"fill:none;stroke:#000000;";
110 os << "stroke-width:" << stroke/scale << ";stroke-opacity:1\" id=\"mesh\" d=\" \n";
111
112 // write all edges
113 for(Index i(0); i < idx.get_num_entities(); ++i)
114 {
115 // get the vertices
116 const auto& v0 = vtx[idx[i][0]];
117 const auto& v1 = vtx[idx[i][1]];
118
119 // transform vertices
120 double v0x = (double(v0[0]) - x_min);
121 double v0y = (y_max - double(v0[1]));
122 double v1x = (double(v1[0]) - x_min);
123 double v1y = (y_max - double(v1[1]));
124
125 // write vertices
126 os << "M " << v0x << " " << v0y;
127 os << " L " << v1x << " " << v1y << "\n";
128 }
129
130 // finish
131 os << "\" />\n";
132 os << "</svg>";
133 }
134
153 template<typename Shape_, typename DataType_>
154 static void write(
155 const String& filename,
157 const double width = 100.0,
158 const double height = 100.0,
159 const double stroke = 0.1,
160 const double extra = 0.0)
161 {
162 std::ofstream ofs(filename);
163 if(!ofs.is_open() || !ofs.good())
164 throw FileError(String("Failed to open output file: ") + filename);
165 write(ofs, mesh, width, height, stroke, extra);
166 ofs.close();
167 }
168 };
169 } // namespace Geometry
170} // namespace FEAT
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Base class for file related errors.
Definition: exception.hpp:173
Conformal mesh class template.
VertexSetType & get_vertex_set()
Returns a reference to the vertex set of the mesh.
SVG exporter class.
Definition: export_svg.hpp:35
static void write(std::ostream &os, const ConformalMesh< Shape_, 2, DataType_ > &mesh, const double width=100.0, const double height=100.0, const double stroke=0.1, const double extra=0.0)
Exports a mesh to a SVG file.
Definition: export_svg.hpp:56
static void write(const String &filename, const ConformalMesh< Shape_, 2, DataType_ > &mesh, const double width=100.0, const double height=100.0, const double stroke=0.1, const double extra=0.0)
Exports a mesh to a SVG file.
Definition: export_svg.hpp:154
String class implementation.
Definition: string.hpp:46
void minimax(T_ x, T_ &a, T_ &b)
Updates the minimum and maximum.
Definition: math.hpp:195
T_ min(T_ a, T_ b)
Returns the minimum of two values.
Definition: math.hpp:123
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.