ぬの部屋(仮)
nu-no-he-ya
  •    1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
    1234567
    891011121314
    15161718192021
    22232425262728
           
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
         12
    3456789
    10111213141516
    17181920212223
    242526272829 
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
        123
    45678910
    11121314151617
    18192021222324
    25262728   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    15161718192021
    293031    
           
         12
    3456789
    10111213141516
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
          1
    2345678
    9101112131415
    16171819202122
    232425262728 
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
  • Blender Pythonで3Dカーソル周辺の頂点を分離する

    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 9.0で表示(7) + win32api

    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 9.0 で表示(6) ライトを追加

    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 9.0 で表示(5) ポリゴンの頂点色を付けて表示

    //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 9.0 で表示(4) ポリゴンを表示

    //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;
    }
    

    vtk 9.0 で表示(3) マウスピック

    マウスクリックされたオブジェクトを特定する。

    参考: 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;
    }