Open3D (C++ API)  0.20.0
Loading...
Searching...
No Matches
Utility.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// Copyright (c) 2018-2026 www.open3d.org
5// SPDX-License-Identifier: MIT
6// ----------------------------------------------------------------------------
7
8#pragma once
9
10#include "open3d/core/Tensor.h"
12
13namespace open3d {
14namespace t {
15namespace geometry {
16
17inline void CheckDepthTensor(const core::Tensor& depth) {
18 if (depth.NumElements() == 0) {
19 utility::LogError("Input depth is empty.");
20 }
21 if (depth.GetDtype() != core::UInt16 && depth.GetDtype() != core::Float32) {
22 utility::LogError("Unsupported depth image dtype {}.",
23 depth.GetDtype().ToString());
24 }
25}
26
27inline void CheckColorTensor(const core::Tensor& color) {
28 if (color.NumElements() == 0) {
29 utility::LogError("Input color is empty.");
30 }
31 if (color.GetDtype() != core::UInt8 && color.GetDtype() != core::Float32) {
32 utility::LogError("Unsupported color image dtype {}.",
33 color.GetDtype().ToString());
34 }
35}
36
37inline void CheckIntrinsicTensor(const core::Tensor& intrinsic) {
38 if (intrinsic.GetShape() != core::SizeVector{3, 3}) {
39 utility::LogError("Unsupported intrinsic matrix shape {}",
40 intrinsic.GetShape());
41 }
42
43 if (intrinsic.GetDtype() != core::Dtype::Float64) {
44 utility::LogError("Unsupported intrinsic matrix dtype {}",
45 intrinsic.GetDtype().ToString());
46 }
47
48 if (!intrinsic.IsContiguous()) {
49 utility::LogError("Intrinsic matrix must be contiguous.");
50 }
51}
52
53inline void CheckExtrinsicTensor(const core::Tensor& extrinsic) {
54 if (extrinsic.GetShape() != core::SizeVector{4, 4}) {
55 utility::LogError("Unsupported extrinsic matrix shape {}",
56 extrinsic.GetShape());
57 }
58
59 if (extrinsic.GetDtype() != core::Dtype::Float64) {
60 utility::LogError("Unsupported extrinsic matrix dtype {}",
61 extrinsic.GetDtype().ToString());
62 }
63
64 if (!extrinsic.IsContiguous()) {
65 utility::LogError("Extrinsic matrix must be contiguous.");
66 }
67}
68
69inline void CheckBlockCoordinates(const core::Tensor& block_coords) {
70 if (block_coords.GetDtype() != core::Dtype::Int32) {
71 utility::LogError("Unsupported block coordinate dtype {}",
72 block_coords.GetDtype().ToString());
73 }
74}
75
78 // Move to CPU for host-side computation (result is always CPU).
79 core::Tensor T_cpu = T.To(core::Device("CPU:0")).Contiguous();
80 core::AssertTensorShape(T_cpu, {4, 4});
81 core::AssertTensorDtype(T_cpu, core::Float64);
82
83 core::Tensor Tinv({4, 4}, core::Float64, core::Device("CPU:0"));
84 const double* T_ptr = T_cpu.GetDataPtr<double>();
85 double* Tinv_ptr = Tinv.GetDataPtr<double>();
86
87 // R' = R.T
88 Tinv_ptr[0 * 4 + 0] = T_ptr[0 * 4 + 0];
89 Tinv_ptr[0 * 4 + 1] = T_ptr[1 * 4 + 0];
90 Tinv_ptr[0 * 4 + 2] = T_ptr[2 * 4 + 0];
91
92 Tinv_ptr[1 * 4 + 0] = T_ptr[0 * 4 + 1];
93 Tinv_ptr[1 * 4 + 1] = T_ptr[1 * 4 + 1];
94 Tinv_ptr[1 * 4 + 2] = T_ptr[2 * 4 + 1];
95
96 Tinv_ptr[2 * 4 + 0] = T_ptr[0 * 4 + 2];
97 Tinv_ptr[2 * 4 + 1] = T_ptr[1 * 4 + 2];
98 Tinv_ptr[2 * 4 + 2] = T_ptr[2 * 4 + 2];
99
100 // t' = -R.T @ t = -R' @ t
101 Tinv_ptr[0 * 4 + 3] = -(Tinv_ptr[0 * 4 + 0] * T_ptr[0 * 4 + 3] +
102 Tinv_ptr[0 * 4 + 1] * T_ptr[1 * 4 + 3] +
103 Tinv_ptr[0 * 4 + 2] * T_ptr[2 * 4 + 3]);
104 Tinv_ptr[1 * 4 + 3] = -(Tinv_ptr[1 * 4 + 0] * T_ptr[0 * 4 + 3] +
105 Tinv_ptr[1 * 4 + 1] * T_ptr[1 * 4 + 3] +
106 Tinv_ptr[1 * 4 + 2] * T_ptr[2 * 4 + 3]);
107 Tinv_ptr[2 * 4 + 3] = -(Tinv_ptr[2 * 4 + 0] * T_ptr[0 * 4 + 3] +
108 Tinv_ptr[2 * 4 + 1] * T_ptr[1 * 4 + 3] +
109 Tinv_ptr[2 * 4 + 2] * T_ptr[2 * 4 + 3]);
110
111 // Remaining part
112 Tinv_ptr[3 * 4 + 0] = 0;
113 Tinv_ptr[3 * 4 + 1] = 0;
114 Tinv_ptr[3 * 4 + 2] = 0;
115 Tinv_ptr[3 * 4 + 3] = 1;
116
117 return Tinv;
118}
119} // namespace geometry
120} // namespace t
121} // namespace open3d
math::float4 color
Definition LineSetBuffers.cpp:45
double t
Definition SurfaceReconstructionPoisson.cpp:175
Definition Device.h:18
static const Dtype Float64
Definition Dtype.h:24
static const Dtype Int32
Definition Dtype.h:27
std::string ToString() const
Definition Dtype.h:64
Definition SizeVector.h:69
Definition Tensor.h:32
SizeVector GetShape() const
Definition Tensor.h:1186
T * GetDataPtr()
Definition Tensor.h:1203
Tensor Contiguous() const
Definition Tensor.cpp:817
bool IsContiguous() const
Definition Tensor.h:1093
int64_t NumElements() const
Definition Tensor.h:1229
Dtype GetDtype() const
Definition Tensor.h:1223
Tensor To(Dtype dtype, bool copy=false) const
Definition Tensor.cpp:784
const Dtype UInt16
Definition Dtype.cpp:49
const Dtype UInt8
Definition Dtype.cpp:48
const Dtype Float64
Definition Dtype.cpp:43
const Dtype Float32
Definition Dtype.cpp:42
core::Tensor InverseTransformation(const core::Tensor &T)
TODO(wei): find a proper place for such functionalities.
Definition Utility.h:77
void CheckIntrinsicTensor(const core::Tensor &intrinsic)
Definition Utility.h:37
void CheckBlockCoordinates(const core::Tensor &block_coords)
Definition Utility.h:69
void CheckColorTensor(const core::Tensor &color)
Definition Utility.h:27
void CheckDepthTensor(const core::Tensor &depth)
Definition Utility.h:17
void CheckExtrinsicTensor(const core::Tensor &extrinsic)
Definition Utility.h:53
Definition PinholeCameraIntrinsic.cpp:16