1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2020 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 
18 
19 
20 #ifndef LIBMESH_POSTSCRIPT_IO_H
21 #define LIBMESH_POSTSCRIPT_IO_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/mesh_output.h"
26 //#include "libmesh/dense_matrix.h"
27 #include "libmesh/point.h"
28 
29 // C++ includes
30 #include <fstream>
31 #include <sstream>
32 #include <vector>
33 
34 namespace libMesh
35 {
36 
37 // Forward declarations
38 class MeshBase;
39 class Elem;
40 
41 /**
42  * This class implements writing 2D meshes in Postscript.  It borrows
43  * several ideas from, and is a more simple-minded version of, the
44  * DataOutBase::write_eps() function from Deal II.  Only output is
45  * supported here, and only the Mesh (none of the data) is written.
46  * The main use I imagined for this class is creating nice Mesh
47  * images for publications, since I didn't find/don't know of a free
48  * visualization program which would do this.
49  *
50  * \author John W. Peterson
51  * \date 2008
52  */
53 class PostscriptIO : public MeshOutput<MeshBase>
54 {
55 public:
56   /**
57    * Constructor.
58    */
59   explicit
60   PostscriptIO (const MeshBase & mesh);
61 
62   /**
63    * Destructor.
64    */
65   virtual ~PostscriptIO ();
66 
67   /**
68    * This method implements writing a mesh to a specified file.
69    */
70   virtual void write (const std::string &) override;
71 
72   /**
73    * Controls greyscale shading of cells.  By default this value
74    * is 0.0 (which actually corresponds to black) and this indicates
75    * "no shading" i.e. only mesh lines will be drawn.  Any other
76    * value in (0,1] will cause the cells to be grey-shaded to some
77    * degree, with higher values being lighter.  A value of 0.75
78    * gives decent results.
79    */
80   Real shade_value;
81 
82   /**
83    * Control the thickness of the lines used.  0.5 is a reasonable default
84    * for printed images, but you may need to decrease this value (or
85    * choose it adaptively) when there are very slim cells present in
86    * the mesh.
87    */
88   Real line_width;
89 
90   /**
91    * Draws an element with Bezier curves
92    */
93   void plot_quadratic_elem(const Elem * elem);
94 
95   /**
96    * Draws an element with straight lines
97    */
98   void plot_linear_elem(const Elem * elem);
99 
100 private:
101   /**
102    * Given a quadratic edge Elem which lies in the x-y plane,
103    * computes the Bezier coefficients.  These may be passed to
104    * the Postscript routine "curveto".
105    */
106   void _compute_edge_bezier_coeffs(const Elem * elem);
107 
108   /**
109    * Coefficients of the transformation from physical-space
110    * edge coordinates to Bezier basis coefficients.  Transforms
111    * x and y separately.
112    */
113   //DenseMatrix<float> _M;
114   static const float _bezier_transform[3][3];
115 
116   /**
117    * Vector containing 3 points corresponding to Bezier coefficients,
118    * as computed by _compute_edge_bezier_coeffs.
119    */
120   std::vector<Point> _bezier_coeffs;
121 
122   /**
123    * Amount to add to every (x,y) point to place it in Postscript coordinates.
124    */
125   Point _offset;
126 
127   /**
128    * Amount by which to stretch each point to place it in Postscript coordinates.
129    */
130   Real _scale;
131 
132   /**
133    * A point object used for temporary calculations
134    */
135   Point _current_point;
136 
137   /**
138    * Drawing style-independent data for a single cell.  This can be
139    * used as a temporary buffer for storing data which may be sent to
140    * the output stream multiple times.
141    */
142   std::ostringstream _cell_string;
143 
144   /**
145    * Output file stream which will be opened when the file name is known
146    */
147   std::ofstream _out;
148 };
149 
150 } // namespace libMesh
151 
152 #endif // LIBMESH_POSTSCRIPT_IO_H
153