Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
NumpyIO.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/core/Blob.h"
30 #include "open3d/core/Dtype.h"
31 #include "open3d/core/SizeVector.h"
32 #include "open3d/core/Tensor.h"
33 
34 namespace open3d {
35 namespace core {
36 
37 class NumpyArray {
38 public:
39  NumpyArray() = delete;
40 
41  NumpyArray(const Tensor& t);
42 
43  NumpyArray(const SizeVector& shape,
44  char type,
45  int64_t word_size,
46  bool fortran_order);
47 
48  template <typename T>
49  T* GetDataPtr() {
50  return reinterpret_cast<T*>(blob_->GetDataPtr());
51  }
52 
53  template <typename T>
54  const T* GetDataPtr() const {
55  return reinterpret_cast<const T*>(blob_->GetDataPtr());
56  }
57 
58  Dtype GetDtype() const;
59 
60  SizeVector GetShape() const { return shape_; }
61 
62  bool IsFortranOrder() const { return fortran_order_; }
63 
64  int64_t NumBytes() const { return num_elements_ * word_size_; }
65 
66  Tensor ToTensor() const;
67 
68  static NumpyArray Load(const std::string& file_name);
69 
70  void Save(std::string file_name) const;
71 
72 private:
73  std::shared_ptr<Blob> blob_ = nullptr;
74  SizeVector shape_;
75  char type_;
76  int64_t word_size_;
77  bool fortran_order_;
78  int64_t num_elements_;
79 };
80 
81 } // namespace core
82 } // namespace open3d
Definition: Dtype.h:39
int64_t NumBytes() const
Definition: NumpyIO.h:64
static NumpyArray Load(const std::string &file_name)
Definition: NumpyIO.cpp:287
Definition: SizeVector.h:102
Dtype GetDtype() const
Definition: NumpyIO.cpp:255
T * GetDataPtr()
Definition: NumpyIO.h:49
char type
Definition: FilePCD.cpp:60
Definition: NumpyIO.h:37
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:50
bool IsFortranOrder() const
Definition: NumpyIO.h:62
SizeVector GetShape() const
Definition: NumpyIO.h:60
const T * GetDataPtr() const
Definition: NumpyIO.h:54
Tensor ToTensor() const
Definition: NumpyIO.cpp:271
void Save(std::string file_name) const
Definition: NumpyIO.cpp:307