File: window.cpp

package info (click to toggle)
glvis 4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,780 kB
  • sloc: cpp: 35,217; ansic: 5,695; sh: 340; makefile: 301; python: 193
file content (278 lines) | stat: -rw-r--r-- 8,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-443271.
//
// This file is part of the GLVis visualization tool and library. For more
// information and source code availability see https://glvis.org.
//
// GLVis is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.

#include "window.hpp"
#include "visual.hpp"

Window &Window::operator=(Window &&w)
{
   internal = std::move(w.internal);

   data_state = std::move(w.data_state);

   window_x = w.window_x;
   window_y = w.window_y;
   window_w = w.window_w;
   window_h = w.window_h;
   window_title = w.window_title;
   headless = w.headless;
   plot_caption = std::move(w.plot_caption);
   extra_caption = std::move(w.extra_caption);

   return *this;
}

// Visualize the data in the global variables mesh, sol/grid_f, etc
bool Window::GLVisInitVis(StreamCollection input_streams)
{
   DataState::FieldType field_type = data_state.GetType();

   if (field_type <= DataState::FieldType::MIN
       || field_type >= DataState::FieldType::MAX)
   {
      return false;
   }

   static const char *window_titles[] = { "GLVis [mesh]",
                                          "GLVis [scalar data]",
                                          "GLVis [vector data]",
                                        };

   const char *win_title = (window_title == nullptr) ?
                           window_titles[(int)field_type] : window_title;

   GLWindow *new_wnd = InitVisualization(win_title, window_x, window_y, window_w,
                                         window_h, headless);
   if (new_wnd != wnd.get()) { internal.wnd.reset(new_wnd); }
   if (!wnd)
   {
      std::cerr << "Initializing the visualization failed." << std::endl;
      return false;
   }

   if (input_streams.size() > 0)
   {
#ifndef __EMSCRIPTEN__
      if (!headless)
      {
         wnd->setOnKeyDown(SDLK_SPACE, ThreadsPauseFunc);
      }
#endif
      internal.glvis_command.reset(new GLVisCommand(*this));
      SetGLVisCommand(glvis_command.get());
#ifndef __EMSCRIPTEN__
      constexpr bool multithreaded = true;
#else
      constexpr bool multithreaded = false;
#endif
      internal.comm_thread.reset(new communication_thread(std::move(input_streams),
                                                          glvis_command.get(), headless, multithreaded));
   }

   locwin = this;

   double mesh_range = -1.0;
   if (field_type == DataState::FieldType::SCALAR
       || field_type == DataState::FieldType::MESH)
   {
      if (data_state.mesh->SpaceDimension() == 2)
      {
         internal.vs.reset(new VisualizationSceneSolution(*this));

         if (field_type == DataState::FieldType::MESH)
         {
            vs->OrthogonalProjection = 1;
            vs->SetLight(false);
            vs->Zoom(1.8);
            // Use the 'bone' palette when visualizing a 2D mesh only (otherwise
            // the 'jet-like' palette is used in 2D, see vssolution.cpp).
            vs->palette.SetFallbackIndex(4);
         }
      }
      else if (data_state.mesh->SpaceDimension() == 3)
      {
         VisualizationSceneSolution3d *vss;
         vss = new VisualizationSceneSolution3d(*this);
         internal.vs.reset(vss);

         if (field_type == DataState::FieldType::MESH)
         {
            if (data_state.mesh->Dimension() == 3)
            {
               // Use the 'white' palette when visualizing a 3D volume mesh only
               vss->palette.SetFallbackIndex(11);
               vss->SetLightMatIdx(4);
            }
            else
            {
               // Use the 'bone' palette when visualizing a surface mesh only
               vss->palette.SetFallbackIndex(4);
            }
            // Otherwise, the 'vivid' palette is used in 3D see vssolution3d.cpp
            vss->ToggleDrawAxes();
            vss->ToggleDrawMesh();
         }
      }
      if (field_type == DataState::FieldType::MESH)
      {
         if (data_state.grid_f)
         {
            mesh_range = data_state.grid_f->Max() + 1.0;
         }
         else
         {
            mesh_range = data_state.sol->Max() + 1.0;
         }
      }
   }
   else if (field_type == DataState::FieldType::VECTOR)
   {
      if (data_state.mesh->SpaceDimension() == 2)
      {
         internal.vs.reset(new VisualizationSceneVector(*this));
      }
      else if (data_state.mesh->SpaceDimension() == 3)
      {
         if (data_state.grid_f)
         {
            data_state.ProjectVectorFEGridFunction();
         }
         internal.vs.reset(new VisualizationSceneVector3d(*this));
      }
   }

   if (vs)
   {
      // increase the refinement factors if visualizing a GridFunction
      if (data_state.grid_f)
      {
         vs->AutoRefine();
         vs->SetShading(VisualizationSceneScalarData::Shading::Noncomforming, true);
      }
      if (mesh_range > 0.0)
      {
         vs->SetValueRange(-mesh_range, mesh_range);
         vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::None);
      }
      if (data_state.mesh->SpaceDimension() == 2
          && field_type == DataState::FieldType::MESH)
      {
         SetVisualizationScene(vs.get(), 2, data_state.keys.c_str());
      }
      else
      {
         SetVisualizationScene(vs.get(), 3, data_state.keys.c_str());
      }
   }
   return true;
}

