ModernC++ Vision Lecture 5
ModernC++ Vision Lecture 5
1
Language support
2
General-purpose Utilities
Program utilities(std::abort).
Date and Time(std::chrono::duration).
Optional, variant and any(std::variant).
Pairs and tuples(std::tuple).
Swap, forward and move(std::move).
Hash support(std::hash).
Formatting library(coming in C++20).
Much more...
3
std::swap
1 int main () {
2 int a = 3;
3 int b = 5;
4
5 // before
6 std :: cout << a << ' ' << b << '\n';
7
8 std :: swap(a, b);
9
10 // after
11 std :: cout << a << ' ' << b << '\n';
12 }
Output:
1 3 5
2 5 3
4
std::variant
1 int main () {
2 std :: variant <int , float > v1;
3 v1 = 12; // v contains int
4 cout << std ::get <int >(v1) << endl;
5 std :: variant <int , float > v2 {3.14F};
6 cout << std ::get <1>(v2) << endl;
7
8 v2 = std ::get <int >(v1); // assigns v1 to v2
9 v2 = std ::get <0>(v1); // same as previous line
10 v2 = v1; // same as previous line
11 cout << std ::get <int >(v2) << endl;
12 }
Output:
1 12
2 3.14
3 12
5
std::any
1 int main () {
2 std :: any a; // any type
3
4 a = 1; // int
5 cout << any_cast <int >(a) << endl;
6
7 a = 3.14; // double
8 cout << any_cast <double >(a) << endl;
9
10 a = true; // bool
11 cout << std :: boolalpha << any_cast <bool >(a) << endl;
12 }
Output:
1 1
2 3.14
3 true
6
std::optional
Output:
1 Modern C++ is Awesome
2 :(
7
std::tuple
1 int main () {
2 std :: tuple <double , char , string > student1 ;
3 using Student = std :: tuple <double , char , string >;
4 Student student2 {1.4 , 'A', "Jose"};
5 PrintStudent ( student2 );
6 cout << std ::get <string >( student2 ) << endl;
7 cout << std ::get <2>( student2 ) << endl;
8
9 // C++17 structured binding :
10 auto [gpa , grade , name] = make_tuple (4.4 , 'B', "");
11 }
Output:
1 GPA: 1.4, grade: A, name: Jose
2 Jose
3 Jose
8
std::chrono
Output:
1 f(42) = 267914296
2 elapsed time: 1.84088 s
9
Much more utilites
10
Error handling with exceptions
We can “throw’’an exception if there is an
error
STL defines classes that represent
exceptions. Base class: std::exception
To use exceptions: #include <stdexcept>
An exception can be “caught” at any point
of the program (try - catch) and even
“thrown” further (throw)
The constructor of an exception receives a
string error message as a parameter
This string can be called through a member
function what()
11
throw exceptions
Runtime Error:
1 // if there is an error
2 if ( badEvent ) {
3 string msg = " specific error string ";
4 // throw error
5 throw runtime_error (msg);
6 }
7 ... some cool code if all ok ...
12
catch exceptions
If we expect an exception, we can “catch” it
Use try - catch to catch exceptions
1 try {
2 // some code that can throw exceptions z.B.
3 x = someUnsafeFunction (a, b, c);
4 }
5 // we can catch multiple types of exceptions
6 catch ( runtime_error &ex ) {
7 cerr << " Runtime error : " << ex.what () << endl;
8 } catch ( logic_error &ex ) {
9 cerr << " Logic error : " << ex.what () << endl;
10 } catch ( exception &ex ) {
11 cerr << "Some exception : " << ex.what () << endl;
12 } catch ( ... ) { // all others
13 cerr << " Error : unknown exception " << endl;
14 }
13
Intuition
0
https://google.github.io/styleguide/cppguide.html#Exceptions
14
Reading and writing to files
Mode Meaning
ios_base::app append output
ios_base::ate seek to EOF when opened
ios_base::binary open file in binary mode
ios_base::in open file for reading
ios_base::out open file for writing
ios_base::trunc overwrite the existing file
16
Regular columns
Use it when:
The file contains organized data
Every line has to have all columns
1 1 2.34 One 0.21
2 2 2.004 two 0.23
3 3 -2.34 string 0.22
O.K. Fail
1 1 2.34 One 0.21 1 1 2.34 One 0.21
2 2 2.004 two 0.23 2 2 2.004 two
3 3 -2.34 string 0.22 3 3 -2.34 string 0.22
17
Reading from ifstream
1 # include <fstream > // For the file streams .
2 # include <iostream >
3 # include <string >
4 using namespace std; // Saving space .
5 int main () {
6 int i;
7 double a, b;
8 string s;
9 // Create an input file stream .
10 ifstream in(" test_cols .txt", ios_base ::in);
11 // Read data , until it is there .
12 while (in >> i >> a >> s >> b) {
13 cout << i << ", " << a << ", "
14 << s << ", " << b << endl;
15 }
16 return (0);
17 }
18
Reading files one line at a time
19
1 # include <fstream > // For the file streams .
2 # include <iostream >
3 using namespace std;
4 int main () {
5 string line , file_name ;
6 ifstream input(" test_bel .txt", ios_base ::in);
7 // Read data line -wise.
8 while ( getline (input , line)) {
9 cout << "Read: " << line << endl;
10 // String has a find method .
11 string :: size_type loc = line.find(" filename ", 0);
12 if (loc != string :: npos) {
13 file_name = line. substr (line.find("=", 0) + 1,
14 string :: npos);
15 }
16 }
17 cout << " Filename found : " << file_name << endl;
18 return (0);
19 }
20
Writing into text files
With the same syntax as cerr und cout
streams, with ofstream we can write directly
into files
1 # include <iomanip > // For setprecision .
2 # include <fstream >
3 using namespace std;
4 int main () {
5 string filename = "out.txt";
6 ofstream outfile ( filename );
7 if (! outfile . is_open ()) { return EXIT_FAILURE ; }
8 double a = 1.123123123;
9 outfile << "Just string " << endl;
10 outfile << setprecision (20) << a << endl;
11 return 0;
12 }
21
Writing to binary files
22
Writing to binary files
1 #include <fstream > // for the file streams
2 #include <vector >
3 using namespace std;
4
5 int main () {
6 string file_name = "image.dat";
7 ofstream file(file_name , ios_base ::out | ios_base :: binary);
8 int rows = 2;
9 int cols = 3;
10 vector <float > vec(rows * cols);
11 file.write(reinterpret_cast <char *>(& rows), sizeof(rows));
12 file.write(reinterpret_cast <char *>(& cols), sizeof(cols));
13 file.write(reinterpret_cast <char *>(&vec.front ()),
14 vec.size () * sizeof(float));
15 return 0;
16 }
23
Reading from binary files
24
Reading from binary files
1 # include <fstream >
2 # include <iostream >
3 # include <vector >
4 using namespace std;
5 int main () {
6 string file_name = "image .dat";
7 int r = 0, c = 0;
8 ifstream in(file_name ,
9 ios_base ::in | ios_base :: binary );
10 if (! in) { return EXIT_FAILURE ; }
11 in.read( reinterpret_cast <char *>(&r), sizeof (r));
12 in.read( reinterpret_cast <char *>(&c), sizeof (c));
13 cout << "Dim: " << r << " x " << c << endl;
14 vector <float > data(r * c, 0);
15 in.read( reinterpret_cast <char *>(& data.front ()),
16 data.size () * sizeof (data.front ()));
17 for ( float d : data) { cout << d << endl; }
18 return 0;
19 } 25
Important facts
Pros
I/O Binary files is faster than ASCII format.
Size of files is drastically smaller.
There are many libraries to facilitate
serialization.
Cons
Ugly Syntax.
File is not readable by human.
You need to now the format before reading.
You need to use this for your homeworks.
26
C++17 Filesystem library
Introduced in C++17.
Use to perform operations on:
paths
regular files
directories
Inspired in boost::filesystem
Makes your life easier.
https://en.cppreference.com/w/cpp/filesystem
27
directory_iterator
1 # include <filesystem >
2 namespace fs = std :: filesystem ;
3
4 int main () {
5 fs :: create_directories (" sandbox /a/b");
6 std :: ofstream (" sandbox /file1 .txt");
7 std :: ofstream (" sandbox /file2 .txt");
8 for (auto& p : fs:: directory_iterator (" sandbox ")) {
9 std :: cout << p.path () << '\n';
10 }
11 fs :: remove_all (" sandbox ");
12 }
Output:
1 " sandbox /a"
2 " sandbox /file1.txt"
3 " sandbox /file2.txt"
28
filename_part1
1 # include <filesystem >
2 namespace fs = std :: filesystem ;
3
4 int main () {
5 cout << fs:: path("/foo/bar.txt"). filename () << '\n'
6 << fs:: path("/foo /. bar"). filename () << '\n'
7 << fs:: path("/foo/bar/"). filename () << '\n'
8 << fs:: path("/foo /."). filename () << '\n'
9 << fs:: path("/foo /.."). filename () << '\n';
10 }
Output:
1 "bar.txt"
2 ". bar"
3 ""
4 "."
5 ".."
29
filename_part2
1 # include <filesystem >
2 namespace fs = std :: filesystem ;
3
4 int main () {
5 cout << fs:: path("/foo /. bar"). filename () << '\n'
6 << fs:: path("."). filename () << '\n'
7 << fs:: path(".."). filename () << '\n'
8 << fs:: path("/"). filename () << '\n'
9 << fs:: path("// host"). filename () << '\n';
10 }
Output:
1 ". bar"
2 "."
3 ".."
4 ""
5 "host"
30
extension_part1
Output:
1 ". txt"
2 "."
3 ""
4 ". png"
31
extension_part2
Output:
1 ""
2 ""
3 ""
4 ". bar"
32
stem
Output:
1 "bar"
2 "00000"
3 ". bar"
33
exists
1 void demo_exists ( const fs:: path& p) {
2 cout << p;
3 if (fs:: exists (p)) cout << " exists \n";
4 else cout << " does not exist \n";
5 }
6
7 int main () {
8 fs :: create_directory (" sandbox ");
9 ofstream (" sandbox /file"); // create regular file
10 demo_exists (" sandbox /file");
11 demo_exists (" sandbox / cacho ");
12 fs :: remove_all (" sandbox ");
13 }
Output:
1 " sandbox /file" exists
2 " sandbox /cacho" does not exist
34
Types are indeed important
0
https://www.simscale.com/blog/2017/12/nasa-mars-climate-orbiter-metric/
35
Type safety
bad – the unit is ambiguous
1 void blink_led_bad (int time_to_blink ) {
2 // do something with time_to_blink
3 }
Usage
1 void use () {
2 blink_led_good (100); // ERROR : What unit?
3 blink_led_good (100 ms); //
4 blink_led_good (5s); // ERROR : Bad unit
5 }
0
Example taken from: https://youtu.be/fX2W3nNjJIo
37
Want more flexibilty?
0
Example taken from: https://youtu.be/fX2W3nNjJIo
38
Type safety in our field
0
https://github.com/ros/ros_tutorials/blob/noetic-devel/roscpp_tutorials/talker/talker.cpp
39
Type safety in our field
0
Extract from: Section 16 of “The C++ Programming Language Book by Bjarne Stroustrup”
41
Class Basics
0
Extract from: Section 16 of “The C++ Programming Language Book by Bjarne Stroustrup”
42
Class Basics
0
Extract from: Section 16 of “The C++ Programming Language Book by Bjarne Stroustrup”
43
Class Basics
0
Extract from: Section 16 of “The C++ Programming Language Book by Bjarne Stroustrup”
44
Example class definition
1 class Image { // Should be in Image .hpp
2 public :
3 Image ( const std :: string & file_name );
4 void Draw ();
5
6 private :
7 int rows_ = 0; // New in C+=11
8 int cols_ = 0; // New in C+=11
9 };
10
11 // Implementation omitted here , should be in Image .cpp
12 int main () {
13 Image image(" some_image .pgm");
14 image .Draw ();
15 return 0;
16 }
45
Classes in our field
1 // 2D entities
2 class Image : public Geometry2D ;
3 class RGBDImage : public Geometry2D ;
4
5 // 3D entities
6 class Image : public Geometry2D ;
7 class OrientedBoundingBox : public Geometry3D ;
8 class AxisAlignedBoundingBox : public Geometry3D ;
9 class LineSet : public Geometry3D ;
10 class MeshBase : public Geometry3D ;
11 class Octree : public Geometry3D ;
12 class PointCloud : public Geometry3D ;
13 class VoxelGrid : public Geometry3D ;
14
15 // 3D surfaces
16 class TetraMesh : public MeshBase ;
17 class TriangleMesh : public MeshBase ;
46
Image class
47
One possible realization
Open3D::Geometry::Image
1 class Image : public Geometry2D {
2 public :
3 /// Width of the image .
4 int width_ = 0;
5 /// Height of the image .
6 int height_ = 0;
7 /// Number of chanels in the image .
8 int num_of_channels_ = 0;
9 /// Image storage buffer .
10 std :: vector <uint8_t > data_;
11 };
48
One possible realization
Open3D::Geometry::Image
1 class Image : public Geometry2D {
2 public :
3 void Clear () const override ;
4 bool IsEmpty () const override ;
5
6 Image FlipHorizontal () const ;
7 Image FlipVertical () const ;
8 Image Filter (Image :: FilterType type) const ;
9
10 protected :
11 void AllocateDataBuffer () {
12 data_. resize ( width_ *
13 height_ *
14 num_of_channels_ );
15 }
16 }
49
Goal achived?
50
Goal achived?
Open3D::Geometry::Image
1 # include <Open3D / Geometry /Image.h>
2
3 using namespace Open3D :: Geometry ;
4 int main () {
5 Image linux_pic (".data/linux .png");
6
7 auto flipped_linux = linux_pic . FlipHorizontal ();
8
9 auto sobel_filter = Image :: FilterType :: Sobel3Dx ;
10 auto filtered_linux = linux_pic . Filter ( sobel_filter );
11
12 if ( filtered_linux . IsEmpty ()) {
13 std :: cerr << " Couldn 't Filter Image !\n";
14 }
15 }
51
Must Watch
Bag of Visual Words Introduction
https://youtu.be/a4cFONdc6nc
52
Suggested Video
Features Descriptors
https://youtu.be/CMolhcwtGAU
53
References
Utility Library
https://en.cppreference.com/w/cpp/utility
Error handling
https://en.cppreference.com/w/cpp/error
IO Library
https://en.cppreference.com/w/cpp/io
Filesystem Library
https://en.cppreference.com/w/cpp/filesystem
Classes
https://en.cppreference.com/w/cpp/classes
54