Open3D (C++ API)  0.18.0+5c982c7
Dtype.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 
8 #pragma once
9 
10 #include <cstring>
11 #include <string>
12 
13 #include "open3d/Macro.h"
14 #include "open3d/core/Dispatch.h"
15 #include "open3d/utility/Logging.h"
16 
17 namespace open3d {
18 namespace core {
19 
21 public:
22  static const Dtype Undefined;
23  static const Dtype Float32;
24  static const Dtype Float64;
25  static const Dtype Int8;
26  static const Dtype Int16;
27  static const Dtype Int32;
28  static const Dtype Int64;
29  static const Dtype UInt8;
30  static const Dtype UInt16;
31  static const Dtype UInt32;
32  static const Dtype UInt64;
33  static const Dtype Bool;
34 
35 public:
36  enum class DtypeCode {
37  Undefined,
38  Bool, // Needed to distinguish bool from uint8_t.
39  Int,
40  UInt,
41  Float,
42  Object,
43  };
44 
45  Dtype() : Dtype(DtypeCode::Undefined, 1, "Undefined") {}
46 
47  explicit Dtype(DtypeCode dtype_code,
48  int64_t byte_size,
49  const std::string &name);
50 
53  template <typename T>
54  static inline const Dtype FromType() {
55  utility::LogError("Unsupported data for Dtype::FromType.");
56  }
57 
58  int64_t ByteSize() const { return byte_size_; }
59 
60  DtypeCode GetDtypeCode() const { return dtype_code_; }
61 
62  bool IsObject() const { return dtype_code_ == DtypeCode::Object; }
63 
64  std::string ToString() const { return name_; }
65 
66  bool operator==(const Dtype &other) const;
67 
68  bool operator!=(const Dtype &other) const;
69 
70 private:
71  static constexpr size_t max_name_len_ = 16;
72  DtypeCode dtype_code_;
73  int64_t byte_size_;
74  char name_[max_name_len_]; // MSVC warns if std::string is exported to DLL.
75 };
76 
77 OPEN3D_API extern const Dtype Undefined;
78 OPEN3D_API extern const Dtype Float32;
79 OPEN3D_API extern const Dtype Float64;
80 OPEN3D_API extern const Dtype Int8;
81 OPEN3D_API extern const Dtype Int16;
82 OPEN3D_API extern const Dtype Int32;
83 OPEN3D_API extern const Dtype Int64;
84 OPEN3D_API extern const Dtype UInt8;
85 OPEN3D_API extern const Dtype UInt16;
86 OPEN3D_API extern const Dtype UInt32;
87 OPEN3D_API extern const Dtype UInt64;
88 OPEN3D_API extern const Dtype Bool;
89 
90 template <>
91 inline const Dtype Dtype::FromType<float>() {
92  return Dtype::Float32;
93 }
94 
95 template <>
96 inline const Dtype Dtype::FromType<double>() {
97  return Dtype::Float64;
98 }
99 
100 template <>
101 inline const Dtype Dtype::FromType<int8_t>() {
102  return Dtype::Int8;
103 }
104 
105 template <>
106 inline const Dtype Dtype::FromType<int16_t>() {
107  return Dtype::Int16;
108 }
109 
110 template <>
111 inline const Dtype Dtype::FromType<int32_t>() {
112  return Dtype::Int32;
113 }
114 
115 template <>
116 inline const Dtype Dtype::FromType<int64_t>() {
117  return Dtype::Int64;
118 }
119 
120 template <>
121 inline const Dtype Dtype::FromType<uint8_t>() {
122  return Dtype::UInt8;
123 }
124 
125 template <>
126 inline const Dtype Dtype::FromType<uint16_t>() {
127  return Dtype::UInt16;
128 }
129 
130 template <>
131 inline const Dtype Dtype::FromType<uint32_t>() {
132  return Dtype::UInt32;
133 }
134 
135 template <>
136 inline const Dtype Dtype::FromType<uint64_t>() {
137  return Dtype::UInt64;
138 }
139 
140 template <>
141 inline const Dtype Dtype::FromType<bool>() {
142  return Dtype::Bool;
143 }
144 
145 } // namespace core
146 } // namespace open3d
#define LogError(...)
Definition: Logging.h:48
#define OPEN3D_API
Definition: Macro.h:31
Definition: Dtype.h:20
static const Dtype UInt8
Definition: Dtype.h:29
static const Dtype Bool
Definition: Dtype.h:33
static const Dtype Int16
Definition: Dtype.h:26
DtypeCode GetDtypeCode() const
Definition: Dtype.h:60
static const Dtype UInt32
Definition: Dtype.h:31
static const Dtype Float64
Definition: Dtype.h:24
Dtype()
Definition: Dtype.h:45
static const Dtype Int32
Definition: Dtype.h:27
static const Dtype UInt16
Definition: Dtype.h:30
std::string ToString() const
Definition: Dtype.h:64
static const Dtype UInt64
Definition: Dtype.h:32
static const Dtype Float32
Definition: Dtype.h:23
bool IsObject() const
Definition: Dtype.h:62
static const Dtype Undefined
Definition: Dtype.h:22
int64_t ByteSize() const
Definition: Dtype.h:58
static const Dtype Int8
Definition: Dtype.h:25
static const Dtype FromType()
Definition: Dtype.h:54
DtypeCode
Definition: Dtype.h:36
static const Dtype Int64
Definition: Dtype.h:28
std::string name
Definition: FilePCD.cpp:39
const Dtype UInt32
Definition: Dtype.cpp:50
const Dtype Int64
Definition: Dtype.cpp:47
const Dtype UInt16
Definition: Dtype.cpp:49
const Dtype Bool
Definition: Dtype.cpp:52
const Dtype Int32
Definition: Dtype.cpp:46
const Dtype Int16
Definition: Dtype.cpp:45
const Dtype Undefined
Definition: Dtype.cpp:41
const Dtype UInt8
Definition: Dtype.cpp:48
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype UInt64
Definition: Dtype.cpp:51
const Dtype Int8
Definition: Dtype.cpp:44
const Dtype Float32
Definition: Dtype.cpp:42
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:157
constexpr bool operator!=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:631
Definition: PinholeCameraIntrinsic.cpp:16