Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

GEnergy.h

00001 /***************************************************************************
00002  *   Copyright (C) 2003-2004 by Raphael Langerhorst                        *
00003  *   raphael-langerhorst@gmx.at                                            *
00004  *                                                                         *
00005  *   Permission is hereby granted, free of charge, to any person obtaining *
00006  *   a copy of this software and associated documentation files (the       *
00007  *   "Software"), to deal in the Software without restriction, including   *
00008  *   without limitation the rights to use, copy, modify, merge, publish,   *
00009  *   distribute, sublicense, and/or sell copies of the Software, and to    *
00010  *   permit persons to whom the Software is furnished to do so, subject to *
00011  *   the following conditions:                                             *
00012  *                                                                         *
00013  *   The above copyright notice and this permission notice shall be        *
00014  *   included in all copies or substantial portions of the Software.       *
00015  *                                                                         *
00016  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
00017  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
00018  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
00019  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
00020  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
00021  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
00022  *   OTHER DEALINGS IN THE SOFTWARE.                                       *
00023  ***************************************************************************/
00024 
00025 
00026 #ifndef GENERGYH
00027 #define GENERGYH
00028 
00029 #include <qmutex.h>
00030 
00031 namespace GCS
00032 {
00033 
00069 class GEnergy : public QMutex
00070 {
00071   protected:
00072 
00081     double Level;
00082 
00088     double Amount;
00089 
00101     double Sigma;
00102 
00103 
00104   public:
00105   
00109     GEnergy()
00110     : Level(0),
00111       Amount(0),
00112       Sigma(0)
00113     {}
00114 
00118     GEnergy(double level, double amount, double sigma)
00119     : Level(level),
00120       Amount(amount),
00121       Sigma(sigma)
00122     {}
00123     
00127     GEnergy(const GEnergy& original)
00128     : QMutex(),
00129       Level(original.Level),
00130       Amount(original.Amount),
00131       Sigma(original.Sigma)
00132     {}
00133       
00134     
00142     virtual ~GEnergy()
00143     {}
00144     
00148     double level() const {return Level;}
00149     
00153     double amount() const {return Amount;}
00154     
00158     double sigma() const {return Sigma;}
00159     
00163     void set(double level, double amount, double sigma)
00164     {
00165       this->Level = level;
00166       this->Amount = amount;
00167       this->Sigma = sigma;
00168     }
00169     
00173     void set(const GEnergy original)
00174     {
00175       set(original.Level,original.Amount,original.Sigma);
00176     }
00177     
00181     void operator = (const GEnergy& original)
00182     {
00183       set(original);
00184     }
00185 
00192     GEnergy take(double fraction)
00193     {
00194       if (fraction>1)
00195         fraction=1;
00196       double difference = Amount*fraction;
00197       Amount -=difference;
00198       return GEnergy(Level,difference,Sigma);
00199     }
00200     
00208     void put(GEnergy& energy)
00209     {
00210       if (energy.Amount<0)
00211         return;
00212         
00213       double fraction = energy.Amount / (this->Amount + energy.Amount);
00214       double d_Level = energy.Level - this->Level;
00215       double d_Sigma = energy.Sigma - this->Sigma;
00216       
00217       this->Amount += energy.Amount;
00218       this->Level += d_Level*fraction;
00219       
00220       //@todo: improve sigma addition
00221       double d_Level_abs = d_Level;
00222       if (d_Level_abs<0)
00223         d_Level_abs = -d_Level_abs;
00224       this->Sigma += (d_Sigma + d_Level_abs)*fraction;
00225       
00226       //now the energy is absorbed...
00227       energy.Level = this->Level;
00228       energy.Amount = 0;
00229       energy.Sigma = this->Sigma;
00230     }
00231 };
00232 
00233 
00234 }
00235 
00236 #endif

Generated on Thu Oct 21 21:18:03 2004 for G System by doxygen 1.3.6