MatrixVariant.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::Core
19 * @author Peter Kaiser <peter.kaiser@kit.edu>
20 * @date 2014
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#include "MatrixVariant.h"
26
27#include <Eigen/Geometry>
28
29#include <SimoxUtility/algorithm/string/string_tools.h>
30
33
34namespace armarx
35{
37 {
38 rows = 0;
39 cols = 0;
40 }
41
42 MatrixFloat::MatrixFloat(int rows, int cols)
43 {
44 this->rows = rows;
45 this->cols = cols;
46 this->data.resize(rows * cols);
47 }
48
49 MatrixFloat::MatrixFloat(const Eigen::MatrixXf& m)
50 {
51 this->rows = m.rows();
52 this->cols = m.cols();
53 int i = 0;
54 data = std::vector<float>(rows * cols);
55
56 for (int col = 0; col < cols; col++)
57 {
58 for (int row = 0; row < rows; row++)
59 {
60 data[i] = m(row, col);
61 i++;
62 }
63 }
64 }
65
66 MatrixFloat::MatrixFloat(int rows, int cols, const std::vector<float>& entries)
67 {
68 this->rows = rows;
69 this->cols = cols;
70 this->data = entries;
71 }
72
73 /*void MatrixFloat::setMatrix(int width, int height, const std::vector<float> &entries)
74 {
75 this->width = width;
76 this->height = height;
77 data = entries;
78 }*/
79
80 Eigen::MatrixXf
82 {
83 int i = 0;
84 Eigen::MatrixXf m(rows, cols);
85
86 for (int col = 0; col < cols; col++)
87 {
88 for (int row = 0; row < rows; row++)
89 {
90 m(row, col) = data[i];
91 i++;
92 }
93 }
94
95 return m;
96 }
97
98 std::vector<float>
100 {
101 return data;
102 }
103
104 float&
105 MatrixFloat::operator()(const int row, const int col)
106 {
107 return data.at(row + col * rows);
108 }
109
110 std::string
112 {
113 std::stringstream stream;
114 stream << "[";
115
116 for (int row = 0; row < rows; row++)
117 {
118 stream << (row > 0 ? ", [" : "[");
119
120 for (int col = 0; col < cols; col++)
121 {
122 stream << (col > 0 ? ", " : "");
123 stream << (*this)(row, col);
124 }
125
126 stream << "]";
127 }
128
129 stream << "]";
130 return stream.str();
131 }
132
133 void
134 MatrixFloat::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
135 {
136 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
137 obj->setInt("rows", rows);
138 obj->setInt("cols", cols);
139 Ice::StringSeq rowContent;
140 Eigen::MatrixXf m = toEigen();
141
142 for (int row = 0; row < rows; row++)
143 {
144 std::stringstream ss;
145
146 for (int col = 0; col < cols; col++)
147 {
148 ss << m(row, col) << (col < cols - 1 ? "," : "");
149 }
150
151 rowContent.push_back(ss.str());
152 }
153
154 obj->setStringArray("rowContent", rowContent);
155 }
156
157 void
158 MatrixFloat::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
159 {
160 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
161 rows = obj->getInt("rows");
162 cols = obj->getInt("cols");
163 Ice::StringSeq rowContent;
164 obj->getStringArray("rowContent", rowContent);
165
166 if ((int)rowContent.size() != rows)
167 {
168 throw LocalException("unexcepted row count: ")
169 << rowContent.size() << ", but expected " << rows;
170 }
171
172 data.resize(rows * cols);
173
174 for (size_t row = 0; row < rowContent.size(); row++)
175 {
176 Ice::StringSeq values = simox::alg::split(rowContent[row], ",");
177 int col = 0;
178
179 if ((int)values.size() != cols)
180 {
181 throw LocalException("unexcepted column count: ")
182 << values.size() << ", but expected " << cols;
183 }
184
185 for (std::string v : values)
186 {
187 data.at(col * rows + row) = atof(v.c_str());
188 col++;
189 }
190 }
191 }
192
194 {
195 rows = 0;
196 cols = 0;
197 }
198
199 MatrixDouble::MatrixDouble(int rows, int cols)
200 {
201 this->rows = rows;
202 this->cols = cols;
203 this->data.resize(rows * cols);
204 }
205
206 MatrixDouble::MatrixDouble(const Eigen::MatrixXd& m)
207 {
208 this->rows = m.rows();
209 this->cols = m.cols();
210 int i = 0;
211 data = std::vector<double>(rows * cols);
212
213 for (int col = 0; col < cols; col++)
214 {
215 for (int row = 0; row < rows; row++)
216 {
217 data[i] = m(row, col);
218 i++;
219 }
220 }
221 }
222
223 MatrixDouble::MatrixDouble(int rows, int cols, const std::vector<double>& entries)
224 {
225 this->rows = rows;
226 this->cols = cols;
227 this->data = entries;
228 }
229
230 /*void MatrixDouble::setMatrix(int width, int height, const std::vector<double> &entries)
231 {
232 this->width = width;
233 this->height = height;
234 data = entries;
235 }*/
236
237 Eigen::MatrixXd
239 {
240 int i = 0;
241 Eigen::MatrixXd m(rows, cols);
242
243 for (int col = 0; col < cols; col++)
244 {
245 for (int row = 0; row < rows; row++)
246 {
247 m(row, col) = data[i];
248 i++;
249 }
250 }
251
252 return m;
253 }
254
255 double&
256 MatrixDouble::operator()(const int row, const int col)
257 {
258 return data.at(row + col * rows);
259 }
260
261 std::string
263 {
264 std::stringstream stream;
265 stream << "[";
266
267 for (int row = 0; row < rows; row++)
268 {
269 stream << (row > 0 ? ", [" : "[");
270
271 for (int col = 0; col < cols; col++)
272 {
273 stream << (col > 0 ? ", " : "");
274 stream << (*this)(row, col);
275 }
276
277 stream << "]";
278 }
279
280 stream << "]";
281 return stream.str();
282 }
283
284 void
285 MatrixDouble::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
286 {
287 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
288 obj->setInt("rows", rows);
289 obj->setInt("cols", cols);
290 Ice::StringSeq rowContent;
291 Eigen::MatrixXd m = toEigen();
292
293 for (int row = 0; row < rows; row++)
294 {
295 std::stringstream ss;
296
297 for (int col = 0; col < cols; col++)
298 {
299 ss << m(row, col) << (col < cols - 1 ? "," : "");
300 }
301
302 rowContent.push_back(ss.str());
303 }
304
305 obj->setStringArray("rowContent", rowContent);
306 }
307
308 void
309 MatrixDouble::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
310 {
311 AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
312 rows = obj->getInt("rows");
313 cols = obj->getInt("cols");
314 Ice::StringSeq rowContent;
315 obj->getStringArray("rowContent", rowContent);
316
317 if ((int)rowContent.size() != rows)
318 {
319 throw LocalException("unexcepted row count: ")
320 << rowContent.size() << ", but expected " << rows;
321 }
322
323 data.resize(rows * cols);
324
325 for (size_t row = 0; row < rowContent.size(); row++)
326 {
327 Ice::StringSeq values = simox::alg::split(rowContent[row], ",");
328 int col = 0;
329
330 if ((int)values.size() != cols)
331 {
332 throw LocalException("unexcepted column count: ")
333 << values.size() << ", but expected " << cols;
334 }
335
336 for (std::string v : values)
337 {
338 data.at(col * rows + row) = atof(v.c_str());
339 col++;
340 }
341 }
342 }
343} // namespace armarx
constexpr T c
double & operator()(const int x, const int y)
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
std::string toJsonRowMajor()
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
virtual Eigen::MatrixXd toEigen() const
std::vector< float > toVector() const
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
std::string toJsonRowMajor()
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
virtual Eigen::MatrixXf toEigen() const
float & operator()(const int x, const int y)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr