vtk ClipDataSetWithPolyData 任意polydata 切割矩形物体,所以这个也有限制,一方必须是矩形网格。
The example that shows how to use the vtkClipDataSet to clip a vtkRectilinearGrid with an arbitrary polydata. vtkImplicitPolyDataDistance is used to turn the polydata into an implicit function. Every point of the grid is evaluated before sending to vtkClipDataSet. This example uses a vtkConeSource to generate polydata to use, however any polydata could be used, including stl files.
#include <vtkActor.h>
#include <vtkBooleanOperationPolyDataFilter.h>
#include <vtkCleanPolyData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkTriangleFilter.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSphereSource.h>
#include <vtksys/SystemTools.hxx>
#include <vtkPolyDataNormals.h>
#include <vtkAppendPolyData.h>
#include <vtkCamera.h>
#include <vtkIntersectionPolyDataFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellTypes.h>
#include <vtkClipDataSet.h>
#include <vtkConeSource.h>
#include <vtkDataSetMapper.h>
#include <vtkFloatArray.h>
#include <vtkImplicitPolyDataDistance.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkUnstructuredGrid.h>
#include <vtkInteractorStyleRubberBandPick.h>
#include <map>
///
//#include"InteractorStyle.hpp"
#include <vtkObjectFactory.h>
#include <vtkSmoothPolyDataFilter.h>
#include <vtkDICOMImageReader.h>
#include <vtkMarchingCubes.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkProperty.h>
#include <vtkStripper.h>
#include <vtkContourFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkDecimatePro.h>
//#include <vtkImageData.h>
#include <vtkImageShrink3D.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkNew.h>
#include <vtkTransform.h>
#include <vtkAutoInit.h>
//VTK_MODULE_INIT(vtkRenderingOpenGL); //未使用cmake生成的项目需要加
//VTK_MODULE_INIT(vtkInteractionStyle);//未使用cmake生成的项目需要加
using namespace vtk;
vtkNew<vtkRenderWindowInteractor> interactor;
vtkNew<vtkDataSetMapper> clipperOutsideMapper;
vtkNew<vtkClipDataSet> clipper;
vtkNew<vtkRenderer> rightRenderer;
vtkNew<vtkActor> coneActor;
vtkNew<vtkDataSetMapper> clipperMapper;
vtkNew<vtkActor> clipperOutsideActor;
vtkNew<vtkActor> clipperActor;
vtkNew<vtkActor> wireActor;
vtkNew<vtkRectilinearGrid> rgrid;
//vtkSmartPointer<vtkSTLReader> reader1 = vtkSmartPointer<vtkSTLReader>::New();
//vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
class MyInteractorStyle : public vtkInteractorStyleRubberBandPick //重载vtkInteractorStyleRubberBandPick
{
public:
static MyInteractorStyle* New();
vtkTypeMacro(MyInteractorStyle, vtkInteractorStyleRubberBandPick);
private:
MyInteractorStyle()
{
DataTwo = vtkSmartPointer<vtkPolyData>::New();
this->CurrentMode = 0;
}
void MyInteractorStyle::OnKeyRelease()
{
vtkRenderWindowInteractor *rwi = this->Interactor;
switch (rwi->GetKeyCode())
{
case 'r':
this->CurrentMode = m_CurrentMode = !m_CurrentMode;
//pWidget->On();
vtkInteractorStyleRubberBandPick::OnKeyRelease();
break;
case 's':
this->CurrentMode = m_CurrentMode = !m_CurrentMode;
vtkInteractorStyleRubberBandPick::OnKeyRelease();
break;
case 'w':
this->CurrentMode = m_CurrentMode = !m_CurrentMode;
vtkInteractorStyleRubberBandPick::OnKeyRelease();
break;
case 'd':
vtkInteractorStyleRubberBandPick::OnKeyRelease();
//this->CurrentMode = m_CurrentMode = !m_CurrentMode;
{
clipper->SetInputData(rgrid);
clipper->InsideOutOn();
clipper->SetValue(0.0);
clipper->GenerateClippedOutputOn();
coneActor->VisibilityOff();
wireActor->VisibilityOff();
clipper->Update();
clipperMapper->SetInputConnection(clipper->GetOutputPort());
clipperActor->SetMapper(clipperMapper.Get());
// clipperActor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
clipperOutsideMapper->SetInputConnection(clipper->GetOutputPort(1));
clipperOutsideActor->SetMapper(clipperOutsideMapper.Get());
mRenderer->Render();
}
break;
}
}
public:
vtkSmartPointer<vtkPolyData> Data;
vtkSmartPointer<vtkPolyData> DataTwo;
vtkSmartPointer<vtkDataSetMapper> mapper;
vtkSmartPointer<vtkPolyDataMapper> mapperTwo;
vtkRenderer *mRenderer;
vtkRenderWindow * mvtkRenderWindow;
vtkSmartPointer<vtkClipDataSet> clipper;
bool m_CurrentMode;
};
vtkStandardNewMacro(MyInteractorStyle);
class StyleTrackballActor :public vtkInteractorStyleTrackballActor
{
public:
static StyleTrackballActor* New();
//vtkTypeMacro(InteractorStyle, vtkInteractorStyleRubberBandPick);
vtkTypeMacro(StyleTrackballActor, vtkInteractorStyleTrackballActor);
virtual void OnLeftButtonDown()
{
// Forward events
vtkInteractorStyleTrackballActor::OnLeftButtonDown();
if (this->InteractionProp == this->ActorA)
{
std::cout << "Picked ActorA." << std::endl;
}
else if (this->InteractionProp == this->ActorB)
{
std::cout << "Picked ActorB." << std::endl;
}
}
virtual void OnMiddleButtonDown()
{
// Forward events
vtkInteractorStyleTrackballActor::OnMiddleButtonDown();
vtkSmartPointer<MyInteractorStyle> style =
vtkSmartPointer<MyInteractorStyle>::New();
//vtkSmartPointer<vtkPolyData> DataTwo = vtkSmartPointer<vtkPolyData>::New();;
//style->DataTwo = DataTwo;
//style->Data = skinMapper->GetOutput();
style->mapper = clipperOutsideMapper.Get();
style->mRenderer = rightRenderer.Get();
style->clipper = clipper.Get();
interactor->SetInteractorStyle(style);
}
vtkActor* ActorA;
vtkActor* ActorB;
};
vtkStandardNewMacro(StyleTrackballActor);
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName);
void PositionCamera(vtkRenderer* renderer, double* viewUp, double* position);
} // namespace
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
// Create polydata to slice the grid with. In this case, use a cone. This
// could
// be any polydata including a stl file.
vtkNew<vtkConeSource> cone;
cone->SetResolution(50);
cone->SetDirection(0, 0, -1);
cone->SetHeight(3.0);
cone->CappingOn();
cone->Update();
//vtkPolyData *
// polydata = reader1->GetOutput();
// Implicit function that will be used to slice the mesh
vtkNew<vtkImplicitPolyDataDistance> implicitPolyDataDistance;
implicitPolyDataDistance->SetInput(cone->GetOutput());
float extendValue = 2.0;
// create a grid
unsigned int dimension = 51;
vtkNew<vtkFloatArray> xCoords;
for (unsigned int i = 0; i < dimension; ++i)
{
xCoords->InsertNextValue(-1.0 +
i * extendValue / static_cast<float>(dimension - 1));
}
vtkNew<vtkFloatArray> yCoords;
for (unsigned int i = 0; i < dimension; ++i)
{
yCoords->InsertNextValue(-1.0 +
i * extendValue / static_cast<float>(dimension - 1));
}
vtkNew<vtkFloatArray> zCoords;
for (unsigned int i = 0; i < dimension; ++i)
{
zCoords->InsertNextValue(-1.0 +
i * extendValue / static_cast<float>(dimension - 1));
}
// The coordinates are assigned to the rectilinear grid. Make sure that
// the number of values in each of the XCoordinates, YCoordinates,
// and ZCoordinates is equal to what is defined in SetDimensions().
//vtkNew<vtkRectilinearGrid> rgrid;
rgrid->SetDimensions(xCoords->GetNumberOfTuples(),
yCoords->GetNumberOfTuples(),
zCoords->GetNumberOfTuples());
rgrid->SetXCoordinates(xCoords.Get());
rgrid->SetYCoordinates(yCoords.Get());
rgrid->SetZCoordinates(zCoords.Get());
// Create an array to hold distance information
vtkNew<vtkFloatArray> signedDistances;
signedDistances->SetNumberOfComponents(1);
signedDistances->SetName("SignedDistances");
// Evaluate the signed distance function at all of the grid points
for (vtkIdType pointId = 0; pointId < rgrid->GetNumberOfPoints(); ++pointId)
{
double p[3];
rgrid->GetPoint(pointId, p);
double signedDistance = implicitPolyDataDistance->EvaluateFunction(p);
signedDistances->InsertNextValue(signedDistance);
}
// Add the SignedDistances to the grid
rgrid->GetPointData()->SetScalars(signedDistances.Get());
// --- mappers, actors, render, etc. ---
// mapper and actor to view the cone
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort());
coneActor->SetMapper(coneMapper.Get());
// geometry filter to view the background grid
vtkNew<vtkRectilinearGridGeometryFilter> geometryFilter;
geometryFilter->SetInputData(rgrid);
//geometryFilter->SetInputData(reader2->GetOutput());
//geometryFilter->SetExtent(0, dimension, 0, dimension, dimension / 2,
// dimension / 2);
geometryFilter->Update();
vtkNew<vtkPolyDataMapper> rgridMapper;
rgridMapper->SetInputConnection(geometryFilter->GetOutputPort());
rgridMapper->SetScalarRange(
rgrid->GetPointData()->GetArray("SignedDistances")->GetRange());
wireActor->SetMapper(rgridMapper.Get());
wireActor->GetProperty()->SetRepresentationToWireframe();
// mapper and actor to view the clipped mesh
//vtkNew<vtkDataSetMapper> clipperMapper;
//clipperMapper->SetInputConnection(clipper->GetOutputPort());
clipperMapper->ScalarVisibilityOff();
//vtkNew<vtkDataSetMapper> clipperOutsideMapper;
//clipperOutsideMapper->SetInputConnection(clipper->GetOutputPort(1));
clipperOutsideMapper->ScalarVisibilityOff();
// A renderer and render window
// Create a renderer, render window, and interactor
double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
vtkNew<vtkRenderer> leftRenderer;
leftRenderer->SetViewport(leftViewport);
//leftRenderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
//vtkNew<vtkRenderer> rightRenderer;
rightRenderer->SetViewport(rightViewport);
//rightRenderer->SetBackground(colors->GetColor3d("CadetBlue").GetData());
// add the actors
leftRenderer->AddActor(coneActor.Get());
leftRenderer->AddActor(wireActor.Get());
leftRenderer->AddActor(clipperActor.Get());
rightRenderer->AddActor(clipperOutsideActor.Get());
vtkNew<vtkRenderWindow> renwin;
renwin->SetSize(640, 480);
renwin->AddRenderer(leftRenderer.Get());
renwin->AddRenderer(rightRenderer.Get());
renwin->SetWindowName("ClipDataSetWithPolyData");
// An interactor
//vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renwin.Get());
add style
vtkSmartPointer<StyleTrackballActor> style =
vtkSmartPointer<StyleTrackballActor>::New();
interactor->SetInteractorStyle(style);
//
// Share the camera
leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
leftRenderer->GetActiveCamera()->Azimuth(30);
leftRenderer->GetActiveCamera()->Elevation(30);
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
renwin->Render();
interactor->Start();
// Generate a report
vtkIdType numberOfCells = clipper->GetOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The clipped dataset(inside) contains a " << std::endl
<< clipper->GetOutput()->GetClassName() << " that has "
<< numberOfCells << " cells" << std::endl;
typedef std::map<int, int> CellContainer;
CellContainer cellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
cellMap[clipper->GetOutput()->GetCellType(i)]++;
}
CellContainer::const_iterator it = cellMap.begin();
while (it != cellMap.end())
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(it->first) << " occurs "
<< it->second << " times." << std::endl;
++it;
}
numberOfCells = clipper->GetClippedOutput()->GetNumberOfCells();
std::cout << "------------------------" << std::endl;
std::cout << "The clipped dataset(outside) contains a " << std::endl
<< clipper->GetClippedOutput()->GetClassName() << " that has "
<< numberOfCells << " cells" << std::endl;
typedef std::map<int, int> OutsideCellContainer;
CellContainer outsideCellMap;
for (vtkIdType i = 0; i < numberOfCells; i++)
{
outsideCellMap[clipper->GetClippedOutput()->GetCellType(i)]++;
}
it = outsideCellMap.begin();
while (it != outsideCellMap.end())
{
std::cout << "\tCell type "
<< vtkCellTypes::GetClassNameFromTypeId(it->first) << " occurs "
<< it->second << " times." << std::endl;
++it;
}
return EXIT_SUCCESS;
}