superquadric-model
superquadric.h
1 /*
2  * Copyright (C) 2015 iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Giulia Vezzani
4  * email: giulia.vezzani@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
18 #ifndef __SUPERQUADRIC_H__
19 #define __SUPERQUADRIC_H__
20 
21 #include <string>
22 #include <deque>
23 #include <map>
24 
25 #include <IpTNLP.hpp>
26 #include <IpIpoptApplication.hpp>
27 #include <IpReturnCodes.hpp>
28 
29 #include <yarp/os/all.h>
30 #include <yarp/sig/all.h>
31 
36 class SuperQuadric_NLP : public Ipopt::TNLP
37 {
38 
39 protected:
43  yarp::sig::Vector x_v;
45  yarp::sig::Vector x0;
47  yarp::sig::Matrix bounds;
48  double aux_objvalue;
50  std::string obj_class;
51 
52  yarp::os::ResourceFinder *rf;
53 
62  /****************************************************************/
63  bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m,Ipopt::Index &nnz_jac_g,
64  Ipopt::Index &nnz_h_lag, Ipopt::TNLP::IndexStyleEnum &index_style);
65 
67  /****************************************************************/
68  void computeBounds();
69 
79  /****************************************************************/
80  bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u,
81  Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u);
82 
83 
96  /****************************************************************/
97  bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x,
98  bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U,
99  Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda);
100 
108  /****************************************************************/
109  bool eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x,
110  Ipopt::Number &obj_value);
111 
118  /****************************************************************/
119  void F(const Ipopt::Number *x, std::deque<yarp::sig::Vector> &points, bool &new_x);
120 
126  /****************************************************************/
127  double f(const Ipopt::Number *x, const yarp::sig::Matrix &R, const yarp::sig::Vector &point_cloud);
128 
134  /****************************************************************/
135  double F_v(const yarp::sig::Vector &x, const std::deque<yarp::sig::Vector> &points);
136 
142  /****************************************************************/
143  double f_v(const yarp::sig::Vector &x, const yarp::sig::Matrix &R, const yarp::sig::Vector &point_cloud);
144 
151  /****************************************************************/
152  bool eval_grad_f(Ipopt::Index n, const Ipopt::Number* x, bool new_x,
153  Ipopt::Number *grad_f);
154 
163  /****************************************************************/
164  bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x,
165  Ipopt::Index m, Ipopt::Number *g);
166 
177  /****************************************************************/
178  bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x,
179  Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow,
180  Ipopt::Index *jCol, Ipopt::Number *values);
181 
186  /****************************************************************/
187  void computeX0(yarp::sig::Vector &x0, std::deque<yarp::sig::Vector> &point_cloud);
188 
193  /****************************************************************/
194  void computeInitialOrientation(yarp::sig::Vector &x0, std::deque<yarp::sig::Vector> &point_cloud);
195 
201  /****************************************************************/
202  yarp::sig::Matrix computeBoundingBox(std::deque<yarp::sig::Vector> &points, const yarp::sig::Vector &x0);
203 
214  /****************************************************************/
215  void finalize_solution(Ipopt::SolverReturn status, Ipopt::Index n,
216  const Ipopt::Number *x, const Ipopt::Number *z_L,
217  const Ipopt::Number *z_U, Ipopt::Index m,
218  const Ipopt::Number *g, const Ipopt::Number *lambda,
219  Ipopt::Number obj_value, const Ipopt::IpoptData *ip_data,
220  Ipopt::IpoptCalculatedQuantities *ip_cq);
221 
222 
223 public:
225  yarp::sig::Vector solution;
227  std::deque<yarp::sig::Vector> points_downsampled;
228 
230  /****************************************************************/
231  void init();
232 
237  /****************************************************************/
238  void setPoints(const std::deque<yarp::sig::Vector> &point_cloud, const int &optimizer_points);
239 
245  /****************************************************************/
246  void configure(yarp::os::ResourceFinder *rf, bool bounds_aut, const std::string &object_class);
247 
251  /****************************************************************/
252  yarp::sig::Vector get_result() const;
253 
261  /****************************************************************/
262  bool readMatrix(const std::string &tag, yarp::sig::Matrix &matrix, const int &dimension, yarp::os::ResourceFinder *rf);
263 
264 };
265 
266 #endif
267 
yarp::sig::Vector solution
Final solution.
Definition: superquadric.h:225
double f_v(const yarp::sig::Vector &x, const yarp::sig::Matrix &R, const yarp::sig::Vector &point_cloud)
Auxiliary function for computing cost function of the nonlinear problem to be solved with ipopt...
std::string obj_class
Object class: cylinder, sphere and box.
Definition: superquadric.h:50
void computeBounds()
Compute bounds variable from the point cloud for speeding up optimization.
bool bounds_automatic
Boolean variable for enabling automatic bounds computation.
Definition: superquadric.h:41
yarp::sig::Matrix bounds
Bounds variable of the optimization problem.
Definition: superquadric.h:47
void F(const Ipopt::Number *x, std::deque< yarp::sig::Vector > &points, bool &new_x)
Auxiliary function for computing cost function of the nonlinear problem to be solved with ipopt...
bool get_starting_point(Ipopt::Index n, bool init_x, Ipopt::Number *x, bool init_z, Ipopt::Number *z_L, Ipopt::Number *z_U, Ipopt::Index m, bool init_lambda, Ipopt::Number *lambda)
Get the starting point for the nonlinear problem to be solved with ipopt.
bool eval_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number &obj_value)
Cost function of the nonlinear problem to be solved with ipopt.
bool get_bounds_info(Ipopt::Index n, Ipopt::Number *x_l, Ipopt::Number *x_u, Ipopt::Index m, Ipopt::Number *g_l, Ipopt::Number *g_u)
Get variable bounds for the nonlinear problem to be solved with ipopt.
void setPoints(const std::deque< yarp::sig::Vector > &point_cloud, const int &optimizer_points)
Set point to be used for superquadric estimation.
void computeInitialOrientation(yarp::sig::Vector &x0, std::deque< yarp::sig::Vector > &point_cloud)
Compute initial superquadric orientation from the point cloud.
bool eval_grad_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number *grad_f)
Gradient of the cost function of the nonlinear problem.
yarp::sig::Vector get_result() const
Extract the solution.
bool readMatrix(const std::string &tag, yarp::sig::Matrix &matrix, const int &dimension, yarp::os::ResourceFinder *rf)
Function for reading matrices from config files.
yarp::sig::Vector x_v
Auxiliar vector for gradient computation.
Definition: superquadric.h:43
double f(const Ipopt::Number *x, const yarp::sig::Matrix &R, const yarp::sig::Vector &point_cloud)
Auxiliary function for computing cost function of the nonlinear problem to be solved with ipopt...
std::deque< yarp::sig::Vector > points_downsampled
3D points actually used for superquadric estimation
Definition: superquadric.h:227
yarp::sig::Matrix computeBoundingBox(std::deque< yarp::sig::Vector > &points, const yarp::sig::Vector &x0)
Compute bounding box from the point cloud.
void configure(yarp::os::ResourceFinder *rf, bool bounds_aut, const std::string &object_class)
Configure function.
yarp::sig::Vector x0
Starting point for the optimization problem.
Definition: superquadric.h:45
void computeX0(yarp::sig::Vector &x0, std::deque< yarp::sig::Vector > &point_cloud)
Compute a good starting point for the nonlinear problem.
bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, Ipopt::TNLP::IndexStyleEnum &index_style)
Get info for the nonlinear problem to be solved with ipopt.
This class solves the optimization problem with the Ipopt software package and returns the estiamted ...
Definition: superquadric.h:36
void finalize_solution(Ipopt::SolverReturn status, Ipopt::Index n, const Ipopt::Number *x, const Ipopt::Number *z_L, const Ipopt::Number *z_U, Ipopt::Index m, const Ipopt::Number *g, const Ipopt::Number *lambda, Ipopt::Number obj_value, const Ipopt::IpoptData *ip_data, Ipopt::IpoptCalculatedQuantities *ip_cq)
Finalize the solution.
bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Jacobian of the constraints of the nonlinear problem.
bool eval_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Number *g)
Constraints of the nonlinear problem.
void init()
Init function.
double F_v(const yarp::sig::Vector &x, const std::deque< yarp::sig::Vector > &points)
Auxiliary function for computing the gradient of cost function of the nonlinear problem.