VTK
vtkBlockSortHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkBlockSortHelper.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
20 #ifndef vtkBlockSortHelper_h
21 #define vtkBlockSortHelper_h
22 
23 #include "vtkCamera.h"
24 #include "vtkImageData.h"
25 #include "vtkMatrix4x4.h"
26 #include "vtkNew.h"
27 #include "vtkRenderer.h"
28 #include "vtkVolumeMapper.h"
29 
30 
32 {
33 
40  template<typename T>
41  struct BackToFront
42  {
43  double CameraPosition[4];
44 
45  //----------------------------------------------------------------------------
47  {
48  vtkCamera* cam = ren->GetActiveCamera();
49  double camWorldPos[4];
50 
51  cam->GetPosition(camWorldPos);
52  camWorldPos[3] = 1.0;
53 
54  // Transform the camera position to the volume (dataset) coordinate system.
55  vtkNew<vtkMatrix4x4> InverseVolumeMatrix;
56  InverseVolumeMatrix->DeepCopy(volMatrix);
57  InverseVolumeMatrix->Invert();
58  InverseVolumeMatrix->MultiplyPoint(camWorldPos, CameraPosition);
59  };
60 
61  //----------------------------------------------------------------------------
62  bool operator()(T* first, T* second);
63 
69  //----------------------------------------------------------------------------
71  vtkImageData* second)
72  {
73  double center[3];
74  double bounds[6];
75 
76  first->GetBounds(bounds);
77  this->ComputeCenter(bounds, center);
78  double const dist1 = vtkMath::Distance2BetweenPoints(center,
79  this->CameraPosition);
80 
81  second->GetBounds(bounds);
82  this->ComputeCenter(bounds, center);
83  double const dist2 = vtkMath::Distance2BetweenPoints(center,
84  this->CameraPosition);
85 
86  return dist2 < dist1;
87  };
88 
89  //----------------------------------------------------------------------------
90  inline void ComputeCenter(double const* bounds, double* center)
91  {
92  center[0] = bounds[0] + std::abs(bounds[1] - bounds[0]) / 2.0;
93  center[1] = bounds[2] + std::abs(bounds[3] - bounds[2]) / 2.0;
94  center[2] = bounds[4] + std::abs(bounds[5] - bounds[4]) / 2.0;
95  };
96  };
97 
98  //----------------------------------------------------------------------------
99  template <>
101  vtkImageData* second)
102  {
103  return CompareByDistanceDescending(first, second);
104  };
105 
106  //----------------------------------------------------------------------------
107  template <>
109  vtkVolumeMapper* second)
110  {
111  vtkImageData* firstIm = first->GetInput();
112  vtkImageData* secondIm = second->GetInput();
113 
114  return CompareByDistanceDescending(firstIm, secondIm);
115  };
116 }
117 
118 #endif // vtkBlockSortHelper_h
119 // VTK-HeaderTest-Exclude: vtkBlockSortHelper.h
bool operator()(T *first, T *second)
Abstract class for a volume mapper.
represent and manipulate 4x4 transformation matrices
Definition: vtkMatrix4x4.h:41
double * GetBounds()
Return a pointer to the geometry bounding box in the form (xmin,xmax, ymin,ymax, zmin,zmax).
abstract specification for renderers
Definition: vtkRenderer.h:63
vtkCamera * GetActiveCamera()
Get the current camera.
virtual double * GetPosition()
Set/Get the position of the camera in world coordinates.
void MultiplyPoint(const float in[4], float out[4])
Multiply a homogeneous coordinate by this matrix, i.e.
Definition: vtkMatrix4x4.h:119
void DeepCopy(const vtkMatrix4x4 *source)
Set the elements of the matrix to the same values as the elements of the given source matrix...
Definition: vtkMatrix4x4.h:59
BackToFront(vtkRenderer *ren, vtkMatrix4x4 *volMatrix)
static void Invert(const vtkMatrix4x4 *in, vtkMatrix4x4 *out)
Matrix Inversion (adapted from Richard Carling in "Graphics Gems," Academic Press, 1990).
Definition: vtkMatrix4x4.h:100
a virtual camera for 3D rendering
Definition: vtkCamera.h:50
operator() for back-to-front sorting.
topologically and geometrically regular array of data
Definition: vtkImageData.h:45
virtual vtkImageData * GetInput()
Set/Get the input data.
Collection of comparison functions for std::sort.
static float Distance2BetweenPoints(const float p1[3], const float p2[3])
Compute distance squared between two points p1 and p2.
Definition: vtkMath.h:1458
void ComputeCenter(double const *bounds, double *center)
bool CompareByDistanceDescending(vtkImageData *first, vtkImageData *second)
Compares distances from images (first, second) to the camera position.