File: TestImageStencilData.cxx

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (215 lines) | stat: -rw-r--r-- 7,107 bytes parent folder | download | duplicates (9)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestImageStencilData.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.

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

// The test creates two box-shaped image stencils from rectangular polydata.
// The stencils are added / subtracted, converted to an image and compared
// to a baseline

#include "vtkPolyDataToImageStencil.h"
#include "vtkLinearExtrusionFilter.h"
#include "vtkImageStencil.h"
#include "vtkImageStencilData.h"
#include "vtkImageData.h"
#include "vtkPolyData.h"
#include "vtkPoints.h"
#include "vtkIdList.h"
#include "vtkSmartPointer.h"
#include "vtkMatrixToLinearTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkMatrix4x4.h"
#include "vtkTrivialProducer.h"
#include "vtkTesting.h"


//----------------------------------------------------------------------------
static vtkSmartPointer< vtkImageStencilData >
CreateBoxStencilData(double d1, double d2 )
{
  // Create two stencil data from polydata's

  vtkPolyData * pd = vtkPolyData::New();
  pd->Allocate(1, 1);
  vtkPoints * points = vtkPoints::New();
  points->InsertNextPoint( d1, d1, 0.0 );
  points->InsertNextPoint( d2, d1, 0.0 );
  points->InsertNextPoint( d2, d2, 0.0 );
  points->InsertNextPoint( d1, d2, 0.0 );
  pd->SetPoints(points);
  vtkIdType ptIds[4];
  ptIds[0] = 0; ptIds[1] = 1; ptIds[2] = 2; ptIds[3] = 3;
  pd->InsertNextCell(VTK_QUAD, 4, ptIds);
  points->Delete();

  // Extrude the contour along the normal to the plane the contour lies on.
  vtkLinearExtrusionFilter *extrudeFilter = vtkLinearExtrusionFilter::New();
  extrudeFilter->SetInputData( pd );
  extrudeFilter->SetScaleFactor(1);
  extrudeFilter->SetExtrusionTypeToNormalExtrusion();
  extrudeFilter->SetVector( 0, 0, 1);
  extrudeFilter->Update();

  // Apply a transformation to the output polydata that subtracts 0.5 from
  // the z co-ordinate.

  const double m[16] = {1,0,0,0,0,1,0,0,0,0,1,-0.5,0,0,0,1};
  vtkMatrixToLinearTransform *linearTransform = vtkMatrixToLinearTransform::New();
  linearTransform->GetMatrix()->DeepCopy( m );
  vtkTransformPolyDataFilter *transformPolyData = vtkTransformPolyDataFilter::New();
  transformPolyData->SetInputConnection( extrudeFilter->GetOutputPort() );
  transformPolyData->SetTransform( linearTransform );
  transformPolyData->Update();
  linearTransform->Delete();

  // Rasterize the polydata (sweep it along the plane the contour lies on,
  // bounded by the extrusion) and get extents into a stencil
  vtkPolyDataToImageStencil *contourStencilFilter
                            = vtkPolyDataToImageStencil::New();
  contourStencilFilter->SetInputConnection( transformPolyData->GetOutputPort() );

  vtkImageData *image = vtkImageData::New();
  image->SetSpacing( 1.0, 1.0, 1.0  );
  image->SetOrigin(  0.0, 0.0, 0.0 );
  image->SetExtent(static_cast<int>(d1)-2,static_cast<int>(d2)+2,
                   static_cast<int>(d1)-2,static_cast<int>(d2)+2, 0, 0 );
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 1);

  vtkImageStencil *stencil = vtkImageStencil::New();
  stencil->SetInputData( image );
  stencil->SetStencilConnection( contourStencilFilter->GetOutputPort() );
  stencil->SetBackgroundValue(0);
  stencil->Update();
  vtkSmartPointer< vtkImageStencilData >
    stencilData = contourStencilFilter->GetOutput();

  extrudeFilter->Delete();
  transformPolyData->Delete();
  contourStencilFilter->Delete();
  stencil->Delete();
  image->Delete();
  pd->Delete();

  return stencilData;
}

