1. 概述
包围盒是指能够包含三维图形的长方体,常常用于模型的碰撞检测。包围盒可以分成:轴对齐包围盒(AABB),有向包围盒(OBB)和凸包(Convex Hull)
今天就来实践一下AABB包围盒
2.绘制一个锥体
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
int main()
{
setbuf(stdout, nullptr);
vtkSmartPointer<vtkConeSource> cone =
vtkSmartPointer<vtkConeSource>::New();
cone->SetDirection(1, 1, 0);
cone->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
//mapper->SetInputData( polydata );
mapper->SetInputData(cone->GetOutput());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
3.绘制包围盒,主要是利用了vtkOutlineFilter
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
int main()
{
setbuf(stdout, nullptr);
vtkSmartPointer<vtkConeSource> cone =
vtkSmartPointer<vtkConeSource>::New();
cone->SetDirection(1, 1, 0);
cone->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
//mapper->SetInputData( polydata );
mapper->SetInputData(cone->GetOutput());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// --------------------- start to draw bounding box ------------------------
vtkSmartPointer<vtkOutlineFilter> outlineFilter =
vtkSmartPointer<vtkOutlineFilter>::New();
outlineFilter->SetInputData(cone->GetOutput());
vtkSmartPointer<vtkPolyDataMapper> outLinemapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
outLinemapper->SetInputConnection(outlineFilter->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outLinemapper);
outlineActor->GetProperty()->SetColor(1, 1, 1);
// --------------------- Drawing bounding box end------------------------
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->AddActor(outlineActor);
renderer->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
效果图:
3.修改包围盒的颜色
1.vtkBoundingBox 使用这个类,获取包围盒8个点的坐标
2.vtkPoints,vtkCellArray。设置8个点的坐标信息,拓扑信息,生成对应的vtkPolyData。
3.修改颜色成红色
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkOBBTree.h>
#include <vtkActor2D.h>
#include <vtkMath.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>
#include <vtkMatrix4x4.h>
#include <vtkInteractorObserver.h>
#include <vtkPolyDataNormals.h>
#include <vtkOutlineFilter.h>
#include <vtkPoints.h>
#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCamera.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkCommand.h"
#include "vtkBoxWidget.h"
#include "vtkTransform.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkBoundingBox.h"
#include "vtkProperty.h"
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
#include<vector>
#include "vtkStructuredData.h"
#include "vtkCellArray.h"
typedef struct PointStruct_ {
PointStruct_(double x, double y, double z)
{
point[0] = x;
point[1] = y;
point[2] = z;
}
double point[3];
//double pointY;
//double pointZ;
}PointStruct;
int main()
{
setbuf(stdout, nullptr);
vtkSmartPointer<vtkConeSource> cone =
vtkSmartPointer<vtkConeSource>::New();
cone->SetDirection(1, 1, 0);
cone->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
//mapper->SetInputData( polydata );
mapper->SetInputData(cone->GetOutput());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// --------------------- start to draw bounding box ------------------------
vtkSmartPointer<vtkOutlineFilter> outlineFilter =
vtkSmartPointer<vtkOutlineFilter>::New();
outlineFilter->SetInputData(cone->GetOutput());
vtkSmartPointer<vtkPolyDataMapper> outLinemapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
outLinemapper->SetInputConnection(outlineFilter->GetOutputPort());
vtkSmartPointer<vtkActor> outlineActor = vtkSmartPointer<vtkActor>::New();
outlineActor->SetMapper(outLinemapper);
outlineActor->GetProperty()->SetColor(1, 1, 1);
// --------------------- Drawing bounding box end------------------------
vtkBoundingBox boundingBox;
for (int i = 0; i < cone->GetOutput()->GetNumberOfPoints(); ++i)
{
boundingBox.AddPoint(cone->GetOutput()->GetPoint(i));
}
double bounds[6] = { 0 };
boundingBox.GetBounds(bounds);
std::vector<PointStruct> pts;
for (int i = 0; i < 2; ++i)
{
for (int j = 2; j < 4; ++j)
{
for (int k = 4; k < 6; ++k)
{
pts.push_back(PointStruct(bounds[i], bounds[j], bounds[k]));
}
}
}
for (auto it : pts)
{
// cout << it;
cout << std::to_string(it.point[0]) << "," << std::to_string(it.point[1]) << "," << std::to_string(it.point[2]) << endl;
}
vtkSmartPointer<vtkPolyData> boundsPolydata =
vtkSmartPointer<vtkPolyData>::New();
vtkSmartPointer<vtkPoints> boundsPoints =
vtkSmartPointer<vtkPoints>::New();
for (int i = 0; i < 8; ++i)
{
boundsPoints->InsertNextPoint(pts[i].point);
}
boundsPolydata->SetPoints(boundsPoints);
vtkSmartPointer<vtkCellArray> cells =
vtkSmartPointer<vtkCellArray>::New();
vtkIdType cell[2] = { 0, 1 };
cells->InsertNextCell(2, cell);
cell[0] = 0; cell[1] = 2;
cells->InsertNextCell(2, cell);
cell[0] = 3; cell[1] = 2;
cells->InsertNextCell(2, cell);
cell[0] = 3; cell[1] = 1;
cells->InsertNextCell(2, cell);
cell[0] = 4; cell[1] = 5;
cells->InsertNextCell(2, cell);
cell[0] = 4; cell[1] = 6;
cells->InsertNextCell(2, cell);
cell[0] = 7; cell[1] = 5;
cells->InsertNextCell(2, cell);
cell[0] = 7; cell[1] = 6;
cells->InsertNextCell(2, cell);
cell[0] = 1; cell[1] = 5;
cells->InsertNextCell(2, cell);
cell[0] = 0; cell[1] = 4;
cells->InsertNextCell(2, cell);
cell[0] = 2; cell[1] = 6;
cells->InsertNextCell(2, cell);
cell[0] = 3; cell[1] = 7;
cells->InsertNextCell(2, cell);
boundsPolydata->SetLines(cells);
vtkSmartPointer<vtkPolyDataMapper> boundsMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
boundsMapper->SetInputData(boundsPolydata);
vtkSmartPointer<vtkActor> boundsActor =
vtkSmartPointer<vtkActor>::New();
boundsActor->SetMapper(boundsMapper);
boundsActor->GetProperty()->SetColor(1, 0, 0);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->AddActor(outlineActor);
renderer->AddActor(boundsActor);
renderer->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->GetInteractorStyle()->SetCurrentRenderer(renderer);
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}