]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
Work on launcher diagrams.
[flightgear.git] / src / Navaids / NavDataCache.hxx
1 /**
2  * NavDataCache.hxx - defines a unified binary cache for navigation
3  * data, parsed from various text / XML sources.
4  */
5  
6 // Written by James Turner, started 2012.
7 //
8 // Copyright (C) 2012 James Turner
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
24 #ifndef FG_NAVDATACACHE_HXX
25 #define FG_NAVDATACACHE_HXX
26
27 #include <memory>
28
29 #include <simgear/misc/strutils.hxx> // for string_list
30 #include <Navaids/positioned.hxx>
31     
32 class SGPath;
33 class FGRunway;
34
35 namespace flightgear
36 {
37   
38 /// a pair of airport ID, runway ID
39 typedef std::pair<PositionedID, PositionedID> AirportRunwayPair;
40   
41 typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned;
42 typedef std::vector<TypedPositioned> TypedPositionedVec;
43
44 // pair of airway ID, destination node ID
45 typedef std::pair<int, PositionedID> AirwayEdge;
46 typedef std::vector<AirwayEdge> AirwayEdgeVec;
47   
48 namespace Octree {
49   class Node;
50   class Branch;
51 }
52   
53 class NavDataCache
54 {
55 public:
56     ~NavDataCache();
57     
58 // singleton accessor
59     static NavDataCache* instance();
60
61 // static creator
62     static NavDataCache* createInstance();
63
64     SGPath path() const;
65     
66   /**
67    * predicate - check if the cache needs to be rebuilt.
68    * This can happen is the cache file is missing or damaged, or one of the
69    ** global input files is changed.
70    */
71   bool isRebuildRequired();
72
73     /**
74      * check if cached scenery paths have changed, and if so, drop scenery-
75      * dependant data such as ground-nets.
76      */
77   bool dropGroundnetsIfRequired();
78
79     enum RebuildPhase
80     {
81         REBUILD_UNKNOWN = 0,
82         REBUILD_AIRPORTS,
83         REBUILD_NAVAIDS,
84         REBUILD_FIXES,
85         REBUILD_POIS,
86         REBUILD_DONE
87     };
88
89   /**
90    * run the cache rebuild - returns the current phase or 'done'
91    */
92   RebuildPhase rebuild();
93
94     unsigned int rebuildPhaseCompletionPercentage() const;
95     void setRebuildPhaseProgress(RebuildPhase ph, unsigned int percent = 0);
96
97   bool isCachedFileModified(const SGPath& path) const;
98   void stampCacheFile(const SGPath& path);
99   
100   int readIntProperty(const std::string& key);
101   double readDoubleProperty(const std::string& key);
102   std::string readStringProperty(const std::string& key);
103   
104   void writeIntProperty(const std::string& key, int value);
105   void writeStringProperty(const std::string& key, const std::string& value);
106   void writeDoubleProperty(const std::string& key, const double& value);
107   
108   string_list readStringListProperty(const std::string& key);
109   void writeStringListProperty(const std::string& key, const string_list& values);
110   
111   /**
112    * retrieve an FGPositioned from the cache.
113    * This may be trivial if the object is previously loaded, or require actual
114    * disk IO.
115    */
116   FGPositionedRef loadById(PositionedID guid);
117   
118   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
119                              const std::string& name);
120   void insertTower(PositionedID airportId, const SGGeod& pos);
121   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
122                           const SGGeod& pos, PositionedID apt,
123                           double heading, double length, double width, double displacedThreshold,
124                           double stopway, int surfaceCode);
125   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
126   void setRunwayILS(PositionedID runway, PositionedID ils);
127   
128   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
129                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
130                             PositionedID apt, PositionedID runway);
131
132   // Assign colocated DME to a navaid
133   void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME);
134   
135   PositionedID insertCommStation(FGPositioned::Type ty,
136                                  const std::string& name, const SGGeod& pos, int freq, int range,
137                                 PositionedID apt);
138   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
139   
140   PositionedID createPOI(FGPositioned::Type ty, const std::string& ident, const SGGeod& aPos);
141   
142   bool removePOI(FGPositioned::Type ty, const std::string& aIdent);
143     
144   void dropGroundnetFor(PositionedID aAirport);
145   
146   /**
147    * Remove all ground-nets globally from the cache.
148    * This includes parking and taxi-nodes and edges between them. It's useful
149    * when scenery paths change, since the ground-nets depend on the scenery.
150    * Using this we can avoid havind to rebuild the entire cache.
151    */
152   void dropAllGroundnets();
153   
154   PositionedID insertParking(const std::string& name, const SGGeod& aPos,
155                              PositionedID aAirport,
156                              double aHeading, int aRadius, const std::string& aAircraftType,
157                              const std::string& aAirlines);
158   
159   void setParkingPushBackRoute(PositionedID parking, PositionedID pushBackNode);
160   
161   PositionedID insertTaxiNode(const SGGeod& aPos, PositionedID aAirport, int aHoldType, bool aOnRunway);
162   
163   void insertGroundnetEdge(PositionedID aAirport, PositionedID from, PositionedID to);
164   
165   /// update the metar flag associated with an airport
166   void setAirportMetar(const std::string& icao, bool hasMetar);
167   
168   /**
169    * Modify the position of an existing item.
170    */
171   void updatePosition(PositionedID item, const SGGeod &pos);
172   
173   FGPositionedList findAllWithIdent( const std::string& ident,
174                                      FGPositioned::Filter* filter,
175                                      bool exact );
176   FGPositionedList findAllWithName( const std::string& ident,
177                                     FGPositioned::Filter* filter,
178                                     bool exact );
179   
180   FGPositionedRef findClosestWithIdent( const std::string& aIdent,
181                                         const SGGeod& aPos,
182                                         FGPositioned::Filter* aFilter );
183   
184
185   /**
186    * Helper to implement the AirportSearch widget. Optimised text search of
187    * airport names and idents, returning a list suitable for passing directly
188    * to PLIB.
189    */
190   char** searchAirportNamesAndIdents(const std::string& aFilter);
191   
192   /**
193    * Find the closest matching comm-station on a frequency, to a position.
194    * The filter with be used for both type ranging and to validate the result
195    * candidates.
196    */
197   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
198   
199   /**
200    * find all items of a specified type (or range of types) at an airport
201    */
202   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
203                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
204     
205   /**
206    * find the first match item of the specified type and ident, at an airport
207    */
208   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
209     
210   /**
211    * Find all navaids matching a particular frequency, sorted by range from the
212    * supplied position. Type-range will be determined from the filter
213    */
214   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
215   
216   /// overload version of the above that does not consider positioned when
217   /// returning results. Only used by TACAN carrier search
218   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
219   
220   /**
221    * Given a runway and type, find the corresponding navaid (ILS / GS / OM)
222    */
223   PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty);
224
225   /**
226    * given a navaid name (or similar) from apt.dat / nav.dat, find the
227    * corresponding airport and runway IDs.
228    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
229    */
230   AirportRunwayPair findAirportRunway(const std::string& name);
231   
232   /**
233    * Given an aiport, and runway and ILS identifier, find the corresponding cache
234    * entry. This matches the data we get in the ils.xml files for airports.
235    */
236   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
237   
238   /**
239    * Given an Octree node ID, return a bit-mask defining which of the child
240    * nodes exist. In practice this means an 8-bit value be sufficent, but
241    * an int works fine too.
242    */
243   int getOctreeBranchChildren(int64_t octreeNodeId);
244   
245   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
246     
247   /**
248    * given an octree leaf, return all its child positioned items and their types
249    */
250   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
251   
252 // airways
253   int findAirway(int network, const std::string& aName);
254   
255   /**
256    * insert an edge between two positioned nodes, into the network.
257    * The airway identifier will be set accordingly. No reverse edge is created
258    * by this method - edges are directional so a reverses must be explicitly
259    * created.
260    */
261   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
262   
263   /// is the specified positioned a node on the network?
264   bool isInAirwayNetwork(int network, PositionedID pos);
265   
266   /**
267    * retrive all the destination points reachcable from a positioned
268    * in an airway
269    */
270   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
271   
272 // ground-network
273   PositionedIDVec groundNetNodes(PositionedID aAirport, bool onlyPushback);
274   void markGroundnetAsPushback(PositionedID nodeId);
275   
276   PositionedID findGroundNetNode(PositionedID airport, const SGGeod& aPos,
277                                  bool onRunway, FGRunway* aRunway = NULL);
278   PositionedIDVec groundNetEdgesFrom(PositionedID pos, bool onlyPushback);
279   
280   PositionedIDVec findAirportParking(PositionedID airport, const std::string& flightType,
281                                      int radius);
282
283
284     class Transaction
285     {
286     public:
287         Transaction(NavDataCache* cache);
288         ~Transaction();
289         
290         void commit();
291     private:
292         NavDataCache* _instance;
293         bool _committed;
294     };
295     
296     bool isReadOnly() const;
297
298     class ThreadedGUISearch
299     {
300     public:
301         ThreadedGUISearch(const std::string& term);
302         ~ThreadedGUISearch();
303         
304         PositionedIDVec results() const;
305
306         bool isComplete() const;
307     private:
308         class ThreadedGUISearchPrivate;
309         std::auto_ptr<ThreadedGUISearchPrivate> d;
310     };
311 private:
312   NavDataCache();
313   
314   friend class RebuildThread;
315   void doRebuild();
316   
317   friend class Transaction;
318   
319     void beginTransaction();
320     void commitTransaction();
321     void abortTransaction();
322     
323   class NavDataCachePrivate;
324   std::auto_ptr<NavDataCachePrivate> d;      
325 };
326   
327 } // of namespace flightgear
328
329 #endif // of FG_NAVDATACACHE_HXX
330