ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
TensorKey.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Logging.h>
11 
12 #include <memory>
13 #include <sstream>
14 
16 
17 namespace cloudViewer {
18 namespace core {
19 
21 public:
22  Impl(TensorKeyMode mode) : mode_(mode) {}
23  TensorKeyMode GetMode() const { return mode_; }
24  virtual ~Impl() {}
25  virtual std::string ToString() const = 0;
26 
27 private:
28  TensorKeyMode mode_;
29 };
30 
32 public:
33  IndexImpl(int64_t index)
34  : TensorKey::Impl(TensorKeyMode::Index), index_(index) {}
35  int64_t GetIndex() const { return index_; }
36  std::string ToString() const override {
37  std::stringstream ss;
38  ss << "TensorKey::Index(" << index_ << ")";
39  return ss.str();
40  }
41 
42 private:
43  int64_t index_;
44 };
45 
47 public:
52  start_(start),
53  stop_(stop),
54  step_(step) {}
55  std::shared_ptr<SliceImpl> InstantiateDimSize(int64_t dim_size) const {
56  return std::make_shared<SliceImpl>(
57  start_.has_value() ? start_.value() : 0,
58  stop_.has_value() ? stop_.value() : dim_size,
59  step_.has_value() ? step_.value() : 1);
60  }
61  int64_t GetStart() const {
62  if (start_.has_value()) {
63  return start_.value();
64  } else {
65  utility::LogError("TensorKeyMode::Slice: start is None.");
66  }
67  }
68  int64_t GetStop() const {
69  if (stop_.has_value()) {
70  return stop_.value();
71  } else {
72  utility::LogError("TensorKeyMode::Slice: stop is None.");
73  }
74  }
75  int64_t GetStep() const {
76  if (step_.has_value()) {
77  return step_.value();
78  } else {
79  utility::LogError("TensorKeyMode::Slice: step is None.");
80  }
81  }
82  std::string ToString() const override {
83  std::stringstream ss;
84  ss << "TensorKey::Slice(";
85  if (start_.has_value()) {
86  ss << start_.value();
87  } else {
88  ss << "None";
89  }
90  ss << ", ";
91  if (stop_.has_value()) {
92  ss << stop_.value();
93  } else {
94  ss << "None";
95  }
96  ss << ", ";
97  if (step_.has_value()) {
98  ss << step_.value();
99  } else {
100  ss << "None";
101  }
102  ss << ")";
103  return ss.str();
104  }
105 
106 private:
110 };
111 
113 public:
114  IndexTensorImpl(const Tensor& index_tensor)
116  index_tensor_(index_tensor) {}
117  Tensor GetIndexTensor() const { return index_tensor_; }
118  std::string ToString() const override {
119  std::stringstream ss;
120  ss << "TensorKey::IndexTensor(" << index_tensor_.ToString() << ")";
121  return ss.str();
122  }
123 
124 private:
125  Tensor index_tensor_;
126 };
127 
128 TensorKey::TensorKey(const std::shared_ptr<Impl>& impl) : impl_(impl) {}
129 
130 TensorKey::TensorKeyMode TensorKey::GetMode() const { return impl_->GetMode(); }
131 
132 std::string TensorKey::ToString() const { return impl_->ToString(); }
133 
134 TensorKey TensorKey::Index(int64_t index) {
135  return TensorKey(std::make_shared<IndexImpl>(index));
136 }
137 
141  return TensorKey(std::make_shared<SliceImpl>(start, stop, step));
142 }
143 
145  return TensorKey(std::make_shared<IndexTensorImpl>(index_tensor));
146 }
147 
148 int64_t TensorKey::GetIndex() const {
149  if (auto index_impl = std::dynamic_pointer_cast<IndexImpl>(impl_)) {
150  return index_impl->GetIndex();
151  } else {
152  utility::LogError("GetIndex() failed: the impl is not IndexImpl.");
153  }
154 }
155 
156 int64_t TensorKey::GetStart() const {
157  if (auto slice_impl = std::dynamic_pointer_cast<SliceImpl>(impl_)) {
158  return slice_impl->GetStart();
159  } else {
160  utility::LogError("GetStart() failed: the impl is not SliceImpl.");
161  }
162 }
163 int64_t TensorKey::GetStop() const {
164  if (auto slice_impl = std::dynamic_pointer_cast<SliceImpl>(impl_)) {
165  return slice_impl->GetStop();
166  } else {
167  utility::LogError("GetStop() failed: the impl is not SliceImpl.");
168  }
169 }
170 int64_t TensorKey::GetStep() const {
171  if (auto slice_impl = std::dynamic_pointer_cast<SliceImpl>(impl_)) {
172  return slice_impl->GetStep();
173  } else {
174  utility::LogError("GetStep() failed: the impl is not SliceImpl.");
175  }
176 }
177 TensorKey TensorKey::InstantiateDimSize(int64_t dim_size) const {
178  if (auto slice_impl = std::dynamic_pointer_cast<SliceImpl>(impl_)) {
179  return TensorKey(slice_impl->InstantiateDimSize(dim_size));
180  } else {
182  "InstantiateDimSize() failed: the impl is not SliceImpl.");
183  }
184 }
185 
187  if (auto index_tensor_impl =
188  std::dynamic_pointer_cast<IndexTensorImpl>(impl_)) {
189  return index_tensor_impl->GetIndexTensor();
190  } else {
192  "GetIndexTensor() failed: the impl is not IndexTensorImpl.");
193  }
194 }
195 
196 } // namespace core
197 } // namespace cloudViewer
virtual std::string ToString() const =0
TensorKeyMode GetMode() const
Definition: TensorKey.cpp:23
std::string ToString() const override
Definition: TensorKey.cpp:36
IndexTensorImpl(const Tensor &index_tensor)
Definition: TensorKey.cpp:114
std::string ToString() const override
Definition: TensorKey.cpp:118
std::shared_ptr< SliceImpl > InstantiateDimSize(int64_t dim_size) const
Definition: TensorKey.cpp:55
SliceImpl(utility::optional< int64_t > start, utility::optional< int64_t > stop, utility::optional< int64_t > step)
Definition: TensorKey.cpp:48
std::string ToString() const override
Definition: TensorKey.cpp:82
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:26
std::string ToString() const
Convert TensorKey to a string representation.
Definition: TensorKey.cpp:132
TensorKeyMode GetMode() const
Returns TensorKey mode.
Definition: TensorKey.cpp:130
TensorKey InstantiateDimSize(int64_t dim_size) const
Definition: TensorKey.cpp:177
static TensorKey IndexTensor(const Tensor &index_tensor)
Definition: TensorKey.cpp:144
Tensor GetIndexTensor() const
Definition: TensorKey.cpp:186
static TensorKey Index(int64_t index)
Definition: TensorKey.cpp:134
static TensorKey Slice(utility::optional< int64_t > start, utility::optional< int64_t > stop, utility::optional< int64_t > step)
Definition: TensorKey.cpp:138
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:780
constexpr bool has_value() const noexcept
Definition: Optional.h:440
constexpr T const & value() const &
Definition: Optional.h:465
#define LogError(...)
Definition: Logging.h:60
constexpr nullopt_t nullopt
Definition: Optional.h:136
Generic file read and write utility for python interface.