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 
29 
30 #include <SimoxUtility/algorithm/string/string_tools.h>
31 
32 #include <Eigen/Geometry>
33 
34 
35 namespace armarx
36 {
38  {
39  rows = 0;
40  cols = 0;
41  }
42 
43  MatrixFloat::MatrixFloat(int rows, int cols)
44  {
45  this->rows = rows;
46  this->cols = cols;
47  this->data.resize(rows * cols);
48  }
49 
50  MatrixFloat::MatrixFloat(const Eigen::MatrixXf& m)
51  {
52  this->rows = m.rows();
53  this->cols = m.cols();
54  int i = 0;
55  data = std::vector<float>(rows * cols);
56 
57  for (int col = 0; col < cols; col++)
58  {
59  for (int row = 0; row < rows; row++)
60  {
61  data[i] = m(row, col);
62  i++;
63  }
64  }
65  }
66 
67 
68  MatrixFloat::MatrixFloat(int rows, int cols, const std::vector<float>& entries)
69  {
70  this->rows = rows;
71  this->cols = cols;
72  this->data = entries;
73  }
74 
75  /*void MatrixFloat::setMatrix(int width, int height, const std::vector<float> &entries)
76  {
77  this->width = width;
78  this->height = height;
79  data = entries;
80  }*/
81 
82  Eigen::MatrixXf MatrixFloat::toEigen() const
83  {
84  int i = 0;
85  Eigen::MatrixXf m(rows, cols);
86 
87  for (int col = 0; col < cols; col++)
88  {
89  for (int row = 0; row < rows; row++)
90  {
91  m(row, col) = data[i];
92  i++;
93  }
94  }
95 
96  return m;
97  }
98 
99  std::vector<float> MatrixFloat::toVector() const
100  {
101  return data;
102  }
103 
104  float& MatrixFloat::operator()(const int row, const int col)
105  {
106  return data.at(row + col * rows);
107  }
108 
110  {
111  std::stringstream stream;
112  stream << "[";
113 
114  for (int row = 0; row < rows; row++)
115  {
116  stream << (row > 0 ? ", [" : "[");
117 
118  for (int col = 0; col < cols; col++)
119  {
120  stream << (col > 0 ? ", " : "");
121  stream << (*this)(row, col);
122  }
123 
124  stream << "]";
125  }
126 
127  stream << "]";
128  return stream.str();
129  }
130 
131 
132  void MatrixFloat::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
133  {
134  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
135  obj->setInt("rows", rows);
136  obj->setInt("cols", cols);
137  Ice::StringSeq rowContent;
138  Eigen::MatrixXf m = toEigen();
139 
140  for (int row = 0; row < rows; row++)
141  {
142  std::stringstream ss;
143 
144  for (int col = 0; col < cols; col++)
145  {
146  ss << m(row, col) << (col < cols - 1 ? "," : "");
147  }
148 
149  rowContent.push_back(ss.str());
150  }
151 
152  obj->setStringArray("rowContent", rowContent);
153  }
154 
155  void MatrixFloat::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
156  {
157  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
158  rows = obj->getInt("rows");
159  cols = obj->getInt("cols");
160  Ice::StringSeq rowContent;
161  obj->getStringArray("rowContent", rowContent);
162 
163  if ((int)rowContent.size() != rows)
164  {
165  throw LocalException("unexcepted row count: ") << rowContent.size() << ", but expected " << rows;
166  }
167 
168  data.resize(rows * cols);
169 
170  for (size_t row = 0; row < rowContent.size(); row++)
171  {
172  Ice::StringSeq values = simox::alg::split(rowContent[row], ",");
173  int col = 0;
174 
175  if ((int)values.size() != cols)
176  {
177  throw LocalException("unexcepted column count: ") << values.size() << ", but expected " << cols;
178  }
179 
180  for (std::string v : values)
181  {
182  data.at(col * rows + row) = atof(v.c_str());
183  col++;
184  }
185  }
186  }
187 
188 
189 
190 
191 
193  {
194  rows = 0;
195  cols = 0;
196  }
197 
198  MatrixDouble::MatrixDouble(int rows, int cols)
199  {
200  this->rows = rows;
201  this->cols = cols;
202  this->data.resize(rows * cols);
203  }
204 
205  MatrixDouble::MatrixDouble(const Eigen::MatrixXd& m)
206  {
207  this->rows = m.rows();
208  this->cols = m.cols();
209  int i = 0;
210  data = std::vector<double>(rows * cols);
211 
212  for (int col = 0; col < cols; col++)
213  {
214  for (int row = 0; row < rows; row++)
215  {
216  data[i] = m(row, col);
217  i++;
218  }
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 MatrixDouble::toEigen() const
238  {
239  int i = 0;
240  Eigen::MatrixXd m(rows, cols);
241 
242  for (int col = 0; col < cols; col++)
243  {
244  for (int row = 0; row < rows; row++)
245  {
246  m(row, col) = data[i];
247  i++;
248  }
249  }
250 
251  return m;
252  }
253 
254  double& MatrixDouble::operator()(const int row, const int col)
255  {
256  return data.at(row + col * rows);
257  }
258 
260  {
261  std::stringstream stream;
262  stream << "[";
263 
264  for (int row = 0; row < rows; row++)
265  {
266  stream << (row > 0 ? ", [" : "[");
267 
268  for (int col = 0; col < cols; col++)
269  {
270  stream << (col > 0 ? ", " : "");
271  stream << (*this)(row, col);
272  }
273 
274  stream << "]";
275  }
276 
277  stream << "]";
278  return stream.str();
279  }
280 
281 
282  void MatrixDouble::serialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current&) const
283  {
284  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
285  obj->setInt("rows", rows);
286  obj->setInt("cols", cols);
287  Ice::StringSeq rowContent;
288  Eigen::MatrixXd m = toEigen();
289 
290  for (int row = 0; row < rows; row++)
291  {
292  std::stringstream ss;
293 
294  for (int col = 0; col < cols; col++)
295  {
296  ss << m(row, col) << (col < cols - 1 ? "," : "");
297  }
298 
299  rowContent.push_back(ss.str());
300  }
301 
302  obj->setStringArray("rowContent", rowContent);
303  }
304 
305  void MatrixDouble::deserialize(const ObjectSerializerBasePtr& serializer, const ::Ice::Current& c)
306  {
307  AbstractObjectSerializerPtr obj = AbstractObjectSerializerPtr::dynamicCast(serializer);
308  rows = obj->getInt("rows");
309  cols = obj->getInt("cols");
310  Ice::StringSeq rowContent;
311  obj->getStringArray("rowContent", rowContent);
312 
313  if ((int)rowContent.size() != rows)
314  {
315  throw LocalException("unexcepted row count: ") << rowContent.size() << ", but expected " << rows;
316  }
317 
318  data.resize(rows * cols);
319 
320  for (size_t row = 0; row < rowContent.size(); row++)
321  {
322  Ice::StringSeq values = simox::alg::split(rowContent[row], ",");
323  int col = 0;
324 
325  if ((int)values.size() != cols)
326  {
327  throw LocalException("unexcepted column count: ") << values.size() << ", but expected " << cols;
328  }
329 
330  for (std::string v : values)
331  {
332  data.at(col * rows + row) = atof(v.c_str());
333  col++;
334  }
335  }
336  }
337 }
armarx::MatrixDouble::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: MatrixVariant.cpp:282
armarx::MatrixFloat::operator()
float & operator()(const int x, const int y)
Definition: MatrixVariant.cpp:104
MatrixVariant.h
armarx::MatrixFloat::toVector
std::vector< float > toVector() const
Definition: MatrixVariant.cpp:99
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
AbstractObjectSerializer.h
armarx::MatrixDouble::MatrixDouble
MatrixDouble()
Definition: MatrixVariant.cpp:192
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::MatrixFloat::toEigen
virtual Eigen::MatrixXf toEigen() const
Definition: MatrixVariant.cpp:82
armarx::MatrixDouble::toEigen
virtual Eigen::MatrixXd toEigen() const
Definition: MatrixVariant.cpp:237
IceInternal::Handle
Definition: forward_declarations.h:8
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::MatrixFloat::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: MatrixVariant.cpp:132
armarx::MatrixDouble::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: MatrixVariant.cpp:305
ExpressionException.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::MatrixDouble::operator()
double & operator()(const int x, const int y)
Definition: MatrixVariant.cpp:254
armarx::MatrixFloat::MatrixFloat
MatrixFloat()
Definition: MatrixVariant.cpp:37
armarx::MatrixDouble::toJsonRowMajor
std::string toJsonRowMajor()
Definition: MatrixVariant.cpp:259
armarx::MatrixFloat::toJsonRowMajor
std::string toJsonRowMajor()
Definition: MatrixVariant.cpp:109
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::MatrixFloat::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: MatrixVariant.cpp:155
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36