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.
29 #include <simgear/compiler.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sgstream.hxx>
33 #include <simgear/route/waypoint.hxx>
39 /**************************************************************************
41 *************************************************************************/
46 bool FGNode::matches(string id, double lt, double ln)
49 (fabs(lt - lat) < 1.0) &&
50 (fabs(ln - lon) < 1.0))
56 /***************************************************************************
58 **************************************************************************/
64 void FGAirway::setStart(node_map *nodes)
66 node_map_iterator itr = nodes->find(startNode);
67 if (itr == nodes->end()) {
68 cerr << "Couldn't find node: " << startNode << endl;
71 start = itr->second->getAddress();
72 itr->second->addAirway(this);
76 void FGAirway::setEnd(node_map *nodes)
78 node_map_iterator itr = nodes->find(endNode);
79 if (itr == nodes->end()) {
80 cerr << "Couldn't find node: " << endNode << endl;
83 end = itr->second->getAddress();
87 // There is probably a computationally cheaper way of
89 void FGAirway::setTrackDistance()
92 SGWayPoint first (start->getLongitude(),
95 SGWayPoint second (end->getLongitude(),
98 first.CourseAndDistance(second, &course, &length);
102 /***************************************************************************
104 **************************************************************************/
107 bool FGAirRoute::next(int *val)
109 //for (intVecIterator i = nodes.begin(); i != nodes.end(); i++)
110 // cerr << "FGTaxiRoute contains : " << *(i) << endl;
111 //cerr << "Offset from end: " << nodes.end() - currNode << endl;
112 //if (currNode != nodes.end())
113 // cerr << "true" << endl;
115 // cerr << "false" << endl;
117 if (currNode == nodes.end())
124 void FGAirRoute::add(const FGAirRoute &other) {
125 for (constIntVecIterator i = other.nodes.begin() ;
126 i != other.nodes.end(); i++)
128 nodes.push_back((*i));
130 distance += other.distance;
132 /***************************************************************************
134 **************************************************************************/
136 FGAirwayNetwork::FGAirwayNetwork()
144 void FGAirwayNetwork::addAirway(const FGAirway &seg)
146 segments.push_back(seg);
149 //void FGAirwayNetwork::addNode(const FGNode &node)
151 // nodes.push_back(node);
155 void FGAirwayNetwork::addNodes(FGParkingVec *parkings)
158 FGParkingVecIterator i = parkings->begin();
159 while (i != parkings->end())
161 n.setIndex(i->getIndex());
162 n.setLatitude(i->getLatitude());
163 n.setLongitude(i->getLongitude());
172 void FGAirwayNetwork::init()
175 FGAirwayVectorIterator i = segments.begin();
176 while(i != segments.end()) {
177 //cerr << "initializing Airway " << i->getIndex() << endl;
178 i->setStart(&nodesMap);
179 i->setEnd (&nodesMap);
180 //i->setTrackDistance();
181 //cerr << "Track distance = " << i->getLength() << endl;
182 //cerr << "Track ends at" << i->getEnd()->getIndex() << endl;
189 void FGAirwayNetwork::load(SGPath path)
191 string identStart, identEnd, token, name;
192 double latStart, lonStart, latEnd, lonEnd;
197 sg_gzifstream in( path.str() );
198 if ( !in.is_open() ) {
199 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
202 // toss the first two lines of the file
206 // read in each remaining line of the file
210 while ( in.get(c) && c != '\0' ) {
213 while ( ! in.eof() ) {
218 if ( token == "99" ) {
219 return; //in >> skipeol;
221 // Read each line from the database
223 in >> latStart >> lonStart >> identEnd >> latEnd >> lonEnd >> type >> base >> top >> name;
224 /*out << identStart << " "
235 //first determine whether the start and end reference database already exist
236 //if not we should create them
237 int startIndex = 0, endIndex=0;
238 // FGNodeVectorIterator i = nodes.begin();
239 // while (i != nodes.end() && (!(i->matches(identStart,latStart, lonStart))))
244 // if (i == nodes.end())
246 // nodes.push_back(FGNode(latStart, lonStart, startIndex, identStart));
247 // //cout << "Adding node: " << identStart << endl;
250 // i = nodes.begin();
251 // while (i != nodes.end() && (!(i->matches(identEnd,latEnd, lonEnd)))) {
255 // if (i == nodes.end()) {
256 // nodes.push_back(FGNode(latEnd, lonEnd, endIndex, identEnd));
257 // //cout << "Adding node: " << identEnd << endl;
259 // generate unique IDs for the nodes, consisting of a combination
260 // of the Navaid identifier + the integer value of the lat/lon position.
261 // identifier alone will not suffice, because they might not be globally unique.
263 string startNode, endNode;
265 snprintf(buffer, 32, "%s%d%d", identStart.c_str(), (int) latStart, (int) lonStart);
268 node_map_iterator itr = nodesMap.find(string(buffer));
269 if (itr == nodesMap.end()) {
270 startIndex = nodes.size();
271 n = new FGNode(latStart, lonStart, startIndex, identStart);
272 nodesMap[string(buffer)] = n;
274 //cout << "Adding node: " << identStart << endl;
277 startIndex = itr->second->getIndex();
280 snprintf(buffer, 32, "%s%d%d", identEnd.c_str(), (int) latEnd, (int) lonEnd);
283 itr = nodesMap.find(string(buffer));
284 if (itr == nodesMap.end()) {
285 endIndex = nodes.size();
286 n = new FGNode(latEnd, lonEnd, endIndex, identEnd);
287 nodesMap[string(buffer)] = n;
289 //cout << "Adding node: " << identEnd << endl;
292 endIndex = itr->second->getIndex();
297 airway.setIndex ( airwayIndex++ );
298 airway.setStartNodeRef ( startNode );
299 airway.setEndNodeRef ( endNode );
300 airway.setType ( type );
301 airway.setBase ( base );
302 airway.setTop ( top );
303 airway.setName ( name );
304 segments.push_back(airway);
305 //cout << " Adding Airway: " << name << " " << startIndex << " " << endIndex << endl;
309 int FGAirwayNetwork::findNearestNode(double lat, double lon)
311 double minDist = HUGE_VAL;
312 double distsqrt, lat2, lon2;
314 SGWayPoint first (lon,
317 //cerr << "Lat " << lat << " lon " << lon << endl;
318 for (FGNodeVectorIterator
320 itr != nodes.end(); itr++)
323 //if ((fabs(lat - ((*itr)->getLatitude())) < 0.001) &&
324 // (fabs(lon - ((*itr)->getLongitude()) < 0.001)))
325 //cerr << "Warning: nodes are near" << endl;
326 //SGWayPoint second ((*itr)->getLongitude(),
327 // (*itr)->getLatitude(),
329 //first.CourseAndDistance(second, &course, &dist);
330 lat2 = (*itr)->getLatitude();
331 lon2 = (*itr)->getLongitude();
332 // Note: This equation should adjust for decreasing distance per longitude
333 // with increasing lat.
335 (lat-lat2)*(lat-lat2) +
336 (lon-lon2)*(lon-lon2);
338 if (distsqrt < minDist)
341 //cerr << "Test" << endl;
342 index = (*itr)->getIndex();
343 //cerr << "Minimum distance of " << minDist << " for index " << index << endl;
344 //cerr << (*itr)->getLatitude() << " " << (*itr)->getLongitude() << endl;
346 //cerr << (*itr)->getIndex() << endl;
348 //cerr << " returning " << index << endl;
352 FGNode *FGAirwayNetwork::findNode(int idx)
354 for (FGNodeVectorIterator
356 itr != nodes.end(); itr++)
358 if ((*itr)->getIndex() == idx)
359 return (*itr)->getAddress();
364 FGAirRoute FGAirwayNetwork::findShortestRoute(int start, int end)
368 FGNode *firstNode = findNode(start);
369 FGNode *lastNode = findNode(end);
370 //prevNode = prevPrevNode = -1;
374 trace(firstNode, end, 0, 0);
379 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find route from waypoint " << start << " to " << end );
380 cerr << "Failed to find route from waypoint " << start << " to " << end << endl;
383 sort(routes.begin(), routes.end());
384 //for (intVecIterator i = route.begin(); i != route.end(); i++)
386 // rte->push_back(*i);
389 if (routes.begin() != routes.end())
390 return *(routes.begin());
396 void FGAirwayNetwork::trace(FGNode *currNode, int end, int depth, double distance)
398 traceStack.push_back(currNode->getIndex());
399 totalDistance += distance;
400 //cerr << depth << " ";
401 //cerr << "Starting trace " << depth << " total distance: " << totalDistance<< endl;
402 //<< currNode->getIndex() << endl;
404 // If the current route matches the required end point we found a valid route
405 // So we can add this to the routing table
406 if (currNode->getIndex() == end)
408 cerr << "Found route : " << totalDistance << "" << " " << *(traceStack.end()-1) << endl;
409 routes.push_back(FGAirRoute(traceStack,totalDistance));
410 traceStack.pop_back();
412 maxDistance = totalDistance;
414 if (totalDistance < maxDistance)
415 maxDistance = totalDistance;
417 totalDistance -= distance;
422 // search if the currentNode has been encountered before
423 // if so, we should step back one level, because it is
424 // rather rediculous to proceed further from here.
425 // if the current node has not been encountered before,
426 // i should point to traceStack.end()-1; and we can continue
427 // if i is not traceStack.end, the previous node was found,
428 // and we should return.
429 // This only works at trace levels of 1 or higher though
431 intVecIterator i = traceStack.begin();
432 while ((*i) != currNode->getIndex()) {
433 //cerr << "Route so far : " << (*i) << endl;
436 if (i != traceStack.end()-1) {
437 traceStack.pop_back();
438 totalDistance -= distance;
441 // If the total distance from start to the current waypoint
442 // is longer than that of a route we can also stop this trace
443 // and go back one level.
444 if ((totalDistance > maxDistance) && foundRoute)
446 cerr << "Stopping rediculously long trace: " << totalDistance << endl;
447 traceStack.pop_back();
448 totalDistance -= distance;
453 //cerr << "2" << endl;
454 if (currNode->getBeginRoute() != currNode->getEndRoute())
456 //cerr << "l3l" << endl;
457 for (FGAirwayPointerVectorIterator
458 i = currNode->getBeginRoute();
459 i != currNode->getEndRoute();
462 //cerr << (*i)->getLenght() << endl;
463 trace((*i)->getEnd(), end, depth+1, (*i)->getLength());
465 // // cerr << currNode -> getIndex() << " ";
466 // route.push_back(currNode->getIndex());
473 //SG_LOG( SG_GENERAL, SG_DEBUG, "4" );
474 //cerr << "4" << endl;
476 traceStack.pop_back();
477 totalDistance -= distance;