Given a point cloud (defined in the Point Cloud Library), it computes the minimum enclosing bounding box.
215 std::vector< pcl::Vertices > polygons;
216 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_hull (
new pcl::PointCloud<pcl::PointXYZRGB>);
217 pcl::ConvexHull<pcl::PointXYZRGB> chull;
218 chull.setInputCloud (cloud);
219 chull.setDimension(3);
220 chull.reconstruct (*cloud_hull,polygons);
222 yarp::sig::Vector x1;
223 yarp::sig::Vector x2;
224 yarp::sig::Vector x3;
225 yarp::sig::Vector y1;
226 yarp::sig::Vector y2;
227 yarp::sig::Vector y3;
228 yarp::sig::Vector z1;
229 yarp::sig::Vector z2;
230 yarp::sig::Vector z3;
232 for (
int i=0; i<polygons.size(); i++)
234 pcl::Vertices vertex=polygons.at(i);
235 x1.push_back(cloud_hull->at(vertex.vertices[0]).x);
236 x2.push_back(cloud_hull->at(vertex.vertices[1]).x);
237 x3.push_back(cloud_hull->at(vertex.vertices[2]).x);
238 y1.push_back(cloud_hull->at(vertex.vertices[0]).y);
239 y2.push_back(cloud_hull->at(vertex.vertices[1]).y);
240 y3.push_back(cloud_hull->at(vertex.vertices[2]).y);
241 z1.push_back(cloud_hull->at(vertex.vertices[0]).z);
242 z2.push_back(cloud_hull->at(vertex.vertices[1]).z);
243 z3.push_back(cloud_hull->at(vertex.vertices[2]).z);
246 Matrix pointCloud(cloud_hull->size(),3);
247 for (
int i=0; i<cloud_hull->size(); i++)
249 pointCloud(i,0)=cloud_hull->at(i).x;
250 pointCloud(i,1)=cloud_hull->at(i).y;
251 pointCloud(i,2)=cloud_hull->at(i).z;
254 yarp::sig::Vector v1x=x2-x1;
255 yarp::sig::Vector v1y=y2-y1;
256 yarp::sig::Vector v1z=z2-z1;
257 std::vector<yarp::sig::Vector> edges1;
259 retrieveEdges(v1x,v1y,v1z,edges1);
261 yarp::sig::Vector v2x=x3-x1;
262 yarp::sig::Vector v2y=y3-y1;
263 yarp::sig::Vector v2z=z3-z1;
264 std::vector<yarp::sig::Vector> edges2;
266 retrieveEdges2(v2x,v2y,v2z,edges1,edges2);
268 std::vector<yarp::sig::Vector> crossProduct;
269 for (
int i=0; i<edges1.size(); i++)
271 yarp::sig::Vector vect=cross(edges1.at(i),edges2.at(i));
272 crossProduct.push_back(vect);
275 yarp::sig::Vector alpha;
276 yarp::sig::Vector beta;
277 yarp::sig::Vector gamma;
278 eulerAngles(edges1, edges2, crossProduct, alpha, beta, gamma);
281 double minVol=100000;
284 for (
int i=0; i<alpha.size(); i++)
287 buildRotMat3(alpha[i],beta[i],gamma[i],rot);
288 Matrix xyz_i=pointCloud*rot;
289 findRotation(xyz_i,rot2);
291 Matrix rot3dim=eye(3,3);
292 rot3dim.setSubmatrix(rot2,0,0);
293 Matrix rotation=rot*rot3dim;
295 xyz_i=pointCloud*rotation;
296 yarp::sig::Vector minimum;
297 Helpers::min(xyz_i,minimum);
298 yarp::sig::Vector maximum;
299 Helpers::max(xyz_i,maximum);
300 yarp::sig::Vector h=maximum-minimum;
302 double prod=h[0]*h[1]*h[2];
307 minmax.setRow(0,minimum);
308 minmax.setRow(1,maximum);
311 Matrix cornerpointsTmp(8,3);
312 assignCorners(minmax,cornerpointsTmp);
313 Matrix cornerpoints=cornerpointsTmp*(bb.
getOrientation().transposed());
315 std::vector<iCub::data3D::PointXYZ> corners;
316 for (
unsigned int i=0; i<cornerpoints.rows(); i++)
317 corners.push_back(PointXYZ(cornerpoints(i,0),cornerpoints(i,1),cornerpoints(i,2)));
The Definition of the BoundingBox class.
yarp::sig::Matrix getOrientation()
Return the orientation of the Box3D structure.
void setCorners(const std::vector< iCub::data3D::PointXYZ > &corners)
Set the corners of the bounding box.
void setOrientation(const yarp::sig::Matrix &orientation)
Modify the orientation of the bounding box.