24 #include <yarp/math/Math.h> 26 #include "superqComputation.h" 36 vector<int> SpatialDensityFilter::filter(
const cv::Mat &data,
const double radius,
const int maxResults, deque<Vector> &points)
39 cv::flann::KDTreeIndexParams indexParams;
40 cv::flann::Index kdtree(data,indexParams);
42 cv::Mat query(1,data.cols,CV_32F);
43 cv::Mat indices,dists;
45 vector<int> res(data.rows);
47 for (
size_t i=0; i<res.size(); i++)
49 for (
int c=0; c<query.cols; c++)
50 query.at<
float>(0,c)=data.at<
float>(i,c);
52 res[i]=kdtree.radiusSearch(query,indices,dists,radius,maxResults,cv::flann::SearchParams(128));
55 if (res[i]>=maxResults)
57 point[0]=data.at<
float>(i,0);
58 point[1]=data.at<
float>(i,1);
59 point[2]=data.at<
float>(i,2);
60 points.push_back(point);
68 SuperqComputation::SuperqComputation(Mutex &_mutex_shared,
int _rate,
bool _filter_points,
bool _filter_superq,
bool _fixed_window,deque<yarp::sig::Vector> &_points,
string _tag_file,
double _threshold_median,
69 const Property &_filter_points_par, Vector &_x, Vector &_x_filtered,
const Property &_filter_superq_par,
const Property &_ipopt_par,
const string &_homeContextPath,
bool _save_points, ResourceFinder *_rf):
70 mutex_shared(_mutex_shared), filter_points(_filter_points), filter_superq(_filter_superq), fixed_window( _fixed_window),tag_file(_tag_file), threshold_median(_threshold_median), save_points(_save_points),
71 filter_points_par(_filter_points_par),filter_superq_par(_filter_superq_par),ipopt_par(_ipopt_par), Thread(), homeContextPath(_homeContextPath), x(_x), x_filtered(_x_filtered), points(_points), rf(_rf)
80 double radiusValue=newOptions.find(
"filter_radius").asDouble();
81 if (newOptions.find(
"filter_radius").isNull() && (first_time==
true))
85 else if (!newOptions.find(
"filter_radius").isNull())
87 if ((radiusValue>0.0000001) && (radiusValue<0.01))
91 else if ((radiusValue>=0.01))
95 else if ((radiusValue<=0.0000001))
101 int nnThreValue=newOptions.find(
"filter_nnThreshold").asInt();
102 if (newOptions.find(
"filter_nnThreshold").isNull() && (first_time==
true))
106 else if (!newOptions.find(
"filter_nnThreshold").isNull())
108 if ((nnThreValue>0) && (nnThreValue<100))
125 advOptions.put(
"filter_radius",
radius);
135 int mOrderValue=newOptions.find(
"median_order").asInt();
136 if (newOptions.find(
"median_order").isNull() && (first_time==
true))
140 else if (!newOptions.find(
"median_order").isNull())
142 if((mOrderValue>=1) && (mOrderValue<=50))
146 else if (mOrderValue<1)
150 else if (mOrderValue>50)
156 mOrderValue=newOptions.find(
"min_median_order").asInt();
157 if (newOptions.find(
"min_median_order").isNull() && (first_time==
true))
161 else if (!newOptions.find(
"min_median_order").isNull())
163 if ((mOrderValue>=1) && (mOrderValue<=50))
167 else if (mOrderValue<1)
171 else if (mOrderValue>50)
177 mOrderValue=newOptions.find(
"max_median_order").asInt();
178 if (newOptions.find(
"max_median_order").isNull() && (first_time==
true))
182 else if (!newOptions.find(
"max_median_order").isNull())
184 if ((mOrderValue>=1) && (mOrderValue<=50) && (mOrderValue>=
min_median_order))
192 else if (mOrderValue>50)
198 double threValue=newOptions.find(
"threshold_median").asDouble();
199 if (newOptions.find(
"threhsold_median").isNull() && (first_time==
true))
203 else if (!newOptions.find(
"threhsold_median").isNull())
205 if ((threValue>0.005) && (threValue<=2.0))
209 else if (threValue<0.005)
213 else if (threValue>2.0)
219 double minNormVel=newOptions.find(
"min_norm_vel").asDouble();
220 if (newOptions.find(
"min_norm_vel").isNull() && (first_time==
true))
224 else if (!newOptions.find(
"min_norm_vel").isNull())
226 if ((minNormVel>0.005) && (minNormVel<=0.1))
230 else if (minNormVel<0.005)
234 else if (minNormVel>0.1)
240 string par=newOptions.find(
"fixed_window").asString();
241 if (newOptions.find(
"fixed_window").isNull() && (first_time==
true))
245 else if (!newOptions.find(
"fixed_window").isNull())
247 if ((par==
"on") || (par==
"off"))
265 advOptions.put(
"fixed_window",
"on");
267 advOptions.put(
"fixed_window",
"off");
281 int points=newOptions.find(
"optimizer_points").asInt();
282 if (newOptions.find(
"optimizer_points").isNull() && (first_time==
true))
286 else if (!newOptions.find(
"optimizer_points").isNull())
302 double maxCpuTime=newOptions.find(
"max_cpu_time").asDouble();
303 if (newOptions.find(
"max_cpu_time").isNull() && (first_time==
true))
307 else if (!newOptions.find(
"max_cpu_time").isNull())
309 if ((maxCpuTime>=0.01) && (maxCpuTime<=10.0))
313 else if (maxCpuTime<0.01)
317 else if (maxCpuTime>10.0)
323 double tolValue=newOptions.find(
"tol").asDouble();
324 if (newOptions.find(
"tol").isNull() && (first_time==
true))
328 else if (!newOptions.find(
"tol").isNull())
330 if ((tolValue>1e-8) && (tolValue<=0.01))
334 else if (tolValue<1e-8)
338 else if (tolValue>0.01)
344 int accIter=newOptions.find(
"acceptable_iter").asInt();
345 if (newOptions.find(
"acceptable_iter").isNull() && (first_time==
true))
349 else if (!newOptions.find(
"acceptable_iter").isNull())
351 if ((accIter>=0 )&& (accIter<=10))
365 int maxIter=newOptions.find(
"max_iter").asInt();
366 if (newOptions.find(
"max_iter").isNull() && (first_time==
true))
370 else if (!newOptions.find(
"max_iter").isNull())
382 string mu_str=newOptions.find(
"mu_strategy").asString();
383 if (newOptions.find(
"mu_strategy").isNull() && (first_time==
true))
387 else if (!newOptions.find(
"mu_strategy").isNull())
389 if ((mu_str==
"adaptive") || (mu_str==
"monotone"))
399 string nlp=newOptions.find(
"nlp_scaling_method").asString();
400 if (newOptions.find(
"nlp_scaling_method").isNull() && (first_time==
true))
404 else if (!newOptions.find(
"nlp_scaling_method").isNull())
406 if ((nlp==
"none") || (nlp==
"gradient-based"))
425 advOptions.put(
"tol",
tol);
426 advOptions.put(
"max_iter",
max_iter);
436 if (par_name==
"tag_file")
438 else if (par_name==
"filter_points")
440 else if (par_name==
"filter_superq")
442 else if (par_name==
"save_points")
444 else if (par_name==
"one_shot")
445 one_shot=(value==
"on");
446 else if (par_name==
"object_class")
460 yInfo()<<
"[SuperqComputation]: Thread initing ... ";
476 yDebug()<<
"[SuperqComputation]: Resize of x";
479 yDebug()<<
"[SuperqComputation]: After resize of x";
489 yDebug()<<
"[SuperqComputation]: is stopping variable "<<isStopping();
490 while (!isStopping())
494 yDebug()<<
"[SuperqComputation]: points size "<<
points.size();
510 yInfo()<<
"[SuperqComputation]: number of points acquired:"<<
points.size();
516 yError(
"[SuperqComputation]: Not found a suitable superquadric! ");
518 else if (
go_on==
true && norm(
x)>0.0 && (
points.size()>0))
546 yInfo()<<
"[SuperComputation]: Thread releasing ... ";
574 pointPort.open(
"/superquadric-model/point:i");
590 if (Bottle *list=reply->get(0).asList())
592 for (
int i=0; i<list->size();i++)
594 if (Bottle *pp=list->get(i).asList())
597 tmp[0]=pp->get(0).asDouble();
598 tmp[1]=pp->get(1).asDouble();
599 tmp[2]=pp->get(2).asDouble();
606 yError()<<
"[SuperqComputation]: Some problems in blob pixels!";
612 yError()<<
"[SuperqComputation]: Some problem in object blob!";
617 yWarning(
"[SuperqComputation]: 3D points not received!");
629 string count_file_str=ss.str();
635 fout<<
points.size()<<
" 0 0"<<endl;
637 for (
size_t i=0; i<
points.size(); i++)
642 fout<<
points[i].subVector(0,2).toString(3,3)<<
" "<<r<<
" "<<g<<
" "<<b<<endl;
648 yError()<<
"[SuperqComputation]: Some problems in opening output file!";
665 if (!pointsFile.is_open())
667 yError()<<
"[SuperqComputation]: problem opening point cloud file!";
671 while (!pointsFile.eof())
673 getline(pointsFile,line);
675 Value firstItem=b.get(0);
676 bool isNumber=firstItem.isInt() || firstItem.isDouble();
680 string tmp=firstItem.asString().c_str();
681 std::transform(tmp.begin(),tmp.end(),tmp.begin(),::toupper);
682 if (tmp==
"OFF" || tmp==
"COFF")
689 nPoints=firstItem.asInt();
695 if (isNumber && (b.size()>=3))
698 point[0]=b.get(0).asDouble();
699 point[1]=b.get(1).asDouble();
700 point[2]=b.get(2).asDouble();
715 numVertices=
points.size();
717 cv:: Mat data(numVertices,3,CV_32F);
719 for (
int i=0; i<numVertices; i++)
722 data.at<
float>(i,0)=(
float)point[0];
723 data.at<
float>(i,1)=(
float)point[1];
724 data.at<
float>(i,2)=(
float)point[2];
729 yInfo()<<
"[SuperqComputation]: Processing points...";
730 double t0=yarp::os::Time::now();
732 double t1=yarp::os::Time::now();
733 yInfo()<<
"[SuperqComputation]: Processed in "<<1e3*(t1-t0)<<
" [ms]";
735 Vector colors(3,0.0);
744 Vector colors(3,0.0);
749 Ipopt::SmartPtr<Ipopt::IpoptApplication> app=
new Ipopt::IpoptApplication;
750 app->Options()->SetNumericValue(
"tol",
tol);
752 app->Options()->SetStringValue(
"mu_strategy",
mu_strategy);
753 app->Options()->SetIntegerValue(
"max_iter",
max_iter);
754 app->Options()->SetNumericValue(
"max_cpu_time",
max_cpu_time);
756 app->Options()->SetStringValue(
"hessian_approximation",
"limited-memory");
757 app->Options()->SetIntegerValue(
"print_level",0);
767 double t0_superq=Time::now();
769 yDebug()<<
"[SuperqComputation]: Start IPOPT ";
771 Ipopt::ApplicationReturnStatus status=app->OptimizeTNLP(GetRawPtr(superQ_nlp));
773 yDebug()<<
"[SuperqComputation]: Finish IPOPT ";
775 double t_s=Time::now()-t0_superq;
777 if (status==Ipopt::Solve_Succeeded)
779 x=superQ_nlp->get_result();
780 yInfo(
"[SuperqComputation]: Solution of the optimization problem: %s",
x.toString(3,3).c_str());
781 yInfo(
"[SuperqComputation]: Execution time : %f", t_s);
784 else if(status==Ipopt::Maximum_CpuTime_Exceeded)
786 x=superQ_nlp->get_result();
787 yWarning(
"[SuperqComputation]: Solution after maximum time exceeded: %s",
x.toString(3,3).c_str());
800 yInfo()<<
"[SuperqComputation]: Filtering the last "<<
median_order <<
" superquadrics...";
802 yInfo()<<
"[SuperqComputation]: x "<<
x.toString();
830 yInfo()<<
"[SuperqComputation]: Filtered superq "<<
x_filtered.toString(3,3);
845 elem_x.resize(3,0.0);
846 elem_x=
x.subVector(5,7);
847 yInfo()<<
"[SuperqComputation]: Old median order "<<
median_order;
849 AWPolyElement el(elem_x,Time::now());
850 Vector vel=
PolyEst->estimate(el);
851 yInfo()<<
"[SuperqComputation]: Velocity estimate "<<
PolyEst->estimate(el).toString();
871 if (filtered_superq==
false)
884 for (
int i=0; i<p.size(); i++)
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.
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.
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.
std::string ob_class
Objec class: cylinder, sphere of box.
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.
This class solves the optimization problem with the Ipopt software package and returns the estiamted ...
std::string nlp_scaling_method
NLP scaling method of the Ipopt algorithm.
yarp::os::Property getSuperqFilterPar()
Get options used for filtering the superquadric.
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.
void init()
Init function.
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.