File: TestGPURayCastClippingUserTransform.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 (369 lines) | stat: -rw-r--r-- 11,598 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*=========================================================================
Program:   Visualization Toolkit
Module:    TestGPURayCastClippingUserTransform.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.
=========================================================================*/

// Description
// This test creates a vtkImageData with two components.
// The data is volume rendered considering the two components as independent.
#include <iostream>
#include <fstream>
using namespace std;

#include "vtkCamera.h"
#include "vtkColorTransferFunction.h"
#include "vtkGPUVolumeRayCastMapper.h"
#include "vtkImageData.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkNew.h"
#include "vtkPiecewiseFunction.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume.h"
#include "vtkVolumeProperty.h"
#include "vtkUnsignedShortArray.h"
#include "vtkImageReader2.h"
#include "vtkPlane.h"
#include "vtkPlaneCollection.h"
#include "vtkPointData.h"
#include "vtkInteractorStyleImage.h"
#include "vtkCommand.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkMatrix4x4.h"

#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"


static double* ComputeNormal(double* reference, bool flipSign)
{
  double* normal = new double[3];
  if (flipSign)
  {
    normal[0] = -reference[0];
    normal[1] = -reference[1];
    normal[2] = -reference[2];
  }
  else
  {
    normal[0] = reference[0];
    normal[1] = reference[1];
    normal[2] = reference[2];
  }

  return normal;
}


static double* ComputeOrigin(double* focalPoint, double* reference,
                             double distance, bool flipSign)
{

  double* origin = new double[3];
  if (flipSign)
  {
    origin[0] = focalPoint[0] - distance * reference[0];
    origin[1] = focalPoint[1] - distance * reference[1];
    origin[2] = focalPoint[2] - distance * reference[2];
  }
  else
  {
    origin[0] = focalPoint[0] + distance * reference[0];
    origin[1] = focalPoint[1] + distance * reference[1];
    origin[2] = focalPoint[2] + distance * reference[2];
  }

  return origin;
}


static void UpdateFrontClippingPlane(vtkPlane* frontClippingPlane,
                                     double* normal, double* focalPoint,
                                     double slabThickness)
{
  // The front plane is the start of ray cast
  // The front normal should be in the same direction as the camera
  // direction (opposite to the plane facing direction)
  double* frontNormal = ComputeNormal(normal, true);

  // Get the origin of the front clipping plane
  double halfSlabThickness = slabThickness / 2;
  double* frontOrigin = ComputeOrigin(focalPoint, normal, halfSlabThickness, false);

  // Set the normal and origin of the front clipping plane
  frontClippingPlane->SetNormal(frontNormal);
  frontClippingPlane->SetOrigin(frontOrigin);
}


static void UpdateRearClippingPlane(vtkPlane* rearClippingPlane, double* normal,
                                    double* focalPoint, double slabThickness)
{

  // The rear normal is the end of ray cast
  // The rear normal should be in the opposite direction to the
  // camera direction (same as the plane facing direction)
  double* rearNormal = ComputeNormal(normal, false);

  // Get the origin of the rear clipping plane
  double halfSlabThickness = slabThickness / 2;
  double* rearOrigin = ComputeOrigin(focalPoint, normal, halfSlabThickness, true);

  // Set the normal and origin of the rear clipping plane
  rearClippingPlane->SetNormal(rearNormal);
  rearClippingPlane->SetOrigin(rearOrigin);
}


class vtkInteractorStyleCallback : public vtkCommand
{
public:
  static vtkInteractorStyleCallback *New()
  {
    return new vtkInteractorStyleCallback;
  }

  void Execute(vtkObject *caller, unsigned long, void*) VTK_OVERRIDE
  {
    vtkInteractorStyle* style = reinterpret_cast<vtkInteractorStyle*>(caller);

    vtkCamera * camera = style->GetCurrentRenderer()->GetActiveCamera();
    //vtkCamera *camera = reinterpret_cast<vtkCamera*>(caller);

    // Get the normal and focal point of the camera
    double* normal = camera->GetViewPlaneNormal();
    double* focalPoint = camera->GetFocalPoint();

    // Fixed slab thickness
    slabThickness = 3.0;
    UpdateFrontClippingPlane(frontClippingPlane, normal, focalPoint, slabThickness);
    UpdateRearClippingPlane(rearClippingPlane, normal, focalPoint, slabThickness);
  }

