Open3D (C++ API)  0.12.0
SizeVector.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 <cstddef>
30 #include <numeric>
31 #include <string>
32 #include <vector>
33 
34 #include "open3d/utility/Console.h"
36 
37 namespace open3d {
38 namespace core {
39 
47 class DynamicSizeVector : public std::vector<utility::optional<int64_t>> {
49 
50 public:
51  DynamicSizeVector(const std::initializer_list<optint64_t>& dim_sizes)
52  : std::vector<optint64_t>(dim_sizes) {}
53 
54  DynamicSizeVector(const std::vector<optint64_t>& dim_sizes)
55  : std::vector<optint64_t>(dim_sizes) {}
56 
58  : std::vector<optint64_t>(other) {}
59 
60  explicit DynamicSizeVector(int64_t n, int64_t initial_value = 0)
61  : std::vector<optint64_t>(n, initial_value) {}
62 
63  template <class InputIterator>
64  DynamicSizeVector(InputIterator first, InputIterator last)
65  : std::vector<optint64_t>(first, last) {}
66 
68 
70  static_cast<std::vector<optint64_t>*>(this)->operator=(v);
71  return *this;
72  }
73 
75  static_cast<std::vector<optint64_t>*>(this)->operator=(v);
76  return *this;
77  }
78 
79  std::string ToString() const {
80  std::stringstream ss;
81  ss << "{";
82  bool first = true;
83  for (const optint64_t& element : *this) {
84  if (first) {
85  first = false;
86  } else {
87  ss << ", ";
88  }
89  if (element.has_value()) {
90  ss << fmt::format("{}", element.value());
91  } else {
92  ss << "None";
93  }
94  }
95  ss << "}";
96  return ss.str();
97  }
98 };
99 
102 class SizeVector : public std::vector<int64_t> {
103 public:
104  SizeVector(const std::initializer_list<int64_t>& dim_sizes)
105  : std::vector<int64_t>(dim_sizes) {}
106 
107  SizeVector(const std::vector<int64_t>& dim_sizes)
108  : std::vector<int64_t>(dim_sizes) {}
109 
110  SizeVector(const SizeVector& other) : std::vector<int64_t>(other) {}
111 
112  explicit SizeVector(int64_t n, int64_t initial_value = 0)
113  : std::vector<int64_t>(n, initial_value) {}
114 
115  template <class InputIterator>
116  SizeVector(InputIterator first, InputIterator last)
117  : std::vector<int64_t>(first, last) {}
118 
120 
122  static_cast<std::vector<int64_t>*>(this)->operator=(v);
123  return *this;
124  }
125 
127  static_cast<std::vector<int64_t>*>(this)->operator=(v);
128  return *this;
129  }
130 
131  int64_t NumElements() const {
132  if (this->size() == 0) {
133  return 1;
134  }
135  return std::accumulate(
136  this->begin(), this->end(), 1LL,
137  [this](const int64_t& lhs, const int64_t& rhs) -> int64_t {
138  if (lhs < 0 || rhs < 0) {
140  "Shape {} cannot contain negative dimensions.",
141  this->ToString());
142  }
143  return std::multiplies<int64_t>()(lhs, rhs);
144  });
145  }
146 
147  int64_t GetLength() const {
148  if (size() == 0) {
149  utility::LogError("Cannot get length of a 0-dimensional shape.");
150  } else {
151  return operator[](0);
152  }
153  }
154 
155  std::string ToString() const { return fmt::format("{}", *this); }
156 
158  const std::string msg = "") const {
159  if (!IsCompatible(dsv)) {
160  if (msg.empty()) {
161  utility::LogError("Shape {} is not compatible with {}.",
162  ToString(), dsv.ToString());
163  } else {
164  utility::LogError("Shape {} is not compatible with {}: {}",
165  ToString(), dsv.ToString(), msg);
166  }
167  }
168  }
169 
170  bool IsCompatible(const DynamicSizeVector& dsv) const {
171  if (size() != dsv.size()) {
172  return false;
173  }
174  for (size_t i = 0; i < size(); ++i) {
175  if (dsv[i].has_value() && dsv[i].value() != at(i)) {
176  return false;
177  }
178  }
179  return true;
180  }
181 };
182 
183 } // namespace core
184 } // namespace open3d
SizeVector()
Definition: SizeVector.h:119
DynamicSizeVector()
Definition: SizeVector.h:67
Definition: SizeVector.h:47
SizeVector & operator=(SizeVector &&v)
Definition: SizeVector.h:126
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
SizeVector(int64_t n, int64_t initial_value=0)
Definition: SizeVector.h:112
Definition: Optional.h:912
DynamicSizeVector(const std::vector< optint64_t > &dim_sizes)
Definition: SizeVector.h:54
int64_t GetLength() const
Definition: SizeVector.h:147
DynamicSizeVector & operator=(const DynamicSizeVector &v)
Definition: SizeVector.h:69
SizeVector(const SizeVector &other)
Definition: SizeVector.h:110
DynamicSizeVector(InputIterator first, InputIterator last)
Definition: SizeVector.h:64
Definition: SizeVector.h:102
int size
Definition: FilePCD.cpp:59
constexpr bool has_value() const noexcept
Definition: Optional.h:447
std::string ToString() const
Definition: SizeVector.h:155
DynamicSizeVector(const std::initializer_list< optint64_t > &dim_sizes)
Definition: SizeVector.h:51
bool IsCompatible(const DynamicSizeVector &dsv) const
Definition: SizeVector.h:170
TR2_OPTIONAL_HOST_CONSTEXPR T const & value() const &
Definition: Optional.h:472
DynamicSizeVector & operator=(DynamicSizeVector &&v)
Definition: SizeVector.h:74
Definition: Optional.h:54
int64_t NumElements() const
Definition: SizeVector.h:131
SizeVector(const std::vector< int64_t > &dim_sizes)
Definition: SizeVector.h:107
SizeVector(InputIterator first, InputIterator last)
Definition: SizeVector.h:116
Definition: PinholeCameraIntrinsic.cpp:35
DynamicSizeVector(int64_t n, int64_t initial_value=0)
Definition: SizeVector.h:60
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:191
DynamicSizeVector(const DynamicSizeVector &other)
Definition: SizeVector.h:57
SizeVector & operator=(const SizeVector &v)
Definition: SizeVector.h:121
SizeVector(const std::initializer_list< int64_t > &dim_sizes)
Definition: SizeVector.h:104
std::string ToString() const
Definition: SizeVector.h:79
void AssertCompatible(const DynamicSizeVector &dsv, const std::string msg="") const
Definition: SizeVector.h:157