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