/usr/share/openscenegraph/examples/osggpx/osggpx.cpp is in openscenegraph-examples 3.2.1-7ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | /* OpenSceneGraph example, osggpx.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Notify>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
#include <osg/DrawPixels>
#include <osg/PolygonOffset>
#include <osg/Geode>
#include <osg/CoordinateSystemNode>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgDB/XmlParser>
#include <osgText/Text>
#include <osgGA/TerrainManipulator>
#include <osgViewer/Viewer>
class TrackSegment : public osg::Object
{
public:
TrackSegment() {}
TrackSegment(const TrackSegment& ts, const osg::CopyOp=osg::CopyOp::SHALLOW_COPY) {}
META_Object(osg, TrackSegment)
struct TrackPoint
{
TrackPoint():
latitude(0.0),
longitude(0.0),
elevation(0.0),
time(0.0) {}
double latitude;
double longitude;
double elevation;
double time;
};
typedef std::vector< TrackPoint > TrackPoints;
void addTrackPoint(const TrackPoint& trackPoint) { _trackPoints.push_back(trackPoint); }
TrackPoints& getTrackPoints() { return _trackPoints; }
const TrackPoints& getTrackPoints() const { return _trackPoints; }
protected:
virtual ~TrackSegment() {}
TrackPoints _trackPoints;
};
class Track : public osg::Object
{
public:
Track() {}
Track(const Track& track, const osg::CopyOp=osg::CopyOp::SHALLOW_COPY) {}
META_Object(osg, Track)
typedef std::vector< osg::ref_ptr<TrackSegment> > TrackSegments;
void addTrackSegment(TrackSegment* trackSegment) { _trackSegments.push_back(trackSegment); }
TrackSegments& getTrackSegments() { return _trackSegments; }
const TrackSegments& getTrackSegments() const { return _trackSegments; }
protected:
virtual ~Track() {}
TrackSegments _trackSegments;
};
double convertTime(const std::string& timestr)
{
osg::notify(osg::NOTICE)<<" time = "<<timestr<<std::endl;
return 0;
}
Track* readTrack(const std::string& filename)
{
std::string foundFilename = osgDB::findDataFile(filename);
if (foundFilename.empty()) return 0;
std::string ext = osgDB::getFileExtension(foundFilename);
if (ext!="gpx") return 0;
osgDB::XmlNode::Input input;
input.open(foundFilename);
input.readAllDataIntoBuffer();
osg::ref_ptr<osgDB::XmlNode> doc = new osgDB::XmlNode;
doc->read(input);
osgDB::XmlNode* root = 0;
for(osgDB::XmlNode::Children::iterator itr = doc->children.begin();
itr != doc->children.end() && !root;
++itr)
{
if ((*itr)->name=="gpx") root = itr->get();
}
if (!root) return 0;
std::string latitude("lat");
std::string longitude("lon");
for(osgDB::XmlNode::Children::iterator itr = root->children.begin();
itr != root->children.end();
++itr)
{
if ((*itr)->name=="rte")
{
osg::ref_ptr<Track> track = new Track;
track->setName(filename);
osg::ref_ptr<TrackSegment> trackSegment = new TrackSegment;
for(osgDB::XmlNode::Children::iterator sitr = (*itr)->children.begin();
sitr != (*itr)->children.end();
++sitr)
{
if ((*sitr)->name=="rtept" )
{
osgDB::XmlNode* trkpt = sitr->get();
TrackSegment::TrackPoint point;
bool valid = false;
if (trkpt->properties.count(latitude)!=0)
{
valid = true;
point.latitude = osg::asciiToDouble(trkpt->properties[latitude].c_str());
}
if (trkpt->properties.count(longitude)!=0)
{
valid = true;
point.longitude = osg::asciiToDouble(trkpt->properties[longitude].c_str());
}
for(osgDB::XmlNode::Children::iterator pitr = trkpt->children.begin();
pitr != trkpt->children.end();
++pitr)
{
if ((*pitr)->name=="ele") point.elevation = osg::asciiToDouble((*pitr)->contents.c_str());
else if ((*pitr)->name=="time") point.time = convertTime((*pitr)->contents);
}
if (valid)
{
osg::notify(osg::NOTICE)<<" point.latitude="<<point.latitude<<", longitude="<<point.longitude<<", elev="<<point.elevation<<", time="<<point.time<<std::endl;
trackSegment->addTrackPoint(point);
}
}
}
if (!trackSegment->getTrackPoints().empty())
{
track->addTrackSegment(trackSegment.get());
}
return track.release();
}
else if ((*itr)->name=="trk")
{
osg::ref_ptr<Track> track = new Track;
track->setName(filename);
for(osgDB::XmlNode::Children::iterator citr = (*itr)->children.begin();
citr != (*itr)->children.end();
++citr)
{
if ((*citr)->name=="trkseg")
{
osg::ref_ptr<TrackSegment> trackSegment = new TrackSegment;
for(osgDB::XmlNode::Children::iterator sitr = (*citr)->children.begin();
sitr != (*citr)->children.end();
++sitr)
{
if ((*sitr)->name=="trkpt" || (*sitr)->name=="rtept" )
{
osgDB::XmlNode* trkpt = sitr->get();
TrackSegment::TrackPoint point;
bool valid = false;
if (trkpt->properties.count(latitude)!=0)
{
valid = true;
point.latitude = osg::asciiToDouble(trkpt->properties[latitude].c_str());
}
if (trkpt->properties.count(longitude)!=0)
{
valid = true;
point.longitude = osg::asciiToDouble(trkpt->properties[longitude].c_str());
}
for(osgDB::XmlNode::Children::iterator pitr = trkpt->children.begin();
pitr != trkpt->children.end();
++pitr)
{
if ((*pitr)->name=="ele") point.elevation = osg::asciiToDouble((*pitr)->contents.c_str());
else if ((*pitr)->name=="time") point.time = convertTime((*pitr)->contents);
}
if (valid)
{
// osg::notify(osg::NOTICE)<<" point.latitude="<<point.latitude<<", longitude="<<point.longitude<<", elev="<<point.elevation<<", time="<<point.time<<std::endl;
trackSegment->addTrackPoint(point);
}
}
}
if (!trackSegment->getTrackPoints().empty())
{
track->addTrackSegment(trackSegment.get());
}
}
}
return track.release();
}
}
return 0;
}
TrackSegment* computeSmoothedTrackSegment(TrackSegment* ts)
{
if (!ts) return 0;
const TrackSegment::TrackPoints& orig_points = ts->getTrackPoints();
if (orig_points.size()>2)
{
// only do smoothing if we have more than two points.
osg::ref_ptr<TrackSegment> new_ts = new TrackSegment;
TrackSegment::TrackPoints& new_points = new_ts->getTrackPoints();
new_points.resize(orig_points.size());
new_points[0] = orig_points[0];
new_points[orig_points.size()-1] = orig_points[orig_points.size()-1];
for(unsigned int i=1; i<orig_points.size()-1; ++i)
{
new_points[i].latitude = (orig_points[i-1].latitude+orig_points[i].latitude+orig_points[i+1].latitude)/3.0;
new_points[i].longitude = (orig_points[i-1].longitude+orig_points[i].longitude+orig_points[i+1].longitude)/3.0;
new_points[i].elevation = (orig_points[i-1].elevation+orig_points[i].elevation+orig_points[i+1].elevation)/3.0;
new_points[i].time = (orig_points[i-1].time+orig_points[i].time+orig_points[i+1].time)/3.0;
}
return new_ts.release();
}
else
{
// we have two or less points and can't do smoothing, so will just return original TrackSegment
return ts;
}
}
TrackSegment* computeAveragedSpeedTrackSegment(TrackSegment* ts)
{
if (!ts) return 0;
osg::ref_ptr<osg::EllipsoidModel> em = new osg::EllipsoidModel;
const TrackSegment::TrackPoints& orig_points = ts->getTrackPoints();
if (orig_points.size()>2)
{
// only do smoothing if we have more than two points.
osg::ref_ptr<TrackSegment> new_ts = new TrackSegment;
// compute overall distance
double total_distance = 0;
for(unsigned int i=1; i<orig_points.size()-1; ++i)
{
osg::Vec3d point_a, point_b;
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(orig_points[i].latitude), osg::DegreesToRadians(orig_points[i].longitude), orig_points[i].elevation,
point_a.x(), point_a.y(), point_a.z());
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(orig_points[i+1].latitude), osg::DegreesToRadians(orig_points[i+1].longitude), orig_points[i+1].elevation,
point_b.x(), point_b.y(), point_b.z());
total_distance += (point_b-point_a).length();
}
double total_time = orig_points[orig_points.size()-1].time - orig_points[0].time;
double average_speed = total_distance/total_time;
OSG_NOTICE<<"total_time = "<<total_time<<std::endl;
OSG_NOTICE<<"total_distance = "<<total_distance<<std::endl;
OSG_NOTICE<<"average_speed = "<<average_speed<<std::endl;
TrackSegment::TrackPoints& new_points = new_ts->getTrackPoints();
new_points.resize(orig_points.size());
new_points[0] = orig_points[0];
double accumulated_distance = 0.0;
for(unsigned int i=0; i<orig_points.size()-1; ++i)
{
osg::Vec3d point_a, point_b;
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(orig_points[i].latitude), osg::DegreesToRadians(orig_points[i].longitude), orig_points[i].elevation,
point_a.x(), point_a.y(), point_a.z());
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(orig_points[i+1].latitude), osg::DegreesToRadians(orig_points[i+1].longitude), orig_points[i+1].elevation,
point_b.x(), point_b.y(), point_b.z());
accumulated_distance += (point_b-point_a).length();
new_points[i+1] = orig_points[i+1];
new_points[i+1].time = accumulated_distance / average_speed;
}
return new_ts.release();
}
else
{
// we have two or less points and can't do smoothing, so will just return original TrackSegment
return ts;
}
}
Track* computeAveragedSpeedTrack(Track* track)
{
osg::ref_ptr<Track> new_track = new Track;
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
new_track->addTrackSegment(computeAveragedSpeedTrackSegment(itr->get()));
}
return new_track.release();
}
Track* computeSmoothedTrack(Track* track)
{
osg::ref_ptr<Track> new_track = new Track;
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
new_track->addTrackSegment(computeSmoothedTrackSegment(itr->get()));
}
return new_track.release();
}
osg::Node* createTrackModel(Track* track, const osg::Vec4& colour)
{
osg::ref_ptr<osg::EllipsoidModel> em = new osg::EllipsoidModel;
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
const TrackSegment::TrackPoints& points = (*itr)->getTrackPoints();
if (points.size()<2) continue;
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
geometry->setVertexArray(vertices.get());
vertices->resize(points.size());
for(unsigned int i=0; i<points.size(); ++i)
{
osg::Vec3d point;
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(points[i].latitude), osg::DegreesToRadians(points[i].longitude), points[i].elevation,
point.x(), point.y(), point.z());
(*vertices)[i] = point;
}
osg::ref_ptr<osg::Vec4Array> colours = new osg::Vec4Array;
colours->push_back(colour);
geometry->setColorArray(colours.get(), osg::Array::BIND_OVERALL);
geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP, 0, points.size()));
geode->addDrawable(geometry.get());
}
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
return geode.release();
}
int main(int argv, char **argc)
{
osg::ArgumentParser arguments(&argv, argc);
typedef std::list< osg::ref_ptr<Track> > Tracks;
Tracks tracks;
bool average = false;
while (arguments.read("-a") || arguments.read("--average")) average = true;
bool smooth = false;
while (arguments.read("-s") || arguments.read("--smooth")) smooth = true;
std::string outputFilename;
while (arguments.read("-o",outputFilename)) {}
std::string trackFilename;
while (arguments.read("-t",trackFilename))
{
osg::ref_ptr<Track> track = readTrack(trackFilename);
if (track.valid()) tracks.push_back(track.get());
}
osg::ref_ptr<osg::EllipsoidModel> em = new osg::EllipsoidModel;
osg::ref_ptr<osg::Group> group = new osg::Group;
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
if (loadedModel.valid()) group->addChild(loadedModel.get());
for(Tracks::iterator itr = tracks.begin();
itr != tracks.end();
++itr)
{
Track* track = itr->get();
group->addChild(createTrackModel(track, osg::Vec4(1.0,1.0,1.0,1.0)));
// smooth the track
if (average)
{
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
*itr = computeAveragedSpeedTrackSegment(itr->get());
}
}
// smooth the track
if (smooth)
{
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
*itr = computeSmoothedTrackSegment(itr->get());
}
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
*itr = computeSmoothedTrackSegment(itr->get());
}
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
*itr = computeSmoothedTrackSegment(itr->get());
}
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
*itr = computeSmoothedTrackSegment(itr->get());
}
}
double totalDistance = 0.0;
double totalAscent = 0.0;
double totalDescent = 0.0;
osg::notify(osg::NOTICE)<<"Track read "<<track->getName()<<std::endl;
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
TrackSegment* ts = itr->get();
const TrackSegment::TrackPoints& points = ts->getTrackPoints();
if (points.size()>1)
{
TrackSegment::TrackPoints::iterator pitr = ts->getTrackPoints().begin();
osg::Vec3d previousPos;
double previousElevation = pitr->elevation;
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(pitr->latitude), osg::DegreesToRadians(pitr->longitude), 0.0,
previousPos.x(), previousPos.y(), previousPos.z());
++pitr;
for(;
pitr != ts->getTrackPoints().end();
++pitr)
{
osg::Vec3d newPos;
double newElevation = pitr->elevation;
em->convertLatLongHeightToXYZ(osg::DegreesToRadians(pitr->latitude), osg::DegreesToRadians(pitr->longitude), 0.0,
newPos.x(), newPos.y(), newPos.z());
double distance = (newPos-previousPos).length();
totalDistance += distance;
if (newElevation>previousElevation) totalAscent += (newElevation-previousElevation);
else totalDescent += (previousElevation-newElevation);
osg::notify(osg::NOTICE)<<" distance="<<distance<<", "<<newElevation-previousElevation<<std::endl;
previousPos = newPos;
previousElevation = newElevation;
}
}
}
double metersToFeet = 1 / 0.3048;
double metersToMiles = 1.0 / 1609.344;
osg::notify(osg::NOTICE)<<"totalDistance = "<<totalDistance<<"m, "<<totalDistance*metersToMiles<<" miles"<<std::endl;
osg::notify(osg::NOTICE)<<"totalAscent = "<<totalAscent<<"m, "<<totalAscent*metersToFeet<<"ft"<<std::endl;
osg::notify(osg::NOTICE)<<"totalDescent = "<<totalDescent<<"m, "<<totalDescent*metersToFeet<<"ft"<<std::endl;
}
if (!outputFilename.empty())
{
std::ofstream fout(outputFilename.c_str());
fout<<"<?xml version=\"1.0\" encoding=\"utf-8\"?><gpx version=\"1.0\" creator=\"osggpx\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">"<<std::endl;
for(Tracks::iterator itr = tracks.begin();
itr != tracks.end();
++itr)
{
Track* track = itr->get();
fout<<"<trk>"<<std::endl;
fout<<"<desc>The track description</desc>"<<std::endl;
for(Track::TrackSegments::iterator itr = track->getTrackSegments().begin();
itr != track->getTrackSegments().end();
++itr)
{
TrackSegment* ts = itr->get();
fout<<"<trkseg>"<<std::endl;
for(TrackSegment::TrackPoints::iterator pitr = ts->getTrackPoints().begin();
pitr != ts->getTrackPoints().end();
++pitr)
{
fout<<"<trkpt lat=\""<<pitr->latitude<<"\" lon=\""<<pitr->longitude<<"\">"<<std::endl;
fout<<"<ele>"<<pitr->elevation<<"</ele>"<<std::endl;
fout<<"<time>"<<pitr->time<<"</time>"<<std::endl;
fout<<"</trkpt>"<<std::endl;
}
fout<<"</trkseg>"<<std::endl;
}
fout<<"</trk>"<<std::endl;
}
fout<<"</gpx>"<<std::endl;
}
osgViewer::Viewer viewer(arguments);
viewer.setCameraManipulator(new osgGA::TerrainManipulator);
viewer.setSceneData(group.get());
return viewer.run();
}
|