感觉这方面的东西还是太少了;
// First include the required header files for the VTK classes we are using.
#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<vector>
//
// Similar to Cone2.cxx, we define a callback for interaction.
//
class vtkMyCallback : public vtkCommand
{
public:
static vtkMyCallback *New()
{ return new vtkMyCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkTransform *t = vtkTransform::New();
vtkBoxWidget *widget = reinterpret_cast<vtkBoxWidget*>(caller);
widget->GetTransform(t);
widget->GetProp3D()->SetUserTransform(t);
t->Delete();
}
};
#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;
#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 "tool.h"
using namespace std;
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);
// --------------------- start to draw bounding box ------------------------
vtkSmartPointer<vtkPolyDataNormals> normals =
vtkSmartPointer<vtkPolyDataNormals>::New();
normals->SetInputConnection(cone->GetOutputPort());
vtkSmartPointer<vtkOutlineFilter> outlineFilter =
vtkSmartPointer<vtkOutlineFilter>::New();
outlineFilter->SetInputConnection(normals->GetOutputPort());
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------------------------
renderer->AddActor(outlineActor);
vtkBoundingBox boundingBox;
for (int i = 0; i < cone->GetOutput()->GetNumberOfPoints(); ++i)
{
boundingBox.AddPoint(cone->GetOutput()->GetPoint(i));
}
double bounds[6] = { 0 };
boundingBox.GetBounds(bounds);
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);
renderer->AddActor(boundsActor);
renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
void CvtkDialogDlg::GetVtkBoundingBox(vtkImageData * resultData, vtkSmartPointer<vtkPolyData> boundsPolydata,vtkSmartPointer<vtkActor> boundsActor)
{
vtkBoundingBox boundingBox;
for (int i = 0; i < resultData->GetNumberOfPoints(); ++i)
{
boundingBox.AddPoint(resultData->GetPoint(i));
}
double bounds[6] = { 0 };
boundingBox.GetBounds(bounds);
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);
}
demo 下载