Open3D (C++ API)
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 
33 class Tensor; // Avoids circular include
34 
35 class NoneType {};
36 
37 extern NoneType None;
38 
53 class TensorKey {
54 public:
55  enum class TensorKeyMode { Index, Slice, IndexTensor };
56 
59  static TensorKey Index(int64_t index);
60 
63  static TensorKey Slice(int64_t start, int64_t stop, int64_t step);
64  static TensorKey Slice(int64_t start, int64_t stop, NoneType step);
65  static TensorKey Slice(int64_t start, NoneType stop, int64_t step);
66  static TensorKey Slice(int64_t start, NoneType stop, NoneType step);
67  static TensorKey Slice(NoneType start, int64_t stop, int64_t step);
68  static TensorKey Slice(NoneType start, int64_t stop, NoneType step);
69  static TensorKey Slice(NoneType start, NoneType stop, int64_t step);
70  static TensorKey Slice(NoneType start, NoneType stop, NoneType step);
71 
74  static TensorKey IndexTensor(const Tensor& index_tensor);
75 
77  TensorKeyMode GetMode() const { return mode_; }
78 
79  int64_t GetIndex() const {
80  AssertMode(TensorKeyMode::Index);
81  return index_;
82  }
83 
84  int64_t GetStart() const {
85  AssertMode(TensorKeyMode::Slice);
86  return start_;
87  }
88 
89  int64_t GetStop() const {
90  AssertMode(TensorKeyMode::Slice);
91  return stop_;
92  }
93 
94  int64_t GetStep() const {
95  AssertMode(TensorKeyMode::Slice);
96  return step_;
97  }
98 
99  bool GetStartIsNone() const {
100  AssertMode(TensorKeyMode::Slice);
101  return start_is_none_;
102  }
103 
104  bool GetStopIsNone() const {
105  AssertMode(TensorKeyMode::Slice);
106  return stop_is_none_;
107  }
108 
109  bool GetStepIsNone() const {
110  AssertMode(TensorKeyMode::Slice);
111  return step_is_none_;
112  }
113 
114  std::shared_ptr<Tensor> GetIndexTensor() const;
115 
124  TensorKey UpdateWithDimSize(int64_t dim_size) const;
125 
126 protected:
128  static TensorKey Slice(int64_t start,
129  int64_t stop,
130  int64_t step,
131  bool start_is_none,
132  bool stop_is_none,
133  bool step_is_none);
134 
138  int64_t index,
139  int64_t start,
140  int64_t stop,
141  int64_t step,
142  bool start_is_none,
143  bool stop_is_none,
144  bool step_is_none,
145  const Tensor& index_tensor);
146 
147  void AssertMode(TensorKeyMode mode) const {
148  if (mode != mode_) {
149  utility::LogError("Wrong TensorKeyMode.");
150  }
151  }
152 
155 
156 public:
158  int64_t index_ = 0;
159 
161  int64_t start_ = 0;
162  int64_t stop_ = 0;
163  int64_t step_ = 0;
164  bool start_is_none_ = false;
165  bool stop_is_none_ = false;
166  bool step_is_none_ = false;
167 
171  std::shared_ptr<Tensor> index_tensor_;
172 };
173 
174 }; // namespace open3d
bool GetStepIsNone() const
Definition: TensorKey.h:109
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:676
int64_t GetIndex() const
Definition: TensorKey.h:79
std::shared_ptr< Tensor > index_tensor_
Definition: TensorKey.h:171
TensorKeyMode GetMode() const
Getters will check the TensorKeyMode.
Definition: TensorKey.h:77
TensorKeyMode mode_
Depending on the mode, some properties may or may not be used.
Definition: TensorKey.h:154
int64_t GetStart() const
Definition: TensorKey.h:84
void LogError(const char *format, const Args &... args)
Definition: Console.h:174
Definition: TensorKey.h:53
void AssertMode(TensorKeyMode mode) const
Definition: TensorKey.h:147
int64_t GetStep() const
Definition: TensorKey.h:94
bool GetStartIsNone() const
Definition: TensorKey.h:99
Definition: Open3DViewer.h:29
TensorKeyMode
Definition: TensorKey.h:55
bool GetStopIsNone() const
Definition: TensorKey.h:104
Definition: TensorKey.h:35
NoneType None
Definition: TensorKey.cpp:33
Definition: Tensor.h:46
int64_t GetStop() const
Definition: TensorKey.h:89