  vtkInteractorStyleCallback(){}

  void SetFrontClippingPlane(vtkPlane* fcPlane)
  {
    this->frontClippingPlane = fcPlane;
  }

  void SetRearClippingPlane(vtkPlane* rcPlane)
  {
    this->rearClippingPlane = rcPlane;
  }

  double slabThickness;
  vtkPlane* frontClippingPlane;
  vtkPlane* rearClippingPlane;
};


int TestGPURayCastClippingUserTransform(int argc, char *argv[])
{
  int width = 256;
  int height = 256;
  int depth = 148;
  double spacing[3] = { 1.4844, 1.4844, 1.2 };

  // Read the image
  streampos size;
  char * memblock;

  char* fname = vtkTestUtilities::ExpandDataFileName(argc, argv,
                                                     "Data/MagnitudeImage_256x256x148");

  ifstream file(fname, ios::in | ios::binary | ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char[size];
    file.seekg(0, ios::beg);
    file.read(memblock, size);
    file.close();
  }
  else
  {
    cout << "Unable to open file";
    return 1;
  }

  // Convert to short
  unsigned short* shortData = new unsigned short[size / 2];
  int idx = 0;
  int idx2 = 0;
  for (idx = 0; idx < size / 2; idx ++) {
    idx2 = idx * 2;
    shortData[idx] = (short)(((memblock[idx2] & 0xFF) << 8) | (memblock[idx2+1] & 0xFF));
  }

  //
  int volumeSizeInSlice = width * height * depth;
  vtkNew<vtkUnsignedShortArray> dataArrayMag;
  dataArrayMag->Allocate(volumeSizeInSlice, 0);
  dataArrayMag->SetNumberOfComponents(1);
  dataArrayMag->SetNumberOfTuples(volumeSizeInSlice);
  dataArrayMag->SetArray(shortData, volumeSizeInSlice, 1);

  vtkNew<vtkImageData> imageData;
  imageData->SetDimensions(width, height, depth);
  imageData->SetSpacing(spacing);
  imageData->GetPointData()->SetScalars(dataArrayMag.GetPointer());

  // Create a clipping plane
  vtkNew<vtkPlane> frontClippingPlane;
  vtkNew<vtkPlane> rearClippingPlane;

  // Create a clipping plane collection
  vtkNew<vtkPlaneCollection> clippingPlaneCollection;
  clippingPlaneCollection->AddItem(frontClippingPlane.GetPointer());
  clippingPlaneCollection->AddItem(rearClippingPlane.GetPointer());

  // Create a mapper
  vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
  //volumeMapper->SetInputConnection(reader->GetOutputPort());
  volumeMapper->SetInputData(imageData.GetPointer());
  volumeMapper->SetBlendModeToMaximumIntensity();
  volumeMapper->AutoAdjustSampleDistancesOff();
  volumeMapper->SetSampleDistance(1.0);
  volumeMapper->SetImageSampleDistance(1.0);
  volumeMapper->SetClippingPlanes(clippingPlaneCollection.GetPointer());

  // Create volume scale opacity
  vtkNew<vtkPiecewiseFunction> volumeScalarOpacity;
  volumeScalarOpacity->AddPoint(0, 0.0);
  volumeScalarOpacity->AddPoint(32767, 1.0);
  volumeScalarOpacity->ClampingOn();

  // Create a property
  vtkNew<vtkVolumeProperty> volumeProperty;
  volumeProperty->SetInterpolationTypeToLinear();
  volumeProperty->ShadeOff();
  volumeProperty->SetAmbient(1.0);
  volumeProperty->SetDiffuse(0.0);
  volumeProperty->SetSpecular(0.0);
  volumeProperty->IndependentComponentsOn();
  volumeProperty->SetScalarOpacity(volumeScalarOpacity.GetPointer());
  volumeProperty->SetColor(volumeScalarOpacity.GetPointer());

  // Create a volume
  vtkNew<vtkVolume> volume;
  volume->SetMapper(volumeMapper.GetPointer());
  volume->SetProperty(volumeProperty.GetPointer());
  volume->PickableOff();

  // Rotate the blue props
  double rowVector[3] = { 0.0, 0.0, -1.0 };
  double columnVector[3] = { 1.0, 0.0, 0.0 };
  double normalVector[3];
  vtkMath::Cross(rowVector, columnVector, normalVector);
  double position[3] = { 0.0, 0.0, 0.0 };

  vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
  matrix->Identity();
  matrix->SetElement(0, 0, rowVector[0]);
  matrix->SetElement(0, 1, rowVector[1]);
  matrix->SetElement(0, 2, rowVector[2]);
  matrix->SetElement(0, 3, position[0]);
  matrix->SetElement(1, 0, columnVector[0]);
  matrix->SetElement(1, 1, columnVector[1]);
  matrix->SetElement(1, 2, columnVector[2]);
  matrix->SetElement(1, 3, position[1]);
  matrix->SetElement(2, 0, normalVector[0]);
  matrix->SetElement(2, 1, normalVector[1]);
  matrix->SetElement(2, 2, normalVector[2]);
  matrix->SetElement(2, 3, position[2]);

  volume->SetUserMatrix(matrix);

  // Create a outline filter
  vtkNew<vtkOutlineFilter> outlineFilter;
  outlineFilter->SetInputData(imageData.GetPointer());

  // Create an outline mapper
  vtkNew<vtkPolyDataMapper> outlineMapper;
  outlineMapper->SetInputConnection(outlineFilter->GetOutputPort());

  vtkNew<vtkActor> outline;
  outline->SetMapper(outlineMapper.GetPointer());
  outline->PickableOff();

  // Create a renderer
  vtkNew<vtkRenderer> ren;
  ren->AddViewProp(volume.GetPointer());
  ren->AddViewProp(outline.GetPointer());

  // Get the center of volume
  double* center = volume->GetCenter();
  double cameraFocal[3];
  cameraFocal[0] = center[0];
  cameraFocal[1] = center[1];
  cameraFocal[2] = center[2];

  double cameraViewUp[3] = { 0.00, -1.00, 0.00 };
  double cameraNormal[3] = { 0.00, 0.00, -1.00 };
  double cameraDistance = 1000.0;

  double cameraPosition[3];
  cameraPosition[0] = cameraFocal[0] + cameraDistance * cameraNormal[0];
  cameraPosition[1] = cameraFocal[1] + cameraDistance * cameraNormal[1];
  cameraPosition[2] = cameraFocal[2] + cameraDistance * cameraNormal[2];

  // Update clipping planes
  UpdateFrontClippingPlane(frontClippingPlane.GetPointer(), cameraNormal, cameraFocal, 3.0);
  UpdateRearClippingPlane(rearClippingPlane.GetPointer(), cameraNormal, cameraFocal, 3.0);

  // Get the active camera
  vtkCamera* camera = ren->GetActiveCamera();
  camera->ParallelProjectionOn();
  camera->SetParallelScale(250);
  camera->SetPosition(cameraPosition);
  camera->SetFocalPoint(cameraFocal);
  camera->SetViewUp(cameraViewUp);

  // Create a render window
  vtkNew<vtkRenderWindow> renWin;
  renWin->SetSize(500, 500);
  renWin->AddRenderer(ren.GetPointer());

  // Create a style
  vtkNew<vtkInteractorStyleImage> style;
  style->SetInteractionModeToImage3D();

  // Create a interactor style callback
  vtkNew<vtkInteractorStyleCallback> interactorStyleCallback;
  interactorStyleCallback->frontClippingPlane = frontClippingPlane.GetPointer();
  interactorStyleCallback->rearClippingPlane = rearClippingPlane.GetPointer();
  style->AddObserver(vtkCommand::InteractionEvent, interactorStyleCallback.GetPointer());

  // Create an interactor
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetInteractorStyle(style.GetPointer());
  iren->SetRenderWindow(renWin.GetPointer());

  // Start
  iren->Initialize();
  renWin->Render();

  int retVal = vtkRegressionTestImageThreshold(renWin.GetPointer(), 70);

  if (retVal == vtkRegressionTester::DO_INTERACTOR)
  {
    iren->Start();
  }


  delete[] memblock;
  delete[] shortData;
  delete[] fname;

  return !retVal;
}