17 #include <iCub/data3D/private/helpers.h> 18 #include <iCub/data3D/boundingBox.h> 27 BoundingBox::BoundingBox(
const iCub::data3D::Box3D &boundingBox)
29 this->boundingBox.corners=boundingBox.corners;
30 this->boundingBox.orientation=boundingBox.orientation;
34 iCub::data3D::Box3D BoundingBox::getBoundingBox()
36 return this->boundingBox;
40 void BoundingBox::setBoundingBox(
const iCub::data3D::Box3D &boundingBox)
42 this->boundingBox.corners=boundingBox.corners;
43 this->boundingBox.orientation=boundingBox.orientation;
47 std::vector<iCub::data3D::PointXYZ> BoundingBox::getCorners()
49 return this->boundingBox.corners;
53 void BoundingBox::setCorners(
const std::vector<iCub::data3D::PointXYZ> &corners)
55 this->boundingBox.corners=corners;
59 yarp::sig::Matrix BoundingBox::getOrientation()
61 return this->boundingBox.orientation;
65 void BoundingBox::setOrientation(
const yarp::sig::Matrix &orientation)
67 this->boundingBox.orientation=orientation;
71 Vector BoundingBox::findIndexes(
const yarp::sig::Matrix &corner_i)
74 yarp::sig::Vector point1(3); point1[0]=corner_i(0,0); point1[1]=corner_i(0,1); point1[2]=corner_i(0,2);
77 for (
int i=0; i<3; i++)
79 for (
int j=0; j<3; j++)
85 for (
int t=1; t<corner_i.rows(); t++)
87 yarp::sig::Vector tmp(3); tmp[0]=corner_i(t,0); tmp[1]=corner_i(t,1); tmp[2]=corner_i(t,2);
88 double value=norm(Helpers::extractSubVector(point1,i,j)-Helpers::extractSubVector(tmp,i,j));
103 Matrix BoundingBox::convertCorners()
105 Matrix cornerpoints(boundingBox.corners.size(), 3);
107 for (
int i=0; i<boundingBox.corners.size(); i++)
109 cornerpoints(i,0)=boundingBox.corners[i].x;
110 cornerpoints(i,1)=boundingBox.corners[i].y;
111 cornerpoints(i,2)=boundingBox.corners[i].z;
114 Matrix corner_i=cornerpoints*boundingBox.orientation;
119 void BoundingBox::getAxis(yarp::sig::Vector &x, yarp::sig::Vector &y, yarp::sig::Vector &z)
121 Matrix corner_i=convertCorners();
122 yarp::sig::Vector indexes=findIndexes(corner_i);
124 yarp::sig::Vector point1(3); point1[0]=corner_i(0,0); point1[1]=corner_i(0,1); point1[2]=corner_i(0,2);
126 yarp::sig::Vector index0(3); index0[0]=corner_i(indexes[0],0); index0[1]=corner_i(indexes[0],1); index0[2]=corner_i(indexes[0],2);
127 yarp::sig::Vector index1(3); index1[0]=corner_i(indexes[1],0); index1[1]=corner_i(indexes[1],1); index1[2]=corner_i(indexes[1],2);
128 yarp::sig::Vector index2(3); index2[0]=corner_i(indexes[2],0); index2[1]=corner_i(indexes[2],1); index2[2]=corner_i(indexes[2],2);
130 yarp::sig::Vector vectz=point1-index0;
131 z=vectz*boundingBox.orientation.transposed();
132 yarp::sig::Vector vecty=point1-index1;
133 y=vecty*boundingBox.orientation.transposed();
134 yarp::sig::Vector vectx=point1-index2;
135 x=vectx*boundingBox.orientation.transposed();
139 Vector BoundingBox::getDim()
141 Matrix corner_i=convertCorners();
142 yarp::sig::Vector indexes=findIndexes(corner_i);
144 yarp::sig::Vector point1(3); point1[0]=corner_i(0,0); point1[1]=corner_i(0,1); point1[2]=corner_i(0,2);
147 dim[2]=max(point1[2],corner_i(indexes[0],2))-min(point1[2],corner_i(indexes[0],2));
148 dim[1]=max(point1[1],corner_i(indexes[1],1))-min(point1[1],corner_i(indexes[1],1));
149 dim[0]=max(point1[0],corner_i(indexes[2],0))-min(point1[0],corner_i(indexes[2],0));
155 Vector BoundingBox::getCenter()
157 Matrix corner_i=convertCorners();
158 yarp::sig::Vector indexes=findIndexes(corner_i);
160 yarp::sig::Vector point1(3); point1[0]=corner_i(0,0); point1[1]=corner_i(0,1); point1[2]=corner_i(0,2);
162 yarp::sig::Vector index0(3); index0[0]=corner_i(indexes[0],0); index0[1]=corner_i(indexes[0],1); index0[2]=corner_i(indexes[0],2);
163 yarp::sig::Vector index1(3); index1[0]=corner_i(indexes[1],0); index1[1]=corner_i(indexes[1],1); index1[2]=corner_i(indexes[1],2);
164 yarp::sig::Vector index2(3); index2[0]=corner_i(indexes[2],0); index2[1]=corner_i(indexes[2],1); index2[2]=corner_i(indexes[2],2);
166 yarp::sig::Vector center(3);
167 center[2]=(point1[2]+index0[2])/2;
168 center[1]=(point1[1]+index1[1])/2;
169 center[0]=(point1[0]+index2[0])/2;
171 yarp::sig::Vector centerroot=center*boundingBox.orientation.transposed();
176 void BoundingBox::drawBoundingBox(boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer,
int viewport)
178 pcl::PointXYZRGB point1;
179 point1.x=boundingBox.corners[0].x;
180 point1.y=boundingBox.corners[0].y;
181 point1.z=boundingBox.corners[0].z;
185 pcl::PointXYZRGB point2;
186 point2.x=boundingBox.corners[1].x;
187 point2.y=boundingBox.corners[1].y;
188 point2.z=boundingBox.corners[1].z;
192 pcl::PointXYZRGB point3;
193 point3.x=boundingBox.corners[2].x;
194 point3.y=boundingBox.corners[2].y;
195 point3.z=boundingBox.corners[2].z;
199 pcl::PointXYZRGB point4;
200 point4.x=boundingBox.corners[3].x;
201 point4.y=boundingBox.corners[3].y;
202 point4.z=boundingBox.corners[3].z;
206 pcl::PointXYZRGB point5;
207 point5.x=boundingBox.corners[4].x;
208 point5.y=boundingBox.corners[4].y;
209 point5.z=boundingBox.corners[4].z;
213 pcl::PointXYZRGB point6;
214 point6.x=boundingBox.corners[5].x;
215 point6.y=boundingBox.corners[5].y;
216 point6.z=boundingBox.corners[5].z;
220 pcl::PointXYZRGB point7;
221 point7.x=boundingBox.corners[6].x;
222 point7.y=boundingBox.corners[6].y;
223 point7.z=boundingBox.corners[6].z;
227 pcl::PointXYZRGB point8;
228 point8.x=boundingBox.corners[7].x;
229 point8.y=boundingBox.corners[7].y;
230 point8.z=boundingBox.corners[7].z;
235 Vector centerroot=getCenter();
236 Vector vectxroot,vectyroot,vectzroot;
237 getAxis(vectxroot,vectyroot,vectzroot);
240 z1.x=centerroot[0]+(vectzroot[0]/2);
241 z1.y=centerroot[1]+(vectzroot[1]/2);
242 z1.z=centerroot[2]+(vectzroot[2]/2);
245 z2.x=centerroot[0]+(-vectzroot[0]/2);
246 z2.y=centerroot[1]+(-vectzroot[1]/2);
247 z2.z=centerroot[2]+(-vectzroot[2]/2);
250 y1.x=centerroot[0]+(vectyroot[0]/2);
251 y1.y=centerroot[1]+(vectyroot[1]/2);
252 y1.z=centerroot[2]+(vectyroot[2]/2);
255 y2.x=centerroot[0]+(-vectyroot[0]/2);
256 y2.y=centerroot[1]+(-vectyroot[1]/2);
257 y2.z=centerroot[2]+(-vectyroot[2]/2);
260 x1.x=centerroot[0]+(vectxroot[0]/2);
261 x1.y=centerroot[1]+(vectxroot[1]/2);
262 x1.z=centerroot[2]+(vectxroot[2]/2);
265 x2.x=centerroot[0]+(-vectxroot[0]/2);
266 x2.y=centerroot[1]+(-vectxroot[1]/2);
267 x2.z=centerroot[2]+(-vectxroot[2]/2);
269 pcl::PointXYZ center(centerroot[0],centerroot[1],centerroot[2]);
273 viewer->addLine<pcl::PointXYZRGB>(point1,point2,
"line1");
274 viewer->addLine<pcl::PointXYZRGB>(point2,point3,
"line2");
275 viewer->addLine<pcl::PointXYZRGB>(point3,point4,
"line3");
276 viewer->addLine<pcl::PointXYZRGB>(point4,point1,
"line4");
277 viewer->addLine<pcl::PointXYZRGB>(point5,point6,
"line5");
278 viewer->addLine<pcl::PointXYZRGB>(point6,point7,
"line6");
279 viewer->addLine<pcl::PointXYZRGB>(point7,point8,
"line7");
280 viewer->addLine<pcl::PointXYZRGB>(point8,point5,
"line8");
281 viewer->addLine<pcl::PointXYZRGB>(point1,point5,
"line9");
282 viewer->addLine<pcl::PointXYZRGB>(point2,point6,
"line10");
283 viewer->addLine<pcl::PointXYZRGB>(point3,point7,
"line11");
284 viewer->addLine<pcl::PointXYZRGB>(point4,point8,
"line12");
286 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(z1,center,1,1,1,1,
"z1");
287 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(z2,center,1,1,1,1,
"z2");
288 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(y1,center,1,1,1,1,
"y1");
289 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(y2,center,1,1,1,1,
"y2");
290 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(x1,center,1,1,1,1,
"x1");
291 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(x2,center,1,1,1,1,
"x2");
295 viewer->addLine<pcl::PointXYZRGB>(point1,point2,
"line1",viewport);
296 viewer->addLine<pcl::PointXYZRGB>(point2,point3,
"line2",viewport);
297 viewer->addLine<pcl::PointXYZRGB>(point3,point4,
"line3",viewport);
298 viewer->addLine<pcl::PointXYZRGB>(point4,point1,
"line4",viewport);
299 viewer->addLine<pcl::PointXYZRGB>(point5,point6,
"line5",viewport);
300 viewer->addLine<pcl::PointXYZRGB>(point6,point7,
"line6",viewport);
301 viewer->addLine<pcl::PointXYZRGB>(point7,point8,
"line7",viewport);
302 viewer->addLine<pcl::PointXYZRGB>(point8,point5,
"line8",viewport);
303 viewer->addLine<pcl::PointXYZRGB>(point1,point5,
"line9",viewport);
304 viewer->addLine<pcl::PointXYZRGB>(point2,point6,
"line10",viewport);
305 viewer->addLine<pcl::PointXYZRGB>(point3,point7,
"line11",viewport);
306 viewer->addLine<pcl::PointXYZRGB>(point4,point8,
"line12",viewport);
308 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(z1,center,0,0,1,1,
"z1",viewport);
309 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(z2,center,0,0,1,1,
"z2",viewport);
310 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(y1,center,0,1,0,1,
"y1",viewport);
311 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(y2,center,0,1,0,1,
"y2",viewport);
312 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(x1,center,1,0,0,1,
"x1",viewport);
313 viewer->addArrow<pcl::PointXYZ,pcl::PointXYZ>(x2,center,1,0,0,1,
"x2",viewport);