GestureRecognitionToolkit  Version: 0.1.0
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 #include "VectorFloat.h"
22 #include "../Util/FileParser.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
27  warningLog.setProceedingText("[WARNING VectorFloat]");
28  errorLog.setProceedingText("[ERROR VectorFloat]");
29 }
30 
31 VectorFloat::VectorFloat(const unsigned int size){
32  warningLog.setProceedingText("[WARNING VectorFloat]");
33  errorLog.setProceedingText("[ERROR VectorFloat]");
34  resize( size );
35 }
36 
37 VectorFloat::VectorFloat( const unsigned int size, const Float &value ){
38  warningLog.setProceedingText("[WARNING VectorFloat]");
39  errorLog.setProceedingText("[ERROR VectorFloat]");
40  resize( size, value );
41 }
42 
44  warningLog.setProceedingText("[WARNING VectorFloat]");
45  errorLog.setProceedingText("[ERROR VectorFloat]");
46 }
47 
49  clear();
50 }
51 
53  if( this != &rhs ){
54  UINT N = rhs.getSize();
55  if( N > 0 ){
56  resize( N );
57  std::copy( rhs.begin(), rhs.end(), this->begin() );
58  }else this->clear();
59  }
60  return *this;
61 }
62 
64  if( this != &rhs ){
65  UINT N = rhs.getSize();
66  if( N > 0 ){
67  resize( N );
68  std::copy( rhs.begin(), rhs.end(), this->begin() );
69  }else this->clear();
70  }
71  return *this;
72 }
73 
74 bool VectorFloat::print(const std::string title) const {
75 
76  if( title != "" ){
77  std::cout << title << std::endl;
78  }
79  const size_t size = this->size();
80  const Float *data = getData();
81  for(size_t i=0; i<size; i++){
82  std::cout << data[i] << "\t";
83  }
84  std::cout << std::endl;
85 
86  return true;
87 }
88 
89 bool VectorFloat::scale( const Float minTarget, const Float maxTarget, const bool constrain ){
90 
91  MinMax range = getMinMax();
92 
93  return scale( range.minValue, range.maxValue, minTarget, maxTarget, constrain );
94 }
95 
96 bool VectorFloat::scale( const Float minSource, const Float maxSource, const Float minTarget, const Float maxTarget, const bool constrain ){
97 
98  const size_t N = this->size();
99 
100  if( N == 0 ){
101  return false;
102  }
103 
104  unsigned int i = 0;
105  Float *data = getData();
106  for( i=0; i<N; i++ ){
107  data[i] = grt_scale(data[i],minSource,maxSource,minTarget,maxTarget,constrain);
108  }
109 
110  return true;
111 }
112 
114  Float minValue = 99e+99;
115  const size_t N = this->size();
116  const Float *data = getData();
117  for(size_t i=0; i<N; i++){
118  if( data[i] < minValue ) minValue = data[i];
119  }
120  return minValue;
121 }
122 
124  Float maxValue = -99e99;
125  const size_t N = this->size();
126  const Float *data = getData();
127  for(size_t i=0; i<N; i++){
128  if( data[i] > maxValue ) maxValue = data[i];
129  }
130  return maxValue;
131 }
132 
133 Float VectorFloat::getMean() const {
134 
135  Float mean = 0.0;
136  const size_t N = this->size();
137  const Float *data = getData();
138  for(size_t i=0; i<N; i++){
139  mean += data[i];
140  }
141  mean /= N;
142 
143  return mean;
144 }
145 
146 Float VectorFloat::getStdDev() const {
147 
148  Float mean = getMean();
149  Float stdDev = 0.0;
150  const size_t N = this->size();
151  const Float *data = getData();
152 
153  for(size_t i=0; i<N; i++){
154  stdDev += grt_sqr(data[i]-mean);
155  }
156  stdDev = grt_sqrt( stdDev / Float(N-1) );
157 
158  return stdDev;
159 }
160 
162 
163  const size_t N = this->size();
164  MinMax range;
165 
166  if( N == 0 ) return range;
167 
168  const Float *data = getData();
169 
170  for(unsigned int i=0; i<N; i++){
171  range.updateMinMax( data[ i ] );
172  }
173 
174  return range;
175 }
176 
177 bool VectorFloat::save(const std::string &filename) const {
178 
179  const size_t N = this->size();
180 
181  if( N == 0 ){
182  warningLog << "save(...) - Vector is empty, nothing to save!" << std::endl;
183  return false;
184  }
185 
186  std::fstream file;
187  file.open(filename.c_str(), std::ios::out);
188 
189  if( !file.is_open() ){
190  return false;
191  }
192 
193  const Float *data = getData();
194  for(size_t i=0; i<N; i++){
195  file << data[i] << (i<N-1 ? "," : "\n");
196  }
197 
198  file.close();
199 
200  return true;
201 }
202 
203 bool VectorFloat::load(const std::string &filename,const char seperator){
204 
205  //Clear any previous data
206  clear();
207 
208  //Open the file
209  std::ifstream file( filename.c_str(), std::ifstream::in );
210  if ( !file.is_open() ){
211  warningLog << "load(...) - Failed to open file: " << filename << std::endl;
212  return false;
213  }
214 
216  Vector< Float > row;
217  std::string line;
218  std::string columnString = "";
219  const int sepValue = seperator;
220  unsigned int numElements = 0;
221  unsigned int length = 0;
222 
223  //Get the data, for a vector it should just be one line
224  if( !getline(file,line) ){
225  warningLog << "load(...) - Failed to read first row!" << std::endl;
226  return false;
227  }
228 
229  //Scan the line contents for the seperator token and parse the contents between each token
230  columnString = "";
231  length = line.length();
232  vec.reserve( length );
233  for(unsigned int i=0; i<length; i++){
234  if( int(line[i]) == sepValue ){
235  vec.push_back( columnString );
236  columnString = "";
237  }else columnString += line[i];
238  }
239 
240  //Add the last column
241  vec.push_back( columnString );
242 
243  //Remove the new line character from the string in the last column
244  if( vec.size() >= 1 ){
245  size_t K = vec.size()-1;
246  size_t foundA = vec[ K ].find('\n');
247  size_t foundB = vec[K ].find('\r');
248  if( foundA != std::string::npos || foundB != std::string::npos ){
249  vec[ K ] = vec[ K ].substr(0,vec[ K ].length()-1);
250  }
251  }
252 
253  numElements = vec.getSize();
254 
255  //Assign the memory
256  if( !resize( numElements ) ){
257  warningLog << "load(...) - Failed to resize memory!" << std::endl;
258  return false;
259  }
260 
261  //Convert the string column values to Float values
262  Float *data = getData();
263  for(unsigned int i=0; i<numElements; i++){
264  data[i] = grt_from_str< Float >( vec[i] );
265  }
266 
267  //Close the file
268  file.close();
269 
270  return true;
271 }
272 
273 GRT_END_NAMESPACE
Float getMaxValue() const
bool save(const std::string &filename) const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
bool load(const std::string &filename, const char seperator= ',')
Float getMinValue() const
bool print(const std::string title="") const
Definition: VectorFloat.cpp:74
unsigned int getSize() const
Definition: Vector.h:193
VectorFloat & operator=(const VectorFloat &rhs)
Definition: VectorFloat.cpp:52
bool scale(const Float minTarget, const Float maxTarget, const bool constrain=true)
Definition: VectorFloat.cpp:89
virtual ~VectorFloat()
Definition: VectorFloat.cpp:48
Definition: MinMax.h:29
Float * getData()
Definition: Vector.h:200
Definition: Vector.h:41
Float getMean() const
Float getStdDev() const
MinMax getMinMax() const