スポンサーリンク
VTKでPointCloudを表示する方法。VTKにはPointCloud型というものがなく、PolyDataで点群も表すようになっている。元となったコードがPythonだったのでそれをC++に書き換えた格好になる。
元コード(Python)
https://stackoverflow.com/questions/7591204/how-to-display-point-cloud-in-vtk-in-different-colors
#pragma once // https://stackoverflow.com/questions/7591204/how-to-display-point-cloud-in-vtk-in-different-colors #include <vtkRenderer.h> #include <vtkSmartPointer.h> #include <vtkPolyData.h> //vtkPolyData用 #include <vtkPolyDataMapper.h> #include <vtkPointData.h> // 頂点データ用 #include <vtkUnsignedCharArray.h> //色データ用 #include <vtkProperty.h> //#include <vtkPoints.h> //#include <vtkPolygon.h> class VtkPointCloud { public: vtkSmartPointer<vtkPolyData> _polydata; vtkSmartPointer<vtkPolyDataMapper> _mapper; vtkSmartPointer<vtkActor> _actor; vtkSmartPointer < vtkPoints > _points; vtkSmartPointer < vtkCellArray > _cells; vtkSmartPointer < vtkUnsignedCharArray > _colors; VtkPointCloud(double zMin = -10.0, double zMax = 10.0, int maxNumPoints = 1e6) { _polydata = vtkSmartPointer<vtkPolyData>::New(); clearPoints(); _mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); _mapper->SetInputData(_polydata); _mapper->SetColorModeToDefault(); //_mapper->SetScalarRange(zMin, zMax); _mapper->SetScalarVisibility(1); _actor = vtkSmartPointer<vtkActor>::New(); _actor->SetMapper(_mapper); } void addPoint(double point[3], unsigned char color[3]) { vtkIdType pointId = _points->InsertNextPoint(point); _cells->InsertNextCell(1); _cells->InsertCellPoint(pointId); _colors->InsertNextTuple3(color[0], color[1], color[2]); } void clearPoints() { _points = vtkSmartPointer < vtkPoints >::New(); _cells = vtkSmartPointer < vtkCellArray>::New(); //////////////////////////////////// _colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); _colors->SetNumberOfComponents(3); _colors->SetName("Colors"); _polydata->GetPointData()->SetScalars(_colors); _polydata->GetPointData()->SetActiveScalars("Colors"); //////////////////////////////////// _polydata->SetPoints(_points); _polydata->SetVerts(_cells); } };
#pragma once #include "VTK_PointCloud.hpp" // VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkWin32RenderWindowInteractor.h> //win32api対応 #include <vtkInteractorStyleImage.h> //////////////////////////////////////////////////////// #pragma comment(lib,"vtkCommonCore-9.1.lib") #pragma comment(lib,"vtkRenderingCore-9.1.lib") #pragma comment(lib,"vtkInteractionStyle-9.1.lib") #pragma comment(lib,"vtkCommonDataModel-9.1.lib ") // ポリゴン用 #pragma comment(lib,"vtkRenderingOpenGL2-9.1.lib") //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); int main(int argc, char** argv) {
// 点群生成
auto ptc = std::make_unique< VtkPointCloud>(); for (size_t i = 0; i < 100000; i++) { double x = (double)rand() / (double)RAND_MAX; double y = (double)rand() / (double)RAND_MAX; double z = (double)rand() / (double)RAND_MAX; ptc->addPoint( std::array<double, 3>{ x, y, z }.data(), std::array<unsigned char, 3>{ (unsigned char)(x * 255), (unsigned char)(y * 255), (unsigned char)(z * 255)}.data() ); } // 頂点サイズを指定する場合 // #include <vtkProperty.h> ptc->_actor->GetProperty()->SetPointSize(3); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(ptc->_actor); renderer->ResetCamera(); ////////////////////////////////////// auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); interactor->SetInteractorStyle(style); ////////////////////////////////////// auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); interactor->Start(); //イベントループへ入る return 0; }