1 // groundnet.cxx - Implimentation of the FlightGear airport ground handling code
3 // Written by Durk Talsma, started June 2005.
5 // Copyright (C) 2004 Durk Talsma.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/compiler.h>
32 //#include <plib/sg.h>
33 //#include <plib/ul.h>
35 //#include <Environment/environment_mgr.hxx>
36 //#include <Environment/environment.hxx>
37 //#include <simgear/misc/sg_path.hxx>
38 //#include <simgear/props/props.hxx>
39 //#include <simgear/structure/subsystem_mgr.hxx>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/route/waypoint.hxx>
42 //#include <Main/globals.hxx>
43 //#include <Main/fg_props.hxx>
44 //#include <Airports/runways.hxx>
48 #include "groundnetwork.hxx"
52 /**************************************************************************
54 *************************************************************************/
55 FGTaxiNode::FGTaxiNode()
59 /***************************************************************************
61 **************************************************************************/
62 FGTaxiSegment::FGTaxiSegment()
66 void FGTaxiSegment::setStart(FGTaxiNodeVector *nodes)
68 FGTaxiNodeVectorIterator i = nodes->begin();
69 while (i != nodes->end())
71 if (i->getIndex() == startNode)
73 start = i->getAddress();
81 void FGTaxiSegment::setEnd(FGTaxiNodeVector *nodes)
83 FGTaxiNodeVectorIterator i = nodes->begin();
84 while (i != nodes->end())
86 if (i->getIndex() == endNode)
88 end = i->getAddress();
95 // There is probably a computationally cheaper way of
97 void FGTaxiSegment::setTrackDistance()
100 SGWayPoint first (start->getLongitude(),
101 start->getLatitude(),
103 SGWayPoint second (end->getLongitude(),
106 first.CourseAndDistance(second, &course, &length);
110 bool FGTaxiRoute::next(int *val)
112 //for (intVecIterator i = nodes.begin(); i != nodes.end(); i++)
113 // cerr << "FGTaxiRoute contains : " << *(i) << endl;
114 //cerr << "Offset from end: " << nodes.end() - currNode << endl;
115 //if (currNode != nodes.end())
116 // cerr << "true" << endl;
118 // cerr << "false" << endl;
120 if (currNode == nodes.end())
126 /***************************************************************************
128 **************************************************************************/
130 FGGroundNetwork::FGGroundNetwork()
138 void FGGroundNetwork::addSegment(const FGTaxiSegment &seg)
140 segments.push_back(seg);
143 void FGGroundNetwork::addNode(const FGTaxiNode &node)
145 nodes.push_back(node);
148 void FGGroundNetwork::addNodes(FGParkingVec *parkings)
151 FGParkingVecIterator i = parkings->begin();
152 while (i != parkings->end())
154 n.setIndex(i->getIndex());
155 n.setLatitude(i->getLatitude());
156 n.setLongitude(i->getLongitude());
165 void FGGroundNetwork::init()
168 FGTaxiSegmentVectorIterator i = segments.begin();
169 while(i != segments.end()) {
170 //cerr << "initializing node " << i->getIndex() << endl;
173 i->setTrackDistance();
174 //cerr << "Track distance = " << i->getLength() << endl;
175 //cerr << "Track ends at" << i->getEnd()->getIndex() << endl;
181 int FGGroundNetwork::findNearestNode(double lat, double lon)
183 double minDist = HUGE_VAL;
186 SGWayPoint first (lon,
190 for (FGTaxiNodeVectorIterator
192 itr != nodes.end(); itr++)
195 SGWayPoint second (itr->getLongitude(),
198 first.CourseAndDistance(second, &course, &dist);
202 index = itr->getIndex();
203 //cerr << "Minimum distance of " << minDist << " for index " << index << endl;
209 FGTaxiNode *FGGroundNetwork::findNode(int idx)
211 for (FGTaxiNodeVectorIterator
213 itr != nodes.end(); itr++)
215 if (itr->getIndex() == idx)
216 return itr->getAddress();
221 FGTaxiRoute FGGroundNetwork::findShortestRoute(int start, int end)
225 FGTaxiNode *firstNode = findNode(start);
226 FGTaxiNode *lastNode = findNode(end);
227 //prevNode = prevPrevNode = -1;
231 trace(firstNode, end, 0, 0);
236 SG_LOG( SG_GENERAL, SG_INFO, "Failed to find route from waypoint " << start << " to " << end );
239 sort(routes.begin(), routes.end());
240 //for (intVecIterator i = route.begin(); i != route.end(); i++)
242 // rte->push_back(*i);
245 if (routes.begin() != routes.end())
246 return *(routes.begin());
252 void FGGroundNetwork::trace(FGTaxiNode *currNode, int end, int depth, double distance)
254 traceStack.push_back(currNode->getIndex());
255 totalDistance += distance;
256 //cerr << "Starting trace " << depth << " total distance: " << totalDistance<< endl;
257 //<< currNode->getIndex() << endl;
259 // If the current route matches the required end point we found a valid route
260 // So we can add this to the routing table
261 if (currNode->getIndex() == end)
263 //cerr << "Found route : " << totalDistance << "" << " " << *(traceStack.end()-1) << endl;
264 routes.push_back(FGTaxiRoute(traceStack,totalDistance));
265 traceStack.pop_back();
267 maxDistance = totalDistance;
269 if (totalDistance < maxDistance)
270 maxDistance = totalDistance;
272 totalDistance -= distance;
277 // search if the currentNode has been encountered before
278 // if so, we should step back one level, because it is
279 // rather rediculous to proceed further from here.
280 // if the current node has not been encountered before,
281 // i should point to traceStack.end()-1; and we can continue
282 // if i is not traceStack.end, the previous node was found,
283 // and we should return.
284 // This only works at trace levels of 1 or higher though
286 intVecIterator i = traceStack.begin();
287 while ((*i) != currNode->getIndex()) {
288 //cerr << "Route so far : " << (*i) << endl;
291 if (i != traceStack.end()-1) {
292 traceStack.pop_back();
293 totalDistance -= distance;
296 // If the total distance from start to the current waypoint
297 // is longer than that of a route we can also stop this trace
298 // and go back one level.
299 if ((totalDistance > maxDistance) && foundRoute)
301 //cerr << "Stopping rediculously long trace: " << totalDistance << endl;
302 traceStack.pop_back();
303 totalDistance -= distance;
308 //cerr << "2" << endl;
309 if (currNode->getBeginRoute() != currNode->getEndRoute())
311 //cerr << "3" << endl;
312 for (FGTaxiSegmentPointerVectorIterator
313 i = currNode->getBeginRoute();
314 i != currNode->getEndRoute();
317 //cerr << (*i)->getLenght() << endl;
318 trace((*i)->getEnd(), end, depth+1, (*i)->getLength());
320 // // cerr << currNode -> getIndex() << " ";
321 // route.push_back(currNode->getIndex());
328 SG_LOG( SG_GENERAL, SG_DEBUG, "4" );
330 traceStack.pop_back();
331 totalDistance -= distance;