2 // by Durk Talsma, started June 2005.
4 // Copyright (C) 2004 Durk Talsma.
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <simgear/compiler.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/misc/sgstream.hxx>
34 #include <simgear/route/waypoint.hxx>
43 /**************************************************************************
45 *************************************************************************/
50 bool FGNode::matches(string id, double lt, double ln)
53 (fabs(lt - lat) < 1.0) &&
54 (fabs(ln - lon) < 1.0))
60 /***************************************************************************
62 **************************************************************************/
68 void FGAirway::setStart(node_map *nodes)
70 node_map_iterator itr = nodes->find(startNode);
71 if (itr == nodes->end()) {
72 cerr << "Couldn't find node: " << startNode << endl;
75 start = itr->second->getAddress();
76 itr->second->addAirway(this);
80 void FGAirway::setEnd(node_map *nodes)
82 node_map_iterator itr = nodes->find(endNode);
83 if (itr == nodes->end()) {
84 cerr << "Couldn't find node: " << endNode << endl;
87 end = itr->second->getAddress();
91 // There is probably a computationally cheaper way of
93 void FGAirway::setTrackDistance()
96 SGWayPoint first (start->getLongitude(),
99 SGWayPoint second (end->getLongitude(),
102 first.CourseAndDistance(second, &course, &length);
106 /***************************************************************************
108 **************************************************************************/
111 bool FGAirRoute::next(int *val)
113 //for (intVecIterator i = nodes.begin(); i != nodes.end(); i++)
114 // cerr << "FGTaxiRoute contains : " << *(i) << endl;
115 //cerr << "Offset from end: " << nodes.end() - currNode << endl;
116 //if (currNode != nodes.end())
117 // cerr << "true" << endl;
119 // cerr << "false" << endl;
121 if (currNode == nodes.end())
128 void FGAirRoute::add(const FGAirRoute &other) {
129 for (constIntVecIterator i = other.nodes.begin() ;
130 i != other.nodes.end(); i++)
132 nodes.push_back((*i));
134 distance += other.distance;
136 /***************************************************************************
138 **************************************************************************/
140 FGAirwayNetwork::FGAirwayNetwork()
148 FGAirwayNetwork::~FGAirwayNetwork()
150 for (unsigned int it = 0; it < nodes.size(); it++) {
154 void FGAirwayNetwork::addAirway(const FGAirway &seg)
156 segments.push_back(seg);
159 //void FGAirwayNetwork::addNode(const FGNode &node)
161 // nodes.push_back(node);
165 void FGAirwayNetwork::addNodes(FGParkingVec *parkings)
168 FGParkingVecIterator i = parkings->begin();
169 while (i != parkings->end())
171 n.setIndex(i->getIndex());
172 n.setLatitude(i->getLatitude());
173 n.setLongitude(i->getLongitude());
182 void FGAirwayNetwork::init()
185 FGAirwayVectorIterator i = segments.begin();
186 while(i != segments.end()) {
187 //cerr << "initializing Airway " << i->getIndex() << endl;
188 i->setStart(&nodesMap);
189 i->setEnd (&nodesMap);
190 //i->setTrackDistance();
191 //cerr << "Track distance = " << i->getLength() << endl;
192 //cerr << "Track ends at" << i->getEnd()->getIndex() << endl;
199 void FGAirwayNetwork::load(SGPath path)
201 string identStart, identEnd, token, name;
202 double latStart, lonStart, latEnd, lonEnd;
207 sg_gzifstream in( path.str() );
208 if ( !in.is_open() ) {
209 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
212 // toss the first two lines of the file
216 // read in each remaining line of the file
217 while ( ! in.eof() ) {
221 if ( token == "99" ) {
222 return; //in >> skipeol;
224 // Read each line from the database
226 in >> latStart >> lonStart >> identEnd >> latEnd >> lonEnd >> type >> base >> top >> name;
228 /*out << identStart << " "
239 //first determine whether the start and end reference database already exist
240 //if not we should create them
241 int startIndex = 0, endIndex=0;
242 // FGNodeVectorIterator i = nodes.begin();
243 // while (i != nodes.end() && (!(i->matches(identStart,latStart, lonStart))))
248 // if (i == nodes.end())
250 // nodes.push_back(FGNode(latStart, lonStart, startIndex, identStart));
251 // //cout << "Adding node: " << identStart << endl;
254 // i = nodes.begin();
255 // while (i != nodes.end() && (!(i->matches(identEnd,latEnd, lonEnd)))) {
259 // if (i == nodes.end()) {
260 // nodes.push_back(FGNode(latEnd, lonEnd, endIndex, identEnd));
261 // //cout << "Adding node: " << identEnd << endl;
263 // generate unique IDs for the nodes, consisting of a combination
264 // of the Navaid identifier + the integer value of the lat/lon position.
265 // identifier alone will not suffice, because they might not be globally unique.
267 string startNode, endNode;
269 buffer[sizeof(buffer)-1] = 0;
270 snprintf(buffer, sizeof(buffer)-1, "%s%d%d", identStart.c_str(), (int) latStart, (int) lonStart);
273 node_map_iterator itr = nodesMap.find(string(buffer));
274 if (itr == nodesMap.end()) {
275 startIndex = nodes.size();
276 n = new FGNode(latStart, lonStart, startIndex, identStart);
277 nodesMap[string(buffer)] = n;
279 //cout << "Adding node: " << identStart << endl;
282 startIndex = itr->second->getIndex();
285 snprintf(buffer, 32, "%s%d%d", identEnd.c_str(), (int) latEnd, (int) lonEnd);
288 itr = nodesMap.find(string(buffer));
289 if (itr == nodesMap.end()) {
290 endIndex = nodes.size();
291 n = new FGNode(latEnd, lonEnd, endIndex, identEnd);
292 nodesMap[string(buffer)] = n;
294 //cout << "Adding node: " << identEnd << endl;
297 endIndex = itr->second->getIndex();
302 airway.setIndex ( airwayIndex++ );
303 airway.setStartNodeRef ( startNode );
304 airway.setEndNodeRef ( endNode );
305 airway.setType ( type );
306 airway.setBase ( base );
307 airway.setTop ( top );
308 airway.setName ( name );
309 segments.push_back(airway);
310 //cout << " Adding Airway: " << name << " " << startIndex << " " << endIndex << endl;
314 int FGAirwayNetwork::findNearestNode(double lat, double lon)
316 double minDist = HUGE_VAL;
317 double distsqrt, lat2, lon2;
319 SGWayPoint first (lon,
322 //cerr << "Lat " << lat << " lon " << lon << endl;
323 for (FGNodeVectorIterator
325 itr != nodes.end(); itr++)
328 //if ((fabs(lat - ((*itr)->getLatitude())) < 0.001) &&
329 // (fabs(lon - ((*itr)->getLongitude()) < 0.001)))
330 //cerr << "Warning: nodes are near" << endl;
331 //SGWayPoint second ((*itr)->getLongitude(),
332 // (*itr)->getLatitude(),
334 //first.CourseAndDistance(second, &course, &dist);
335 lat2 = (*itr)->getLatitude();
336 lon2 = (*itr)->getLongitude();
337 // Note: This equation should adjust for decreasing distance per longitude
338 // with increasing lat.
340 (lat-lat2)*(lat-lat2) +
341 (lon-lon2)*(lon-lon2);
343 if (distsqrt < minDist)
346 //cerr << "Test" << endl;
347 index = (*itr)->getIndex();
348 //cerr << "Minimum distance of " << minDist << " for index " << index << endl;
349 //cerr << (*itr)->getLatitude() << " " << (*itr)->getLongitude() << endl;
351 //cerr << (*itr)->getIndex() << endl;
353 //cerr << " returning " << index << endl;
357 FGNode *FGAirwayNetwork::findNode(int idx)
359 for (FGNodeVectorIterator
361 itr != nodes.end(); itr++)
363 if ((*itr)->getIndex() == idx)
364 return (*itr)->getAddress();
369 FGAirRoute FGAirwayNetwork::findShortestRoute(int start, int end)
373 FGNode *firstNode = findNode(start);
374 FGNode *lastNode = findNode(end);
375 //prevNode = prevPrevNode = -1;
379 trace(firstNode, end, 0, 0);
384 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find route from waypoint " << start << " to " << end );
385 cerr << "Failed to find route from waypoint " << start << " to " << end << endl;
388 sort(routes.begin(), routes.end());
389 //for (intVecIterator i = route.begin(); i != route.end(); i++)
391 // rte->push_back(*i);
394 if (routes.begin() != routes.end())
395 return *(routes.begin());
401 void FGAirwayNetwork::trace(FGNode *currNode, int end, int depth, double distance)
403 traceStack.push_back(currNode->getIndex());
404 totalDistance += distance;
405 //cerr << depth << " ";
406 //cerr << "Starting trace " << depth << " total distance: " << totalDistance<< endl;
407 //<< currNode->getIndex() << endl;
409 // If the current route matches the required end point we found a valid route
410 // So we can add this to the routing table
411 if (currNode->getIndex() == end)
413 cerr << "Found route : " << totalDistance << "" << " " << *(traceStack.end()-1) << endl;
414 routes.push_back(FGAirRoute(traceStack,totalDistance));
415 traceStack.pop_back();
417 maxDistance = totalDistance;
419 if (totalDistance < maxDistance)
420 maxDistance = totalDistance;
422 totalDistance -= distance;
427 // search if the currentNode has been encountered before
428 // if so, we should step back one level, because it is
429 // rather rediculous to proceed further from here.
430 // if the current node has not been encountered before,
431 // i should point to traceStack.end()-1; and we can continue
432 // if i is not traceStack.end, the previous node was found,
433 // and we should return.
434 // This only works at trace levels of 1 or higher though
436 intVecIterator i = traceStack.begin();
437 while ((*i) != currNode->getIndex()) {
438 //cerr << "Route so far : " << (*i) << endl;
441 if (i != traceStack.end()-1) {
442 traceStack.pop_back();
443 totalDistance -= distance;
446 // If the total distance from start to the current waypoint
447 // is longer than that of a route we can also stop this trace
448 // and go back one level.
449 if ((totalDistance > maxDistance) && foundRoute)
451 cerr << "Stopping rediculously long trace: " << totalDistance << endl;
452 traceStack.pop_back();
453 totalDistance -= distance;
458 //cerr << "2" << endl;
459 if (currNode->getBeginRoute() != currNode->getEndRoute())
461 //cerr << "l3l" << endl;
462 for (FGAirwayPointerVectorIterator
463 i = currNode->getBeginRoute();
464 i != currNode->getEndRoute();
467 //cerr << (*i)->getLenght() << endl;
468 trace((*i)->getEnd(), end, depth+1, (*i)->getLength());
470 // // cerr << currNode -> getIndex() << " ";
471 // route.push_back(currNode->getIndex());
478 //SG_LOG( SG_GENERAL, SG_DEBUG, "4" );
479 //cerr << "4" << endl;
481 traceStack.pop_back();
482 totalDistance -= distance;