HI, I 'm using vtkTableBasedClipDataSet to cut a cylinder. It works fairly well. But when cutting along a same plane(implicit function) multiple times, some duplicate points would present in the output. As a result, the render time drops dramatically.
The image below shows the situation.
I apply the vtkmCleanGrid to deal with it, but still the same result.
And here is my code:
#cpp
#include "RenderWindowUISingleInheritance.h"
#include "ui_RenderWindowUISingleInheritance.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkVersion.h>
#include <vtkPlane.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkCallbackCommand.h>
#include <vtkIdFilter.h>
#include <vtkmCleanGrid.h>
namespace {
void CallbackFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId),
void* vtkNotUsed(clientData), void* vtkNotUsed(callData))
{
vtkRenderer* renderer = static_cast<vtkRenderer*>(caller);
std::cout << "render time: " << renderer->GetLastRenderTimeInSeconds() << std::endl;
}
}
RenderWindowUISingleInheritance::RenderWindowUISingleInheritance(
QWidget* parent)
: QMainWindow(parent), ui(new Ui::RenderWindowUISingleInheritance)
{
this->ui->setupUi(this);
cutTimer = new QTimer(this);
vtkNew<vtkNamedColors> colors;
vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
this->ui->qvtkWidget->setRenderWindow(renderWindow);
vtkNew<vtkXMLUnstructuredGridReader> Reader;
Reader->SetFileName("../cylinder.vtu");
Reader->Update();
clipper = vtkSmartPointer<vtkTableBasedClipDataSet>::New();
Source = vtkUnstructuredGrid::New();
Source->DeepCopy(Reader->GetOutput());
clipper->SetInputData(Source);
Mapper = vtkSmartPointer<vtkDataSetMapper>::New();
Actor = vtkSmartPointer<vtkActor>::New();
Mapper->SetInputData(Source);
Actor->SetMapper(Mapper);
// VTK Renderer
vtkNew<vtkRenderer> renderer;
renderer->AddActor(Actor);
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
vtkNew<vtkCallbackCommand> rendererCallback;
rendererCallback->SetCallback(CallbackFunction);
renderer->AddObserver(vtkCommand::EndEvent, rendererCallback);
this->ui->qvtkWidget->renderWindow()->AddRenderer(renderer);
connect(cutTimer, &QTimer::timeout, [=]() {
std::cout << "points befor clip = " << Source->GetNumberOfPoints() << std::endl;
std::cout << "cells befor clip = " << Source->GetNumberOfCells() << std::endl;
vtkNew<vtkPlane> plane;
clipper->RemoveAllInputs();
clipper->SetInputData(Source);
clipper->SetClipFunction(plane);
clipper->Update();
Source->DeepCopy(clipper->GetOutput());
std::cout << "points after clip = " << Source->GetNumberOfPoints() << std::endl;
std::cout << "cells after clip = " << Source->GetNumberOfCells() << std::endl;
std::cout << "=================================================== " << std::endl;
std::cout << std::endl;
vtkSmartPointer<vtkmCleanGrid> cg = vtkSmartPointer<vtkmCleanGrid>::New();
cg->SetInputData(Source);
vtkNew<vtkIdFilter> idFilter;
idFilter->SetInputConnection(cg->GetOutputPort());
idFilter->SetPointIds(1);
idFilter->SetCellIds(1);
idFilter->Update();
Mapper->RemoveAllInputs();
Mapper->SetInputData(idFilter->GetOutput());
this->ui->qvtkWidget->renderWindow()->Render();
});
cutTimer->start(1000);
}
RenderWindowUISingleInheritance::~RenderWindowUISingleInheritance()
{
delete this->ui;
}
#header
#ifndef RenderWindowUISingleInheritance_H
#define RenderWindowUISingleInheritance_H
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkTableBasedClipDataSet.h>
#include <vtkDataSetMapper.h>
#include <QMainWindow>
#include <QTimer>
/*
* See "The Single Inheritance Approach" in this link:
* [Using a Designer UI File in Your C++
* Application](https://doc.qt.io/qt-5/designer-using-a-ui-file.html)
*/
namespace Ui {
class RenderWindowUISingleInheritance;
}
class RenderWindowUISingleInheritance : public QMainWindow
{
Q_OBJECT
public:
// Constructor/Destructor
explicit RenderWindowUISingleInheritance(QWidget* parent = nullptr);
virtual ~RenderWindowUISingleInheritance();
vtkSmartPointer<vtkTableBasedClipDataSet> clipper;
vtkUnstructuredGrid* Source;
vtkSmartPointer<vtkDataSetMapper> Mapper;
vtkSmartPointer<vtkActor> Actor;
private:
// Designer form
Ui::RenderWindowUISingleInheritance* ui;
QTimer* cutTimer;
public slots:
};
#endif
As far as I know, the vtkTableBasedClipDataSet will generate the least cell when clipping.
How can I address this problem?