superquadric-model
superqComputation.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 __COMPUTATION_H__
19 #define __COMPUTATION_H__
20 
21 #include <yarp/dev/all.h>
22 
23 #include <opencv2/opencv.hpp>
24 
25 #include <iCub/ctrl/filters.h>
26 #include <iCub/ctrl/adaptWinPolyEstimator.h>
27 
28 #include "superquadric.h"
29 
33 /*******************************************************************************/
35 {
36 public:
37 
38  /*******************************************************************************/
39  static std::vector<int> filter(const cv::Mat &data,const double radius, const int maxResults, std::deque<yarp::sig::Vector> &points);
40 };
41 
47 /*******************************************************************************/
48 class SuperqComputation : public yarp::os::Thread
49 {
50 protected:
51 
53  int count;
57  std::string tag_file;
59  std::string homeContextPath;
61  yarp::os::ConstString pointCloudFileName;
63  std::vector<cv::Point> contour;
64 
66  yarp::os::BufferedPort<yarp::os::Bottle> pointPort;
67 
68  // Filters parameters
70  double radius;
73  int numVertices;
83  bool fixed_window;
89  double min_norm_vel;
90 
91  // On/off parameters
93  bool go_on;
94  bool one_shot;
95 
96  // Options for optimization with ipopt
98  double tol;
99  double sum;
101  double max_cpu_time;
105  int max_iter;
111  std::string mu_strategy;
113  std::string nlp_scaling_method;
114  yarp::sig::Vector elem_x;
115 
117  double t_superq;
118  int count_file;
120  std::string ob_class;
121 
122  yarp::os::ResourceFinder *rf;
123  double t,t0;
124  yarp::os::Mutex mutex;
125  yarp::os::Mutex &mutex_shared;
126 
127  // Classes uses for filtering the superquadric
129  iCub::ctrl::MedianFilter *mFilter;
131  iCub::ctrl::AWPolyEstimator *PolyEst;
132 
133  // Properties with all the options
135  yarp::os::Property filter_points_par;
137  yarp::os::Property filter_superq_par;
139  yarp::os::Property ipopt_par;
140 public:
141 
146 
148  yarp::sig::Vector &x;
150  yarp::sig::Vector &x_filtered;
152  std::deque<yarp::sig::Vector> &points;
154  std::deque<cv::Point> blob_points;
155 
156  /***********************************************************************/
157  SuperqComputation(yarp::os::Mutex &mutex_shared, int _rate, bool _filter_points, bool _filter_superq, bool _fixed_window, std::deque<yarp::sig::Vector> &_points,
158  std::string _tag_file, double _threshold_median, const yarp::os::Property &filters_points_par, yarp::sig::Vector &_x, yarp::sig::Vector &_x_filtered,
159  const yarp::os::Property &filters_superq_par, const yarp::os::Property &optimizer_par, const std::string &_homeContextPath, bool _save_points, yarp::os::ResourceFinder *rf);
160 
165  /***********************************************************************/
166  void setPointsFilterPar(const yarp::os::Property &newOptions, bool first_time);
167 
172  /***********************************************************************/
173  void setSuperqFilterPar(const yarp::os::Property &newOptions, bool first_time);
174 
179  /***********************************************************************/
180  void setIpoptPar(const yarp::os::Property &newOptions, bool first_time);
181 
185  /***********************************************************************/
186  yarp::os::Property getPointsFilterPar();
187 
192  yarp::os::Property getSuperqFilterPar();
193 
198  yarp::os::Property getIpoptPar();
199 
204  /***********************************************************************/
205  void setPar(const std::string &par_name, const std::string &value);
206 
208  /***********************************************************************/
209  virtual bool threadInit();
210 
212  /***********************************************************************/
213  virtual void run();
214 
216  /***********************************************************************/
217  virtual void threadRelease();
218 
223  /***********************************************************************/
224  void savePoints(const std::string &namefile, const yarp::sig::Vector &colors);
225 
227  /***********************************************************************/
228  bool readPointCloud();
229 
231  /***********************************************************************/
232  void filter();
233 
237  /***********************************************************************/
238  bool computeSuperq();
239 
241  /***********************************************************************/
242  void filterSuperq();
243 
245  /***********************************************************************/
246  void resetMedianFilter();
247 
249  /***********************************************************************/
250  int adaptWindComputation();
251 
253  /***********************************************************************/
254  bool configFilterSuperq();
255 
257  /***********************************************************************/
258  bool config3Dpoints();
259 
264  /***********************************************************************/
265  yarp::sig::Vector getSolution(bool filtered_or_not);
266 
270  /***********************************************************************/
271  void sendPoints(const std::deque<yarp::sig::Vector> &p);
272 
274  /***********************************************************************/
275  void getPoints3D();
276 
280  /***********************************************************************/
281  double getTime();
282 };
283 
284 #endif
285 
286 
yarp::os::Property getPointsFilterPar()
Get options used for filtering the point cloud.
bool bounds_automatic
Boolean varibal for enabling automatic computation of variables bounds.
void getPoints3D()
Get the point cloud for computing the superquadric.
double max_cpu_time
Max cpu time allowed for solving the optimization problem.
bool configFilterSuperq()
Configure superquadric filter.
bool filter_points
Boolean variable for enabling point cloud filtering.
bool go_on
Boolean variable for going to the next step of the state machine.
iCub::ctrl::MedianFilter * mFilter
Median filter for discarding wrong superquadrics.
double radius
Radius for spatial density filter.
std::string tag_file
Tag name of files for saving 3D points.
bool filter_superq
Boolean variable for enabling superquadric filtering.
double min_norm_vel
Minimum norm of velocity for considering the object moving.
double t_superq
Time required for computing superquadric.
bool readPointCloud()
Read the object point cloud from txt for offline tests.
std::deque< cv::Point > blob_points
Object 2D blob.
double tol
Tolerance of the optimization problem.
yarp::os::Property filter_points_par
Parameters of point cloud filter.
std::string mu_strategy
Mu strategy of the Ipopt algorithm.
void setSuperqFilterPar(const yarp::os::Property &newOptions, bool first_time)
Set options for filtering the superquadric.
yarp::os::Property filter_superq_par
Parameters of superquadric filter.
bool config3Dpoints()
Configure point cloud filter.
virtual void threadRelease()
Release function of Thread class.
yarp::sig::Vector & x_filtered
Filtered object superquadric.
double getTime()
Return the computation time for estimating the superquadric.
This class filters the point cloud according to its density.
void filterSuperq()
Filter the superquadri with the median filter.
void filter()
Filter the received point cloud.
bool save_points
Boolean variable for enabling point cloud saving.
void setPar(const std::string &par_name, const std::string &value)
Set a parameter equal to a value.
std::deque< yarp::sig::Vector > & points
Object point cloud.
int max_median_order
max median order width
int max_iter
Maximum iteration allowed of Ipopt algorithm.
int std_median_order
median order width
void setPointsFilterPar(const yarp::os::Property &newOptions, bool first_time)
Set options for filtering the point cloud.
yarp::sig::Vector getSolution(bool filtered_or_not)
Return the computed superquadric.
virtual void run()
Run function of Thread class.
int nnThreshold
Density threshold for spatial density filter.
void savePoints(const std::string &namefile, const yarp::sig::Vector &colors)
Save point cloud used for reconstructing the superquadric.
int count
Count variable.
std::string ob_class
Objec class: cylinder, sphere of box.
This class provides a thread for computing in real time the superquadric, filtering the point cloud a...
std::string homeContextPath
Path where code context is located.
int adaptWindComputation()
Compute window size according to the object velocity.
int min_median_order
Minimum median filder order allowed.
bool fixed_window
Boolean variable for enabling the use of a fixed window during the median filter. ...
iCub::ctrl::AWPolyEstimator * PolyEst
Time Polyestimator for estimating object velocity.
double threshold_median
Threshold for velocity estimation for median order.
int optimizer_points
Number of 3D points used for optimization.
std::string nlp_scaling_method
NLP scaling method of the Ipopt algorithm.
yarp::os::Property getSuperqFilterPar()
Get options used for filtering the superquadric.
std::vector< cv::Point > contour
OpenCV variable for blob extraction.
void setIpoptPar(const yarp::os::Property &newOptions, bool first_time)
Set options for the optimization problem solved by Ipopt.
yarp::os::ConstString pointCloudFileName
Pointcloud name file in case the module runs offline.
yarp::os::Property ipopt_par
Parameters of the Ipopt optimization problem.
bool computeSuperq()
Compute the superquadric modeling the object.
void resetMedianFilter()
Reset median filter.
void sendPoints(const std::deque< yarp::sig::Vector > &p)
Get the point cloud for computing the superquadric.
int new_median_order
New median filder order estimated.
yarp::os::Property getIpoptPar()
Get options used for solving the optimization problem with Ipopt.
int acceptable_iter
Acceptable iter of Ipopt algorithm.
yarp::os::BufferedPort< yarp::os::Bottle > pointPort
Port for receiving point cloud.
yarp::sig::Vector & x
Object superquadric.
virtual bool threadInit()
Init function of Thread class.
int median_order
Median filder order.