ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
CVTools.cpp
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 #include "CVTools.h"
9 
10 // CV_CORE_LIB
11 #include "CVLog.h"
12 #include "CVPlatform.h"
13 
14 // QT
15 #include <QDir>
16 #include <QFile>
17 
18 // SYSTEM
19 #include <codecvt>
20 #include <locale>
21 #include <regex>
22 #if defined(CV_WINDOWS)
23 #include "stdio.h"
24 #include "windows.h"
25 #endif // (CV_WINDOWS)
26 
27 QElapsedTimer CVTools::s_time;
28 
29 std::string CVTools::GetFileName(const std::string file_name) {
30  std::string subname;
31  for (auto i = file_name.end() - 1; *i != '/'; i--) {
32  subname.insert(subname.begin(), *i);
33  }
34  return subname;
35 }
36 
37 void CVTools::TimeStart() { s_time.start(); }
38 
39 QString CVTools::TimeOff() {
40  int timediff = s_time.elapsed();
41  double f = timediff / 1000.0;
42  return QString("%1").arg(f); // float->QString
43 }
44 
45 QString CVTools::ToNativeSeparators(const QString& path) {
46  QString newPath = path;
47  if (!QFile::exists(path)) {
48  newPath = newPath.replace("\\\\", QDir::separator());
49  newPath = newPath.replace("\\", QDir::separator());
50  newPath = QDir::toNativeSeparators(newPath);
51  }
52 
53  return newPath;
54 }
55 
56 std::string CVTools::FromUnicode(const QString& qstr) {
58  if (!pCodec) {
59  CVLog::PrintDebug("Failed to call qtCompatCodecForLocale");
60  return qstr.toStdString();
61  }
62 
63  QByteArray arr = pCodec->fromUnicode(qstr);
64  std::string cstr = arr.data();
65  return cstr;
66 }
67 
68 QString CVTools::ToUnicode(const std::string& cstr) {
70  if (!pCodec) {
71  CVLog::PrintDebug("Failed to call qtCompatCodecForLocale");
72  return QString(cstr.c_str());
73  }
74 
75  QString qstr =
76  pCodec->toUnicode(cstr.c_str(), static_cast<int>(cstr.length()));
77  return qstr;
78 }
79 
80 std::string CVTools::ExtractDigitAlpha(const std::string& str) {
81  std::smatch m;
82  std::regex e("([a-z0-9A-Z]?)");
83  std::string result;
84  for (int i = 0; i < str.size(); i++) {
85  std::string x(1, str[i]);
86  regex_search(x, m, e);
87  if (m.str() != " ") result += m.str();
88  }
89  return result;
90 }
91 
92 QString CVTools::ToQString(const std::string& s) {
93 #if defined(CV_WINDOWS)
94  return ToUnicode(s);
95 #else // do not support coding in Linux or mac platform!
96  return QString(s.c_str());
97 #endif
98 }
99 
100 std::string CVTools::FromQString(const QString& qs) {
101 #if defined(CV_WINDOWS)
102  return FromUnicode(qs);
103 #else // do not support coding in Linux or mac platform!
104  return qs.toStdString();
105 #endif
106 }
107 
108 std::string CVTools::JoinStrVec(const std::vector<std::string>& v,
109  std::string splitor) {
110  std::string s = "";
111  if (v.size() == 0) return s;
112  for (std::size_t i = 0; i != v.size() - 1; ++i) {
113  s += (v[i] + splitor);
114  }
115  s += v[v.size() - 1];
116  return s;
117 }
118 
120  int k = key;
121  bool legal = true;
122  if (k >= Qt::Key_0 && k <= Qt::Key_9) {
123  } else if (k >= Qt::Key_A && k <= Qt::Key_Z) {
124  } else if (k >= Qt::Key_F1 && k <= Qt::Key_F24) {
125  k &= 0x000000ff;
126  k += 0x40;
127  } else if (k == Qt::Key_Tab) {
128  k = 0x09;
129  } else if (k == Qt::Key_Backspace) {
130  k = 0x08;
131  } else if (k == Qt::Key_Return) {
132  k = 0x0d;
133  } else if (k <= Qt::Key_Down && k >= Qt::Key_Left) {
134  int off = k - Qt::Key_Left;
135  k = 0x25 + off;
136  } else if (k == Qt::Key_Shift) {
137  k = 0x10;
138  } else if (k == Qt::Key_Control) {
139  k = 0x11;
140  } else if (k == Qt::Key_Alt) {
141  k = 0x12;
142  } else if (k == Qt::Key_Meta) {
143  k = 0x5b;
144  } else if (k == Qt::Key_Insert) {
145  k = 0x2d;
146  } else if (k == Qt::Key_Delete) {
147  k = 0x2e;
148  } else if (k == Qt::Key_Home) {
149  k = 0x24;
150  } else if (k == Qt::Key_End) {
151  k = 0x23;
152  } else if (k == Qt::Key_PageUp) {
153  k = 0x21;
154  } else if (k == Qt::Key_Down) {
155  k = 0x22;
156  } else if (k == Qt::Key_CapsLock) {
157  k = 0x14;
158  } else if (k == Qt::Key_NumLock) {
159  k = 0x90;
160  } else if (k == Qt::Key_Space) {
161  k = 0x20;
162  } else
163  legal = false;
164 
165  if (!legal) return 0;
166  return k;
167 }
168 
169 std::wstring CVTools::Char2Wchar(const char* szStr) {
170  std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
171  std::wstring wideStr = conv.from_bytes(szStr);
172  return wideStr;
173 }
174 
175 std::string CVTools::Wchar2Char(const wchar_t* szStr) {
176  std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
177  std::string wideStr = conv.to_bytes(szStr);
178  return wideStr;
179 }
180 
181 bool CVTools::FileMappingReader(const std::string& filename,
182  void* data,
183  unsigned long& size) {
184 #if defined(CV_WINDOWS)
185  HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE,
186  FILE_SHARE_READ, NULL, OPEN_EXISTING,
187  FILE_ATTRIBUTE_NORMAL, NULL);
188 
189  if (hFile == INVALID_HANDLE_VALUE) {
190  CVLog::Error("FileMappingReader: %d", GetLastError());
191  CloseHandle(hFile);
192  return false;
193  }
194 
195  DWORD dwFileSize = GetFileSize(hFile, NULL);
196  size = dwFileSize / sizeof(char);
197 
198  HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0,
199  dwFileSize + sizeof(char), NULL);
200 
201  if (hFileMapping == NULL) {
202  CVLog::Error("FileMappingReader: %d", GetLastError());
203  CloseHandle(hFile);
204  return false;
205  }
206 
207  char* pbFile = (char*)MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
208  if (pbFile == NULL) {
209  CVLog::Error("FileMappingReader: %d", GetLastError());
210  CloseHandle(hFile);
211  CloseHandle(hFileMapping);
212  return false;
213  }
214 
215  // memcpy(pbFile, data, size);
216  // MoveMemory(pbFile, data, size);
217  if (data) {
218  delete data;
219  }
220  data = new char[size];
221  CopyMemory(data, pbFile, size);
222 
223  UnmapViewOfFile(pbFile);
224  CloseHandle(hFileMapping);
225 
226  CloseHandle(hFile);
227  return true;
228 #else
229  CVLog::Warning("[FileMappingReader] only support windows!");
230  return false;
231 #endif
232 }
233 
234 bool CVTools::FileMappingWriter(const std::string& filename,
235  const void* data,
236  unsigned long size) {
237 #if defined(CV_WINDOWS)
238  HANDLE hFile = CreateFile(filename.c_str(), GENERIC_READ | GENERIC_WRITE,
239  FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
240  CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
241 
242  if (hFile == INVALID_HANDLE_VALUE) {
243  CVLog::Error("FileMappingWriter: %d", GetLastError());
244  CloseHandle(hFile);
245  return false;
246  }
247 
248  HANDLE hFileMapping =
249  CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, size, NULL);
250 
251  if (hFileMapping == NULL) {
252  CVLog::Error("FileMappingWriter: %d", GetLastError());
253  CloseHandle(hFileMapping);
254  return false;
255  }
256 
257  TCHAR* pbFile =
258  (TCHAR*)MapViewOfFile(hFileMapping, FILE_MAP_WRITE, 0, 0, 0);
259  if (pbFile == NULL) {
260  CVLog::Error("FileMappingWriter: %d", GetLastError());
261  UnmapViewOfFile(pbFile);
262  return false;
263  }
264 
265  // memcpy(pbFile, data, size);
266  // MoveMemory(pbFile, data, size);
267  CopyMemory(pbFile, data, size);
268 
269  UnmapViewOfFile(pbFile);
270  CloseHandle(hFileMapping);
271 
272  CloseHandle(hFile);
273  return true;
274 #else
275  CVLog::Warning("[FileMappingWriter] only support windows!");
276  return false;
277 #endif
278 }
279 
280 bool CVTools::QMappingReader(const std::string& filename,
281  std::vector<size_t>& indices) {
282  indices.clear();
283 
284  QFile input(filename.c_str());
285  if (!input.open(QIODevice::ReadOnly)) {
286  CVLog::Error(QString("[CVTools::QMappingReader] Cannot open file : %1")
287  .arg(filename.c_str()));
288  return false;
289  }
290 
291  int size = static_cast<int>(input.size());
292  // memory map
293  uchar* fptr = input.map(0, input.size());
294  if (!fptr) {
295  CVLog::Error(QString("[CVTools::QMappingReader] Cannot open file : %1")
296  .arg(filename.c_str()));
297  return false;
298  }
299 
300  char skipChar1[2] = "\r";
301  char skipChar2[2] = "\n";
302  char* buf = reinterpret_cast<char*>(fptr);
303  std::string currentLine;
304  int value;
305  int index = 0; // line index
306  for (int i = 0; i < size; ++i) {
307  // skip \r if exist
308  if (buf[i] == *skipChar1) {
309  continue;
310  }
311  // detect \n and output current line
312  if (buf[i] == *skipChar2) {
313  value = std::stoi(currentLine);
314  currentLine.clear();
315  if (value < 0) {
316  continue;
317  }
318  indices.push_back(static_cast<size_t>(value));
319  index++;
320  continue;
321  }
322 
323  currentLine += buf[i];
324  }
325 
326  input.unmap(fptr);
327  input.close();
328  return true;
329 }
330 
331 bool CVTools::QMappingWriter(const std::string& filename,
332  const void* data,
333  std::size_t length) {
334  QFile file(filename.c_str());
335  if (!file.exists()) {
336  file.open(QIODevice::WriteOnly);
337  file.close();
338  }
339 
340  if (!file.resize(static_cast<qint64>(length))) {
341  CVLog::Error(
342  "[CVTools::QMappingWriter] Reserve space error! May have not "
343  "enough space avaliable!");
344  return false;
345  }
346 
347  if (!file.open(QIODevice::ReadWrite)) {
348  CVLog::Error(QString("[CVTools::QMappingWriter] Cannot open file : %1")
349  .arg(filename.c_str()));
350  return false;
351  }
352 
353  // memory map
354  uchar* fptr = file.map(0, file.size());
355  if (!fptr) {
356  CVLog::Error(
357  QString("[CVTools::QMappingWriter] Mapping file(%1) failed!")
358  .arg(filename.c_str()));
359  fptr = file.map(0, static_cast<qint64>(length));
360  return false;
361  }
362 
363  char* buf = reinterpret_cast<char*>(fptr);
364  if (!buf) {
365  CVLog::Error(
366  QString("[CVTools::QMappingWriter] Converting uchar* to char* "
367  "failed!"));
368  return false;
369  }
370 
371  memcpy(buf, data, length);
372 
373  file.unmap(fptr);
374  file.close();
375  return true;
376 }
std::string filename
int size
QtCompatTextCodec * qtCompatCodecForLocale()
Definition: QtCompat.h:650
#define NULL
core::Tensor result
Definition: VtkUtils.cpp:76
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
Definition: CVLog.cpp:153
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
static QString TimeOff()
Definition: CVTools.cpp:39
static std::string ExtractDigitAlpha(const std::string &str)
Definition: CVTools.cpp:80
static std::string FromQString(const QString &qs)
Definition: CVTools.cpp:100
static QString ToQString(const std::string &s)
Definition: CVTools.cpp:92
static int TranslateKeyCode(int key)
Definition: CVTools.cpp:119
static std::string FromUnicode(const QString &qstr)
Definition: CVTools.cpp:56
static void TimeStart()
Definition: CVTools.cpp:37
static std::wstring Char2Wchar(const char *szStr)
Definition: CVTools.cpp:169
static bool QMappingWriter(const std::string &filename, const void *data, std::size_t length)
Definition: CVTools.cpp:331
static QString ToUnicode(const std::string &cstr)
Definition: CVTools.cpp:68
static std::string Wchar2Char(const wchar_t *szStr)
Definition: CVTools.cpp:175
static std::string GetFileName(const std::string file_name)
Definition: CVTools.cpp:29
static bool QMappingReader(const std::string &filename, std::vector< size_t > &indices)
Definition: CVTools.cpp:280
static bool FileMappingReader(const std::string &filename, void *data, unsigned long &size)
Definition: CVTools.cpp:181
static bool FileMappingWriter(const std::string &filename, const void *data, unsigned long size)
Definition: CVTools.cpp:234
static QString ToNativeSeparators(const QString &path)
Definition: CVTools.cpp:45
static std::string JoinStrVec(const std::vector< std::string > &v, std::string splitor=" ")
Definition: CVTools.cpp:108
QString toUnicode(const char *chars, int len=-1)
Definition: QtCompat.h:610
QByteArray fromUnicode(const QString &str)
Definition: QtCompat.h:594
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
static const std::string path
Definition: PointCloud.cpp:59
unsigned char uchar
Definition: matrix.h:41