- 3.0.2 core module.
InfoFileParser.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <iostream>
9 
10 #include <boost/property_tree/ptree.hpp>
11 #include <boost/property_tree/info_parser.hpp>
12 
13 
14 namespace ct {
15 namespace core {
16 
30 template <typename SCALAR>
31 void loadScalar(const std::string& filename, const std::string& scalarName, SCALAR& scalar, const std::string& ns = "")
32 {
33  boost::property_tree::ptree pt;
34  boost::property_tree::read_info(filename, pt);
35 
36  scalar = pt.get<SCALAR>(ns + scalarName);
37 }
38 
50 template <typename SCALAR>
51 void loadScalarOptional(const std::string& filename,
52  const std::string& scalarName,
53  SCALAR& scalar,
54  const SCALAR& defaultValue,
55  const std::string& ns = "")
56 {
57  boost::property_tree::ptree pt;
58  boost::property_tree::read_info(filename, pt);
59 
60  scalar = pt.get<SCALAR>(ns + scalarName, defaultValue);
61 }
62 
110 template <typename SCALAR, int ROW, int COL>
111 void loadMatrix(const std::string& filename,
112  const std::string& matrixName,
113  Eigen::Matrix<SCALAR, ROW, COL>& matrix,
114  const std::string& ns = "")
115 {
116  size_t rows = matrix.rows();
117  size_t cols = matrix.cols();
118 
119  boost::property_tree::ptree pt;
120  boost::property_tree::read_info(filename, pt);
121 
122  double scaling = pt.get<double>(ns + matrixName + ".scaling", 1);
123 
124  for (size_t i = 0; i < rows; i++)
125  {
126  for (size_t j = 0; j < cols; j++)
127  {
128  matrix(i, j) =
129  scaling *
130  pt.get<double>(ns + matrixName + "." + "(" + std::to_string(i) + "," + std::to_string(j) + ")", 0.0);
131  }
132  }
133 }
134 }
135 }
void loadMatrix(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &ns="")
Loads a matrix/vector file from an .info file.
Definition: InfoFileParser.h:111
void loadScalar(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &ns="")
Loads a scalar file from an .info file.
Definition: InfoFileParser.h:31
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void loadScalarOptional(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const SCALAR &defaultValue, const std::string &ns="")
Tries to load a scalar from an .info file. Falls back to default value if not found.
Definition: InfoFileParser.h:51
for i