Salome HOME
aaab69514333b2b46046f27b9b862bfd57b5bac1
[tools/medcoupling.git] / src / INTERP_KERNEL / ExprEval / InterpKernelUnit.hxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 UnitDataBase
33   {
34   public:
35     INTERPKERNEL_EXPORT UnitDataBase();
36     INTERPKERNEL_EXPORT const short *getInfoForUnit(const std::string& unit, double& addFact, double& mFact) const;
37     INTERPKERNEL_EXPORT static UnitDataBase _uniqueMapForExpr;
38     INTERPKERNEL_EXPORT static const int SIZE_OF_UNIT_BASE=5;
39   private:
40     std::map<std::string,double> _prefix_pow_10;
41     std::map<std::string,const short *> _units_semantic;
42     std::map<std::string,double> _units_mul;
43     std::map<std::string,double> _units_add;
44   private:
45     static const int NB_OF_PREF_POW10=22;
46     static const char *PREF_POW10[NB_OF_PREF_POW10];
47     static const double POW10[NB_OF_PREF_POW10];
48     static const int NB_OF_UNITS_RECOGN=29;
49     static const char *UNITS_RECOGN[NB_OF_UNITS_RECOGN];
50     static const short PROJ_IN_BASE[NB_OF_UNITS_RECOGN][SIZE_OF_UNIT_BASE];
51     static const double MUL_COEFF[NB_OF_UNITS_RECOGN];
52     static const double ADD_COEFF[NB_OF_UNITS_RECOGN];
53   };
54
55   class DecompositionInUnitBase
56   {
57   public:
58     INTERPKERNEL_EXPORT DecompositionInUnitBase();
59     INTERPKERNEL_EXPORT void setInfo(const short *vals, double addFact, double mFact);
60     INTERPKERNEL_EXPORT short operator[](int i) const { return _value[i]; }
61     INTERPKERNEL_EXPORT bool operator==(const DecompositionInUnitBase& other) const;
62     INTERPKERNEL_EXPORT void getTranslationParams(const DecompositionInUnitBase& other, double& mul, double& add) const;
63     INTERPKERNEL_EXPORT bool isEqual(short mass, short lgth, short time, short intensity, short temp,
64                                      double add, double mult);
65     INTERPKERNEL_EXPORT bool isUnitary() const;
66     //! \b WARNING no test is done on the fact that unit is adimensionnal.
67     INTERPKERNEL_EXPORT void negate();
68     INTERPKERNEL_EXPORT bool isAdimensional() const;
69     INTERPKERNEL_EXPORT void tryToConvertInUnit(double val);
70     INTERPKERNEL_EXPORT DecompositionInUnitBase &operator*(const DecompositionInUnitBase& other);
71     INTERPKERNEL_EXPORT DecompositionInUnitBase &operator/(const DecompositionInUnitBase& other);
72     INTERPKERNEL_EXPORT DecompositionInUnitBase &operator^(const DecompositionInUnitBase& other);
73   private:
74     void dealWithAddFactor(const DecompositionInUnitBase& other);
75     static int couldItBeConsideredAsInt(double val);
76     static bool areDoubleEquals(double a, double b);
77     static double powInt(double val, int exp);
78   private:
79     short _value[UnitDataBase::SIZE_OF_UNIT_BASE];
80     double _add_to_base;
81     double _mult_fact_to_base;
82   };
83
84   /*!
85    * This class deals with units.
86    * This class has two main responsibilities :
87    *      - interpret units by giving simply their representation in string type.
88    *      - performing operations on these units.
89    *
90    * All the possible units are represented with a unique tuple with 5 elements
91    * representing the unique decomposition of a unit in the following base.
92    *
93    * dimension 0 stands for mass in g (\b NOT kg to simplify parsing).
94    * dimension 1 stands for length in m.
95    * dimension 2 stands for time in s.
96    * dimension 3 stands for elec intensity A.
97    * dimension 4 stands for temperature in K.
98    */
99   class Unit
100   {
101   public:
102     INTERPKERNEL_EXPORT Unit(const char *reprC, bool tryToInterp=true);
103     INTERPKERNEL_EXPORT Unit(const char *reprFortran, int sizeOfRepr, bool tryToInterp=true);
104     INTERPKERNEL_EXPORT void tryToInterprate() const;
105     INTERPKERNEL_EXPORT bool isInterpretationOK() const;
106     INTERPKERNEL_EXPORT bool isCompatibleWith(const Unit& other) const;
107     INTERPKERNEL_EXPORT double convert(const Unit& target, double sourceVal) const;
108     INTERPKERNEL_EXPORT std::string getCoarseRepr() const;
109   private:
110     std::string _coarse_repr;
111     mutable bool _is_interpreted;
112     mutable bool _is_interpretation_ok;
113     mutable DecompositionInUnitBase _decomp_in_base;
114   };
115 }
116
117 #endif