]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/NavDataCache.hxx
Use a helper thread to rebuild the navcache.
[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
34 namespace flightgear
35 {
36   
37 /// a pair of airport ID, runway ID
38 typedef std::pair<PositionedID, PositionedID> AirportRunwayPair;
39   
40 typedef std::pair<FGPositioned::Type, PositionedID> TypedPositioned;
41 typedef std::vector<TypedPositioned> TypedPositionedVec;
42
43 // pair of airway ID, destination node ID
44 typedef std::pair<int, PositionedID> AirwayEdge;
45 typedef std::vector<AirwayEdge> AirwayEdgeVec;
46   
47 namespace Octree {
48   class Node;
49   class Branch;
50 }
51   
52 class NavDataCache
53 {
54 public:
55     ~NavDataCache();
56     
57 // singleton accessor
58     static NavDataCache* instance();
59           
60   /**
61    * predicate - check if the cache needs to be rebuilt.
62    * This can happen is the cache file is missing or damaged, or one of the
63    ** global input files is changed.
64    */
65   bool isRebuildRequired();
66   
67   /**
68    * run the cache rebuild - returns true if rebuild is complete,
69    * otherwise keep going.
70    */
71   bool rebuild();
72   
73   bool isCachedFileModified(const SGPath& path) const;
74   void stampCacheFile(const SGPath& path);
75   
76   int readIntProperty(const std::string& key);
77   double readDoubleProperty(const std::string& key);
78   std::string readStringProperty(const std::string& key);
79   
80   void writeIntProperty(const std::string& key, int value);
81   void writeStringProperty(const std::string& key, const std::string& value);
82   void writeDoubleProperty(const std::string& key, const double& value);
83   
84   string_list readStringListProperty(const std::string& key);
85   void writeStringListProperty(const std::string& key, const string_list& values);
86   
87   FGPositioned* loadById(PositionedID guid);
88   
89   PositionedID insertAirport(FGPositioned::Type ty, const std::string& ident,
90                              const std::string& name);
91   void insertTower(PositionedID airportId, const SGGeod& pos);
92   PositionedID insertRunway(FGPositioned::Type ty, const std::string& ident,
93                           const SGGeod& pos, PositionedID apt,
94                           double heading, double length, double width, double displacedThreshold,
95                           double stopway, int surfaceCode);
96   void setRunwayReciprocal(PositionedID runway, PositionedID recip);
97   void setRunwayILS(PositionedID runway, PositionedID ils);
98   
99   void updateRunwayThreshold(PositionedID runwayID, const SGGeod &aThreshold,
100                     double aHeading, double aDisplacedThreshold,
101                     double aStopway);
102   
103   PositionedID insertNavaid(FGPositioned::Type ty, const std::string& ident,
104                             const std::string& name, const SGGeod& pos, int freq, int range, double multiuse,
105                             PositionedID apt, PositionedID runway);
106   void updateILS(PositionedID ils, const SGGeod& newPos, double aHdg);
107   
108   PositionedID insertCommStation(FGPositioned::Type ty,
109                                  const std::string& name, const SGGeod& pos, int freq, int range,
110                                 PositionedID apt);
111   PositionedID insertFix(const std::string& ident, const SGGeod& aPos);
112   
113   PositionedID createUserWaypoint(const std::string& ident, const SGGeod& aPos);
114   
115   void setAirportMetar(const std::string& icao, bool hasMetar);
116   
117   void updatePosition(PositionedID item, const SGGeod &pos);
118   
119   FGPositioned::List findAllWithIdent(const std::string& ident,
120                                       FGPositioned::Filter* filter, bool exact);
121   FGPositioned::List findAllWithName(const std::string& ident,
122                                       FGPositioned::Filter* filter, bool exact);
123   
124   FGPositionedRef findClosestWithIdent(const std::string& aIdent,
125                                       const SGGeod& aPos, FGPositioned::Filter* aFilter);
126   
127
128   char** searchAirportNamesAndIdents(const std::string& aFilter);
129   
130   FGPositionedRef findCommByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
131   
132   PositionedIDVec airportItemsOfType(PositionedID apt, FGPositioned::Type ty,
133                                      FGPositioned::Type maxTy = FGPositioned::INVALID);
134     
135   PositionedID airportItemWithIdent(PositionedID apt, FGPositioned::Type ty, const std::string& ident);
136   
137   PositionedIDVec findNavaidsByFreq(int freqKhz, const SGGeod& pos, FGPositioned::Filter* filt);
138   
139   /// overload version of the above that does not consider positioned when
140   /// returning results. Only used by TACAN carrier search
141   PositionedIDVec findNavaidsByFreq(int freqKhz, FGPositioned::Filter* filt);
142   
143   /**
144    * given a navaid name (or similar) from apt.dat / nav.dat, find the
145    * corresponding airport and runway IDs.
146    * Such names look like: 'LHBP 31L DME-ILS' or 'UEEE 23L MM'
147    */
148   AirportRunwayPair findAirportRunway(const std::string& name);
149   
150   /**
151    * Given an aiport, and runway and ILS identifier, find the corresponding cache
152    * entry. This matches the data we get in the ils.xml files for airports.
153    */
154   PositionedID findILS(PositionedID airport, const std::string& runway, const std::string& navIdent);
155   
156   /**
157    * Given an Octree node ID, return a bit-mask defining which of the child
158    * nodes exist. In practice this means an 8-bit value be sufficent, but
159    * an int works fine too.
160    */
161   int getOctreeBranchChildren(int64_t octreeNodeId);
162   
163   void defineOctreeNode(Octree::Branch* pr, Octree::Node* nd);
164     
165   /**
166    * given an octree leaf, return all its child positioned items and their types
167    */
168   TypedPositionedVec getOctreeLeafChildren(int64_t octreeNodeId);
169   
170 // airways
171   int findAirway(int network, const std::string& aName);
172   
173   void insertEdge(int network, int airwayID, PositionedID from, PositionedID to);
174   
175   bool isInAirwayNetwork(int network, PositionedID pos);
176   
177   /**
178    * retrive all the destination points reachcable from a positioned
179    * in an airway
180    */
181   AirwayEdgeVec airwayEdgesFrom(int network, PositionedID pos);
182 private:
183   NavDataCache();
184   
185   friend class RebuildThread;
186   void doRebuild();
187   
188   class NavDataCachePrivate;
189   std::auto_ptr<NavDataCachePrivate> d;      
190 };
191   
192 } // of namespace flightgear
193
194 #endif // of FG_NAVDATACACHE_HXX
195