Salome HOME
Merge from V6_main (04/10/2012)
[modules/med.git] / src / INTERP_KERNEL / ExprEval / InterpKernelUnit.hxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __INTERPKERNELUNIT_HXX__
22 #define __INTERPKERNELUNIT_HXX__
23
24 #include "INTERPKERNELDefines.hxx"
25 #include "InterpKernelException.hxx"
26
27 #include <map>
28 #include <sstream>
29
30 namespace INTERP_KERNEL
31 {
32   class INTERPKERNEL_EXPORT UnitDataBase
33   {
34   public:
35     UnitDataBase();
36     const short *getInfoForUnit(const std::string& unit,
37                                 double& addFact, double& mFact) const throw(INTERP_KERNEL::Exception);
38     static UnitDataBase _uniqueMapForExpr;
39     static const int SIZE_OF_UNIT_BASE=5;
40   private:
41     std::map<std::string,double> _prefix_pow_10;
42     std::map<std::string,const short *> _units_semantic;
43     std::map<std::string,double> _units_mul;
44     std::map<std::string,double> _units_add;
45   private:
46     static const int NB_OF_PREF_POW10=22;
47     static const char *PREF_POW10[NB_OF_PREF_POW10];
48     static const double POW10[NB_OF_PREF_POW10];
49     static const int NB_OF_UNITS_RECOGN=29;
50     static const char *UNITS_RECOGN[NB_OF_UNITS_RECOGN];
51     static const short PROJ_IN_BASE[NB_OF_UNITS_RECOGN][SIZE_OF_UNIT_BASE];
52     static const double MUL_COEFF[NB_OF_UNITS_RECOGN];
53     static const double ADD_COEFF[NB_OF_UNITS_RECOGN];
54   };
55
56   class INTERPKERNEL_EXPORT DecompositionInUnitBase
57   {
58   public:
59     DecompositionInUnitBase();
60     void setInfo(const short *vals, double addFact, double mFact);
61     short operator[](int i) const { return _value[i]; }
62     bool operator==(const DecompositionInUnitBase& other) const;
63     void getTranslationParams(const DecompositionInUnitBase& other, double& mul, double& add) const;
64     bool isEqual(short mass, short lgth, short time, short intensity, short temp,
65                  double add, double mult);
66     bool isUnitary() const;
67     //! \b WARNING no test is done on the fact that unit is adimensionnal.
68     void negate();
69     bool isAdimensional() const;
70     void tryToConvertInUnit(double val) throw(INTERP_KERNEL::Exception);
71     DecompositionInUnitBase &operator*(const DecompositionInUnitBase& other);
72     DecompositionInUnitBase &operator/(const DecompositionInUnitBase& other);
73     DecompositionInUnitBase &operator^(const DecompositionInUnitBase& other) throw(INTERP_KERNEL::Exception);
74   private:
75     void dealWithAddFactor(const DecompositionInUnitBase& other);
76     static int couldItBeConsideredAsInt(double val) throw(INTERP_KERNEL::Exception);
77     static bool areDoubleEquals(double a, double b);
78     static double powInt(double val, int exp);
79   private:
80     short _value[UnitDataBase::SIZE_OF_UNIT_BASE];
81     double _add_to_base;
82     double _mult_fact_to_base;
83   };
84
85   /*!
86    * This class deals with units.
87    * This class has two main responsabilities :
88    *      - interprete units by giving simply their representation in string type.
89    *      - performing operations on these units.
90    *
91    * All the possible units are represented with a unique tuple with 5 elements
92    * representing the unique decomposition of a unit in the following base.
93    *
94    * dimension 0 stands for mass in g (\b NOT kg to simplify parsing).
95    * dimension 1 stands for length in m.
96    * dimension 2 stands for time in s.
97    * dimension 3 stands for elec intensity A.
98    * dimension 4 stands for temperature in K.
99    */
100   class INTERPKERNEL_EXPORT Unit
101   {
102   public:
103     Unit(const char *reprC, bool tryToInterp=true);
104     Unit(const char *reprFortran, int sizeOfRepr, bool tryToInterp=true);
105     void tryToInterprate() const;
106     bool isInterpretationOK() const;
107     bool isCompatibleWith(const Unit& other) const;
108     double convert(const Unit& target, double sourceVal) const;
109     std::string getCoarseRepr() const;
110   private:
111     std::string _coarse_repr;
112     mutable bool _is_interpreted;
113     mutable bool _is_interpretation_ok;
114     mutable DecompositionInUnitBase _decomp_in_base;
115   };
116 }
117
118 #endif