一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
 1 #include "vtkAutoInit.h" 
 2 VTK_MODULE_INIT(vtkRenderingOpenGL2);
 3 VTK_MODULE_INIT(vtkInteractionStyle);
 4 
 5 #include <vtkMath.h>
 6 #include <vtkSmartPointer.h>
 7 #include <vtkImageCanvasSource2D.h>
 8 #include <vtkImageMathematics.h>
 9 #include <vtkRenderWindow.h>
10 #include <vtkRenderWindowInteractor.h>
11 #include <vtkInteractorStyleImage.h>
12 #include <vtkRenderer.h>
13 #include <vtkImageActor.h>
14 
15 int main(int, char *[])
16 {
17     vtkSmartPointer<vtkImageCanvasSource2D> imageSource = 
18         vtkSmartPointer<vtkImageCanvasSource2D>::New();
19     imageSource->SetNumberOfScalarComponents(3);
20     imageSource->SetScalarTypeToUnsignedChar();
21     imageSource->SetExtent(0, 4, 0, 4, 0, 0);
22     imageSource->SetDrawColor(100.0, 0, 0);
23     imageSource->FillBox(0, 4, 0, 4);
24     imageSource->Update();
25 
26     vtkSmartPointer<vtkImageMathematics> imageMath = 
27         vtkSmartPointer<vtkImageMathematics>::New();
28     imageMath->SetOperationToMultiplyByK();
29     imageMath->SetConstantK(2.0);
30     imageMath->SetInputConnection(imageSource->GetOutputPort());
31     imageMath->Update();
32 
33     vtkSmartPointer<vtkImageActor> originalActor =
34         vtkSmartPointer<vtkImageActor>::New();
35     originalActor->SetInputData(imageSource->GetOutput());
36 
37     vtkSmartPointer<vtkImageActor> mathActor =
38         vtkSmartPointer<vtkImageActor>::New();
39     mathActor->SetInputData(imageMath->GetOutput());
40 
41     double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
42     double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};
43 
44     vtkSmartPointer<vtkRenderer> originalRenderer =
45         vtkSmartPointer<vtkRenderer>::New();
46     originalRenderer->SetViewport(leftViewport);
47     originalRenderer->AddActor(originalActor);
48     originalRenderer->ResetCamera();
49     originalRenderer->SetBackground(1.0, 1.0, 1.0);
50 
51     vtkSmartPointer<vtkRenderer> gradientMagnitudeRenderer =
52         vtkSmartPointer<vtkRenderer>::New();
53     gradientMagnitudeRenderer->SetViewport(rightViewport);
54     gradientMagnitudeRenderer->AddActor(mathActor);
55     gradientMagnitudeRenderer->ResetCamera();
56     gradientMagnitudeRenderer->SetBackground(1.0, 1.0, 1.0);
57 
58     vtkSmartPointer<vtkRenderWindow> renderWindow =
59         vtkSmartPointer<vtkRenderWindow>::New();
60     renderWindow->AddRenderer(originalRenderer);
61     renderWindow->AddRenderer(gradientMagnitudeRenderer);
62     renderWindow->SetSize(640, 480);
63     renderWindow->Render();
64     renderWindow->SetWindowName("ImageMathematicsExample");
65 
66     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
67         vtkSmartPointer<vtkRenderWindowInteractor>::New();
68     vtkSmartPointer<vtkInteractorStyleImage> style =
69         vtkSmartPointer<vtkInteractorStyleImage>::New();
70 
71     renderWindowInteractor->SetInteractorStyle(style);
72     renderWindowInteractor->SetRenderWindow(renderWindow);
73     renderWindowInteractor->Initialize();
74     renderWindowInteractor->Start();
75 
76     return EXIT_SUCCESS;
77 }

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