Skip to content

Commit b09dff5

Browse files
authored
C++17 filesystem for doc/tutorials (#5934)
* C++17 filesystem for doc/tutorials * Review changes
1 parent b701652 commit b09dff5

File tree

5 files changed

+65
-35
lines changed

5 files changed

+65
-35
lines changed

doc/tutorials/content/sources/iccv2011/src/build_all_object_models.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66
#include <pcl/console/parse.h>
77
#include <pcl/io/pcd_io.h>
88
#include <pcl/visualization/pcl_visualizer.h>
9+
10+
#if (__cplusplus >= 201703L)
11+
#include <filesystem>
12+
namespace pcl_fs = std::filesystem;
13+
#else
914
#include <boost/filesystem.hpp>
15+
namespace pcl_fs = boost::filesystem;
16+
#endif
17+
1018
#include <boost/algorithm/string.hpp> // for split, is_any_of
11-
namespace bf = boost::filesystem;
1219

1320
inline void
14-
getModelsInDirectory (bf::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths)
21+
getModelsInDirectory (pcl_fs::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths)
1522
{
16-
for (const auto& dir_entry : bf::directory_iterator(dir))
23+
for (const auto& dir_entry : pcl_fs::directory_iterator(dir))
1724
{
1825
//check if its a directory, then get models in it
19-
if (bf::is_directory (dir_entry))
26+
if (pcl_fs::is_directory (dir_entry))
2027
{
2128
std::string so_far = rel_path_so_far + dir_entry.path ().filename ().string () + "/";
22-
bf::path curr_path = dir_entry.path ();
29+
pcl_fs::path curr_path = dir_entry.path ();
2330
getModelsInDirectory (curr_path, so_far, relative_paths);
2431
}
2532
else
@@ -189,7 +196,7 @@ main (int argc, char ** argv)
189196

190197
std::string directory (argv[1]);
191198
//Find all raw* files in input_directory
192-
bf::path dir_path = directory;
199+
pcl_fs::path dir_path = directory;
193200
std::vector < std::string > files;
194201
std::string start = "";
195202
getModelsInDirectory (dir_path, start, files);

doc/tutorials/content/sources/iros2011/src/build_all_object_models.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@
66
#include <pcl/console/parse.h>
77
#include <pcl/io/pcd_io.h>
88
#include <pcl/visualization/pcl_visualizer.h>
9+
10+
#if (__cplusplus >= 201703L)
11+
#include <filesystem>
12+
namespace pcl_fs = std::filesystem;
13+
#else
914
#include <boost/filesystem.hpp>
15+
namespace pcl_fs = boost::filesystem;
16+
#endif
17+
1018
#include <boost/algorithm/string.hpp> // for split, is_any_of
11-
namespace bf = boost::filesystem;
1219

1320
inline void
14-
getModelsInDirectory (bf::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths)
21+
getModelsInDirectory (pcl_fs::path & dir, std::string & rel_path_so_far, std::vector<std::string> & relative_paths)
1522
{
16-
for (const auto& dir_entry : bf::directory_iterator(dir))
23+
for (const auto& dir_entry : pcl_fs::directory_iterator(dir))
1724
{
1825
//check if its a directory, then get models in it
19-
if (bf::is_directory (dir_entry))
26+
if (pcl_fs::is_directory (dir_entry))
2027
{
2128
std::string so_far = rel_path_so_far + dir_entry.path ().filename ().string () + "/";
22-
bf::path curr_path = dir_entry.path ();
29+
pcl_fs::path curr_path = dir_entry.path ();
2330
getModelsInDirectory (curr_path, so_far, relative_paths);
2431
}
2532
else
@@ -189,7 +196,7 @@ main (int argc, char ** argv)
189196

190197
std::string directory (argv[1]);
191198
//Find all raw* files in input_directory
192-
bf::path dir_path = directory;
199+
pcl_fs::path dir_path = directory;
193200
std::vector < std::string > files;
194201
std::string start = "";
195202
getModelsInDirectory (dir_path, start, files);

doc/tutorials/content/sources/vfh_recognition/build_tree.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
#include <pcl/point_cloud.h>
33
#include <pcl/console/print.h>
44
#include <pcl/io/pcd_io.h>
5+
6+
#if (__cplusplus >= 201703L)
7+
#include <filesystem>
8+
namespace pcl_fs = std::filesystem;
9+
#else
510
#include <boost/filesystem.hpp>
11+
namespace pcl_fs = boost::filesystem;
12+
#endif
13+
614
#include <flann/flann.h>
715
#include <flann/io/hdf5.h>
816
#include <fstream>
@@ -15,7 +23,7 @@ typedef std::pair<std::string, std::vector<float> > vfh_model;
1523
* \param vfh the resultant VFH model
1624
*/
1725
bool
18-
loadHist (const boost::filesystem::path &path, vfh_model &vfh)
26+
loadHist (const pcl_fs::path &path, vfh_model &vfh)
1927
{
2028
int vfh_idx;
2129
// Load the file as a PCD
@@ -63,22 +71,22 @@ loadHist (const boost::filesystem::path &path, vfh_model &vfh)
6371
* \param models the resultant vector of histogram models
6472
*/
6573
void
66-
loadFeatureModels (const boost::filesystem::path &base_dir, const std::string &extension,
74+
loadFeatureModels (const pcl_fs::path &base_dir, const std::string &extension,
6775
std::vector<vfh_model> &models)
6876
{
69-
if (!boost::filesystem::exists (base_dir) && !boost::filesystem::is_directory (base_dir))
77+
if (!pcl_fs::exists (base_dir) && !pcl_fs::is_directory (base_dir))
7078
return;
7179

72-
for (boost::filesystem::directory_iterator it (base_dir); it != boost::filesystem::directory_iterator (); ++it)
80+
for (pcl_fs::directory_iterator it (base_dir); it != pcl_fs::directory_iterator (); ++it)
7381
{
74-
if (boost::filesystem::is_directory (it->status ()))
82+
if (pcl_fs::is_directory (it->status ()))
7583
{
7684
std::stringstream ss;
7785
ss << it->path ();
7886
pcl::console::print_highlight ("Loading %s (%lu models loaded so far).\n", ss.str ().c_str (), (unsigned long)models.size ());
7987
loadFeatureModels (it->path (), extension, models);
8088
}
81-
if (boost::filesystem::is_regular_file (it->status ()) && it->path ().extension ().string () == extension)
89+
if (pcl_fs::is_regular_file (it->status ()) && it->path ().extension ().string () == extension)
8290
{
8391
vfh_model m;
8492
if (loadHist (base_dir / it->path ().filename (), m))

doc/tutorials/content/sources/vfh_recognition/nearest_neighbors.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
#include <limits>
1111
#include <flann/flann.h>
1212
#include <flann/io/hdf5.h>
13+
14+
#if (__cplusplus >= 201703L)
15+
#include <filesystem>
16+
namespace pcl_fs = std::filesystem;
17+
#else
1318
#include <boost/filesystem.hpp>
19+
namespace pcl_fs = boost::filesystem;
20+
#endif
21+
1422
#include <boost/algorithm/string/replace.hpp> // for replace_last
1523
typedef std::pair<std::string, std::vector<float> > vfh_model;
1624

@@ -19,7 +27,7 @@ typedef std::pair<std::string, std::vector<float> > vfh_model;
1927
* \param vfh the resultant VFH model
2028
*/
2129
bool
22-
loadHist (const boost::filesystem::path &path, vfh_model &vfh)
30+
loadHist (const pcl_fs::path &path, vfh_model &vfh)
2331
{
2432
int vfh_idx;
2533
// Load the file as a PCD
@@ -152,7 +160,7 @@ main (int argc, char** argv)
152160
flann::Matrix<float> k_distances;
153161
flann::Matrix<float> data;
154162
// Check if the data has already been saved to disk
155-
if (!boost::filesystem::exists ("training_data.h5") || !boost::filesystem::exists ("training_data.list"))
163+
if (!pcl_fs::exists ("training_data.h5") || !pcl_fs::exists ("training_data.list"))
156164
{
157165
pcl::console::print_error ("Could not find training data models files %s and %s!\n",
158166
training_data_h5_file_name.c_str (), training_data_list_file_name.c_str ());
@@ -167,7 +175,7 @@ main (int argc, char** argv)
167175
}
168176

169177
// Check if the tree index has already been saved to disk
170-
if (!boost::filesystem::exists (kdtree_idx_file_name))
178+
if (!pcl_fs::exists (kdtree_idx_file_name))
171179
{
172180
pcl::console::print_error ("Could not find kd-tree index in file %s!", kdtree_idx_file_name.c_str ());
173181
return (-1);

doc/tutorials/content/vfh_recognition.rst

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,21 @@ Once all VFH features have been loaded, we convert them to FLANN format, using:
110110

111111
.. literalinclude:: sources/vfh_recognition/build_tree.cpp
112112
:language: cpp
113-
:lines: 113-118
113+
:lines: 121-126
114114

115115

116116
Since we're lazy, and we want to use this data (and not reload it again by crawling the directory structure in the testing phase), we dump the data to disk:
117117

118118
.. literalinclude:: sources/vfh_recognition/build_tree.cpp
119119
:language: cpp
120-
:lines: 120-126
120+
:lines: 128-134
121121

122122

123123
Finally, we create the KdTree, and save its structure to disk:
124124

125125
.. literalinclude:: sources/vfh_recognition/build_tree.cpp
126126
:language: cpp
127-
:lines: 129-133
127+
:lines: 137-141
128128

129129

130130
Here we will use a ``LinearIndex``, which does a brute-force search using a
@@ -164,7 +164,7 @@ In lines:
164164

165165
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
166166
:language: cpp
167-
:lines: 132-143
167+
:lines: 141-152
168168

169169

170170
we load the first given user histogram (and ignore the rest). Then we proceed
@@ -177,15 +177,15 @@ In lines:
177177

178178
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
179179
:language: cpp
180-
:lines: 162-163
180+
:lines: 171-172
181181

182182

183183
we load the training data from disk, together with the list of file names that
184184
we previously stored in ``build_tree.cpp``. Then, we read the kd-tree and rebuild the index:
185185

186186
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
187187
:language: cpp
188-
:lines: 176-177
188+
:lines: 185-186
189189

190190

191191
Here we need to make sure that we use the **exact** distance metric
@@ -194,53 +194,53 @@ the tree. The most important part of the code comes here:
194194

195195
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
196196
:language: cpp
197-
:lines: 178
197+
:lines: 187
198198

199199
Inside ``nearestKSearch``, we first convert the query point to FLANN format:
200200

201201

202202
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
203203
:language: cpp
204-
:lines: 75-76
204+
:lines: 84-85
205205

206206
Followed by obtaining the resultant nearest neighbor indices and distances for the query in:
207207

208208
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
209209
:language: cpp
210-
:lines: 77-80
210+
:lines: 86-89
211211

212212

213213
Lines:
214214

215215
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
216216
:language: cpp
217-
:lines: 177-191
217+
:lines: 186-200
218218

219219
create a ``PCLVisualizer`` object, and sets up a set of different viewports (e.g., splits the screen into different chunks), which will be enabled in:
220220

221221
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
222222
:language: cpp
223-
:lines: 211
223+
:lines: 220
224224

225225
Using the file names representing the models that we previously obtained in
226226
``loadFileList``, we proceed at loading the model file names using:
227227

228228
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
229229
:language: cpp
230-
:lines: 219-226
230+
:lines: 228-235
231231

232232
For visualization purposes, we demean the point cloud by computing its centroid and then subtracting it:
233233

234234
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
235235
:language: cpp
236-
:lines: 238-243
236+
:lines: 247-252
237237

238238

239239
Finally we check if the distance obtained by ``nearestKSearch`` is larger than the user given threshold, and if it is, we display a red line over the cloud that is being rendered in the viewport:
240240

241241
.. literalinclude:: sources/vfh_recognition/nearest_neighbors.cpp
242242
:language: cpp
243-
:lines: 252-258
243+
:lines: 261-267
244244

245245

246246
Compiling and running the code

0 commit comments

Comments
 (0)