GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
VectorFloat.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #define GRT_DLL_EXPORTS
22 #include "VectorFloat.h"
23 #include "../Util/FileParser.h"
24 
25 GRT_BEGIN_NAMESPACE
26 
28  warningLog.setKey("[WARNING VectorFloat]");
29  errorLog.setKey("[ERROR VectorFloat]");
30 }
31 
32 VectorFloat::VectorFloat(const size_type size){
33  warningLog.setKey("[WARNING VectorFloat]");
34  errorLog.setKey("[ERROR VectorFloat]");
35  resize( size );
36 }
37 
38 VectorFloat::VectorFloat( const size_type size, const Float &value ){
39  warningLog.setKey("[WARNING VectorFloat]");
40  errorLog.setKey("[ERROR VectorFloat]");
41  resize( size, value );
42 }
43 
45  warningLog.setKey("[WARNING VectorFloat]");
46  errorLog.setKey("[ERROR VectorFloat]");
47 }
48 
50  clear();
51 }
52 
54  if( this != &rhs ){
55  UINT N = rhs.getSize();
56  if( N > 0 ){
57  resize( N );
58  std::copy( rhs.begin(), rhs.end(), this->begin() );
59  }else this->clear();
60  }
61  return *this;
62 }
63 
65  if( this != &rhs ){
66  UINT N = rhs.getSize();
67  if( N > 0 ){
68  resize( N );
69  std::copy( rhs.begin(), rhs.end(), this->begin() );
70  }else this->clear();
71  }
72  return *this;
73 }
74 
75 bool VectorFloat::print(const std::string title) const {
76 
77  if( title != "" ){
78  std::cout << title << std::endl;
79  }
80  const size_type size = this->size();
81  const Float *data = getData();
82  for(size_type i=0; i<size; i++){
83  std::cout << data[i] << "\t";
84  }
85  std::cout << std::endl;
86 
87  return true;
88 }
89 
90 bool VectorFloat::scale( const Float minTarget, const Float maxTarget, const bool constrain ){
91 
92  MinMax range = getMinMax();
93 
94  return scale( range.minValue, range.maxValue, minTarget, maxTarget, constrain );
95 }
96 
97 bool VectorFloat::scale( const Float minSource, const Float maxSource, const Float minTarget, const Float maxTarget, const bool constrain ){
98 
99  const size_type N = this->size();
100 
101  if( N == 0 ){
102  return false;
103  }
104 
105  size_type i = 0;
106  Float *data = getData();
107  for( i=0; i<N; i++ ){
108  data[i] = grt_scale(data[i],minSource,maxSource,minTarget,maxTarget,constrain);
109  }
110 
111  return true;
112 }
113 
115  Float minValue = 99e+99;
116  const size_type N = this->size();
117  const Float *data = getData();
118  for(size_type i=0; i<N; i++){
119  if( data[i] < minValue ) minValue = data[i];
120  }
121  return minValue;
122 }
123 
125  Float maxValue = -99e99;
126  const size_type N = this->size();
127  const Float *data = getData();
128  for(size_type i=0; i<N; i++){
129  if( data[i] > maxValue ) maxValue = data[i];
130  }
131  return maxValue;
132 }
133 
134 Float VectorFloat::getMean() const {
135 
136  Float mean = 0.0;
137  const size_type N = this->size();
138  const Float *data = getData();
139  for(size_type i=0; i<N; i++){
140  mean += data[i];
141  }
142  mean /= N;
143 
144  return mean;
145 }
146 
147 Float VectorFloat::getStdDev() const {
148 
149  Float mean = getMean();
150  Float stdDev = 0.0;
151  const size_type N = this->size();
152  const Float *data = getData();
153 
154  for(size_type i=0; i<N; i++){
155  stdDev += grt_sqr(data[i]-mean);
156  }
157  stdDev = grt_sqrt( stdDev / Float(N-1) );
158 
159  return stdDev;
160 }
161 
163 
164  const size_type N = this->size();
165  MinMax range;
166 
167  if( N == 0 ) return range;
168 
169  const Float *data = getData();
170 
171  for(size_type i=0; i<N; i++){
172  range.updateMinMax( data[ i ] );
173  }
174 
175  return range;
176 }
177 
178 bool VectorFloat::save(const std::string &filename) const {
179 
180  const size_type N = this->size();
181 
182  if( N == 0 ){
183  warningLog << "save(...) - Vector is empty, nothing to save!" << std::endl;
184  return false;
185  }
186 
187  std::fstream file;
188  file.open(filename.c_str(), std::ios::out);
189 
190  if( !file.is_open() ){
191  return false;
192  }
193 
194  const Float *data = getData();
195  for(size_type i=0; i<N; i++){
196  file << data[i] << (i<N-1 ? "," : "\n");
197  }
198 
199  file.close();
200 
201  return true;
202 }
203 
204 bool VectorFloat::load(const std::string &filename,const char seperator){
205 
206  //Clear any previous data
207  clear();
208 
209  //Open the file
210  std::ifstream file( filename.c_str(), std::ifstream::in );
211  if ( !file.is_open() ){
212  warningLog << "load(...) - Failed to open file: " << filename << std::endl;
213  return false;
214  }
215 
217  Vector< Float > row;
218  std::string line;
219  std::string columnString = "";
220  const int sepValue = seperator;
221  unsigned int numElements = 0;
222  size_t length = 0;
223 
224  //Get the data, for a vector it should just be one line
225  if( !getline(file,line) ){
226  warningLog << "load(...) - Failed to read first row!" << std::endl;
227  return false;
228  }
229 
230  //Scan the line contents for the seperator token and parse the contents between each token
231  columnString = "";
232  length = line.length();
233  vec.reserve( length );
234  for(size_t i=0; i<length; i++){
235  if( int(line[i]) == sepValue ){
236  vec.push_back( columnString );
237  columnString = "";
238  }else columnString += line[i];
239  }
240 
241  //Add the last column
242  vec.push_back( columnString );
243 
244  //Remove the new line character from the string in the last column
245  if( vec.size() >= 1 ){
246  size_t K = vec.size()-1;
247  size_t foundA = vec[ K ].find('\n');
248  size_t foundB = vec[K ].find('\r');
249  if( foundA != std::string::npos || foundB != std::string::npos ){
250  vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
251  }
252  }
253 
254  numElements = vec.getSize();
255 
256  //Assign the memory
257  if( !resize( numElements ) ){
258  warningLog << "load(...) - Failed to resize memory!" << std::endl;
259  return false;
260  }
261 
262  //Convert the string column values to Float values
263  Float *data = getData();
264  for(unsigned int i=0; i<numElements; i++){
265  data[i] = grt_from_str< Float >( vec[i] );
266  }
267 
268  //Close the file
269  file.close();
270 
271  return true;
272 }
273 
274 GRT_END_NAMESPACE
GRT_API Float getMaxValue() const
GRT_API bool save(const std::string &filename) const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool setKey(const std::string &key)
sets the key that gets written at the start of each message, this will be written in the format &#39;key ...
Definition: Log.h:166
UINT getSize() const
Definition: Vector.h:201
GRT_API bool load(const std::string &filename, const char seperator= ',')
GRT_API Float getMinValue() const
GRT_API bool print(const std::string title="") const
Definition: VectorFloat.cpp:75
GRT_API VectorFloat & operator=(const VectorFloat &rhs)
Definition: VectorFloat.cpp:53
GRT_API VectorFloat()
Definition: VectorFloat.cpp:27
GRT_API bool scale(const Float minTarget, const Float maxTarget, const bool constrain=true)
Definition: VectorFloat.cpp:90
virtual GRT_API ~VectorFloat()
Definition: VectorFloat.cpp:49
Definition: MinMax.h:29
Float * getData()
Definition: Vector.h:208
Definition: Vector.h:41
GRT_API Float getMean() const
GRT_API Float getStdDev() const
GRT_API MinMax getMinMax() const