1 #include "vtkAutoInit.h"
2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
3 VTK_MODULE_INIT(vtkInteractionStyle);
4
5 #include <vtkSmartPointer.h>
6 #include <vtkImageData.h>
7 #include <vtkImageCanvasSource2D.h>
8 #include <vtkRenderWindow.h>
9 #include <vtkRenderWindowInteractor.h>
10 #include <vtkInteractorStyleImage.h>
11 #include <vtkRenderer.h>
12 #include <vtkImageActor.h>
13
14 int main()
15 {
16 //定义一个vtkImageCanvasSource2D类型对象canvas
17 vtkSmartPointer<vtkImageCanvasSource2D> canvas =
18 vtkSmartPointer<vtkImageCanvasSource2D>::New();
19 //设置画布的像素数据类型为UnsignedChar
20 canvas->SetScalarTypeToUnsignedChar();
21 //设置组分数目
22 canvas->SetNumberOfScalarComponents(1);
23 //设置画布的大小
24 canvas->SetExtent(0, 100, 0, 100, 0, 0);
25 //利用SetDrawColor方法设置绘制矩形颜色
26 canvas->SetDrawColor(0, 0, 0, 0);//黑色
27 //利用FillBox方法在画布中绘制矩形
28 canvas->FillBox(0,100,0,100);
29 canvas->SetDrawColor(255, 0, 0, 0);//白色
30 canvas->FillBox(20,40,20,40);
31 canvas->Update();
32
33 // Create actors
34 vtkSmartPointer<vtkImageActor> redActor =
35 vtkSmartPointer<vtkImageActor>::New();
36 //redActor->SetInput(canvas->GetOutput());
37 redActor->SetInputData(canvas->GetOutput());
38
39 // Define viewport ranges
40 // (xmin, ymin, xmax, ymax)
41 double redViewport[4] = {0.0, 0.0, 1.0, 1.0};
42 // Setup renderers
43 vtkSmartPointer<vtkRenderer> redRenderer =
44 vtkSmartPointer<vtkRenderer>::New();
45 redRenderer->SetViewport(redViewport);
46 redRenderer->AddActor(redActor);
47 redRenderer->ResetCamera();
48 redRenderer->SetBackground(1.0, 1.0, 1.0);
49
50 // Setup render window
51 vtkSmartPointer<vtkRenderWindow> renderWindow =
52 vtkSmartPointer<vtkRenderWindow>::New();
53 renderWindow->AddRenderer(redRenderer);
54 renderWindow->SetSize( 640, 480 );
55 renderWindow->Render();
56 renderWindow->SetWindowName("ImageCanvasSource2D");
57
58 // Setup render window interactor
59 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
60 vtkSmartPointer<vtkRenderWindowInteractor>::New();
61 vtkSmartPointer<vtkInteractorStyleImage> style =
62 vtkSmartPointer<vtkInteractorStyleImage>::New();
63
64 renderWindowInteractor->SetInteractorStyle(style);
65 // Render and start interaction
66 renderWindowInteractor->SetRenderWindow(renderWindow);
67 renderWindowInteractor->Initialize();
68 renderWindowInteractor->Start();
69
70 return EXIT_SUCCESS;
71 }