NDArray.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // Header
25 #include "NDArray.h"
26 
27 // Simox
28 #include <SimoxUtility/algorithm/string.h>
29 
30 // ArmarX
33 
34 namespace armarx::aron::data
35 {
36  // constructors
37  NDArray::NDArray(const Path& path) :
38  detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
39  data::Descriptor::NDARRAY,
40  path)
41  {
42  }
43 
44  NDArray::NDArray(const data::dto::NDArrayPtr& o, const Path& path) :
45  detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
46  o,
47  data::Descriptor::NDARRAY,
48  path)
49  {
50  }
51 
52  NDArray::NDArray(const std::vector<int>& dim,
53  const std::string& t,
54  const std::vector<unsigned char>& data,
55  const Path& path) :
56  NDArray(data::dto::NDArrayPtr(new data::dto::NDArray(aron::Version(), dim, t, data)), path)
57  {
58  }
59 
60  // operators
61  bool
62  NDArray::operator==(const NDArray& other) const
63  {
64  const auto otherAron = other.toNDArrayDTO();
65  if (aron->shape != otherAron->shape)
66  {
67  return false;
68  }
69  if (aron->type != otherAron->type)
70  {
71  return false;
72  }
73  // performs memcmp
74  if (aron->data != otherAron->data)
75  {
76  return false;
77  }
78  return true;
79  }
80 
81  bool
82  NDArray::operator==(const NDArrayPtr& other) const
83  {
84  if (!other)
85  {
86  return false;
87  }
88  return *this == *other;
89  }
90 
92  NDArray::clone(const Path& p) const
93  {
95  return ret;
96  }
97 
98  // static methods
101  {
102  if (!aron)
103  {
104  return nullptr;
105  }
106  return std::make_shared<NDArray>(aron);
107  }
108 
111  {
112  return variant ? variant->toNDArrayDTO() : nullptr;
113  }
114 
115  // public member functions
116  DictPtr
118  {
119  auto dict = std::make_shared<Dict>();
120  auto copy_this = FromAronDTO(toAronDTO(), getPath());
121  dict->addElement("data", copy_this);
122  return dict;
123  }
124 
125  unsigned char*
127  {
128  return aron->data.data();
129  }
130 
131  void
132  NDArray::setData(unsigned int elements, const unsigned char* src)
133  {
134  aron->data = std::vector<unsigned char>(elements);
135  std::memcpy(aron->data.data(), src, elements);
136  }
137 
138  std::vector<unsigned char>
140  {
141  return aron->data;
142  }
143 
144  std::vector<int>
146  {
147  return aron->shape;
148  }
149 
150  void
151  NDArray::setShape(const std::vector<int>& d)
152  {
153  aron->shape = d;
154  }
155 
156  void
158  {
159  aron->shape.push_back(i);
160  }
161 
162  std::string
164  {
165  return aron->type;
166  }
167 
168  void
169  NDArray::setType(const std::string& t)
170  {
171  if (t.empty())
172  {
173  ARMARX_TRACE;
174  throw error::AronException(__PRETTY_FUNCTION__, "The type cannot be empty", getPath());
175  }
176 
177  aron->type = t;
178  }
179 
182  {
183  return aron;
184  }
185 
186  // virtual implementations
187  std::string
189  {
190  return "NDArray";
191  }
192 
193  std::string
195  {
196  return "armarx::aron::data::NDArray<" + simox::alg::to_string(aron->shape, ", ") + ", " +
197  aron->type + ">";
198  }
199 
202  {
203  ARMARX_TRACE;
204  throw error::NotImplementedYetException(__PRETTY_FUNCTION__);
205  }
206 
207  bool
209  {
210  if (!type)
211  return true;
212 
213  type::Descriptor typeDesc = type->getDescriptor();
214  switch (typeDesc)
215  {
217  {
218  auto casted = type::Matrix::DynamicCastAndCheck(type);
219  ARMARX_TRACE;
220  return (aron->shape.size() == 3 && aron->shape[0] == casted->getRows() &&
221  aron->shape[1] == casted->getCols());
222  }
224  {
225  auto casted = type::Quaternion::DynamicCastAndCheck(type);
226  ARMARX_TRACE;
227  return (aron->shape.size() == 3 && aron->shape[0] == 1 && aron->shape[1] == 4);
228  }
230  {
231  auto casted = type::PointCloud::DynamicCastAndCheck(type);
232  ARMARX_TRACE;
233  return (aron->shape.size() == 3);
234  }
236  {
237  auto casted = type::Image::DynamicCastAndCheck(type);
238  ARMARX_TRACE;
239  return (aron->shape.size() == 3);
240  }
242  {
243  auto casted = type::NDArray::DynamicCastAndCheck(type);
244  ARMARX_TRACE;
245  return (aron->shape.size() == (unsigned int)casted->getNumberDimensions());
246  }
247  default:
248  ARMARX_TRACE;
249  return false;
250  }
251  }
252 
253  std::string
254  NDArray::DimensionsToString(const std::vector<int>& dimensions)
255  {
256  std::stringstream ss;
257  ss << "(" << simox::alg::join(simox::alg::multi_to_string(dimensions), ", ") << ")";
258  return ss.str();
259  }
260 
261 } // namespace armarx::aron::data
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::data::NDArray::getDataAsVector
std::vector< unsigned char > getDataAsVector() const
Definition: NDArray.cpp:139
armarx::aron::data::NDArray::getFullName
virtual std::string getFullName() const override
get the full str representation of this variant
Definition: NDArray.cpp:194
armarx::aron::data::NDArray::setType
void setType(const std::string &)
Definition: NDArray.cpp:169
armarx::aron::type::Descriptor::IMAGE
@ IMAGE
armarx::aron::data::Variant::FromAronDTO
static VariantPtr FromAronDTO(const data::dto::GenericDataPtr &, const Path &=Path())
create a variant from a dto object
Definition: Variant.cpp:39
armarx::aron::data::NDArray
Definition: NDArray.h:48
armarx::aron::data::Variant::getPath
Path getPath() const
get the path
Definition: Variant.h:110
armarx::aron::data::NDArray::getType
std::string getType() const
Definition: NDArray.cpp:163
armarx::aron::data::NDArray::getShortName
virtual std::string getShortName() const override
get a short str representation of this variant
Definition: NDArray.cpp:188
armarx::aron::data::NDArray::recalculateType
virtual type::VariantPtr recalculateType() const override
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
Definition: NDArray.cpp:201
detail
Definition: OpenCVUtil.cpp:127
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::NDArray::ToNDArrayDTO
static data::dto::NDArrayPtr ToNDArrayDTO(const PointerType &navigator)
Definition: NDArray.cpp:110
armarx::aron::data::NDArray::getShape
std::vector< int > getShape() const
Definition: NDArray.cpp:145
armarx::aron::data::NDArray::DimensionsToString
static std::string DimensionsToString(const std::vector< int > &dimensions)
Return dimensions in a readable string such as "(2, 3, 4)".
Definition: NDArray.cpp:254
armarx::aron::error::NotImplementedYetException
The NotImplementedYetException class.
Definition: Exception.h:88
armarx::aron::type::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::detail::SpecializedVariantBase< data::dto::NDArray, NDArray >::clone
virtual PointerType clone() const
Definition: SpecializedVariant.h:82
armarx::aron::data::NDArray::setShape
void setShape(const std::vector< int > &)
Definition: NDArray.cpp:151
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::NDArray::operator==
virtual bool operator==(const NDArray &) const override
Definition: NDArray.cpp:62
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::type::Descriptor::MATRIX
@ MATRIX
armarx::aron::data::NDArray::addToShape
void addToShape(int)
Definition: NDArray.cpp:157
armarx::aron::type::Descriptor::QUATERNION
@ QUATERNION
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::data
A convenience header to include all aron files (full include, not forward declared)
Definition: aron_conversions.cpp:3
armarx::aron::data::detail::SpecializedVariantBase< data::dto::NDArray, NDArray >::toAronDTO
data::dto::GenericDataPtr toAronDTO() const override
Definition: SpecializedVariant.h:103
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::aron::data::NDArray::FromNDArrayDTO
static PointerType FromNDArrayDTO(const data::dto::NDArrayPtr &aron)
Definition: NDArray.cpp:100
All.h
armarx::aron::data::NDArray::getAsDict
DictPtr getAsDict() const
Definition: NDArray.cpp:117
armarx::aron::type::detail::SpecializedVariantBase< type::dto::Matrix, Matrix >::DynamicCastAndCheck
static std::shared_ptr< Matrix > DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:124
armarx::aron::type::Descriptor::POINTCLOUD
@ POINTCLOUD
armarx::aron::data::NDArray::toNDArrayDTO
data::dto::NDArrayPtr toNDArrayDTO() const
Definition: NDArray.cpp:181
armarx::aron::data::NDArray::setData
void setData(unsigned int, const unsigned char *)
Definition: NDArray.cpp:132
armarx::aron::data::NDArray::NDArray
NDArray(const Path &path=Path())
Definition: NDArray.cpp:37
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::data::NDArray::getData
unsigned char * getData() const
Definition: NDArray.cpp:126
Factory.h
armarx::aron::data::detail::SpecializedVariantBase< data::dto::NDArray, NDArray >::aron
AronDataType::PointerType aron
Definition: SpecializedVariant.h:154
armarx::aron::data::NDArray::fullfillsType
virtual bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition: NDArray.cpp:208
NDArray.h