-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
osgfilecache.cpp
491 lines (392 loc) · 14.8 KB
/
osgfilecache.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
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial applications,
* as long as this copyright notice is maintained.
*
* This application 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.
*/
#include <osg/Timer>
#include <osg/ArgumentParser>
#include <osg/ApplicationUsage>
#include <osg/CoordinateSystemNode>
#include <osg/Geometry>
#include <osg/PagedLOD>
#include <osg/io_utils>
#include <osgTerrain/TerrainTile>
#include <osgDB/Archive>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgDB/FileUtils>
#include <osgDB/FileCache>
#include <osgDB/FileNameUtils>
#include <iostream>
#include <algorithm>
#include <signal.h>
#include <stdlib.h>
static bool s_ExitApplication = false;
struct Extents
{
Extents():
_maxLevel(0),
_min(DBL_MAX,DBL_MAX),
_max(-DBL_MAX,-DBL_MAX) {}
Extents(unsigned int maxLevel, double minX, double minY, double maxX, double maxY):
_maxLevel(maxLevel),
_min(minX, minY),
_max(maxX, maxY) {}
Extents(const Extents& extents):
_maxLevel(extents._maxLevel),
_min(extents._min),
_max(extents._max) {}
Extents& operator = (const Extents& rhs)
{
if (&rhs == this) return *this;
_maxLevel = rhs._maxLevel;
_min = rhs._min;
_max = rhs._max;
return *this;
}
bool intersects(unsigned level, osg::Vec2d& in_min, osg::Vec2d& in_max)
{
osg::notify(osg::INFO)<<"intersects("<<level<<", min="<<in_min<<" max="<<in_max<<")"<<std::endl;
osg::notify(osg::INFO)<<" _maxLevel="<<_maxLevel<<", _min="<<_min<<" _max="<<_max<<std::endl;
if (level>_maxLevel) return false;
osg::Vec2d union_min, union_max;
// handle mins
if (_min.x()!=DBL_MAX && in_min.x()!=DBL_MAX)
{
// both _min.x() and in_min.x() are defined so use max of two
union_min.x() = _min.x()>in_min.x() ? _min.x() : in_min.x();
}
else
{
// one or both _min.x() and in_min.x() aren't defined so use min of two
union_min.x() = _min.x()<in_min.x() ? _min.x() : in_min.x();
}
if (_min.y()!=DBL_MAX && in_min.y()!=DBL_MAX)
{
// both _min.y() and in_min.y() are defined so use max of two
union_min.y() = _min.y()>in_min.y() ? _min.y() : in_min.y();
}
else
{
// one or both _min.y() and in_min.y() aren't defined so use min of two
union_min.y() = _min.y()<in_min.y() ? _min.y() : in_min.y();
}
// handle maxs
if (_max.x()!=-DBL_MAX && in_max.x()!=-DBL_MAX)
{
// both _max.x() and in_max.x() are defined so use max of two
union_max.x() = _max.x()<in_max.x() ? _max.x() : in_max.x();
}
else
{
// one or both _max.x() and in_max.x() aren't defined so use max of two
union_max.x() = _max.x()>in_max.x() ? _max.x() : in_max.x();
}
if (_max.y()!=-DBL_MAX && in_max.y()!=-DBL_MAX)
{
// both _max.y() and in_max.y() are defined so use max of two
union_max.y() = _max.y()<in_max.y() ? _max.y() : in_max.y();
}
else
{
// one or both _max.y() and in_max.y() aren't defined so use max of two
union_max.y() = _max.y()>in_max.y() ? _max.y() : in_max.y();
}
bool result = union_min.x()<union_max.x() && union_min.y()<union_max.y() ;
osg::notify(osg::INFO)<<" union_min="<<union_min<<" union_max="<<union_max<<" result = "<<result<<std::endl;
return result;
}
unsigned int _maxLevel;
osg::Vec2d _min;
osg::Vec2d _max;
};
class LoadDataVisitor : public osg::NodeVisitor
{
public:
LoadDataVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_currentLevel(0) {}
void setFileCache(osgDB::FileCache* fileCache) { _fileCache = fileCache; }
void addExtents(unsigned int maxLevel, double minX, double minY, double maxX, double maxY)
{
_extentsList.push_back(Extents(maxLevel, minX, minY, maxX, maxY));
}
void addExtents(unsigned int maxLevel)
{
_extentsList.push_back(Extents(maxLevel, DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX));
}
void apply(osg::CoordinateSystemNode& cs)
{
_csnStack.push_back(&cs);
if (!s_ExitApplication) traverse(cs);
_csnStack.pop_back();
}
void apply(osg::Group& group)
{
if (s_ExitApplication) return;
osgTerrain::TerrainTile* terrainTile = dynamic_cast<osgTerrain::TerrainTile*>(&group);
osgTerrain::Locator* locator = terrainTile ? terrainTile->getLocator() : 0;
if (locator)
{
osg::Vec3d l00(0.0,0.0,0.0);
osg::Vec3d l10(1.0,0.0,0.0);
osg::Vec3d l11(1.0,1.0,0.0);
osg::Vec3d l01(0.0,1.0,0.0);
osg::Vec3d w00, w10, w11, w01;
locator->convertLocalToModel(l00, w00);
locator->convertLocalToModel(l10, w10);
locator->convertLocalToModel(l11, w11);
locator->convertLocalToModel(l01, w01);
if (locator->getEllipsoidModel() &&
locator->getCoordinateSystemType()==osgTerrain::Locator::GEOCENTRIC)
{
convertXYZToLatLongHeight(locator->getEllipsoidModel(), w00);
convertXYZToLatLongHeight(locator->getEllipsoidModel(), w10);
convertXYZToLatLongHeight(locator->getEllipsoidModel(), w11);
convertXYZToLatLongHeight(locator->getEllipsoidModel(), w01);
}
updateBound(w00);
updateBound(w10);
updateBound(w11);
updateBound(w01);
return;
}
traverse(group);
}
void apply(osg::Transform& transform)
{
osg::Matrix matrix;
if (!_matrixStack.empty()) matrix = _matrixStack.back();
transform.computeLocalToWorldMatrix(matrix,this);
pushMatrix(matrix);
traverse(transform);
popMatrix();
}
void apply(osg::PagedLOD& plod)
{
if (s_ExitApplication) return;
++_currentLevel;
initBound();
// first compute the bounds of this subgraph
for(unsigned int i=0; i<plod.getNumFileNames(); ++i)
{
if (plod.getFileName(i).empty())
{
traverse(plod);
}
}
if (intersects())
{
for(unsigned int i=0; i<plod.getNumFileNames(); ++i)
{
osg::notify(osg::INFO)<<" filename["<<i<<"] "<<plod.getFileName(i)<<std::endl;
if (!plod.getFileName(i).empty())
{
std::string filename;
if (!plod.getDatabasePath().empty())
{
filename = plod.getDatabasePath() + plod.getFileName(i);
}
else
{
filename = plod.getFileName(i);
}
osg::ref_ptr<osg::Node> node = readNodeFileAndWriteToCache(filename);
if (!s_ExitApplication && node.valid()) node->accept(*this);
}
}
}
--_currentLevel;
}
void apply(osg::Geode& geode)
{
for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
{
osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
if (geom)
{
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());
if (vertices) updateBound(*vertices);
}
}
}
osg::Node* readNodeFileAndWriteToCache(const std::string& filename)
{
osg::Node* node = 0;
if (_fileCache.valid() )
{
if (_fileCache->existsInCache(filename))
{
osg::notify(osg::NOTICE)<<"reading from FileCache: "<<filename<<std::endl;
node = _fileCache->readNode(filename, osgDB::Registry::instance()->getOptions()).takeNode();
}
else
{
osg::notify(osg::NOTICE)<<"reading : "<<filename<<std::endl;
node = osgDB::readNodeFile(filename);
if (node)
{
osg::notify(osg::NOTICE)<<"write to FileCache : "<<filename<<std::endl;
_fileCache->writeNode(*node, filename, osgDB::Registry::instance()->getOptions());
}
}
}
else
{
osg::notify(osg::NOTICE)<<"reading : "<<filename<<std::endl;
node = osgDB::readNodeFile(filename);
}
return node;
}
protected:
inline void pushMatrix(osg::Matrix& matrix) { _matrixStack.push_back(matrix); }
inline void popMatrix() { _matrixStack.pop_back(); }
void convertXYZToLatLongHeight(osg::EllipsoidModel* em, osg::Vec3d& v)
{
em->convertXYZToLatLongHeight(v.x(), v.y(), v.z(),
v.y(), v.x(), v.z());
v.x() = osg::RadiansToDegrees(v.x());
v.y() = osg::RadiansToDegrees(v.y());
}
void initBound()
{
_min.set(DBL_MAX, DBL_MAX);
_max.set(-DBL_MAX, -DBL_MAX);
}
void updateBound(osg::Vec3d& v)
{
if (v.x() < _min.x()) _min.x() = v.x();
if (v.y() < _min.y()) _min.y() = v.y();
if (v.x() > _max.x()) _max.x() = v.x();
if (v.y() > _max.y()) _max.y() = v.y();
}
void updateBound(osg::Vec3Array& vertices)
{
// set up matrix
osg::Matrix matrix;
if (!_matrixStack.empty()) matrix = _matrixStack.back();
// set up ellipsoid model
osg::EllipsoidModel* em = !_csnStack.empty() ? _csnStack.back()->getEllipsoidModel() : 0;
for(osg::Vec3Array::iterator itr = vertices.begin();
itr != vertices.end();
++itr)
{
osg::Vec3d v = osg::Vec3d(*itr) * matrix;
if (em) convertXYZToLatLongHeight(em, v);
updateBound(v);
}
}
bool intersects()
{
osg::notify(osg::INFO)<<"intersects() _min = "<<_min<<" _max = "<<_max<<std::endl;
for(ExtentsList::iterator itr = _extentsList.begin();
itr != _extentsList.end();
++itr)
{
if (itr->intersects(_currentLevel, _min, _max)) return true;
}
return false;
}
typedef std::vector<Extents> ExtentsList;
typedef std::vector<osg::Matrix> MatrixStack;
typedef std::vector<osg::CoordinateSystemNode*> CSNStack;
osg::ref_ptr<osgDB::FileCache> _fileCache;
ExtentsList _extentsList;
unsigned int _currentLevel;
MatrixStack _matrixStack;
CSNStack _csnStack;
osg::Vec2d _min;
osg::Vec2d _max;
};
static void signalHandler(int sig)
{
printf("\nCaught signal %d, requesting exit...\n\n",sig);
s_ExitApplication = true;
}
int main( int argc, char **argv )
{
#ifndef _WIN32
signal(SIGHUP, signalHandler);
signal(SIGQUIT, signalHandler);
signal(SIGKILL, signalHandler);
signal(SIGUSR1, signalHandler);
signal(SIGUSR2, signalHandler);
#endif
signal(SIGABRT, signalHandler);
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is an application for collecting a set of separate files into a single archive file that can be later read in OSG applications..");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-l level","Read down to level across the whole database.");
arguments.getApplicationUsage()->addCommandLineOption("-e level minX minY maxX maxY","Read down to <level> across the extents minX, minY to maxY, maxY. Note, for geocentric datase X and Y are longitude and latitude respectively.");
arguments.getApplicationUsage()->addCommandLineOption("-c directory","Shorthand for --file-cache directory.");
arguments.getApplicationUsage()->addCommandLineOption("--file-cache directory","Set directory as to place cache download files.");
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
LoadDataVisitor ldv;
std::string fileCachePath;
while(arguments.read("--file-cache",fileCachePath) || arguments.read("-c",fileCachePath)) {}
if (fileCachePath.empty())
{
const char* env_fileCachePath = getenv("OSG_FILE_CACHE");
if (env_fileCachePath)
{
fileCachePath = env_fileCachePath;
}
}
if (fileCachePath.empty())
{
std::cout<<"No path to the file cache defined, please set OSG_FILE_PATH env var, or use --file-cache <directory> to set a suitable directory for the file cache."<<std::endl;
return 1;
}
ldv.setFileCache(new osgDB::FileCache(fileCachePath));
unsigned int maxLevels = 0;
while(arguments.read("-l",maxLevels))
{
ldv.addExtents(maxLevels);
}
double minX, maxX, minY, maxY;
while(arguments.read("-e",maxLevels, minX, minY, maxX, maxY))
{
ldv.addExtents(maxLevels, minX, minY, maxX, maxY);
}
std::string filename;
for(int i=1; i<arguments.argc(); ++i)
{
if (!arguments.isOption(i))
{
filename = arguments[i];
break;
}
}
if (filename.empty())
{
std::cout<<"No file to load specified."<<std::endl;
return 1;
}
osg::ref_ptr<osg::Node> loadedModel = ldv.readNodeFileAndWriteToCache(filename);
if (!loadedModel)
{
std::cout<<"No data loaded, please specify a database to load"<<std::endl;
return 1;
}
loadedModel->accept(ldv);
if (s_ExitApplication)
{
std::cout<<"osgfilecache cleanly exited in response to signal."<<std::endl;
}
return 0;
}