-
Notifications
You must be signed in to change notification settings - Fork 3
/
FieldViewer.cpp
629 lines (510 loc) · 17.1 KB
/
FieldViewer.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
/*******************************************************************************
FieldViewer: Dynamics Toolset application class.
Copyright (c) 2006-2008 Jordan Van Aalsburg
This file is part of the Dynamics Toolset.
The Dynamics Toolset is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
The Dynamics Toolset is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License
along with the Dynamics Toolset. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "FieldViewer.h"
// Vrui includes
//
#include <Images/RGBImage.h>
#include <Images/ReadImageFile.h>
// ToolBox includes
//
#include <ToolBox/ToolBox>
#include <ToolBox/Extensions/ToolRotator.h>
// Project includes
//
#include "Dynamics/IntegrationStepSize.h"
#include "Directory.h"
#include <algorithm>
DynamicsFactory Factory;
Viewer::Viewer(int &argc, char** argv, char** appDefaults) :
Vrui::Application(argc, argv, appDefaults), tools(ToolList()), model(NULL),
frameRateDialog(NULL),
positionDialog(NULL),
parameterDialog(NULL),
currentOptionsDialog(NULL),
optionsDialogs(DialogArray()),
elapsedTime(0.0), stepSizeVersion(0), modelVersion(0),
masterout(std::cout), nodeout(std::cout), debugout(std::cerr)
{
// load ToolBox
ToolBox::ToolBoxFactory::instance();
// load dynamics plugins
try {
model_names=loadPlugins();
}
catch (std::runtime_error& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
}
catch (...) {
std::cerr << "UNDEFINED ERROR." << std::endl;
}
// alphabetize the names list
std::sort(model_names.begin(), model_names.end());
// initialize the model dynamics (first one alphabetically)
//model=Factory[model_names[0]]();
// load lorenz
std::string name = "Lorenz Attractor";
model = Factory[name]();
// create and set the main menu
mainMenu=createMainMenu();
Vrui::setMainMenu(mainMenu);
// create other dialogs
frameRateDialog = new FrameRateDialog(mainMenu);
positionDialog = new PositionDialog(mainMenu);
// create and assign associated parameter dialog
parameterDialog=model->createParameterDialog(mainMenu);
// Make sure the correct system is toggled
setRadioToggles(dynamicsToggleButtons, name + "toggle");
// center the display
resetNavigationCallback(0);
}
Viewer::~Viewer()
{
delete mainMenu;
delete parameterDialog;
delete model;
for (ToolList::iterator tool=tools.begin(); tool != tools.end(); ++tool)
delete *tool;
// close all dynamic libs (plugins)
for (DLList::iterator lib=dl_list.begin(); lib != dl_list.end(); ++lib)
dlclose(*lib);
}
//
// Internal methods
//
void Viewer::setRadioToggles(ToggleArray& toggles, const std::string& name)
{
for (ToggleArray::iterator button=toggles.begin(); button != toggles.end(); ++button) {
if (strcmp((*button)->getName(), name.c_str()) != 0
and (*button)->getToggle())
(*button)->setToggle(false);
else if (strcmp((*button)->getName(), name.c_str()) == 0)
(*button)->setToggle(true);
}
}
std::vector<std::string> Viewer::loadPlugins() throw(std::runtime_error)
{
// create Directory object and parse plugin directory
std::string directory(RESOURCEDIR);
directory += "/plugins";
masterout() << "Loading plugins from: " << directory << std::endl;
Directory dir;
dir.addExtensionFilter("so");
dir.read(directory);
void *dlib;
for (std::vector<std::string>::const_iterator lib=dir.contents().begin(); lib
!= dir.contents().end(); ++lib)
{
masterout() << "\topening " << *lib << "..." << std::endl;
// prepend directory name to library file name
std::string file=directory + "/" + *lib;
// open the library
dlib=dlopen(file.c_str(), RTLD_NOW);
//dlib=dlopen(file.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (dlib == NULL)
throw std::runtime_error(dlerror());
// add the handle to our list
dl_list.insert(dl_list.end(), dlib);
}
// create an array of model names
std::vector<std::string> model_names;
for (DynamicsFactory::iterator itr=Factory.begin(); itr != Factory.end(); ++itr)
{
model_names.insert(model_names.end(), itr->first);
}
// return the name array
return model_names;
}
//
// Interface
//
void Viewer::initContext(GLContextData& contextData) const
{
// create data item and add it to the context data
DTS::DataItem* dataItem=new DTS::DataItem;
contextData.addDataItem(this, dataItem);
// get point sprite image
std::string directory(RESOURCEDIR);
Images::RGBImage spriteTexture=Images::readImageFile((directory+"/particle.png").c_str());
// initialize texture parameters
glBindTexture(GL_TEXTURE_2D, dataItem->spriteTextureObjectId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
spriteTexture.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB);
glBindTexture(GL_TEXTURE_2D, 0);
}
void Viewer::display(GLContextData& contextData) const
{
// get data item from context data
DTS::DataItem* dataItem=contextData.retrieveDataItem<DTS::DataItem> (this);
// iterator over all tools and render data
for (ToolList::const_iterator tool=tools.begin(); tool != tools.end(); ++tool)
{
if ((*tool)->isDisabled())
continue;
else if ( (*tool)->needsGLSL() && not dataItem->hasShaders )
{
continue;
}
else
{
(*tool)->render(dataItem);
}
}
}
void Viewer::frame()
{
// check if the integrator has updated
unsigned int _stepSizeVersion = IntegrationStepSize::instance()->getValueVersion();
unsigned int _modelVersion = model->getModelVersion();
bool updatedIntegrator = false;
if (stepSizeVersion != _stepSizeVersion || modelVersion != _modelVersion)
{
updatedIntegrator = true;
}
// frame rate
double frameTime = Vrui::getCurrentFrameTime();
double throttledFrameRate = frameRateDialog->getThrottledFrameRate();
frameRateDialog->setFrameRate(1.0/frameTime);
elapsedTime += frameTime;
bool stepTools = false;
if (elapsedTime >= 1.0/throttledFrameRate)
{
stepTools = true;
//std::cout << "throttled value reached" << std::endl;
elapsedTime = 0.0;
}
// iterate over all tools and do required processing
for (ToolList::iterator tool=tools.begin(); tool != tools.end(); ++tool)
{
if ((*tool)->isDisabled())
{
continue;
}
else
{
if (updatedIntegrator)
{
(*tool)->updatedIntegrator();
}
if (stepTools)
{
(*tool)->step();
}
// If we want multiple users to be able to track various tools,
// we'll need to go back to the TrackerTool creation. For single
// position tracking, we just track the position of the first tool.
if (tool == tools.begin())
{
Vrui::Point position = tools[0]->toolBox()->deviceTransformationInModel().getOrigin();//Vrui::getDeviceTransformation(input.getDevice(0)).getOrigin();
positionDialog->setPosition(position);
}
}
}
// mark versions the same
stepSizeVersion = _stepSizeVersion;
modelVersion = _modelVersion;
// update the display
Vrui::requestUpdate();
}
void Viewer::updateToolToggles()
{
// loop over toggle buttons
for (ToggleArray::iterator button=toolsToggleButtons.begin(); button
!= toolsToggleButtons.end(); ++button)
{
// get toggle button name
std::string toggle_name=(*button)->getName();
// erase "toggle" from name
toggle_name.erase(toggle_name.size() - 6, toggle_name.size());
toggle_name+="Tool";
// use the toolmap (names:tools) to get the tool pointer
AbstractDynamicsTool* tool=toolmap[toggle_name];
// set the toggle button state based on tool state
bool state=tool->isDisabled();
(*button)->setToggle(!state);
}
}
void Viewer::updateCurrentOptionsDialog()
{
// get the current tool from toolbox
ToolBox::Tool* currentTool=tools[0]->toolBox()->currentTool();
if (currentTool == NULL)
{
nodeout() << "ERROR::currentTool not defined." << std::endl;
return; // I feel better already...
}
// find the tool in the application tool list
int index=0;
for (ToolList::iterator tool=tools.begin(); tool != tools.end(); ++tool)
{
if (*tool == currentTool)
break;
index++;
}
bool popup=false;
bool reset=false;
GLMotif::WidgetManager::Transformation oldTrans;
// if tool options dialog has not been defined set the dialog and return
if (currentOptionsDialog == NULL)
{
currentOptionsDialog=optionsDialogs[index];
return;
}
// save the transformation of existing dialog
oldTrans=currentOptionsDialog->getTransformation();
// hide dialog before reassigning
if (currentOptionsDialog->state() == CaveDialog::ACTIVE)
{
currentOptionsDialog->hide();
popup=true;
}
if (!currentOptionsDialog->initialized())
{
reset=true;
}
// set dialog to one associated with current tool
currentOptionsDialog=optionsDialogs[index];
if (!reset)
currentOptionsDialog->setTransformation(oldTrans);
else
currentOptionsDialog->reset();
// popup if necessary
if (popup)
currentOptionsDialog->show();
}
//
// Callbacks
//
void Viewer::toolCreationCallback(Vrui::ToolManager::ToolCreationCallbackData* cbData)
{
ToolBox::ToolBox* toolBox=dynamic_cast<ToolBox::ToolBox*> (cbData->tool);
if (toolBox != 0)
{
(new ToolBox::Extensions::ToolRotator(toolBox))->alwaysShowCurrent(false).positionOfCurrent(180).closeDelay(2.0);
masterout() << "Creating tools and adding to toolbox..." << std::endl;
AbstractDynamicsTool *tool;
// add tools
masterout() << "\tAdding Static Solver..." << std::endl;
tool=new StaticSolverTool(toolBox, this);
// set dynamical integrator and add tool to array
tool->setIntegrator(model);
tools.push_back(tool);
// create associated options dialog and add to dialog array
optionsDialogs.push_back(tool->createOptionsDialog(mainMenu));
toolmap["StaticSolverTool"]=tool;
masterout() << "\tAdding Dot Spreader..." << std::endl;
tool=new DotSpreaderTool(toolBox, this);
// set dynamical integrator and add tool to array
tool->setIntegrator(model);
tools.push_back(tool);
// create associated options dialog and add to dialog array
optionsDialogs.push_back(tool->createOptionsDialog(mainMenu));
toolmap["DotSpreaderTool"]=tool;
masterout() << "\tAdding Particle Sprayer..." << std::endl;
tool=new ParticleSprayerTool(toolBox, this);
// set dynamical integrator and add tool to array
tool->setIntegrator(model);
tools.push_back(tool);
// create associated options dialog and add to dialog array
optionsDialogs.push_back(tool->createOptionsDialog(mainMenu));
toolmap["ParticleSprayerTool"]=tool;
masterout() << "\tAdding Dynamic Solver..." << std::endl;
tool=new DynamicSolverTool(toolBox, this);
// set dynamical integrator and add tool to array
tool->setIntegrator(model);
tools.push_back(tool);
// create associated options dialog and add to dialog array
optionsDialogs.push_back(tool->createOptionsDialog(mainMenu));
toolmap["DynamicSolverTool"]=tool;
// automatically load the first tool and set options dialog
AbstractDynamicsTool* currentTool = static_cast<AbstractDynamicsTool*>(tools.front());
currentTool->grab();
updateCurrentOptionsDialog();
}
}
void Viewer::toolDestructionCallback(Vrui::ToolManager::ToolDestructionCallbackData* cbData)
{
// need to fix this to handle multiple users each with their own toolbox
tools.clear();
toolmap.clear();
}
void Viewer::resetNavigationCallback(Misc::CallbackData* cbData)
{
Vrui::setNavigationTransformation(model->center(), 40.0);
}
void Viewer::mainMenuTogglesCallback(GLMotif::ToggleButton::ValueChangedCallbackData *cbData)
{
std::string name=cbData->toggle->getName();
if (name == "ShowParameterDialogToggle")
{
// if toggle is set show the parameter dialog
if (cbData->toggle->getToggle())
{
parameterDialog->show();
}
// otherwise hide the parameter dialog
else
{
parameterDialog->hide();
}
}
else if (name == "ShowOptionsDialogsToggle")
{
// if toggle is set show tool options dialogs
if (cbData->toggle->getToggle())
{
// if no tool is set reset toggle and return
if (!currentOptionsDialog)
{
cbData->toggle->setToggle(false);
return;
}
currentOptionsDialog->show();
}
// otherwise hide the current dialog
else
{
currentOptionsDialog->hide();
}
}
else if (name == "ShowFrameRateDialogToggle")
{
// if toggle is set show the parameter dialog
if (cbData->toggle->getToggle())
{
frameRateDialog->show();
}
// otherwise hide the parameter dialog
else
{
frameRateDialog->hide();
}
}
else if (name == "ShowPositionDialog")
{
// if toggle is set to show the position dialog
if (cbData->toggle->getToggle())
{
positionDialog->show();
}
else
{
positionDialog->hide();
}
}
else
{
}
}
void Viewer::dynamicsMenuCallback(GLMotif::ToggleButton::ValueChangedCallbackData *cbData)
{
bool popup=false;
// delete current dynamical model
if (model != NULL)
delete model;
GLMotif::WidgetManager::Transformation oldTrans;
// delete current parameter dialog
if (parameterDialog != NULL)
{
// save the old transformation
oldTrans=parameterDialog->getTransformation();
// if dialog is currently shown hide before deleting
if (parameterDialog->state() == CaveDialog::ACTIVE)
{
parameterDialog->hide();
// set flag to popup after creating new dialog
popup=true;
}
delete parameterDialog;
}
std::string name=cbData->toggle->getName();
// create the key from the toggle name
std::string key(name);
key.erase(key.find("toggle"));
debugout() << " toggle: " << name << "\tkey: " << key << std::endl;
// create the dynamical model
model=Factory[key]();
// create/assign parameter dialog
parameterDialog=model->createParameterDialog(mainMenu);
parameterDialog->setTransformation(oldTrans);
// popup parameter dialog if previously shown or system requests it
if (popup)
parameterDialog->show();
else if (model->autoShowParameterDialog())
{
showParameterDialogToggle->setToggle(true);
parameterDialog->show();
}
// update the integration step size
double step_size=IntegrationStepSize::instance()->getSavedValue(key);
if (step_size > 0.0)
{
masterout() << key << ":restoring step size [" << step_size << "]"
<< std::endl;
}
else
{
// set the integration step size to the default value
step_size=IntegrationStepSize::DefaultStepSize;
masterout() << key << ":integration step size not set."
<< " Setting to default (" << step_size << ")" << std::endl;
}
IntegrationStepSize::instance()->setValue(step_size);
// iterate through tools and sets dynamical integrator
for (ToolList::iterator tool=tools.begin(); tool != tools.end(); ++tool)
{
(*tool)->setIntegrator(model);
}
// fake radio-button behavior
setRadioToggles(dynamicsToggleButtons, name);
}
void Viewer::toolsMenuCallback(GLMotif::ToggleButton::ValueChangedCallbackData *cbData)
{
AbstractDynamicsTool* tool;
std::string name=cbData->toggle->getName();
{
if (name == "ParticleSprayerToggle")
{
masterout() << "Current Tool: Particle Sprayer" << std::endl;
tool=toolmap["ParticleSprayerTool"];
bool state=tool->isDisabled();
tool->setDisabled(!state);
}
else if (name == "DotSpreaderToggle")
{
masterout() << "Current Tool: Dot Spreader" << std::endl;
tool=toolmap["DotSpreaderTool"];
bool state=tool->isDisabled();
tool->setDisabled(!state);
}
else if (name == "StaticSolverToggle")
{
masterout() << "CurrentTool: Static Solver" << std::endl;
tool=toolmap["StaticSolverTool"];
bool state=tool->isDisabled();
tool->setDisabled(!state);
}
else if (name == "DynamicSolverToggle")
{
masterout() << "CurrentTool: Dynamic Solver" << std::endl;
tool=toolmap["DynamicSolverTool"];
bool state=tool->isDisabled();
tool->setDisabled(!state);
}
else
{
}
}
}