import bpy from bpy import context from mathutils import Vector radius = 30 # 3Dカーソルの位置を取得 center = bpy.context.scene.cursor.location # 選択中のオブジェクトを取得 obj = context.active_object bpy.ops.object.mode_set(mode="OBJECT") for v in obj.data.vertices: # 3Dカーソルの周辺の頂点以外を選択 if (center - v.co).length < radius: v.select = False else: v.select = True bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.separate(type='SELECTED') bpy.ops.object.mode_set(mode="OBJECT")
VTKではなく、自作のウィンドウにレンダリングする方法。
#pragma once // VTK内でstd::minにひっかからないため #define NOMINMAX #include<Windows.h> // VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkWin32RenderWindowInteractor.h> //win32api対応 #include <vtkInteractorStyleImage.h> //円筒とその表示に必要 #include <vtkCylinderSource.h> #include <vtkPolyDataMapper.h> #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") #pragma comment(lib,"vtkRenderingUI-9.0.lib") //win32api対応 #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); vtkSmartPointer<vtkRenderWindowInteractor> iren; vtkSmartPointer<vtkRenderWindow> renWin;
void init_vtk(HWND hwnd);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { RECT rect; GetClientRect(hwnd, &rect); switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_ERASEBKGND://背景の描画を処理したことにする(ちらつき防止) return TRUE; case WM_PAINT: if (renWin) { //画面描画 renWin->Render(); } return 0; case WM_SIZING: //ウィンドウサイズ設定 renWin->SetSize(rect.right, rect.bottom); InvalidateRect(hwnd, nullptr, FALSE); return 0; } return DefWindowProc(hwnd, msg, wp, lp); }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nCmdShow) { HWND hwnd; WNDCLASS winc; MSG msg; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL, IDI_APPLICATION); winc.hCursor = LoadCursor(NULL, IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("VTK-TEST"); if (!RegisterClass(&winc)) return 0; hwnd = CreateWindow( TEXT("VTK-TEST"), TEXT("vtk test"), WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL ); if (hwnd == NULL) return 0;
init_vtk(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) DispatchMessage(&msg); return msg.wParam; }
void init_vtk(HWND hwnd) { ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // Create a vtkCylinderSource vtkSmartPointer<vtkCylinderSource> cylinderSource = vtkSmartPointer<vtkCylinderSource>::New(); cylinderSource->SetCenter(0.0, 0.0, 0.0); cylinderSource->SetRadius(5.0); cylinderSource->SetHeight(7.0); cylinderSource->SetResolution(100); // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(cylinderSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); renderer->ResetCamera(); ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// // VTKの表示領域をWin32apiのウィンドウに貼り付ける RECT rect; GetClientRect(hwnd, &rect); renWin = vtkRenderWindow::New(); renWin->AddRenderer(renderer); renWin->SetParentId(hwnd); renWin->SetSize(rect.right, rect.bottom); iren = vtkSmartPointer<vtkWin32RenderWindowInteractor>::New(); vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); iren->SetInteractorStyle(style); iren->SetRenderWindow(renWin); }
VTKでライトを使用。問題はデフォルトのカメラの位置からのライトがどうやっても消せないことで、ひとまずRender()の後にRemoveAllLightsで全てのライトを消すことで対処している。
より大きな問題は一番最初の描画ではライトの設定が反映されていないものが描画されること。多分バッファの更新ができていないのだと思う。要検討。
参考:
https://vtk.org/Wiki/VTK/Examples/Cxx/Lighting/Light
//VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> //円筒とその表示に必要 #include <vtkCylinderSource.h> #include <vtkPolyDataMapper.h> //ライトに必要 #include <vtkLight.h> #include <vtkLightCollection.h> #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") //必須 #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); vtkSmartPointer<vtkActor> create_cylinder_and_map();
vtkSmartPointer<vtkLight> mylight() { double lightPosition[3] = { 0, 0, 3 }; // Create a light double lightFocalPoint[3] = { 0,0,0 }; vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New(); light->SetLightTypeToSceneLight(); light->SetPosition(lightPosition[0], lightPosition[1], lightPosition[2]); light->SetFocalPoint(lightFocalPoint[0], lightFocalPoint[1], lightFocalPoint[2]); light->SetDiffuseColor(1, 0, 0); light->SetAmbientColor(0, 1, 0); light->SetSpecularColor(0, 0, 1); //light->SetConeAngle(10); //light->SetPositional(true); // required for vtkLightActor below return light; }
int main(int /*argc*/, char ** /*argv*/) { ////////////////////////////////////// auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->ResetCamera(); ////////////////////////////////////// vtkSmartPointer<vtkActor> cylinder_actor = create_cylinder_and_map(); renderer->AddActor(cylinder_actor); ////////////////////////////////////// vtkSmartPointer<vtkLight> light = mylight(); ////////////////////////////////////// auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); ////////////////////////////////////// auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); // 効果がない(?) renderer->LightFollowCameraOff(); vtkLightCollection* originalLights = renderer->GetLights(); std::cout << "Originally there are " << originalLights->GetNumberOfItems() << " lights." << std::endl; //renderer->RemoveLight((vtkLight*)originalLights->GetItemAsObject(0)); renderer->RemoveAllLights();// デフォルトのライトを消すために入れている // renderWindow->Render() の後にAddLightする renderer->AddLight(light); interactor->Start(); //イベントループへ入る return 0; }
vtkSmartPointer<vtkActor> create_cylinder_and_map() { // Create a vtkCylinderSource vtkSmartPointer<vtkCylinderSource> cylinderSource = vtkSmartPointer<vtkCylinderSource>::New(); cylinderSource->SetCenter(0.0, 0.0, 0.0); cylinderSource->SetRadius(5.0); cylinderSource->SetHeight(7.0); cylinderSource->SetResolution(100); // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(cylinderSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); return actor; }
//VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkPoints.h> // vtkPoints用 #include <vtkPolygon.h> // vtkPolygon用 #include <vtkPolyData.h> //vtkPolyData用 #include <vtkPolyDataMapper.h> #include <vtkUnsignedCharArray.h> //色データ用 #include <vtkPointData.h> // 頂点データ用 //必須 #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") #pragma comment(lib,"vtkCommonDataModel-9.0.lib ") // ポリゴン用 #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); int main(int /*argc*/, char ** /*argv*/) { ////////////////////////////////////// vtkSmartPointer < vtkPoints > points = vtkSmartPointer < vtkPoints >::New(); points->InsertNextPoint(0.0, 0.0, 0.0); points->InsertNextPoint(1.0, 0.0, 0.0); points->InsertNextPoint(1.0, 1.0, 0.0); points->InsertNextPoint(0.0, 0.4, 0.0); ////////////////////////////////////// vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New(); polygon->GetPointIds()->SetNumberOfIds(4);//4頂点 polygon->GetPointIds()->SetId(0, 0); polygon->GetPointIds()->SetId(1, 1); polygon->GetPointIds()->SetId(2, 2); polygon->GetPointIds()->SetId(3, 3); ////////////////////////////////////// vtkSmartPointer<vtkCellArray> polylist = vtkSmartPointer<vtkCellArray>::New(); polylist->InsertNextCell(polygon); ////////////////////////////////////// vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New(); polydata->SetPoints(points); polydata->SetPolys(polylist);
//////////////////////////////////////////// //////////////////////////////////////////// vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); colors->SetNumberOfComponents(3); colors->SetName("Colors"); colors->InsertNextTuple3(255, 0, 0); colors->InsertNextTuple3(0, 255, 0); colors->InsertNextTuple3(0, 0, 255); colors->InsertNextTuple3(255, 255, 0); polydata->GetPointData()->SetScalars(colors); //////////////////////////////////////////// ////////////////////////////////////////////
////////////////////////////////////// // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputData(polydata); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); ////////////////////////////////////// auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); renderer->ResetCamera(); ////////////////////////////////////// auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); ////////////////////////////////////// auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); interactor->Start(); //イベントループへ入る return 0; }
//VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkPoints.h> // vtkPoints用 #include <vtkPolygon.h> // vtkPolygon用 #include <vtkPolyData.h> //vtkPolyData用 #include <vtkPolyDataMapper.h> #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") #pragma comment(lib,"vtkCommonDataModel-9.0.lib ") // ポリゴン用 //必須 #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); int main(int /*argc*/, char ** /*argv*/) { ////////////////////////////////////// vtkSmartPointer < vtkPoints > points = vtkSmartPointer < vtkPoints >::New(); points->InsertNextPoint(0.0, 0.0, 0.0); points->InsertNextPoint(1.0, 0.0, 0.0); points->InsertNextPoint(1.0, 1.0, 0.0); points->InsertNextPoint(0.0, 0.4, 0.0); ////////////////////////////////////// vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New(); polygon->GetPointIds()->SetNumberOfIds(4);//4頂点 polygon->GetPointIds()->SetId(0, 0); polygon->GetPointIds()->SetId(1, 1); polygon->GetPointIds()->SetId(2, 2); polygon->GetPointIds()->SetId(3, 3); ////////////////////////////////////// vtkSmartPointer<vtkCellArray> polylist = vtkSmartPointer<vtkCellArray>::New(); polylist->InsertNextCell(polygon); ////////////////////////////////////// vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New(); polydata->SetPoints(points); polydata->SetPolys(polylist); ////////////////////////////////////// // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); //mapper->SetInputConnection(cylinderSource->GetOutputPort()); mapper->SetInputData(polydata); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); ////////////////////////////////////// auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); renderer->ResetCamera(); ////////////////////////////////////// auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); //vtkInteractorStyleTrackballCamera ////////////////////////////////////// auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); interactor->Start(); //イベントループへ入る return 0; }
マウスクリックされたオブジェクトを特定する。
参考: https://vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking
//VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleImage.h> #include <vtkPropPicker.h>// ピックに必要 #include <vtkPolyDataMapper.h> #include <vtkPlaneSource.h> #ifdef _DEBUG #pragma comment(lib,"vtkCommonCore-9.0d.lib") #pragma comment(lib,"vtkRenderingCore-9.0d.lib") #pragma comment(lib,"vtkInteractionStyle-9.0d.lib") #pragma comment(lib,"vtkFiltersSources-9.0d.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0d.lib") #pragma comment(lib,"vtkRenderingOpenGL2-9.0d.lib") #else #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") #endif //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); ///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
// 参考:
// https://vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking
// Handle mouse events class MouseInteractorStyle2 : public vtkInteractorStyleTrackballCamera { public: static MouseInteractorStyle2* New(); vtkTypeMacro(MouseInteractorStyle2, vtkInteractorStyleTrackballCamera); virtual void OnLeftButtonDown() { int* clickPos = this->GetInteractor()->GetEventPosition(); // ピック下オブジェクトを取得 auto picker = vtkSmartPointer<vtkPropPicker>::New(); picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer()); // オブジェクトがクリックされていたら位置とアドレスを出力 if (picker->GetActor() != nullptr) { double* pos = picker->GetPickPosition(); printf("(%lf %lf %lf)[%p]\n", pos[0], pos[1], pos[2], picker->GetActor()); } // Forward events vtkInteractorStyleTrackballCamera::OnLeftButtonDown(); } private: };
vtkStandardNewMacro(MouseInteractorStyle2); // Execute application. int main(int, char *[]) { auto planeSource = vtkSmartPointer<vtkPlaneSource>::New(); planeSource->Update(); // Create a polydata object vtkPolyData* polydata = planeSource->GetOutput(); // Create a mapper auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputData(polydata); // Create an actor auto actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); std::cout << "Actor address: " << actor << std::endl; // A renderer and render window auto renderer = vtkSmartPointer<vtkRenderer>::New(); auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); // An interactor auto renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); // Set the custom stype to use for interaction. auto style = vtkSmartPointer<MouseInteractorStyle2>::New(); style->SetDefaultRenderer(renderer); renderWindowInteractor->SetInteractorStyle(style); // Add the actors to the scene renderer->AddActor(actor); renderer->SetBackground(0, 0, 1); // Render and interact renderWindow->Render(); renderWindowInteractor->Initialize(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
//VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> //円筒とその表示に必要 #include <vtkCylinderSource.h> #include <vtkPolyDataMapper.h> #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") //必須 #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") //必須 VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkInteractionStyle); int main(int /*argc*/, char ** /*argv*/) { ////////////////////////////////////// // Create a vtkCylinderSource vtkSmartPointer<vtkCylinderSource> cylinderSource = vtkSmartPointer<vtkCylinderSource>::New(); cylinderSource->SetCenter(0.0, 0.0, 0.0); cylinderSource->SetRadius(5.0); cylinderSource->SetHeight(7.0); cylinderSource->SetResolution(100); ////////////////////////////////////// // Create a mapper and actor vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(cylinderSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); ////////////////////////////////////// auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(actor); renderer->ResetCamera(); ////////////////////////////////////// auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); ////////////////////////////////////// auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); interactor->Start(); //イベントループへ入る return 0; }
投稿順を間違えた。なお、以下のディレクトリ設定さえ間違えなければwarning一つでないほど綺麗にCMakeできる。
VTK-9.0.1をCMakeしようとしたら以下のようなエラーが出た。
原因は以下のディレクトリをWhere is the source codeに指定したことによる。
解凍したフォルダの一番上の階層にあるCMakeListではなく、その一つ下のVTK-9.0.1ディレクトリを指定しなければならない。
// VTK_MODULE_INITに必要 #include <vtkAutoInit.h> #include <vtkSmartPointer.h> #include <vtkTextActor.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #pragma comment(lib,"vtkCommonCore-9.0.lib") #pragma comment(lib,"vtkRenderingCore-9.0.lib") #pragma comment(lib,"vtkInteractionStyle-9.0.lib") #pragma comment(lib,"vtkFiltersSources-9.0.lib") #pragma comment(lib,"vtkCommonExecutionModel-9.0.lib") #pragma comment(lib,"vtkRenderingOpenGL2-9.0.lib") #pragma comment(lib,"vtkRenderingFreeType-9.0.lib") VTK_MODULE_INIT(vtkRenderingOpenGL2); VTK_MODULE_INIT(vtkRenderingFreeType); VTK_MODULE_INIT(vtkInteractionStyle); int main(int /*argc*/, char ** /*argv*/) { auto textActor = vtkSmartPointer<vtkTextActor>::New(); textActor->SetInput("Hello World"); auto renderer = vtkSmartPointer<vtkRenderer>::New(); renderer->AddActor(textActor); renderer->ResetCamera(); auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); renderWindow->SetInteractor(interactor); renderWindow->Render(); interactor->Start(); return 0; }
PointCloudのサンプルは沢山あるのだがランダム頂点を表示するような物ばかりで、任意の頂点を追加する方法がわからず一月近く調べていた。Pythonは普段使わないのでそういうことになったのだが以下がとりあえずの答。
import open3d import numpy # 点群型データ作成 pcd = open3d.geometry.PointCloud() # 座標設定 pcd.points.append([0.0,0.0,0.0]) pcd.points.append([1.0,1.0,1.0]) pcd.points.append([1.0,2.0,0.0]) # 色設定 pcd.colors.append([1.0,0.0,0.0]); pcd.colors.append([0.0,1.0,0.0]); pcd.colors.append([0.0,0.0,1.0]); # 法線設定 pcd.normals.append([1.0,0.0,0.0]); pcd.normals.append([0.0,1.0,0.0]); pcd.normals.append([0.0,0.0,1.0]); open3d.visualization.draw_geometries( [pcd], width=400, height=400, point_show_normal = True )
一応、私のように必要に迫られてPythonやってる人のためにどうやって調べたかを書いておくと、以下のプログラムを書いて走らせる。
import open3d pcd = open3d.io.read_point_cloud("bun000.ply") print( type ( pcd.points ) ) print( type ( pcd.points[0] ) )
すると以下のような結果を得られる。
<class 'open3d.cpu.pybind.utility.Vector3dVector'>
<class 'numpy.ndarray'>
これでどうやらVector3dVectorという型が頂点リストになっているらしいので以下を検索する。
http://www.open3d.org/docs/release/python_api/open3d.utility.Vector3dVector.html
append
(self: open3d.cpu.pybind.utility.Vector3dVector, x: numpy.ndarray[float64[3, 1]])
するとappendぽい関数がある。この読み方、
引数名:データ型 , 引き数名:データ型 , ...
となっているらしい。で、pythonのselfはC++におけるthisなので無視して、与えるのがxでその型がnumpy.ndarrayだと言うのでndarrayについて調べる。
受け手側がndarrayなので、ndarrayには普通の配列[x,y,z]などをそのまま入れられるらしい?ので、最初のコードになる。