Mercurial Hosting > traffic-intelligence
annotate include/utils.hpp @ 300:f65b828e5521
working on trajectory simulation
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 13 Feb 2013 18:26:49 -0500 |
parents | b32947b002da |
children | bee0e7407af7 |
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 |
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
|
4 #include <iosfwd> |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
5 #include <string> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
6 #include <vector> |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
7 |
12 | 8 static const double pi = 3.14159265358979323846; |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
9 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
10 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
|
11 const char commentChar = '#'; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
12 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
13 /** 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
|
14 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
|
15 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
16 /** 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
|
17 * 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
|
18 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
|
19 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
20 /** 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
|
21 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
|
22 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
23 /** 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
|
24 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
|
25 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
26 /** 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
|
27 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
|
28 |
20
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
29 /** 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
|
30 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
|
31 |
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
|
32 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
|
33 |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
34 /** 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
|
35 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
|
36 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
|
37 |
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 // inline |
ef0d7caf8e91
draft code for feature saving (UBC format)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
40 |
12 | 41 inline double square(const int& a) { return a*a;} |
42 | |
43 /** Implements key bindings, for example for cvWaitKey(). */ | |
44 inline bool forwardKey(const int& pressedKey) { return (((char)pressedKey) == '+');} | |
45 | |
46 inline bool backwardKey(const int& pressedKey) { return (((char)pressedKey) == '-');} | |
47 | |
48 inline bool saveKey(const int& pressedKey) { return (((char)pressedKey) == 's' || ((char)pressedKey) == 'S');} | |
49 | |
50 inline bool interruptionKey(const int& pressedKey) { return (((char)pressedKey) == 'q' || ((char)pressedKey) == 'Q');} | |
51 | |
52 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
|
53 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
54 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
55 // templates |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
56 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
57 /** 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
|
58 template<class Seq> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
59 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
|
60 int n = text.length(); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
61 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
62 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
|
63 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
|
64 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
|
65 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
|
66 stop = n; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
67 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
|
68 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
|
69 } |
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 /** Converts strings to numbers. */ |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
73 template<typename T> |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
74 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
|
75 std::istringstream iss(s); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
76 return iss >> result != 0; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
77 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
70
diff
changeset
|
78 |
3
ace29ecfb846
basic files and directories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
79 #endif |