-
Notifications
You must be signed in to change notification settings - Fork 91
/
DEMTool.cpp
207 lines (169 loc) · 5.92 KB
/
DEMTool.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
/***********************************************************************
DEMTool - Tool class to load a digital elevation model into an augmented
reality sandbox to colorize the sand surface based on distance to the
DEM.
Copyright (c) 2013-2015 Oliver Kreylos
This file is part of the Augmented Reality Sandbox (SARndbox).
The Augmented Reality Sandbox 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 2 of the
License, or (at your option) any later version.
The Augmented Reality Sandbox 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 Augmented Reality Sandbox; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "DEMTool.h"
#include <Misc/StandardValueCoders.h>
#include <Misc/ConfigurationFile.h>
#include <Geometry/GeometryValueCoders.h>
#include <Vrui/OpenFile.h>
#include "Sandbox.h"
/*******************************
Methods of class DEMToolFactory:
*******************************/
DEMToolFactory::DEMToolFactory(Vrui::ToolManager& toolManager)
:ToolFactory("DEMTool",toolManager),
demSelectionHelper(Vrui::getWidgetManager(),"",".grid",Vrui::openDirectory("."))
{
/* Initialize tool layout: */
layout.setNumButtons(1);
#if 0
/* Insert class into class hierarchy: */
ToolFactory* toolFactory=toolManager.loadClass("Tool");
toolFactory->addChildClass(this);
addParentClass(toolFactory);
#endif
/* Set tool class' factory pointer: */
DEMTool::factory=this;
}
DEMToolFactory::~DEMToolFactory(void)
{
/* Reset tool class' factory pointer: */
DEMTool::factory=0;
}
const char* DEMToolFactory::getName(void) const
{
return "Show DEM";
}
const char* DEMToolFactory::getButtonFunction(int) const
{
return "Toggle DEM";
}
Vrui::Tool* DEMToolFactory::createTool(const Vrui::ToolInputAssignment& inputAssignment) const
{
return new DEMTool(this,inputAssignment);
}
void DEMToolFactory::destroyTool(Vrui::Tool* tool) const
{
delete tool;
}
/********************************
Static elements of class DEMTool:
********************************/
DEMToolFactory* DEMTool::factory=0;
/************************
Methods of class DEMTool:
************************/
void DEMTool::loadDEMFile(const char* demFileName)
{
/* Load the selected DEM file: */
load(demFileName);
OGTransform demT;
if(haveDemTransform)
demT=demTransform;
else
{
/* Calculate an appropriate DEM transformation to fit the DEM into the sandbox's domain: */
const Scalar* demBox=getDemBox();
Scalar demSx=demBox[2]-demBox[0];
Scalar demSy=demBox[3]-demBox[1];
Scalar boxSx=application->bbox.getSize(0);
Scalar boxSy=application->bbox.getSize(1);
/* Shift the DEM's center to the box's center: */
Point demCenter;
demCenter[0]=Math::mid(demBox[0],demBox[2]);
demCenter[1]=Math::mid(demBox[1],demBox[3]);
demCenter[2]=Scalar(calcAverageElevation());
demT=OGTransform::translateFromOriginTo(demCenter);
/* Determine whether the DEM should be rotated: */
Scalar scale=Math::min(demSx/boxSx,demSy/boxSy);
Scalar scaleRot=Math::min(demSx/boxSy,demSy/boxSx);
if(scale<scaleRot)
{
/* Scale and rotate DEM: */
demT*=OGTransform::rotate(OGTransform::Rotation::rotateZ(Math::rad(Scalar(90))));
scale=scaleRot;
}
/* Scale DEM without rotation: */
demT*=OGTransform::scale(scale);
}
/* Shift the DEM vertically: */
demT*=OGTransform::translate(Vector(0,0,demVerticalShift/demVerticalScale));
/* Set the DEM transformation: */
setTransform(demT*OGTransform(application->boxTransform),demVerticalScale,demT.getOrigin()[2]);
}
void DEMTool::loadDEMFileCallback(GLMotif::FileSelectionDialog::OKCallbackData* cbData)
{
/* Load the selected DEM file: */
loadDEMFile(cbData->selectedDirectory->getPath(cbData->selectedFileName).c_str());
}
DEMToolFactory* DEMTool::initClass(Vrui::ToolManager& toolManager)
{
/* Create the tool factory: */
factory=new DEMToolFactory(toolManager);
/* Register and return the class: */
toolManager.addClass(factory,Vrui::ToolManager::defaultToolFactoryDestructor);
return factory;
}
DEMTool::DEMTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Vrui::Tool(factory,inputAssignment),
haveDemTransform(false),demTransform(OGTransform::identity),
demVerticalShift(0),demVerticalScale(1)
{
}
DEMTool::~DEMTool(void)
{
}
void DEMTool::configure(const Misc::ConfigurationFileSection& configFileSection)
{
/* Query DEM file name: */
demFileName=configFileSection.retrieveString("./demFileName",demFileName);
/* Read the DEM transformation: */
if(configFileSection.hasTag("./demTransform"))
{
haveDemTransform=true;
demTransform=configFileSection.retrieveValue<OGTransform>("./demTransform",demTransform);
}
demVerticalShift=configFileSection.retrieveValue<Scalar>("./demVerticalShift",demVerticalShift);
demVerticalScale=configFileSection.retrieveValue<Scalar>("./demVerticalScale",demVerticalScale);
}
void DEMTool::initialize(void)
{
/* Bring up a file selection dialog if there is no pre-configured DEM file: */
if(demFileName.empty())
{
/* Load a DEM file: */
factory->demSelectionHelper.loadFile("Load DEM File...",this,&DEMTool::loadDEMFileCallback);
}
else
{
/* Load the configured DEM file: */
loadDEMFile(demFileName.c_str());
}
}
const Vrui::ToolFactory* DEMTool::getFactory(void) const
{
return factory;
}
void DEMTool::buttonCallback(int buttonSlotIndex,Vrui::InputDevice::ButtonCallbackData* cbData)
{
if(cbData->newButtonState)
{
/* Toggle this DEM tool as the active one: */
application->toggleDEM(this);
}
}