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 detail::ComplexVariant<data::dto::NDArray, NDArray>::ComplexVariant(
60 path)
61 {
62 // Initialize fields manually because delegating to the generated Ice constructor can leave
63 // newly added optional fields uninitialized under -Werror=uninitialized.
64 aron->VERSION = aron::Version();
65 aron->shape = dim;
66 aron->type = t;
67 aron->data = data;
68 }
69
70 // operators
71 bool
72 NDArray::operator==(const NDArray& other) const
73 {
74 const auto otherAron = other.toNDArrayDTO();
75 if (aron->shape != otherAron->shape)
76 {
77 return false;
78 }
79 if (aron->type != otherAron->type)
80 {
81 return false;
82 }
83 // performs memcmp
84 if (aron->data != otherAron->data)
85 {
86 return false;
87 }
88 return true;
89 }
90
91 bool
92 NDArray::operator==(const NDArrayPtr& other) const
93 {
94 if (!other)
95 {
96 return false;
97 }
98 return *this == *other;
99 }
100
102 NDArray::clone(const Path& p) const
103 {
105 ret->setImportance(getImportance());
106 return ret;
107 }
108
109 // static methods
111 NDArray::FromNDArrayDTO(const data::dto::NDArrayPtr& aron)
112 {
113 if (!aron)
114 {
115 return nullptr;
116 }
117 return std::make_shared<NDArray>(aron);
118 }
119
120 data::dto::NDArrayPtr
122 {
123 return variant ? variant->toNDArrayDTO() : nullptr;
124 }
125
126 // public member functions
127 DictPtr
129 {
130 auto dict = std::make_shared<Dict>();
131 auto copy_this = FromAronDTO(toAronDTO(), getPath());
132 dict->addElement("data", copy_this);
133 return dict;
134 }
135
136 unsigned char*
138 {
139 return aron->data.data();
140 }
141
142 void
143 NDArray::setData(unsigned int elements, const unsigned char* src)
144 {
145 aron->data = std::vector<unsigned char>(elements);
146 std::memcpy(aron->data.data(), src, elements);
147 }
148
149 std::vector<unsigned char>
151 {
152 return aron->data;
153 }
154
155 std::vector<int>
157 {
158 return aron->shape;
159 }
160
161 void
162 NDArray::setShape(const std::vector<int>& d)
163 {
164 aron->shape = d;
165 }
166
167 void
169 {
170 aron->shape.push_back(i);
171 }
172
173 std::string
175 {
176 return aron->type;
177 }
178
179 void
180 NDArray::setType(const std::string& t)
181 {
182 if (t.empty())
183 {
185 throw error::AronException(__PRETTY_FUNCTION__, "The type cannot be empty", getPath());
186 }
187
188 aron->type = t;
189 }
190
191 data::dto::NDArrayPtr
193 {
194 return aron;
195 }
196
197 // virtual implementations
198 std::string
200 {
201 return "NDArray";
202 }
203
204 std::string
206 {
207 return "armarx::aron::data::NDArray<" + simox::alg::to_string(aron->shape, ", ") + ", " +
208 aron->type + ">";
209 }
210
213 {
215 throw error::NotImplementedYetException(__PRETTY_FUNCTION__);
216 }
217
218 bool
220 {
221 if (!type)
222 return true;
223
224 type::Descriptor typeDesc = type->getDescriptor();
225 switch (typeDesc)
226 {
228 {
229 auto casted = type::Matrix::DynamicCastAndCheck(type);
231 return (aron->shape.size() == 3 &&
232 (aron->shape[0] == casted->getRows() || casted->getRows() == -1) &&
233 (aron->shape[1] == casted->getCols() || casted->getCols() == -1));
234 }
236 {
239 return (aron->shape.size() == 3 && aron->shape[0] == 1 && aron->shape[1] == 4);
240 }
242 {
245 return (aron->shape.size() == 3);
246 }
248 {
251 return (aron->shape.size() == 3);
252 }
254 {
257 return (aron->shape.size() == (unsigned int)casted->getNumberDimensions());
258 }
259 default:
261 return false;
262 }
263 }
264
265 std::string
266 NDArray::DimensionsToString(const std::vector<int>& dimensions)
267 {
268 std::stringstream ss;
269 ss << "(" << simox::alg::join(simox::alg::multi_to_string(dimensions), ", ") << ")";
270 return ss.str();
271 }
272
273} // 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:199
void setData(unsigned int, const unsigned char *)
Definition NDArray.cpp:143
static PointerType FromNDArrayDTO(const data::dto::NDArrayPtr &aron)
Definition NDArray.cpp:111
data::dto::NDArrayPtr toNDArrayDTO() const
Definition NDArray.cpp:192
virtual type::VariantPtr recalculateType() const override
recalculate the type of a data variant. Please not tha the mapping ist NOT bijective,...
Definition NDArray.cpp:212
static std::string DimensionsToString(const std::vector< int > &dimensions)
Return dimensions in a readable string such as "(2, 3, 4)".
Definition NDArray.cpp:266
virtual std::string getFullName() const override
get the full str representation of this variant
Definition NDArray.cpp:205
std::vector< unsigned char > getDataAsVector() const
Definition NDArray.cpp:150
std::string getType() const
Definition NDArray.cpp:174
unsigned char * getData() const
Definition NDArray.cpp:137
NDArray(const Path &path=Path())
Definition NDArray.cpp:39
virtual bool operator==(const NDArray &) const override
Definition NDArray.cpp:72
virtual bool fullfillsType(const type::VariantPtr &) const override
checks, if the current data variant fullfills the given type
Definition NDArray.cpp:219
std::vector< int > getShape() const
Definition NDArray.cpp:156
void setShape(const std::vector< int > &)
Definition NDArray.cpp:162
DictPtr getAsDict() const
Definition NDArray.cpp:128
static data::dto::NDArrayPtr ToNDArrayDTO(const PointerType &navigator)
Definition NDArray.cpp:121
void setType(const std::string &)
Definition NDArray.cpp:180
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:111
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:75