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 
31 
32 // ArmarX
35 
36 namespace armarx::aron::data
37 {
38  // constructors
39  NDArray::NDArray(const Path& path) :
40  detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
41  data::Descriptor::NDARRAY,
42  path)
43  {
44  }
45 
46  NDArray::NDArray(const data::dto::NDArrayPtr& o, const Path& path) :
47  detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
48  o,
49  data::Descriptor::NDARRAY,
50  path)
51  {
52  }
53 
54  NDArray::NDArray(const std::vector<int>& dim,
55  const std::string& t,
56  const std::vector<unsigned char>& data,
57  const Path& path) :
58  NDArray(data::dto::NDArrayPtr(new data::dto::NDArray(aron::Version(), dim, t, data)), path)
59  {
60  }
61 
62  // operators
63  bool
64  NDArray::operator==(const NDArray& other) const
65  {
66  const auto otherAron = other.toNDArrayDTO();
67  if (aron->shape != otherAron->shape)
68  {
69  return false;
70  }
71  if (aron->type != otherAron->type)
72  {
73  return false;
74  }
75  // performs memcmp
76  if (aron->data != otherAron->data)
77  {
78  return false;
79  }
80  return true;
81  }
82 
83  bool
84  NDArray::operator==(const NDArrayPtr& other) const
85  {
86  if (!other)
87  {
88  return false;
89  }
90  return *this == *other;
91  }
92 
94  NDArray::clone(const Path& p) const
95  {
97  return ret;
98  }
99 
100  // static methods
101  NDArrayPtr
103  {
104  if (!aron)
105  {
106  return nullptr;
107  }
108  return std::make_shared<NDArray>(aron);
109  }
110 
113  {
114  return variant ? variant->toNDArrayDTO() : nullptr;
115  }
116 
117  // public member functions
118  DictPtr
120  {
121  auto dict = std::make_shared<Dict>();
122  auto copy_this = FromAronDTO(toAronDTO(), getPath());
123  dict->addElement("data", copy_this);
124  return dict;
125  }
126 
127  unsigned char*
129  {
130  return aron->data.data();
131  }
132 
133  void
134  NDArray::setData(unsigned int elements, const unsigned char* src)
135  {
136  aron->data = std::vector<unsigned char>(elements);
137  std::memcpy(aron->data.data(), src, elements);
138  }
139 
140  std::vector<unsigned char>
142  {
143  return aron->data;
144  }
145 
146  std::vector<int>
148  {
149  return aron->shape;
150  }
151 
152  void
153  NDArray::setShape(const std::vector<int>& d)
154  {
155  aron->shape = d;
156  }
157 
158  void
160  {
161  aron->shape.push_back(i);
162  }
163 
164  std::string
166  {
167  return aron->type;
168  }
169 
170  void
171  NDArray::setType(const std::string& t)
172  {
173  if (t.empty())
174  {
175  ARMARX_TRACE;
176  throw error::AronException(__PRETTY_FUNCTION__, "The type cannot be empty", getPath());
177  }
178 
179  aron->type = t;
180  }
181 
184  {
185  return aron;
186  }
187 
188  // virtual implementations
189  std::string
191  {
192  return "NDArray";
193  }
194 
195  std::string
197  {
198  return "armarx::aron::data::NDArray<" + simox::alg::to_string(aron->shape, ", ") + ", " +
199  aron->type + ">";
200  }
201 
204  {
205  ARMARX_TRACE;
206  throw error::NotImplementedYetException(__PRETTY_FUNCTION__);
207  }
208 
209  bool
211  {
212  if (!type)
213  return true;
214 
215  type::Descriptor typeDesc = type->getDescriptor();
216  switch (typeDesc)
217  {
219  {
220  auto casted = type::Matrix::DynamicCastAndCheck(type);
221  ARMARX_TRACE;
222  return (aron->shape.size() == 3 &&
223  (aron->shape[0] == casted->getRows() || casted->getRows() == -1) &&
224  (aron->shape[1] == casted->getCols() || casted->getCols() == -1));
225  }
227  {
228  auto casted = type::Quaternion::DynamicCastAndCheck(type);
229  ARMARX_TRACE;
230  return (aron->shape.size() == 3 && aron->shape[0] == 1 && aron->shape[1] == 4);
231  }
233  {
234  auto casted = type::PointCloud::DynamicCastAndCheck(type);
235  ARMARX_TRACE;
236  return (aron->shape.size() == 3);
237  }
239  {
240  auto casted = type::Image::DynamicCastAndCheck(type);
241  ARMARX_TRACE;
242  return (aron->shape.size() == 3);
243  }
245  {
246  auto casted = type::NDArray::DynamicCastAndCheck(type);
247  ARMARX_TRACE;
248  return (aron->shape.size() == (unsigned int)casted->getNumberDimensions());
249  }
250  default:
251  ARMARX_TRACE;
252  return false;
253  }
254  }
255 
256  std::string
257  NDArray::DimensionsToString(const std::vector<int>& dimensions)
258  {
259  std::stringstream ss;
260  ss << "(" << simox::alg::join(simox::alg::multi_to_string(dimensions), ", ") << ")";
261  return ss.str();
262  }
263 
264 } // namespace armarx::aron::data
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
armarx::aron::type::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:13
armarx::aron::data::NDArray::getDataAsVector
std::vector< unsigned char > getDataAsVector() const
Definition: NDArray.cpp:141
armarx::aron::data::NDArray::getFullName
virtual std::string getFullName() const override
get the full str representation of this variant
Definition: NDArray.cpp:196
armarx::aron::data::NDArray::setType
void setType(const std::string &)
Definition: NDArray.cpp:171
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:165
armarx::aron::data::NDArray::getShortName
virtual std::string getShortName() const override
get a short str representation of this variant
Definition: NDArray.cpp:190
trace.h
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:203
detail
Definition: OpenCVUtil.cpp:128
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:179
armarx::aron::data::NDArray::ToNDArrayDTO
static data::dto::NDArrayPtr ToNDArrayDTO(const PointerType &navigator)
Definition: NDArray.cpp:112
armarx::aron::data::NDArray::getShape
std::vector< int > getShape() const
Definition: NDArray.cpp:147
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:257
armarx::aron::error::NotImplementedYetException
The NotImplementedYetException class.
Definition: Exception.h:60
armarx::aron::type::Descriptor::NDARRAY
@ NDARRAY
armarx::aron::data::detail::SpecializedVariantBase< data::dto::NDArray, NDArray >::clone
virtual PointerType clone() const
Definition: SpecializedVariant.h:81
armarx::aron::data::NDArray::setShape
void setShape(const std::vector< int > &)
Definition: NDArray.cpp:153
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::data::NDArray::operator==
virtual bool operator==(const NDArray &) const override
Definition: NDArray.cpp:64
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
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:159
armarx::aron::type::Descriptor::QUATERNION
@ QUATERNION
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
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:102
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:102
All.h
armarx::aron::data::NDArray::getAsDict
DictPtr getAsDict() const
Definition: NDArray.cpp:119
armarx::aron::type::detail::SpecializedVariantBase< type::dto::Matrix, Matrix >::DynamicCastAndCheck
static std::shared_ptr< Matrix > DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:119
armarx::aron::type::Descriptor::POINTCLOUD
@ POINTCLOUD
armarx::aron::data::NDArray::toNDArrayDTO
data::dto::NDArrayPtr toNDArrayDTO() const
Definition: NDArray.cpp:183
armarx::aron::data::NDArray::setData
void setData(unsigned int, const unsigned char *)
Definition: NDArray.cpp:134
armarx::aron::data::NDArray::NDArray
NDArray(const Path &path=Path())
Definition: NDArray.cpp:39
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:69
armarx::aron::data::NDArray::getData
unsigned char * getData() const
Definition: NDArray.cpp:128
Factory.h
armarx::aron::data::detail::SpecializedVariantBase< data::dto::NDArray, NDArray >::aron
AronDataType::PointerType aron
Definition: SpecializedVariant.h:153
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:210
NDArray.h