1 /*****************************************************************************
3 Module: FGLocalWeatherDatabase.cpp
4 Author: Christian Mayer
6 Called by: main program
8 -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.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 11.10.1999 Christian Mayer changed set<> to map<> on Bernie Bright's
39 19.10.1999 Christian Mayer change to use PLIB's sg instead of Point[2/3]D
40 and lots of wee code cleaning
41 *****************************************************************************/
43 /****************************************************************************/
45 /****************************************************************************/
46 #include <Include/compiler.h>
47 #include <Include/fg_constants.h>
49 #include <Aircraft/aircraft.hxx>
51 #include "FGLocalWeatherDatabase.h"
52 #include "FGVoronoi.h"
55 /****************************************************************************/
56 /********************************** CODE ************************************/
57 /****************************************************************************/
59 /****************************************************************************/
60 /* return the index (better: ID) of the area with point p */
61 /****************************************************************************/
62 unsigned int FGLocalWeatherDatabase::AreaWith(const sgVec2& p) const
65 for (FGMicroWeatherList::size_type i = 0; i != WeatherAreas.size(); i++)
67 if (WeatherAreas[i].hasPoint(p) == true)
71 return 0; //nothing found
74 /****************************************************************************/
75 /* make tiles out of points on a 2D plane */
76 /****************************************************************************/
77 void FGLocalWeatherDatabase::tileLocalWeather(const FGPhysicalProperties2DVector& EntryList)
79 FGVoronoiInputList input;
81 for (FGPhysicalProperties2DVectorConstIt it1 = EntryList.begin(); it1 != EntryList.end(); it1++)
82 input.push_back(FGVoronoiInput(it1->p, *it1));
84 FGVoronoiOutputList output = Voronoiate(input);
86 for (FGVoronoiOutputList::iterator it2 = output.begin(); it2 != output.end(); it2++)
87 WeatherAreas.push_back(FGMicroWeather(it2->value, it2->boundary));
90 /****************************************************************************/
91 /* Constructor and Destructor */
92 /****************************************************************************/
93 FGLocalWeatherDatabase* FGLocalWeatherDatabase::theFGLocalWeatherDatabase = 0;
94 FGLocalWeatherDatabase *WeatherDatabase;
96 FGLocalWeatherDatabase::FGLocalWeatherDatabase(const sgVec3& posititon, const WeatherPrecision visibility, const DatabaseWorkingType type)
98 cerr << "Initializing FGLocalWeatherDatabase\n";
99 cerr << "-----------------------------------\n";
101 if (theFGLocalWeatherDatabase)
103 //FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one local weather allowed" );
104 cerr << "Error: only one local weather allowed";
108 setWeatherVisibility(visibility);
110 DatabaseStatus = type;
111 global = 0; //just get sure...
112 sgCopyVec3(last_known_position, posititon);
115 theFGLocalWeatherDatabase = this;
117 switch(DatabaseStatus)
121 global = new FGGlobalWeatherDatabase; //initialize GlobalDatabase
122 global->setDatabaseStatus(FGGlobalWeatherDatabase_working);
123 tileLocalWeather(global->getAll(posititon, WeatherVisibility, 3));
128 cerr << "FGLocalWeatherDatabase error: Distant database isn't implemented yet!\n";
129 cerr << " using random mode instead!\n";
134 vector<sgVec2Wrap> emptyList;
135 WeatherAreas.push_back(FGMicroWeather(FGPhysicalProperties2D(), emptyList)); //in these cases I've only got one tile
140 cerr << "FGLocalWeatherDatabase error: Unknown database type specified!\n";
144 FGLocalWeatherDatabase::~FGLocalWeatherDatabase()
148 //delete every stored area
149 WeatherAreas.erase(WeatherAreas.begin(), WeatherAreas.end());
151 //delete global database if necessary
152 if (DatabaseStatus == use_global)
156 /****************************************************************************/
157 /* reset the whole database */
158 /****************************************************************************/
159 void FGLocalWeatherDatabase::reset(const DatabaseWorkingType type)
161 //delete global database if necessary
162 if ((DatabaseStatus == use_global) && (type != use_global))
165 DatabaseStatus = type;
166 if (DatabaseStatus == use_global)
167 tileLocalWeather(global->getAll(last_known_position, WeatherVisibility, 3));
169 //delete every stored area
170 WeatherAreas.erase(WeatherAreas.begin(), WeatherAreas.end());
173 /****************************************************************************/
174 /* update the database. Since the last call we had dt seconds */
175 /****************************************************************************/
176 void FGLocalWeatherDatabase::update(const WeatherPrecision dt)
178 if (DatabaseStatus==use_global)
182 void FGLocalWeatherDatabase::update(const sgVec3& p) //position has changed
184 sgCopyVec3(last_known_position, p);
185 //cerr << "****\nupdate inside\n";
186 //cerr << "Parameter: " << p << "\n";
190 void FGLocalWeatherDatabase::update(const sgVec3& p, const WeatherPrecision dt) //time and/or position has changed
192 sgCopyVec3(last_known_position, p);
194 if (DatabaseStatus==use_global)
198 /****************************************************************************/
199 /* Get the physical properties on the specified point p out of the database */
200 /****************************************************************************/
201 FGPhysicalProperty FGLocalWeatherDatabase::get(const sgVec3& p) const
203 unsigned int a = AreaWith(p);
205 return WeatherAreas[a-1].get(p[3]);
206 else //point is outside => ask GlobalWeatherDatabase
207 return global->get(p);
210 FGPhysicalProperties FGLocalWeatherDatabase::get(const sgVec2& p) const
213 sgSetVec3(temp, p[0], p[1], 0.0);
215 unsigned int a = AreaWith(temp);
217 return WeatherAreas[a-1].get();
218 else //point is outside => ask GlobalWeatherDatabase
219 return global->get(p);
222 WeatherPrecision FGLocalWeatherDatabase::getAirDensity(const sgVec3& p) const
224 FGPhysicalProperty dummy;
225 unsigned int a = AreaWith(p);
227 dummy = WeatherAreas[a-1].get(p[3]);
228 else //point is outside => ask GlobalWeatherDatabase
229 dummy = global->get(p);
232 (dummy.AirPressure*FG_WEATHER_DEFAULT_AIRDENSITY*FG_WEATHER_DEFAULT_TEMPERATURE) /
233 (dummy.Temperature*FG_WEATHER_DEFAULT_AIRPRESSURE);
236 /****************************************************************************/
237 /* Add a weather feature at the point p and surrounding area */
238 /****************************************************************************/
239 void FGLocalWeatherDatabase::addWind(const WeatherPrecision alt, const sgVec3& x, const sgVec2& p)
241 unsigned int a = AreaWith(p);
243 WeatherAreas[a-1].addWind(alt, x);
246 void FGLocalWeatherDatabase::addTurbulence(const WeatherPrecision alt, const sgVec3& x, const sgVec2& p)
248 unsigned int a = AreaWith(p);
250 WeatherAreas[a-1].addTurbulence(alt, x);
253 void FGLocalWeatherDatabase::addTemperature(const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p)
255 unsigned int a = AreaWith(p);
257 WeatherAreas[a-1].addTemperature(alt, x);
260 void FGLocalWeatherDatabase::addAirPressure(const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p)
262 unsigned int a = AreaWith(p);
264 WeatherAreas[a-1].addAirPressure(alt, x);
267 void FGLocalWeatherDatabase::addVaporPressure(const WeatherPrecision alt, const WeatherPrecision x, const sgVec2& p)
269 unsigned int a = AreaWith(p);
271 WeatherAreas[a-1].addVaporPressure(alt, x);
274 void FGLocalWeatherDatabase::addCloud(const WeatherPrecision alt, const FGCloudItem& x, const sgVec2& p)
276 unsigned int a = AreaWith(p);
278 WeatherAreas[a-1].addCloud(alt, x);
281 void FGLocalWeatherDatabase::setSnowRainIntensity(const WeatherPrecision x, const sgVec2& p)
283 unsigned int a = AreaWith(p);
285 WeatherAreas[a-1].setSnowRainIntensity(x);
288 void FGLocalWeatherDatabase::setSnowRainType(const SnowRainType x, const sgVec2& p)
290 unsigned int a = AreaWith(p);
292 WeatherAreas[a-1].setSnowRainType(x);
295 void FGLocalWeatherDatabase::setLightningProbability(const WeatherPrecision x, const sgVec2& p)
297 unsigned int a = AreaWith(p);
299 WeatherAreas[a-1].setLightningProbability(x);
302 void FGLocalWeatherDatabase::addProperties(const FGPhysicalProperties2D& x)
304 if (DatabaseStatus==use_global)
308 //BAD, BAD, BAD thing I'm doing here: I'm adding to the global database a point that
309 //changes my voronoi diagram but I don't update it! instead I'm changing one local value
310 //that could be anywhere!!
311 //This only *might* work when the plane moves so far so fast that the diagram gets new
313 unsigned int a = AreaWith(x.p);
315 WeatherAreas[a-1].setStoredWeather(x);
319 unsigned int a = AreaWith(x.p);
321 WeatherAreas[a-1].setStoredWeather(x);
325 void FGLocalWeatherDatabase::setProperties(const FGPhysicalProperties2D& x)
327 if (DatabaseStatus==use_global)
331 //BAD, BAD, BAD thing I'm doing here: I'm adding to the global database a point that
332 //changes my voronoi diagram but I don't update it! Instead I'm changing one local value
333 //that could be anywhere!!
334 //This only *might* work when the plane moves so far so fast that the diagram gets newly
336 unsigned int a = AreaWith(x.p);
338 WeatherAreas[a-1].setStoredWeather(x);
342 unsigned int a = AreaWith(x.p);
344 WeatherAreas[a-1].setStoredWeather(x);
348 void fgUpdateWeatherDatabase(void)
350 //cerr << "FGLocalWeatherDatabase::update()\n";
353 current_aircraft.fdm_state->get_Latitude(),
354 current_aircraft.fdm_state->get_Longitude(),
355 current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
357 WeatherDatabase->update( position );