Open3D (C++ API)  0.12.0
TensorKey.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 "open3d/utility/Console.h"
30 
31 namespace open3d {
32 namespace core {
33 
34 class Tensor; // Avoids circular include
35 
36 // TODO: refactor this to use utility::optional
37 class NoneType {};
38 
39 extern NoneType None;
40 
55 class TensorKey {
56 public:
57  enum class TensorKeyMode { Index, Slice, IndexTensor };
58 
61  static TensorKey Index(int64_t index);
62 
65  static TensorKey Slice(int64_t start, int64_t stop, int64_t step);
66  static TensorKey Slice(int64_t start, int64_t stop, NoneType step);
67  static TensorKey Slice(int64_t start, NoneType stop, int64_t step);
68  static TensorKey Slice(int64_t start, NoneType stop, NoneType step);
69  static TensorKey Slice(NoneType start, int64_t stop, int64_t step);
70  static TensorKey Slice(NoneType start, int64_t stop, NoneType step);
71  static TensorKey Slice(NoneType start, NoneType stop, int64_t step);
72  static TensorKey Slice(NoneType start, NoneType stop, NoneType step);
73 
76  static TensorKey IndexTensor(const Tensor& index_tensor);
77 
79  TensorKeyMode GetMode() const { return mode_; }
80 
81  int64_t GetIndex() const {
82  AssertMode(TensorKeyMode::Index);
83  return index_;
84  }
85 
86  int64_t GetStart() const {
87  AssertMode(TensorKeyMode::Slice);
88  return start_;
89  }
90 
91  int64_t GetStop() const {
92  AssertMode(TensorKeyMode::Slice);
93  return stop_;
94  }
95 
96  int64_t GetStep() const {
97  AssertMode(TensorKeyMode::Slice);
98  return step_;
99  }
100 
101  bool GetStartIsNone() const {
102  AssertMode(TensorKeyMode::Slice);
103  return start_is_none_;
104  }
105 
106  bool GetStopIsNone() const {
107  AssertMode(TensorKeyMode::Slice);
108  return stop_is_none_;
109  }
110 
111  bool GetStepIsNone() const {
112  AssertMode(TensorKeyMode::Slice);
113  return step_is_none_;
114  }
115 
116  std::shared_ptr<Tensor> GetIndexTensor() const;
117 
126  TensorKey UpdateWithDimSize(int64_t dim_size) const;
127 
129  static TensorKey Slice(int64_t start,
130  int64_t stop,
131  int64_t step,
132  bool start_is_none,
133  bool stop_is_none,
134  bool step_is_none);
135 
137  std::string ToString() const;
138 
139 protected:
143  int64_t index,
144  int64_t start,
145  int64_t stop,
146  int64_t step,
147  bool start_is_none,
148  bool stop_is_none,
149  bool step_is_none,
150  const Tensor& index_tensor);
151 
152  void AssertMode(TensorKeyMode mode) const {
153  if (mode != mode_) {
154  utility::LogError("Wrong TensorKeyMode.");
155  }
156  }
157 
160 
161 public:
163  int64_t index_ = 0;
164 
166  int64_t start_ = 0;
167  int64_t stop_ = 0;
168  int64_t step_ = 0;
169  bool start_is_none_ = false;
170  bool stop_is_none_ = false;
171  bool step_is_none_ = false;
172 
176  std::shared_ptr<Tensor> index_tensor_;
177 };
178 
179 } // namespace core
180 } // namespace open3d
void AssertMode(TensorKeyMode mode) const
Definition: TensorKey.h:152
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 timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode mode
Definition: K4aPlugin.cpp:680
NoneType None
Definition: TensorKey.cpp:37
Definition: TensorKey.h:55
bool GetStopIsNone() const
Definition: TensorKey.h:106
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
int64_t GetIndex() const
Definition: TensorKey.h:81
TensorKeyMode GetMode() const
Getters will check the TensorKeyMode.
Definition: TensorKey.h:79
int64_t GetStop() const
Definition: TensorKey.h:91
Definition: TensorKey.h:37
TensorKeyMode
Definition: TensorKey.h:57
bool GetStartIsNone() const
Definition: TensorKey.h:101
int64_t GetStep() const
Definition: TensorKey.h:96
Definition: PinholeCameraIntrinsic.cpp:35
std::shared_ptr< Tensor > index_tensor_
Definition: TensorKey.h:176
Definition: Tensor.h:48
bool GetStepIsNone() const
Definition: TensorKey.h:111
TensorKeyMode mode_
Depending on the mode, some properties may or may not be used.
Definition: TensorKey.h:159
int64_t GetStart() const
Definition: TensorKey.h:86