Mercurial Hosting > traffic-intelligence
annotate include/utils.hpp @ 1022:b7689372c0ec
renamed quantile to centile
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 06 Jun 2018 10:35:06 -0400 |
parents | 6c5ce3ec497e |
children |
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 |
1002
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
21 /** Get relative filename if not absolute */ |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
22 std::string getRelativeFilename(const std::string& parentDirname, const std::string& filename); |
6c5ce3ec497e
improved handling of path for feature based tracking
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
703
diff
changeset
|
23 |
144
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 an integer. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
25 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
|
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 float. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
28 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
|
29 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
30 /** 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
|
31 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
|
32 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
33 /** 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
|
34 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
|
35 |
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 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
|
37 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
38 /** 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
|
39 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
|
40 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
|
41 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
42 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
43 // inline |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
44 |
12 | 45 inline double square(const int& a) { return a*a;} |
46 | |
47 /** Implements key bindings, for example for cvWaitKey(). */ | |
48 inline bool forwardKey(const int& pressedKey) { return (((char)pressedKey) == '+');} | |
49 | |
50 inline bool backwardKey(const int& pressedKey) { return (((char)pressedKey) == '-');} | |
51 | |
52 inline bool saveKey(const int& pressedKey) { return (((char)pressedKey) == 's' || ((char)pressedKey) == 'S');} | |
53 | |
54 inline bool interruptionKey(const int& pressedKey) { return (((char)pressedKey) == 'q' || ((char)pressedKey) == 'Q');} | |
55 | |
56 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
|
57 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
58 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
59 // templates |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
60 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
61 /** 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
|
62 template<class Seq> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
63 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
|
64 int n = text.length(); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
65 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 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
|
70 stop = n; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
71 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
|
72 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
|
73 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
74 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
75 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
76 /** Converts strings to numbers. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
77 template<typename T> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
78 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
|
79 std::istringstream iss(s); |
703
bee0e7407af7
corrected compilation bug on linux
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
80 iss >> result; |
bee0e7407af7
corrected compilation bug on linux
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
81 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
|
82 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
83 |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
84 #endif |