Open3D (C++ API)
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 "string"
30 
31 #include "Open3D/Core/Dispatch.h"
32 #include "Open3D/Utility/Console.h"
33 
34 static_assert(sizeof(float) == 4,
35  "Unsupported platform: float must be 4 bytes");
36 static_assert(sizeof(double) == 8,
37  "Unsupported platform: double must be 8 bytes");
38 static_assert(sizeof(int) == 4, "Unsupported platform: int must be 4 bytes");
39 static_assert(sizeof(int32_t) == 4,
40  "Unsupported platform: int32_t must be 4 bytes");
41 static_assert(sizeof(int64_t) == 8,
42  "Unsupported platform: int64_t must be 8 bytes");
43 static_assert(sizeof(uint8_t) == 1,
44  "Unsupported platform: uint8_t must be 1 byte");
45 static_assert(sizeof(bool) == 1, "Unsupported platform: bool must be 1 byte");
46 
47 namespace open3d {
48 
49 enum class Dtype {
50  Undefined, // Dtype for uninitialized Tensor
51  Float32,
52  Float64,
53  Int32,
54  Int64,
55  UInt8,
56  Bool,
57 };
58 
59 class DtypeUtil {
60 public:
61  static int64_t ByteSize(const Dtype &dtype) {
62  int64_t byte_size = 0;
63  switch (dtype) {
64  case Dtype::Float32:
65  byte_size = 4;
66  break;
67  case Dtype::Float64:
68  byte_size = 8;
69  break;
70  case Dtype::Int32:
71  byte_size = 4;
72  break;
73  case Dtype::Int64:
74  byte_size = 8;
75  break;
76  case Dtype::UInt8:
77  byte_size = 1;
78  break;
79  case Dtype::Bool:
80  byte_size = 1;
81  break;
82  default:
83  utility::LogError("Unsupported data type");
84  }
85  return byte_size;
86  }
87 
91  template <typename T>
92  static inline Dtype FromType() {
93  utility::LogError("Unsupported data type");
94  return Dtype::Undefined;
95  }
96 
97  static std::string ToString(const Dtype &dtype) {
98  std::string str = "";
99  switch (dtype) {
100  case Dtype::Undefined:
101  str = "Undefined";
102  break;
103  case Dtype::Float32:
104  str = "Float32";
105  break;
106  case Dtype::Float64:
107  str = "Float64";
108  break;
109  case Dtype::Int32:
110  str = "Int32";
111  break;
112  case Dtype::Int64:
113  str = "Int64";
114  break;
115  case Dtype::UInt8:
116  str = "UInt8";
117  break;
118  case Dtype::Bool:
119  str = "Bool";
120  break;
121  default:
122  utility::LogError("Unsupported data type");
123  }
124  return str;
125  }
126 };
127 
128 template <>
129 inline Dtype DtypeUtil::FromType<float>() {
130  return Dtype::Float32;
131 }
132 
133 template <>
134 inline Dtype DtypeUtil::FromType<double>() {
135  return Dtype::Float64;
136 }
137 
138 template <>
139 inline Dtype DtypeUtil::FromType<int32_t>() {
140  return Dtype::Int32;
141 }
142 
143 template <>
144 inline Dtype DtypeUtil::FromType<int64_t>() {
145  return Dtype::Int64;
146 }
147 
148 template <>
149 inline Dtype DtypeUtil::FromType<uint8_t>() {
150  return Dtype::UInt8;
151 }
152 
153 template <>
154 inline Dtype DtypeUtil::FromType<bool>() {
155  return Dtype::Bool;
156 }
157 
158 } // namespace open3d
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t int32_t
Definition: K4aPlugin.cpp:394
static int64_t ByteSize(const Dtype &dtype)
Definition: Dtype.h:61
Definition: Dtype.h:59
Definition: Open3DViewer.h:29
static std::string ToString(const Dtype &dtype)
Definition: Dtype.h:97
static Dtype FromType()
Definition: Dtype.h:92
Dtype
Definition: Dtype.h:49