Mercurial Hosting > traffic-intelligence
annotate include/utils.hpp @ 853:95e7622b11be
added option to limit the number of loaded objects
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 22 Sep 2016 16:49:43 -0400 |
parents | bee0e7407af7 |
children | 6c5ce3ec497e |
rev | line source |
---|---|
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #ifndef UTILS_HPP |
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 #define UTILS_HPP |
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 |
703
bee0e7407af7
corrected compilation bug on linux
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
4 #include <sstream> |
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
|
5 #include <iosfwd> |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
6 #include <string> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
7 #include <vector> |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
8 |
12 | 9 static const double pi = 3.14159265358979323846; |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
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 static const std::string separator = " "; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
12 const char commentChar = '#'; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
13 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
14 /** Loads lines of numbers from a text file, separated by some character */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
15 std::vector<std::vector<float> > loadNumbers(const std::string& filename, const std::string& separator = ::separator); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
16 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
17 /** Gets line in a file, skipping comments (starting with '#') (wrapper around getline). |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
18 * Warning: returns an empty string if all the lines to the end of the file are comments. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
19 std::string getlineComment(std::ifstream& f); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
20 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
21 /** Converts a string to an integer. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
22 int toInt(const std::string& s); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
23 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
24 /** Converts a string to a float. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
25 float toFloat(const std::string& s); |
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 /** Converts a string to a double. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
28 double toDouble(const std::string& s); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
29 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
30 /** Opens file for writing with fixed scientific precision. */ |
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
|
31 void openWriteScientificPrecision(std::ofstream& out, const std::string& filename, const int& precision); |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
32 |
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
|
33 void openWritePrecision(std::ofstream& out, const std::string& filename, const int& precision); |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
34 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
35 /** Opens files and checks how it went. */ |
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
|
36 bool openCheck(std::ifstream& f, const std::string& filename, const std::string& callingFunctionName); |
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
|
37 bool openCheck(std::ofstream& f, const std::string& filename, const std::string& callingFunctionName); |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
38 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
39 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
40 // inline |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
41 |
12 | 42 inline double square(const int& a) { return a*a;} |
43 | |
44 /** Implements key bindings, for example for cvWaitKey(). */ | |
45 inline bool forwardKey(const int& pressedKey) { return (((char)pressedKey) == '+');} | |
46 | |
47 inline bool backwardKey(const int& pressedKey) { return (((char)pressedKey) == '-');} | |
48 | |
49 inline bool saveKey(const int& pressedKey) { return (((char)pressedKey) == 's' || ((char)pressedKey) == 'S');} | |
50 | |
51 inline bool interruptionKey(const int& pressedKey) { return (((char)pressedKey) == 'q' || ((char)pressedKey) == 'Q');} | |
52 | |
53 inline bool skipKey(const int& pressedKey) { return (((char)pressedKey) == 'n' || ((char)pressedKey) == 'N');} | |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
54 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
55 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
56 // templates |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
57 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
58 /** Splits a string on the separator. The resulting string items are added to the result (the list is not cleared). */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
59 template<class Seq> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
60 inline void split(Seq& result, const std::string& text, const std::string& separator = ::separator) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
61 int n = text.length(); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
62 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
63 int start = text.find_first_not_of(separator); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
64 while ((start >= 0) && (start < n)) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
65 int stop = text.find_first_of(separator, start); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
66 if ((stop < 0) || (stop > n)) |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
67 stop = n; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
68 result.push_back(text.substr(start, stop - start)); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
69 start = text.find_first_not_of(separator, stop+1); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
70 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
71 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
72 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
73 /** Converts strings to numbers. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
74 template<typename T> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
75 bool fromString(T & result, const std::string & s) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
76 std::istringstream iss(s); |
703
bee0e7407af7
corrected compilation bug on linux
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
77 iss >> result; |
bee0e7407af7
corrected compilation bug on linux
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
78 return iss.good() || iss.eof(); |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
79 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
80 |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
81 #endif |