How to load a PLY file into a QT widget using VTK
-
QtWidgetsApplication1::QtWidgetsApplication1(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);this->widget = new In3DTestWidget(this); this->widget->setGeometry(300, 100, 200, 200); this->widget->resize(900, 400); this->widget->LoadTest(); //어떤 함수에서 어떻게 처리할지 connect(ui.pushButton, &QPushButton::clicked, this, &QtWidgetsApplication1::test); cl = vtkUnsignedCharArray::New(); connect(ui.pushButton_2, &QPushButton::clicked, this, &QtWidgetsApplication1::colchan);
}
//This is the constructor for the main widget.
void In3DTestWidget::LoadTest()
{
qDebug() << "LoadTest Call";vtkSmartPointer<vtkPLYReader> reader{ vtkSmartPointer<vtkPLYReader>::New() }; reader->SetFileName(u8"C:\\Users\\dbzho\\OneDrive\\Desktop\\Field_Training\\vtk\\vtk_실습용_프로젝트\\upperJaw_1.ply"); reader->Update(); polyData = reader->GetOutput(); // 클리핑 전 RGB 색상 정보를 저장합니다. vtkUnsignedCharArray* originalColors{ vtkUnsignedCharArray::SafeDownCast(polyData->GetPointData()->GetScalars()) }; // 클리핑된 폴리데이터의 각 점에 대한 새로운 RGB 색상 배열을 생성합니다. clippedColors = vtkUnsignedCharArray::New(); clippedColors->SetNumberOfComponents(3); // R, G, B clippedColors->SetName("Colors"); // Create a new array to store the HSV values hsvValues = vtkFloatArray::New(); hsvValues->SetNumberOfComponents(3); hsvValues->SetName("HSVValues"); for (int i{}; i < originalColors->GetNumberOfTuples(); ++i){ float r{ static_cast<float>(originalColors->GetComponent(i, 0)) }; float g{ static_cast<float>(originalColors->GetComponent(i, 1)) }; float b{ static_cast<float>(originalColors->GetComponent(i, 2)) }; // Convert RGB to HSV float maxVal{ std::max(std::max(r, g), b) }; float minVal{ std::min(std::min(r, g), b) }; float delta{ maxVal - minVal }; float hue, saturation, value; if (0==delta ){ hue = 0; saturation = 0; value = maxVal; } else{ if (maxVal == r) hue = (g - b) / delta; else if (maxVal == g) hue = 2 + (b - r) / delta; else hue = 4 + (r - g) / delta; hue *= 60; if (0>hue ) hue += 360; saturation = delta / maxVal; value = maxVal; } // 변환된 RGB 값을 vtkFloatArray에 추가합니다. hsvValues->InsertNextTuple3(hue, saturation, value); } polyData->GetPointData()->SetScalars(hsvValues); // 클리핑을 수행합니다. vtkClipPolyData* clipper{ vtkClipPolyData::New() }; clipper->SetInputData(polyData); clipper->SetValue(20); clipper->Update(); // 클리핑된 폴리데이터를 가져옵니다. polyData = clipper->GetOutput(); // 클리핑된 폴리데이터의 각 점에 대해 가장 가까운 원본 점의 색상을 매핑합니다. for (int i{}; i < polyData->GetNumberOfPoints(); ++i) clippedColors->InsertNextTuple3(255, 255, 255); // 클리핑된 폴리데이터에 매핑된 색상 정보를 설정합니다. polyData->GetPointData()->SetScalars(clippedColors); // Visualize mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputData(polyData); actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New(); renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData()); //QVTKOpenGLNativeWidget::interactor()->Render(); renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
}
//This is the part where we load the ply file using VTK.
void QtWidgetsApplication1::test()
{
ui.pushButton->setText(QCoreApplication::translate("QtWidgetsApplication1Class", "pressed", nullptr));this->widget->renderWindow->AddRenderer(this->widget->renderer); this->widget->renderWindow->SetWindowId((void*)this->widget->winId()); this->widget->interactor->SetRenderWindow(this->widget->renderWindow); this->widget->renderWindow->Render(); this->widget->interactor->Start();
}
//This will show the PLY file inside the widget, which will not show the PLY file when you move the mouse.
//When I do this, the PLY file is displayed inside the widget, but when I move the mouse, the PLY file is not displayed.
//What if you want a PLY file to be output from within the widget when the mouse moves?
//this->widget->renderWindow->SetWindowId((void*)this->widget->winId());
If you comment out this code, the fly file will not be output within the widget and will open in a new window, but the fly file will be rendered when you move the mouse. However, you do not want the fly file to disappear within the widget when you move the mouse.//I've attached the GitHub address. I'd appreciate it if you could check it out while you're busy.