1 // TankProperties.cxx -- expose (fuel)-tank properties
3 // Written by Torsten Dreyer, started January 2011.
5 // Copyright (C) 2011 Torsten Dreyer - Torsten (at) t3r _dot_ de
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "TankProperties.hxx"
28 #include <simgear/math/SGMath.hxx>
29 #include <simgear/sg_inlines.h>
30 #include <Main/fg_props.hxx>
32 static const double LBS_PER_KG = 2.20462262;
33 static const double KG_PER_LBS = 1.0/LBS_PER_KG;
34 static const double USGAL_PER_M3 = 1000.0/3.785411784;
35 static const double M3_PER_USGAL = 1.0/USGAL_PER_M3;
36 static const double IMPGAL_PER_M3 = 1000.0/4.54609;
37 static const double M3_PER_IMPGAL = 1.0/IMPGAL_PER_M3;
39 TankProperties::TankProperties(SGPropertyNode_ptr rootNode ) :
45 _tiedProperties.setRoot( rootNode );
48 void TankProperties::bind()
50 _tiedProperties.Tie("level-kg", this, &TankProperties::getContent_kg, &TankProperties::setContent_kg );
51 _tiedProperties.Tie("density-kgpm3", this, &TankProperties::getDensity_kgpm3, &TankProperties::setDensity_kgpm3 );
52 _tiedProperties.Tie("capacity-m3", this, &TankProperties::getCapacity_m3, &TankProperties::setCapacity_m3 );
53 _tiedProperties.Tie("unusable-m3", this, &TankProperties::getUnusable_m3, &TankProperties::setUnusable_m3 );
54 _tiedProperties.Tie("level-m3", this, &TankProperties::getContent_m3, &TankProperties::setContent_m3 );
55 _tiedProperties.Tie("level-norm", this, &TankProperties::getContent_norm, &TankProperties::setContent_norm );
57 _tiedProperties.Tie("density-ppg", this, &TankProperties::getDensity_ppg, &TankProperties::setDensity_ppg );
58 _tiedProperties.Tie("level-lbs", this, &TankProperties::getContent_lbs, &TankProperties::setContent_lbs );
59 _tiedProperties.Tie("level-gal_us", this, &TankProperties::getContent_gal_us, &TankProperties::setContent_gal_us );
60 _tiedProperties.Tie("level-gal_imp", this, &TankProperties::getContent_gal_imp, &TankProperties::setContent_gal_imp );
62 _tiedProperties.Tie("capacity-gal_us", this, &TankProperties::getCapacity_gal_us, &TankProperties::setCapacity_gal_us );
63 _tiedProperties.Tie("unusable-gal_us", this, &TankProperties::getUnusable_gal_us, &TankProperties::setUnusable_gal_us );
65 _tiedProperties.Tie("capacity-gal_imp", this, &TankProperties::getCapacity_gal_imp, &TankProperties::setCapacity_gal_imp );
66 _tiedProperties.Tie("unusable-gal_imp", this, &TankProperties::getUnusable_gal_imp, &TankProperties::setUnusable_gal_imp );
68 _tiedProperties.Tie("empty", this, &TankProperties::getEmpty );
71 TankProperties::~TankProperties()
75 void TankProperties::unbind()
77 _tiedProperties.Untie();
80 double TankProperties::getContent_kg() const
85 void TankProperties::setContent_kg( double value )
87 _content_kg = SG_MAX2<double>(value, 0.0);
90 double TankProperties::getDensity_kgpm3() const
92 return _density_kgpm3;
95 void TankProperties::setDensity_kgpm3( double value )
97 _density_kgpm3 = SG_MAX2<double>(value, 0.0);
100 double TankProperties::getDensity_ppg() const
102 return _density_kgpm3 * LBS_PER_KG / USGAL_PER_M3;
105 void TankProperties::setDensity_ppg( double value )
107 _density_kgpm3 = SG_MAX2<double>(value * KG_PER_LBS / M3_PER_USGAL, 0.0);
110 double TankProperties::getContent_lbs() const
112 return _content_kg * LBS_PER_KG;
115 void TankProperties::setContent_lbs( double value )
117 _content_kg = SG_MAX2<double>(value * KG_PER_LBS, 0.0);
120 double TankProperties::getContent_m3() const
122 return _density_kgpm3 > SGLimitsd::min() ? _content_kg / _density_kgpm3 : 0.0;
125 void TankProperties::setContent_m3( double value )
127 // ugly hack to allow setting of a volumetric content without having the density
128 _content_kg = SG_MAX2<double>(value * (_density_kgpm3>0.0?_density_kgpm3:755.0), 0.0);
131 double TankProperties::getContent_gal_us() const
133 return getContent_m3() * USGAL_PER_M3;
136 void TankProperties::setContent_gal_us( double value )
138 setContent_m3( value * M3_PER_USGAL );
141 double TankProperties::getContent_gal_imp() const
143 return getContent_m3() * IMPGAL_PER_M3;
146 void TankProperties::setContent_gal_imp( double value )
148 setContent_m3( value * M3_PER_IMPGAL );
151 double TankProperties::getCapacity_m3() const
156 void TankProperties::setCapacity_m3( double value )
158 _capacity_m3 = SG_MAX2<double>(value, 0.0);
161 double TankProperties::getCapacity_gal_us() const
163 return _capacity_m3 * USGAL_PER_M3;
166 void TankProperties::setCapacity_gal_us( double value )
168 _capacity_m3 = SG_MAX2<double>(value * M3_PER_USGAL, 0.0);
171 double TankProperties::getCapacity_gal_imp() const
173 return _capacity_m3 * IMPGAL_PER_M3;
176 void TankProperties::setCapacity_gal_imp( double value )
178 _capacity_m3 = SG_MAX2<double>(value * M3_PER_IMPGAL, 0.0);
181 double TankProperties::getUnusable_m3() const
186 void TankProperties::setUnusable_m3( double value )
188 _unusable_m3 = SG_MAX2<double>(value, 0.0);
191 double TankProperties::getUnusable_gal_us() const
193 return _unusable_m3 * USGAL_PER_M3;
196 void TankProperties::setUnusable_gal_us( double value )
198 _unusable_m3 = SG_MAX2<double>(value * M3_PER_USGAL, 0.0);
202 double TankProperties::getUnusable_gal_imp() const
204 return _unusable_m3 * IMPGAL_PER_M3;
207 void TankProperties::setUnusable_gal_imp( double value )
209 _unusable_m3 = SG_MAX2<double>(value * M3_PER_IMPGAL, 0.0);
212 double TankProperties::getContent_norm() const
214 return _capacity_m3 > SGLimitsd::min() ? getContent_m3() / _capacity_m3 : 0.0;
217 void TankProperties::setContent_norm( double value )
219 setContent_m3(_capacity_m3 * value);
222 bool TankProperties::getEmpty() const
224 return getContent_m3() <= _unusable_m3;
227 TankPropertiesList::TankPropertiesList( SGPropertyNode_ptr rootNode )
229 // we don't have a global rule how many tanks we support, so I assume eight.
230 // Because hard coded values suck, make it settable by a property.
231 // If tanks were configured, use that number
232 int n = rootNode->getChildren("tank").size();
233 if( n == 0 ) n = rootNode->getIntValue( "numtanks", 8 );
234 for( int i = 0; i < n; i++ ) {
235 push_back( new TankProperties( rootNode->getChild( "tank", i, true ) ) );
238 _tiedProperties.setRoot( rootNode );
241 double TankPropertiesList::getTotalContent_lbs() const
244 for( const_iterator it = begin(); it != end(); ++it )
245 value += (*it)->getContent_lbs();
249 double TankPropertiesList::getTotalContent_kg() const
252 for( const_iterator it = begin(); it != end(); ++it )
253 value += (*it)->getContent_kg();
257 double TankPropertiesList::getTotalContent_gal_us() const
260 for( const_iterator it = begin(); it != end(); ++it )
261 value += (*it)->getContent_gal_us();
265 double TankPropertiesList::getTotalContent_gal_imp() const
268 for( const_iterator it = begin(); it != end(); ++it )
269 value += (*it)->getContent_gal_imp();
273 double TankPropertiesList::getTotalContent_m3() const
276 for( const_iterator it = begin(); it != end(); ++it )
277 value += (*it)->getContent_m3();
281 double TankPropertiesList::getTotalContent_norm() const
283 double content = 0.0;
284 double capacity = 0.0;
285 for( const_iterator it = begin(); it != end(); ++it ) {
286 content += (*it)->getContent_m3();
287 capacity += (*it)->getCapacity_m3();
289 return capacity > SGLimitsd::min() ? content / capacity : 0.0;
292 void TankPropertiesList::bind()
294 _tiedProperties.Tie("total-fuel-kg", this, &TankPropertiesList::getTotalContent_kg );
295 _tiedProperties.Tie("total-fuel-lbs", this, &TankPropertiesList::getTotalContent_lbs );
296 _tiedProperties.Tie("total-fuel-gal_us", this, &TankPropertiesList::getTotalContent_gal_us );
297 _tiedProperties.Tie("total-fuel-gals", this, &TankPropertiesList::getTotalContent_gal_us );
298 _tiedProperties.Tie("total-fuel-gal_imp", this, &TankPropertiesList::getTotalContent_gal_imp );
299 _tiedProperties.Tie("total-fuel-norm", this, &TankPropertiesList::getTotalContent_norm );
300 for( const_iterator it = begin(); it != end(); ++it ) {
305 void TankPropertiesList::unbind()
307 for( const_iterator it = begin(); it != end(); ++it ) {
310 _tiedProperties.Untie();