Matrix.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 "Matrix.h"
26
27#include <SimoxUtility/meta/type_name.h>
28
30
32{
33 const std::map<type::matrix::ElementType, std::tuple<std::string, int, std::string>>
35 {type::matrix::UINT8,
37 sizeof(unsigned char),
38 "::armarx::aron::type::matrix::UINT8"}},
39 {type::matrix::UINT16,
41 sizeof(unsigned short),
42 "::armarx::aron::type::matrix::UINT16"}},
43 {type::matrix::INT8,
44 {TypeName<char>::Get(), sizeof(char), "::armarx::aron::type::matrix::INT8"}},
45 {type::matrix::INT16,
46 {TypeName<short>::Get(), sizeof(short), "::armarx::aron::type::matrix::INT16"}},
47 {type::matrix::INT32,
48 {TypeName<int>::Get(), sizeof(int), "::armarx::aron::type::matrix::INT32"}},
49 {type::matrix::INT64,
50 {TypeName<long>::Get(), sizeof(long), "::armarx::aron::type::matrix::INT64"}},
51 {type::matrix::FLOAT32,
52 {TypeName<float>::Get(), sizeof(float), "::armarx::aron::type::matrix::FLOAT32"}},
53 {type::matrix::FLOAT64,
54 {TypeName<double>::Get(), sizeof(double), "::armarx::aron::type::matrix::FLOAT64"}}};
55
56 // constructors
57 Matrix::Matrix(const type::Matrix& n) :
58 detail::NDArrayGenerator<type::Matrix, Matrix>(
59 "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " +
60 (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " +
61 (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) +
62 (n.getCols() != 1 ? ", Eigen::RowMajor>" : ">"),
63 "Eigen::Matrix<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ", " +
64 (n.getRows() == -1 ? "Eigen::Dynamic" : std::to_string(n.getRows())) + ", " +
65 (n.getCols() == -1 ? "Eigen::Dynamic" : std::to_string(n.getCols())) +
66 (n.getCols() != 1 ? ", Eigen::RowMajor>" : ">"),
67 simox::meta::get_type_name<data::dto::NDArray>(),
68 simox::meta::get_type_name<type::dto::Matrix>(),
69 n)
70 {
71 }
72
73 std::vector<std::string>
74 Matrix::getRequiredIncludes() const
75 {
76 return {"<RobotAPI/libraries/aron/common/rw/eigen.h>"};
77 }
78
79 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
80 Matrix::getCtorInitializers(const std::string& name) const
81 {
82 if (type.getCols() == -1 || type.getRows() == -1)
83 {
84 return {{}, false};
85 }
86
87 if (type.getDefaultValue() == aron::type::matrix::default_value::DEFAULT ||
88 type.getDefaultValue() == aron::type::matrix::default_value::IDENTITY)
89 {
90 return {{{name, getInstantiatedCppTypename() + "::Identity()"}}, true};
91 }
92 else if (type.getDefaultValue() == aron::type::matrix::default_value::ZEROS)
93 {
94 return {{{name, getInstantiatedCppTypename() + "::Zero()"}}, true};
95 }
96 else if (type.getDefaultValue() == aron::type::matrix::default_value::ONES)
97 {
98 return {{{name, getInstantiatedCppTypename() + "::One()"}}, true};
99 }
100 else if (type.getMaybe() == type::Maybe::OPTIONAL)
101 {
102 return {{{name, "std::nullopt"}}, true};
103 }
104 else
105 {
106 // try to parse num. We ensure from typereader that defaultValue is valid number
107 return {{{name, getInstantiatedCppTypename() + "::One() * " + type.getDefaultValue()}},
108 true};
109 }
110 }
111
113 Matrix::getResetHardBlock(const std::string& cppAccessor) const
114 {
115 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
116
117 if (type.getCols() == -1 || type.getRows() == -1)
118 {
119 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "();");
120 }
121
122 else if (type.getDefaultValue() == aron::type::matrix::default_value::IDENTITY)
123 {
124 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
125 "::Identity();");
126 }
127 else if (type.getDefaultValue() == aron::type::matrix::default_value::DEFAULT ||
128 type.getDefaultValue() == aron::type::matrix::default_value::ZEROS)
129 {
130 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
131 "::Zero();");
132 }
133 else if (type.getDefaultValue() == aron::type::matrix::default_value::ONES)
134 {
135 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "::One();");
136 }
137 else if (not type.getDefaultValue().empty())
138 {
139 // try to parse num. We ensure from typereader that defaultValue is valid number
140 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
141 "::One() * " + type.getDefaultValue() + ";");
142 }
143 return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
144 }
145
147 Matrix::getResetSoftBlock(const std::string& cppAccessor) const
148 {
149 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
150 block_if_data->addLine(cppAccessor + nextEl() + "setZero();");
151 return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
152 }
153
155 Matrix::getWriteTypeBlock(const std::string& typeAccessor,
156 const std::string& cppAccessor,
157 const Path& p,
158 std::string& variantAccessor) const
159 {
160 CppBlockPtr b = std::make_shared<CppBlock>();
161 std::string escaped_accessor = EscapeAccessor(cppAccessor);
162 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
163 b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR +
164 ".writeMatrix(static_cast<int>( " + std::to_string(type.getRows()) + "), " +
165 "static_cast<int>( " + std::to_string(type.getCols()) + "), " +
166 std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + "\"" +
167 type.getDefaultValue() + "\", " +
168 conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" +
169 ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
170 "})); // of " + cppAccessor);
171
172 return b;
173 }
174
176 Matrix::getWriteBlock(const std::string& cppAccessor,
177 const Path& p,
178 std::string& variantAccessor) const
179 {
180 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
181 std::string escaped_accessor = EscapeAccessor(cppAccessor);
182 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
183 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
184
185 block_if_data->addLine("armarx::aron::write(" + ARON_WRITER_ACCESSOR + ", " +
186 resolved_accessor + ", " + variantAccessor + ", " +
187 "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
188 simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
189
190 return resolveMaybeWriteBlock(block_if_data, cppAccessor);
191 }
192
194 Matrix::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
195 {
196 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
197 std::string escaped_accessor = EscapeAccessor(cppAccessor);
198 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
199
200 if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
201 {
202 block_if_data->addLine(reset);
203 }
204
205 block_if_data->addLine("armarx::aron::read(" + ARON_READER_ACCESSOR + ", " +
206 variantAccessor + ", " + resolved_accessor + "); // of " +
207 cppAccessor);
208
209 return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
210 }
211
213 Matrix::getEqualsBlock(const std::string& accessor,
214 const std::string& otherInstanceAccessor) const
215 {
216 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
217 block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" +
218 resolveMaybeAccessor(otherInstanceAccessor) + ")))");
219 block_if_data->addLineAsBlock("return false;");
220 return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
221 }
222} // namespace armarx::aron::codegenerator::cpp::generator
#define float
Definition 16_Level.h:22
The Path class.
Definition Path.h:36
std::vector< std::string > getPath() const
Definition Path.cpp:87
std::string resolveMaybeAccessor(const std::string &) const
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
CppBlockPtr resolveMaybeEqualsBlock(const CppBlockPtr &, const std::string &, const std::string &) const
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
static std::string EscapeAccessor(const std::string &)
Definition Generator.cpp:53
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:164
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:163
static const std::string ARON_WRITER_ACCESSOR
Definition Generator.h:165
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition Generator.h:167
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
The Matrix class.
Definition Matrix.h:40
const std::map< type::Maybe, std::string > Maybe2CppString
Definition Generator.h:48
const std::map< type::matrix::ElementType, std::tuple< std::string, int, std::string > > ElementType2Cpp
Definition Matrix.cpp:34
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
const std::string & to_string(const std::string &s)
Definition Impl.cpp:41
static const char * Get()
Definition TypeName.h:14