SimpleJsonLoggerEntry.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 Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "SimpleJsonLoggerEntry.h"
25 #include <IceUtil/Time.h>
26 
27 namespace armarx
28 {
30  : obj(new JsonObject)
31  {
32  }
33 
34  void SimpleJsonLoggerEntry::AddAsArr(const std::string& key, const Eigen::Vector3f& vec)
35  {
36  obj->add(key, ToArr((Eigen::VectorXf)vec));
37  }
38 
39  void SimpleJsonLoggerEntry::AddAsArr(const std::string& key, const Eigen::VectorXf& vec)
40  {
41  obj->add(key, ToArr(vec));
42  }
43 
44  void SimpleJsonLoggerEntry::AddAsObj(const std::string& key, const Eigen::Vector3f& vec)
45  {
46  obj->add(key, ToObj(vec));
47  }
48 
49  void SimpleJsonLoggerEntry::AddAsArr(const std::string& key, const Eigen::Matrix4f& mat)
50  {
51  obj->add(key, ToArr(mat));
52  }
53 
54  void SimpleJsonLoggerEntry::AddMatrix(const std::string& key, const Eigen::MatrixXf& mat)
55  {
56  obj->add(key, MatrixToArr(mat));
57  }
58 
59  void SimpleJsonLoggerEntry::Add(const std::string& key, const std::string& value)
60  {
61  obj->add(key, JsonValue::Create(value));
62  }
63 
64  void SimpleJsonLoggerEntry::Add(const std::string& key, float value)
65  {
66  obj->add(key, JsonValue::Create(value));
67  }
68 
69  void SimpleJsonLoggerEntry::Add(const std::string& key, const std::vector<float>& value)
70  {
71  obj->add(key, ToArr(value));
72  }
73 
74  void SimpleJsonLoggerEntry::Add(const std::string& key, const std::map<std::string, float>& value)
75  {
76  obj->add(key, ToObj(value));
77  }
78 
79 
80  JsonArrayPtr SimpleJsonLoggerEntry::ToArr(const Eigen::VectorXf& vec)
81  {
82  JsonArrayPtr arr(new JsonArray);
83  for (int i = 0; i < vec.rows(); i++)
84  {
85  arr->add(JsonValue::Create(vec(i)));
86  }
87  return arr;
88  }
89 
90  JsonArrayPtr SimpleJsonLoggerEntry::ToArr(const std::vector<float>& vec)
91  {
92  JsonArrayPtr arr(new JsonArray);
93  for (float v : vec)
94  {
95  arr->add(JsonValue::Create(v));
96  }
97  return arr;
98  }
99 
101  {
103  obj->add("x", JsonValue::Create(vec(0)));
104  obj->add("y", JsonValue::Create(vec(1)));
105  obj->add("z", JsonValue::Create(vec(2)));
106  return obj;
107  }
108 
109  JsonObjectPtr SimpleJsonLoggerEntry::ToObj(const std::map<std::string, float>& value)
110  {
112  for (const auto& pair : value)
113  {
114  obj->add(pair.first, JsonValue::Create(pair.second));
115  }
116  return obj;
117  }
118 
120  {
121  JsonArrayPtr arr(new JsonArray);
122  JsonArrayPtr row1(new JsonArray);
123  JsonArrayPtr row2(new JsonArray);
124  JsonArrayPtr row3(new JsonArray);
125  JsonArrayPtr row4(new JsonArray);
126 
127  row1->add(JsonValue::Create(mat(0, 0)));
128  row1->add(JsonValue::Create(mat(0, 1)));
129  row1->add(JsonValue::Create(mat(0, 2)));
130  row1->add(JsonValue::Create(mat(0, 3)));
131 
132  row2->add(JsonValue::Create(mat(1, 0)));
133  row2->add(JsonValue::Create(mat(1, 1)));
134  row2->add(JsonValue::Create(mat(1, 2)));
135  row2->add(JsonValue::Create(mat(1, 3)));
136 
137  row3->add(JsonValue::Create(mat(2, 0)));
138  row3->add(JsonValue::Create(mat(2, 1)));
139  row3->add(JsonValue::Create(mat(2, 2)));
140  row3->add(JsonValue::Create(mat(2, 3)));
141 
142  row4->add(JsonValue::Create(mat(3, 0)));
143  row4->add(JsonValue::Create(mat(3, 1)));
144  row4->add(JsonValue::Create(mat(3, 2)));
145  row4->add(JsonValue::Create(mat(3, 3)));
146 
147  arr->add(row1);
148  arr->add(row2);
149  arr->add(row3);
150  arr->add(row4);
151 
152  return arr;
153  }
154 
156  {
157  JsonArrayPtr arr(new JsonArray);
158  for (int i = 0; i < mat.rows(); i++)
159  {
160  JsonArrayPtr row(new JsonArray);
161  for (int j = 0; j < mat.cols(); j++)
162  {
163  row->add(JsonValue::Create(mat(i, j)));
164  }
165  arr->add(row);
166  }
167  return arr;
168  }
169 
171  {
172  IceUtil::Time now = IceUtil::Time::now();
173  obj->add("timestamp", JsonValue::Create(now.toDateTime()));
174  }
175 }
armarx::SimpleJsonLoggerEntry::AddAsArr
void AddAsArr(const std::string &key, const Eigen::Vector3f &vec)
Definition: SimpleJsonLoggerEntry.cpp:34
armarx::SimpleJsonLoggerEntry::ToArr
static JsonArrayPtr ToArr(const Eigen::VectorXf &vec)
Definition: SimpleJsonLoggerEntry.cpp:80
armarx::JsonArrayPtr
std::shared_ptr< JsonArray > JsonArrayPtr
Definition: JsonArray.h:32
armarx::SimpleJsonLoggerEntry::AddMatrix
void AddMatrix(const std::string &key, const Eigen::MatrixXf &mat)
Definition: SimpleJsonLoggerEntry.cpp:54
armarx::JsonValue::Create
static JsonValuePtr Create(const std::string &value)
Definition: JsonValue.cpp:65
armarx::JsonArray
Definition: JsonArray.h:34
armarx::SimpleJsonLoggerEntry::SimpleJsonLoggerEntry
SimpleJsonLoggerEntry()
Definition: SimpleJsonLoggerEntry.cpp:29
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::SimpleJsonLoggerEntry::obj
JsonObjectPtr obj
Definition: SimpleJsonLoggerEntry.h:59
armarx::SimpleJsonLoggerEntry::AddAsObj
void AddAsObj(const std::string &key, const Eigen::Vector3f &vec)
Definition: SimpleJsonLoggerEntry.cpp:44
armarx::SimpleJsonLoggerEntry::Add
void Add(const std::string &key, const std::string &value)
Definition: SimpleJsonLoggerEntry.cpp:59
SimpleJsonLoggerEntry.h
armarx::SimpleJsonLoggerEntry::MatrixToArr
static JsonArrayPtr MatrixToArr(Eigen::MatrixXf mat)
Definition: SimpleJsonLoggerEntry.cpp:155
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::JsonObject
Definition: JsonObject.h:36
armarx::SimpleJsonLoggerEntry::ToObj
static JsonObjectPtr ToObj(Eigen::Vector3f vec)
Definition: SimpleJsonLoggerEntry.cpp:100
armarx::SimpleJsonLoggerEntry::AddTimestamp
void AddTimestamp()
Definition: SimpleJsonLoggerEntry.cpp:170
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::JsonObjectPtr
std::shared_ptr< JsonObject > JsonObjectPtr
Definition: JsonObject.h:34