ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
utils.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 <QColor>
11 #include <QImage>
12 #include <QtMath>
13 #include <complex>
14 
15 #include "../qPCL.h"
16 
17 class vtkActor;
18 namespace Utils {
19 
20 // alphabetical char
21 QString QPCL_ENGINE_LIB_API character(int index);
22 void QPCL_ENGINE_LIB_API explorer(const QString& path);
23 QImage QPCL_ENGINE_LIB_API star(const QSize& size = QSize(30, 30));
24 
25 // declare some tools functions
26 double QPCL_ENGINE_LIB_API random(int low, int high);
27 
28 template <typename T>
29 inline static std::complex<T> random(int low, int high) {
30  std::complex<T> c(random(low, high), random(low, high));
31  return c;
32 }
33 
34 // convert QColor to double[3] clr
35 void QPCL_ENGINE_LIB_API vtkColor(const QColor& clr, double* vtkClr);
36 QColor QPCL_ENGINE_LIB_API qColor(double* pClr);
37 void QPCL_ENGINE_LIB_API qColor2HSV(const QColor& clr, double* hsv);
38 
39 template <typename T, int size = 3>
41 public:
42  bool operator()(const T* lhs, const T* rhs) {
43  for (auto i = 0; i < size; ++i) {
44  if (lhs[i] != rhs[i]) return false;
45  }
46  return true;
47  }
48 };
49 
50 template <typename T, int size = 3>
52 public:
53  void operator()(T* lhs, const T* rhs) {
54  // memset(lhs, rhs, 3 * sizeof(T));
55  for (auto i = 0; i < size; ++i) lhs[i] = rhs[i];
56  }
57 };
58 
59 template <typename T, int size = 3>
61 public:
62  void operator()(T* array, T value = T()) {
63  for (auto i = 0; i < size; ++i) array[i] = value;
64  }
65 };
66 
67 class Normalizer {
68 public:
69  void operator()(const double* input, double* output) {
70  double mod = qSqrt(input[0] * input[0] + input[1] * input[1] +
71  input[2] * input[2]);
72  if (mod == 0) {
73  output[0] = input[0];
74  output[1] = input[1];
75  output[2] = input[2];
76  } else {
77  output[0] = input[0] / mod;
78  output[1] = input[1] / mod;
79  output[2] = input[2] / mod;
80  }
81  }
82 };
83 
84 typedef QList<vtkActor*> ActorList;
85 
86 template <class T>
87 inline void vtkSafeDelete(T* obj) {
88  if (obj) obj->Delete();
89 }
90 
91 template <class T>
92 inline void vtkSafeDelete(QList<T*>& objList) {
93  foreach (T* obj, objList) obj->Delete();
94  objList.clear();
95 }
96 
97 template <typename T>
98 inline T boundedValue(const T& value, const T& min, const T& max) {
99  if (value < min) return min;
100  if (value > max) return max;
101  return value;
102 }
103 
104 template <typename T>
105 static inline double module(T* vector) {
106  return qSqrt(vector[0] * vector[0] + vector[1] * vector[1] +
107  vector[2] * vector[2]);
108 }
109 
110 template <typename T>
111 static inline double distance(T* pot1, T* pot2) {
112  double dX = pot2[0] - pot1[0];
113  double dY = pot2[1] - pot1[1];
114  double dZ = pot2[2] - pot1[2];
115  return qSqrt(dX * dX + dY * dY + dZ * dZ);
116 }
117 
121 template <typename T>
122 static inline void normal(T* inPot1, T* inPot2, T* outPot) {
123  outPot[0] = -(inPot2[1] - inPot1[1]);
124  outPot[1] = inPot2[0] - inPot1[0];
125  outPot[2] = inPot1[2];
126 }
127 
128 } // namespace Utils
int size
void operator()(T *lhs, const T *rhs)
Definition: utils.h:53
bool operator()(const T *lhs, const T *rhs)
Definition: utils.h:42
void operator()(T *array, T value=T())
Definition: utils.h:62
void operator()(const double *input, double *output)
Definition: utils.h:69
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
QList< vtkActor * > ActorList
Definition: utils.h:84
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 vtkSafeDelete(T *obj)
Definition: utils.h:87
void explorer(const QString &path)
Definition: utils.cpp:25
static double distance(T *pot1, T *pot2)
Definition: utils.h:111
static double module(T *vector)
Definition: utils.h:105
static void normal(T *inPot1, T *inPot2, T *outPot)
get vector between two points
Definition: utils.h:122
T boundedValue(const T &value, const T &min, const T &max)
Definition: utils.h:98
static const std::string path
Definition: PointCloud.cpp:59
#define QPCL_ENGINE_LIB_API
Definition: qPCL.h:15