unit_converter.cpp
Go to the documentation of this file.
1 //======================================================================
28 //======================================================================
29 
30 //----------------------------------------------------------------------
31 // System Includes - include with <>
32 //----------------------------------------------------------------------
33 
34 #include <assert.h>
35 #include <iostream>
36 
37 using namespace std;
38 
39 //----------------------------------------------------------------------
40 // Project Includes - include with ""
41 //----------------------------------------------------------------------
42 
43 
44 #include "unit_converter.h"
45 
47 
48 //----------------------------------------------------------------------
49 // Defines, enums, unions, structs,
50 //----------------------------------------------------------------------
51 
52 
53 //----------------------------------------------------------------------
54 // Global variables
55 //----------------------------------------------------------------------
56 
57 
58 //----------------------------------------------------------------------
59 // Function definitions
60 //----------------------------------------------------------------------
61 
62 
63 //----------------------------------------------------------------------
64 // Class definitions
65 //----------------------------------------------------------------------
66 
67 
68 cUnitConverter::cUnitConverter( char const* _kind, char const* _name, char const* _symbol, double _factor, double _offset, int _decimal_places )
69 {
70  assert(_factor != 0.0); // must not be 0 since we devide by it
71 
72  kind = _kind;
73  name = _name;
74  symbol = _symbol;
75  factor = _factor;
76  offset = _offset;
77  decimal_places = _decimal_places;
78 }
79 //----------------------------------------------------------------------
80 
81 
82 double cUnitConverter::ToExternal( double internal ) const
83 {
84  return internal * factor + offset;
85 }
86 //----------------------------------------------------------------------
87 
88 
90 {
91  cSimpleVector rv;
92 
93  for ( int i = 0; i < internal.eNUMBER_OF_ELEMENTS; i++ )
94  {
95  if ( internal.Valid( i ) )
96  rv[i] = internal[i] * factor + offset;
97  }
98  return rv;
99 }
100 //----------------------------------------------------------------------
101 
102 
103 std::vector<double> cUnitConverter::ToExternal( std::vector<double> const& internal ) const
104 {
105  std::vector<double> rv;
106 
107  for ( std::vector<double>::const_iterator vi = internal.begin();
108  vi != internal.end();
109  vi++ )
110  {
111  rv.push_back( *vi * factor + offset );
112  }
113  return rv;
114 }
115 //----------------------------------------------------------------------
116 
117 
118 double cUnitConverter::ToInternal( double external ) const
119 {
120  return (external - offset) / factor;
121 }
122 //----------------------------------------------------------------------
123 
124 
126 {
127  cSimpleVector rv;
128 
129  for ( int i = 0; i < external.eNUMBER_OF_ELEMENTS; i++ )
130  {
131  if ( external.Valid( i ) )
132  rv[i] = (external[i] - offset) / factor;
133  }
134  return rv;
135 }
136 //----------------------------------------------------------------------
137 
138 
139 std::vector<double> cUnitConverter::ToInternal( std::vector<double> const& external ) const
140 {
141  std::vector<double> rv;
142 
143  for ( std::vector<double>::const_iterator vi = external.begin();
144  vi != external.end();
145  vi++ )
146  {
147  rv.push_back( ( *vi - offset) / factor );
148  }
149  return rv;
150 }
151 //----------------------------------------------------------------------
152 
153 
154 char const* cUnitConverter::GetKind( void ) const
155 {
156  return kind;
157 }
158 //----------------------------------------------------------------------
159 
160 
161 char const* cUnitConverter::GetName( void ) const
162 {
163  return name;
164 }
165 //----------------------------------------------------------------------
166 
167 
168 char const* cUnitConverter::GetSymbol( void ) const
169 {
170  return symbol;
171 }
172 //----------------------------------------------------------------------
173 
174 
175 double cUnitConverter::GetFactor( void ) const
176 {
177  return factor;
178 }
179 //----------------------------------------------------------------------
180 
181 
182 
183 double cUnitConverter::GetOffset( void ) const
184 {
185  return offset;
186 }
187 //----------------------------------------------------------------------
188 
189 
190 
192 {
193  return decimal_places;
194 }
195 //======================================================================
196 
198 
199 cUnitConverter const uc_identity( "any", "any", "?", 1.0, 0.0, 4 );
200 
202 
203 
204 //======================================================================
205 /*
206  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
207  (e.g. to explicitely set C++ mode for *.h header files)
208 
209  Local Variables:
210  mode:C++
211  mode:ELSE
212  End:
213 */
214 //======================================================================
uc_identity
NAMESPACE_SDH_START const cUnitConverter uc_identity("any", "any", "?", 1.0, 0.0, 4)
USING_NAMESPACE_SDH
#define USING_NAMESPACE_SDH
Definition: sdhlibrary_settings.h:81
cUnitConverter::GetSymbol
char const * GetSymbol(void) const
Return the symbol of the external unit (something like "deg" or "ms")
Definition: unit_converter.cpp:168
NAMESPACE_SDH_END
#define NAMESPACE_SDH_END
Definition: sdhlibrary_settings.h:80
cSimpleVector
A simple vector implementation.
Definition: simplevector.h:91
unit_converter.h
Interface of class #SDH::cUnitConverter.
cUnitConverter::ToInternal
double ToInternal(double external) const
Definition: unit_converter.cpp:118
cSimpleVector::eNUMBER_OF_ELEMENTS
@ eNUMBER_OF_ELEMENTS
number of elements in vector
Definition: simplevector.h:97
cUnitConverter::GetFactor
double GetFactor(void) const
Return the conversion factor from internal to external units.
Definition: unit_converter.cpp:175
NAMESPACE_SDH_START
#define NAMESPACE_SDH_START
Definition: sdhlibrary_settings.h:79
cUnitConverter::GetDecimalPlaces
int GetDecimalPlaces(void) const
Return the number of decimal places for printing values in the external unit system.
Definition: unit_converter.cpp:191
cUnitConverter::ToExternal
double ToExternal(double internal) const
Definition: unit_converter.cpp:82
cUnitConverter::cUnitConverter
cUnitConverter(char const *_kind, char const *_name, char const *_symbol, double _factor=1.0, double _offset=0.0, int _decimal_places=1)
Definition: unit_converter.cpp:68
cUnitConverter::GetOffset
double GetOffset(void) const
Return the conversion offset from internal to external units.
Definition: unit_converter.cpp:183
cSimpleVector::Valid
bool Valid(unsigned int index) const
Return true if vector element index is valid (has been accessed at least once)
Definition: simplevector.cpp:168
cUnitConverter::GetName
char const * GetName(void) const
Return the name of the external unit (something like "degrees" or "milliseconds")
Definition: unit_converter.cpp:161
cUnitConverter
Unit conversion class to convert values between physical unit systems.
Definition: unit_converter.h:75
cUnitConverter::GetKind
char const * GetKind(void) const
Return the kind of unit converted (something like "angle" or "time")
Definition: unit_converter.cpp:154


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:58