ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Cloud.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 <time.h>
11 
12 #include <algorithm>
13 #include <cmath>
14 #include <iomanip>
15 #include <iostream>
16 #include <map>
17 #include <numeric>
18 #include <unordered_map>
19 #include <vector>
20 
21 namespace cloudViewer {
22 namespace ml {
23 namespace contrib {
24 
25 // Point class
26 // ***********
27 
28 class PointXYZ {
29 public:
30  // Elements
31  // ********
32 
33  float x, y, z;
34 
35  // Methods
36  // *******
37 
38  // Constructor
39  PointXYZ() = default;
40 
41  PointXYZ(float x0, float y0, float z0) {
42  x = x0;
43  y = y0;
44  z = z0;
45  }
46 
47  // array type accessor
48  float operator[](int i) const {
49  if (i == 0)
50  return x;
51  else if (i == 1)
52  return y;
53  else
54  return z;
55  }
56 
57  // opperations
58  float dot(const PointXYZ P) const { return x * P.x + y * P.y + z * P.z; }
59 
60  float sq_norm() { return x * x + y * y + z * z; }
61 
62  PointXYZ cross(const PointXYZ P) const {
63  return PointXYZ(y * P.z - z * P.y, z * P.x - x * P.z,
64  x * P.y - y * P.x);
65  }
66 
68  x += P.x;
69  y += P.y;
70  z += P.z;
71  return *this;
72  }
73 
75  x -= P.x;
76  y -= P.y;
77  z -= P.z;
78  return *this;
79  }
80 
81  PointXYZ& operator*=(const float& a) {
82  x *= a;
83  y *= a;
84  z *= a;
85  return *this;
86  }
87 
88  static PointXYZ floor(const PointXYZ P) {
89  return PointXYZ(std::floor(P.x), std::floor(P.y), std::floor(P.z));
90  }
91 };
92 
93 // Point Opperations
94 // *****************
95 
96 inline PointXYZ operator+(const PointXYZ A, const PointXYZ B) {
97  return PointXYZ(A.x + B.x, A.y + B.y, A.z + B.z);
98 }
99 
100 inline PointXYZ operator-(const PointXYZ A, const PointXYZ B) {
101  return PointXYZ(A.x - B.x, A.y - B.y, A.z - B.z);
102 }
103 
104 inline PointXYZ operator*(const PointXYZ P, const float a) {
105  return PointXYZ(P.x * a, P.y * a, P.z * a);
106 }
107 
108 inline PointXYZ operator*(const float a, const PointXYZ P) {
109  return PointXYZ(P.x * a, P.y * a, P.z * a);
110 }
111 
112 inline std::ostream& operator<<(std::ostream& os, const PointXYZ P) {
113  return os << "[" << P.x << ", " << P.y << ", " << P.z << "]";
114 }
115 
116 inline bool operator==(const PointXYZ A, const PointXYZ B) {
117  return A.x == B.x && A.y == B.y && A.z == B.z;
118 }
119 
120 PointXYZ max_point(std::vector<PointXYZ> points);
121 PointXYZ min_point(std::vector<PointXYZ> points);
122 
123 struct PointCloud {
124  std::vector<PointXYZ> pts;
125 
126  // Must return the number of data points
127  inline size_t kdtree_get_point_count() const { return pts.size(); }
128 
129  // Returns the dim'th component of the idx'th point in the class:
130  // Since this is inlined and the "dim" argument is typically an immediate
131  // value, the
132  // "if/else's" are actually solved at compile time.
133  inline float kdtree_get_pt(const size_t idx, const size_t dim) const {
134  if (dim == 0)
135  return pts[idx].x;
136  else if (dim == 1)
137  return pts[idx].y;
138  else
139  return pts[idx].z;
140  }
141 
142  // Optional bounding-box computation: return false to default to a standard
143  // bbox computation loop.
144  // Return true if the BBOX was already computed by the class and returned
145  // in "bb" so it can be avoided to redo it again. Look at bb.size() to
146  // find out the expected dimensionality (e.g. 2 or 3 for point clouds)
147  template <class BBOX>
148  bool kdtree_get_bbox(BBOX& /* bb */) const {
149  return false;
150  }
151 };
152 
153 } // namespace contrib
154 } // namespace ml
155 } // namespace cloudViewer
int points
float operator[](int i) const
Definition: Cloud.h:48
PointXYZ & operator-=(const PointXYZ &P)
Definition: Cloud.h:74
PointXYZ(float x0, float y0, float z0)
Definition: Cloud.h:41
float dot(const PointXYZ P) const
Definition: Cloud.h:58
PointXYZ cross(const PointXYZ P) const
Definition: Cloud.h:62
PointXYZ & operator*=(const float &a)
Definition: Cloud.h:81
PointXYZ & operator+=(const PointXYZ &P)
Definition: Cloud.h:67
static PointXYZ floor(const PointXYZ P)
Definition: Cloud.h:88
@ BBOX
Definition: CVTypes.h:154
PointXYZ min_point(std::vector< PointXYZ > points)
Definition: Cloud.cpp:37
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:116
std::ostream & operator<<(std::ostream &os, const PointXYZ P)
Definition: Cloud.h:112
PointXYZ operator-(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:100
PointXYZ operator*(const PointXYZ P, const float a)
Definition: Cloud.h:104
PointXYZ operator+(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:96
PointXYZ max_point(std::vector< PointXYZ > points)
Definition: Cloud.cpp:21
MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:75
Generic file read and write utility for python interface.
bool kdtree_get_bbox(BBOX &) const
Definition: Cloud.h:148
size_t kdtree_get_point_count() const
Definition: Cloud.h:127
std::vector< PointXYZ > pts
Definition: Cloud.h:124
float kdtree_get_pt(const size_t idx, const size_t dim) const
Definition: Cloud.h:133