ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
misc.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <boost/filesystem.hpp>
11 #include <fstream>
12 #include <iostream>
13 #include <string>
14 #include <vector>
15 
16 #include "util/endian.h"
17 #include "util/logging.h"
18 #include "util/string.h"
19 
20 namespace colmap {
21 
22 #ifndef STRINGIFY
23 #define STRINGIFY(s) STRINGIFY_(s)
24 #define STRINGIFY_(s) #s
25 #endif // STRINGIFY
26 
27 enum class CopyType { COPY, HARD_LINK, SOFT_LINK };
28 
29 // Append trailing slash to string if it does not yet end with a slash.
30 std::string EnsureTrailingSlash(const std::string& str);
31 
32 // Check whether file name has the file extension (case insensitive).
33 bool HasFileExtension(const std::string& file_name, const std::string& ext);
34 
35 // Split the path into its root and extension, for example,
36 // "dir/file.jpg" into "dir/file" and ".jpg".
37 void SplitFileExtension(const std::string& path,
38  std::string* root,
39  std::string* ext);
40 
41 // Copy or link file from source to destination path
42 void FileCopy(const std::string& src_path,
43  const std::string& dst_path,
45 
46 // Check if the path points to an existing directory.
47 bool ExistsFile(const std::string& path);
48 
49 // Check if the path points to an existing directory.
50 bool ExistsDir(const std::string& path);
51 
52 // Check if the path points to an existing file or directory.
53 bool ExistsPath(const std::string& path);
54 
55 // Create the directory if it does not exist.
56 void CreateDirIfNotExists(const std::string& path);
57 
58 // Extract the base name of a path, e.g., "image.jpg" for "/dir/image.jpg".
59 std::string GetPathBaseName(const std::string& path);
60 
61 // Get the path of the parent directory for the given path.
62 std::string GetParentDir(const std::string& path);
63 
64 // Get the relative path between from and to. Both the from and to paths must
65 // exist.
66 std::string GetRelativePath(const std::string& from, const std::string& to);
67 
68 // Join multiple paths into one path.
69 template <typename... T>
70 std::string JoinPaths(T const&... paths);
71 
72 // Return list of files in directory.
73 std::vector<std::string> GetFileList(const std::string& path);
74 
75 // Return list of files, recursively in all sub-directories.
76 std::vector<std::string> GetRecursiveFileList(const std::string& path);
77 
78 // Return list of directories, recursively in all sub-directories.
79 std::vector<std::string> GetDirList(const std::string& path);
80 
81 // Return list of directories, recursively in all sub-directories.
82 std::vector<std::string> GetRecursiveDirList(const std::string& path);
83 
84 // Get the size in bytes of a file.
85 size_t GetFileSize(const std::string& path);
86 
87 // Print first-order heading with over- and underscores to `std::cout`.
88 void PrintHeading1(const std::string& heading);
89 
90 // Print second-order heading with underscores to `std::cout`.
91 void PrintHeading2(const std::string& heading);
92 
93 // Check if vector contains elements.
94 template <typename T>
95 bool VectorContainsValue(const std::vector<T>& vector, const T value);
96 
97 template <typename T>
98 bool VectorContainsDuplicateValues(const std::vector<T>& vector);
99 
100 // Parse CSV line to a list of values.
101 template <typename T>
102 std::vector<T> CSVToVector(const std::string& csv);
103 
104 // Concatenate values in list to comma-separated list.
105 template <typename T>
106 std::string VectorToCSV(const std::vector<T>& values);
107 
108 // Read contiguous binary blob from file.
109 template <typename T>
110 void ReadBinaryBlob(const std::string& path, std::vector<T>* data);
111 
112 // Write contiguous binary blob to file.
113 template <typename T>
114 void WriteBinaryBlob(const std::string& path, const std::vector<T>& data);
115 
116 // Read each line of a text file into a separate element. Empty lines are
117 // ignored and leading/trailing whitespace is removed.
118 std::vector<std::string> ReadTextFileLines(const std::string& path);
119 
120 // Remove an argument from the list of command-line arguments.
121 void RemoveCommandLineArgument(const std::string& arg, int* argc, char** argv);
122 
124 // Implementation
126 
127 template <typename... T>
128 std::string JoinPaths(T const&... paths) {
130  int unpack[]{0, (result = result / boost::filesystem::path(paths), 0)...};
131  static_cast<void>(unpack);
132  return result.string();
133 }
134 
135 template <typename T>
136 bool VectorContainsValue(const std::vector<T>& vector, const T value) {
137  return std::find_if(vector.begin(), vector.end(), [value](const T element) {
138  return element == value;
139  }) != vector.end();
140 }
141 
142 template <typename T>
143 bool VectorContainsDuplicateValues(const std::vector<T>& vector) {
144  std::vector<T> unique_vector = vector;
145  return std::unique(unique_vector.begin(), unique_vector.end()) !=
146  unique_vector.end();
147 }
148 
149 template <typename T>
150 std::string VectorToCSV(const std::vector<T>& values) {
151  std::string string;
152  for (const T value : values) {
153  string += std::to_string(value) + ", ";
154  }
155  return string.substr(0, string.length() - 2);
156 }
157 
158 template <typename T>
159 void ReadBinaryBlob(const std::string& path, std::vector<T>* data) {
160  std::ifstream file(path, std::ios::binary | std::ios::ate);
161  CHECK(file.is_open()) << path;
162  file.seekg(0, std::ios::end);
163  const size_t num_bytes = file.tellg();
164  CHECK_EQ(num_bytes % sizeof(T), 0);
165  data->resize(num_bytes / sizeof(T));
166  file.seekg(0, std::ios::beg);
167  ReadBinaryLittleEndian<T>(&file, data);
168 }
169 
170 template <typename T>
171 void WriteBinaryBlob(const std::string& path, const std::vector<T>& data) {
172  std::ofstream file(path, std::ios::binary);
173  CHECK(file.is_open()) << path;
174  WriteBinaryLittleEndian<T>(&file, data);
175 }
176 
177 } // namespace colmap
char type
core::Tensor result
Definition: VtkUtils.cpp:76
GraphType data
Definition: graph_cut.cc:138
static const std::string path
Definition: PointCloud.cpp:59
void ReadBinaryBlob(const std::string &path, std::vector< T > *data)
Definition: misc.h:159
CopyType
Definition: misc.h:27
size_t GetFileSize(const std::string &path)
Definition: misc.cc:219
void PrintHeading2(const std::string &heading)
Definition: misc.cc:231
bool ExistsPath(const std::string &path)
Definition: misc.cc:108
bool VectorContainsDuplicateValues(const std::vector< T > &vector)
Definition: misc.h:143
bool HasFileExtension(const std::string &file_name, const std::string &ext)
Definition: misc.cc:51
std::vector< std::string > GetRecursiveFileList(const std::string &path)
Definition: misc.cc:183
std::vector< std::string > CSVToVector(const std::string &csv)
Definition: misc.cc:237
std::string GetPathBaseName(const std::string &path)
Definition: misc.cc:118
std::string GetRelativePath(const std::string &from, const std::string &to)
Definition: misc.cc:132
void SplitFileExtension(const std::string &path, std::string *root, std::string *ext)
Definition: misc.cc:64
void WriteBinaryBlob(const std::string &path, const std::vector< T > &data)
Definition: misc.h:171
std::vector< std::string > ReadTextFileLines(const std::string &path)
Definition: misc.cc:308
bool ExistsDir(const std::string &path)
Definition: misc.cc:104
bool ExistsFile(const std::string &path)
Definition: misc.cc:100
void CreateDirIfNotExists(const std::string &path)
Definition: misc.cc:112
std::string JoinPaths(T const &... paths)
Definition: misc.h:128
std::string EnsureTrailingSlash(const std::string &str)
Definition: misc.cc:40
void PrintHeading1(const std::string &heading)
Definition: misc.cc:225
std::string VectorToCSV(const std::vector< T > &values)
Definition: misc.h:150
std::vector< std::string > GetRecursiveDirList(const std::string &path)
Definition: misc.cc:207
bool VectorContainsValue(const std::vector< T > &vector, const T value)
Definition: misc.h:136
std::vector< std::string > GetFileList(const std::string &path)
Definition: misc.cc:171
std::string GetParentDir(const std::string &path)
Definition: misc.cc:128
void RemoveCommandLineArgument(const std::string &arg, int *argc, char **argv)
Definition: misc.cc:327
std::vector< std::string > GetDirList(const std::string &path)
Definition: misc.cc:195
void FileCopy(const std::string &src_path, const std::string &dst_path, CopyType type)
Definition: misc.cc:85
std::string to_string(const T &n)
Definition: Common.h:20