一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

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

posted on 2023-08-15 11:28  一杯清酒邀明月  阅读(70)  评论(0编辑  收藏  举报