FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
export_eps.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 {
34 {
35 public:
54 template<typename Shape_, typename DataType_>
55 static void write(
56 std::ostream& os,
58 const double width = 100.0,
59 const double height = 100.0,
60 const double stroke = 0.1,
61 const double extra = 0.0)
62 {
63 XASSERTM(width >= 1.0, "invalid bounding box width");
64 XASSERTM(height >= 1.0, "invalid bounding box height");
65 XASSERTM(stroke > 0.01, "invalid stroke width");
66 XASSERTM(extra >= 0.0, "invalid bounding box extra");
67
68 // get vertices-at-edge
69 const auto& vtx = mesh.get_vertex_set();
70 const auto& idx = mesh.template get_index_set<1,0>();
71
72 // compute minimal and maximal x/y coords
73 DataType_ x_min, x_max, y_min, y_max;
74 x_min = x_max = vtx[0][0];
75 y_min = y_max = vtx[0][1];
76 for(Index i(1); i < vtx.get_num_vertices(); ++i)
77 {
78 const auto& v = vtx[i];
79 Math::minimax(v[0], x_min, x_max);
80 Math::minimax(v[1], y_min, y_max);
81 }
82
83 // compute x/y scale:
84 // Note: PostScript units are "points"
85 // 1 inch = 25.4 millimeters
86 // 1 inch = 72 points
87 // ==> 1 millimeter = 72 / 25.4 points
88 const double x_sc = (72.0 / 25.4) * width / double(x_max - x_min);
89 const double y_sc = (72.0 / 25.4) * height / double(y_max - y_min);
90
91 // compute scaling factor
92 const double scale = Math::min(x_sc, y_sc);
93
94 // compute bounding box (rounding up)
95 const int box_x = int(scale * double(x_max - x_min) + 2.0*extra) + 1;
96 const int box_y = int(scale * double(y_max - y_min) + 2.0*extra) + 1;
97
98 // compute offset from extra
99 const double box_o = 72.0 * extra / 25.4;
100
101 // write EPS header
102 os << "%!!PS-Adobe-3.0 EPSF-3.0\n";
103
104 // write bounding box
105 os << "%%BoundingBox: 0 0 " << box_x << " " << box_y << '\n';
106
107 // set stroke width
108 os << (72.0 * stroke / 25.4) << " setlinewidth\n";
109
110 // begin a new path
111 os << "newpath\n";
112
113 // write all edges
114 for(Index i(0); i < idx.get_num_entities(); ++i)
115 {
116 // get the vertices
117 const auto& v0 = vtx[idx[i][0]];
118 const auto& v1 = vtx[idx[i][1]];
119
120 // transform vertices
121 double v0x = box_o + (double(v0[0]) - x_min) * scale;
122 double v0y = box_o + (double(v0[1]) - y_min) * scale;
123 double v1x = box_o + (double(v1[0]) - x_min) * scale;
124 double v1y = box_o + (double(v1[1]) - y_min) * scale;
125
126 // write vertices
127 os << v0x << " " << v0y << " moveto\n";
128 os << v1x << " " << v1y << " lineto\n";
129 }
130
131 // draw path and show page
132 os << "stroke\n";
133 os << "showpage\n";
134 }
135
154 template<typename Shape_, typename DataType_>
155 static void write(
156 const String& filename,
158 const double width = 100.0,
159 const double height = 100.0,
160 const double stroke = 0.1,
161 const double extra = 0.0)
162 {
163 std::ofstream ofs(filename);
164 if(!ofs.is_open() || !ofs.good())
165 throw FileError(String("Failed to open output file: ") + filename);
166 write(ofs, mesh, width, height, stroke, extra);
167 ofs.close();
168 }
169 };
170 } // namespace Geometry
171} // 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.
EPS exporter class.
Definition: export_eps.hpp:34
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 an EPS file.
Definition: export_eps.hpp:55
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 an EPS file.
Definition: export_eps.hpp:155
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.