Pick up
- Picking is used to select objects in the scene and obtain information about the objects
- Usually call vtkAbstractPicker::Pick() method to pick up the object
- Different picking subclasses return different picking information, such as the object's global coordinates, unit ID, point ID, etc.
- Calling syntax: Pick (SelectX, SelectY, SelectZ, Renderer)
- The characters associated with the Renderer are all picked objects
- The Pick method cannot be called directly, but is managed by the interactive class vtkRenderWindowInteractor
Main picking subcategories
Pickup | effect | return value |
---|---|---|
vtkWorldPointPicker | Quickly pick up the characters in the scene | Returns the world coordinate value of the picked point |
vtkAbstractPropPicker | Pick up objects in the scene | Returns the data type of the picked object |
vtkPropPicker:vtkAbstractPropPicker | Pickup operation for hardware support | – |
vtkPicker:vtkAbstractPropPicker | Use software to realize boundary picking | – |
vtkCellPicker:vtkPicker | Information used to pick up the unit | Return unit ID, parameter coordinates |
vtkPointPicker:vtkPicker | Used to get point information | Return the ID and coordinates of the point |
vtkAssemblyPath | To access every object in vtkAssembly | Returns the node and transformation matrix of each object |
Use pick objects in interactive subclasses to meet user-defined interactive picking operations
//构建一个vtkCellPicker的实例
vtkSmartPointer<vtkCellPicker> cellPicker = vtkSmartPointer<vtkCellPicker>::New();
//构建vtk内建的交互类对象
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
//将pick对象交由交互类vtkRenderWindowInteractor进行管理
iren->SetPicker(cellPicker);
//将绘制窗口对象与绘制交互类绑定
iren->SetRenderWindow(renWin);
vtkSmartPointer<MyPointPickerInteractorStyle> style = vtkSmartPointer<MyPointPickerInteractorStyle>::New();
iren->SetInteractorStyle(style);
class MyPointPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static MyPointPickerInteractorStyle* New();
vtkTypeMacro(MyPointPickerInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnLeftButtonDown()
{
vtkRenderWindowInteractor *rwi = this->Interactor;
auto render = rwi->GetRenderWindow()->GetRenderers()->GetFirstRenderer();
double screenPoint[3];
screenPoint[0] = rwi->GetEventPosition()[0];
screenPoint[1] = rwi->GetEventPosition()[1];
screenPoint[2] = rwi->GetEventPosition()[2];
auto detailPicker = (vtkCellPicker*)(this->Interactor->GetPicker());
vtkIdType pointId = 0;
if (detailPicker->Pick(screenPoint[0], screenPoint[1], 0, render))
{
vtkIdType cellID = detailPicker->GetCellId();
pointId = detailPicker->GetPointId();
}
}
};
vtkStandardNewMacro(MyPointPickerInteractorStyle);
VTK coordinate system
!(C:\Users\qiang.shen\Downloads\VTK-8.2.0\jpg picture\picture three_VTK coordinate conversion.JPG)
- vtk supports many different types of coordinate systems
- vtk uses the class vtkCoordinate to transform in different coordinate systems
Mainly involved coordinate system
Coordinate System | meaning | Remarks |
---|---|---|
DISPLAY | The origin is at the lower left corner of the window, and the x/y length is the pixel value of the width and height of the window | – |
NORMALIZED DISPLAY | The origin is in the lower left corner of the window, and the length of x/y is 0~1 | – |
VIEWPORT | A window can be divided into multiple viewports, the lower left corner of each viewport is the origin, and the x/y length is the pixel value of the viewport width and height | – |
NORMALIZED VIEWPORT | A window can be divided into multiple viewports, the lower left corner of each viewport is the origin, and the x/y length is 0~1 | – |
VIEWPORT | A window can be divided into multiple viewports, the lower left corner of each viewport is the origin, and the x/y length is the pixel value of the viewport width and height | – |
NORMALIZED VIEWPORT | A window can be divided into multiple viewports, the lower left corner of each viewport is the origin, and the x/y length is 0~1 | – |
VIEW | Clipping coordinate system in camera space, xyz is -1~1, z direction is depth | – |
WORLD | Unified global time coordinate system, xyz is the global actual length | – |
MODEL | The coordinate system used when defining the model, usually a local Cartesian coordinate system | – |
A demo using vtkCoordinate
#include <vtkSmartPointer.h>
#include <vtkCoordinate.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
int main(int, char *[])
{
vtkSmartPointer<vtkRenderWindow> rendererWindow = vtkSmartPointer<vtkRenderWindow>::New();
rendererWindow->SetSize(400, 400);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
rendererWindow->AddRenderer(renderer);
rendererWindow->Render();
vtkSmartPointer<vtkCoordinate> coordinate = vtkSmartPointer<vtkCoordinate>::New();
//设置当前的坐标系为归一化的屏幕坐标系
coordinate->SetCoordinateSystemToNormalizedDisplay();
//设置当前的坐标系为屏幕坐标系
//coordinate->SetCoordinateSystemToDisplay();
//设置当前的坐标系为视口坐标系
//coordinate->SetCoordinateSystemToViewport();
//设置当前的坐标系为归一化视口坐标系
//coordinate->SetCoordinateSystemToNormalizedViewport();
//设置当前的坐标系为裁剪坐标系
//coordinate->SetCoordinateSystemToView();
//设置当前的坐标系为世界坐标系
//coordinate->SetCoordinateSystemToWorld()
//取当前坐标系下的一个坐标(0.5, 0,5, 0)
coordinate->SetValue(0.5, 0.5, 0);
std::cout << *coordinate << std::endl;
std::cout << coordinate->GetCoordinateSystemAsString() << std::endl;
int* val;
//将设定的坐标转换为屏幕坐标
val = coordinate->GetComputedDisplayValue(renderer);
std::cout << "Val: " << val[0] << " " << val[1] << std::endl;
getchar();
return EXIT_SUCCESS;
}
vtkActor2D
Express two-dimensional graphics and perform operations on two-dimensional graphics. In the following example, draw discrete two-dimensional point graphics
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkActor2D.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty2D.h>
int main(int, char *[])
{
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(10,10,0);
points->InsertNextPoint(100,100,0);
points->InsertNextPoint(200,200,0);
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
glyphFilter->SetInputData(polydata);
glyphFilter->Update();
vtkSmartPointer<vtkPolyDataMapper2D> mapper = vtkSmartPointer<vtkPolyDataMapper2D>::New();
mapper->SetInputConnection(glyphFilter->GetOutputPort());
mapper->Update();
vtkSmartPointer<vtkActor2D> actor = vtkSmartPointer<vtkActor2D>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderWindow->SetSize(300,300);
actor->GetProperty()->SetColor(0,1,0);
actor->GetProperty()->SetPointSize(3);
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
Annotation
Plane annotation
Put the text on the two-dimensional graphics window, the text will not change due to the user's operation
#include "vtkSmartPointer.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTextProperty.h"
#include "vtkTextActor.h"
#include "vtkTextMapper.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkAutoInit.h"
#include "vtkScaledTextActor.h"
int main()
{
VTK_MODULE_INIT(vtkRenderingFreeType)
//建立文本属性对象
vtkSmartPointer<vtkTextProperty> pTexProper = vtkSmartPointer<vtkTextProperty>::New();
pTexProper->SetColor(0 , 0 , 1);
pTexProper->SetFontSize(18);
pTexProper->SetFontSize(18);
pTexProper->SetFontFamily(0);
pTexProper->SetJustification(1);
pTexProper->SetBold(1);
pTexProper->SetItalic(1);
pTexProper->SetShadow(1);
//建立文本映射器对象
//vtkTextMapper : public vtkMapper2D
vtkSmartPointer<vtkTextMapper> pTexMap = vtkSmartPointer<vtkTextMapper>::New();
//pTexMap->SetInput("This is a sphere");
pTexMap->SetTextProperty(pTexProper);
//vtkSmartPointer<vtkPolyDataMapper2D> pp = vtkPolyDataMapper2D::SafeDownCast(pTexMap);
vtkSmartPointer<vtkScaledTextActor> textActor = vtkSmartPointer<vtkScaledTextActor>::New();
//vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
//SetMapper(vtkMapper2D)为私有方法
textActor->SetInput("This is a sphere");
//textActor->SetMapper(pTexMap);//设置文本属性有问题
textActor->SetHeight(0.1);
//设置字体显示的位置
textActor->SetPosition(0 , 0);
//创建三维球体
vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphere->GetOutputPort());
vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->AddActor(sphereActor);
ren->AddActor(textActor);
vtkSmartPointer<vtkRenderWindow> win = vtkSmartPointer<vtkRenderWindow>::New();
win->AddRenderer(ren);
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(win);
win->Render();
iren->Start();
getchar();
return 0;
}
3D annotation
Add text to the generated 3D graphics, or display text directly in the form of 3D graphics
- Three-dimensional annotation is to use vtkVectorText to create annotation content text
- vtkFollower is a way to draw 3D annotations
//建立3D 文本,为多边形数据
vtkSmartPointer<vtkVectorText> atext = vtkSmartPointer<vtkVectorText>::New();
atext->SetText("Origin");
//映射文本,注意映射器的类型
vtkSmartPointer<vtkPolyDataMapper> textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
textMapper->SetInputConnection(atext->GetOutputPort());
vtkSmartPointer<vtkFollower> textActor = vtkSmartPointer<vtkFollower>::New();
textActor->SetMapper(textMapper);
textActor->SetPosition(0 , -0.1 , 0);
textActor->SetScale(0.2 , 0.2 , 0.2);