Open3D (C++ API)  0.11.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"
35 
36 namespace open3d {
37 namespace core {
38 
41 class SizeVector : public std::vector<int64_t> {
42 public:
43  SizeVector(const std::initializer_list<int64_t>& dim_sizes)
44  : std::vector<int64_t>(dim_sizes) {}
45 
46  SizeVector(const std::vector<int64_t>& dim_sizes)
47  : std::vector<int64_t>(dim_sizes) {}
48 
49  SizeVector(const SizeVector& other) : std::vector<int64_t>(other) {}
50 
51  explicit SizeVector(int64_t n, int64_t initial_value = 0)
52  : std::vector<int64_t>(n, initial_value) {}
53 
54  template <class InputIterator>
55  SizeVector(InputIterator first, InputIterator last)
56  : std::vector<int64_t>(first, last) {}
57 
59 
61  static_cast<std::vector<int64_t>*>(this)->operator=(v);
62  return *this;
63  }
64 
66  static_cast<std::vector<int64_t>*>(this)->operator=(v);
67  return *this;
68  }
69 
70  int64_t NumElements() const {
71  if (this->size() == 0) {
72  return 1;
73  }
74  return std::accumulate(
75  this->begin(), this->end(), 1LL,
76  [this](const int64_t& lhs, const int64_t& rhs) -> int64_t {
77  if (lhs < 0 || rhs < 0) {
79  "Shape {} cannot contain negative dimensions.",
80  this->ToString());
81  }
82  return std::multiplies<int64_t>()(lhs, rhs);
83  });
84  }
85 
86  std::string ToString() const { return fmt::format("{}", *this); }
87 };
88 
89 } // namespace core
90 } // namespace open3d
SizeVector()
Definition: SizeVector.h:58
SizeVector & operator=(SizeVector &&v)
Definition: SizeVector.h:65
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
SizeVector(int64_t n, int64_t initial_value=0)
Definition: SizeVector.h:51
Definition: Optional.h:922
SizeVector(const SizeVector &other)
Definition: SizeVector.h:49
Definition: SizeVector.h:41
int size
Definition: FilePCD.cpp:59
std::string ToString() const
Definition: SizeVector.h:86
int64_t NumElements() const
Definition: SizeVector.h:70
SizeVector(const std::vector< int64_t > &dim_sizes)
Definition: SizeVector.h:46
SizeVector(InputIterator first, InputIterator last)
Definition: SizeVector.h:55
Definition: PinholeCameraIntrinsic.cpp:35
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:191
SizeVector & operator=(const SizeVector &v)
Definition: SizeVector.h:60
SizeVector(const std::initializer_list< int64_t > &dim_sizes)
Definition: SizeVector.h:43