2023-05-20 22:36:08 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileName: fgelev.cxx
|
|
|
|
* SPDX-FileComment: compute scenery elevation
|
|
|
|
* SPDX-FileCopyrightText: Copyright (C) 2009 - 2012 Mathias Froehlich
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2012-08-18 11:00:08 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
#include <osg/ArgumentParser>
|
2012-08-25 10:04:50 +00:00
|
|
|
#include <osg/Image>
|
2012-08-18 11:00:08 +00:00
|
|
|
|
|
|
|
#include <simgear/props/props.hxx>
|
|
|
|
#include <simgear/props/props_io.hxx>
|
2012-08-24 19:20:44 +00:00
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2012-08-18 11:00:08 +00:00
|
|
|
#include <simgear/misc/ResourceManager.hxx>
|
2012-08-24 19:20:44 +00:00
|
|
|
#include <simgear/bvh/BVHNode.hxx>
|
|
|
|
#include <simgear/bvh/BVHLineSegmentVisitor.hxx>
|
2012-08-24 19:20:44 +00:00
|
|
|
#include <simgear/bvh/BVHPager.hxx>
|
|
|
|
#include <simgear/bvh/BVHPageNode.hxx>
|
2016-11-08 09:42:48 +00:00
|
|
|
#include <simgear/bvh/BVHMaterial.hxx>
|
2012-08-24 19:20:44 +00:00
|
|
|
#include <simgear/scene/material/matlib.hxx>
|
|
|
|
#include <simgear/scene/model/BVHPageNodeOSG.hxx>
|
|
|
|
#include <simgear/scene/model/ModelRegistry.hxx>
|
|
|
|
#include <simgear/scene/util/SGReaderWriterOptions.hxx>
|
2012-08-25 10:04:50 +00:00
|
|
|
#include <simgear/scene/util/OptionsReadFileCallback.hxx>
|
2020-11-15 21:47:18 +00:00
|
|
|
#include <simgear/scene/util/SGSceneFeatures.hxx>
|
2012-08-24 19:20:44 +00:00
|
|
|
#include <simgear/scene/tgdb/userdata.hxx>
|
|
|
|
|
|
|
|
namespace sg = simgear;
|
2012-08-18 11:00:08 +00:00
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
class Visitor : public sg::BVHLineSegmentVisitor {
|
2012-08-18 11:00:08 +00:00
|
|
|
public:
|
2012-08-24 19:20:44 +00:00
|
|
|
Visitor(const SGLineSegmentd& lineSegment, sg::BVHPager& pager) :
|
|
|
|
BVHLineSegmentVisitor(lineSegment, 0),
|
|
|
|
_pager(pager)
|
2012-08-18 11:00:08 +00:00
|
|
|
{ }
|
2012-08-24 19:20:44 +00:00
|
|
|
virtual ~Visitor()
|
|
|
|
{ }
|
|
|
|
virtual void apply(sg::BVHPageNode& node)
|
2012-08-18 11:00:08 +00:00
|
|
|
{
|
2012-08-24 19:20:44 +00:00
|
|
|
// we have a non threaded pager so load just right here.
|
|
|
|
_pager.use(node);
|
|
|
|
BVHLineSegmentVisitor::apply(node);
|
2012-08-18 11:00:08 +00:00
|
|
|
}
|
|
|
|
private:
|
2012-08-24 19:20:44 +00:00
|
|
|
sg::BVHPager& _pager;
|
2012-08-18 11:00:08 +00:00
|
|
|
};
|
|
|
|
|
2012-08-25 10:04:50 +00:00
|
|
|
// Short circuit reading image files.
|
|
|
|
class ReadFileCallback : public sg::OptionsReadFileCallback {
|
|
|
|
public:
|
|
|
|
virtual ~ReadFileCallback()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& name, const osgDB::Options*)
|
|
|
|
{ return new osg::Image; }
|
|
|
|
};
|
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
static bool
|
|
|
|
intersect(sg::BVHNode& node, sg::BVHPager& pager,
|
2016-11-08 09:42:48 +00:00
|
|
|
const SGVec3d& start, SGVec3d& end, double offset, const simgear::BVHMaterial** material)
|
2012-08-24 19:20:44 +00:00
|
|
|
{
|
|
|
|
SGVec3d perp = offset*perpendicular(start - end);
|
|
|
|
Visitor visitor(SGLineSegmentd(start + perp, end + perp), pager);
|
|
|
|
node.accept(visitor);
|
|
|
|
if (visitor.empty())
|
|
|
|
return false;
|
|
|
|
end = visitor.getLineSegment().getEnd();
|
2016-11-08 09:42:48 +00:00
|
|
|
* material = visitor.getMaterial();
|
2012-08-24 19:20:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-18 11:00:08 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
/// Read arguments and environment variables.
|
|
|
|
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
2014-10-06 09:23:27 +00:00
|
|
|
unsigned expire;
|
|
|
|
if (arguments.read("--expire", expire)) {
|
|
|
|
} else expire = 10;
|
2016-11-08 09:42:48 +00:00
|
|
|
|
|
|
|
bool printSolidness = arguments.read("--print-solidness");
|
2017-01-23 19:17:50 +00:00
|
|
|
|
2012-08-18 11:00:08 +00:00
|
|
|
std::string fg_root;
|
|
|
|
if (arguments.read("--fg-root", fg_root)) {
|
|
|
|
} else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
|
|
|
|
fg_root = fg_root_env;
|
|
|
|
} else {
|
|
|
|
fg_root = PKGLIBDIR;
|
|
|
|
}
|
|
|
|
|
2016-06-23 13:26:34 +00:00
|
|
|
SGPath fg_scenery;
|
|
|
|
std::string s;
|
|
|
|
if (arguments.read("--fg-scenery", s)) {
|
|
|
|
fg_scenery = SGPath::fromLocal8Bit(s.c_str());
|
|
|
|
} else if (std::getenv("FG_SCENERY")) {
|
|
|
|
fg_scenery = SGPath::fromEnv("FG_SCENERY");
|
2012-08-18 11:00:08 +00:00
|
|
|
} else {
|
|
|
|
SGPath path(fg_root);
|
|
|
|
path.append("Scenery");
|
2016-06-23 13:26:34 +00:00
|
|
|
fg_scenery = path;
|
2012-08-18 11:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
|
|
|
|
try {
|
|
|
|
SGPath preferencesFile = fg_root;
|
2017-01-11 17:58:22 +00:00
|
|
|
preferencesFile.append("defaults.xml");
|
2016-06-23 13:26:34 +00:00
|
|
|
readProperties(preferencesFile, props);
|
2012-08-18 11:00:08 +00:00
|
|
|
} catch (...) {
|
|
|
|
// In case of an error, at least make summer :)
|
|
|
|
props->getNode("sim/startup/season", true)->setStringValue("summer");
|
|
|
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set.");
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:47:18 +00:00
|
|
|
bool use_vpb = arguments.read("--use-vpb");
|
|
|
|
props->setBoolValue("/scenery/use-vpb", use_vpb);
|
|
|
|
SGSceneFeatures::instance()->setVPBActive(use_vpb);
|
|
|
|
|
2012-08-18 11:00:08 +00:00
|
|
|
/// now set up the simgears required model stuff
|
|
|
|
|
|
|
|
simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
|
|
|
|
// Just reference simgears reader writer stuff so that the globals get
|
|
|
|
// pulled in by the linker ...
|
|
|
|
simgear::ModelRegistry::instance();
|
|
|
|
|
|
|
|
sgUserDataInit(props.get());
|
2014-01-19 16:49:32 +00:00
|
|
|
SGMaterialLibPtr ml = new SGMaterialLib;
|
2012-08-18 11:00:08 +00:00
|
|
|
SGPath mpath(fg_root);
|
|
|
|
mpath.append("Materials/default/materials.xml");
|
|
|
|
try {
|
2016-06-23 13:26:34 +00:00
|
|
|
ml->load(fg_root, mpath.local8BitStr(), props);
|
2012-08-18 11:00:08 +00:00
|
|
|
} catch (...) {
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
|
|
|
|
<< "Probably FG_ROOT is not properly set.");
|
|
|
|
}
|
|
|
|
simgear::SGModelLib::init(fg_root, props);
|
|
|
|
|
|
|
|
// Set up the reader/writer options
|
|
|
|
osg::ref_ptr<simgear::SGReaderWriterOptions> options;
|
|
|
|
if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
|
|
|
|
options = new simgear::SGReaderWriterOptions(*ropt);
|
|
|
|
else
|
|
|
|
options = new simgear::SGReaderWriterOptions;
|
2016-06-23 13:26:34 +00:00
|
|
|
osgDB::convertStringPathIntoFilePathList(fg_scenery.local8BitStr(),
|
2012-08-18 11:00:08 +00:00
|
|
|
options->getDatabasePathList());
|
|
|
|
options->setMaterialLib(ml);
|
|
|
|
options->setPropertyNode(props);
|
2012-08-25 10:04:50 +00:00
|
|
|
options->setReadFileCallback(new ReadFileCallback);
|
2012-08-18 11:00:08 +00:00
|
|
|
options->setPluginStringData("SimGear::FG_ROOT", fg_root);
|
2012-08-24 19:20:44 +00:00
|
|
|
// we do not need the builtin boundingvolumes
|
|
|
|
options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
|
2012-08-25 06:43:25 +00:00
|
|
|
// And we only want terrain, no objects on top.
|
|
|
|
options->setPluginStringData("SimGear::FG_ONLY_TERRAIN", "ON");
|
2017-01-23 19:17:50 +00:00
|
|
|
|
|
|
|
string_list scenerySuffixes = {"Terrain"}; // Just Terrain
|
|
|
|
options->setSceneryPathSuffixes(scenerySuffixes);
|
|
|
|
|
2012-08-25 06:43:25 +00:00
|
|
|
props->getNode("sim/rendering/random-objects", true)->setBoolValue(false);
|
|
|
|
props->getNode("sim/rendering/random-vegetation", true)->setBoolValue(false);
|
2012-08-18 11:00:08 +00:00
|
|
|
|
2020-11-15 21:47:18 +00:00
|
|
|
std::string bvhFile = "w180s90-360x180.spt";
|
|
|
|
if (arguments.read("--tile-file", s))
|
|
|
|
bvhFile = s;
|
|
|
|
|
2012-08-18 11:00:08 +00:00
|
|
|
// Here, all arguments are processed
|
|
|
|
arguments.reportRemainingOptionsAsUnrecognized();
|
|
|
|
arguments.writeErrorMessages(std::cerr);
|
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
// Get the whole world bvh tree
|
|
|
|
SGSharedPtr<sg::BVHNode> node;
|
2023-02-23 19:50:43 +00:00
|
|
|
node = sg::BVHPageNodeOSG::load(bvhFile, options, use_vpb);
|
2012-08-18 11:00:08 +00:00
|
|
|
|
|
|
|
// if no model has been successfully loaded report failure.
|
2012-08-24 19:20:44 +00:00
|
|
|
if (!node.valid()) {
|
2012-08-18 11:00:08 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
|
|
|
|
<< ": No data loaded");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
// We assume that the above is a paged database.
|
|
|
|
sg::BVHPager pager;
|
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
while (std::cin.good()) {
|
2012-08-24 19:20:44 +00:00
|
|
|
// Increment the paging relevant number
|
|
|
|
pager.setUseStamp(1 + pager.getUseStamp());
|
|
|
|
// and expire everything not accessed for the past 30 requests
|
2014-10-06 09:23:27 +00:00
|
|
|
pager.update(expire);
|
2012-08-24 19:20:44 +00:00
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
std::string id;
|
|
|
|
std::cin >> id;
|
2012-08-18 11:00:08 +00:00
|
|
|
double lon, lat;
|
2012-08-24 19:20:44 +00:00
|
|
|
std::cin >> lon >> lat;
|
|
|
|
if (std::cin.fail())
|
2012-08-18 11:00:08 +00:00
|
|
|
return EXIT_FAILURE;
|
2012-08-24 19:20:44 +00:00
|
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
2012-08-18 11:00:08 +00:00
|
|
|
|
|
|
|
SGVec3d start = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, 10000));
|
|
|
|
SGVec3d end = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, -1000));
|
2012-08-24 19:20:44 +00:00
|
|
|
|
2016-11-08 09:42:48 +00:00
|
|
|
const simgear::BVHMaterial* material = NULL;
|
2012-08-24 19:20:44 +00:00
|
|
|
// Try to find an intersection
|
2016-11-08 09:42:48 +00:00
|
|
|
bool found = intersect(*node, pager, start, end, 0, &material);
|
2012-08-24 19:20:44 +00:00
|
|
|
double scale = 1e-5;
|
|
|
|
while (!found && scale <= 1) {
|
2016-11-08 09:42:48 +00:00
|
|
|
found = intersect(*node, pager, start, end, scale, &material);
|
2012-08-24 19:20:44 +00:00
|
|
|
scale *= 2;
|
|
|
|
}
|
|
|
|
if (1e-5 < scale)
|
|
|
|
std::cerr << "Found hole of minimum diameter "
|
|
|
|
<< scale << "m at lon = " << lon
|
|
|
|
<< "deg lat = " << lat << "deg" << std::endl;
|
2012-08-18 11:00:08 +00:00
|
|
|
|
2012-08-24 19:20:44 +00:00
|
|
|
std::cout << id << ": ";
|
2023-05-20 22:36:08 +00:00
|
|
|
std::string solid = material && material->get_solid() ? "solid" : "-";
|
2012-08-24 19:20:44 +00:00
|
|
|
if (!found) {
|
2012-08-18 11:00:08 +00:00
|
|
|
std::cout << "-1000" << std::endl;
|
|
|
|
} else {
|
2012-08-24 19:20:44 +00:00
|
|
|
SGGeod geod = SGGeod::fromCart(end);
|
2016-11-08 09:42:48 +00:00
|
|
|
std::cout << std::fixed << std::setprecision(3) << geod.getElevationM();
|
|
|
|
if( printSolidness )
|
|
|
|
std::cout << " " << (material && material->get_solid() ? "solid" : "-");
|
|
|
|
std::cout << std::endl;
|
2012-08-18 11:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|