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
36namespace armarx::aron::data
37{
38 // constructors
40 detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
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,
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
102 NDArray::FromNDArrayDTO(const data::dto::NDArrayPtr& aron)
103 {
104 if (!aron)
105 {
106 return nullptr;
107 }
108 return std::make_shared<NDArray>(aron);
109 }
110
111 data::dto::NDArrayPtr
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 {
176 throw error::AronException(__PRETTY_FUNCTION__, "The type cannot be empty", getPath());
177 }
178
179 aron->type = t;
180 }
181
182 data::dto::NDArrayPtr
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 {
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);
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 {
230 return (aron->shape.size() == 3 && aron->shape[0] == 1 && aron->shape[1] == 4);
231 }
233 {
236 return (aron->shape.size() == 3);
237 }
239 {
242 return (aron->shape.size() == 3);
243 }
245 {
248 return (aron->shape.size() == (unsigned int)casted->getNumberDimensions());
249 }
250 default:
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
The Path class.
Definition Path.h:36
virtual std::string getShortName() const override
get a short str representation of this variant
Definition NDArray.cpp:190
void setData(unsigned int, const unsigned char *)
Definition NDArray.cpp:134
static PointerType FromNDArrayDTO(const data::dto::NDArrayPtr &aron)
Definition NDArray.cpp:102
data::dto::NDArrayPtr toNDArrayDTO() const
Definition NDArray.cpp:183
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
static std::string DimensionsToString(const std::vector< int > &dimensions)
Return dimensions in a readable string such as "(2, 3, 4)".
Definition NDArray.cpp:257
virtual std::string getFullName() const override
get the full str representation of this variant
Definition NDArray.cpp:196
std::vector< unsigned char > getDataAsVector() const
Definition NDArray.cpp:141
std::string getType() const
Definition NDArray.cpp:165
unsigned char * getData() const
Definition NDArray.cpp:128
NDArray(const Path &path=Path())
Definition NDArray.cpp:39
virtual bool operator==(const NDArray &) const override
Definition NDArray.cpp:64
virtual bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition NDArray.cpp:210
std::vector< int > getShape() const
Definition NDArray.cpp:147
void setShape(const std::vector< int > &)
Definition NDArray.cpp:153
DictPtr getAsDict() const
Definition NDArray.cpp:119
static data::dto::NDArrayPtr ToNDArrayDTO(const PointerType &navigator)
Definition NDArray.cpp:112
void setType(const std::string &)
Definition NDArray.cpp:171
static VariantPtr FromAronDTO(const data::dto::GenericDataPtr &, const Path &=Path())
create a variant from a dto object
Definition Variant.cpp:39
Path getPath() const
get the path
Definition Variant.h:110
A base class for aron exceptions.
Definition Exception.h:37
The NotImplementedYetException class.
Definition Exception.h:61
static std::shared_ptr< Quaternion > DynamicCastAndCheck(const VariantPtr &n)
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Dict > DictPtr
Definition Dict.h:42
std::shared_ptr< NDArray > NDArrayPtr
Definition NDArray.h:46
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Variant > VariantPtr
#define ARMARX_TRACE
Definition trace.h:77