]> git.mxchange.org Git - flightgear.git/blob - src/Airports/groundnetwork.hxx
Implement a persistent cache for navigation data.
[flightgear.git] / src / Airports / groundnetwork.hxx
1 // groundnet.hxx - A number of classes to handle taxiway
2 // assignments by the AI code
3 //
4 // Written by Durk Talsma, started June 2005.
5 //
6 // Copyright (C) 2004 Durk Talsma.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifndef _GROUNDNETWORK_HXX_
25 #define _GROUNDNETWORK_HXX_
26
27 #include <osg/Geode>
28 #include <osg/Geometry>
29 #include <osg/MatrixTransform>
30 #include <osg/Shape>
31
32
33 #include <simgear/compiler.h>
34
35 #include <string>
36 #include <vector>
37 #include <list>
38
39 class Block;
40
41 #include "gnnode.hxx"
42 #include "parking.hxx"
43 #include <ATC/trafficcontrol.hxx>
44
45
46 class FGTaxiSegment; // forward reference
47 class FGAIFlightPlan; // forward reference
48 class FGAirport;      // forward reference
49
50 typedef vector<FGTaxiSegment*>  FGTaxiSegmentVector;
51 typedef vector<FGTaxiSegment*>::iterator FGTaxiSegmentVectorIterator;
52
53 //typedef vector<FGTaxiSegment*> FGTaxiSegmentPointerVector;
54 //typedef vector<FGTaxiSegment*>::iterator FGTaxiSegmentPointerVectorIterator;
55
56 class Block
57 {
58 private:
59     int id;
60     time_t blocktime;
61     time_t touch;
62 public:
63     Block(int i, time_t bt, time_t curr) { id = i; blocktime= bt; touch = curr; };
64     ~Block() {};
65     int getId() { return id; };
66     void updateTimeStamps(time_t bt, time_t now) { blocktime = (bt < blocktime) ? bt : blocktime; touch = now; };
67     const time_t getBlockTime() const { return blocktime; };
68     time_t getTimeStamp() { return touch; };
69     bool operator< (const Block &other) const { return blocktime < other.blocktime; };
70 };
71
72 typedef vector<Block> BlockList;
73 typedef BlockList::iterator BlockListIterator;
74
75 /***************************************************************************************
76  * class FGTaxiSegment
77  **************************************************************************************/
78 class FGTaxiSegment
79 {
80 private:
81     int startNode;
82     int endNode;
83     double length;
84     double heading;
85     SGGeod center;
86     bool isActive;
87     bool isPushBackRoute;
88     BlockList blockTimes;
89     FGTaxiNode *start;
90     FGTaxiNode *end;
91     int index;
92     FGTaxiSegment *oppositeDirection;
93
94
95
96 public:
97     FGTaxiSegment() :
98             startNode(0),
99             endNode(0),
100             length(0),
101             heading(0),
102             isActive(0),
103             isPushBackRoute(0),
104             start(0),
105             end(0),
106             index(0),
107             oppositeDirection(0)
108     {
109     };
110
111     FGTaxiSegment         (const FGTaxiSegment &other) :
112             startNode         (other.startNode),
113             endNode           (other.endNode),
114             length            (other.length),
115             heading           (other.heading),
116             center            (other.center),
117             isActive          (other.isActive),
118             isPushBackRoute   (other.isPushBackRoute),
119             blockTimes        (other.blockTimes),
120             start             (other.start),
121             end               (other.end),
122             index             (other.index),
123             oppositeDirection (other.oppositeDirection)
124     {
125     };
126
127     FGTaxiSegment& operator=(const FGTaxiSegment &other)
128     {
129         startNode          = other.startNode;
130         endNode            = other.endNode;
131         length             = other.length;
132         heading            = other.heading;
133         center             = other.center;
134         isActive           = other.isActive;
135         isPushBackRoute    = other.isPushBackRoute;
136         blockTimes         = other.blockTimes;
137         start              = other.start;
138         end                = other.end;
139         index              = other.index;
140         oppositeDirection  = other.oppositeDirection;
141         return *this;
142     };
143
144     void setIndex        (int val) {
145         index     = val;
146     };
147     void setStartNodeRef (int val) {
148         startNode = val;
149     };
150     void setEndNodeRef   (int val) {
151         endNode   = val;
152     };
153
154     void setOpposite(FGTaxiSegment *opp) {
155         oppositeDirection = opp;
156     };
157
158     void setStart(FGTaxiNodeVector *nodes);
159     void setEnd  (FGTaxiNodeVector *nodes);
160     void setPushBackType(bool val) {
161         isPushBackRoute = val;
162     };
163     void setDimensions(double elevation);
164     void block(int id, time_t blockTime, time_t now);
165     void unblock(time_t now); 
166     bool hasBlock(time_t now);
167
168     FGTaxiNode * getEnd() {
169         return end;
170     };
171     FGTaxiNode * getStart() {
172         return start;
173     };
174     double getLength() {
175         return length;
176     };
177     int getIndex() {
178         return index;
179     };
180     double getLatitude()  {
181         return center.getLatitudeDeg();
182     };
183     double getLongitude() {
184         return center.getLongitudeDeg();
185     };
186     double getHeading()   {
187         return heading;
188     };
189     bool isPushBack() {
190         return isPushBackRoute;
191     };
192
193     int getPenalty(int nGates);
194
195     FGTaxiSegment *getAddress() {
196         return this;
197     };
198
199     bool operator<(const FGTaxiSegment &other) const {
200         return index < other.index;
201     };
202     //bool hasSmallerHeadingDiff (const FGTaxiSegment &other) const { return headingDiff < other.headingDiff; };
203     FGTaxiSegment *opposite() {
204         return oppositeDirection;
205     };
206     void setCourseDiff(double crse);
207
208
209
210
211 };
212
213
214
215
216 typedef vector<int> intVec;
217 typedef vector<int>::iterator intVecIterator;
218
219
220
221 /***************************************************************************************
222  * class FGTaxiRoute
223  **************************************************************************************/
224 class FGTaxiRoute
225 {
226 private:
227     intVec nodes;
228     intVec routes;
229     double distance;
230 //  int depth;
231     intVecIterator currNode;
232     intVecIterator currRoute;
233
234 public:
235     FGTaxiRoute() {
236         distance = 0;
237         currNode = nodes.begin();
238         currRoute = routes.begin();
239     };
240     FGTaxiRoute(intVec nds, intVec rts, double dist, int dpth) {
241         nodes = nds;
242         routes = rts;
243         distance = dist;
244         currNode = nodes.begin();
245         currRoute = routes.begin();
246 //    depth = dpth;
247     };
248
249     FGTaxiRoute& operator= (const FGTaxiRoute &other) {
250         nodes = other.nodes;
251         routes = other.routes;
252         distance = other.distance;
253 //    depth = other.depth;
254         currNode = nodes.begin();
255         currRoute = routes.begin();
256         return *this;
257     };
258
259     FGTaxiRoute(const FGTaxiRoute& copy) :
260             nodes(copy.nodes),
261             routes(copy.routes),
262             distance(copy.distance),
263 //    depth(copy.depth),
264             currNode(nodes.begin()),
265             currRoute(routes.begin())
266     {};
267
268     bool operator< (const FGTaxiRoute &other) const {
269         return distance < other.distance;
270     };
271     bool empty () {
272         return nodes.begin() == nodes.end();
273     };
274     bool next(int *nde);
275     bool next(int *nde, int *rte);
276     void rewind(int legNr);
277
278     void first() {
279         currNode = nodes.begin();
280         currRoute = routes.begin();
281     };
282     int size() {
283         return nodes.size();
284     };
285     int nodesLeft() {
286         return nodes.end() - currNode;
287     };
288
289 //  int getDepth() { return depth; };
290 };
291
292 typedef vector<FGTaxiRoute> TaxiRouteVector;
293 typedef vector<FGTaxiRoute>::iterator TaxiRouteVectorIterator;
294
295 /**************************************************************************************
296  * class FGGroundNetWork
297  *************************************************************************************/
298 class FGGroundNetwork : public FGATCController
299 {
300 private:
301     bool hasNetwork;
302     bool networkInitialized;
303     time_t nextSave;
304     //int maxDepth;
305     int count;
306     int version;
307     FGTaxiNodeVector    nodes;
308     FGTaxiNodeVector    pushBackNodes;
309     FGTaxiSegmentVector segments;
310     //intVec route;
311     //intVec nodesStack;
312     //intVec routesStack;
313     TaxiRouteVector routes;
314     TrafficVector activeTraffic;
315     TrafficVectorIterator currTraffic;
316
317     bool foundRoute;
318     double totalDistance, maxDistance;
319     FGTowerController *towerController;
320     FGAirport *parent;
321
322
323     //void printRoutingError(string);
324
325     void checkSpeedAdjustment(int id, double lat, double lon,
326                               double heading, double speed, double alt);
327     void checkHoldPosition(int id, double lat, double lon,
328                            double heading, double speed, double alt);
329
330
331
332 public:
333     FGGroundNetwork();
334     ~FGGroundNetwork();
335
336     void addNode   (const FGTaxiNode& node);
337     void addNodes  (FGParkingVec *parkings);
338     void addSegment(const FGTaxiSegment& seg);
339     void setVersion (int v) { version = v;};
340     
341     int getVersion() { return version; };
342
343     void init();
344     bool exists() {
345         return hasNetwork;
346     };
347     void setTowerController(FGTowerController *twrCtrlr) {
348         towerController = twrCtrlr;
349     };
350
351     int findNearestNode(double lat, double lon);
352     int findNearestNode(const SGGeod& aGeod);
353     int findNearestNodeOnRunway(const SGGeod& aGeod);
354
355     FGTaxiNode *findNode(unsigned idx);
356     FGTaxiSegment *findSegment(unsigned idx);
357     FGTaxiRoute findShortestRoute(int start, int end, bool fullSearch=true);
358     //void trace(FGTaxiNode *, int, int, double dist);
359
360     int getNrOfNodes() {
361         return nodes.size();
362     };
363
364     void setParent(FGAirport *par) {
365         parent = par;
366     };
367
368     virtual void announcePosition(int id, FGAIFlightPlan *intendedRoute, int currentRoute,
369                                   double lat, double lon, double hdg, double spd, double alt,
370                                   double radius, int leg, FGAIAircraft *aircraft);
371     virtual void signOff(int id);
372     virtual void updateAircraftInformation(int id, double lat, double lon, double heading, double speed, double alt, double dt);
373     virtual bool hasInstruction(int id);
374     virtual FGATCInstruction getInstruction(int id);
375
376     bool checkTransmissionState(int minState, int MaxState, TrafficVectorIterator i, time_t now, AtcMsgId msgId,
377                                 AtcMsgDir msgDir);
378     bool checkForCircularWaits(int id);
379     virtual void render(bool);
380     virtual string getName();
381     virtual void update(double dt);
382
383     void saveElevationCache();
384     void addVersion(int v) {version = v; };
385 };
386
387
388 #endif