-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkMap.cxx
246 lines (204 loc) · 7.18 KB
/
vtkMap.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkMap.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkMap.h"
#include "vtkMapTile.h"
// VTK Includes
#include <vtkObjectFactory.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkCamera.h>
#include <vtkMatrix4x4.h>
#include <vtkPlaneSource.h>
#include <vtkCallbackCommand.h>
#include <vtkRenderWindowInteractor.h>
#include <sstream>
vtkStandardNewMacro(vtkMap)
class MapCallback : public vtkCallbackCommand
{
vtkMap *map;
public:
MapCallback(vtkMap *map)
{
this->map = map;
}
static double DistanceToZoomMap[18];
virtual void Execute(vtkObject *caller, unsigned long eid, void *callData)
{
std::cout << "Execute Renderer!" << std::endl;
double distance = map->GetRenderer()->GetActiveCamera()->GetPosition()[2];
std::cout << distance << std::endl;
for(int i = 0; i < sizeof(DistanceToZoomMap) / sizeof(DistanceToZoomMap[0]); i++)
{
if(distance > DistanceToZoomMap[i])
{
map->SetZoom(i + 1);
map->Draw();
break;
}
}
}
};
double MapCallback::DistanceToZoomMap[] = {
8.39055, // Greater => Zoom Level 1
4.73624, // Greater => Zoom Level 2
2.67349, // Greater => Zoom Level 3
1.2443, // Greater => Zoom Level 4
0 // Greater => Zoom Level 5
};
//----------------------------------------------------------------------------
vtkMap::vtkMap()
{
this->Zoom = 1;
this->Center[0] = this->Center[1] = 0.0;
this->Initialized = false;
callback = 0;
}
//----------------------------------------------------------------------------
vtkMap::~vtkMap()
{
}
//----------------------------------------------------------------------------
void vtkMap::PrintSelf(ostream &os, vtkIndent indent)
{
Superclass::PrintSelf(os, indent);
os << "vtkMap" << std::endl
<< "Zoom Level: " << this->Zoom
<< "Center: " << this->Center[0] << " " << this->Center[1] << std::endl;
}
//----------------------------------------------------------------------------
void vtkMap::Update()
{
RemoveTiles();
AddTiles();
}
//----------------------------------------------------------------------------
void vtkMap::Draw()
{
if (!this->Initialized && this->Renderer)
{
this->Initialized = true;
this->Renderer->GetActiveCamera()->SetPosition(this->Center[0],
this->Center[1],
100.0);
callback = new MapCallback(this);
this->Renderer->GetRenderWindow()->GetInteractor()->AddObserver(vtkCommand::MouseWheelBackwardEvent, callback);
this->Renderer->GetRenderWindow()->GetInteractor()->AddObserver(vtkCommand::MouseWheelForwardEvent, callback);
}
this->Update();
this->Renderer->GetRenderWindow()->Render();
}
//----------------------------------------------------------------------------
void vtkMap::RemoveTiles()
{
// To do.
}
//----------------------------------------------------------------------------
void vtkMap::AddTiles()
{
// Obtain Window Size
double xmax = Renderer->GetSize()[0];
double ymax = Renderer->GetSize()[1];
double bottomLeft[2], topRight[2];
// Obtain world coordinates of viewport corner points
Renderer->SetWorldPoint(0, 0, 0, 0);
Renderer->WorldToDisplay();
Renderer->GetDisplayPoint(bottomLeft);
vtkWarningMacro( << "Bottom Left Display: " << bottomLeft[0] << " " << bottomLeft[1] << " " << bottomLeft[2]);
Renderer->SetWorldPoint(1, 1, 0, 0);
Renderer->WorldToDisplay();
Renderer->GetDisplayPoint(topRight);
vtkWarningMacro( << "Top Right Display: " << topRight[0] << " " << topRight[1] << " " << topRight[2]);
// Obtain window size in terms of world coordinates
int height = topRight[0] - bottomLeft[0];
int width = topRight[1] - bottomLeft[1];
// Obtain dimensions of tile grid
int ROWS = ymax / height + 1;
ROWS = ROWS < (2 << (this->Zoom - 1)) ? ROWS : (2 << (this->Zoom - 1));
int COLS = xmax / width + 1;
COLS = COLS < (2 << (this->Zoom - 1)) ? COLS : (2 << (this->Zoom - 1));
std::cout << "Rows and Cols: " << ROWS << " " << COLS << std::endl;
// Convert latitude longitude to Grid Coordinates
int pixX, pixY, tileX, tileY;
LatLongToPixelXY(this->Center[1], this->Center[0], this->Zoom, pixX, pixY);
PixelXYToTileXY(pixX, pixY, tileX, tileY);
int temp_tileX = tileX - ROWS / 2;
int temp_tileY = tileY - ROWS / 2 > 0 ? tileY - ROWS / 2 : 0;
for (int i = -ROWS / 2; i < ROWS / 2; i++)
{
temp_tileX = tileX - ROWS / 2 > 0 ? tileX - ROWS / 2 : 0;
for (int j = -COLS / 2; j < COLS / 2; j++)
{
// Create a tile
vtkMapTile *tile = vtkMapTile::New();
// Set tile position
tile->SetCenter(j, -i, 0);
// Set tile texture source
tile->SetQuadKey(TileXYToQuadKey(temp_tileX, temp_tileY, this->Zoom).c_str());
// Initialise the tile
tile->init();
// Add tile to the renderer
Renderer->AddActor(tile->GetActor());
temp_tileX++;
}
temp_tileY++;
}
}
//----------------------------------------------------------------------------
double vtkMap::Clip(double n, double minValue, double maxValue)
{
double max = n > minValue ? n : minValue;
double min = max > maxValue ? maxValue : max;
return min;
}
//----------------------------------------------------------------------------
void vtkMap::LatLongToPixelXY(double latitude, double longitude, int levelOfDetail, int &pixelX, int &pixelY)
{
latitude = Clip(latitude, MinLatitude, MaxLatitude);
longitude = Clip(longitude, MinLongitude, MaxLongitude);
double x = (longitude + 180) / 360;
double sinLatitude = sin(latitude * 3.142 / 180);
double y = 0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * 3.142);
uint mapSize = MapSize(levelOfDetail);
pixelX = (int) Clip(x * mapSize + 0.5, 0, mapSize - 1);
pixelY = (int) Clip(y * mapSize + 0.5, 0, mapSize - 1);
}
//----------------------------------------------------------------------------
uint vtkMap::MapSize(int levelOfDetail)
{
return (uint) 256 << levelOfDetail;
}
//----------------------------------------------------------------------------
void vtkMap::PixelXYToTileXY(int pixelX, int pixelY, int &tileX, int &tileY)
{
tileX = pixelX / 256;
tileY = pixelY / 256;
}
//----------------------------------------------------------------------------
std::string vtkMap::TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
{
std::stringstream quadKey;
for (int i = levelOfDetail; i > 0; i--)
{
char digit = '0';
int mask = 1 << (i - 1);
if ( (tileX & mask) != 0 )
{
digit++;
}
if ( (tileY & mask) != 0 )
{
digit++;
digit++;
}
quadKey << digit;
}
return quadKey.str();
}