File: TestGPURayCastCompositeBinaryMask1.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (136 lines) | stat: -rw-r--r-- 4,395 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestGPURayCastCompositeBinaryMask1.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

// This test masks a rectangular volume to a cylindrical shape and tests that
// the mask is persistent with changing volume property parameters

#include "vtkColorTransferFunction.h"
#include "vtkGPUVolumeRayCastMapper.h"
#include "vtkImageData.h"
#include "vtkNew.h"
#include "vtkPiecewiseFunction.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkTesting.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"

int TestGPURayCastCompositeBinaryMask1(int argc, char *argv[])
{
  cout << "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)" << endl;

  // Dimensions of object
  const int cx = 128;
  const int cy = 128;
  const int cz = 512;

  // Full scale value for data
  const double fullScale = 100.0;

  // Create the image data and mask objects
  vtkNew<vtkImageData> imageData;
  imageData->SetDimensions(cx, cy, cz);
  imageData->AllocateScalars(VTK_UNSIGNED_SHORT, 1);

  vtkNew<vtkImageData> imageMask;
  imageMask->SetDimensions(cx, cy, cz);
  imageMask->AllocateScalars(VTK_UNSIGNED_CHAR, 1);

  // Get pointers to the image and mask data's scalar arrays
  unsigned short *image =
    static_cast<unsigned short*>(imageData->GetScalarPointer());
  unsigned char *mask =
    static_cast<unsigned char*>(imageMask->GetScalarPointer());

  // Initialize image and mask with data
  int index = 0;
  for (int z = 0; z < cz; ++z)
    {
    for( int y=0; y < cy; ++y)
      {
      for( int x=0; x < cx; ++x)
        {
        // Data will increase from 0 to full scale in the z direction
        image[index] = static_cast<unsigned short>( fullScale * z / cz);

        // Inside the mask? Radius of cylinder mask is 1/2 cx which should
        // equal cy
        const double radius = cx / 2.0;
        const double xCenter = cx / 2.0;
        const double yCenter = cy / 2.0;
        const double distance = sqrt(pow(x-xCenter, 2.0) + pow(y-yCenter, 2.0));
        const bool inside = distance < radius;
        mask[index] = (inside) ? 255 : 0;
        index++;
        }
      }
    }

  // Create a volume mapper and add image data and mask
  vtkNew<vtkGPUVolumeRayCastMapper> mapper;
  mapper->SetInputData(imageData.GetPointer());
  mapper->SetMaskInput(imageMask.GetPointer());
  mapper->SetMaskTypeToBinary();

  // Create color and opacity nodes (red and blue)
  vtkNew<vtkColorTransferFunction> colors;
  colors->AddHSVPoint(0.0*fullScale, 0.0, 0.5, 0.5);
  colors->AddHSVPoint(1.0*fullScale, 2.0/3.0, 0.5, 0.5);

  vtkNew<vtkPiecewiseFunction> opacities;
  opacities->AddPoint(0.0*fullScale, 0.6);
  opacities->AddPoint(1.0*fullScale, 0.6);

  // Create color property
  vtkNew<vtkVolumeProperty> colorProperty;
  colorProperty->SetColor(colors.GetPointer());
  colorProperty->SetScalarOpacity(opacities.GetPointer());

  // Create volume
  vtkNew<vtkVolume> volume;
  volume->SetMapper(mapper.GetPointer());
  volume->SetProperty(colorProperty.GetPointer());

  // Render
  vtkNew<vtkRenderWindow> renWin;
  renWin->SetMultiSamples(0);
  renWin->SetSize(301, 300); // Intentional NPOT size

  vtkNew<vtkRenderer> ren;
  renWin->AddRenderer(ren.GetPointer());

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin.GetPointer());

  renWin->Render();
  int valid = mapper->IsRenderSupported(renWin.GetPointer(),
                                        colorProperty.GetPointer());
  if (!valid)
  {
    cout << "Required extensions not supported." << endl;
    return EXIT_SUCCESS;
  }

  ren->AddVolume(volume.GetPointer());
  renWin->Render();
  double values[6];
  colors->GetNodeValue(0, values);
  values[2] = 0.5; values[3] = 0.5;
  colors->SetNodeValue(0, values);
  ren->ResetCamera();
  renWin->Render();
  return vtkTesting::InteractorEventLoop( argc, argv, iren.GetPointer() );
}