Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 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"
33 #include "open3d/core/Dispatch.h"
34 #include "open3d/utility/Console.h"
35 
36 namespace open3d {
37 namespace core {
38 
40 public:
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 
54 public:
55  enum class DtypeCode {
56  Undefined,
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  Dtype(DtypeCode dtype_code, int64_t byte_size, const std::string &name);
67 
70  template <typename T>
71  static inline const Dtype FromType() {
72  utility::LogError("Unsupported data for Dtype::FromType.");
73  }
74 
75  int64_t ByteSize() const { return byte_size_; }
76 
77  DtypeCode GetDtypeCode() const { return dtype_code_; }
78 
79  bool IsObject() const { return dtype_code_ == DtypeCode::Object; }
80 
81  std::string ToString() const { return name_; }
82 
83  bool operator==(const Dtype &other) const;
84 
85  bool operator!=(const Dtype &other) const;
86 
87 private:
88  static constexpr size_t max_name_len_ = 16;
89  DtypeCode dtype_code_;
90  int64_t byte_size_;
91  char name_[max_name_len_]; // MSVC warns if std::string is exported to DLL.
92 };
93 
94 template <>
95 inline const Dtype Dtype::FromType<float>() {
96  return Dtype::Float32;
97 }
98 
99 template <>
100 inline const Dtype Dtype::FromType<double>() {
101  return Dtype::Float64;
102 }
103 
104 template <>
105 inline const Dtype Dtype::FromType<int8_t>() {
106  return Dtype::Int8;
107 }
108 
109 template <>
110 inline const Dtype Dtype::FromType<int16_t>() {
111  return Dtype::Int16;
112 }
113 
114 template <>
115 inline const Dtype Dtype::FromType<int32_t>() {
116  return Dtype::Int32;
117 }
118 
119 template <>
120 inline const Dtype Dtype::FromType<int64_t>() {
121  return Dtype::Int64;
122 }
123 
124 template <>
125 inline const Dtype Dtype::FromType<uint8_t>() {
126  return Dtype::UInt8;
127 }
128 
129 template <>
130 inline const Dtype Dtype::FromType<uint16_t>() {
131  return Dtype::UInt16;
132 }
133 
134 template <>
135 inline const Dtype Dtype::FromType<uint32_t>() {
136  return Dtype::UInt32;
137 }
138 
139 template <>
140 inline const Dtype Dtype::FromType<uint64_t>() {
141  return Dtype::UInt64;
142 }
143 
144 template <>
145 inline const Dtype Dtype::FromType<bool>() {
146  return Dtype::Bool;
147 }
148 
149 } // namespace core
150 } // namespace open3d
Dtype()
Definition: Dtype.h:64
Definition: Dtype.h:39
static const Dtype FromType()
Definition: Dtype.h:71
DtypeCode
Definition: Dtype.h:55
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:151
static const Dtype Int8
Definition: Dtype.h:44
constexpr bool operator!=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:625
std::string ToString() const
Definition: Dtype.h:81
static const Dtype UInt8
Definition: Dtype.h:48
#define LogError(...)
Definition: Console.h:79
static const Dtype Int32
Definition: Dtype.h:46
static const Dtype UInt32
Definition: Dtype.h:50
static const Dtype Undefined
Definition: Dtype.h:41
static const Dtype UInt16
Definition: Dtype.h:49
static const Dtype Float32
Definition: Dtype.h:42
static const Dtype Int64
Definition: Dtype.h:47
#define OPEN3D_API
Definition: Macro.h:45
Definition: PinholeCameraIntrinsic.cpp:35
std::string name
Definition: FilePCD.cpp:58
int64_t ByteSize() const
Definition: Dtype.h:75
static const Dtype Int16
Definition: Dtype.h:45
static const Dtype Bool
Definition: Dtype.h:52
static const Dtype UInt64
Definition: Dtype.h:51
bool IsObject() const
Definition: Dtype.h:79
DtypeCode GetDtypeCode() const
Definition: Dtype.h:77
static const Dtype Float64
Definition: Dtype.h:43