Open3D (C++ API)  0.17.0
Cloud.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 // Source code from: https://github.com/HuguesTHOMAS/KPConv.
8 //
9 // MIT License
10 //
11 // Copyright (c) 2019 HuguesTHOMAS
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 // SOFTWARE.
30 
31 //
32 //
33 // 0==========================0
34 // | Local feature test |
35 // 0==========================0
36 //
37 // version 1.0 :
38 // >
39 //
40 //---------------------------------------------------
41 //
42 // Cloud header
43 //
44 //----------------------------------------------------
45 //
46 // Hugues THOMAS - 10/02/2017
47 //
48 
49 #pragma once
50 
51 #include <time.h>
52 
53 #include <algorithm>
54 #include <cmath>
55 #include <iomanip>
56 #include <iostream>
57 #include <map>
58 #include <numeric>
59 #include <unordered_map>
60 #include <vector>
61 
62 namespace open3d {
63 namespace ml {
64 namespace contrib {
65 
66 // Point class
67 // ***********
68 
69 class PointXYZ {
70 public:
71  // Elements
72  // ********
73 
74  float x, y, z;
75 
76  // Methods
77  // *******
78 
79  // Constructor
80  PointXYZ() = default;
81 
82  PointXYZ(float x0, float y0, float z0) {
83  x = x0;
84  y = y0;
85  z = z0;
86  }
87 
88  // array type accessor
89  float operator[](int i) const {
90  if (i == 0)
91  return x;
92  else if (i == 1)
93  return y;
94  else
95  return z;
96  }
97 
98  // operations
99  float dot(const PointXYZ P) const { return x * P.x + y * P.y + z * P.z; }
100 
101  float sq_norm() { return x * x + y * y + z * z; }
102 
103  PointXYZ cross(const PointXYZ P) const {
104  return PointXYZ(y * P.z - z * P.y, z * P.x - x * P.z,
105  x * P.y - y * P.x);
106  }
107 
109  x += P.x;
110  y += P.y;
111  z += P.z;
112  return *this;
113  }
114 
116  x -= P.x;
117  y -= P.y;
118  z -= P.z;
119  return *this;
120  }
121 
122  PointXYZ& operator*=(const float& a) {
123  x *= a;
124  y *= a;
125  z *= a;
126  return *this;
127  }
128 
129  static PointXYZ floor(const PointXYZ P) {
130  return PointXYZ(std::floor(P.x), std::floor(P.y), std::floor(P.z));
131  }
132 };
133 
134 // Point Operations
135 // *****************
136 
137 inline PointXYZ operator+(const PointXYZ A, const PointXYZ B) {
138  return PointXYZ(A.x + B.x, A.y + B.y, A.z + B.z);
139 }
140 
141 inline PointXYZ operator-(const PointXYZ A, const PointXYZ B) {
142  return PointXYZ(A.x - B.x, A.y - B.y, A.z - B.z);
143 }
144 
145 inline PointXYZ operator*(const PointXYZ P, const float a) {
146  return PointXYZ(P.x * a, P.y * a, P.z * a);
147 }
148 
149 inline PointXYZ operator*(const float a, const PointXYZ P) {
150  return PointXYZ(P.x * a, P.y * a, P.z * a);
151 }
152 
153 inline std::ostream& operator<<(std::ostream& os, const PointXYZ P) {
154  return os << "[" << P.x << ", " << P.y << ", " << P.z << "]";
155 }
156 
157 inline bool operator==(const PointXYZ A, const PointXYZ B) {
158  return A.x == B.x && A.y == B.y && A.z == B.z;
159 }
160 
161 PointXYZ max_point(std::vector<PointXYZ> points);
162 PointXYZ min_point(std::vector<PointXYZ> points);
163 
164 struct PointCloud {
165  std::vector<PointXYZ> pts;
166 
167  // Must return the number of data points
168  inline size_t kdtree_get_point_count() const { return pts.size(); }
169 
170  // Returns the dim'th component of the idx'th point in the class:
171  // Since this is inlined and the "dim" argument is typically an immediate
172  // value, the
173  // "if/else's" are actually solved at compile time.
174  inline float kdtree_get_pt(const size_t idx, const size_t dim) const {
175  if (dim == 0)
176  return pts[idx].x;
177  else if (dim == 1)
178  return pts[idx].y;
179  else
180  return pts[idx].z;
181  }
182 
183  // Optional bounding-box computation: return false to default to a standard
184  // bbox computation loop.
185  // Return true if the BBOX was already computed by the class and returned
186  // in "bb" so it can be avoided to redo it again. Look at bb.size() to
187  // find out the expected dimensionality (e.g. 2 or 3 for point clouds)
188  template <class BBOX>
189  bool kdtree_get_bbox(BBOX& /* bb */) const {
190  return false;
191  }
192 };
193 
194 } // namespace contrib
195 } // namespace ml
196 } // namespace open3d
Eigen::Matrix3d B
Definition: PointCloudPlanarPatchDetection.cpp:506
Definition: Cloud.h:69
PointXYZ & operator+=(const PointXYZ &P)
Definition: Cloud.h:108
PointXYZ(float x0, float y0, float z0)
Definition: Cloud.h:82
PointXYZ & operator*=(const float &a)
Definition: Cloud.h:122
float z
Definition: Cloud.h:74
float y
Definition: Cloud.h:74
PointXYZ cross(const PointXYZ P) const
Definition: Cloud.h:103
static PointXYZ floor(const PointXYZ P)
Definition: Cloud.h:129
PointXYZ & operator-=(const PointXYZ &P)
Definition: Cloud.h:115
float operator[](int i) const
Definition: Cloud.h:89
float sq_norm()
Definition: Cloud.h:101
float dot(const PointXYZ P) const
Definition: Cloud.h:99
float x
Definition: Cloud.h:74
int points
Definition: FilePCD.cpp:54
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:157
PointXYZ operator-(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:141
PointXYZ max_point(std::vector< PointXYZ > points)
Definition: Cloud.cpp:61
PointXYZ operator+(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:137
PointXYZ operator*(const PointXYZ P, const float a)
Definition: Cloud.h:145
PointXYZ min_point(std::vector< PointXYZ > points)
Definition: Cloud.cpp:77
std::ostream & operator<<(std::ostream &os, const PointXYZ P)
Definition: Cloud.h:153
FN_SPECIFIERS MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:75
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Cloud.h:164
bool kdtree_get_bbox(BBOX &) const
Definition: Cloud.h:189
size_t kdtree_get_point_count() const
Definition: Cloud.h:168
float kdtree_get_pt(const size_t idx, const size_t dim) const
Definition: Cloud.h:174
std::vector< PointXYZ > pts
Definition: Cloud.h:165