model-fixture.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2025 CNRS INRIA
3 //
4 
5 #ifndef __pinocchio_benchmark_model_fixture_hpp__
6 #define __pinocchio_benchmark_model_fixture_hpp__
7 
11 
13 
14 #include <boost/optional.hpp>
15 #include <boost/none.hpp>
16 
17 #include <benchmark/benchmark.h>
18 
19 #include <Eigen/Core>
20 
21 #include <iostream>
22 
24 struct ExtraArgs
25 {
26  bool with_ff = true;
27  std::string model_filename;
28 
30  : model_filename(PINOCCHIO_MODEL_DIR + std::string("/simple_humanoid.urdf"))
31  {
32  }
33 };
34 
37 struct ModelFixture : benchmark::Fixture
38 {
39  void SetUp(benchmark::State &)
40  {
41  model = MODEL;
43 
44  const Eigen::VectorXd qmax(Eigen::VectorXd::Ones(model.nq));
45  q = randomConfiguration(model, -qmax, qmax);
46  v = Eigen::VectorXd::Random(model.nv);
47  a = Eigen::VectorXd::Random(model.nv);
48  tau = Eigen::VectorXd::Random(model.nv);
49  }
50 
51  void TearDown(benchmark::State &)
52  {
53  }
54 
55  static void GlobalSetUp(const ExtraArgs & extra_args)
56  {
57  if (extra_args.model_filename == "HS")
58  {
60  }
61  else if (extra_args.with_ff)
62  {
64  extra_args.model_filename,
68  }
69  else
70  {
72  }
73  std::cout << "nq = " << ModelFixture::MODEL.nq << std::endl;
74  std::cout << "nv = " << ModelFixture::MODEL.nv << std::endl;
75  std::cout << "name = " << ModelFixture::MODEL.name << std::endl;
76  std::cout << "--" << std::endl;
77  }
78 
81  Eigen::VectorXd q;
82  Eigen::VectorXd v;
83  Eigen::VectorXd a;
84  Eigen::VectorXd tau;
85 
87 };
88 
89 // Parse --no-ff and --model arguments
90 inline boost::optional<ExtraArgs> parseExtraArgs(int argc, char ** argv)
91 {
93  for (int i = 1; i < argc; ++i)
94  {
95  if (std::strcmp(argv[i], "--no-ff") == 0)
96  {
97  args.with_ff = false;
98  }
99  else if (std::strcmp(argv[i], "--model") == 0)
100  {
101  if (argc > (i + 1))
102  {
103  args.model_filename = argv[i + 1];
104  --argc;
105  }
106  else
107  {
108  std::cerr
109  << argv[0]
110  << ": error: unrecognized command-line flag: --model should be followed by an urdf path"
111  << std::endl;
112  return boost::none;
113  }
114  }
115  else
116  {
117  std::cerr << argv[0] << ": error: unrecognized command-line flag: " << argv[i] << std::endl;
118  return boost::none;
119  }
120  }
121  return args;
122 }
123 
124 // BENCHMARK_MAIN() macro expansion edited to take some extra argument
125 // GLOBAL_SETUP is a function to setup global variable (void(const ExtraArgs& args)).
126 #define PINOCCHIO_BENCHMARK_MAIN_WITH_SETUP(GLOBAL_SETUP) \
127  pinocchio::Model ModelFixture::MODEL; \
128  int main(int argc, char ** argv) \
129  { \
130  char arg0_default[] = "benchmark"; \
131  char * args_default = arg0_default; \
132  if (!argv) \
133  { \
134  argc = 1; \
135  argv = &args_default; \
136  } \
137  ::benchmark::Initialize(&argc, argv); \
138  \
139  /* It's important to report errors so typos on google benchmark arguments */ \
140  /* are detected. */ \
141  auto extra_args = parseExtraArgs(argc, argv); \
142  if (!extra_args) \
143  { \
144  return 1; \
145  } \
146  GLOBAL_SETUP(*extra_args); \
147  \
148  ::benchmark ::RunSpecifiedBenchmarks(); \
149  ::benchmark ::Shutdown(); \
150  return 0; \
151  } \
152  int main(int, char **)
153 
154 // BENCHMARK_MAIN() macro expansion edited to take some extra argument
155 #define PINOCCHIO_BENCHMARK_MAIN() PINOCCHIO_BENCHMARK_MAIN_WITH_SETUP(ModelFixture::GlobalSetUp)
156 
157 #endif // #ifndef __pinocchio_benchmark_model_fixture_hpp__
ModelFixture::v
Eigen::VectorXd v
Definition: model-fixture.hpp:82
pinocchio::DataTpl
Definition: context/generic.hpp:25
ModelFixture::model
pinocchio::Model model
Definition: model-fixture.hpp:79
ModelFixture::TearDown
void TearDown(benchmark::State &)
Definition: model-fixture.hpp:51
ModelFixture::a
Eigen::VectorXd a
Definition: model-fixture.hpp:83
ModelFixture
Definition: model-fixture.hpp:37
inverse-kinematics.i
int i
Definition: inverse-kinematics.py:17
model.hpp
ExtraArgs::with_ff
bool with_ff
Definition: model-fixture.hpp:26
pinocchio::JointModelFreeFlyerTpl
Definition: multibody/joint/fwd.hpp:110
ModelFixture::data
pinocchio::Data data
Definition: model-fixture.hpp:80
pinocchio::buildModels::humanoidRandom
void humanoidRandom(ModelTpl< Scalar, Options, JointCollectionTpl > &model, bool usingFF=true, bool mimic=false)
Create a humanoid kinematic tree with 6-DOF limbs and random joint placements.
pinocchio::ModelTpl::name
std::string name
Model name.
Definition: multibody/model.hpp:220
parseExtraArgs
boost::optional< ExtraArgs > parseExtraArgs(int argc, char **argv)
Definition: model-fixture.hpp:90
pinocchio::randomConfiguration
void randomConfiguration(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const Eigen::MatrixBase< ConfigVectorIn1 > &lowerLimits, const Eigen::MatrixBase< ConfigVectorIn2 > &upperLimits, const Eigen::MatrixBase< ReturnType > &qout)
Generate a configuration vector uniformly sampled among provided limits.
Definition: joint-configuration.hpp:315
ModelFixture::SetUp
void SetUp(benchmark::State &)
Definition: model-fixture.hpp:39
args
args
ModelFixture::GlobalSetUp
static void GlobalSetUp(const ExtraArgs &extra_args)
Definition: model-fixture.hpp:55
pinocchio::Data
DataTpl< context::Scalar, context::Options > Data
Definition: multibody/fwd.hpp:34
urdf.hpp
data.hpp
ExtraArgs::ExtraArgs
ExtraArgs()
Definition: model-fixture.hpp:29
ModelFixture::MODEL
static pinocchio::Model MODEL
Definition: model-fixture.hpp:86
pinocchio::context::Options
@ Options
Definition: context/generic.hpp:82
std
Definition: autodiff/casadi/utils/static-if.hpp:64
pinocchio::ModelTpl::nv
int nv
Dimension of the velocity vector space.
Definition: multibody/model.hpp:101
ExtraArgs::model_filename
std::string model_filename
Definition: model-fixture.hpp:27
ModelFixture::tau
Eigen::VectorXd tau
Definition: model-fixture.hpp:84
sample-models.hpp
pinocchio::ModelTpl< context::Scalar, context::Options >
ModelFixture::q
Eigen::VectorXd q
Definition: model-fixture.hpp:81
pinocchio::context::Scalar
PINOCCHIO_SCALAR_TYPE Scalar
Definition: context/generic.hpp:79
pinocchio::ModelTpl::nq
int nq
Dimension of the configuration vector representation.
Definition: multibody/model.hpp:98
ExtraArgs
Store custom command line arguments.
Definition: model-fixture.hpp:24
PINOCCHIO_MODEL_DIR
#define PINOCCHIO_MODEL_DIR
Definition: build-reduced-model.cpp:11
pinocchio::urdf::buildModel
ModelTpl< Scalar, Options, JointCollectionTpl > & buildModel(const std::string &filename, const typename ModelTpl< Scalar, Options, JointCollectionTpl >::JointModel &rootJoint, const std::string &rootJointName, ModelTpl< Scalar, Options, JointCollectionTpl > &model, const bool verbose=false, const bool mimic=false)
Build the model from a URDF file with a particular joint as root of the model tree inside the model g...


pinocchio
Author(s):
autogenerated on Wed Apr 16 2025 02:41:50