From Ken Sewel, added osgDB::fileType(filename) method and usage of this
new function in osgdem to automatcally handle directories as part of the regular -d and -t options.
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <osgDB/WriteFile>
|
||||
#include <osgDB/ImageOptions>
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/FileUtils>
|
||||
|
||||
#include <osgUtil/Optimizer>
|
||||
#include <osgUtil/TriStripVisitor>
|
||||
@@ -108,6 +109,56 @@ void ellipsodeTransformTest(double latitude, double longitude, double height)
|
||||
std::cout<<"lat = "<<osg::RadiansToDegrees(newLat)<<"\tlong="<<osg::RadiansToDegrees(newLong)<<"\t"<<newHeight<<std::endl;
|
||||
}
|
||||
|
||||
void processFile(std::string filename,
|
||||
osgTerrain::DataSet::Source::Type type,
|
||||
std::string currentCS,
|
||||
osg::Matrixd &geoTransform,
|
||||
bool geoTransformSet,
|
||||
bool geoTransformScale,
|
||||
osg::ref_ptr<osgTerrain::DataSet> dataset) {
|
||||
|
||||
if(filename.empty()) return;
|
||||
|
||||
if(osgDB::fileType(filename) == osgDB::REGULAR_FILE) {
|
||||
|
||||
osgTerrain::DataSet::Source* source = new osgTerrain::DataSet::Source(type, filename);
|
||||
if (source)
|
||||
{
|
||||
if (!currentCS.empty())
|
||||
{
|
||||
std::cout<<"source->setCoordySystem "<<currentCS<<std::endl;
|
||||
source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
|
||||
source->setCoordinateSystem(currentCS);
|
||||
}
|
||||
|
||||
if (geoTransformSet)
|
||||
{
|
||||
std::cout<<"source->setGeoTransform "<<geoTransform<<std::endl;
|
||||
source->setGeoTransformPolicy(geoTransformScale ?
|
||||
osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION :
|
||||
osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
|
||||
source->setGeoTransform(geoTransform);
|
||||
}
|
||||
|
||||
dataset->addSource(source);
|
||||
}
|
||||
} else if (osgDB::fileType(filename) == osgDB::DIRECTORY) {
|
||||
|
||||
osgDB::DirectoryContents dirContents= osgDB::getDirectoryContents(filename);
|
||||
|
||||
// loop through directory contents and call processFile
|
||||
std::vector<std::string>::iterator i;
|
||||
std::string fullfilename;
|
||||
for(i = dirContents.begin(); i != dirContents.end(); ++i) {
|
||||
if((*i != ".") && (*i != "..")) {
|
||||
fullfilename = filename + '/' + *i;
|
||||
processFile(fullfilename, type, currentCS, geoTransform, geoTransformSet, geoTransformScale, dataset);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
@@ -197,7 +248,6 @@ int main( int argc, char **argv )
|
||||
while(pos<arguments.argc())
|
||||
{
|
||||
std::string def;
|
||||
osgTerrain::DataSet::Source* source = 0;
|
||||
|
||||
if (arguments.read(pos, "--cs",def))
|
||||
{
|
||||
@@ -358,28 +408,18 @@ int main( int argc, char **argv )
|
||||
|
||||
else if (arguments.read(pos, "-d",filename))
|
||||
{
|
||||
if (!filename.empty())
|
||||
{
|
||||
std::cout<<"-d "<<filename<<std::endl;
|
||||
|
||||
source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::HEIGHT_FIELD,filename);
|
||||
}
|
||||
std::cout<<"-d "<<filename<<std::endl;
|
||||
processFile(filename, osgTerrain::DataSet::Source::HEIGHT_FIELD, currentCS, geoTransform, geoTransformSet, geoTransformScale, dataset);
|
||||
}
|
||||
else if (arguments.read(pos, "-t",filename))
|
||||
{
|
||||
if (!filename.empty())
|
||||
{
|
||||
std::cout<<"-t "<<filename<<std::endl;
|
||||
source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::IMAGE,filename);
|
||||
}
|
||||
std::cout<<"-t "<<filename<<std::endl;
|
||||
processFile(filename, osgTerrain::DataSet::Source::IMAGE, currentCS, geoTransform, geoTransformSet, geoTransformScale, dataset);
|
||||
}
|
||||
else if (arguments.read(pos, "-m",filename))
|
||||
{
|
||||
if (!filename.empty())
|
||||
{
|
||||
std::cout<<"-m "<<filename<<std::endl;
|
||||
source = new osgTerrain::DataSet::Source(osgTerrain::DataSet::Source::MODEL,filename);
|
||||
}
|
||||
std::cout<<"-m "<<filename<<std::endl;
|
||||
processFile(filename, osgTerrain::DataSet::Source::MODEL, currentCS, geoTransform, geoTransformSet, geoTransformScale, dataset);
|
||||
}
|
||||
else if (arguments.read(pos, "-o",filename))
|
||||
{
|
||||
@@ -400,29 +440,6 @@ int main( int argc, char **argv )
|
||||
// if no argument read advance to next argument.
|
||||
++pos;
|
||||
}
|
||||
|
||||
if (source)
|
||||
{
|
||||
if (!currentCS.empty())
|
||||
{
|
||||
std::cout<<"source->setCoordySystem "<<currentCS<<std::endl;
|
||||
source->setCoordinateSystemPolicy(osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
|
||||
source->setCoordinateSystem(currentCS);
|
||||
}
|
||||
|
||||
if (geoTransformSet)
|
||||
{
|
||||
std::cout<<"source->setGeoTransform "<<geoTransform<<std::endl;
|
||||
source->setGeoTransformPolicy(geoTransformScale ?
|
||||
osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS_BUT_SCALE_BY_FILE_RESOLUTION :
|
||||
osgTerrain::DataSet::Source::PREFER_CONFIG_SETTINGS);
|
||||
source->setGeoTransform(geoTransform);
|
||||
}
|
||||
|
||||
dataset->addSource(source);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// any option left unread are converted into errors to write out later.
|
||||
|
||||
@@ -29,10 +29,19 @@ enum CaseSensitivity
|
||||
CASE_INSENSITIVE,
|
||||
};
|
||||
|
||||
enum FileType
|
||||
{
|
||||
FILE_NOT_FOUND,
|
||||
REGULAR_FILE,
|
||||
DIRECTORY,
|
||||
};
|
||||
|
||||
|
||||
/** return true if a file exisits. */
|
||||
extern OSGDB_EXPORT bool fileExists(const std::string& filename);
|
||||
|
||||
/** return type of file. */
|
||||
extern OSGDB_EXPORT FileType fileType(const std::string& filename);
|
||||
|
||||
/** find specified file in specified file path.*/
|
||||
extern OSGDB_EXPORT std::string findFileInPath(const std::string& filename, const FilePathList& filePath,CaseSensitivity caseSensitivity=CASE_SENSITIVE);
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
#define F_OK 4
|
||||
#else // unix
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#include <osg/Notify>
|
||||
@@ -40,6 +42,40 @@ bool osgDB::fileExists(const std::string& filename)
|
||||
return access( filename.c_str(), F_OK ) == 0;
|
||||
}
|
||||
|
||||
osgDB::FileType osgDB::fileType(const std::string& filename)
|
||||
{
|
||||
#if defined(WIN32) && !defined(__CYGWIN__)
|
||||
if (!fileExists(const std::string& filename))
|
||||
{
|
||||
return FILE_NOT_FOUND;
|
||||
}
|
||||
else
|
||||
{
|
||||
return REGULAR_FILE;
|
||||
}
|
||||
|
||||
#else
|
||||
struct stat fileStat;
|
||||
|
||||
if(stat(filename.c_str(), &fileStat) == -1)
|
||||
{
|
||||
return FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
if(S_ISREG(fileStat.st_mode))
|
||||
{
|
||||
return REGULAR_FILE;
|
||||
}
|
||||
|
||||
if(S_ISDIR(fileStat.st_mode))
|
||||
{
|
||||
return DIRECTORY;
|
||||
}
|
||||
|
||||
return FILE_NOT_FOUND;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string osgDB::findFileInPath(const std::string& filename, const FilePathList& filepath,CaseSensitivity caseSensitivity)
|
||||
{
|
||||
if (filename.empty())
|
||||
|
||||
Reference in New Issue
Block a user