ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
utils.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 "utils.h"
9 
10 #include <QPainter>
11 #include <QPainterPath>
12 #include <QProcess>
13 #include <cstdlib>
14 
15 const float Pi = 3.14159f;
16 
17 namespace Utils {
18 
19 QString character(int index) {
20  char baseChar = 'x';
21  QString strChar = QString("%1").arg((char)(baseChar + index % 26));
22  return strChar;
23 }
24 
25 void explorer(const QString& path) {
26 #ifdef Q_OS_WIN
27  QString tmpPath(path);
28  QStringList params;
29  params << "/select," << tmpPath.replace("/", "\\");
30  QProcess::startDetached("explorer.exe", params);
31 #endif
32 }
33 
34 QImage star(const QSize& size) {
35  QPainterPath starPath;
36  starPath.moveTo(30, 15);
37  for (int i = 1; i < 5; ++i) {
38  starPath.lineTo(15 + 15 * qCos(0.8 * i * Pi),
39  15 + 15 * qSin(0.8 * i * Pi));
40  }
41  starPath.closeSubpath();
42 
43  QImage star(size, QImage::Format_ARGB32);
44  star.fill(Qt::transparent);
45 
46  QPainter painter(&star);
47  painter.setRenderHint(QPainter::Antialiasing);
48  painter.setPen(QRgb(0xf6a625));
49  painter.setBrush(painter.pen().color());
50  painter.drawPath(starPath);
51 
52  return star;
53 }
54 
55 double random(int low, int high) {
56  double f = (double)std::rand() / RAND_MAX;
57  return f * (high - low) + low;
58 }
59 
60 void vtkColor(const QColor& clr, double* vtkClr) {
61  double r = (double)clr.red() / 255;
62  double g = (double)clr.green() / 255;
63  double b = (double)clr.blue() / 255;
64  vtkClr[0] = r;
65  vtkClr[1] = g;
66  vtkClr[2] = b;
67 }
68 
69 QColor qColor(double* pClr) {
70  return QColor(pClr[0] * 255, pClr[1] * 255, pClr[2] * 255);
71 }
72 
73 static void RGB2HSV(float r, float g, float b, float* h, float* s, float* v) {
74  float min, max, delta;
75  min = qMin(r, qMin(g, b));
76  max = qMax(r, qMax(g, b));
77  *v = max; // v
78  delta = max - min;
79  if (max != 0) {
80  *s = delta / max; // s
81  } else {
82  // r = g = b = 0 // s = 0, v is undefined
83  *s = 0;
84  *h = -1;
85  return;
86  }
87  if (r == max)
88  *h = (g - b) / delta; // between yellow & magenta
89  else if (g == max)
90  *h = 2 + (b - r) / delta; // between cyan & yellow
91  else
92  *h = 4 + (r - g) / delta; // between magenta & cyan
93  *h *= 60; // degrees
94  if (*h < 0) *h += 360;
95 }
96 
97 void qColor2HSV(const QColor& clr, double* hsv) {
98  double clrArr[3];
99  vtkColor(clr, clrArr);
100  float h, s, v;
101  RGB2HSV(clrArr[0], clrArr[1], clrArr[2], &h, &s, &v);
102  hsv[0] = (float)h / 360;
103  hsv[1] = s;
104  hsv[2] = v;
105 }
106 
107 } // namespace Utils
int size
float
static void RGB2HSV(float r, float g, float b, float *h, float *s, float *v)
Definition: utils.cpp:73
QColor qColor(double *pClr)
Definition: utils.cpp:69
void vtkColor(const QColor &clr, double *vtkClr)
Definition: utils.cpp:60
double random(int low, int high)
Definition: utils.cpp:55
QString character(int index)
Definition: utils.cpp:19
void qColor2HSV(const QColor &clr, double *hsv)
Definition: utils.cpp:97
QImage star(const QSize &size)
Definition: utils.cpp:34
void explorer(const QString &path)
Definition: utils.cpp:25
static const std::string path
Definition: PointCloud.cpp:59
const float Pi
Definition: utils.cpp:15