vtkGPUVolumeRayCastMapper.h 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGPUVolumeRayCastMapper.h

  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.

=========================================================================*/
// .NAME vtkGPUVolumeRayCastMapper - Ray casting performed on the GPU.
// .SECTION Description
// vtkGPUVolumeRayCastMapper is a volume mapper that performs ray casting on
// the GPU using fragment programs.
//


22 23
#ifndef vtkGPUVolumeRayCastMapper_h
#define vtkGPUVolumeRayCastMapper_h
24

25
#include "vtkRenderingVolumeModule.h" // For export macro
26 27 28 29 30 31 32
#include "vtkVolumeMapper.h"

class vtkVolumeProperty;
class vtkRenderWindow;

//class vtkKWAMRVolumeMapper; // friend class.

33
class VTKRENDERINGVOLUME_EXPORT vtkGPUVolumeRayCastMapper : public vtkVolumeMapper
34 35 36
{
public:
  static vtkGPUVolumeRayCastMapper *New();
37
  vtkTypeMacro(vtkGPUVolumeRayCastMapper,vtkVolumeMapper);
38
  void PrintSelf( ostream& os, vtkIndent indent );
39

40 41
  // Description:
  // If AutoAdjustSampleDistances is on, the the ImageSampleDistance
42
  // will be varied to achieve the allocated render time of this
43
  // prop (controlled by the desired update rate and any culling in
44
  // use).
45 46 47 48
  vtkSetClampMacro( AutoAdjustSampleDistances, int, 0, 1 );
  vtkGetMacro( AutoAdjustSampleDistances, int );
  vtkBooleanMacro( AutoAdjustSampleDistances, int );

49 50 51 52 53 54 55 56
  // Description:
  // If UseJittering is on, each ray traversal direction will be
  // perturbed slightly using a noise-texture to get rid of wood-grain
  // effect.
  vtkSetClampMacro( UseJittering, int, 0, 1 );
  vtkGetMacro( UseJittering, int );
  vtkBooleanMacro( UseJittering, int );

57
  // Description:
58
  // Set/Get the distance between samples used for rendering
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
  // when AutoAdjustSampleDistances is off, or when this mapper
  // has more than 1 second allocated to it for rendering.
  // Initial value is 1.0.
  vtkSetMacro( SampleDistance, float );
  vtkGetMacro( SampleDistance, float );

  // Description:
  // Sampling distance in the XY image dimensions. Default value of 1 meaning
  // 1 ray cast per pixel. If set to 0.5, 4 rays will be cast per pixel. If
  // set to 2.0, 1 ray will be cast for every 4 (2 by 2) pixels. This value
  // will be adjusted to meet a desired frame rate when AutoAdjustSampleDistances
  // is on.
  vtkSetClampMacro( ImageSampleDistance, float, 0.1f, 100.0f );
  vtkGetMacro( ImageSampleDistance, float );

  // Description:
  // This is the minimum image sample distance allow when the image
  // sample distance is being automatically adjusted.
  vtkSetClampMacro( MinimumImageSampleDistance, float, 0.1f, 100.0f );
  vtkGetMacro( MinimumImageSampleDistance, float );

  // Description:
  // This is the maximum image sample distance allow when the image
  // sample distance is being automatically adjusted.
  vtkSetClampMacro( MaximumImageSampleDistance, float, 0.1f, 100.0f );
  vtkGetMacro( MaximumImageSampleDistance, float );


  // Description:
  // Set/Get the window / level applied to the final color.
  // This allows brightness / contrast adjustments on the
  // final image.
  // window is the width of the window.
  // level is the center of the window.
  // Initial window value is 1.0
  // Initial level value is 0.5
  // window cannot be null but can be negative, this way
  // values will be reversed.
  // |window| can be larger than 1.0
  // level can be any real value.
  vtkSetMacro( FinalColorWindow, float );
  vtkGetMacro( FinalColorWindow, float );
  vtkSetMacro( FinalColorLevel,  float );
  vtkGetMacro( FinalColorLevel,  float );

  // Description:
  // Maximum size of the 3D texture in GPU memory.
  // Will default to the size computed from the graphics
  // card. Can be adjusted by the user.
  vtkSetMacro( MaxMemoryInBytes, vtkIdType );
  vtkGetMacro( MaxMemoryInBytes, vtkIdType );
110

111 112 113 114 115 116
  // Description:
  // Maximum fraction of the MaxMemoryInBytes that should
  // be used to hold the texture. Valid values are 0.1 to
  // 1.0.
  vtkSetClampMacro( MaxMemoryFraction, float, 0.1f, 1.0f );
  vtkGetMacro( MaxMemoryFraction, float );
117

118 119 120 121 122 123 124 125 126
  // Description:
  // Tells if the mapper will report intermediate progress.
  // Initial value is true. As the progress works with a GL blocking
  // call (glFinish()), this can be useful for huge dataset but can
  // slow down rendering of small dataset. It should be set to true
  // for big dataset or complex shading and streaming but to false for
  // small datasets.
  vtkSetMacro(ReportProgress,bool);
  vtkGetMacro(ReportProgress,bool);
127

128 129 130 131 132 133 134 135 136 137
  // Description:
  // Based on hardware and properties, we may or may not be able to
  // render using 3D texture mapping. This indicates if 3D texture
  // mapping is supported by the hardware, and if the other extensions
  // necessary to support the specific properties are available.
  virtual int IsRenderSupported(vtkRenderWindow *vtkNotUsed(window),
                                vtkVolumeProperty *vtkNotUsed(property))
    {
      return 0;
    }
138

139 140 141 142 143 144
  void CreateCanonicalView( vtkRenderer *ren,
                            vtkVolume *volume,
                            vtkImageData *image,
                            int blend_mode,
                            double viewDirection[3],
                            double viewUp[3] );
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  // Description:
  // Optionally, set a mask input. This mask may be a binary mask or a label
  // map. This must be specified via SetMaskType.
  //
  // If the mask is a binary mask, the volume rendering is confined to regions
  // within the binary mask. The binary mask is assumed to have a datatype of
  // UCHAR and values of 255 (inside) and 0 (outside).
  //
  // The mask may also be a label map. The label map is allowed to contain only
  // 3 labels (values of 0, 1 and 2) and must have a datatype of UCHAR. In voxels
  // with label value of 0, the color transfer function supplied by component
  // 0 is used.
  //   In voxels with label value of 1, the color transfer function supplied by
  // component 1 is used and blended with the transfer function supplied by
  // component 0, with the blending weight being determined by
  // MaskBlendFactor.
  //    In voxels with a label value of 2, the color transfer function supplied
  //  by component 2 is used and blended with the transfer function supplied by
  // component 0, with the blending weight being determined by
  // MaskBlendFactor.
166 167
  void SetMaskInput(vtkImageData *mask);
  vtkGetObjectMacro(MaskInput, vtkImageData);
168

169 170 171 172 173 174 175 176 177 178 179 180
  //BTX
  enum { BinaryMaskType = 0, LabelMapMaskType };
  //ETX

  // Description:
  // Set the mask type, if mask is to be used. See documentation for
  // SetMaskInput(). The default is a LabelMapMaskType.
  vtkSetMacro( MaskType, int );
  vtkGetMacro( MaskType, int );
  void SetMaskTypeToBinary();
  void SetMaskTypeToLabelMap();

181 182
  // Description:
  // Tells how much mask color transfer function is used compared to the
183 184 185
  // standard color transfer function when the mask is true. This is relevant
  // only for the label map mask.
  //   0.0 means only standard color transfer function.
186
  //   1.0 means only mask color transfer function.
187
  // The default value is 1.0.
188 189
  vtkSetClampMacro(MaskBlendFactor,float,0.0f,1.0f);
  vtkGetMacro(MaskBlendFactor,float);
190

191 192 193 194 195 196 197 198 199 200 201 202 203
  // Description:
  // Enable or disable setting output of volume rendering to be
  // color and depth textures. By default this is set to 0 (off).
  // It should be noted that it is possible that underlying API specific
  // mapper may not supoport RenderToImage mode.
  vtkSetMacro(RenderToImage, int);
  vtkGetMacro(RenderToImage, int);
  vtkBooleanMacro(RenderToImage, int);

  // Description:
  // Low level API to export the depth texture as vtkImageData in
  // RenderToImage mode.
  // Should be implemented by the graphics API specific mapper (GL or other).
204
  virtual void GetDepthImage(vtkImageData*) {};
205 206 207 208 209

  // Description:
  // Low level API to export the color texture as vtkImageData in
  // RenderToImage mode.
  // Should be implemented by the graphics API specific mapper (GL or other).
210
  virtual void GetColorImage(vtkImageData*) {};
211

212 213 214 215 216 217 218 219 220 221
//BTX
  // Description:
  // WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
  // Initialize rendering for this volume.
  void Render( vtkRenderer *, vtkVolume * );

  // Description:
  // Handled in the subclass - the actual render method
  // \pre input is up-to-date.
  virtual void GPURender( vtkRenderer *, vtkVolume *) {}
222

223 224 225 226 227
  // Description:
  // WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
  // Release any graphics resources that are being consumed by this mapper.
  // The parameter window could be used to determine which graphic
  // resources to release.
228
  void ReleaseGraphicsResources(vtkWindow *) {}
229

230 231 232 233 234 235 236 237 238
  // Description:
  // Return how much the dataset has to be reduced in each dimension to
  // fit on the GPU. If the value is 1.0, there is no need to reduce the
  // dataset.
  // \pre the calling thread has a current OpenGL context.
  // \pre mapper_supported: IsRenderSupported(renderer->GetRenderWindow(),0)
  // The computation is based on hardware limits (3D texture indexable size)
  // and MaxMemoryInBytes.
  // \post valid_i_ratio: ratio[0]>0 && ratio[0]<=1.0
239 240
  // \post valid_j_ratio: ratio[1]>0 && ratio[1]<=1.0
  // \post valid_k_ratio: ratio[2]>0 && ratio[2]<=1.0
241
  virtual void GetReductionRatio(double ratio[3])=0;
242

243
//ETX
244

245 246 247 248 249 250 251
protected:
  vtkGPUVolumeRayCastMapper();
  ~vtkGPUVolumeRayCastMapper();

  // Check to see that the render will be OK
  int ValidateRender( vtkRenderer *, vtkVolume * );

252

253 254 255
  // Special version of render called during the creation
  // of a canonical view.
  void CanonicalViewRender( vtkRenderer *, vtkVolume * );
256

257 258 259 260 261 262 263
  // Methods called by the AMR Volume Mapper.
  virtual void PreRender(vtkRenderer *ren,
                         vtkVolume *vol,
                         double datasetBounds[6],
                         double scalarRange[2],
                         int numberOfScalarComponents,
                         unsigned int numberOfLevels)=0;
264

265 266 267 268
  // \pre input is up-to-date
  virtual void RenderBlock(vtkRenderer *ren,
                           vtkVolume *vol,
                           unsigned int level)=0;
269

270 271
  virtual void PostRender(vtkRenderer *ren,
                          int numberOfScalarComponents)=0;
272

273 274 275 276 277
  // Description:
  // Called by the AMR Volume Mapper.
  // Set the flag that tells if the scalars are on point data (0) or
  // cell data (1).
  void SetCellFlag(int cellFlag);
278

279
  int    AutoAdjustSampleDistances;
280 281 282
  float  ImageSampleDistance;
  float  MinimumImageSampleDistance;
  float  MaximumImageSampleDistance;
283 284 285 286

  // Render to texture mode flag
  int RenderToImage;

287 288 289
  // Enable / disable stochasting jittering
  int UseJittering;

290 291
  // The distance between sample points along the ray
  float  SampleDistance;
292

293 294 295 296 297 298 299 300 301 302
  int    SmallVolumeRender;
  double BigTimeToDraw;
  double SmallTimeToDraw;

  float FinalColorWindow;
  float FinalColorLevel;

  // 1 if we are generating the canonical image, 0 otherwise
  int   GeneratingCanonicalView;
  vtkImageData *CanonicalViewImageData;
303

304 305 306 307 308 309
  // Description:
  // Set the mapper in AMR Mode or not. Initial value is false.
  // Called only by the vtkKWAMRVolumeMapper
  vtkSetClampMacro(AMRMode,int,0,1);
  vtkGetMacro(AMRMode,int);
  vtkBooleanMacro(AMRMode,int);
310

311 312 313 314
  vtkImageData * MaskInput;
  float          MaskBlendFactor;
  int            MaskType;

315
  int AMRMode;
316 317 318

  // Point data or cell data (or field data, not handled) ?
  int CellFlag;
319

320 321 322 323 324 325 326 327 328 329 330 331
  // Description:
  // Compute the cropping planes clipped by the bounds of the volume.
  // The result is put into this->ClippedCroppingRegionPlanes.
  // NOTE: IT WILL BE MOVED UP TO vtkVolumeMapper after bullet proof usage
  // in this mapper. Other subclasses will use the ClippedCroppingRegionsPlanes
  // members instead of CroppingRegionPlanes.
  // \pre volume_exists: this->GetInput()!=0
  // \pre valid_cropping: this->Cropping &&
  //             this->CroppingRegionPlanes[0]<this->CroppingRegionPlanes[1] &&
  //             this->CroppingRegionPlanes[2]<this->CroppingRegionPlanes[3] &&
  //             this->CroppingRegionPlanes[4]<this->CroppingRegionPlanes[5])
  virtual void ClipCroppingRegionPlanes();
332

333
  double         ClippedCroppingRegionPlanes[6];
334 335 336 337

  vtkIdType MaxMemoryInBytes;
  float MaxMemoryFraction;

338
  bool           ReportProgress;
339

340
  vtkImageData * TransformedInput;
341 342 343 344

  vtkGetObjectMacro(TransformedInput, vtkImageData);
  void SetTransformedInput(vtkImageData*);

345 346 347 348 349
  // Description:
  // This is needed only to check if the input data has been changed since the last
  // Render() call.
  vtkImageData* LastInput;

350 351 352 353 354 355 356
private:
  vtkGPUVolumeRayCastMapper(const vtkGPUVolumeRayCastMapper&);  // Not implemented.
  void operator=(const vtkGPUVolumeRayCastMapper&);  // Not implemented.
};

#endif