|
| 1 | +#include <vtkCornerAnnotation.h> |
| 2 | +#include <vtkImageData.h> |
| 3 | +#include <vtkImageMapper.h> |
| 4 | +#include <vtkInteractorStyleImage.h> |
| 5 | +#include <vtkProperty2D.h> |
| 6 | +#include <vtkRenderWindow.h> |
| 7 | +#include <vtkRenderWindowInteractor.h> |
| 8 | +#include <vtkRenderer.h> |
| 9 | +#include <vtkSmartPointer.h> |
| 10 | +#include <vtkTextProperty.h> |
| 11 | + |
| 12 | +#include <vtkTestUtilities.h> |
| 13 | +#include <vtkRegressionTestImage.h> |
| 14 | + |
| 15 | +namespace |
| 16 | +{ |
| 17 | + |
| 18 | +vtkSmartPointer<vtkImageData> CreateColorImage(unsigned int dim, bool transparent) |
| 19 | +{ |
| 20 | + vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New(); |
| 21 | + image->SetDimensions(dim, dim, 1); |
| 22 | + image->AllocateScalars(VTK_UNSIGNED_CHAR, 4); |
| 23 | + |
| 24 | + for (unsigned int x = 0; x < dim; x++) |
| 25 | + { |
| 26 | + for (unsigned int y = 0; y < dim; y++) |
| 27 | + { |
| 28 | + unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(x, y, 0)); |
| 29 | + pixel[0] = 255; |
| 30 | + pixel[1] = 0; |
| 31 | + pixel[2] = 255; |
| 32 | + pixel[3] = transparent ? 127 : 255; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return image; |
| 37 | +} |
| 38 | + |
| 39 | +vtkSmartPointer<vtkActor2D> CreateImageActor(int dim, int displayLocation, bool transparent) |
| 40 | +{ |
| 41 | + vtkSmartPointer<vtkImageData> colorImage = CreateColorImage(dim, transparent); |
| 42 | + |
| 43 | + vtkSmartPointer<vtkImageMapper> imageMapper = vtkSmartPointer<vtkImageMapper>::New(); |
| 44 | + imageMapper->SetInputData(colorImage); |
| 45 | + imageMapper->SetColorWindow(255); |
| 46 | + imageMapper->SetColorLevel(127.5); |
| 47 | + |
| 48 | + vtkSmartPointer<vtkActor2D> imageActor = vtkSmartPointer<vtkActor2D>::New(); |
| 49 | + imageActor->SetMapper(imageMapper); |
| 50 | + imageActor->SetPosition(dim, 0); |
| 51 | + imageActor->GetProperty()->SetDisplayLocation(displayLocation); |
| 52 | + |
| 53 | + return imageActor; |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +int TestImageAndAnnotations(int argc, char *argv[]) |
| 59 | +{ |
| 60 | + // Setup renderer |
| 61 | + vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); |
| 62 | + |
| 63 | + // Setup render window |
| 64 | + vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); |
| 65 | + renderWindow->SetSize(600, 600); |
| 66 | + renderWindow->AddRenderer(renderer); |
| 67 | + |
| 68 | + // Setup render window interactor |
| 69 | + vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = |
| 70 | + vtkSmartPointer<vtkRenderWindowInteractor>::New(); |
| 71 | + vtkSmartPointer<vtkInteractorStyleImage> style = |
| 72 | + vtkSmartPointer<vtkInteractorStyleImage>::New(); |
| 73 | + renderWindowInteractor->SetInteractorStyle(style); |
| 74 | + |
| 75 | + // Setup corner annotation |
| 76 | + vtkSmartPointer<vtkCornerAnnotation> cornerAnnotation = |
| 77 | + vtkSmartPointer<vtkCornerAnnotation>::New(); |
| 78 | + cornerAnnotation->SetLinearFontScaleFactor(2); |
| 79 | + cornerAnnotation->SetNonlinearFontScaleFactor(1); |
| 80 | + cornerAnnotation->SetMaximumFontSize(20); |
| 81 | + cornerAnnotation->SetText(0, "background/opaque"); // lower left |
| 82 | + cornerAnnotation->SetText(1, "foreground/opaque"); // lower right |
| 83 | + cornerAnnotation->SetText(2, "background/transparent"); // upper left |
| 84 | + cornerAnnotation->SetText(3, "foreground/transparent"); // upper right |
| 85 | + cornerAnnotation->GetTextProperty()->SetColor(1, 1, 1); |
| 86 | + |
| 87 | + renderer->AddViewProp(cornerAnnotation); |
| 88 | + |
| 89 | + // Setup images |
| 90 | + const unsigned int Dim = 300; |
| 91 | + { |
| 92 | + // lower left: background/opaque |
| 93 | + bool transparent = false; |
| 94 | + vtkSmartPointer<vtkActor2D> imageActor = CreateImageActor(Dim, VTK_BACKGROUND_LOCATION, transparent); |
| 95 | + imageActor->SetPosition(0, 0); |
| 96 | + renderer->AddActor(imageActor); |
| 97 | + } |
| 98 | + { |
| 99 | + // lower right: foreground/opaque |
| 100 | + bool transparent = false; |
| 101 | + vtkSmartPointer<vtkActor2D> imageActor = CreateImageActor(Dim, VTK_FOREGROUND_LOCATION, transparent); |
| 102 | + imageActor->SetPosition(Dim, 0); |
| 103 | + renderer->AddActor(imageActor); |
| 104 | + } |
| 105 | + { |
| 106 | + // upper left: background/transparent |
| 107 | + bool transparent = true; |
| 108 | + vtkSmartPointer<vtkActor2D> imageActor = CreateImageActor(Dim, VTK_BACKGROUND_LOCATION, transparent); |
| 109 | + imageActor->SetPosition(0, Dim); |
| 110 | + renderer->AddActor(imageActor); |
| 111 | + } |
| 112 | + { |
| 113 | + // upper right: foreground/transparent |
| 114 | + bool transparent = true; |
| 115 | + vtkSmartPointer<vtkActor2D> imageActor = CreateImageActor(Dim, VTK_FOREGROUND_LOCATION, transparent); |
| 116 | + imageActor->SetPosition(Dim, Dim); |
| 117 | + renderer->AddActor(imageActor); |
| 118 | + } |
| 119 | + |
| 120 | + renderer->ResetCamera(); |
| 121 | + |
| 122 | + // Render and start interaction |
| 123 | + renderWindowInteractor->SetRenderWindow(renderWindow); |
| 124 | + renderWindowInteractor->Initialize(); |
| 125 | + |
| 126 | + int retVal = vtkRegressionTestImage( renderWindow ); |
| 127 | + if ( retVal == vtkRegressionTester::DO_INTERACTOR) |
| 128 | + { |
| 129 | + renderWindowInteractor->Start(); |
| 130 | + } |
| 131 | + |
| 132 | + return !retVal; |
| 133 | +} |
0 commit comments