void Window::GLVisStartVis()
{
   RunVisualization();
   internal.vs.reset();
   internal.wnd.reset();
   if (glvis_command)
   {
      glvis_command->Terminate();
      internal.comm_thread.reset();
      internal.glvis_command.reset();
   }
   std::cout << "GLVis window closed." << std::endl;
}

void Window::SwitchComplexSolution(DataState::ComplexSolution cmplx_type)
{
   data_state.SetComplexSolution(cmplx_type);
   ResetMeshAndSolution(data_state);
}

void Window::SwitchQuadSolution(DataState::QuadSolution quad_type)
{
   data_state.SwitchQuadSolution(quad_type);
   ResetMeshAndSolution(data_state);
}

void Window::UpdateComplexPhase(double ph)
{
   data_state.cmplx_phase += ph;
   data_state.cmplx_phase -= floor(data_state.cmplx_phase);
   DataState::ComplexSolution cs = data_state.GetComplexSolution();
   // check if magnitude is viewed, which remains the same
   if (cs == DataState::ComplexSolution::Magnitude) { return; }
   data_state.SetComplexSolution(cs, false);
   // do not autoscale for animation
   auto as = vs->GetAutoscale();
   vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::None);
   ResetMeshAndSolution(data_state);
   vs->SetAutoscale(as, false);
}

bool Window::SetNewMeshAndSolution(DataState new_state)
{
   if (new_state.mesh->SpaceDimension() == data_state.mesh->SpaceDimension() &&
       new_state.GetType() == data_state.GetType() &&
       (((new_state.grid_f && data_state.grid_f) &&
         (new_state.grid_f->VectorDim() == data_state.grid_f->VectorDim()))
        ||(!new_state.grid_f && !data_state.grid_f)))
   {
      ResetMeshAndSolution(new_state);

      data_state = std::move(new_state);

      return true;
   }
   else
   {
      return false;
   }
}

void Window::ResetMeshAndSolution(DataState &ss)
{
   if (ss.mesh->SpaceDimension() == 3 &&
       ss.GetType() == DataState::FieldType::VECTOR)
   {
      ss.ProjectVectorFEGridFunction();
   }

   vs->NewMeshAndSolution(ss);
}

thread_local Window *Window::locwin = NULL;

void Window::SwitchSolution()
{
   if (locwin->data_state.cgrid_f)
   {
      SwitchComplexSolution();
   }
   else if (locwin->data_state.quad_f)
   {
      SwitchQuadSolution();
   }
}

void Window::SwitchComplexSolution()
{
   int ics = ((int)locwin->data_state.GetComplexSolution()+1)
             % ((int)DataState::ComplexSolution::MAX);
   locwin->SwitchComplexSolution((DataState::ComplexSolution)ics);
   SendExposeEvent();
}

void Window::SwitchQuadSolution()
{
   int iqs = ((int)locwin->data_state.GetQuadSolution()+1)
             % ((int)DataState::QuadSolution::MAX);
   locwin->SwitchQuadSolution((DataState::QuadSolution)iqs);
   SendExposeEvent();
}