//----------------------------------------------------------------------------
static void GetStencilDataAsImageData(
  vtkImageStencilData * stencilData, vtkImageData *image )
{
  int extent[6];
  stencilData->GetExtent( extent );
  extent[5] = extent[4]; // Otherwise we cannot write it out as a PNG!
  int extent1[6] = {0,50,0,50,0,0};
  image->SetExtent(extent1);
  image->AllocateScalars(VTK_UNSIGNED_CHAR, 3);

  // Fill image with zeroes
  for (int y=extent1[2]; y <= extent1[3]; y++)
    {
    unsigned char *ptr=static_cast<unsigned char *>(
      image->GetScalarPointer(extent1[0], y, extent1[4] ));
    for (int x=extent1[0]; x <= extent1[1]; x++)
      {
      *ptr = 0;  ++ptr;
      *ptr = 0;  ++ptr;
      *ptr = 0;  ++ptr;
      }
    }

  vtkIdType increments[3];
  image->GetIncrements( increments );

  int iter = 0;
  for (int y=extent1[2]; y <= extent1[3]; y++, iter = 0)
    {
    int r1,r2;
    int moreSubExtents = 1;
    while( moreSubExtents )
      {
      moreSubExtents = stencilData->GetNextExtent(
        r1, r2, extent1[0], extent1[1], y, extent1[4], iter);

      // sanity check
      if (r1 <= r2 )
        {
        unsigned char *beginExtent =static_cast<unsigned char *>(
          image->GetScalarPointer( r1, y, extent1[4] ));
        unsigned char *endExtent   =static_cast<unsigned char *>(
          image->GetScalarPointer( r2, y, extent1[4] ));
        while (beginExtent <= endExtent)
          {
          *beginExtent     = static_cast<unsigned char>(255);
          *(beginExtent+1) = static_cast<unsigned char>(255);
          *(beginExtent+2) = static_cast<unsigned char>(255);
          beginExtent += increments[0];
          }
        }
      } // end for each extent tuple
    } // end for each scan line
}

//----------------------------------------------------------------------------
int TestImageStencilData( int argc, char * argv [] )
{
  vtkSmartPointer< vtkImageStencilData > stencil1 =
    CreateBoxStencilData(10.0, 30.0);
  vtkSmartPointer< vtkImageStencilData > stencil2 =
    CreateBoxStencilData(20.0, 40.0);

  vtkImageData *image = vtkImageData::New();
  vtkTesting *testing = vtkTesting::New();
  int cc;
  for ( cc = 1; cc < argc; cc ++ )
    {
    testing->AddArgument(argv[cc]);
    }

  if (atoi(argv[1]) == 1)
    {
    // Test Add stencils
    stencil1->Add(stencil2);
    GetStencilDataAsImageData(stencil1, image);
    }
  else if (atoi(argv[1]) == 2)
    {
    // Test subtraction of stencils
    stencil1->Subtract(stencil2);
    GetStencilDataAsImageData(stencil1, image);
    }
  else if (atoi(argv[1]) == 3)
    {
    // Test clipping of stencils
    stencil1->Add(stencil2);
    int clipExtents1[6] = { 15, 35, 15, 35, 0, 0 };
    stencil1->Clip( clipExtents1 );
    int clipExtents2[6] = { 35, 39, 35, 39, 0, 0 };
    stencil2->Clip( clipExtents2 );
    stencil1->Add(stencil2);
    GetStencilDataAsImageData(stencil1, image);
    }
  else
    {
    return EXIT_FAILURE;
    }

  vtkSmartPointer< vtkTrivialProducer > producer =
    vtkSmartPointer< vtkTrivialProducer >::New();
  producer->SetOutput(image);
  int retval = testing->RegressionTest( producer, 10 );
  testing->Delete();
  image->Delete();

  return !retval;
}