]> git.mxchange.org Git - flightgear.git/blob - src/WeatherCM/FGMicroWeather.cpp
Initial revisions.
[flightgear.git] / src / WeatherCM / FGMicroWeather.cpp
1 /*****************************************************************************
2
3  Module:       FGMicroWeather.cpp
4  Author:       Christian Mayer
5  Date started: 28.05.99
6  Called by:    FGLocalWeatherDatabase
7
8  -------- Copyright (C) 1999 Christian Mayer (fgfs@christianmayer.de) --------
9
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
13  version.
14
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
18  details.
19
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.
23
24  Further information about the GNU General Public License can also be found on
25  the world wide web at http://www.gnu.org.
26
27 FUNCTIONAL DESCRIPTION
28 ------------------------------------------------------------------------------
29 Handle the weather areas
30
31 HISTORY
32 ------------------------------------------------------------------------------
33 28.05.1999 Christian Mayer      Created
34 16.06.1999 Durk Talsma          Portability for Linux
35 20.06.1999 Christian Mayer      added lots of consts
36 11.10.1999 Christian Mayer      changed set<> to map<> on Bernie Bright's 
37                                 suggestion
38 19.10.1999 Christian Mayer      change to use PLIB's sg instead of Point[2/3]D
39                                 and lots of wee code cleaning
40 *****************************************************************************/
41
42 /****************************************************************************/
43 /* INCLUDES                                                                 */
44 /****************************************************************************/
45 #include <Include/compiler.h>
46 #include <Include/fg_constants.h>
47
48 #include "FGMicroWeather.h"
49
50 /****************************************************************************/
51 /********************************** CODE ************************************/
52 /****************************************************************************/
53 FGMicroWeather::FGMicroWeather(const FGPhysicalProperties2D& p, const positionList& points)
54 {
55     StoredWeather = p;
56     position = points;
57 }
58
59 FGMicroWeather::~FGMicroWeather()
60 {
61 }
62
63 /****************************************************************************/
64 /* Add the features to the micro weather                                    */
65 /* return succss                                                            */
66 /****************************************************************************/
67 void FGMicroWeather::addWind(const WeatherPrecision alt, const FGWindItem& x)
68 {
69     StoredWeather.Wind[alt] = x;
70 }
71
72 void FGMicroWeather::addTurbulence(const WeatherPrecision alt, const FGTurbulenceItem& x)
73 {
74     StoredWeather.Turbulence[alt] = x;
75 }
76
77 void FGMicroWeather::addTemperature(const WeatherPrecision alt, const WeatherPrecision x)
78 {
79     StoredWeather.Temperature[alt] = x;
80 }
81
82 void FGMicroWeather::addAirPressure(const WeatherPrecision alt, const WeatherPrecision x)
83 {
84     cerr << "Error: caught attempt to add AirPressure which is logical wrong\n";
85     //StoredWeather.AirPressure[alt] = x;
86 }
87
88 void FGMicroWeather::addVaporPressure(const WeatherPrecision alt, const WeatherPrecision x)
89 {
90     StoredWeather.VaporPressure[alt] = x;
91 }
92
93 void FGMicroWeather::addCloud(const WeatherPrecision alt, const FGCloudItem& x)
94 {
95     StoredWeather.Clouds[alt] = x;
96 }
97
98 void FGMicroWeather::setSnowRainIntensity(const WeatherPrecision x)
99 {
100     StoredWeather.SnowRainIntensity = x;
101 }
102
103 void FGMicroWeather::setSnowRainType(const SnowRainType x)
104 {
105     StoredWeather.snowRainType = x;
106 }
107
108 void FGMicroWeather::setLightningProbability(const WeatherPrecision x)
109 {
110     StoredWeather.LightningProbability = x;
111 }
112
113 void FGMicroWeather::setStoredWeather(const FGPhysicalProperties2D& x)
114 {
115     StoredWeather = x;
116 }
117
118 /****************************************************************************/
119 /* return true if p is inside this micro weather                            */
120 /* code stolen from $FG_ROOT/Simulator/Objects/fragment.cxx, which was      */
121 /* written by Curtis L. Olson - curt@me.umn.edu                             */
122 /****************************************************************************/
123
124 template <class T> //template to help with the calulation
125 inline const int FG_SIGN(const T& x) {
126     return x < T(0) ? -1 : 1;
127 }
128
129 bool FGMicroWeather::hasPoint(const sgVec2& p) const
130 {
131     if (position.size()==0)
132         return true;    //no border => this tile is infinite
133     
134     if (position.size()==1)
135         return false;   //a border on 1 point?!?
136
137     //when I'm here I've got at least 2 points
138     
139     WeatherPrecision t;
140     signed char side1, side2;
141     const_positionListIt it = position.begin();
142     const_positionListIt it2 = it; it2++;
143
144     for (;;)
145     {
146         
147         if (it2 == position.end())
148             break;
149
150         if (fabs(it->p[0] - it2->p[0]) >= FG_EPSILON)
151         {
152             t = (it->p[1] - it2->p[1]) / (it->p[0] - it2->p[0]);
153
154             side1 = FG_SIGN (t * (StoredWeather.p[0] - it2->p[0]) + it2->p[1] - StoredWeather.p[1]);
155             side2 = FG_SIGN (t * (              p[0] - it2->p[0]) + it2->p[1] -               p[1]);
156             if ( side1 != side2 ) 
157                 return false; //cout << "failed side check\n";
158         }
159         else
160         {
161             t = (it->p[0] - it2->p[0]) / (it->p[1] - it2->p[1]);
162
163             side1 = FG_SIGN (t * (StoredWeather.p[1] - it2->p[1]) + it2->p[0] - StoredWeather.p[0]);
164             side2 = FG_SIGN (t * (              p[1] - it2->p[1]) + it2->p[0] -               p[0]);
165             if ( side1 != side2 ) 
166                 return false; //cout << "failed side check\n";
167         }
168
169         it++; it2++;
170     }
171
172     return true;
173 }