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 
30 
32 {
33  const std::map<type::quaternion::ElementType, std::tuple<std::string, int, std::string>>
34  ElementType2Cpp = {{type::quaternion::ElementType::FLOAT32,
36  sizeof(float),
37  "::armarx::aron::type::quaternion::ElementType::FLOAT32"}},
38  {type::quaternion::ElementType::FLOAT64,
40  sizeof(double),
41  "::armarx::aron::type::quaternion::ElementType::FLOAT64"}}};
42 
43  // constructors
45  detail::NDArrayGenerator<type::Quaternion, Quaternion>(
46  "Eigen::Quaternion<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ">",
47  "Eigen::Quaternion<" + std::get<0>(ElementType2Cpp.at(n.getElementType())) + ">",
48  simox::meta::get_type_name<data::dto::NDArray>(),
49  simox::meta::get_type_name<type::dto::Quaternion>(),
50  n)
51  {
52  }
53 
54  std::vector<std::string>
56  {
57  return {"<RobotAPI/libraries/aron/common/rw/eigen.h>"};
58  }
59 
60  std::pair<std::vector<std::pair<std::string, std::string>>, bool>
61  Quaternion::getCtorInitializers(const std::string& name) const
62  {
63  if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT)
64  {
65  return {{{name, getInstantiatedCppTypename() + "::Identity()"}}, true};
66  }
67  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
68  {
69  return {{{name, getInstantiatedCppTypename() + "(0, 0, 0, 0)"}}, true};
70  }
71  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
72  {
73  return {{{name, getInstantiatedCppTypename() + "(1, 1, 1, 1)"}}, true};
74  }
75  else
76  {
77  // try to parse num. We ensure from typereader that defaultValue is valid number
78  return {{{name, getInstantiatedCppTypename() + "(" + type.getDefaultValue() + ")"}},
79  true};
80  }
81  }
82 
84  Quaternion::getResetHardBlock(const std::string& cppAccessor) const
85  {
86  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
87 
88  if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT)
89  {
90  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "::Identity();");
91  }
92  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
93  {
94  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(0, 0, 0, 0);");
95  }
96  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
97  {
98  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(1, 1, 1, 1);");
99  }
100  else if (not type.getDefaultValue().empty())
101  {
102  // try to parse num. We ensure from typereader that defaultValue is valid number
103  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(" + type.getDefaultValue() + ");");
104  }
105  return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
106  }
107 
109  Quaternion::getResetSoftBlock(const std::string& cppAccessor) const
110  {
111  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
112  block_if_data->addLine(cppAccessor + nextEl() + "setIdentity();");
113  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
114  }
115 
117  Quaternion::getWriteTypeBlock(const std::string& typeAccessor,
118  const std::string& cppAccessor,
119  const Path& p,
120  std::string& variantAccessor) const
121  {
122  CppBlockPtr b = std::make_shared<CppBlock>();
123  std::string escaped_accessor = EscapeAccessor(cppAccessor);
124  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
125 
126  b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeQuaternion(" +
127  std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + "\"" +
128  type.getDefaultValue() + "\", " +
129  conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" +
130  ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
131  "})); // of " + cppAccessor);
132  return b;
133  }
134 
136  Quaternion::getWriteBlock(const std::string& cppAccessor,
137  const Path& p,
138  std::string& variantAccessor) const
139  {
140  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
141  std::string escaped_accessor = EscapeAccessor(cppAccessor);
142  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
143  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
144 
145  block_if_data->addLine("armarx::aron::write(" + ARON_WRITER_ACCESSOR + ", " +
146  resolved_accessor + ", " + variantAccessor + ", " +
147  "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
148  simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
149 
150  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
151  }
152 
154  Quaternion::getReadBlock(const std::string& cppAccessor,
155  const 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 
161  if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
162  {
163  block_if_data->addLine(reset);
164  }
165 
166  block_if_data->addLine("armarx::aron::read(" + ARON_READER_ACCESSOR + ", " +
167  variantAccessor + ", " + resolved_accessor + "); // of " +
168  cppAccessor);
169 
170  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
171  }
172 
174  Quaternion::getEqualsBlock(const std::string& accessor,
175  const std::string& otherInstanceAccessor) const
176  {
177  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
178  block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" +
179  otherInstanceAccessor + ")))");
180  block_if_data->addLineAsBlock("return false;");
181  return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
182  }
183 } // namespace armarx::aron::codegenerator::cpp::generator
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:154
armarx::aron::codegenerator::cpp::generator::Quaternion::Quaternion
Quaternion(const type::Quaternion &)
Definition: Quaternion.cpp:44
armarx::aron::codegenerator::cpp::generator::Quaternion::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Quaternion.cpp:117
armarx::aron::codegenerator::cpp::generator::Quaternion::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Quaternion.cpp:136
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::codegenerator::cpp::generator::Quaternion
Definition: Quaternion.h:34
armarx::aron::codegenerator::cpp::Generator::EscapeAccessor
static std::string EscapeAccessor(const std::string &)
Definition: Generator.cpp:53
armarx::aron::codegenerator::cpp::Generator::resolveMaybeGeneratorWithSetter
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
Definition: Generator.cpp:602
detail
Definition: OpenCVUtil.cpp:127
armarx::aron::codegenerator::cpp::Generator::resolveMaybeAccessor
std::string resolveMaybeAccessor(const std::string &) const
Definition: Generator.cpp:563
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:152
armarx::aron::codegenerator::cpp::generator::Quaternion::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Quaternion.cpp:55
armarx::aron::codegenerator::cpp::generator::Quaternion::getResetHardBlock
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition: Quaternion.cpp:84
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::codegenerator::cpp::generator::Quaternion::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Quaternion.cpp:154
armarx::aron::type::Quaternion
The Qaternion class.
Definition: Quaternion.h:39
armarx::aron::Path
The Path class.
Definition: Path.h:36
Quaternion.h
armarx::aron::codegenerator::cpp::generator::NDArray
Definition: NDArray.h:33
armarx::aron::codegenerator::cpp::generator::Quaternion::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Quaternion.cpp:109
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::type::Quaternion::getElementType
type::quaternion::ElementType getElementType() const
Definition: Quaternion.cpp:58
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Quaternion, Quaternion >::type
type::Quaternion type
Definition: SpecializedGenerator.h:52
armarx::aron::codegenerator::cpp::generator::ElementType2Cpp
const std::map< type::matrix::ElementType, std::tuple< std::string, int, std::string > > ElementType2Cpp
Definition: Matrix.cpp:34
armarx::aron::codegenerator::cpp::Generator::resolveMaybeReadBlock
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
Definition: Generator.cpp:711
TypeName.h
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
armarx::aron::codegenerator::cpp::Generator::resolveMaybeResetHardBlock
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:655
armarx::aron::codegenerator::cpp::Generator::resolveMaybeEqualsBlock
CppBlockPtr resolveMaybeEqualsBlock(const CppBlockPtr &, const std::string &, const std::string &) const
Definition: Generator.cpp:735
armarx::aron::codegenerator::cpp::generator::Quaternion::getEqualsBlock
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
Definition: Quaternion.cpp:174
float
#define float
Definition: 16_Level.h:22
std
Definition: Application.h:66
armarx::aron::codegenerator::cpp::Generator::nextEl
std::string nextEl() const
Definition: Generator.cpp:613
armarx::aron::codegenerator::cpp::Generator::resolveMaybeResetSoftBlock
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:677
armarx::aron::type::detail::NDArrayVariant::getDefaultValue
std::string getDefaultValue() const
Definition: NDArrayVariant.h:70
armarx::aron::codegenerator::cpp::Generator::ARON_READER_ACCESSOR
static const std::string ARON_READER_ACCESSOR
Definition: Generator.h:151
armarx::aron::codegenerator::cpp::Generator::getInstantiatedCppTypename
std::string getInstantiatedCppTypename() const
Definition: Generator.cpp:110
armarx::aron::codegenerator::cpp::generator::Quaternion::getCtorInitializers
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const final
Definition: Quaternion.cpp:61
simox
Definition: Impl.cpp:40
armarx::aron::codegenerator::cpp::Generator::resolveMaybeWriteBlock
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:694
armarx::aron::codegenerator::cpp::conversion::Maybe2CppString
const std::map< type::Maybe, std::string > Maybe2CppString
Definition: Generator.h:49
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:85
armarx::aron::TypeName::Get
static const char * Get()
Definition: TypeName.h:14