-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample.cxx
351 lines (309 loc) · 11.4 KB
/
example.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
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
#include <curl/curl.h>
#include "vtkFeatureLayer.h"
#include "vtkGeoMapSelection.h"
#include "vtkInteractorStyleGeoMap.h"
#include "vtkMap.h"
#include "vtkMapMarkerSet.h"
#include "vtkMercator.h"
#include "vtkMultiThreadedOsmLayer.h"
#include "vtkOsmLayer.h"
#include "vtkPolydataFeature.h"
#include <vtkCollection.h>
#include <vtkCommand.h>
#include <vtkIdList.h>
#include <vtkInteractorStyle.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRegularPolygonSource.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtksys/CommandLineArguments.hxx>
#include <iostream>
// ------------------------------------------------------------
class PickCallback : public vtkCommand
{
public:
static PickCallback* New() { return new PickCallback; }
void Execute(vtkObject* caller, unsigned long event, void* data) override
{
switch (event)
{
case vtkInteractorStyleGeoMap::DisplayClickCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Point coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::DisplayDrawCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Rectangle coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::SelectionCompleteEvent:
{
vtkObject* object = static_cast<vtkObject*>(data);
vtkGeoMapSelection* selection =
vtkGeoMapSelection::SafeDownCast(object);
double* latLonCoords = selection->GetLatLngBounds();
std::cout << "Selected coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
vtkCollection* collection = selection->GetSelectedFeatures();
std::cout << "Number of features: " << collection->GetNumberOfItems()
<< std::endl;
vtkNew<vtkIdList> cellIdList;
vtkNew<vtkIdList> markerIdList;
vtkNew<vtkIdList> clusterIdList;
for (int i = 0; i < collection->GetNumberOfItems(); i++)
{
vtkObject* object = collection->GetItemAsObject(i);
std::cout << " " << object->GetClassName() << "\n";
vtkFeature* feature = vtkFeature::SafeDownCast(object);
// Retrieve polydata cells (if relevant)
if (selection->GetPolyDataCellIds(feature, cellIdList.GetPointer()))
{
if (cellIdList->GetNumberOfIds() > 0)
{
std::cout << " Cell ids: ";
for (int j = 0; j < cellIdList->GetNumberOfIds(); j++)
{
std::cout << " " << cellIdList->GetId(j);
}
std::cout << std::endl;
}
}
// Retrieve marker ids (if relevant)
if (selection->GetMapMarkerIds(
feature, markerIdList.GetPointer(), clusterIdList.GetPointer()))
{
std::cout << " Marker ids: ";
for (int j = 0; j < markerIdList->GetNumberOfIds(); j++)
{
std::cout << " " << markerIdList->GetId(j);
}
std::cout << std::endl;
std::cout << " Cluster ids: ";
for (int j = 0; j < clusterIdList->GetNumberOfIds(); j++)
{
std::cout << " " << clusterIdList->GetId(j);
}
std::cout << std::endl;
}
} // for (i)
} // case
break;
case vtkInteractorStyleGeoMap::ZoomCompleteEvent:
{
double* latLonCoords = static_cast<double*>(data);
std::cout << "Zoom coordinates: \n"
<< " " << latLonCoords[0] << ", " << latLonCoords[1]
<< "\n " << latLonCoords[2] << ", " << latLonCoords[3]
<< std::endl;
}
break;
case vtkInteractorStyleGeoMap::RightButtonCompleteEvent:
{
int* coords = static_cast<int*>(data);
std::cout << "Right mouse click at (" << coords[0] << ", " << coords[1]
<< ")" << std::endl;
}
break;
} // switch
}
void SetMap(vtkMap* map) { this->Map = map; }
protected:
vtkMap* Map;
};
// ------------------------------------------------------------
int main(int argc, char* argv[])
{
// Initialize libcurl for vtkMap to avoid bad surprises
curl_global_init(CURL_GLOBAL_DEFAULT);
// Setup command line arguments
int clusteringOff = false;
bool showHelp = false;
bool perspective = false;
bool rubberBandDisplayOnly = false;
bool rubberBandSelection = false;
bool drawPolygonSelection = false;
bool rubberBandZoom = false;
bool singleThreaded = false;
int zoomLevel = 2;
std::vector<double> centerLatLon;
std::string tileExtension = "png";
std::string tileServer;
std::string tileServerAttribution;
vtksys::CommandLineArguments arg;
arg.Initialize(argc, argv);
arg.StoreUnusedArguments(true);
arg.AddArgument("-h", vtksys::CommandLineArguments::NO_ARGUMENT, &showHelp,
"show help message");
arg.AddArgument("--help", vtksys::CommandLineArguments::NO_ARGUMENT,
&showHelp, "show help message");
arg.AddArgument("-a", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileServerAttribution, "map-tile server attribution");
arg.AddArgument("-d", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandDisplayOnly, "set interactor to rubberband-draw mode");
arg.AddArgument("-e", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileExtension, "map-tile file extension (jpg, png, etc.)");
arg.AddArgument("-c", vtksys::CommandLineArguments::MULTI_ARGUMENT,
¢erLatLon, "initial center (latitude longitude)");
arg.AddArgument("-m", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&tileServer, "map-tile server (tile.openstreetmaps.org)");
arg.AddArgument("-o", vtksys::CommandLineArguments::NO_ARGUMENT,
&clusteringOff, "turn clustering off");
arg.AddArgument("-p", vtksys::CommandLineArguments::NO_ARGUMENT, &perspective,
"use perspective projection");
arg.AddArgument("-q", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandZoom, "set interactor to rubberband zoom mode");
arg.AddArgument("-r", vtksys::CommandLineArguments::NO_ARGUMENT,
&rubberBandSelection, "set interactor to rubberband selection mode");
arg.AddArgument("-P", vtksys::CommandLineArguments::NO_ARGUMENT,
&drawPolygonSelection, "set interactor to polygon selection mode");
arg.AddArgument("-s", vtksys::CommandLineArguments::NO_ARGUMENT,
&singleThreaded, "use single-threaded map I/O");
arg.AddArgument("-z", vtksys::CommandLineArguments::SPACE_ARGUMENT,
&zoomLevel, "initial zoom level (1-20)");
if (!arg.Parse() || showHelp)
{
std::cout << "\n" << arg.GetHelp() << std::endl;
return -1;
}
vtkNew<vtkMap> map;
// For reference, Kitware geo coords (Clifton Park, NY)
double kwLatitude = 42.849604;
double kwLongitude = -73.758345;
// Note: Always set map's renderer *before* adding layers
vtkNew<vtkRenderer> rend;
map->SetRenderer(rend.GetPointer());
// Set viewport
if (centerLatLon.size() == 2)
{
map->SetCenter(centerLatLon[0], centerLatLon[1]);
}
else
{
// If center not specified, display CONUS
double latLngCoords[] = { 25.0, -115.0, 50.0, -75.0 };
map->SetVisibleBounds(latLngCoords);
}
map->SetPerspectiveProjection(perspective);
// Adjust zoom level to perspective vs orthographic projection
// Internally, perspective is zoomed in one extra level
// Do the same here for perspective.
zoomLevel += perspective ? 0 : 1;
map->SetZoom(zoomLevel);
vtkOsmLayer* osmLayer;
if (singleThreaded)
{
osmLayer = vtkOsmLayer::New();
}
else
{
osmLayer = vtkMultiThreadedOsmLayer::New();
}
map->AddLayer(osmLayer);
if (tileServer != "")
{
osmLayer->SetMapTileServer(
tileServer.c_str(), tileServerAttribution.c_str(), tileExtension.c_str());
}
vtkNew<vtkRenderWindow> wind;
wind->SetMultiSamples(0); // MSAA will create interpolated pixels
wind->AddRenderer(rend.GetPointer());
//wind->SetSize(1920, 1080);
wind->SetSize(800, 600);
vtkNew<vtkRenderWindowInteractor> intr;
intr->SetRenderWindow(wind.GetPointer());
map->SetInteractor(intr.GetPointer());
vtkMapType::Interaction mode = vtkMapType::Interaction::Default;
if (rubberBandDisplayOnly)
{
mode = vtkMapType::Interaction::RubberBandDisplayOnly;
}
else if (rubberBandSelection)
{
mode = vtkMapType::Interaction::RubberBandSelection;
}
else if (drawPolygonSelection)
{
mode = vtkMapType::Interaction::PolygonSelection;
}
else if (rubberBandZoom)
{
mode = vtkMapType::Interaction::RubberBandZoom;
}
map->SetInteractionMode(mode);
intr->Initialize();
map->Draw();
double latLon[4];
map->GetVisibleBounds(latLon);
std::cout << "lat-lon bounds: "
<< "(" << latLon[0] << ", " << latLon[1] << ")"
<< " "
<< "(" << latLon[2] << ", " << latLon[3] << ")" << std::endl;
// Initialize test polygon
vtkNew<vtkFeatureLayer> featureLayer;
featureLayer->SetName("test-polygon");
// Note: Always add feature layer to the map *before* adding features
map->AddLayer(featureLayer.GetPointer());
vtkNew<vtkRegularPolygonSource> testPolygon;
testPolygon->SetNumberOfSides(50);
testPolygon->SetRadius(2.0);
vtkNew<vtkPolydataFeature> feature;
feature->GetMapper()->SetInputConnection(testPolygon->GetOutputPort());
feature->GetActor()->GetProperty()->SetColor(
0.0, 80.0 / 255, 80.0 / 255); // (teal)
feature->GetActor()->GetProperty()->SetOpacity(0.5);
double x = kwLongitude;
double y = vtkMercator::lat2y(kwLatitude);
feature->GetActor()->SetPosition(x, y, 0.0);
featureLayer->AddFeature(feature.GetPointer());
map->Draw();
// Instantiate markers for array of lat-lon coordinates
double latlonCoords[][2] = {
0.0, 0.0, 42.849604, -73.758345, // KHQ
35.911373, -79.072205, // KRS
32.301393, -90.871495 // ERDC
};
vtkNew<vtkMapMarkerSet> markerSet;
markerSet->SetClustering(!clusteringOff);
featureLayer->AddFeature(markerSet.GetPointer());
unsigned numMarkers = sizeof(latlonCoords) / sizeof(double) / 2;
for (unsigned i = 0; i < numMarkers; ++i)
{
double lat = latlonCoords[i][0];
double lon = latlonCoords[i][1];
markerSet->AddMarker(lat, lon);
}
map->Draw();
// Set callbacks
vtkNew<PickCallback> pickCallback;
pickCallback->SetMap(map.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::DisplayClickCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::DisplayDrawCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::SelectionCompleteEvent,
pickCallback.GetPointer());
map->AddObserver(
vtkInteractorStyleGeoMap::ZoomCompleteEvent, pickCallback.GetPointer());
map->AddObserver(vtkInteractorStyleGeoMap::RightButtonCompleteEvent,
pickCallback.GetPointer());
intr->Start();
//map->Print(std::cout);
osmLayer->Delete();
// global libcurl cleanup
curl_global_cleanup();
return 0;
}