1 #include <vtkAutoInit.h>
2 VTK_MODULE_INIT(vtkRenderingOpenGL);
3
4 #include <vtkSmartPointer.h>
5 #include <vtkImageCanvasSource2D.h>
6 #include <vtkImageAppendComponents.h>
7 #include <vtkImageActor.h>
8 #include <vtkRenderer.h>
9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkInteractorStyleImage.h>
12
13 int main()
14 {
15 vtkSmartPointer<vtkImageCanvasSource2D> red =
16 vtkSmartPointer<vtkImageCanvasSource2D>::New();
17 red->SetScalarTypeToUnsignedChar();
18 red->SetNumberOfScalarComponents(1);
19 red->SetExtent(0, 100, 0, 100, 0, 0);
20 red->SetDrawColor(0, 0, 0, 0);
21 red->FillBox(0, 100, 0, 100);
22 red->SetDrawColor(255, 0, 0, 0);
23 red->FillBox(20, 40, 20, 40);
24 red->Update();
25
26 vtkSmartPointer<vtkImageCanvasSource2D> green =
27 vtkSmartPointer<vtkImageCanvasSource2D>::New();
28 green->SetScalarTypeToUnsignedChar();
29 green->SetNumberOfScalarComponents(1);
30 green->SetExtent(0, 100, 0, 100, 0, 0);
31 green->SetDrawColor(0, 0, 0, 0);
32 green->FillBox(0, 100, 0, 100);
33 green->SetDrawColor(255, 0, 0, 0);
34 green->FillBox(30, 50, 30, 50);
35 green->Update();
36
37 vtkSmartPointer<vtkImageCanvasSource2D> blue =
38 vtkSmartPointer<vtkImageCanvasSource2D>::New();
39 blue->SetScalarTypeToUnsignedChar();
40 blue->SetNumberOfScalarComponents(1);
41 blue->SetExtent(0, 100, 0, 100, 0, 0);
42 blue->SetDrawColor(0, 0, 0, 0);
43 blue->FillBox(0, 100, 0, 100);
44 blue->SetDrawColor(255, 0, 0, 0);
45 blue->FillBox(40, 60, 40, 60);
46 blue->Update();
47
48 vtkSmartPointer<vtkImageAppendComponents> appendFilter =
49 vtkSmartPointer<vtkImageAppendComponents>::New();
50 appendFilter->SetInputConnection(0, red->GetOutputPort());
51 appendFilter->AddInputConnection(0, green->GetOutputPort());
52 appendFilter->AddInputConnection(0, blue->GetOutputPort());
53 appendFilter->Update();
54
55 vtkSmartPointer<vtkImageActor> redActor =
56 vtkSmartPointer<vtkImageActor>::New();
57 redActor->SetInputData(red->GetOutput());
58
59 vtkSmartPointer<vtkImageActor> greenActor =
60 vtkSmartPointer<vtkImageActor>::New();
61 greenActor->SetInputData(green->GetOutput());
62
63 vtkSmartPointer<vtkImageActor> blueActor =
64 vtkSmartPointer<vtkImageActor>::New();
65 blueActor->SetInputData(blue->GetOutput());
66
67 vtkSmartPointer<vtkImageActor> combinedActor =
68 vtkSmartPointer<vtkImageActor>::New();
69 combinedActor->SetInputData(appendFilter->GetOutput());
70
71 // Define viewport ranges
72 // (xmin, ymin, xmax, ymax)
73 double redViewport[4] = { 0.0, 0.0, 0.25, 1.0 };
74 double greenViewport[4] = { 0.25, 0.0, 0.5, 1.0 };
75 double blueViewport[4] = { 0.5, 0.0, 0.75, 1.0 };
76 double combinedViewport[4] = { 0.75, 0.0, 1.0, 1.0 };
77
78 // Setup renderers
79 vtkSmartPointer<vtkRenderer> redRenderer =
80 vtkSmartPointer<vtkRenderer>::New();
81 redRenderer->SetViewport(redViewport);
82 redRenderer->AddActor(redActor);
83 redRenderer->ResetCamera();
84 redRenderer->SetBackground(1.0, 1.0, 1.0);
85
86 vtkSmartPointer<vtkRenderer> greenRenderer =
87 vtkSmartPointer<vtkRenderer>::New();
88 greenRenderer->SetViewport(greenViewport);
89 greenRenderer->AddActor(greenActor);
90 greenRenderer->ResetCamera();
91 greenRenderer->SetBackground(1.0, 1.0, 1.0);
92
93 vtkSmartPointer<vtkRenderer> blueRenderer =
94 vtkSmartPointer<vtkRenderer>::New();
95 blueRenderer->SetViewport(blueViewport);
96 blueRenderer->AddActor(blueActor);
97 blueRenderer->ResetCamera();
98 blueRenderer->SetBackground(1.0, 1.0, 1.0);
99
100 vtkSmartPointer<vtkRenderer> combinedRenderer =
101 vtkSmartPointer<vtkRenderer>::New();
102 combinedRenderer->SetViewport(combinedViewport);
103 combinedRenderer->AddActor(combinedActor);
104 combinedRenderer->ResetCamera();
105 combinedRenderer->SetBackground(1.0, 1.0, 1.0);
106
107 vtkSmartPointer<vtkRenderWindow> renderWindow =
108 vtkSmartPointer<vtkRenderWindow>::New();
109 renderWindow->AddRenderer(redRenderer);
110 renderWindow->AddRenderer(greenRenderer);
111 renderWindow->AddRenderer(blueRenderer);
112 renderWindow->AddRenderer(combinedRenderer);
113 renderWindow->SetSize(1200, 300);
114 renderWindow->Render();
115 renderWindow->SetWindowName("ImageAppendComponentsExample");
116
117 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
118 vtkSmartPointer<vtkRenderWindowInteractor>::New();
119 vtkSmartPointer<vtkInteractorStyleImage> style =
120 vtkSmartPointer<vtkInteractorStyleImage>::New();
121
122 renderWindowInteractor->SetInteractorStyle(style);
123 renderWindowInteractor->SetRenderWindow(renderWindow);
124 renderWindowInteractor->Initialize();
125 renderWindowInteractor->Start();
126
127 return 0;
128 }