• 三维控件中定位一个点_vtkPointWidget



    开发环境:

    1. Windows 11 家庭中文版
    2. Microsoft Visual Studio Community 2019
    3. VTK-9.3.0.rc0
    4. vtk-example
    5. 参考代码

    demo解决问题:允许用户使用三维光标在三维空间中定位一个点。关键类vtkPointWidget , 光标具有轮廓边界框、轴对齐十字准线和轴阴影(轮廓和阴影可以关闭)。(可以关闭轮廓和阴影)。vtkPointWidget 和其他 3D widget 一样,具有一个很好的特点,即它可以与当前的交互样式一起工作。也就是说,如果 vtkPointWidget 没有处理事件,那么所有其他已注册的观察者(包括交互样式)都有机会处理该事件。否则,vtkPointWidget 将终止处理它所处理的事件。

    在这里插入图片描述

    主流程:(不看probe)

    1. 数据源1:构造一个网格化的sphereSource数据源
    2. 数据源2:point的位置使用cone符号化为圆锥体
    3. 数据源3:添加一个AddActor2D,固定在视口左下角
    4. 数据源4:构造3D控件pointWidget,并添加观察者myCallback,监控pointWidget交互事件

    注意:point符号化的过程中,一开始是没有符号的,所以圆锥体一开始不显示,交互时间开始后设置了point的值,点背符号化后有了圆锥体,Execute中关键代码:

    //获取定义该点的多边形数据(包括点)。单个点和一个顶点组成 vtkPolyData。
    pointWidget->GetPolyData(this->PolyData);//给this->PolyData / point 赋值,在多个管道中间中途修改值,修改后update修改过的管道,render
    this->PositionActor->SetInput(text.str().c_str());
    
    • 1
    • 2
    • 3

    glyph的输入我把probefilter删了,直接用point数据,也是可以相同效果的,目前没有明白为什么要加一个vtkProbeFilter,理解的帮解答下,谢谢拉!!!

    另一个需要重点关注的是需要区分以下接口接口

      vtkNew<vtkProbeFilter> probe;
      //指定一个数据对象作为输入。请注意,此方法不会建立管道连接。使用 SetInputConnection() 来 建立管道连接。
      probe->SetInputData(point);//输入: 此时的point值为空,需要事件内根据鼠标位置进行赋值
      //指定将在输入点进行探测的数据集。 
      //输入为输出提供几何图形(点和单元)、 而源点则通过探测(插值)生成标量、 矢量等。
      probe->SetSourceData(inputPolyData);//源: 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参考链接1
    参考链接2
    参考链接3

      vtkNew<vtkGlyph3D> glyph;
      // glyph->SetInputConnection(probe->GetOutputPort());//???不理解
      glyph->SetInputData(point); //此处直接使用point也可以达到效果,但是为什么非要用vtkProbeFilter 没有想通
      glyph->SetSourceConnection(cone->GetOutputPort());
    
    • 1
    • 2
    • 3
    • 4

    参考链接1
    参考链接2


    prj name: Arbitrary3DCursor

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    // This does the actual work: updates the probe.
    // Callback for the interaction.
    class vtkmyPWCallback : public vtkCallbackCommand
    {
    public:
      vtkmyPWCallback() = default;
    
      static vtkmyPWCallback* New()
      {
        return new vtkmyPWCallback;
      }
      virtual void Execute(vtkObject* caller, unsigned long, void*)
      {
        vtkPointWidget* pointWidget = reinterpret_cast<vtkPointWidget*>(caller);
        //获取定义该点的多边形数据(包括点)。单个点和一个顶点组成 vtkPolyData。
        pointWidget->GetPolyData(this->PolyData);//给this->PolyData / point 赋值,在多个管道中间中途修改值,修改后update修改过的管道,render
        double position[3];
        pointWidget->GetPosition(position);
        std::ostringstream text;
        text << "cursor: " << std::fixed << std::setprecision(4) << position[0]
             << ", " << position[1] << ", " << position[2];
        this->PositionActor->SetInput(text.str().c_str());
        // this->CursorActor->VisibilityOn();
        std::cout << PolyData->GetNumberOfCells() << std::endl;
        std::cout << PolyData->GetNumberOfPoints() << std::endl;
        std::cout << PolyData->GetNumberOfPolys() << std::endl;
    
      }
    
      vtkPolyData* PolyData = nullptr;      //与传入的锥形有关
      //vtkActor* CursorActor = nullptr;    //可以不需要,如果需要控制显示隐藏状态,可以传入
      vtkTextActor* PositionActor = nullptr;//实时显示坐标状态
    };
    
    int main(int argc, char* argv[])
    {
      vtkSmartPointer<vtkPolyData> inputPolyData;
    
      if (argc > 1)
      {
        vtkNew<vtkXMLPolyDataReader> reader;
        reader->SetFileName(argv[1]);
        reader->Update();
        inputPolyData = reader->GetOutput();
      }
      else
      {
        vtkNew<vtkSphereSource> sphereSource;
        sphereSource->SetPhiResolution(15);
        sphereSource->SetThetaResolution(15);
        sphereSource->Update();
        inputPolyData = sphereSource->GetOutput();
      }
    
      vtkNew<vtkNamedColors> colors;
    
      vtkNew<vtkPolyData> point;
    
      //https://blog.csdn.net/liushao1031177/article/details/122860254
      //https://blog.csdn.net/yuyangyg/article/details/78165570
      //https://www.cnblogs.com/ankier/p/3166210.html
      /*
        在指定点位置采样数据值
    
        vtkProbeFilter 是一个过滤器,用于计算指定点位置的点属性(如标量、矢量等)。
            该过滤器有两个输入:输入和源。输入的几何结构通过过滤器。
            通过对源数据进行插值,在输入点位置计算出点属性。
            例如,我们可以根据体积(源数据)计算平面(指定为输入的平面)上的数据值。
            源数据的单元格数据会根据每个输入点所在的源单元格复制到输出端。
            如果源点数据和单元格数据中都存在同名数组,则只探查点数据中的数组。
    
        该过滤器可用于重新采样数据,或将一种数据集形式转换为另一种数据集形式。
            例如,非结构化网格(vtkUnstructuredGrid)可以用体积(三维 vtkImageData)进行探测,然后使用体积渲染技术将结果可视化。
            另一个例子:可以使用一条直线或曲线来探测数据,以生成沿该直线或曲线的 x-y 图。
    
        警告
        vtkProbeFilter 的一个关键算法组件是其查找包含探测点的单元格的方式。
            默认情况下,vtkDataSet::FindCell() 方法会被使用,该方法反过来使用 vtkPointLocator 来执行加速搜索。
            不过,在某些情况下,使用 vtkPointLocator 可能无法识别包围单元格。
            更稳健但更慢的方法是使用 vtkCellLocator 执行 FindCell() 操作(通过指定 CellLocatorPrototype)。
            最后,可以通过指定 vtkFindCellStrategy 的实例来配置更高级的搜索。
                (注意:图像数据探测从不使用定位器,因为查找包含的单元格是一个简单、快速的操作。
                因此指定 vtkFindCellStrategy 或单元格定位器原型没有任何作用)。
        vtkProbeFilter 一旦找到包含查询点的单元格,就会使用单元格的插值函数来执行插值/计算点属性。
            vtkPointInterpolator 支持多种广义内核,而 vtkSPHInterpolator 则支持多种 SPH 内核。
      */
      vtkNew<vtkProbeFilter> probe;
      //指定一个数据对象作为输入。请注意,此方法不会建立管道连接。使用 SetInputConnection() 来 建立管道连接。
      probe->SetInputData(point);//输入: 此时的point值为空,需要事件内根据鼠标位置进行赋值
      //指定将在输入点进行探测的数据集。 
      //输入为输出提供几何图形(点和单元)、 而源点则通过探测(插值)生成标量、 矢量等。
      probe->SetSourceData(inputPolyData);//源: 
    
      std::cout << point->GetNumberOfCells() << std::endl;
      std::cout << point->GetNumberOfPoints() << std::endl;
      std::cout << point->GetNumberOfPolys() << std::endl;
    
      // Create glyph.
      vtkNew<vtkConeSource> cone;
      cone->SetResolution(30);
    
      //https://blog.csdn.net/jigetage/article/details/86633156
      //https://www.cnblogs.com/vaughnhuang/p/17584058.html
      vtkNew<vtkGlyph3D> glyph;
      // glyph->SetInputConnection(probe->GetOutputPort());//???不理解
      glyph->SetInputData(point); //此处直接使用point也可以达到效果,但是为什么非要用vtkProbeFilter 没有想通
      glyph->SetSourceConnection(cone->GetOutputPort());
      glyph->SetVectorModeToUseVector();
      glyph->SetScaleModeToDataScalingOff();
      glyph->SetScaleFactor(inputPolyData->GetLength() * 0.1);
    
      vtkNew<vtkPolyDataMapper> glyphMapper;
      glyphMapper->SetInputConnection(glyph->GetOutputPort());
    
      vtkNew<vtkActor> glyphActor;
      glyphActor->SetMapper(glyphMapper);
      glyphActor->VisibilityOn();//point为空,没有glyph显示
    
      vtkNew<vtkPolyDataMapper> mapper;
      mapper->SetInputData(inputPolyData);
    
      vtkNew<vtkActor> actor;
      actor->SetMapper(mapper);
      actor->GetProperty()->SetRepresentationToWireframe();
      actor->GetProperty()->SetColor(colors->GetColor3d("gold").GetData());
    
      vtkNew<vtkTextActor> textActor;
      textActor->GetTextProperty()->SetFontSize(12);
      textActor->SetPosition(10, 20);
      textActor->SetInput("cursor:");
      textActor->GetTextProperty()->SetColor(colors->GetColor3d("White").GetData());
    
      // Create the RenderWindow, Render1er and both Actors.
      vtkNew<vtkRenderer> ren1;
      vtkNew<vtkRenderWindow> renWin;
      renWin->AddRenderer(ren1);
    
      vtkNew<vtkRenderWindowInteractor> iren;
      iren->SetRenderWindow(renWin);
    
      // The SetInteractor method is how 3D widgets are associated with the render
      // window interactor. Internally, SetInteractor sets up a bunch of callbacks
      // using the Command/Observer mechanism (AddObserver()).
      vtkNew<vtkmyPWCallback> myCallback;
      myCallback->PolyData = point;
      //myCallback->PolyData = inputPolyData;
      //myCallback->CursorActor = glyphActor;
      myCallback->PositionActor = textActor;
    
      // The point widget is used probe the dataset.
      vtkNew<vtkPointWidget> pointWidget;
      pointWidget->SetInteractor(iren);
      pointWidget->SetInputData(inputPolyData);//指定移动范围
      pointWidget->AllOff();
      pointWidget->PlaceWidget();
      pointWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);
    
      ren1->AddActor(glyphActor);
      ren1->AddActor(actor);
      ren1->AddActor2D(textActor);//2D actor
    
      // Add the actors to the renderer, set the background and size.
      ren1->GradientBackgroundOn();
      ren1->SetBackground(colors->GetColor3d("SlateGray").GetData());
      ren1->SetBackground2(colors->GetColor3d("Wheat").GetData());
    
      renWin->SetSize(300, 300);
      renWin->SetWindowName("Arbitrary3DCursor");
      renWin->Render();
      pointWidget->On();
    
      // Render the image
      iren->Initialize();
      renWin->Render();
    
      iren->Start();
    
      return EXIT_SUCCESS;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
  • 相关阅读:
    图像Star特征提取
    数据集笔记:分析OpenCellID 不同radio/ create_time update_time可视化
    AI爆文变现-写作项目-脚本配置教程-解放双手
    品牌线上控价,如何平台投诉
    uva1344
    爆料,前华为微服务专家纯手打500页落地架构实战笔记,已开源
    【LeetCode】链表题解汇总
    spring boot 显示数据库中图片
    开发常用指令
    html资源提示符
  • 原文地址:https://blog.csdn.net/chengfenglee/article/details/134563802