annotate c/utils.cpp @ 145:7bf8084e720f

removed bug with loadMat and added diagnosis code for empty frames
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 29 Aug 2011 19:20:37 -0400
parents b32947b002da
children c8a149fccfda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
1 #include "utils.hpp"
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
2
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
3 #include <boost/foreach.hpp>
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
4
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
5 #include <iostream>
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
6 #include <fstream>
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
7 #include <sstream>
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
8
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
9 using namespace std;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
10
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
11 vector<vector<float> > loadNumbers(const string& filename, const string& separator /* = " " */) {
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
12 ifstream in(filename.c_str());
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
13 vector<vector<float> > result;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
14
145
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
15 if (::openCheck(in, filename, "loadNumbers")) {
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
16 while (!in.eof()) {
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
17 string line = ::getlineComment(in);
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
18 vector<string> tokens;
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
19 ::split(tokens, line, separator);
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
20 vector<float> numbers;
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
21 BOOST_FOREACH(string s, tokens)
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
22 numbers.push_back(::toInt(s));
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
23 if (!numbers.empty())
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
24 result.push_back(numbers);
7bf8084e720f removed bug with loadMat and added diagnosis code for empty frames
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 144
diff changeset
25 }
144
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
26 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
27
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
28 return result;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
29 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
30
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
31 int toInt(const std::string& s) { int i; fromString(i, s); return i;} //atoi
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
32
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
33 float toFloat(const std::string& s) { float x; fromString(x, s); return x;}// lexical_cast<float>(s)
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
34
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
35 double toDouble(const std::string& s) { double x; fromString(x, s); return x;}
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
36
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
37 string getlineComment(ifstream& f) {
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
38 string s;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
39 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
40 while ((!f.eof()) && (s[0] == ::commentChar)) {
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
41 getline(f, s);
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
42 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
43
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
44 if (s[0] == ::commentChar)
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
45 s.clear();
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
46 return s;
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
47 }
b32947b002da added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 70
diff changeset
48
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
49 void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
50 ::openWritePrecision(out, filename, precision);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
51 out.setf(ios::scientific);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
52 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
53
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
54 void openWritePrecision(ofstream& out, const string& filename, const int& precision) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
55 out.open(filename.c_str(), ios::binary);
70
a52653dca25d got track features to compile, updated paths to headers and libraries for OpenCV 2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 20
diff changeset
56 ::openCheck(out, filename, "openWritePrecision");
20
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
57 out.precision(precision);
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
58 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
59
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
60 bool openCheck(ifstream& f, const string& filename, const string& callingFunctionName) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
61 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
62 cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
63 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
64 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
65 return true;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
66 }
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
67
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
68 bool openCheck(ofstream& f, const string& filename, const string& callingFunctionName) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
69 if (!f.is_open()) {
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
70 cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
71 return false;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
72 } else
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
73 return true;
ef0d7caf8e91 draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 3
diff changeset
74 }