Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TSDFVoxel.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include <atomic>
28 
29 #include "open3d/core/Dispatch.h"
32 
33 #define DISPATCH_BYTESIZE_TO_VOXEL(BYTESIZE, ...) \
34  [&] { \
35  if (BYTESIZE == sizeof(ColoredVoxel32f)) { \
36  using voxel_t = ColoredVoxel32f; \
37  return __VA_ARGS__(); \
38  } else if (BYTESIZE == sizeof(ColoredVoxel16i)) { \
39  using voxel_t = ColoredVoxel16i; \
40  return __VA_ARGS__(); \
41  } else if (BYTESIZE == sizeof(Voxel32f)) { \
42  using voxel_t = Voxel32f; \
43  return __VA_ARGS__(); \
44  } else { \
45  utility::LogError("Unsupported voxel bytesize"); \
46  } \
47  }()
48 
49 namespace open3d {
50 namespace t {
51 namespace geometry {
52 namespace kernel {
53 namespace tsdf {
57 struct Voxel32f {
58  float tsdf;
59  float weight;
60 
61  static bool HasColor() { return false; }
62  OPEN3D_HOST_DEVICE float GetTSDF() { return tsdf; }
63  OPEN3D_HOST_DEVICE float GetWeight() { return static_cast<float>(weight); }
64  OPEN3D_HOST_DEVICE float GetR() { return 1.0; }
65  OPEN3D_HOST_DEVICE float GetG() { return 1.0; }
66  OPEN3D_HOST_DEVICE float GetB() { return 1.0; }
67 
68  OPEN3D_HOST_DEVICE void Integrate(float dsdf) {
69  tsdf = (weight * tsdf + dsdf) / (weight + 1);
70  weight += 1;
71  }
72  OPEN3D_HOST_DEVICE void Integrate(float dsdf,
73  float dr,
74  float dg,
75  float db) {
76  printf("[Voxel32f] should never reach here.\n");
77  }
78 };
79 
86  static const uint16_t kMaxUint16 = 65535;
87  static constexpr float kColorFactor = 255.0f;
88 
89  float tsdf;
90  uint16_t weight;
91 
92  uint16_t r;
93  uint16_t g;
94  uint16_t b;
95 
96  static bool HasColor() { return true; }
97  OPEN3D_HOST_DEVICE float GetTSDF() { return tsdf; }
98  OPEN3D_HOST_DEVICE float GetWeight() { return static_cast<float>(weight); }
100  return static_cast<float>(r / kColorFactor);
101  }
103  return static_cast<float>(g / kColorFactor);
104  }
106  return static_cast<float>(b / kColorFactor);
107  }
108  OPEN3D_HOST_DEVICE void Integrate(float dsdf) {
109  float inc_wsum = static_cast<float>(weight) + 1;
110  float inv_wsum = 1.0f / inc_wsum;
111  tsdf = (static_cast<float>(weight) * tsdf + dsdf) * inv_wsum;
112  weight = static_cast<uint16_t>(inc_wsum < static_cast<float>(kMaxUint16)
113  ? weight + 1
114  : kMaxUint16);
115  }
116  OPEN3D_HOST_DEVICE void Integrate(float dsdf,
117  float dr,
118  float dg,
119  float db) {
120  float inc_wsum = static_cast<float>(weight) + 1;
121  float inv_wsum = 1.0f / inc_wsum;
122  tsdf = (weight * tsdf + dsdf) * inv_wsum;
123  r = static_cast<uint16_t>(
124  roundf((weight * r + dr * kColorFactor) * inv_wsum));
125  g = static_cast<uint16_t>(
126  roundf((weight * g + dg * kColorFactor) * inv_wsum));
127  b = static_cast<uint16_t>(
128  roundf((weight * b + db * kColorFactor) * inv_wsum));
129  weight = static_cast<uint16_t>(inc_wsum < static_cast<float>(kMaxUint16)
130  ? weight + 1
131  : kMaxUint16);
132  }
133 };
134 
138  float tsdf;
139  float weight;
140 
141  float r;
142  float g;
143  float b;
144 
145  static bool HasColor() { return true; }
146  OPEN3D_HOST_DEVICE float GetTSDF() { return tsdf; }
147  OPEN3D_HOST_DEVICE float GetWeight() { return weight; }
148  OPEN3D_HOST_DEVICE float GetR() { return r; }
149  OPEN3D_HOST_DEVICE float GetG() { return g; }
150  OPEN3D_HOST_DEVICE float GetB() { return b; }
151  OPEN3D_HOST_DEVICE void Integrate(float dsdf) {
152  float inv_wsum = 1.0f / (weight + 1);
153  tsdf = (weight * tsdf + dsdf) * inv_wsum;
154  weight += 1;
155  }
156  OPEN3D_HOST_DEVICE void Integrate(float dsdf,
157  float dr,
158  float dg,
159  float db) {
160  float inv_wsum = 1.0f / (weight + 1);
161  tsdf = (weight * tsdf + dsdf) * inv_wsum;
162  r = (weight * r + dr) * inv_wsum;
163  g = (weight * g + dg) * inv_wsum;
164  b = (weight * b + db) * inv_wsum;
165 
166  weight += 1;
167  }
168 };
169 
170 // Get a voxel in a certain voxel block given the block id with its neighbors.
171 template <typename voxel_t>
173  int xo,
174  int yo,
175  int zo,
176  int curr_block_idx,
177  int resolution,
178  const NDArrayIndexer& nb_block_masks_indexer,
179  const NDArrayIndexer& nb_block_indices_indexer,
180  const NDArrayIndexer& blocks_indexer) {
181  int xn = (xo + resolution) % resolution;
182  int yn = (yo + resolution) % resolution;
183  int zn = (zo + resolution) % resolution;
184 
185  int64_t dxb = Sign(xo - xn);
186  int64_t dyb = Sign(yo - yn);
187  int64_t dzb = Sign(zo - zn);
188 
189  int64_t nb_idx = (dxb + 1) + (dyb + 1) * 3 + (dzb + 1) * 9;
190 
191  bool block_mask_i =
192  *nb_block_masks_indexer.GetDataPtr<bool>(curr_block_idx, nb_idx);
193  if (!block_mask_i) return nullptr;
194 
195  int64_t block_idx_i = *nb_block_indices_indexer.GetDataPtr<int64_t>(
196  curr_block_idx, nb_idx);
197 
198  return blocks_indexer.GetDataPtr<voxel_t>(xn, yn, zn, block_idx_i);
199 }
200 
201 // Get TSDF gradient as normal in a certain voxel block given the block id with
202 // its neighbors.
203 template <typename voxel_t>
205  int xo,
206  int yo,
207  int zo,
208  int curr_block_idx,
209  float* n,
210  int resolution,
211  float voxel_size,
212  const NDArrayIndexer& nb_block_masks_indexer,
213  const NDArrayIndexer& nb_block_indices_indexer,
214  const NDArrayIndexer& blocks_indexer) {
215  auto GetVoxelAt = [&] OPEN3D_DEVICE(int xo, int yo, int zo) {
216  return DeviceGetVoxelAt<voxel_t>(
217  xo, yo, zo, curr_block_idx, resolution, nb_block_masks_indexer,
218  nb_block_indices_indexer, blocks_indexer);
219  };
220  voxel_t* vxp = GetVoxelAt(xo + 1, yo, zo);
221  voxel_t* vxn = GetVoxelAt(xo - 1, yo, zo);
222  voxel_t* vyp = GetVoxelAt(xo, yo + 1, zo);
223  voxel_t* vyn = GetVoxelAt(xo, yo - 1, zo);
224  voxel_t* vzp = GetVoxelAt(xo, yo, zo + 1);
225  voxel_t* vzn = GetVoxelAt(xo, yo, zo - 1);
226  if (vxp && vxn) n[0] = (vxp->GetTSDF() - vxn->GetTSDF()) / (2 * voxel_size);
227  if (vyp && vyn) n[1] = (vyp->GetTSDF() - vyn->GetTSDF()) / (2 * voxel_size);
228  if (vzp && vzn) n[2] = (vzp->GetTSDF() - vzn->GetTSDF()) / (2 * voxel_size);
229 };
230 
231 inline OPEN3D_DEVICE int64_t
233  int yo,
234  int zo,
235  int curr_block_idx,
236  int resolution,
237  const NDArrayIndexer& nb_block_masks_indexer,
238  const NDArrayIndexer& nb_block_indices_indexer) {
239  int xn = (xo + resolution) % resolution;
240  int yn = (yo + resolution) % resolution;
241  int zn = (zo + resolution) % resolution;
242 
243  int64_t dxb = Sign(xo - xn);
244  int64_t dyb = Sign(yo - yn);
245  int64_t dzb = Sign(zo - zn);
246 
247  int64_t nb_idx = (dxb + 1) + (dyb + 1) * 3 + (dzb + 1) * 9;
248 
249  bool block_mask_i =
250  *nb_block_masks_indexer.GetDataPtr<bool>(curr_block_idx, nb_idx);
251  if (!block_mask_i) return -1;
252 
253  int block_idx_i =
254  *nb_block_indices_indexer.GetDataPtr<int>(curr_block_idx, nb_idx);
255 
256  return (((block_idx_i * resolution) + zn) * resolution + yn) * resolution +
257  xn;
258 }
259 
260 } // namespace tsdf
261 } // namespace kernel
262 } // namespace geometry
263 } // namespace t
264 } // namespace open3d
OPEN3D_HOST_DEVICE void Integrate(float dsdf, float dr, float dg, float db)
Definition: TSDFVoxel.h:156
Definition: GeometryIndexer.h:180
OPEN3D_HOST_DEVICE float GetR()
Definition: TSDFVoxel.h:64
OPEN3D_HOST_DEVICE void * GetDataPtr() const
Definition: GeometryIndexer.h:335
OPEN3D_HOST_DEVICE int Sign(int x)
Definition: GeometryMacros.h:96
OPEN3D_HOST_DEVICE void Integrate(float dsdf)
Definition: TSDFVoxel.h:108
OPEN3D_DEVICE void DeviceGetNormalAt(int xo, int yo, int zo, int curr_block_idx, float *n, int resolution, float voxel_size, const NDArrayIndexer &nb_block_masks_indexer, const NDArrayIndexer &nb_block_indices_indexer, const NDArrayIndexer &blocks_indexer)
Definition: TSDFVoxel.h:204
OPEN3D_HOST_DEVICE void Integrate(float dsdf, float dr, float dg, float db)
Definition: TSDFVoxel.h:116
OPEN3D_HOST_DEVICE float GetTSDF()
Definition: TSDFVoxel.h:146
float weight
Definition: TSDFVoxel.h:59
float tsdf
Definition: TSDFVoxel.h:58
static bool HasColor()
Definition: TSDFVoxel.h:61
OPEN3D_HOST_DEVICE float GetB()
Definition: TSDFVoxel.h:66
OPEN3D_HOST_DEVICE void Integrate(float dsdf, float dr, float dg, float db)
Definition: TSDFVoxel.h:72
#define OPEN3D_DEVICE
Definition: CUDAUtils.h:64
#define OPEN3D_HOST_DEVICE
Definition: CUDAUtils.h:63
static bool HasColor()
Definition: TSDFVoxel.h:96
OPEN3D_DEVICE voxel_t * DeviceGetVoxelAt(int xo, int yo, int zo, int curr_block_idx, int resolution, const NDArrayIndexer &nb_block_masks_indexer, const NDArrayIndexer &nb_block_indices_indexer, const NDArrayIndexer &blocks_indexer)
Definition: TSDFVoxel.h:172
OPEN3D_HOST_DEVICE float GetTSDF()
Definition: TSDFVoxel.h:62
OPEN3D_HOST_DEVICE float GetWeight()
Definition: TSDFVoxel.h:63
float weight
Definition: TSDFVoxel.h:139
OPEN3D_HOST_DEVICE float GetG()
Definition: TSDFVoxel.h:149
OPEN3D_HOST_DEVICE float GetR()
Definition: TSDFVoxel.h:99
OPEN3D_HOST_DEVICE float GetG()
Definition: TSDFVoxel.h:65
uint16_t g
Definition: TSDFVoxel.h:93
OPEN3D_HOST_DEVICE float GetB()
Definition: TSDFVoxel.h:105
OPEN3D_HOST_DEVICE void Integrate(float dsdf)
Definition: TSDFVoxel.h:68
OPEN3D_HOST_DEVICE float GetTSDF()
Definition: TSDFVoxel.h:97
static bool HasColor()
Definition: TSDFVoxel.h:145
Definition: PinholeCameraIntrinsic.cpp:35
OPEN3D_HOST_DEVICE float GetWeight()
Definition: TSDFVoxel.h:98
OPEN3D_DEVICE int64_t DeviceGetLinearIdx(int xo, int yo, int zo, int curr_block_idx, int resolution, const NDArrayIndexer &nb_block_masks_indexer, const NDArrayIndexer &nb_block_indices_indexer)
Definition: TSDFVoxel.h:232
OPEN3D_HOST_DEVICE float GetG()
Definition: TSDFVoxel.h:102
uint16_t weight
Definition: TSDFVoxel.h:90
OPEN3D_HOST_DEVICE float GetB()
Definition: TSDFVoxel.h:150
OPEN3D_HOST_DEVICE float GetR()
Definition: TSDFVoxel.h:148
OPEN3D_HOST_DEVICE float GetWeight()
Definition: TSDFVoxel.h:147
uint16_t b
Definition: TSDFVoxel.h:94
OPEN3D_HOST_DEVICE void Integrate(float dsdf)
Definition: TSDFVoxel.h:151
uint16_t r
Definition: TSDFVoxel.h:92