]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
Trying to bullet-proof the traffic code.
[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     enum RebuildPhase
74     {
75         REBUILD_UNKNOWN = 0,
76         REBUILD_AIRPORTS,
77         REBUILD_NAVAIDS,
78         REBUILD_FIXES,
79         REBUILD_POIS,
80         REBUILD_DONE
81     };
82
83   /**
84    * run the cache rebuild - returns the current phase or 'done'
85    */
86   RebuildPhase rebuild();
87
88   unsigned int rebuildPhaseCompletionPercentage() const;
89   void setRebuildPhaseProgress(RebuildPhase ph, unsigned int percent = 0);
90
91   bool isCachedFileModified(const SGPath& path) const;
92   void stampCacheFile(const SGPath& path);
93
94   int readIntProperty(const std::string& key);
95   double readDoubleProperty(const std::string& key);
96   std::string readStringProperty(const std::string& key);
97
98   void writeIntProperty(const std::string& key, int value);
99   void writeStringProperty(const std::string& key, const std::string& value);
100   void writeDoubleProperty(const std::string& key, const double& value);
101
102   string_list readStringListProperty(const std::string& key);
103   void writeStringListProperty(const std::string& key, const string_list& values);
104
105   /**
106    * retrieve an FGPositioned from the cache.
107    * This may be trivial if the object is previously loaded, or require actual
108    * disk IO.
109    */
110   FGPositionedRef loadById(PositionedID guid);
111
112   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
113                              const std::string& name);
114   void insertTower(PositionedID airportId, const SGGeod& pos);
115   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
116                           const SGGeod& pos, PositionedID apt,
117                           double heading, double length, double width, double displacedThreshold,
118                           double stopway, int surfaceCode);
119   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
120   void setRunwayILS(PositionedID runway, PositionedID ils);
121
122   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
123                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
124                             PositionedID apt, PositionedID runway);
125
126   // Assign colocated DME to a navaid
127   void setNavaidColocated(PositionedID navaid, PositionedID colocatedDME);
128
129   PositionedID insertCommStation(FGPositioned::Type ty,
130                                  const std::string& name, const SGGeod& pos, int freq, int range,
131                                 PositionedID apt);
132   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
133
134   PositionedID createPOI(FGPositioned::Type ty, const std::string& ident, const SGGeod& aPos);
135
136   bool removePOI(FGPositioned::Type ty, const std::string& aIdent);
137
138   /// update the metar flag associated with an airport
139   void setAirportMetar(const std::string& icao, bool hasMetar);
140
141   /**
142    * Modify the position of an existing item.
143    */
144   void updatePosition(PositionedID item, const SGGeod &pos);
145
146   FGPositionedList findAllWithIdent( const std::string& ident,
147                                      FGPositioned::Filter* filter,
148                                      bool exact );
149   FGPositionedList findAllWithName( const std::string& ident,
150                                     FGPositioned::Filter* filter,
151                                     bool exact );
152
153   FGPositionedRef findClosestWithIdent( const std::string& aIdent,
154                                         const SGGeod& aPos,
155                                         FGPositioned::Filter* aFilter );
156
157
158   /**
159    * Helper to implement the AirportSearch widget. Optimised text search of
160    * airport names and idents, returning a list suitable for passing directly
161    * to PLIB.
162    */
163   char** searchAirportNamesAndIdents(const std::string& aFilter);
164
165   /**
166    * Find the closest matching comm-station on a frequency, to a position.
167    * The filter with be used for both type ranging and to validate the result
168    * candidates.
169    */
170   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
171
172   /**
173    * find all items of a specified type (or range of types) at an airport
174    */
175   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
176                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
177
178   /**
179    * find the first match item of the specified type and ident, at an airport
180    */
181   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
182
183   /**
184    * Find all navaids matching a particular frequency, sorted by range from the
185    * supplied position. Type-range will be determined from the filter
186    */
187   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
188
189   /// overload version of the above that does not consider positioned when
190   /// returning results. Only used by TACAN carrier search
191   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
192
193   /**
194    * Given a runway and type, find the corresponding navaid (ILS / GS / OM)
195    */
196   PositionedID findNavaidForRunway(PositionedID runway, FGPositioned::Type ty);
197
198   /**
199    * given a navaid name (or similar) from apt.dat / nav.dat, find the
200    * corresponding airport and runway IDs.
201    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
202    */
203   AirportRunwayPair findAirportRunway(const std::string& name);
204
205   /**
206    * Given an aiport, and runway and ILS identifier, find the corresponding cache
207    * entry. This matches the data we get in the ils.xml files for airports.
208    */
209   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
210
211   /**
212    * Given an Octree node ID, return a bit-mask defining which of the child
213    * nodes exist. In practice this means an 8-bit value be sufficent, but
214    * an int works fine too.
215    */
216   int getOctreeBranchChildren(int64_t octreeNodeId);
217
218   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
219
220   /**
221    * given an octree leaf, return all its child positioned items and their types
222    */
223   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
224
225 // airways
226   int findAirway(int network, const std::string& aName);
227
228   /**
229    * insert an edge between two positioned nodes, into the network.
230    * The airway identifier will be set accordingly. No reverse edge is created
231    * by this method - edges are directional so a reverses must be explicitly
232    * created.
233    */
234   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
235
236   /// is the specified positioned a node on the network?
237   bool isInAirwayNetwork(int network, PositionedID pos);
238
239   /**
240    * retrive all the destination points reachcable from a positioned
241    * in an airway
242    */
243   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
244
245     /**
246      * Waypoints on the airway
247      */
248   PositionedIDVec airwayWaypts(int id);
249
250     class Transaction
251     {
252     public:
253         Transaction(NavDataCache* cache);
254         ~Transaction();
255
256         void commit();
257     private:
258         NavDataCache* _instance;
259         bool _committed;
260     };
261
262     bool isReadOnly() const;
263
264     class ThreadedGUISearch
265     {
266     public:
267         ThreadedGUISearch(const std::string& term);
268         ~ThreadedGUISearch();
269
270         PositionedIDVec results() const;
271
272         bool isComplete() const;
273     private:
274         class ThreadedGUISearchPrivate;
275         std::auto_ptr<ThreadedGUISearchPrivate> d;
276     };
277 private:
278   NavDataCache();
279
280   friend class RebuildThread;
281   void doRebuild();
282
283   friend class Transaction;
284
285     void beginTransaction();
286     void commitTransaction();
287     void abortTransaction();
288
289   class NavDataCachePrivate;
290   std::auto_ptr<NavDataCachePrivate> d;
291 };
292
293 } // of namespace flightgear
294
295 #endif // of FG_NAVDATACACHE_HXX