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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
|
// 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 "script_controller.hpp"
#include "file_reader.hpp"
#include "coll_reader.hpp"
#include "stream_reader.hpp"
#include "visual.hpp"
#include "sdl/sdl.hpp"
#include "egl/egl.hpp"
#include <array>
#include <algorithm>
#include <thread>
using namespace std;
using namespace mfem;
enum class Command
{
Mesh,
Solution,
ParSolution,
Quadrature,
ParQuadrature,
DataCollMesh,
DataCollField,
DataCollQuad,
DataCollCycle,
DataCollProto,
Screenshot,
Viewcenter,
Perspective,
Light,
View,
Zoom,
Shading,
Subdivisions,
Valuerange,
Autoscale,
Levellines,
AxisNumberFormat,
ColorbarNumberFormat,
Window,
Keys,
Palette,
PaletteFile,
PaletteName,
PaletteRepeat,
ToggleAttributes,
Rotmat,
Camera,
Scale,
Translate,
PlotCaption,
Headless,
//----------
Max
};
class ScriptCommands
{
struct CmdItem
{
const char *keyword;
const char *params;
const char *desc;
bool operator==(const string &key) const { return key == keyword; }
};
array<CmdItem,(size_t)Command::Max> commands;
public:
ScriptCommands();
decltype(commands)::const_iterator begin() const { return commands.begin(); }
decltype(commands)::const_iterator end() const { return commands.end(); }
CmdItem& operator[](Command cmd) { return commands[(size_t)cmd]; }
const CmdItem& operator[](Command cmd) const { return commands[(size_t)cmd]; }
};
static const ScriptCommands commands;
ScriptCommands::ScriptCommands()
{
(*this)[Command::Mesh] = {"mesh", "<file>", "Visualize the mesh."};
(*this)[Command::Solution] = {"solution", "<mesh> <solution>", "Visualize the solution."};
(*this)[Command::ParSolution] = {"psolution", "<np> <mesh prefix> <keep attributes> <solution prefix>", "Visualize the distributed solution."};
(*this)[Command::Quadrature] = {"quadrature", "<mesh> <quadrature>", "Visualize the quadrature."};
(*this)[Command::ParQuadrature] = {"pquadrature", "<np> <mesh prefix> <keep attributes> <quadrature prefix>", "Visualize the distributed quadrature."};
(*this)[Command::DataCollMesh] = {"data_coll_mesh", "<type> <data coll>", "Visualize the mesh from data collection."};
(*this)[Command::DataCollField] = {"data_coll_field", "<type> <data coll> <field>", "Visualize the field from data collection."};
(*this)[Command::DataCollQuad] = {"data_coll_quad", "<type> <data coll> <quad>", "Visualize the Q-field from data collection."};
(*this)[Command::DataCollCycle] = {"data_coll_cycle", "<cycle>", "Preset the cycle of the data collection."};
(*this)[Command::DataCollProto] = {"data_coll_protocol", "<protocol>", "Preset the protocol of the data collection."};
(*this)[Command::Screenshot] = {"screenshot", "<file>", "Take a screenshot, saving it to the file."};
(*this)[Command::Viewcenter] = {"viewcenter", "<x> <y>", "Change the viewcenter."};
(*this)[Command::Perspective] = {"perspective", "<on/off>", "Turn on or off perspective projection."};
(*this)[Command::Light] = {"light", "<on/off>", "Turn on or off light."};
(*this)[Command::View] = {"view", "<theta> <phi>", "Change the solid angle of view."};
(*this)[Command::Zoom] = {"zoom", "<zoom>", "Change the zoom factor."};
(*this)[Command::Shading] = {"shading", "<flat/smooth/cool>", "Change the shading algorithm."};
(*this)[Command::Subdivisions] = {"subdivisions", "<times> <dummy>", "Change the refinement level."};
(*this)[Command::Valuerange] = {"valuerange", "<min> <max>", "Change the value range."};
(*this)[Command::Autoscale] = {"autoscale", "<off/on/value/mesh>", "Change the autoscale algorithm."};
(*this)[Command::Levellines] = {"levellines", "<min> <max> <num>", "Set the level lines."};
(*this)[Command::AxisNumberFormat] = {"axis_numberformat", "'<format>'", "Set the axis number format."};
(*this)[Command::ColorbarNumberFormat] = {"colorbar_numberformat", "'<format>'", "Set the colorbar number format."};
(*this)[Command::Window] = {"window", "<x> <y> <w> <h>", "Set the position and size of the window."};
(*this)[Command::Keys] = {"keys", "<keys>", "Send the control key sequence."};
(*this)[Command::Palette] = {"palette", "<index>", "Set the palette index."};
(*this)[Command::PaletteFile] = {"palette_file", "<filename>", "Load in a palette file."};
(*this)[Command::PaletteName] = {"palette_name", "<palette_name>", "Use palette with given name."};
(*this)[Command::PaletteRepeat] = {"palette_repeat", "<times>", "Set the repetition of the palette."};
(*this)[Command::ToggleAttributes] = {"toggle_attributes", "<1/0> [[<1/0>] ...];", "Toggle visibility of the attributes."};
(*this)[Command::Rotmat] = {"rotmat", "<[0,0]> <[1,0]> ... <[3,3]>", "Set the rotation matrix."};
(*this)[Command::Camera] = {"camera", "<cam[0]> ... <cam[2]> <dir[0]> ... <dir[2]> <up[0]> ... <up[2]>", "Set the camera position, direction and upward vector."};
(*this)[Command::Scale] = {"scale", "<scale>", "Set the scaling factor."};
(*this)[Command::Translate] = {"translate", "<x> <y> <z>", "Set the translation coordinates."};
(*this)[Command::PlotCaption] = {"plot_caption", "'<caption>'", "Set the plot caption."};
(*this)[Command::Headless] = {"headless", "", "Change the session to headless."};
}
int ScriptController::ScriptReadSolution(istream &scr, DataState &state)
{
int err_read;
string mword,sword;
cout << "Script: solution: " << flush;
// read the mesh
scr >> ws >> mword; // mesh filename (can't contain spaces)
cout << "mesh: " << mword << "; " << flush;
named_ifgzstream imesh(mword.c_str());
if (!imesh)
{
cout << "Can not open mesh file: " << mword << endl;
return 1;
}
state.SetMesh(new Mesh(imesh, 1, 0, state.fix_elem_orient));
// read the solution (GridFunction)
scr >> ws >> sword;
cout << "solution: " << sword << endl;
FileReader reader(state);
err_read = reader.ReadSerial(FileReader::FileType::GRID_FUNC, mword.c_str(),
sword.c_str());
return err_read;
}
int ScriptController::ScriptReadQuadrature(istream &scr, DataState &state)
{
int err_read;
string mword,sword;
cout << "Script: quadrature: " << flush;
// read the mesh
scr >> ws >> mword; // mesh filename (can't contain spaces)
cout << "mesh: " << mword << "; " << flush;
named_ifgzstream imesh(mword.c_str());
if (!imesh)
{
cout << "Can not open mesh file: " << mword << endl;
return 1;
}
state.SetMesh(new Mesh(imesh, 1, 0, state.fix_elem_orient));
// read the quadrature (QuadratureFunction)
scr >> ws >> sword;
cout << "quadrature: " << sword << endl;
FileReader reader(state);
err_read = reader.ReadSerial(FileReader::FileType::QUAD_FUNC, mword.c_str(),
sword.c_str());
return err_read;
}
int ScriptController::ScriptReadParSolution(istream &scr, DataState &state)
{
int np, scr_keep_attr, err_read;
string mesh_prefix, sol_prefix;
cout << "Script: psolution: " << flush;
// read number of processors
scr >> np;
cout << "# processors: " << np << "; " << flush;
// read the mesh prefix
scr >> ws >> mesh_prefix; // mesh prefix (can't contain spaces)
cout << "mesh prefix: " << mesh_prefix << "; " << flush;
scr >> ws >> scr_keep_attr;
if (scr_keep_attr)
{
cout << "(real attributes); " << flush;
}
else
{
cout << "(processor attributes); " << flush;
}
state.keep_attr = scr_keep_attr;
// read the solution prefix
scr >> ws >> sol_prefix;
cout << "solution prefix: " << sol_prefix << endl;
FileReader reader(state);
err_read = reader.ReadParallel(np, FileReader::FileType::GRID_FUNC,
mesh_prefix.c_str(), sol_prefix.c_str());
return err_read;
}
int ScriptController::ScriptReadParQuadrature(istream &scr, DataState &state)
{
int np, scr_keep_attr, err_read;
string mesh_prefix, quad_prefix;
cout << "Script: pquadrature: " << flush;
// read number of processors
scr >> np;
cout << "# processors: " << np << "; " << flush;
// read the mesh prefix
scr >> ws >> mesh_prefix; // mesh prefix (can't contain spaces)
cout << "mesh prefix: " << mesh_prefix << "; " << flush;
scr >> ws >> scr_keep_attr;
if (scr_keep_attr)
{
cout << "(real attributes); " << flush;
}
else
{
cout << "(processor attributes); " << flush;
}
state.keep_attr = scr_keep_attr;
// read the quadrature prefix
scr >> ws >> quad_prefix;
cout << "quadrature prefix: " << quad_prefix << endl;
FileReader reader(state);
err_read = reader.ReadParallel(np, FileReader::FileType::QUAD_FUNC,
mesh_prefix.c_str(), quad_prefix.c_str());
return err_read;
}
int ScriptController::ScriptReadDisplMesh(istream &scr, DataState &state)
{
DataState meshstate;
string word;
cout << "Script: mesh: " << flush;
scr >> ws >> word;
{
named_ifgzstream imesh(word.c_str());
if (!imesh)
{
cout << "Can not open mesh file: " << word << endl;
return 1;
}
cout << word << endl;
meshstate.SetMesh(new Mesh(imesh, 1, 0, state.fix_elem_orient));
}
meshstate.ExtrudeMeshAndSolution();
Mesh* const m = meshstate.mesh.get();
if (init_nodes == NULL)
{
init_nodes.reset(new Vector);
meshstate.mesh->GetNodes(*init_nodes);
state.SetMesh(NULL);
state.SetGridFunction(NULL);
}
else
{
FiniteElementCollection *vfec = NULL;
FiniteElementSpace *vfes;
vfes = (FiniteElementSpace *)m->GetNodalFESpace();
if (vfes == NULL)
{
vfec = new LinearFECollection;
vfes = new FiniteElementSpace(m, vfec, m->SpaceDimension());
}
meshstate.SetGridFunction(new GridFunction(vfes));
GridFunction * const g = meshstate.grid_f.get();
if (vfec)
{
g->MakeOwner(vfec);
}
m->GetNodes(*g);
if (g->Size() == init_nodes->Size())
{
subtract(*init_nodes, *g, *g);
}
else
{
cout << "Script: incompatible meshes!" << endl;
*g = 0.0;
}
state = std::move(meshstate);
}
return 0;
}
int ScriptController::ScriptReadDataColl(istream &scr, DataState &state,
bool mesh_only, bool quad)
{
int err_read;
int type;
string cword, fword;
cout << "Script: data_collection: " << flush;
// read the collection
scr >> ws >> type; // collection type
cout << "type: " << type << "; " << flush;
scr >> ws >> cword; // collection filename (can't contain spaces)
cout << "collection: " << cword << "; " << flush;
if (!mesh_only)
{
// read the field
scr >> ws >> fword;
cout << "field: " << fword << endl;
}
DataCollectionReader reader(state);
if (dc_protocol != string_default)
{
reader.SetProtocol(dc_protocol.c_str());
}
if (mesh_only)
err_read = reader.ReadSerial((DataCollectionReader::CollType)type,
cword.c_str(), dc_cycle);
else
err_read = reader.ReadSerial((DataCollectionReader::CollType)type,
cword.c_str(), dc_cycle, fword.c_str(), quad);
return err_read;
}
void ScriptController::PrintCommands()
{
cout << "Available commands are:" << endl;
for (const auto &ci : commands)
{
cout << "\t" << ci.keyword << " " << ci.params << " - " << ci.desc << endl;
}
}
bool ScriptController::ExecuteScriptCommand()
{
if (!script)
{
cout << "No script stream defined! (Bug?)" << endl;
return false;
}
istream &scr = *script;
string word;
int done_one_command = 0;
while (!done_one_command)
{
scr >> ws;
if (!scr.good())
{
cout << "End of script." << endl;
scr_level = 0;
if (win.headless)
{
win.wnd->signalQuit();
}
return false;
}
if (scr.peek() == '#')
{
getline(scr, word);
continue;
}
scr >> word;
if (word == "{")
{
scr_level++;
continue;
}
else if (word == "}")
{
scr_level--;
if (scr_level < 0)
{
scr_level = 0;
}
continue;
}
auto it = find(commands.begin(), commands.end(), word);
if (it == commands.end())
{
cout << "Unknown command in script: " << word << endl;
PrintCommands();
return false;
}
const Command cmd = (Command)(it - commands.begin());
switch (cmd)
{
case Command::Mesh:
case Command::Solution:
case Command::ParSolution:
case Command::Quadrature:
case Command::ParQuadrature:
case Command::DataCollMesh:
case Command::DataCollField:
case Command::DataCollQuad:
{
DataState new_state;
switch (cmd)
{
case Command::Solution:
if (ScriptReadSolution(scr, new_state))
{
done_one_command = 1;
continue;
}
break;
case Command::Quadrature:
if (ScriptReadQuadrature(scr, new_state))
{
done_one_command = 1;
continue;
}
break;
case Command::Mesh:
if (ScriptReadDisplMesh(scr, new_state))
{
done_one_command = 1;
continue;
}
if (new_state.mesh == NULL)
{
cout << "Script: unexpected 'mesh' command!" << endl;
done_one_command = 1;
continue;
}
break;
case Command::ParSolution:
if (ScriptReadParSolution(scr, new_state))
{
done_one_command = 1;
continue;
}
break;
case Command::ParQuadrature:
if (ScriptReadParQuadrature(scr, new_state))
{
done_one_command = 1;
continue;
}
break;
case Command::DataCollMesh:
if (ScriptReadDataColl(scr, new_state))
{
done_one_command = 1;
continue;
}
break;
case Command::DataCollField:
if (ScriptReadDataColl(scr, new_state, false))
{
done_one_command = 1;
continue;
}
break;
case Command::DataCollQuad:
if (ScriptReadDataColl(scr, new_state, false, true))
{
done_one_command = 1;
continue;
}
break;
default:
break;
}
if (win.SetNewMeshAndSolution(std::move(new_state)))
{
MyExpose();
}
else
{
cout << "Different type of mesh / solution." << endl;
}
}
break;
case Command::DataCollCycle:
scr >> dc_cycle;
break;
case Command::DataCollProto:
scr >> dc_protocol;
break;
case Command::Screenshot:
{
scr >> ws >> word;
cout << "Script: screenshot: -> " << word << endl;
// Allow GlWindow to handle the expose and screenshot action, in case
// any actions need to be taken before MyExpose().
win.wnd->screenshot(word, true);
if (scr_min_val > win.vs->GetMinV())
{
scr_min_val = win.vs->GetMinV();
}
if (scr_max_val < win.vs->GetMaxV())
{
scr_max_val = win.vs->GetMaxV();
}
}
break;
case Command::Viewcenter:
{
scr >> win.vs->ViewCenterX >> win.vs->ViewCenterY;
cout << "Script: viewcenter: "
<< win.vs->ViewCenterX << ' ' << win.vs->ViewCenterY << endl;
MyExpose();
}
break;
case Command::Perspective:
{
scr >> ws >> word;
cout << "Script: perspective: " << word;
if (word == "off")
{
win.vs->OrthogonalProjection = 1;
}
else if (word == "on")
{
win.vs->OrthogonalProjection = 0;
}
else
{
cout << '?';
}
cout << endl;
MyExpose();
}
break;
case Command::Light:
{
scr >> ws >> word;
cout << "Script: light: " << word;
if (word == "off")
{
win.vs->SetLight(false);
}
else if (word == "on")
{
win.vs->SetLight(true);
}
else
{
cout << '?';
}
cout << endl;
MyExpose();
}
break;
case Command::View:
{
double theta, phi;
scr >> theta >> phi;
cout << "Script: view: " << theta << ' ' << phi << endl;
win.vs->SetView(theta, phi);
MyExpose();
}
break;
case Command::Zoom:
{
double factor;
scr >> factor;
cout << "Script: zoom: " << factor << endl;
win.vs->Zoom(factor);
MyExpose();
}
break;
case Command::Shading:
{
scr >> ws >> word;
cout << "Script: shading: " << flush;
VisualizationSceneScalarData::Shading s =
VisualizationSceneScalarData::Shading::Invalid;
if (word == "flat")
{
s = VisualizationSceneScalarData::Shading::Flat;
}
else if (word == "smooth")
{
s = VisualizationSceneScalarData::Shading::Smooth;
}
else if (word == "cool")
{
s = VisualizationSceneScalarData::Shading::Noncomforming;
}
if (s != VisualizationSceneScalarData::Shading::Invalid)
{
win.vs->SetShading(s, false);
cout << word << endl;
MyExpose();
}
else
{
cout << word << " ?" << endl;
}
}
break;
case Command::Subdivisions:
{
int t, b;
scr >> t >> b;
cout << "Script: subdivisions: " << flush;
win.vs->SetRefineFactors(t, b);
cout << t << ' ' << b << endl;
MyExpose();
}
break;
case Command::Valuerange:
{
double min, max;
scr >> min >> max;
cout << "Script: valuerange: " << flush;
win.vs->SetValueRange(min, max);
cout << min << ' ' << max << endl;
MyExpose();
}
break;
case Command::Autoscale:
{
scr >> ws >> word;
cout << "Script: autoscale: " << word;
if (word == "off")
{
win.vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::None);
}
else if (word == "on")
{
win.vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::MeshAndValue);
}
else if (word == "value")
{
win.vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::Value);
}
else if (word == "mesh")
{
win.vs->SetAutoscale(VisualizationSceneScalarData::Autoscale::Mesh);
}
else
{
cout << '?';
}
cout << endl;
}
break;
case Command::Levellines:
{
double min, max;
int num;
scr >> min >> max >> num;
cout << "Script: levellines: " << flush;
win.vs->SetLevelLines(min, max, num);
win.vs->UpdateLevelLines();
cout << min << ' ' << max << ' ' << num << endl;
MyExpose();
}
break;
case Command::AxisNumberFormat:
{
char delim;
string axis_formatting;
scr >> ws >> delim;
getline(scr, axis_formatting, delim);
cout << "Script: axis_numberformat: " << flush;
win.vs->SetAxisNumberFormat(axis_formatting);
cout << axis_formatting << endl;
MyExpose();
}
break;
case Command::ColorbarNumberFormat:
{
char delim;
string colorbar_formatting;
scr >> ws >> delim;
getline(scr, colorbar_formatting, delim);
cout << "Script: colorbar_numberformat: " << flush;
win.vs->SetColorbarNumberFormat(colorbar_formatting);
cout << colorbar_formatting << endl;
MyExpose();
}
break;
case Command::Window:
{
scr >> win.window_x >> win.window_y >> win.window_w >> win.window_h;
cout << "Script: window: " << win.window_x << ' ' << win.window_y
<< ' ' << win.window_w << ' ' << win.window_h << endl;
MoveResizeWindow(win.window_x, win.window_y, win.window_w, win.window_h);
MyExpose();
}
break;
case Command::Keys:
{
scr >> win.data_state.keys;
cout << "Script: keys: '" << win.data_state.keys << "'" << endl;
// SendKeySequence(keys.c_str());
CallKeySequence(win.data_state.keys.c_str());
MyExpose();
}
break;
case Command::Palette:
{
int pal;
scr >> pal;
cout << "Script: palette: " << pal << endl;
win.vs->palette.SetIndex(pal-1);
MyExpose();
}
break;
case Command::PaletteFile:
{
std::string palette_file;
scr >> ws;
std::getline(scr, palette_file);
cout << "Script: palette_file: " << palette_file << endl;
BasePalettes.Load(palette_file);
win.vs->palette.GenerateTextures(true); // need to reinitialize
MyExpose();
}
break;
case Command::PaletteName:
{
std::string palette_name;
scr >> palette_name;
cout << "Script: palette_name: " << palette_name << endl;
win.vs->palette.SetByName(palette_name);
MyExpose();
}
break;
case Command::PaletteRepeat:
{
int rpt_times;
scr >> rpt_times;
cout << "Script: palette_repeat: " << rpt_times << endl;
win.vs->palette.SetRepeatTimes(rpt_times);
win.vs->palette.GenerateTextures();
MyExpose();
}
break;
case Command::ToggleAttributes:
{
Array<int> attr_list;
cout << "Script: toggle_attributes:";
for (scr >> ws; scr.peek() != ';'; scr >> ws)
{
attr_list.Append(0);
scr >> attr_list.Last();
if (attr_list.Size() <= 256)
{
cout << ' ' << attr_list.Last();
}
else if (attr_list.Size() == 257)
{
cout << " ... " << flush;
}
}
scr.get(); // read the end symbol: ';'
cout << endl;
win.vs->ToggleAttributes(attr_list);
MyExpose();
}
break;
case Command::Rotmat:
{
cout << "Script: rotmat:";
for (int i = 0; i < 16; i++)
{
scr >> win.vs->rotmat[i/4][i%4];
cout << ' ' << win.vs->rotmat[i/4][i%4];
}
cout << endl;
MyExpose();
}
break;
case Command::Camera:
{
double cam[9];
cout << "Script: camera:";
for (int i = 0; i < 9; i++)
{
scr >> cam[i];
cout << ' ' << cam[i];
}
cout << endl;
win.vs->cam.Set(cam);
MyExpose();
}
break;
case Command::Scale:
{
double scale;
cout << "Script: scale:";
scr >> scale;
cout << ' ' << scale;
cout << endl;
win.vs->Scale(scale);
MyExpose();
}
break;
case Command::Translate:
{
double x, y, z;
cout << "Script: translate:";
scr >> x >> y >> z;
cout << ' ' << x << ' ' << y << ' ' << z;
cout << endl;
win.vs->Translate(x, y, z);
MyExpose();
}
break;
case Command::PlotCaption:
{
char delim;
scr >> ws >> delim;
getline(scr, win.plot_caption, delim);
win.vs->PrepareCaption(); // turn on or off the caption
MyExpose();
}
break;
case Command::Headless:
cout << "The session cannot become headless after initialization" << endl;
break;
case Command::Max: //dummy
break;
}
done_one_command = 1;
}
return true;
}
thread_local ScriptController *ScriptController::script_ctrl = NULL;
void ScriptController::ScriptIdleFunc()
{
script_ctrl->ExecuteScriptCommand();
if (script_ctrl->scr_level == 0 && !script_ctrl->win.headless)
{
ScriptControl();
}
}
void ScriptController::ScriptControl()
{
if (script_ctrl->scr_running)
{
script_ctrl->scr_running = 0;
RemoveIdleFunc(ScriptIdleFunc);
}
else
{
script_ctrl->scr_running = 1;
AddIdleFunc(ScriptIdleFunc);
}
}
void ScriptController::PlayScript(Window win, istream &scr)
{
string word;
bool done = false;
ScriptController script(std::move(win));
script.scr_min_val = numeric_limits<double>::infinity();
script.scr_max_val = -script.scr_min_val;
// read initializing commands
while (!done)
{
scr >> ws;
if (!scr.good())
{
cout << "Error in script" << endl;
return;
}
if (scr.peek() == '#')
{
getline(scr, word);
continue;
}
scr >> word;
auto it = find(commands.begin(), commands.end(), word);
if (it == commands.end())
{
cout << "Unknown command in script: " << word << endl;
PrintCommands();
return;
}
const Command cmd = (Command)(it - commands.begin());
switch (cmd)
{
case Command::Window:
scr >> script.win.window_x >> script.win.window_y >> script.win.window_w >>
script.win.window_h;
break;
case Command::Headless:
script.win.headless = true;
break;
case Command::DataCollCycle:
scr >> script.dc_cycle;
break;
case Command::DataCollProto:
scr >> script.dc_protocol;
break;
case Command::Solution:
if (ScriptReadSolution(scr, script.win.data_state))
{
return;
}
done = true; // start the visualization
break;
case Command::Quadrature:
if (ScriptReadQuadrature(scr, script.win.data_state))
{
return;
}
done = true; // start the visualization
break;
case Command::ParSolution:
if (ScriptReadParSolution(scr, script.win.data_state))
{
return;
}
done = true; // start the visualization
break;
case Command::ParQuadrature:
if (ScriptReadParQuadrature(scr, script.win.data_state))
{
return;
}
done = true; // start the visualization
break;
case Command::Mesh:
if (script.ScriptReadDisplMesh(scr, script.win.data_state))
{
return;
}
done = script.win.data_state.mesh != nullptr;
break;
case Command::DataCollMesh:
if (script.ScriptReadDataColl(scr, script.win.data_state))
{
return;
}
done = true; // start the visualization
break;
case Command::DataCollField:
if (script.ScriptReadDataColl(scr, script.win.data_state, false))
{
return;
}
done = true; // start the visualization
break;
case Command::DataCollQuad:
if (script.ScriptReadDataColl(scr, script.win.data_state, false, true))
{
return;
}
done = true; // start the visualization
break;
default:
cout << "Command not supported at this level: " << word << endl;
break;
}
}
script.scr_level = script.scr_running = 0;
script.script = &scr;
script.win.data_state.keys.clear();
// backup the headless flag as the window is moved
const bool headless = script.win.headless;
// Make sure the returned singleton object is
// initialized from the main thread.
GetMainThread(headless);
std::thread worker_thread
{
[&](ScriptController local_script)
{
script_ctrl = &local_script;
if (local_script.win.GLVisInitVis({}))
{
if (!local_script.win.headless)
{
local_script.win.wnd->setOnKeyDown(SDLK_SPACE, ScriptControl);
}
else
{
// execute all commands, updating the scene every time
ScriptControl();
}
local_script.win.GLVisStartVis();
}
},
std::move(script)
};
MainThreadLoop(headless);
worker_thread.join();
}
|