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 FGAirwayNetwork::~FGAirwayNetwork()
146 for (unsigned int it = 0; it < nodes.size(); it++) {
150 void FGAirwayNetwork::addAirway(const FGAirway &seg)
152 segments.push_back(seg);
155 //void FGAirwayNetwork::addNode(const FGNode &node)
157 // nodes.push_back(node);
161 void FGAirwayNetwork::addNodes(FGParkingVec *parkings)
164 FGParkingVecIterator i = parkings->begin();
165 while (i != parkings->end())
167 n.setIndex(i->getIndex());
168 n.setLatitude(i->getLatitude());
169 n.setLongitude(i->getLongitude());
178 void FGAirwayNetwork::init()
181 FGAirwayVectorIterator i = segments.begin();
182 while(i != segments.end()) {
183 //cerr << "initializing Airway " << i->getIndex() << endl;
184 i->setStart(&nodesMap);
185 i->setEnd (&nodesMap);
186 //i->setTrackDistance();
187 //cerr << "Track distance = " << i->getLength() << endl;
188 //cerr << "Track ends at" << i->getEnd()->getIndex() << endl;
195 void FGAirwayNetwork::load(SGPath path)
197 string identStart, identEnd, token, name;
198 double latStart, lonStart, latEnd, lonEnd;
203 sg_gzifstream in( path.str() );
204 if ( !in.is_open() ) {
205 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
208 // toss the first two lines of the file
212 // read in each remaining line of the file
216 while ( in.get(c) && c != '\0' ) {
219 while ( ! in.eof() ) {
224 if ( token == "99" ) {
225 return; //in >> skipeol;
227 // Read each line from the database
229 in >> latStart >> lonStart >> identEnd >> latEnd >> lonEnd >> type >> base >> top >> name;
230 /*out << identStart << " "
241 //first determine whether the start and end reference database already exist
242 //if not we should create them
243 int startIndex = 0, endIndex=0;
244 // FGNodeVectorIterator i = nodes.begin();
245 // while (i != nodes.end() && (!(i->matches(identStart,latStart, lonStart))))
250 // if (i == nodes.end())
252 // nodes.push_back(FGNode(latStart, lonStart, startIndex, identStart));
253 // //cout << "Adding node: " << identStart << endl;
256 // i = nodes.begin();
257 // while (i != nodes.end() && (!(i->matches(identEnd,latEnd, lonEnd)))) {
261 // if (i == nodes.end()) {
262 // nodes.push_back(FGNode(latEnd, lonEnd, endIndex, identEnd));
263 // //cout << "Adding node: " << identEnd << endl;
265 // generate unique IDs for the nodes, consisting of a combination
266 // of the Navaid identifier + the integer value of the lat/lon position.
267 // identifier alone will not suffice, because they might not be globally unique.
269 string startNode, endNode;
271 buffer[sizeof(buffer)-1] = 0;
272 snprintf(buffer, sizeof(buffer)-1, "%s%d%d", identStart.c_str(), (int) latStart, (int) lonStart);
275 node_map_iterator itr = nodesMap.find(string(buffer));
276 if (itr == nodesMap.end()) {
277 startIndex = nodes.size();
278 n = new FGNode(latStart, lonStart, startIndex, identStart);
279 nodesMap[string(buffer)] = n;
281 //cout << "Adding node: " << identStart << endl;
284 startIndex = itr->second->getIndex();
287 snprintf(buffer, 32, "%s%d%d", identEnd.c_str(), (int) latEnd, (int) lonEnd);
290 itr = nodesMap.find(string(buffer));
291 if (itr == nodesMap.end()) {
292 endIndex = nodes.size();
293 n = new FGNode(latEnd, lonEnd, endIndex, identEnd);
294 nodesMap[string(buffer)] = n;
296 //cout << "Adding node: " << identEnd << endl;
299 endIndex = itr->second->getIndex();
304 airway.setIndex ( airwayIndex++ );
305 airway.setStartNodeRef ( startNode );
306 airway.setEndNodeRef ( endNode );
307 airway.setType ( type );
308 airway.setBase ( base );
309 airway.setTop ( top );
310 airway.setName ( name );
311 segments.push_back(airway);
312 //cout << " Adding Airway: " << name << " " << startIndex << " " << endIndex << endl;
316 int FGAirwayNetwork::findNearestNode(double lat, double lon)
318 double minDist = HUGE_VAL;
319 double distsqrt, lat2, lon2;
321 SGWayPoint first (lon,
324 //cerr << "Lat " << lat << " lon " << lon << endl;
325 for (FGNodeVectorIterator
327 itr != nodes.end(); itr++)
330 //if ((fabs(lat - ((*itr)->getLatitude())) < 0.001) &&
331 // (fabs(lon - ((*itr)->getLongitude()) < 0.001)))
332 //cerr << "Warning: nodes are near" << endl;
333 //SGWayPoint second ((*itr)->getLongitude(),
334 // (*itr)->getLatitude(),
336 //first.CourseAndDistance(second, &course, &dist);
337 lat2 = (*itr)->getLatitude();
338 lon2 = (*itr)->getLongitude();
339 // Note: This equation should adjust for decreasing distance per longitude
340 // with increasing lat.
342 (lat-lat2)*(lat-lat2) +
343 (lon-lon2)*(lon-lon2);
345 if (distsqrt < minDist)
348 //cerr << "Test" << endl;
349 index = (*itr)->getIndex();
350 //cerr << "Minimum distance of " << minDist << " for index " << index << endl;
351 //cerr << (*itr)->getLatitude() << " " << (*itr)->getLongitude() << endl;
353 //cerr << (*itr)->getIndex() << endl;
355 //cerr << " returning " << index << endl;
359 FGNode *FGAirwayNetwork::findNode(int idx)
361 for (FGNodeVectorIterator
363 itr != nodes.end(); itr++)
365 if ((*itr)->getIndex() == idx)
366 return (*itr)->getAddress();
371 FGAirRoute FGAirwayNetwork::findShortestRoute(int start, int end)
375 FGNode *firstNode = findNode(start);
376 FGNode *lastNode = findNode(end);
377 //prevNode = prevPrevNode = -1;
381 trace(firstNode, end, 0, 0);
386 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find route from waypoint " << start << " to " << end );
387 cerr << "Failed to find route from waypoint " << start << " to " << end << endl;
390 sort(routes.begin(), routes.end());
391 //for (intVecIterator i = route.begin(); i != route.end(); i++)
393 // rte->push_back(*i);
396 if (routes.begin() != routes.end())
397 return *(routes.begin());
403 void FGAirwayNetwork::trace(FGNode *currNode, int end, int depth, double distance)
405 traceStack.push_back(currNode->getIndex());
406 totalDistance += distance;
407 //cerr << depth << " ";
408 //cerr << "Starting trace " << depth << " total distance: " << totalDistance<< endl;
409 //<< currNode->getIndex() << endl;
411 // If the current route matches the required end point we found a valid route
412 // So we can add this to the routing table
413 if (currNode->getIndex() == end)
415 cerr << "Found route : " << totalDistance << "" << " " << *(traceStack.end()-1) << endl;
416 routes.push_back(FGAirRoute(traceStack,totalDistance));
417 traceStack.pop_back();
419 maxDistance = totalDistance;
421 if (totalDistance < maxDistance)
422 maxDistance = totalDistance;
424 totalDistance -= distance;
429 // search if the currentNode has been encountered before
430 // if so, we should step back one level, because it is
431 // rather rediculous to proceed further from here.
432 // if the current node has not been encountered before,
433 // i should point to traceStack.end()-1; and we can continue
434 // if i is not traceStack.end, the previous node was found,
435 // and we should return.
436 // This only works at trace levels of 1 or higher though
438 intVecIterator i = traceStack.begin();
439 while ((*i) != currNode->getIndex()) {
440 //cerr << "Route so far : " << (*i) << endl;
443 if (i != traceStack.end()-1) {
444 traceStack.pop_back();
445 totalDistance -= distance;
448 // If the total distance from start to the current waypoint
449 // is longer than that of a route we can also stop this trace
450 // and go back one level.
451 if ((totalDistance > maxDistance) && foundRoute)
453 cerr << "Stopping rediculously long trace: " << totalDistance << endl;
454 traceStack.pop_back();
455 totalDistance -= distance;
460 //cerr << "2" << endl;
461 if (currNode->getBeginRoute() != currNode->getEndRoute())
463 //cerr << "l3l" << endl;
464 for (FGAirwayPointerVectorIterator
465 i = currNode->getBeginRoute();
466 i != currNode->getEndRoute();
469 //cerr << (*i)->getLenght() << endl;
470 trace((*i)->getEnd(), end, depth+1, (*i)->getLength());
472 // // cerr << currNode -> getIndex() << " ";
473 // route.push_back(currNode->getIndex());
480 //SG_LOG( SG_GENERAL, SG_DEBUG, "4" );
481 //cerr << "4" << endl;
483 traceStack.pop_back();
484 totalDistance -= distance;