Quaternion.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 "Quaternion.h"
26
27#include <SimoxUtility/meta/type_name.h>
28
29#include <RobotAPI/interface/aron/Aron.h>
31
33{
34 const std::map<type::quaternion::ElementType, std::tuple<std::string, int, std::string>>
35 ElementType2Cpp = {{type::quaternion::ElementType::FLOAT32,
37 sizeof(float),
38 "::armarx::aron::type::quaternion::ElementType::FLOAT32"}},
39 {type::quaternion::ElementType::FLOAT64,
41 sizeof(double),
42 "::armarx::aron::type::quaternion::ElementType::FLOAT64"}}};
43
44 // constructors
46 detail::NDArrayGenerator<type::Quaternion, Quaternion>(
47 "Eigen::Quaternion<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ">",
48 "Eigen::Quaternion<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ">",
49 simox::meta::get_type_name<data::dto::NDArray>(),
50 simox::meta::get_type_name<type::dto::Quaternion>(),
51 n)
52 {
53 }
54
55 std::vector<std::string>
57 {
58 return {"<RobotAPI/libraries/aron/common/rw/eigen.h>"};
59 }
60
61 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
62 Quaternion::getCtorInitializers(const std::string& name) const
63 {
64
65
66 if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT ||
67 type.getDefaultValue() == aron::type::quaternion::default_value::IDENTITY)
68 {
69 return {{{name, getInstantiatedCppTypename() + "::Identity()"}}, true};
70 }
71 else if (type.getMaybe() == type::Maybe::OPTIONAL)
72 {
73 return {{{name, "std::nullopt"}}, true};
74 }
75
76 else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
77 {
78 std::cerr << "WARNING: using ZEROS as a default value for Quaternions is highly "
79 "frowned upon and will be removed in the future";
80 return {{{name, getInstantiatedCppTypename() + "(0, 0, 0, 0)"}}, true};
81 }
82 else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
83 {
84 std::cerr << "WARNING: using ONES as a default value for Quaternions is highly frowned "
85 "upon and will be removed in the future";
86 return {{{name, getInstantiatedCppTypename() + "(1, 1, 1, 1)"}}, true};
87 }
88 else
89 {
90 // try to parse num. We ensure from typereader that defaultValue is valid number
91 return {{{name, getInstantiatedCppTypename() + "(" + type.getDefaultValue() + ")"}},
92 true};
93 }
94 }
95
97 Quaternion::getResetHardBlock(const std::string& cppAccessor) const
98 {
99 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
100
101 if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT)
102 {
103 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
104 "::Identity();");
105 }
106 else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
107 {
108 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
109 "(0, 0, 0, 0);");
110 }
111 else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
112 {
113 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
114 "(1, 1, 1, 1);");
115 }
116 else if (not type.getDefaultValue().empty())
117 {
118 // try to parse num. We ensure from typereader that defaultValue is valid number
119 block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(" +
120 type.getDefaultValue() + ");");
121 }
122 return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
123 }
124
126 Quaternion::getResetSoftBlock(const std::string& cppAccessor) const
127 {
128 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
129 block_if_data->addLine(cppAccessor + nextEl() + "setIdentity();");
130 return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
131 }
132
134 Quaternion::getWriteTypeBlock(const std::string& typeAccessor,
135 const std::string& cppAccessor,
136 const Path& p,
137 std::string& variantAccessor) const
138 {
139 CppBlockPtr b = std::make_shared<CppBlock>();
140 std::string escaped_accessor = EscapeAccessor(cppAccessor);
141 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
142
143 b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeQuaternion(" +
144 std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + "\"" +
145 type.getDefaultValue() + "\", " +
146 conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" +
147 ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
148 "})); // of " + cppAccessor);
149 return b;
150 }
151
153 Quaternion::getWriteBlock(const std::string& cppAccessor,
154 const Path& p,
155 std::string& variantAccessor) const
156 {
157 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
158 std::string escaped_accessor = EscapeAccessor(cppAccessor);
159 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
160 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
161
162 block_if_data->addLine("armarx::aron::write(" + ARON_WRITER_ACCESSOR + ", " +
163 resolved_accessor + ", " + variantAccessor + ", " +
164 "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
165 simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
166
167 return resolveMaybeWriteBlock(block_if_data, cppAccessor);
168 }
169
171 Quaternion::getReadBlock(const std::string& cppAccessor,
172 const std::string& variantAccessor) const
173 {
174 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
175 std::string escaped_accessor = EscapeAccessor(cppAccessor);
176 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
177
178 if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
179 {
180 block_if_data->addLine(reset);
181 }
182
183 block_if_data->addLine("armarx::aron::read(" + ARON_READER_ACCESSOR + ", " +
184 variantAccessor + ", " + resolved_accessor + "); // of " +
185 cppAccessor);
186
187 return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
188 }
189
191 Quaternion::getEqualsBlock(const std::string& accessor,
192 const std::string& otherInstanceAccessor) const
193 {
194 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
195 block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" +
196 (type.getMaybe() != type::Maybe::NONE ? "*" + otherInstanceAccessor
197 : otherInstanceAccessor) +
198 ")))");
199 block_if_data->addLineAsBlock("return false;");
200 return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
201 }
202} // namespace armarx::aron::codegenerator::cpp::generator
#define float
Definition 16_Level.h:22
Quaternion()
Construct an identity quaternion.
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
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
std::vector< std::string > getRequiredIncludes() const final
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const final
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
The Qaternion class.
Definition Quaternion.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
Definition Impl.cpp:41
static const char * Get()
Definition TypeName.h:14