ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SizeVector.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 #include <Optional.h>
12 
13 #include <algorithm>
14 #include <cstddef>
15 #include <numeric>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 
20 namespace cloudViewer {
21 namespace core {
22 
24  const std::initializer_list<utility::optional<int64_t>>& dim_sizes)
25  : super_t(dim_sizes) {}
26 
28  const std::vector<utility::optional<int64_t>>& dim_sizes)
29  : super_t(dim_sizes.begin(), dim_sizes.end()) {}
30 
32  : super_t(other) {}
33 
34 DynamicSizeVector::DynamicSizeVector(int64_t n, int64_t initial_value)
35  : super_t(n, initial_value) {}
36 
38  : DynamicSizeVector(dim_sizes.begin(), dim_sizes.end()) {}
39 
41  SizeVector sv(size());
42  std::transform(begin(), end(), sv.begin(), [](const auto& v) {
43  if (!v.has_value()) {
44  utility::LogError("Cannot convert dynamic shape to SizeVector.");
45  }
46  return v.value();
47  });
48  return sv;
49 }
50 
51 DynamicSizeVector& DynamicSizeVector::operator=(const DynamicSizeVector& v) {
52  static_cast<super_t*>(this)->operator=(v);
53  return *this;
54 }
55 
56 DynamicSizeVector& DynamicSizeVector::operator=(DynamicSizeVector&& v) {
57  static_cast<super_t*>(this)->operator=(v);
58  return *this;
59 }
60 
61 std::string DynamicSizeVector::ToString() const {
62  std::stringstream ss;
63  ss << "{";
64  bool first = true;
65  for (const utility::optional<int64_t>& element : *this) {
66  if (first) {
67  first = false;
68  } else {
69  ss << ", ";
70  }
71  if (element.has_value()) {
72  ss << fmt::format("{}", element.value());
73  } else {
74  ss << "None";
75  }
76  }
77  ss << "}";
78  return ss.str();
79 }
80 
81 bool DynamicSizeVector::IsDynamic() const {
82  return std::any_of(
83  this->begin(), this->end(),
84  [](const utility::optional<int64_t>& v) { return !v.has_value(); });
85 }
86 
87 SizeVector::SizeVector(const std::initializer_list<int64_t>& dim_sizes)
88  : super_t(dim_sizes) {}
89 
90 SizeVector::SizeVector(const std::vector<int64_t>& dim_sizes)
91  : super_t(dim_sizes.begin(), dim_sizes.end()) {}
92 
93 SizeVector::SizeVector(const SizeVector& other) : super_t(other) {}
94 
95 SizeVector::SizeVector(int64_t n, int64_t initial_value)
96  : super_t(n, initial_value) {}
97 
99  static_cast<super_t*>(this)->operator=(v);
100  return *this;
101 }
102 
104  static_cast<super_t*>(this)->operator=(v);
105  return *this;
106 }
107 
108 int64_t SizeVector::NumElements() const {
109  if (this->size() == 0) {
110  return 1;
111  }
112  return std::accumulate(
113  this->begin(), this->end(), 1LL,
114  [this](const int64_t& lhs, const int64_t& rhs) -> int64_t {
115  if (lhs < 0 || rhs < 0) {
117  "Shape {} cannot contain negative dimensions.",
118  this->ToString());
119  }
120  return std::multiplies<int64_t>()(lhs, rhs);
121  });
122 }
123 
124 int64_t SizeVector::GetLength() const {
125  if (size() == 0) {
126  utility::LogError("Cannot get length of a 0-dimensional shape.");
127  } else {
128  return operator[](0);
129  }
130 }
131 
132 std::string SizeVector::ToString() const {
133  return fmt::format("{{{}}}", fmt::join(*this, ", "));
134 }
135 
137  const std::string msg) const {
138  if (!IsCompatible(dsv)) {
139  if (msg.empty()) {
140  utility::LogError("Shape {} is not compatible with {}.", ToString(),
141  dsv.ToString());
142  } else {
143  utility::LogError("Shape {} is not compatible with {}: {}",
144  ToString(), dsv.ToString(), msg);
145  }
146  }
147 }
148 
150  if (size() != dsv.size()) {
151  return false;
152  }
153  for (size_t i = 0; i < size(); ++i) {
154  if (dsv[i].has_value() && dsv[i].value() != this->operator[](i)) {
155  return false;
156  }
157  }
158  return true;
159 }
160 
161 } // namespace core
162 } // namespace cloudViewer
filament::Texture::InternalFormat format
bool IsCompatible(const DynamicSizeVector &dsv) const
Definition: SizeVector.cpp:149
void AssertCompatible(const DynamicSizeVector &dsv, const std::string msg="") const
Definition: SizeVector.cpp:136
std::string ToString() const
Definition: SizeVector.cpp:132
SizeVector & operator=(const SizeVector &v)
Definition: SizeVector.cpp:98
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
Generic file read and write utility for python interface.