1 /*****************************************************************************
3 Module: FGLocalWeatherDatabase.cpp
4 Author: Christian Mayer
6 Called by: main program
8 ---------- Copyright (C) 1999 Christian Mayer (vader@t-online.de) ----------
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 Place - Suite 330, Boston, MA 02111-1307, USA.
24 Further information about the GNU General Public License can also be found on
25 the world wide web at http://www.gnu.org.
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 Database for the local weather
30 This database is the only one that gets called from FG
33 ------------------------------------------------------------------------------
34 28.05.1999 Christian Mayer Created
35 16.06.1999 Durk Talsma Portability for Linux
36 20.06.1999 Christian Mayer added lots of consts
37 *****************************************************************************/
39 /****************************************************************************/
41 /****************************************************************************/
42 #include "FGLocalWeatherDatabase.h"
43 #include "FGVoronoi.h"
44 #include "fg_constants.h"
46 #include <Aircraft/aircraft.hxx>
47 #include <Include/fg_constants.h>
49 /****************************************************************************/
50 /********************************** CODE ************************************/
51 /****************************************************************************/
53 /****************************************************************************/
54 /* return the index (better: ID) of the area with point p */
55 /****************************************************************************/
56 unsigned int FGLocalWeatherDatabase::AreaWith(const Point2D& p) const
59 for (FGMicroWeatherList::size_type i = 0; i != WeatherAreas.size(); i++)
61 if (WeatherAreas[i].hasPoint(p) == true)
65 return 0; //nothing found
68 /****************************************************************************/
69 /* make tiles out of points on a 2D plane */
70 /****************************************************************************/
71 void FGLocalWeatherDatabase::tileLocalWeather(const FGPhysicalProperties2DVector& EntryList)
73 FGVoronoiInputList input;
75 for (FGPhysicalProperties2DVector::const_iterator it1 = EntryList.begin(); it1 != EntryList.end(); it1++)
76 input.push_back(FGVoronoiInput(it1->p, *it1));
78 FGVoronoiOutputList output = Voronoiate(input);
80 for (FGVoronoiOutputList::iterator it2 = output.begin(); it2 != output.end(); it2++)
81 WeatherAreas.push_back(FGMicroWeather(it2->value, it2->boundary));
84 /****************************************************************************/
85 /* Constructor and Destructor */
86 /****************************************************************************/
87 FGLocalWeatherDatabase* FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 0;
88 FGLocalWeatherDatabase *WeatherDatabase;
90 FGLocalWeatherDatabase::FGLocalWeatherDatabase(const Point3D& posititon, const WeatherPrecition& visibility, const DatabaseWorkingType& type)
92 cerr << "Initializing FGLocalWeatherDatabase\n";
93 cerr << "-----------------------------------\n";
95 if (theFGLocalWeatherDatabase)
97 //FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one local weather allowed" );
98 cerr << "Error: only one local weather allowed";
102 setWeatherVisibility(visibility);
103 //WeatherVisibility = visibility;
104 DatabaseStatus = type;
105 global = 0; //just get sure...
106 last_known_position = posititon;
109 theFGLocalWeatherDatabase = this;
111 switch(DatabaseStatus)
115 global = new FGGlobalWeatherDatabase; //initialize GlobalDatabase
116 global->setDatabaseStatus(FGGlobalWeatherDatabase_working);
117 tileLocalWeather(global->getAll(posititon, WeatherVisibility, 3));
122 cerr << "FGLocalWeatherDatabase error: Distant database isn't implemented yet!\n";
123 cerr << " using random mode instead!\n";
128 vector<Point2D> emptyList;
129 WeatherAreas.push_back(FGMicroWeather(FGPhysicalProperties2D(), emptyList)); //in these cases I've only got one tile
134 cerr << "FGLocalWeatherDatabase error: Unknown database type specified!\n";
138 FGLocalWeatherDatabase::~FGLocalWeatherDatabase()
142 //delete every stored area
143 WeatherAreas.erase(WeatherAreas.begin(), WeatherAreas.end());
145 //delete global database if necessary
146 if (DatabaseStatus == use_global)
150 /****************************************************************************/
151 /* reset the whole database */
152 /****************************************************************************/
153 void FGLocalWeatherDatabase::reset(const DatabaseWorkingType& type)
155 //delete global database if necessary
156 if ((DatabaseStatus == use_global) && (type != use_global))
159 DatabaseStatus = type;
160 if (DatabaseStatus == use_global)
161 tileLocalWeather(global->getAll(last_known_position, WeatherVisibility, 3));
163 //delete every stored area
164 WeatherAreas.erase(WeatherAreas.begin(), WeatherAreas.end());
167 /****************************************************************************/
168 /* update the database. Since the last call we had dt seconds */
169 /****************************************************************************/
170 void FGLocalWeatherDatabase::update(const WeatherPrecition& dt)
172 if (DatabaseStatus==use_global)
176 void FGLocalWeatherDatabase::update(const Point3D& p) //position has changed
178 last_known_position = p;
179 //cerr << "****\nupdate inside\n";
180 //cerr << "Parameter: " << p << "\n";
184 void FGLocalWeatherDatabase::update(const Point3D& p, const WeatherPrecition& dt) //time and/or position has changed
186 last_known_position = p;
188 if (DatabaseStatus==use_global)
192 /****************************************************************************/
193 /* Get the physical properties on the specified point p out of the database */
194 /****************************************************************************/
195 FGPhysicalProperty FGLocalWeatherDatabase::get(const Point3D& p) const
197 unsigned int a = AreaWith(p);
199 return WeatherAreas[a-1].get(p.elev());
200 else //point is outside => ask GlobalWeatherDatabase
201 return global->get(p);
204 WeatherPrecition FGLocalWeatherDatabase::getAirDensity(const Point3D& p) const
206 FGPhysicalProperty dummy;
207 unsigned int a = AreaWith(p);
209 dummy = WeatherAreas[a-1].get(p.elev());
210 else //point is outside => ask GlobalWeatherDatabase
211 dummy = global->get(p);
214 (dummy.AirPressure*FG_WEATHER_DEFAULT_AIRDENSITY*FG_WEATHER_DEFAULT_TEMPERATURE) /
215 (dummy.Temperature*FG_WEATHER_DEFAULT_AIRPRESSURE);
219 /****************************************************************************/
220 /* Add a weather feature at the point p and surrounding area */
221 /****************************************************************************/
222 void FGLocalWeatherDatabase::addWind(const FGWindItem& x, const Point2D& p)
224 unsigned int a = AreaWith(p);
226 WeatherAreas[a-1].addWind(x);
229 void FGLocalWeatherDatabase::addTurbulence(const FGTurbulenceItem& x, const Point2D& p)
231 unsigned int a = AreaWith(p);
233 WeatherAreas[a-1].addTurbulence(x);
236 void FGLocalWeatherDatabase::addTemperature(const FGTemperatureItem& x, const Point2D& p)
238 unsigned int a = AreaWith(p);
240 WeatherAreas[a-1].addTemperature(x);
243 void FGLocalWeatherDatabase::addAirPressure(const FGAirPressureItem& x, const Point2D& p)
245 unsigned int a = AreaWith(p);
247 WeatherAreas[a-1].addAirPressure(x);
250 void FGLocalWeatherDatabase::addVaporPressure(const FGVaporPressureItem& x, const Point2D& p)
252 unsigned int a = AreaWith(p);
254 WeatherAreas[a-1].addVaporPressure(x);
257 void FGLocalWeatherDatabase::addCloud(const FGCloudItem& x, const Point2D& p)
259 unsigned int a = AreaWith(p);
261 WeatherAreas[a-1].addCloud(x);
264 void FGLocalWeatherDatabase::setSnowRainIntensity(const WeatherPrecition& x, const Point2D& p)
266 unsigned int a = AreaWith(p);
268 WeatherAreas[a-1].setSnowRainIntensity(x);
271 void FGLocalWeatherDatabase::setSnowRainType(const SnowRainType& x, const Point2D& p)
273 unsigned int a = AreaWith(p);
275 WeatherAreas[a-1].setSnowRainType(x);
278 void FGLocalWeatherDatabase::setLightningProbability(const WeatherPrecition& x, const Point2D& p)
280 unsigned int a = AreaWith(p);
282 WeatherAreas[a-1].setLightningProbability(x);
285 void FGLocalWeatherDatabase::addProperties(const FGPhysicalProperties2D& x)
287 if (DatabaseStatus==use_global)
291 //BAD, BAD, BAD thing I'm doing here: I'm adding to the global database a point that
292 //changes my voronoi diagram but I don't update it! instead I'm changing one local value
293 //that could be anywhere!!
294 //This only *might* work when the plane moves so far so fast that the diagram gets new
296 unsigned int a = AreaWith(x.p);
298 WeatherAreas[a-1].setStoredWeather(x);
302 unsigned int a = AreaWith(x.p);
304 WeatherAreas[a-1].setStoredWeather(x);
308 void FGLocalWeatherDatabase::setProperties(const FGPhysicalProperties2D& x)
310 if (DatabaseStatus==use_global)
314 //BAD, BAD, BAD thing I'm doing here: I'm adding to the global database a point that
315 //changes my voronoi diagram but I don't update it! Instead I'm changing one local value
316 //that could be anywhere!!
317 //This only *might* work when the plane moves so far so fast that the diagram gets newly
319 unsigned int a = AreaWith(x.p);
321 WeatherAreas[a-1].setStoredWeather(x);
325 unsigned int a = AreaWith(x.p);
327 WeatherAreas[a-1].setStoredWeather(x);
331 void fgUpdateWeatherDatabase(void)
333 //cerr << "FGLocalWeatherDatabase::update()\n";
334 WeatherDatabase->update( Point3D(
335 current_aircraft.fdm_state->get_Latitude(),
336 current_aircraft.fdm_state->get_Longitude(),
337 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER) );