gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
PView.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 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 #include <string.h>
7 #include <algorithm>
8 #include "PView.h"
9 #include "PViewDataList.h"
10 #include "PViewDataGModel.h"
11 #include "PViewOptions.h"
12 #include "VertexArray.h"
13 #include "SmoothData.h"
14 #include "adaptiveData.h"
15 #include "GmshMessage.h"
16 
17 int PView::_globalTag = 1;
18 std::vector<PView *> PView::list;
19 
20 void PView::_init(int tag)
21 {
22  if(tag >= 0) {
23  if(tag == 0)
24  Msg::Warning("Using tag == 0 in view is discouraged; view tags should be >= 1");
25  _tag = tag;
26  _globalTag = std::max(_globalTag, _tag) + 1;
27  }
28  else {
29  _tag = _globalTag++;
30  }
31 
32  _changed = true;
33  _aliasOf = -1;
34  _eye = SPoint3(0., 0., 0.);
36  normals = nullptr;
37 
38  for(std::size_t i = 0; i < list.size(); i++) {
39  if(list[i]->getTag() == _tag) {
40  // in normal operation this should not happen, but we allow it when
41  // programmatically forcing view tags (e.g. when using the views from
42  // within getdp's post-processing operations); this is dangerous, as it
43  // breaks aliases
44  Msg::Info("Removing existing View[%d] (tag = %d)", i, _tag);
45  delete list[i]; // warning: this changes the list
46  }
47  }
48 
49  list.push_back(this);
50  for(std::size_t i = 0; i < list.size(); i++) list[i]->setIndex(i);
51 }
52 
53 PView::PView(int tag)
54 {
55  _init(tag);
56  _data = new PViewDataList();
61 }
62 
63 PView::PView(PViewData *data, int tag)
64 {
65  _init(tag);
66  _data = data;
71 }
72 
73 PView::PView(PView *ref, bool copyOptions, int tag)
74 {
75  _init(tag);
76 
77  if(ref->getAliasOf() >= 0) { // alias of an alias
78  PView *orig = getViewByTag(ref->getAliasOf());
79  if(orig)
80  _aliasOf = orig->getTag();
81  else {
82  Msg::Warning("Original view for alias does not exist anymore");
83  _aliasOf = ref->getTag();
84  }
85  }
86  else
87  _aliasOf = ref->getTag();
88 
89  _data = ref->getData();
90  if(copyOptions)
91  _options = new PViewOptions(*ref->getOptions());
92  else
97 }
98 
99 PView::PView(const std::string &xname, const std::string &yname,
100  std::vector<double> &x, std::vector<double> &y)
101 {
102  _init();
103  _data = new PViewDataList();
104  _data->setXY(x, y);
105  _data->setName(yname);
106  _data->setFileName(yname + ".pos");
109  _options->axes = 3;
110  _options->lineWidth = 2.;
111  _options->pointSize = 4.;
112  _options->axesLabel[0] = xname;
113 }
114 
115 void PView::addStep(std::vector<double> &y)
116 {
117  PViewDataList *d = dynamic_cast<PViewDataList *>(_data);
118  if(d)
119  d->addStep(y);
120  else
121  Msg::Error("Can only add step data to list-based datasets");
122 }
123 
124 PView::PView(const std::string &name, std::vector<double> &x,
125  std::vector<double> &y, std::vector<double> &z,
126  std::vector<double> &v)
127 {
128  _init();
129  _data = new PViewDataList();
130  _data->setXYZV(x, y, z, v);
131  _data->setName(name);
132  _data->setFileName(name + ".pos");
134  _options->axes = 3;
135  _options->pointSize = 7.;
136  _options->pointType = 1.;
137 }
138 
139 PView::PView(const std::string &name, const std::string &type, GModel *model,
140  std::map<int, std::vector<double> > &data, double time,
141  int numComp, int tag)
142 {
143  _init(tag);
145  if(type == "NodeData")
147  else if(type == "ElementData")
149  else if(type == "ElementNodeData")
151  else if(type == "Beam")
153  else {
154  Msg::Error("Unknown type of view to create '%s'", type.c_str());
155  return;
156  }
157  PViewDataGModel *d = new PViewDataGModel(t);
158  d->addData(model, data, 0, time, 1, numComp);
159  d->setName(name);
160  d->setFileName(name + ".msh");
161  _data = d;
166 }
167 
168 void PView::addStep(GModel *model,
169  const std::map<int, std::vector<double> > &data,
170  double time, int numComp)
171 {
172  PViewDataGModel *d = dynamic_cast<PViewDataGModel *>(_data);
173  if(d)
174  d->addData(model, data, d->getNumTimeSteps(), time, 1, numComp);
175  else
176  Msg::Error("Can only add step data to mesh-based datasets");
177 }
178 
180 {
182  if(normals) delete normals;
183  if(_options) delete _options;
184 
185  auto it = std::find(list.begin(), list.end(), this);
186  if(it != list.end()) list.erase(it);
187  for(std::size_t i = 0; i < list.size(); i++) list[i]->setIndex(i);
188 
189  if(!_data) return;
190 
191  // do not delete if another view is an alias of this one
192  for(std::size_t i = 0; i < list.size(); i++)
193  if(list[i]->getAliasOf() == _tag) return;
194 
195  // do not delete if this view is an alias and 1) if the original
196  // still exists, or 2) if there are other aliases to the same view
197  if(_aliasOf >= 0)
198  for(std::size_t i = 0; i < list.size(); i++)
199  if(list[i]->getTag() == _aliasOf || list[i]->getAliasOf() == _aliasOf)
200  return;
201 
202  Msg::Debug("Deleting data in View[%d] (tag = %d)", _index, _tag);
203  delete _data;
204 }
205 
207 
208 void PView::setGlobalTag(int tag) { _globalTag = tag; }
209 
211 {
212  if(va_points) delete va_points;
213  va_points = nullptr;
214  if(va_lines) delete va_lines;
215  va_lines = nullptr;
216  if(va_triangles) delete va_triangles;
217  va_triangles = nullptr;
218  if(va_vectors) delete va_vectors;
219  va_vectors = nullptr;
220  if(va_ellipses) delete va_ellipses;
221  va_ellipses = nullptr;
222 }
223 
225 {
226  // deep copy options
227  if(val)
228  *_options = *val;
229  else if(_options)
231 }
232 
233 PViewData *PView::getData(bool useAdaptiveIfAvailable)
234 {
235  if(useAdaptiveIfAvailable && _data->getAdaptiveData() && !_data->isRemote())
236  return _data->getAdaptiveData()->getData();
237  else
238  return _data;
239 }
240 
241 void PView::setChanged(bool val)
242 {
243  _changed = val;
244  // reset the eye position everytime we change the view so that the
245  // arrays get resorted for transparency
246  if(_changed) _eye = SPoint3(0., 0., 0.);
247 }
248 
249 void PView::combine(bool time, int how, bool remove, bool copyOptions)
250 {
251  // time == true: combine the timesteps (oherwise combine the elements)
252  // how == 0: try to combine all visible views
253  // 1: try to combine all views
254  // 2: try to combine all views having identical names
255 
256  std::vector<nameData> nds;
257  for(std::size_t i = 0; i < list.size(); i++) {
258  PView *p = list[i];
259  PViewData *data = p->getData();
260  if(how || p->getOptions()->visible) {
261  nameData nd;
262  // this will lead to weird results if there are views named
263  // "__all__" or "__vis__" :-)
264  if(how == 2)
265  nd.name = data->getName();
266  else if(how == 1)
267  nd.name = "__all__";
268  else
269  nd.name = "__vis__";
270  std::size_t j = 0;
271  while(j < nds.size()) {
272  if(nds[j].name == nd.name) {
273  nds[j].data.push_back(data);
274  nds[j].indices.push_back(i);
275  break;
276  }
277  j++;
278  }
279  if(j == nds.size()) {
280  nd.data.push_back(data);
281  nd.indices.push_back(i);
282  nd.options = p->getOptions();
283  nds.push_back(nd);
284  }
285  }
286  }
287 
288  std::set<PView *> rm;
289  for(std::size_t i = 0; i < nds.size(); i++) {
290  if(nds[i].data.size() > 1) { // there's potentially something to combine
291  // sanity checks:
292  bool allListBased = true, allModelBased = true;
293  for(std::size_t j = 0; j < nds[i].data.size(); j++) {
294  PViewDataList *d1 = dynamic_cast<PViewDataList *>(nds[i].data[j]);
295  if(!d1) allListBased = false;
296  PViewDataGModel *d2 = dynamic_cast<PViewDataGModel *>(nds[i].data[j]);
297  if(!d2) allModelBased = false;
298  }
299  PViewData *data = nullptr;
300  if(allListBased) { data = new PViewDataList(); }
301  else if(allModelBased) {
302  PViewDataGModel *d2 = dynamic_cast<PViewDataGModel *>(nds[i].data[0]);
303  data = new PViewDataGModel(d2->getType());
304  }
305  else {
306  Msg::Error("Cannot combine hybrid list/mesh-based datasets");
307  continue;
308  }
309  PView *p = new PView(data);
310  bool res = time ? data->combineTime(nds[i]) : data->combineSpace(nds[i]);
311  if(res) {
312  for(std::size_t j = 0; j < nds[i].indices.size(); j++)
313  rm.insert(list[nds[i].indices[j]]);
314  PViewOptions *opt = p->getOptions();
315  if(opt->adaptVisualizationGrid) {
316  // the (empty) adaptive data created in PView() must be recreated,
317  // since we added some data
318  data->destroyAdaptiveData();
320  opt->targetError);
321  }
322  if(copyOptions && nds[i].options)
323  p->setOptions(new PViewOptions(*nds[i].options));
324  }
325  else
326  delete p;
327  }
328  }
329  if(remove)
330  for(auto it = rm.begin(); it != rm.end(); it++) delete *it;
331 }
332 
334 public:
335  bool operator()(PView *v1, PView *v2) const
336  {
337  return v1->getData()->getName() < v2->getData()->getName();
338  }
339 };
340 
342 {
343  std::sort(list.begin(), list.end(), PViewLessThanName());
344  for(std::size_t i = 0; i < list.size(); i++) list[i]->setIndex(i);
345 }
346 
347 PView *PView::getViewByName(const std::string &name, int timeStep,
348  int partition, const std::string &fileName)
349 {
350  // search views from most recently to least recently added
351  for(int i = list.size() - 1; i >= 0; i--) {
352  if(list[i]->getData()->getName() == name &&
353  ((timeStep < 0 || !list[i]->getData()->hasTimeStep(timeStep)) ||
354  (partition < 0 ||
355  !list[i]->getData()->hasPartition(timeStep, partition))) &&
356  (fileName.empty() || !list[i]->getData()->hasFileName(fileName)))
357  return list[i];
358  }
359  return nullptr;
360 }
361 
362 PView *PView::getViewByFileName(const std::string &fileName, int timeStep,
363  int partition)
364 {
365  // search views from most recently to least recently added
366  for(int i = list.size() - 1; i >= 0; i--) {
367  if(list[i]->getData()->getFileName() == fileName &&
368  ((timeStep < 0 || !list[i]->getData()->hasTimeStep(timeStep)) ||
369  (partition < 0 ||
370  !list[i]->getData()->hasPartition(timeStep, partition))))
371  return list[i];
372  }
373  return nullptr;
374 }
375 
376 PView *PView::getViewByTag(int tag, int timeStep, int partition)
377 {
378  for(std::size_t i = 0; i < list.size(); i++) {
379  if(list[i]->getTag() == tag &&
380  ((timeStep < 0 || !list[i]->getData()->hasTimeStep(timeStep)) ||
381  (partition < 0 ||
382  !list[i]->getData()->hasPartition(timeStep, partition))))
383  return list[i];
384  }
385  return nullptr;
386 }
387 
389 {
390  double mem = 0;
391  if(va_points) mem += va_points->getMemoryInMb();
392  if(va_lines) mem += va_lines->getMemoryInMb();
394  if(va_vectors) mem += va_vectors->getMemoryInMb();
395  if(va_ellipses) mem += va_ellipses->getMemoryInMb();
396  mem += getData()->getMemoryInMb();
397  return mem;
398 }
PViewOptions::timeStep
int timeStep
Definition: PViewOptions.h:61
PView::va_points
VertexArray * va_points
Definition: PView.h:156
PViewOptions::lineWidth
double lineWidth
Definition: PViewOptions.h:69
PView::_init
void _init(int tag=-1)
Definition: PView.cpp:20
PView
Definition: PView.h:27
PView::~PView
~PView()
Definition: PView.cpp:179
PViewDataGModel::ElementData
@ ElementData
Definition: PViewDataGModel.h:195
nameData::indices
std::vector< int > indices
Definition: PView.h:172
PViewDataGModel
Definition: PViewDataGModel.h:191
PViewDataGModel::NodeData
@ NodeData
Definition: PViewDataGModel.h:194
PView::getViewByName
static PView * getViewByName(const std::string &name, int timeStep=-1, int partition=-1, const std::string &fileName="")
Definition: PView.cpp:347
PView::getMemoryInMb
double getMemoryInMb()
Definition: PView.cpp:388
VertexArray::getMemoryInMb
double getMemoryInMb()
Definition: VertexArray.cpp:34
PViewData::combineSpace
virtual bool combineSpace(nameData &nd)
Definition: PViewData.cpp:242
Msg::Info
static void Info(const char *fmt,...)
Definition: GmshMessage.cpp:587
Msg::Debug
static void Debug(const char *fmt,...)
Definition: GmshMessage.cpp:752
nameData::name
std::string name
Definition: PView.h:171
PViewDataList
Definition: PViewDataList.h:17
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
VertexArray.h
PView::_eye
SPoint3 _eye
Definition: PView.h:39
SPoint3
Definition: SPoint3.h:14
PView::_options
PViewOptions * _options
Definition: PView.h:41
nameData
Definition: PView.h:169
nameData::options
PViewOptions * options
Definition: PView.h:174
PView.h
PView::va_vectors
VertexArray * va_vectors
Definition: PView.h:156
PView::_tag
int _tag
Definition: PView.h:31
GmshMessage.h
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
PViewData::getAdaptiveData
adaptiveData * getAdaptiveData()
Definition: PViewData.h:238
PView::setChanged
void setChanged(bool val)
Definition: PView.cpp:241
PViewData::initAdaptiveData
void initAdaptiveData(int step, int level, double tol)
Definition: PViewData.cpp:37
adaptiveData.h
PViewDataList::addStep
void addStep(std::vector< double > &y)
Definition: PViewDataList.cpp:64
PViewOptions::pointType
int pointType
Definition: PViewOptions.h:68
PView::_index
int _index
Definition: PView.h:33
PView::_aliasOf
int _aliasOf
Definition: PView.h:37
PView::getViewByTag
static PView * getViewByTag(int tag, int timeStep=-1, int partition=-1)
Definition: PView.cpp:376
PView::getTag
int getTag()
Definition: PView.h:89
PView::normals
smooth_normals * normals
Definition: PView.h:166
nameData::data
std::vector< PViewData * > data
Definition: PView.h:173
PView::va_ellipses
VertexArray * va_ellipses
Definition: PView.h:156
PView::combine
static void combine(bool time, int how, bool remove, bool copyOptions)
Definition: PView.cpp:249
GModel
Definition: GModel.h:44
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
PView::PView
PView(int tag=-1)
Definition: PView.cpp:53
PViewOptions.h
PViewDataList.h
PViewData::destroyAdaptiveData
void destroyAdaptiveData()
Definition: PViewData.cpp:79
PViewDataGModel::BeamData
@ BeamData
Definition: PViewDataGModel.h:198
PViewData::combineTime
virtual bool combineTime(nameData &nd)
Definition: PViewData.cpp:236
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
PView::_changed
bool _changed
Definition: PView.h:35
PViewOptions::maxRecursionLevel
int maxRecursionLevel
Definition: PViewOptions.h:78
PViewOptions::targetError
double targetError
Definition: PViewOptions.h:79
PViewDataGModel.h
adaptiveData::getData
PViewData * getData()
Definition: adaptiveData.h:674
PView::_globalTag
static int _globalTag
Definition: PView.h:29
PViewData::isRemote
virtual bool isRemote()
Definition: PViewData.h:281
PViewOptions::Plot2D
@ Plot2D
Definition: PViewOptions.h:18
PView::_data
PViewData * _data
Definition: PView.h:43
PViewDataGModel::getNumTimeSteps
int getNumTimeSteps()
Definition: PViewDataGModel.cpp:270
PView::getAliasOf
int getAliasOf()
Definition: PView.h:100
PViewDataGModel::getType
int getType(int step, int ent, int ele)
Definition: PViewDataGModel.cpp:700
PViewData
Definition: PViewData.h:29
PViewOptions::type
int type
Definition: PViewOptions.h:40
PViewOptions::reference
static PViewOptions * reference()
Definition: PViewOptions.cpp:27
PViewOptions::adaptVisualizationGrid
int adaptVisualizationGrid
Definition: PViewOptions.h:78
PView::getGlobalTag
static int getGlobalTag()
Definition: PView.cpp:206
PViewDataGModel::ElementNodeData
@ ElementNodeData
Definition: PViewDataGModel.h:196
PViewData::setXYZV
virtual void setXYZV(std::vector< double > &x, std::vector< double > &y, std::vector< double > &z, std::vector< double > &v)
Definition: PViewData.h:275
PViewData::getMemoryInMb
virtual double getMemoryInMb()
Definition: PViewData.h:288
PView::sortByName
static void sortByName()
Definition: PView.cpp:341
PView::va_triangles
VertexArray * va_triangles
Definition: PView.h:156
z
const double z
Definition: GaussQuadratureQuad.cpp:56
PViewLessThanName
Definition: PView.cpp:333
PView::deleteVertexArrays
void deleteVertexArrays()
Definition: PView.cpp:210
PViewOptions
Definition: PViewOptions.h:16
PView::getOptions
PViewOptions * getOptions()
Definition: PView.h:81
PView::setIndex
void setIndex(int val)
Definition: PView.h:93
PViewDataGModel::DataType
DataType
Definition: PViewDataGModel.h:193
PViewOptions::axes
int axes
Definition: PViewOptions.h:43
PView::getViewByFileName
static PView * getViewByFileName(const std::string &fileName, int timeStep=-1, int partition=-1)
Definition: PView.cpp:362
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
PViewLessThanName::operator()
bool operator()(PView *v1, PView *v2) const
Definition: PView.cpp:335
PView::setGlobalTag
static void setGlobalTag(int tag)
Definition: PView.cpp:208
PView::addStep
void addStep(GModel *model, const std::map< int, std::vector< double > > &data, double time=0., int numComp=-1)
Definition: PView.cpp:168
PView::list
static std::vector< PView * > list
Definition: PView.h:112
PViewOptions::pointSize
double pointSize
Definition: PViewOptions.h:69
PView::setOptions
void setOptions(PViewOptions *val=nullptr)
Definition: PView.cpp:224
PViewOptions::axesLabel
std::string axesLabel[3]
Definition: PViewOptions.h:45
PView::va_lines
VertexArray * va_lines
Definition: PView.h:156
PViewData::setXY
virtual void setXY(std::vector< double > &x, std::vector< double > &y)
Definition: PViewData.h:272
SmoothData.h
PViewOptions::visible
int visible
Definition: PViewOptions.h:54
PViewDataGModel::addData
bool addData(GModel *model, const std::map< int, std::vector< double > > &data, int step, double time, int partition, int numComp)
Definition: PViewDataGModelIO.cpp:19