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  if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT)
65  {
66  return {{{name, getInstantiatedCppTypename() + "::Identity()"}}, true};
67  }
68  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
69  {
70  return {{{name, getInstantiatedCppTypename() + "(0, 0, 0, 0)"}}, true};
71  }
72  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
73  {
74  return {{{name, getInstantiatedCppTypename() + "(1, 1, 1, 1)"}}, true};
75  }
76  else
77  {
78  // try to parse num. We ensure from typereader that defaultValue is valid number
79  return {{{name, getInstantiatedCppTypename() + "(" + type.getDefaultValue() + ")"}},
80  true};
81  }
82  }
83 
85  Quaternion::getResetHardBlock(const std::string& cppAccessor) const
86  {
87  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
88 
89  if (type.getDefaultValue() == aron::type::quaternion::default_value::DEFAULT)
90  {
91  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
92  "::Identity();");
93  }
94  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ZEROS)
95  {
96  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
97  "(0, 0, 0, 0);");
98  }
99  else if (type.getDefaultValue() == aron::type::quaternion::default_value::ONES)
100  {
101  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() +
102  "(1, 1, 1, 1);");
103  }
104  else if (not type.getDefaultValue().empty())
105  {
106  // try to parse num. We ensure from typereader that defaultValue is valid number
107  block_if_data->addLine(cppAccessor + " = " + getInstantiatedCppTypename() + "(" +
108  type.getDefaultValue() + ");");
109  }
110  return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
111  }
112 
114  Quaternion::getResetSoftBlock(const std::string& cppAccessor) const
115  {
116  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
117  block_if_data->addLine(cppAccessor + nextEl() + "setIdentity();");
118  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
119  }
120 
122  Quaternion::getWriteTypeBlock(const std::string& typeAccessor,
123  const std::string& cppAccessor,
124  const Path& p,
125  std::string& variantAccessor) const
126  {
127  CppBlockPtr b = std::make_shared<CppBlock>();
128  std::string escaped_accessor = EscapeAccessor(cppAccessor);
129  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
130 
131  b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeQuaternion(" +
132  std::get<2>(ElementType2Cpp.at(type.getElementType())) + ", " + "\"" +
133  type.getDefaultValue() + "\", " +
134  conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" +
135  ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
136  "})); // of " + cppAccessor);
137  return b;
138  }
139 
141  Quaternion::getWriteBlock(const std::string& cppAccessor,
142  const Path& p,
143  std::string& variantAccessor) const
144  {
145  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
146  std::string escaped_accessor = EscapeAccessor(cppAccessor);
147  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
148  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
149 
150  block_if_data->addLine("armarx::aron::write(" + ARON_WRITER_ACCESSOR + ", " +
151  resolved_accessor + ", " + variantAccessor + ", " +
152  "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
153  simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
154 
155  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
156  }
157 
159  Quaternion::getReadBlock(const std::string& cppAccessor,
160  const std::string& variantAccessor) const
161  {
162  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
163  std::string escaped_accessor = EscapeAccessor(cppAccessor);
164  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
165 
166  if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
167  {
168  block_if_data->addLine(reset);
169  }
170 
171  block_if_data->addLine("armarx::aron::read(" + ARON_READER_ACCESSOR + ", " +
172  variantAccessor + ", " + resolved_accessor + "); // of " +
173  cppAccessor);
174 
175  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
176  }
177 
179  Quaternion::getEqualsBlock(const std::string& accessor,
180  const std::string& otherInstanceAccessor) const
181  {
182  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
183  block_if_data->addLine("if (not (" + accessor + nextEl() + "isApprox(" +
184  (type.getMaybe() != type::Maybe::NONE ? "*" + otherInstanceAccessor
185  : otherInstanceAccessor) +
186  ")))");
187  block_if_data->addLineAsBlock("return false;");
188  return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
189  }
190 } // 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:167
armarx::aron::codegenerator::cpp::generator::Quaternion::Quaternion
Quaternion(const type::Quaternion &)
Definition: Quaternion.cpp:45
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:122
armarx::aron::codegenerator::cpp::generator::Quaternion::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Quaternion.cpp:141
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:87
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:128
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:37
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:165
armarx::aron::codegenerator::cpp::generator::Quaternion::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Quaternion.cpp:56
armarx::aron::codegenerator::cpp::generator::Quaternion::getResetHardBlock
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition: Quaternion.cpp:85
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:163
armarx::aron::codegenerator::cpp::generator::Quaternion::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Quaternion.cpp:159
armarx::aron::type::Quaternion
The Qaternion class.
Definition: Quaternion.h:39
armarx::aron::Path
The Path class.
Definition: Path.h:35
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:114
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:59
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:28
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:179
float
#define float
Definition: 16_Level.h:22
std
Definition: Application.h:66
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:14
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:68
armarx::aron::codegenerator::cpp::Generator::ARON_READER_ACCESSOR
static const std::string ARON_READER_ACCESSOR
Definition: Generator.h:164
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:62
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:48
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:87
armarx::aron::TypeName::Get
static const char * Get()
Definition: TypeName.h:14