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