1 // Gmsh - Copyright (C) 1997-2021 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef PVIEW_H
7 #define PVIEW_H
8 
9 #include <vector>
10 #include <map>
11 #include <string>
12 #include "MVertex.h"
13 #include "MElement.h"
14 #include "SPoint3.h"
15 
16 class PViewData;
17 class PViewOptions;
18 class VertexArray;
19 class smooth_normals;
20 class GModel;
21 class GMSH_PostPlugin;
22 namespace onelab {
23   class localNetworkClient;
24 }
25 
26 // A post-processing view.
27 class PView {
28 private:
29   static int _globalTag;
30   // unique tag of the view (>= 0)
31   int _tag;
32   // index of the view in the current view list
33   int _index;
34   // flag to mark that the view has changed1
35   bool _changed;
36   // tag of the source view if this view is an alias, -1 otherwise
37   int _aliasOf;
38   // eye position (for transparency sorting)
39   SPoint3 _eye;
40   // the options
41   PViewOptions *_options;
42   // the data
43   PViewData *_data;
44   // initialize private stuff
45   void _init(int tag = -1);
46 
47 public:
48   // create a new view with list-based data
49   PView(int tag = -1);
50   // construct a new view using the given data
51   PView(PViewData *data, int tag = -1);
52   // construct a new view, alias of the view "ref"
53   PView(PView *ref, bool copyOptions = true, int tag = -1);
54   // construct a new list-based view from a simple 2D point dataset
55   PView(const std::string &xname, const std::string &yname,
56         std::vector<double> &x, std::vector<double> &y);
57   // construct a new list-based view from a simple 3D point dataset
58   PView(const std::string &name, std::vector<double> &x, std::vector<double> &y,
59         std::vector<double> &z, std::vector<double> &v);
60   // construct a new mesh-based view from a bunch of data
61   PView(const std::string &name, const std::string &type, GModel *model,
62         std::map<int, std::vector<double> > &data, double time = 0.,
63         int numComp = -1, int tag = -1);
64   // add a new time step to a given mesh-based view
65   void addStep(GModel *model, const std::map<int, std::vector<double> > &data,
66                double time = 0., int numComp = -1);
67   // add a new step to a list-based scalar point dataset
68   void addStep(std::vector<double> &y);
69 
70   // default destructor
71   ~PView();
72 
73   // set/get global tag
74   static int getGlobalTag();
75   static void setGlobalTag(int tag);
76 
77   // delete the vertex arrays, used to draw the view efficiently
78   void deleteVertexArrays();
79 
80   // get/set the display options
getOptions()81   PViewOptions *getOptions() { return _options; }
82   void setOptions(PViewOptions *val = nullptr);
83 
84   // get/set the view data
85   PViewData *getData(bool useAdaptiveIfAvailable = false);
setData(PViewData * val)86   void setData(PViewData *val) { _data = val; }
87 
88   // get the view tag (unique and immutable)
getTag()89   int getTag() { return _tag; }
90 
91   // get/set the view index (in the view list)
getIndex()92   int getIndex() { return _index; }
setIndex(int val)93   void setIndex(int val) { _index = val; }
94 
95   // get/set the changed flag
getChanged()96   bool &getChanged() { return _changed; }
97   void setChanged(bool val);
98 
99   // check if the view is an alias ("light copy") of another view
getAliasOf()100   int getAliasOf() { return _aliasOf; }
101 
102   // get/set the eye position (for transparency calculations)
getEye()103   SPoint3 &getEye() { return _eye; }
setEye(SPoint3 & p)104   void setEye(SPoint3 &p) { _eye = p; }
105   //  void setDrawContext(drawContext *ctx){_ctx=ctx;}
106 
107   // get (approx.) memory used by the view, in Mb
108   double getMemoryInMb();
109 
110 #ifndef SWIG
111   // the static list of all loaded views
112   static std::vector<PView *> list;
113 #endif
114 
115   // combine view
116   static void combine(bool time, int how, bool remove, bool copyOptions);
117 
118   // find view by name, by fileName, or by number. If timeStep >= 0, return view
119   // only if it does *not* contain that timestep; if partition >= 0, return view
120   // only if it does *not* contain that partition, if fileName is not empty,
121   // return view only if it does *not* have that fileName.
122   static PView *getViewByName(const std::string &name, int timeStep = -1,
123                               int partition = -1,
124                               const std::string &fileName = "");
125   static PView *getViewByFileName(const std::string &fileName,
126                                   int timeStep = -1, int partition = -1);
127   static PView *getViewByTag(int tag, int timeStep = -1, int partition = -1);
128 
129   // sort views in ::list by name
130   static void sortByName();
131 
132   // IO read routines (these are global: they can create multiple
133   // views)
134   static bool readPOS(const std::string &fileName, int fileIndex = -1);
135   static bool readMSH(const std::string &fileName, int fileIndex = -1,
136                       int partitionToRead = -1);
137   static bool readCGNS(const std::vector<std::vector<MVertex *> > &vertPerZone,
138                        const std::vector<std::vector<MElement *> > &eltPerZone,
139                        const std::string &fileName);
140   static bool readMED(const std::string &fileName, int fileIndex = -1);
141   static bool readPCH(const std::string &fileName, int fileIndex = -1);
142   static bool writeX3D(const std::string &fileName);
143   // IO write routine
144   bool write(const std::string &fileName, int format, bool append = false);
145 
146   // send to ONELAB server
147   void sendToServer(const std::string &name);
148 
149   // Routines for export of adapted views to pvtu file format for parallel
150   // visualization with paraview
151   bool writeAdapt(const std::string &fileName, int useDefaultName,
152                   bool isBinary, int adaptLev, double adaptErr, int npart,
153                   bool append = false);
154 
155   // vertex arrays to draw the elements efficiently
156   VertexArray *va_points, *va_lines, *va_triangles, *va_vectors, *va_ellipses;
157 
158   // fill the vertex arrays, given the current option and data
159   bool fillVertexArrays();
160 
161   // fill a vertex array using a raw stream of bytes
162   static void fillVertexArray(onelab::localNetworkClient *remote, int length,
163                               const char *data, int swap);
164 
165   // smoothed normals
166   smooth_normals *normals;
167 };
168 
169 class nameData {
170 public:
171   std::string name;
172   std::vector<int> indices;
173   std::vector<PViewData *> data;
174   PViewOptions *options;
175 };
176 
177 // this is the maximum number of nodes of elements we actually *draw*
178 // (high order elements are always subdivided before drawing)
179 #define PVIEW_NMAX 8
180 void changeCoordinates(PView *p, int ient, int iele, int numNodes, int type,
181                        int numComp, double **xyz, double **val);
182 bool isElementVisible(PViewOptions *opt, int dim, int numNodes, double **xyz);
183 
184 #endif
185