-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathvsdata.cpp
1979 lines (1736 loc) · 52.3 KB
/
vsdata.cpp
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010-2024, 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 <cstdlib>
#include <cmath>
#include <unordered_set>
#include <iomanip>
#include <sstream>
#include <limits>
using namespace std;
#include "vsdata.hpp"
#include "aux_vis.hpp"
#include "material.hpp"
#include "palettes.hpp"
#include "gl2ps.h"
const char *strings_off_on[] = { "off", "on" };
void VisualizationSceneScalarData::FixValueRange()
{
double am = fabs(minv);
if (am < fabs(maxv)) { am = fabs(maxv); }
if (float(am) < 100*numeric_limits<float>::min()) { am = 1e-3; }
if ((maxv-minv) < 1e-5*am)
{
// Shading quality may be bad since OpenGL uses single precision. We
// should probably pre-scale the solution before feeding it to OpenGL
int old_prec = cout.precision(12);
cout << "[minv,maxv] = " << "[" << minv << "," << maxv
<< "] (maxv-minv = " << maxv-minv << ")\n --> ";
minv -= 0.49999e-5*am;
maxv += 0.50001e-5*am;
cout << "[" << minv << "," << maxv << "]" << endl;
cout.precision(old_prec);
}
}
int VisualizationSceneScalarData::GetFunctionAutoRefineFactor(GridFunction &gf)
{
Mesh *mesh = gf.FESpace()->GetMesh();
const int order = gf.FESpace()->GetMaxElementOrder();
// check for integral elements
const int dim = mesh->Dimension();
const FiniteElementCollection *fec = gf.FESpace()->FEColl();
if (fec && fec->GetMapType(dim) == FiniteElement::INTEGRAL)
{
cout << "Warning: integral elements are non-polynomial in the physical space,\n"
<< " consider increasing the refinement by the key 'o'."
<< endl;
}
return std::max(order, 1);
}
int VisualizationSceneScalarData::GetAutoRefineFactor()
{
const int dim = mesh->Dimension();
const int ne = (dim == 3)?(mesh->GetNBE()):(mesh->GetNE());
// determine the refinement based on the order of the mesh and grid function
int order_ref = GetFunctionAutoRefineFactor();
// mesh
const FiniteElementSpace *nfes = mesh->GetNodalFESpace();
if (nfes)
{
const int order = nfes->GetMaxElementOrder();
order_ref = std::max(order_ref, order);
}
// limit the total number of vertices
int auto_ref_surf_vert = ne * (order_ref+1) * (order_ref+1);
auto_ref_surf_vert = std::min(std::max(auto_ref_surf_vert,
auto_ref_min_surf_vert), auto_ref_max_surf_vert);
// approach the given number of vertices
int ref = 1;
while (ref < auto_ref_max && ne*(ref+2)*(ref+2) <= auto_ref_surf_vert)
{ ref++; }
if (ref < order_ref)
{
cout << "Warning: the automatic refinement does not resolve the data fully,\n"
<< " consider increasing the refinement by the key 'o'."
<< endl;
}
return ref;
}
void VisualizationSceneScalarData::DoAutoscale(bool prepare)
{
if (autoscale == 1)
{
FindNewBoxAndValueRange(prepare);
}
else if (autoscale == 2)
{
FindNewValueRange(prepare);
}
else if (autoscale == 3)
{
FindMeshBox(prepare);
}
}
void VisualizationSceneScalarData::DoAutoscaleValue(bool prepare)
{
if (autoscale == 1 || autoscale == 3)
{
FindNewBoxAndValueRange(prepare);
}
else
{
FindNewValueRange(prepare);
}
}
template<typename T>
static std::array<float, 3> ToVec3(const T* vec)
{
return { (float) vec[0], (float) vec[1], (float) vec[2] };
}
void VisualizationSceneScalarData::Cone(gl3::GlDrawable& buf,
glm::mat4 xfrm,
double cval)
{
const int n = 8;
const double step = 2*M_PI/n;
const double nz = (1.0/4.0);
double point = step;
glm::mat3 normXfrm = glm::inverseTranspose(glm::mat3(xfrm));
glm::vec3 start1vtx = glm::vec3(xfrm * glm::vec4(0, 0, 0, 1));
glm::vec3 start1norm = glm::vec3(normXfrm * glm::vec3(0, 0, 1));
glm::vec3 start2vtx = glm::vec3(xfrm * glm::vec4(1, 0, -4, 1));
glm::vec3 start2norm = glm::vec3(normXfrm * glm::vec3(1, 0, nz));
int indices[n*3];
for (int i = 0; i < n; i++)
{
indices[3*i] = 0;
indices[3*i+1] = i+1;
indices[3*i+2] = i+2;
}
if (cval == HUGE_VAL)
{
gl3::VertexNorm verts[n+2];
verts[0].coord = ToVec3(glm::value_ptr(start1vtx));
verts[0].norm = ToVec3(glm::value_ptr(start1norm));
verts[1].coord = ToVec3(glm::value_ptr(start2vtx));
verts[1].norm = ToVec3(glm::value_ptr(start2norm));
for (int i = 2; i <= n+1; i++)
{
glm::vec3 baseVtx = glm::vec3(xfrm * glm::vec4(cos(point), sin(point), -4, 1));
glm::vec3 baseNorm = glm::vec3(normXfrm * glm::vec3(cos(point), sin(point),
nz));
verts[i].coord = ToVec3(glm::value_ptr(baseVtx));
verts[i].norm = ToVec3(glm::value_ptr(baseNorm));
point += step;
}
buf.addTriangleIndexed(n+2, verts, n*3, indices);
}
else
{
float colortex = palette.GetColorCoord(cval, minv, maxv);
gl3::VertexNormTex verts[n+2];
verts[0].coord = ToVec3(glm::value_ptr(start1vtx));
verts[0].norm = ToVec3(glm::value_ptr(start1norm));
verts[0].texCoord = colortex;
verts[1].coord = ToVec3(glm::value_ptr(start2vtx));
verts[1].norm = ToVec3(glm::value_ptr(start2norm));
verts[1].texCoord = colortex;
for (int i = 2; i <= n+1; i++)
{
glm::vec3 baseVtx = glm::vec3(xfrm * glm::vec4(cos(point), sin(point), -4, 1));
glm::vec3 baseNorm = glm::vec3(normXfrm * glm::vec3(cos(point), sin(point),
nz));
verts[i].coord = ToVec3(glm::value_ptr(baseVtx));
verts[i].norm = ToVec3(glm::value_ptr(baseNorm));
verts[i].texCoord = colortex;
point += step;
}
buf.addTriangleIndexed(n+2, verts, n*3, indices);
}
}
// Draw an arrow starting at point (px, py, pz) with orientation (vx, vy, vz)
// and length "length".
void VisualizationSceneScalarData::Arrow3(gl3::GlDrawable& buf,
double px, double py, double pz,
double vx, double vy, double vz,
double length,
double cone_scale,
double cval)
{
double xc = 0.5*(bb.x[0]+bb.x[1]);
double yc = 0.5*(bb.y[0]+bb.y[1]);
double zc = 0.5*(bb.z[0]+bb.z[1]);
glm::mat4 xfrm(1.0);
xfrm = glm::translate(xfrm, glm::vec3(xc, yc, zc));
xfrm = glm::scale(xfrm, glm::vec3(1.0/xscale, 1.0/yscale, 1.0/zscale));
double rlen = length/sqrt(vx*vx+vy*vy+vz*vz);
px = (px-xc)*xscale;
py = (py-yc)*yscale;
pz = (pz-zc)*zscale;
vx *= rlen*xscale;
vy *= rlen*yscale;
vz *= rlen*zscale;
if (arrow_scaling_type == 0)
{
length = sqrt(vx*vx+vy*vy+vz*vz);
}
xfrm = glm::translate(xfrm, glm::vec3(px, py, pz));
double rhos = sqrt (vx*vx+vy*vy+vz*vz);
float phi = acos(vz/rhos);
float theta;
theta = atan2 (vy, vx);
xfrm = glm::rotate(xfrm, theta, glm::vec3(0, 0, 1));
xfrm = glm::rotate(xfrm, phi, glm::vec3(0, 1, 0));
xfrm = glm::scale(xfrm, glm::vec3(length));
if (arrow_type == 1)
{
xfrm = glm::translate(xfrm, glm::vec3(0, 0, -0.5));
}
glm::vec4 pt1 = xfrm * glm::vec4(0, 0, 0, 1);
glm::vec4 pt2 = xfrm * glm::vec4(0, 0, 1, 1);
if (cval == HUGE_VAL)
{
buf.addLine<gl3::Vertex>(
gl3::Vertex{ToVec3(glm::value_ptr(pt1))},
gl3::Vertex{ToVec3(glm::value_ptr(pt2))});
}
else
{
double colortex = palette.GetColorCoord(cval, minv, maxv);
buf.addLine<gl3::VertexTex>(
gl3::VertexTex{ToVec3(glm::value_ptr(pt1)), (float)colortex},
gl3::VertexTex{ToVec3(glm::value_ptr(pt2)), (float)colortex});
}
xfrm = glm::translate(xfrm, glm::vec3(0, 0, 1));
xfrm = glm::scale(xfrm, glm::vec3(cone_scale));
if (cone_scale > 0.0)
{
Cone(buf, xfrm, cval);
}
}
void VisualizationSceneScalarData::Arrow2(gl3::GlDrawable& buf,
double px, double py, double pz,
double vx, double vy, double vz,
double length,
double cone_scale,
double cval)
{
glm::mat4 xfrm(1.0);
xfrm = glm::translate(xfrm, glm::vec3(px, py, pz));
double rhos = sqrt (vx*vx+vy*vy+vz*vz);
float phi = acos(vz/rhos);
float theta;
theta = atan2 (vy, vx);
xfrm = glm::rotate(xfrm, theta, glm::vec3(0, 0, 1));
xfrm = glm::rotate(xfrm, phi, glm::vec3(0, 1, 0));
xfrm = glm::scale(xfrm, glm::vec3(length));
glm::vec4 pt1 = xfrm * glm::vec4(0, 0, 0, 1);
glm::vec4 pt2 = xfrm * glm::vec4(0, 0, 1, 1);
if (cval == HUGE_VAL)
{
buf.addLine<gl3::Vertex>(
gl3::Vertex{ToVec3(glm::value_ptr(pt1))},
gl3::Vertex{ToVec3(glm::value_ptr(pt2))});
}
else
{
double colortex = palette.GetColorCoord(cval, minv, maxv);
buf.addLine<gl3::VertexTex>(
gl3::VertexTex{ToVec3(glm::value_ptr(pt1)), (float)colortex},
gl3::VertexTex{ToVec3(glm::value_ptr(pt2)), (float)colortex});
}
xfrm = glm::translate(xfrm, glm::vec3(0, 0, 1));
xfrm = glm::scale(xfrm, glm::vec3(cone_scale));
Cone(buf, xfrm, cval);
}
void VisualizationSceneScalarData::Arrow(gl3::GlDrawable& buf,
double px, double py, double pz,
double vx, double vy, double vz,
double length,
double cone_scale,
double cval)
{
double rhos = sqrt (vx*vx+vy*vy+vz*vz);
if (rhos == 0.0)
{
return;
}
double phi = acos(vz/rhos), theta = atan2(vy, vx);
constexpr int n = 8;
const double step = 2*M_PI/n, nz = (1.0/4.0);
double point = step, cone[n+4][3], normal[n+2][3];
cone[0][0] = 0; cone[0][1] = 0; cone[0][2] = 1;
cone[1][0] = cone_scale; cone[1][1] = 0; cone[1][2] = -4*cone_scale + 1;
normal[0][0] = 0.0/cone_scale;
normal[0][1] = 0.0/cone_scale;
normal[0][2] = 1.0/cone_scale;
normal[1][0] = 1.0/cone_scale;
normal[1][1] = 0.0/cone_scale;
normal[1][2] = nz/cone_scale;
for (int i=2; i<n+1; i++)
{
normal[i][0] = cos(point)/cone_scale;
normal[i][1] = sin(point)/cone_scale;
normal[i][2] = nz/cone_scale;
cone[i][0] = cos(point)*cone_scale;
cone[i][1] = sin(point)*cone_scale;
cone[i][2] = -4*cone_scale + 1;
point += step;
}
cone[n+1][0] = cone_scale; cone[n+1][1] = 0; cone[n+1][2] =-4*cone_scale + 1;
normal[n+1][0] = 1.0/cone_scale;
normal[n+1][1] = 0.0/cone_scale;
normal[n+1][2] = nz/cone_scale;
cone[n+2][0] = 0; cone[n+2][1] = 0; cone[n+2][2] = 0;
cone[n+3][0] = 0; cone[n+3][1] = 0; cone[n+3][2] = 1;
if (arrow_scaling_type == 0)
{
length = rhos;
}
// double xc = 0.5*(x[0]+x[1]), yc = 0.5*(y[0]+y[1]), zc = 0.5*(z[0]+z[1]);
double coord[3];
// double rlen = length/rhos;
// px = (px-xc)*xscale; py = (py-yc)*yscale; pz = (pz-zc)*zscale;
// vx *= rlen*xscale; vy *= rlen*yscale; vz *= rlen*zscale;
if (arrow_type == 1)
for (int i=0; i<n+4; i++)
{
cone[i][2] -= 0.5;
}
double M[3][3]= {{cos(theta)*cos(phi), -sin(theta), cos(theta)*sin(phi)},
{sin(theta)*cos(phi), cos(theta), sin(theta)*sin(phi)},
{ -sin(phi), 0., cos(phi)}
};
double v[3] = { M[0][2]/xscale, M[1][2]/yscale, M[2][2]/zscale };
length /= sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
for (int i=0; i<n+4; i++)
{
for (int j=0; j<3; j++)
{
coord[j] = cone[i][j] * length;
}
for (int k=0; k<3; k++)
{
cone[i][k] = 0.;
for (int j=0; j<3; j++)
{
cone[i][k] += M[k][j] * coord[j];
}
}
// cone[i][0] = (cone[i][0] + px)/xscale + xc;
// cone[i][1] = (cone[i][1] + py)/yscale + yc;
// cone[i][2] = (cone[i][2] + pz)/zscale + zc;
cone[i][0] = cone[i][0]/xscale + px;
cone[i][1] = cone[i][1]/yscale + py;
cone[i][2] = cone[i][2]/zscale + pz;
}
for (int i=0; i<=n+1; i++)
{
for (int j=0; j<3; j++)
{
coord[j] = normal[i][j];
}
for (int k=0; k<3; k++)
{
normal[i][k] = 0.;
for (int j=0; j<3; j++)
{
normal[i][k] += M[k][j] * coord[j];
}
}
normal[i][0] *= xscale;
normal[i][1] *= yscale;
normal[i][2] *= zscale;
}
int indices[n*3];
for (int i = 0; i < n; i++)
{
indices[3*i] = 0;
indices[3*i+1] = i+1;
indices[3*i+2] = i+2;
}
if (cval == HUGE_VAL)
{
gl3::VertexNorm verts[n+2];
for (int i = 0; i <= n+1; i++)
{
verts[i].coord = ToVec3(cone[i]);
verts[i].norm = ToVec3(normal[i]);
}
buf.addTriangleIndexed(n+2, verts, n*3, indices);
buf.addLine<gl3::Vertex>(
gl3::Vertex{ToVec3(cone[n+2])},
gl3::Vertex{ToVec3(cone[n+3])});
}
else
{
float colortex = palette.GetColorCoord(cval, minv, maxv);
gl3::VertexNormTex verts[n+2];
for (int i = 0; i <= n+1; i++)
{
verts[i].coord = ToVec3(cone[i]);
verts[i].norm = ToVec3(normal[i]);
verts[i].texCoord = colortex;
}
buf.addTriangleIndexed(n+2, verts, n*3, indices);
buf.addLine<gl3::VertexTex>(
gl3::VertexTex{ToVec3(cone[n+2]), colortex},
gl3::VertexTex{ToVec3(cone[n+3]), colortex});
}
}
void VisualizationSceneScalarData::SetColorbarNumberFormat(int precision,
char format,
bool showsign)
{
colorbar_formatter = NumberFormatter(precision, format, showsign);
// The first two arguments are required but I don't think they are used?
PrepareColorBar(0,0);
}
void VisualizationSceneScalarData::SetColorbarNumberFormat(string formatting)
{
colorbar_formatter = NumberFormatter(formatting);
// The first two arguments are required but I don't think they are used?
PrepareColorBar(0,0);
}
void VisualizationSceneScalarData::PrepareColorBar (double minval,
double maxval,
Array<double> *mesh_level,
Array<double> *lsurf_levels)
{
int i;
float miny;
float maxy;
float minx;
float maxx;
float posz = -4.0;
if (OrthogonalProjection)
{
miny = -.65;
maxy = .65;
minx = 0.73;
maxx = 0.80;
}
else
{
miny = -1.;
maxy = 1.;
minx = 1.2;
maxx = 1.3;
}
color_bar.clear();
color_bar.addQuad<gl3::VertexTex>(
{{minx, miny, posz}, 0.f},
{{maxx, miny, posz}, 0.f},
{{maxx, maxy, posz}, 1.f},
{{minx, maxy, posz}, 1.f}
);
static const int border = 2;
if (border == 1)
{
color_bar.addLines<gl3::Vertex>(
{
{minx, miny, posz}, {maxx, miny, posz},
{maxx, miny, posz}, {maxx, maxy, posz},
{maxx, maxy, posz}, {minx, maxy, posz},
{minx, maxy, posz}, {minx, miny, posz}
});
}
else if (border == 2)
{
color_bar.addLine<gl3::Vertex>({minx, miny, posz}, {minx, maxy, posz});
color_bar.addLine<gl3::Vertex>({maxx, miny, posz}, {maxx, maxy, posz});
}
if (lsurf_levels)
{
for (i = 0; i < lsurf_levels->Size(); i++)
{
float Y = miny + (maxy - miny) * LogUVal((*lsurf_levels)[i]);
color_bar.addLine<gl3::Vertex>({minx, Y, posz}, {maxx, Y, posz});
}
}
if (mesh_level)
{
for (i = 0; i < mesh_level->Size(); i++)
{
float Y = miny + (maxy - miny) * LogUVal((*mesh_level)[i]);
color_bar.addLine<gl3::Vertex>({minx, Y, posz}, {maxx, Y, posz});
}
}
const double text_x = maxx + 0.4*(maxx-minx);
double val;
double Y;
if (!mesh_level)
{
for (i = 0; i <= 4; i++)
{
Y = miny + i * (maxy-miny) / 4;
val = ULogVal(i / 4.0);
color_bar.addText(text_x,Y,posz,colorbar_formatter(val));
}
}
else
{
for (i = 0; i < mesh_level->Size(); i++)
{
val = (*mesh_level)[i];
Y = miny + (maxy - miny) * LogUVal(val);
color_bar.addText(text_x,Y,posz,colorbar_formatter(val));
}
}
if (lsurf_levels)
{
for (i = 0; i < lsurf_levels->Size(); i++)
{
val = (*lsurf_levels)[i];
Y = miny + (maxy - miny) * LogUVal(val);
color_bar.addText(text_x,Y,posz,colorbar_formatter(val));
}
}
updated_bufs.emplace_back(&color_bar);
}
// Draw a centered caption at the top (visible with the colorbar)
void VisualizationSceneScalarData::PrepareCaption()
{
bool empty = plot_caption.empty();
colorbar = (colorbar ? empty+1 : !empty);
string caption(plot_caption);
if (!extra_caption.empty())
{
caption += " (" + extra_caption + ")";
}
caption_buf.clear();
caption_buf.addText(0, 0, 0, caption);
updated_bufs.emplace_back(&caption_buf);
GetFont()->getObjectSize(caption, caption_w, caption_h);
}
thread_local VisualizationSceneScalarData * vsdata;
extern thread_local VisualizationScene * locscene;
void KeycPressed(GLenum state)
{
if (state & KMOD_ALT)
{
cout << "Setting colorbar number formatting..." << endl;
int default_precision = 4;
char default_format = 'd';
bool default_showsign = false;
int precision = prompt<int>("Enter precision (4): ",
&default_precision, [](int p) { return p>=0; });
char format =
prompt<char>("Enter format [(d)efault, (f)ixed, (s)cientific] (d): ",
&default_format, [](char c) { return c=='d' || c=='f' || c=='s'; });
bool showsign = prompt<bool>("Show sign? [(1)true, (0)false] (0): ",
&default_showsign);
vsdata->SetColorbarNumberFormat(precision, format, showsign);
SendExposeEvent();
}
else
{
vsdata->ToggleDrawColorbar();
SendExposeEvent();
}
}
void KeyCPressed()
{
cout << "Enter new caption: " << flush;
std::getline(cin, plot_caption);
vsdata->PrepareCaption(); // turn on or off the caption
SendExposeEvent();
}
void KeySPressed()
{
vsdata -> ToggleScaling();
SendExposeEvent();
}
void KeyaPressed()
{
vsdata -> ToggleDrawAxes();
SendExposeEvent();
}
void Key_Mod_a_Pressed(GLenum state)
{
if (state & KMOD_CTRL)
{
static const char *autoscale_modes[] = { "off", "on", "value", "mesh" };
int autoscale = vsdata->GetAutoscale();
autoscale = (autoscale + 1)%4;
cout << "Autoscale: " << flush;
vsdata->SetAutoscale(autoscale);
cout << autoscale_modes[autoscale] << endl;
SendExposeEvent();
}
else if (state & KMOD_ALT)
{
cout << "Setting axes number formatting..." << endl;
int default_precision = 4;
char default_format = 'd';
bool default_showsign = false;
int precision = prompt<int>("Enter precision (4): ",
&default_precision, [](int p) { return p>=0; });
char format =
prompt<char>("Enter format [(d)efault, (f)ixed, (s)cientific] (d): ",
&default_format, [](char c) { return c=='d' || c=='f' || c=='s'; });
bool showsign = prompt<bool>("Show sign? [(1)true, (0)false] (0): ",
&default_showsign);
vsdata->SetAxisNumberFormat(precision, format, showsign);
SendExposeEvent();
}
else
{
vsdata->ToggleDrawAxes();
SendExposeEvent();
}
}
void KeyHPressed()
{
cout << vsdata->GetHelpString() << flush;
}
void KeylPressed()
{
vsdata -> ToggleLight();
SendExposeEvent();
}
void KeyLPressed()
{
vsdata->ToggleLogscale(true);
SendExposeEvent();
}
void KeyrPressed()
{
locscene -> spinning = 0;
RemoveIdleFunc(MainLoop);
vsdata -> CenterObject();
locscene -> ViewAngle = 45.0;
locscene -> ViewScale = 1.0;
locscene -> ViewCenterX = 0.0;
locscene -> ViewCenterY = 0.0;
locscene->cam.Reset();
vsdata -> key_r_state = 0;
SendExposeEvent();
}
void KeyRPressed()
{
locscene->spinning = 0;
RemoveIdleFunc(MainLoop);
vsdata->Toggle2DView();
SendExposeEvent();
}
void KeypPressed(GLenum state)
{
if (state & KMOD_CTRL)
{
KeyCtrlP();
}
else
{
locscene->palette.NextIndex();
SendExposeEvent();
}
}
void KeyPPressed()
{
locscene->palette.PrevIndex();
SendExposeEvent();
}
static void KeyF5Pressed()
{
int n;
double min, max;
cout << "Enter min : " << flush;
cin >> min;
cout << "Enter max : " << flush;
cin >> max;
cout << "Enter n : " << flush;
cin >> n;
vsdata -> SetLevelLines (min, max, n, 0);
vsdata -> UpdateLevelLines();
SendExposeEvent();
}
void KeyF6Pressed()
{
int RepeatPaletteTimes = vsdata->palette.GetRepeatTimes();
cout << "Palette is repeated " << RepeatPaletteTimes << " times.\n"
<< "(Negative value means the palette is flipped.)\n"
<< "Enter new value: " << flush;
cin >> RepeatPaletteTimes;
if (RepeatPaletteTimes == 0)
{
RepeatPaletteTimes = 1;
}
cout << "Palette will be repeated " << RepeatPaletteTimes
<< " times now.\n\n";
vsdata->palette.SetRepeatTimes(RepeatPaletteTimes);
int pal = vsdata->palette.ChoosePalette();
int colors_used = vsdata->palette.GetNumColors(pal);
int palette_size = vsdata->palette.GetSize(pal);
cout << "\nPalette is using " << colors_used << " colors.\n"
<< "Enter new value (0 = use original " << palette_size
<< " colors): " << flush;
cin >> colors_used;
if (colors_used == 1) { colors_used = 0; }
vsdata->palette.SetNumColors(colors_used);
vsdata->palette.GenerateTextures();
vsdata->palette.SetIndex(pal);
colors_used = vsdata->palette.GetNumColors();
cout << "Palette will be using " << colors_used << " colors now.\n";
vsdata->EventUpdateColors();
SendExposeEvent();
}
void KeyF7Pressed(GLenum state)
{
if (state & KMOD_SHIFT)
{
cout << "Current bounding box:\n"
<< " min: (" << vsdata->bb.x[0] << ',' << vsdata->bb.y[0] << ','
<< vsdata->bb.z[0] << ")\n"
<< " max: (" << vsdata->bb.x[1] << ',' << vsdata->bb.y[1] << ','
<< vsdata->bb.z[1] << ")\n"
<< "Enter new bounding box:\n"
<< "x_min = " << flush;
cin >> vsdata->bb.x[0];
cout << "y_min = " << flush;
cin >> vsdata->bb.y[0];
cout << "z_min = " << flush;
cin >> vsdata->bb.z[0];
cout << "x_max = " << flush;
cin >> vsdata->bb.x[1];
cout << "y_max = " << flush;
cin >> vsdata->bb.y[1];
cout << "z_max = " << flush;
cin >> vsdata->bb.z[1];
cout << "New bounding box:\n"
<< " min: (" << vsdata->bb.x[0] << ',' << vsdata->bb.y[0] << ','
<< vsdata->bb.z[0] << ")\n"
<< " max: (" << vsdata->bb.x[1] << ',' << vsdata->bb.y[1] << ','
<< vsdata->bb.z[1] << ")\n" << flush;
vsdata->UpdateBoundingBox();
SendExposeEvent();
}
else
{
cout << "[minv,maxv] = [" << vsdata->GetMinV() << "," << vsdata->GetMaxV()
<< "] maxv-minv = " << vsdata->GetMaxV()-vsdata->GetMinV() << "\n"
<< "New value for minv: " << flush;
cin >> vsdata->GetMinV();
cout << "New value for maxv: " << flush;
cin >> vsdata->GetMaxV();
vsdata->UpdateValueRange(true);
SendExposeEvent();
}
}
void KeyBackslashPressed()
{
float x, y, z, w;
cout << "Enter light source position\n(0,0,1,w) - from camera\n"
"(0,1,0,w) - from above\n(1,0,0,w) - from the right\n"
"w = 0/1 defines directional/spot light\n";
cout << "x = " << flush;
cin >> x;
cout << "y = " << flush;
cin >> y;
cout << "z = " << flush;
cin >> z;
cout << "w = " << flush;
cin >> w;
vsdata->SetLight0CustomPos({x, y, z, w});
SendExposeEvent();
}
void KeyTPressed()
{
int ml;
ml = (vsdata->GetLightMatIdx() + 1) % 5;
vsdata->SetLightMatIdx(ml);
SendExposeEvent();
cout << "New material/light : " << ml << endl;
}
void KeygPressed()
{
vsdata->ToggleBackground();
vsdata->PrepareAxes();
vsdata->EventUpdateBackground();
SendExposeEvent();
}
void KeyGPressed()
{
vsdata->glTF_Export();
}
void KeyF1Pressed()
{
vsdata->PrintState();
}
void KeyF2Pressed()
{
vsdata -> EventUpdateColors();
vsdata -> PrepareLines();
// vsdata->CPPrepare();
SendExposeEvent();
}
void KeykPressed()
{
locscene->matAlpha -= 0.05;
if (locscene->matAlpha < 0.0)
{
locscene->matAlpha = 0.0;
}
locscene->GenerateAlphaTexture();
SendExposeEvent();
}
void KeyKPressed()
{
locscene->matAlpha += 0.05;
if (locscene->matAlpha > 1.0)
{
locscene->matAlpha = 1.0;
}
locscene->GenerateAlphaTexture();
SendExposeEvent();
}
void KeyAPressed()
{
bool curr_aa = GetAppWindow()->getRenderer().getAntialiasing();
GetAppWindow()->getRenderer().setAntialiasing(!curr_aa);
cout << "Multisampling/Antialiasing: "
<< strings_off_on[!curr_aa ? 1 : 0] << endl;
// vsdata -> EventUpdateColors();
SendExposeEvent();
}
void KeyCommaPressed()
{
locscene->matAlphaCenter -= 0.25;
// vsdata -> EventUpdateColors();
locscene->GenerateAlphaTexture();
SendExposeEvent();
#ifdef GLVIS_DEBUG
cout << "MatAlphaCenter = " << locscene->matAlphaCenter << endl;
#endif
}
void KeyLessPressed()
{
locscene->matAlphaCenter += 0.25;
// vsdata -> EventUpdateColors();
locscene->GenerateAlphaTexture();
SendExposeEvent();
#ifdef GLVIS_DEBUG
cout << "MatAlphaCenter = " << locscene->matAlphaCenter << endl;
#endif
}
void KeyGravePressed()
{
vsdata->ToggleRuler();
SendExposeEvent();
}
void KeyTildePressed()
{
vsdata->RulerPosition();
SendExposeEvent();
}
void KeyToggleTexture()
{
vsdata->ToggleTexture();
SendExposeEvent();
}
void VisualizationSceneScalarData::PrintLogscale(bool warn)
{
if (warn)
{
cout << "The range [" << minv << ',' << maxv
<< "] is not appropriate for logarithmic scale!" << endl;
}
cout << "Logarithmic scale: " << strings_off_on[logscale ? 1 : 0]
<< endl;
}
void VisualizationSceneScalarData::ToggleLogscale(bool print)
{
if (logscale || LogscaleRange())
{
logscale = !logscale;