ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Common.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 <stdio.h>
11 
12 #include <ctime>
13 #include <functional>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 
18 namespace patch {
19 template <typename T>
20 std::string to_string(const T& n) {
21  std::ostringstream stm;
22  stm << n;
23  return stm.str();
24 }
25 } // namespace patch
26 
27 enum fidelityType { L2, linear, KL, SPG };
28 
30  GenericParameter(std::string inName = "in_name",
31  double reg_strength = 0,
32  double fidelity = 0) {
33  this->in_name = inName;
34  char* buffer = new char[inName.size() + 10];
35  // char buffer [inName.size() + 10];
36  std::string extension = inName.substr(inName.find_last_of(".") + 1);
37  this->extension = extension;
38  std::string baseName =
39  inName.substr(0, inName.size() - extension.size() - 1);
40  this->base_name = baseName;
41  sprintf(buffer, "%s_out_%1.0f_%.0f.%s", baseName.c_str(), fidelity,
42  reg_strength * 1000, extension.c_str());
43  this->out_name = std::string(buffer);
44  this->natureOfData = 0;
45  this->fidelity = L2;
46  }
47 
51 };
52 
53 class TimeStack {
54 public:
56 
57  void tic() { lastTime = clock(); }
58 
59  std::string toc() const {
60  std::ostringstream stm;
61  stm << static_cast<double>(clock() - lastTime) / CLOCKS_PER_SEC;
62  return stm.str();
63  }
64 
65  double tocDouble() const {
66  double x = static_cast<double>(clock() - lastTime) / CLOCKS_PER_SEC;
67  return x;
68  }
69 
70 protected:
71  clock_t lastTime;
72 };
73 
74 template <typename T>
75 struct ComponentsFusion { // this class encode a potential fusion between two
76  // cadjacent component and is ordered wrt the
77  // merge_gain
78  ComponentsFusion(std::size_t c1,
79  std::size_t c2,
80  std::size_t ind = 0,
81  T gain = 0.) {
82  this->comp1 = c1;
83  this->comp2 = c2;
84  this->border_index = ind;
85  this->merge_gain = gain;
86  }
87 
88  std::size_t comp1, comp2; // index of the components
89  std::size_t border_index; // index of the border-edge
90  T merge_gain; // gain obtained by mergeing the components
91  std::vector<T>
92  merged_value; // value of the new components when they are merged
93 };
94 
95 template <typename T>
98  const ComponentsFusion<T> rhs) const {
99  return lhs.merge_gain < rhs.merge_gain;
100  }
101 };
102 
103 template <typename T>
105  // VectorOfCentroids is a vector of size k x 2 x d where k is the number of
106  // components and
107  // d the dimension of the observation
108 public:
109  std::vector<std::vector<std::vector<T>>> centroids;
110  VectorOfCentroids(std::size_t nb_comp, std::size_t dim) {
111  this->centroids = std::vector<std::vector<std::vector<T>>>(
112  nb_comp,
113  std::vector<std::vector<T>>(2, std::vector<T>(dim, 0.0)));
114  }
115 };
116 template <typename T>
117 class Point3D {
118 public:
119  T x, y, z;
120  Point3D(T x = 0., T y = 0., T z = 0.) {
121  this->x = x;
122  this->y = y;
123  this->z = z;
124  }
125 };
126 
127 template <typename T>
128 struct lessPoint3D {
129  bool operator()(const Point3D<T> lhs, const Point3D<T> rhs) const {
130  if (lhs.x != rhs.x) {
131  return lhs.x < rhs.x;
132  }
133  if (lhs.y != rhs.y) {
134  return lhs.y < rhs.y;
135  }
136  if (lhs.z > rhs.z) {
137  return lhs.z < rhs.z;
138  }
139  return true;
140  }
141 };
fidelityType
Definition: Common.h:27
@ L2
Definition: Common.h:27
@ linear
Definition: Common.h:27
@ KL
Definition: Common.h:27
@ SPG
Definition: Common.h:27
TimeStack()
Definition: Common.h:55
clock_t lastTime
Definition: Common.h:71
std::string toc() const
Definition: Common.h:59
double tocDouble() const
Definition: Common.h:65
void tic()
Definition: Common.h:57
std::vector< std::vector< std::vector< T > > > centroids
Definition: Common.h:109
VectorOfCentroids(std::size_t nb_comp, std::size_t dim)
Definition: Common.h:110
Definition: Common.h:18
std::string to_string(const T &n)
Definition: Common.h:20
std::size_t comp1
Definition: Common.h:88
ComponentsFusion(std::size_t c1, std::size_t c2, std::size_t ind=0, T gain=0.)
Definition: Common.h:78
std::size_t border_index
Definition: Common.h:89
std::size_t comp2
Definition: Common.h:88
std::vector< T > merged_value
Definition: Common.h:92
std::string out_name
Definition: Common.h:48
fidelityType fidelity
Definition: Common.h:50
GenericParameter(std::string inName="in_name", double reg_strength=0, double fidelity=0)
Definition: Common.h:30
std::string in_name
Definition: Common.h:48
int natureOfData
Definition: Common.h:49
std::string base_name
Definition: Common.h:48
std::string extension
Definition: Common.h:48
T y
Definition: Common.h:119
T z
Definition: Common.h:119
Point3D(T x=0., T y=0., T z=0.)
Definition: Common.h:120
T x
Definition: Common.h:119
bool operator()(const ComponentsFusion< T > lhs, const ComponentsFusion< T > rhs) const
Definition: Common.h:97
bool operator()(const Point3D< T > lhs, const Point3D< T > rhs) const
Definition: Common.h:129