Debian Codegraph @

utils/fgelev/fgelev.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: fgelev.cxx
3 * SPDX-FileComment: compute scenery elevation
4 * SPDX-FileCopyrightText: Copyright (C) 2009 - 2012 Mathias Froehlich
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <iostream>
13 #include <cstdlib>
14 #include <iomanip>
15
16 #include <osg/ArgumentParser>
17 #include <osg/Image>
18
19 #include <simgear/props/props.hxx>
20 #include <simgear/props/props_io.hxx>
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/misc/ResourceManager.hxx>
23 #include <simgear/bvh/BVHNode.hxx>
24 #include <simgear/bvh/BVHLineSegmentVisitor.hxx>
25 #include <simgear/bvh/BVHPager.hxx>
26 #include <simgear/bvh/BVHPageNode.hxx>
27 #include <simgear/bvh/BVHMaterial.hxx>
28 #include <simgear/scene/material/matlib.hxx>
29 #include <simgear/scene/model/BVHPageNodeOSG.hxx>
30 #include <simgear/scene/model/ModelRegistry.hxx>
31 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
32 #include <simgear/scene/util/OptionsReadFileCallback.hxx>
33 #include <simgear/scene/util/SGSceneFeatures.hxx>
34 #include <simgear/scene/tgdb/userdata.hxx>
35
36 namespace sg = simgear;
37
38 class Visitor : public sg::BVHLineSegmentVisitor {
39 public:
40 Visitor(const SGLineSegmentd& lineSegment, sg::BVHPager& pager) :
41 BVHLineSegmentVisitor(lineSegment, 0),
42 _pager(pager)
43 { }
44 virtual ~Visitor()
45 { }
46 virtual void apply(sg::BVHPageNode& node)
47 {
48 // we have a non threaded pager so load just right here.
49 _pager.use(node);
50 BVHLineSegmentVisitor::apply(node);
51 }
52 private:
53 sg::BVHPager& _pager;
54 };
55
56 // Short circuit reading image files.
57 class ReadFileCallback : public sg::OptionsReadFileCallback {
58 public:
59 virtual ~ReadFileCallback()
60 { }
61
62 virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& name, const osgDB::Options*)
63 { return new osg::Image; }
64 };
65
66 static bool
67 intersect(sg::BVHNode& node, sg::BVHPager& pager,
68 const SGVec3d& start, SGVec3d& end, double offset, const simgear::BVHMaterial** material)
69 {
70 SGVec3d perp = offset*perpendicular(start - end);
71 Visitor visitor(SGLineSegmentd(start + perp, end + perp), pager);
72 node.accept(visitor);
73 if (visitor.empty())
74 return false;
75 end = visitor.getLineSegment().getEnd();
76 * material = visitor.getMaterial();
77 return true;
78 }
79
80 int
81 main(int argc, char** argv)
82 {
83 /// Read arguments and environment variables.
84
85 // use an ArgumentParser object to manage the program arguments.
86 osg::ArgumentParser arguments(&argc, argv);
87
88 if (arguments.read("--help")) {
89 std::cout << "Usage: fgelev [Options...]\n\nQuery the elevation of lon/lat points in the scenery." << std::endl;;
90 std::cout << "Input is read from STDIN line by line in the format <id> <lon> <lat>. Output is to STDOUT in the format <id>: <elevation-m> [solid|-]" << std::endl << std::endl;
91 std::cout << "Optional arguments:" << std::endl;
92 std::cout << "\t[--expire N]\t\tExpire items in the cache after N requests (default 10). Use to control memory occupancy." << std::endl;
93 std::cout << "\t[--print-solidness]\tOutput whether the location is on solid ground (\"solid\") or not (\"-\")." << std::endl;
94 std::cout << "\t[--fg-root <path>]\tSet the FG data directory. Used to retrieve terrain material information. Default to $FG_ROOT." << std::endl;
95 std::cout << "\t[--fg-scenery <path>]\tSet the scenery directory. Defaults to $FG_SCENERY, or $FG_ROOT/Scenery if $FG_SCENERY not set." << std::endl;
96 std::cout << "\t[--use-vpb]\t\tUse VirtualPlanetBuilder (aka WS3.0)." << std::endl;
97 std::cout << "\t[--tile-file <file>]\t\tSet the VPB (WS3.0) tile to read." << std::endl;
98 return EXIT_FAILURE;
99 }
100
101 unsigned expire;
102 if (arguments.read("--expire", expire)) {
103 } else expire = 10;
104
105 bool printSolidness = arguments.read("--print-solidness");
106
107 std::string fg_root;
108 if (arguments.read("--fg-root", fg_root)) {
109 } else if (const char *fg_root_env = std::getenv("FG_ROOT")) {
110 fg_root = fg_root_env;
111 } else {
112 fg_root = PKGLIBDIR;
113 }
114
115 SGPath fg_scenery;
116 std::string s;
117 if (arguments.read("--fg-scenery", s)) {
118 fg_scenery = SGPath::fromLocal8Bit(s.c_str());
119 } else if (std::getenv("FG_SCENERY")) {
120 fg_scenery = SGPath::fromEnv("FG_SCENERY");
121 } else {
122 SGPath path(fg_root);
123 path.append("Scenery");
124 fg_scenery = path;
125 }
126
127 SGSharedPtr<SGPropertyNode> props = new SGPropertyNode;
128 try {
129 SGPath preferencesFile = fg_root;
130 preferencesFile.append("defaults.xml");
131 readProperties(preferencesFile, props);
132 } catch (...) {
133 // In case of an error, at least make summer :)
134 props->getNode("sim/startup/season", true)->setStringValue("summer");
135
136 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear preferences.\n"
137 << "Probably FG_ROOT is not properly set.");
138 }
139
140 bool use_vpb = arguments.read("--use-vpb");
141 props->setBoolValue("/scenery/use-vpb", use_vpb);
142 SGSceneFeatures::instance()->setVPBActive(use_vpb);
143
144 /// now set up the simgears required model stuff
145
146 simgear::ResourceManager::instance()->addBasePath(fg_root, simgear::ResourceManager::PRIORITY_DEFAULT);
147 // Just reference simgears reader writer stuff so that the globals get
148 // pulled in by the linker ...
149 simgear::ModelRegistry::instance();
150
151 sgUserDataInit(props.get());
152 SGMaterialLibPtr ml = new SGMaterialLib;
153 SGPath mpath(fg_root);
154 mpath.append("Materials/default/materials.xml");
155 try {
156 ml->load(fg_root, mpath.local8BitStr(), props);
157 } catch (...) {
158 SG_LOG(SG_GENERAL, SG_ALERT, "Problems loading FlightGear materials.\n"
159 << "Probably FG_ROOT is not properly set.");
160 }
161 simgear::SGModelLib::init(fg_root, props);
162
163 // Set up the reader/writer options
164 osg::ref_ptr<simgear::SGReaderWriterOptions> options;
165 if (osgDB::Options* ropt = osgDB::Registry::instance()->getOptions())
166 options = new simgear::SGReaderWriterOptions(*ropt);
167 else
168 options = new simgear::SGReaderWriterOptions;
169 osgDB::convertStringPathIntoFilePathList(fg_scenery.local8BitStr(),
170 options->getDatabasePathList());
171 options->setMaterialLib(ml);
172 options->setPropertyNode(props);
173 options->setReadFileCallback(new ReadFileCallback);
174 options->setPluginStringData("SimGear::FG_ROOT", fg_root);
175 // we do not need the builtin boundingvolumes
176 options->setPluginStringData("SimGear::BOUNDINGVOLUMES", "OFF");
177 // And we only want terrain, no objects on top.
178 options->setPluginStringData("SimGear::FG_ONLY_TERRAIN", "ON");
179
180 string_list scenerySuffixes = {"Terrain"}; // Just Terrain
181 options->setSceneryPathSuffixes(scenerySuffixes);
182
183 props->getNode("sim/rendering/random-objects", true)->setBoolValue(false);
184 props->getNode("sim/rendering/random-vegetation", true)->setBoolValue(false);
185
186 std::string bvhFile = "w180s90-360x180.spt";
187 if (arguments.read("--tile-file", s))
188 bvhFile = s;
189
190 // Here, all arguments are processed
191 arguments.reportRemainingOptionsAsUnrecognized();
192 arguments.writeErrorMessages(std::cerr);
193
194 // Get the whole world bvh tree
195 SGSharedPtr<sg::BVHNode> node;
196 node = sg::BVHPageNodeOSG::load(bvhFile, options, use_vpb);
197
198 // if no model has been successfully loaded report failure.
199 if (!node.valid()) {
200 SG_LOG(SG_GENERAL, SG_ALERT, arguments.getApplicationName()
201 << ": No data loaded");
202 return EXIT_FAILURE;
203 }
204
205 // We assume that the above is a paged database.
206 sg::BVHPager pager;
207
208 while (std::cin.good()) {
209 // Increment the paging relevant number
210 pager.setUseStamp(1 + pager.getUseStamp());
211 // and expire everything not accessed for the past 30 requests
212 pager.update(expire);
213
214 std::string id;
215 std::cin >> id;
216 double lon, lat;
217 std::cin >> lon >> lat;
218 if (std::cin.fail())
219 return EXIT_FAILURE;
220 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
221
222 SGVec3d start = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, 10000));
223 SGVec3d end = SGVec3d::fromGeod(SGGeod::fromDegM(lon, lat, -1000));
224
225 const simgear::BVHMaterial* material = NULL;
226 // Try to find an intersection
227 bool found = intersect(*node, pager, start, end, 0, &material);
228 double scale = 1e-5;
229 while (!found && scale <= 1) {
230 found = intersect(*node, pager, start, end, scale, &material);
231 scale *= 2;
232 }
233 if (1e-5 < scale)
234 std::cerr << "Found hole of minimum diameter "
235 << scale << "m at lon = " << lon
236 << "deg lat = " << lat << "deg" << std::endl;
237
238 std::cout << id << ": ";
239 if (!found) {
240 std::cout << "-1000" << std::endl;
241 } else {
242 SGGeod geod = SGGeod::fromCart(end);
243 std::cout << std::fixed << std::setprecision(3) << geod.getElevationM();
244 if( printSolidness )
245 std::cout << " " << (material && material->get_solid() ? "solid" : "-");
246 std::cout << std::endl;
247 }
248 }
249
250 return EXIT_SUCCESS;
251 }