什么是包围盒
包围盒是指能够包容物体的立方体或者二维长方形,是包围体的一种,常常用于模型的碰撞检测。包围体主要包括球体、轴对齐包围盒(AABB)、有向包围盒(OBB)和凸包(Convex Hull)。
在较早的时候,包围盒按照坐标系的坐标轴进行排列,这被称为轴对齐的包围盒(AABB/Axis-aligned bounding box)。为了将AABB与通用的情况区分开来,将任意的包围盒称为有向包围盒(OBB/Oriented bounding box)。AABB检测模型的相交要比OBB更简单,但是他的缺点是当模型旋转的时候无法随之旋转,而必须重新计算。
轴对齐包围盒
在VTK中,轴对齐包围盒被定义为包含该模型,且边平行于坐标轴的最小六面体。AABB比较简单,存储空间小,仅需六个标量。
获取bounds的方法
1. vtkPolyData
vtkPolyData通过继承关系拥有GetBounds()方法,实际在vtkDataSet中定义,
double *GetBounds() VTK_SIZEHINT(6);
void GetBounds(double bounds[6]);
获取包围盒的参数,即三个轴向上的最大值和最小值。
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
double bounds[6];
polydata->GetBounds(bounds);
需要注意的是,通过reader或者source来的模型,需要先Update();
而有时候仅仅是获取这些参数并不直观,还需要显示包围盒,vtkOutlineFilter提供了这一方便的操作,输入模型的数据,就很容易的可视化显示。
vtkSmartPointer<vtkOutlineFilter> outline =
vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputData(polydata);
outline->Update();
如果需要看实心表面,则需要打开,默认是关闭的。
outline->GenerateFacesOn();
2. vtkBoundingBox
另一种方法是用vtkBoundingBox的GetBounds方法
void GetBounds(double bounds[6]) const;
void GetBounds(double &xMin, double &xMax,
double &yMin, double &yMax,
double &zMin, double &zMax) const;
vtkBoundingBox是专门用来处理3D bounds的,快速小巧,用了很多内联函数。它没有继承vtkObject,因此可以不用new,直接在栈上分配。
它的输入对的是点,所以对于点云来说很方便。
double p0[3] = {3, 4, 5};
vtkBoundingBox boundingBox;
boundingBox.AddPoint(p0);
double bounds[6];
boundingBox.GetBounds(bounds);
效果
示例代码
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkOutlineFilter.h>
#include <vtkProperty.h>
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
if (argc > 1)
{
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(argv[1]);
reader->Update();
polydata = reader->GetOutput();
}
else
{
vtkSmartPointer<vtkSphereSource> modelSource =
vtkSmartPointer<vtkSphereSource>::New();
modelSource->Update();
polydata = modelSource->GetOutput();
}
vtkSmartPointer<vtkOutlineFilter> outline =
vtkSmartPointer<vtkOutlineFilter>::New();
outline->SetInputData(polydata);
outline->Update();
double bounds[6];
polydata->GetBounds(bounds);
std::cout << "Polydata bounds: " << std::endl;
std::cout << "xmin: " << bounds[0] << " "
<< "xmax: " << bounds[1] << std::endl
<< "ymin: " << bounds[2] << " "
<< "ymax: " << bounds[3] << std::endl
<< "zmin: " << bounds[4] << " "
<< "zmax: " << bounds[5] << std::endl;
outline->GetOutput()->GetBounds(bounds);
std::cout << "Outline bounds: " << std::endl;
std::cout << "xmin: " << bounds[0] << " "
<< "xmax: " << bounds[1] << std::endl
<< "ymin: " << bounds[2] << " "
<< "ymax: " << bounds[3] << std::endl
<< "zmin: " << bounds[4] << " "
<< "zmax: " << bounds[5] << std::endl;
vtkSmartPointer<vtkPolyDataMapper> modelMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
modelMapper->SetInputData(polydata);
vtkSmartPointer<vtkActor> modelActor =
vtkSmartPointer<vtkActor>::New();
modelActor->SetMapper(modelMapper);
vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
outlineMapper->SetInputConnection(outline->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor =
vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outlineMapper);
outlineActor->GetProperty()->SetColor(1, 0, 0);
outlineActor->GetProperty()->SetLineWidth(1.5);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(300, 300);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(modelActor);
renderer->AddActor(outlineActor);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}