superquadric-model
superqVisualization.cpp
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 #include <yarp/math/Math.h>
19 
20 #include "superqVisualization.h"
21 
22 using namespace std;
23 using namespace yarp::os;
24 using namespace yarp::sig;
25 using namespace yarp::dev;
26 using namespace yarp::math;
27 
28 /***********************************************************************/
29 SuperqVisualization::SuperqVisualization(int _rate,const string &_eye, const string &_what_to_plot, Vector &_x, Vector &_x_filtered,
30  deque<int> &_Color,IGazeControl *_igaze, const Matrix _K, deque<Vector> &_points,
31  const int &_vis_points, const int &_vis_step):
32  RateThread(_rate), eye(_eye), what_to_plot(_what_to_plot), Color(_Color), igaze(_igaze), K(_K),
33  vis_points(_vis_points), vis_step(_vis_step), superq(_x), superq_filtered(_x_filtered), points(_points)
34 {
35 
36 }
37 
38 /***********************************************************************/
39 bool SuperqVisualization::showSuperq(Vector &x_toshow)
40 {
41  LockGuard lg(mutex);
42 
43  PixelRgb color(Color[0],Color[1],Color[2]);
44  Vector pos, orient;
45  double co,so,ce,se;
46  Stamp *stamp=NULL;
47 
48  ImageOf<PixelRgb> &imgOut=portImgOut.prepare();
49  imgOut=*imgIn;
50 
51  R=euler2dcm(x_toshow.subVector(8,10));
52  R=R.transposed();
53 
54  if ((norm(x_toshow)>0.0))
55  {
56  if (eye=="left")
57  {
58  if (igaze->getLeftEyePose(pos,orient,stamp))
59  {
60  H=axis2dcm(orient);
61  H.setSubcol(pos,0,3);
62  H=SE3inv(H);
63  }
64  }
65  else
66  {
67  if (igaze->getRightEyePose(pos,orient,stamp))
68  {
69  H=axis2dcm(orient);
70  H.setSubcol(pos,0,3);
71  H=SE3inv(H);
72  }
73  }
74 
75  double step=2*M_PI/vis_points;
76 
77  for (double eta=-M_PI; eta<M_PI; eta+=step)
78  {
79  for (double omega=-M_PI; omega<M_PI;omega+=step)
80  {
81  co=cos(omega); so=sin(omega);
82  ce=cos(eta); se=sin(eta);
83 
84  point[0]=x_toshow[0] * sign(ce)*(pow(abs(ce),x_toshow[3])) * sign(co)*(pow(abs(co),x_toshow[4])) * R(0,0) +
85  x_toshow[1] * sign(ce)*(pow(abs(ce),x_toshow[3]))* sign(so)*(pow(abs(so),x_toshow[4])) * R(0,1)+
86  x_toshow[2] * sign(se)*(pow(abs(se),x_toshow[3])) * R(0,2) + x_toshow[5];
87 
88  point[1]=x_toshow[0] * sign(ce)*(pow(abs(ce),x_toshow[3])) * sign(co)*(pow(abs(co),x_toshow[4])) * R(1,0) +
89  x_toshow[1] * sign(ce)*(pow(abs(ce),x_toshow[3])) * sign(so)*(pow(abs(so),x_toshow[4])) * R(1,1)+
90  x_toshow[2] * sign(se)*(pow(abs(se),x_toshow[3])) * R(1,2) + x_toshow[6];
91 
92  point[2]=x_toshow[0] * sign(ce)*(pow(abs(ce),x_toshow[3])) * sign(co)*(pow(abs(co),x_toshow[4])) * R(2,0) +
93  x_toshow[1] * sign(ce)*(pow(abs(ce),x_toshow[3])) * sign(so)*(pow(abs(so),x_toshow[4])) * R(2,1)+
94  x_toshow[2] * sign(se)*(pow(abs(se),x_toshow[3])) * R(2,2) + x_toshow[7];
95 
96  point2D=from3Dto2D(point);
97 
98  cv::Point target_point((int)point2D[0],(int)point2D[1]);
99 
100  if ((target_point.x<0) || (target_point.y<0) || (target_point.x>=320) || (target_point.y>=240))
101  {
102  yWarning("[SuperqVisualization]: Not acceptable pixels!");
103  }
104  else
105  imgOut.pixel(target_point.x, target_point.y)=color;
106  }
107  }
108  }
109 
110  portImgOut.write();
111 
112  return true;
113 
114 }
115 
116 /***********************************************************************/
118 {
119  PixelRgb color(Color[0],Color[1],Color[2]);
120  Stamp *stamp=NULL;
121  Vector pos, orient;
122 
123  ImageOf<PixelRgb> &imgOut=portImgOut.prepare();
124  imgOut=*imgIn;
125 
126  if (eye=="left")
127  {
128  if (igaze->getLeftEyePose(pos,orient,stamp))
129  {
130  H=axis2dcm(orient);
131  H.setSubcol(pos,0,3);
132  H=SE3inv(H);
133  }
134  }
135  else
136  {
137  if (igaze->getRightEyePose(pos,orient,stamp))
138  {
139  H=axis2dcm(orient);
140  H.setSubcol(pos,0,3);
141  H=SE3inv(H);
142  }
143  }
144 
145  Vector point(3,0.0);
146 
147  for (size_t i=0; i<points.size(); i+=vis_step)
148  {
149  point=points[i].subVector(0,2);
150  point2D=from3Dto2D(point);
151 
152  cv::Point target_point((int)point2D[0],(int)point2D[1]);
153 
154  if ((target_point.x<0) || (target_point.y<0) || (target_point.x>=320) || (target_point.y>=240))
155  {
156  yWarning("[SuperqVisualization]: Not acceptable pixels!");
157  }
158  else
159  imgOut.pixel(target_point.x, target_point.y)=color;
160  }
161 
162  portImgOut.write();
163 
164  return true;
165 }
166 
167 /*******************************************************************************/
168 Vector SuperqVisualization::from3Dto2D(const Vector &point3D)
169 {
170  Vector point2D(3,0.0);
171  Vector point_aux(4,1.0);
172  point_aux.setSubvector(0,point3D);
173  point2D=K*H*point_aux;
174  return point2D.subVector(0,1)/point2D[2];
175 }
176 
177 /***********************************************************************/
179 {
180  yInfo()<<"[SuperqVisualization]: Thread initing ... ";
181 
182  portImgIn.open("/superquadric-model/img:i");
183  portImgOut.open("/superquadric-model/img:o");
184 
185  R.resize(4,4);
186  H.resize(4,4);
187  point2D.resize(2,0.0);
188  point.resize(3,0.0);
189  point1.resize(3,0.0);
190 
191  return true;
192 }
193 
194 /***********************************************************************/
196 {
197  double t0=Time::now();
198  if (imgIn=portImgIn.read())
199  {
200  if (what_to_plot=="superq")
202  else if ((what_to_plot=="points") && (points.size()>0))
203  showPoints();
204  }
205  t_vis=Time::now()-t0;
206 }
207 
208 /**********************************************************************/
210 {
211  portImgIn.interrupt();
212  portImgOut.interrupt();
213 }
214 
215 /**********************************************************************/
217 {
218  yInfo()<<"[SuperVisualization]: Thread releasing ... ";
219 
220  if (!portImgIn.isClosed())
221  portImgIn.close();
222 
223  if (!portImgOut.isClosed())
224  portImgOut.close();
225 }
226 
227 /***********************************************************************/
228 void SuperqVisualization::setPar(const Property &newOptions, bool first_time)
229 {
230  LockGuard lg(mutex);
231  int v_points=newOptions.find("visualized_points").asInt();
232  if (newOptions.find("visualized_points").isNull() && (first_time==true))
233  {
234  vis_points=3;
235  }
236  else if (!newOptions.find("visualized_points").isNull())
237  {
238  if ((v_points>=1) && (v_points<=300))
239  {
240  vis_points=v_points;
241  }
242  else
243  {
244  vis_points=3;
245  }
246  }
247 
248  string plot=newOptions.find("what_to_plot").asString();
249  if (newOptions.find("what_to_plot").isNull() && (first_time==true))
250  {
251  what_to_plot="superq";
252  }
253  else if (!newOptions.find("what_to_plot").isNull())
254  {
255  if ((plot=="superq") || (plot=="points"))
256  {
257  what_to_plot=plot;
258  }
259  else
260  {
261  what_to_plot="superq";
262  }
263  }
264 
265  int vpoint=newOptions.find("visualized_points_step").asInt();
266  if (newOptions.find("visualized_points_step").isNull() && (first_time==true))
267  {
268  vis_step=10;
269  }
270  else if (!newOptions.find("visualized_points_step").isNull())
271  {
272  if ((vpoint>=10) && (vpoint<=100))
273  {
274  vis_step=vpoint;
275  }
276  else if (vpoint<10)
277  {
278  vis_step=10;
279  }
280  else if (vpoint>100)
281  {
282  vis_step=100;
283  }
284  }
285 
286  string cam=newOptions.find("camera").asString();
287  if (newOptions.find("camera").isNull() && (first_time==true))
288  {
289  eye="left";
290  }
291  else if (!newOptions.find("camera").isNull())
292  {
293  if ((cam=="left") || (cam=="right"))
294  {
295  eye=cam;
296  }
297  else
298  {
299  eye="left";
300  }
301  }
302 
303  string col=newOptions.find("color").asString();
304  if (newOptions.find("color").isNull() && (first_time==true))
305  {
306  Color[0]=255; Color[1]=0; Color[2]=0;
307  }
308  else if (!newOptions.find("color").isNull())
309  {
310  if (col=="red")
311  {
312  Color[0]=255; Color[1]=0; Color[2]=0;
313  }
314  else if (col=="green")
315  {
316  Color[0]=0; Color[1]=255; Color[2]=0;
317  }
318  else if (col=="blue")
319  {
320  Color[0]=0; Color[1]=0; Color[2]=255;
321  }
322  else
323  {
324  Color[0]=255; Color[1]=0; Color[2]=0;
325  }
326  }
327 }
328 
329 /***********************************************************************/
331 {
332  LockGuard lg(mutex);
333 
334  Property advOptions;
335  advOptions.put("visualized_points",vis_points);
336  if (Color[0]==255 && Color[1]==0 && Color[2]==0)
337  advOptions.put("color","red");
338  else if (Color[0]==0 && Color[1]==255 && Color[2]==0)
339  advOptions.put("color","green");
340  else if (Color[0]==0 && Color[1]==0 &&Color[2]==255)
341  advOptions.put("color","blue");
342  advOptions.put("camera",eye);
343  advOptions.put("visualized_points_step",vis_step);
344  advOptions.put("what_to_plot",what_to_plot);
345  return advOptions;
346 }
347 
348 /***********************************************************************/
350 {
351  LockGuard lg(mutex);
352  return t_vis;
353 }
354 
yarp::dev::IGazeControl * igaze
Gaze Control interface.
yarp::sig::Vector & superq_filtered
Filtered superquadric.
yarp::os::Property getPar()
Get parameters for visualization.
std::string eye
Eye camera selected.
yarp::sig::Vector from3Dto2D(const yarp::sig::Vector &point3D)
Compute 2D pixels from 3D points.
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > portImgIn
Input image port.
bool showPoints()
Show point cloud on the image.
bool showSuperq(yarp::sig::Vector &x_to_show)
Show reconstructed superquadric on the image.
yarp::sig::ImageOf< yarp::sig::PixelRgb > * imgIn
Input image.
double getTime()
Get time required for visualization.
std::deque< yarp::sig::Vector > & points
Object point cloud.
double t_vis
Time for visualization.
void interruptPorts()
Interrupt ports functionalities.
virtual bool threadInit()
Init function of RateThread.
std::string what_to_plot
String used for deciding what to plot: "points" or "superq".
int vis_step
Number of visualization step.
void setPar(const std::string &par_name, const std::string &value)
Set a given parameter equal to a string.
virtual void run()
Run function of RateThread.
yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelRgb > > portImgOut
Output image port *.
virtual void threadRelease()
Release function of RateThread.
int vis_points
Number of points used for visualization.