1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkAMRVolumeMapper.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 =========================================================================*/
15 /**
16  * @class   vtkAMRVolumeMapper
17  * @brief   AMR class for a volume mapper
18  *
19  *
20  * vtkAMRVolumeMapper is the definition of a volume mapper.
21  * for AMR Structured Data
22  *
23  *
24  */
25 
26 #ifndef vtkAMRVolumeMapper_h
27 #define vtkAMRVolumeMapper_h
28 
29 #include "vtkImageReslice.h"             // for VTK_RESLICE_NEAREST, VTK_RESLICE_CUBIC
30 #include "vtkRenderingVolumeAMRModule.h" // For export macro
31 #include "vtkVolumeMapper.h"
32 
33 class vtkAMRResampleFilter;
34 class vtkCamera;
35 class vtkImageData;
36 class vtkOverlappingAMR;
37 class vtkSmartVolumeMapper;
38 class vtkUniformGrid;
39 
40 class VTKRENDERINGVOLUMEAMR_EXPORT vtkAMRVolumeMapper : public vtkVolumeMapper
41 {
42 public:
43   static vtkAMRVolumeMapper* New();
44   vtkTypeMacro(vtkAMRVolumeMapper, vtkVolumeMapper);
45   void PrintSelf(ostream& os, vtkIndent indent) override;
46 
47   ///@{
48   /**
49    * Set the input data
50    */
51   void SetInputData(vtkImageData*) override;
52   void SetInputData(vtkDataSet*) override;
53   void SetInputData(vtkRectilinearGrid*) override;
54   virtual void SetInputData(vtkOverlappingAMR*);
55   void SetInputConnection(int port, vtkAlgorithmOutput* input) override;
SetInputConnection(vtkAlgorithmOutput * input)56   void SetInputConnection(vtkAlgorithmOutput* input) override
57   {
58     this->SetInputConnection(0, input);
59   }
60   ///@}
61 
62   ///@{
63   /**
64    * Return bounding box (array of six doubles) of data expressed as
65    * (xmin,xmax, ymin,ymax, zmin,zmax).
66    */
67   double* GetBounds() override;
GetBounds(double bounds[6])68   void GetBounds(double bounds[6]) override { this->vtkVolumeMapper::GetBounds(bounds); }
69   ///@}
70 
71   /**
72    * Control how the mapper works with scalar point data and cell attribute
73    * data.  By default (ScalarModeToDefault), the mapper will use point data,
74    * and if no point data is available, then cell data is used. Alternatively
75    * you can explicitly set the mapper to use point data
76    * (ScalarModeToUsePointData) or cell data (ScalarModeToUseCellData).
77    * You can also choose to get the scalars from an array in point field
78    * data (ScalarModeToUsePointFieldData) or cell field data
79    * (ScalarModeToUseCellFieldData).  If scalars are coming from a field
80    * data array, you must call SelectScalarArray.
81    */
82   void SetScalarMode(int mode) override;
83 
84   ///@{
85   /**
86    * Set/Get the blend mode.
87    * Additive blend mode adds scalars along the ray and multiply them by
88    * their opacity mapping value.
89    */
90   void SetBlendMode(int mode) override;
91   int GetBlendMode() override;
92   ///@}
93 
94   ///@{
95   /**
96    * When ScalarMode is set to UsePointFieldData or UseCellFieldData,
97    * you can specify which scalar array to use during rendering.
98    * The transfer function in the vtkVolumeProperty (attached to the calling
99    * vtkVolume) will decide how to convert vectors to colors.
100    */
101   void SelectScalarArray(int arrayNum) override;
102   void SelectScalarArray(const char* arrayName) override;
103   ///@}
104 
105   ///@{
106   /**
107    * Get the array name or number and component to use for rendering.
108    */
109   char* GetArrayName() override;
110   int GetArrayId() override;
111   int GetArrayAccessMode() override;
112   ///@}
113 
114   /**
115    * Return the method for obtaining scalar data.
116    */
117   const char* GetScalarModeAsString();
118   ///@{
119   /**
120    * Turn On/Off orthogonal cropping. (Clipping planes are
121    * perpendicular to the coordinate axes.)
122    */
123   void SetCropping(vtkTypeBool) override;
124   vtkTypeBool GetCropping() override;
125   ///@}
126 
127   ///@{
128   /**
129    * Set/Get the Cropping Region Planes ( xmin, xmax, ymin, ymax, zmin, zmax )
130    * These planes are defined in volume coordinates - spacing and origin are
131    * considered.
132    */
133   void SetCroppingRegionPlanes(
134     double arg1, double arg2, double arg3, double arg4, double arg5, double arg6) override;
SetCroppingRegionPlanes(const double * planes)135   void SetCroppingRegionPlanes(const double* planes) override
136   {
137     this->SetCroppingRegionPlanes(planes[0], planes[1], planes[2], planes[3], planes[4], planes[5]);
138   }
139   void GetCroppingRegionPlanes(double* planes) override;
140   double* GetCroppingRegionPlanes() VTK_SIZEHINT(6) override;
141   ///@}
142   ///@{
143   /**
144    * Set the flags for the cropping regions. The clipping planes divide the
145    * volume into 27 regions - there is one bit for each region. The regions
146    * start from the one containing voxel (0,0,0), moving along the x axis
147    * fastest, the y axis next, and the z axis slowest. These are represented
148    * from the lowest bit to bit number 27 in the integer containing the
149    * flags. There are several convenience functions to set some common
150    * configurations - subvolume (the default), fence (between any of the
151    * clip plane pairs), inverted fence, cross (between any two of the
152    * clip plane pairs) and inverted cross.
153    */
154   void SetCroppingRegionFlags(int mode) override;
155   int GetCroppingRegionFlags() override;
156   ///@}
157 
158   // The possible values for the default and current render mode ivars
159   enum
160   {
161     DefaultRenderMode = 0,
162     RayCastAndTextureRenderMode,
163     RayCastRenderMode,
164     TextureRenderMode,
165     GPURenderMode,
166     UndefinedRenderMode,
167     InvalidRenderMode
168   };
169 
170   ///@{
171   /**
172    * Set the requested render mode. The default is
173    * vtkSmartVolumeMapper::DefaultRenderMode.
174    */
175   void SetRequestedRenderMode(int mode);
176   int GetRequestedRenderMode();
177   ///@}
178 
179   /**
180    * Set the requested render mode to vtkAMRVolumeMapper::DefaultRenderMode.
181    * This is the best option for an application that must adapt to different
182    * data types, hardware, and rendering parameters.
183    */
SetRequestedRenderModeToDefault()184   void SetRequestedRenderModeToDefault()
185   {
186     this->SetRequestedRenderMode(vtkAMRVolumeMapper::DefaultRenderMode);
187   }
188 
189   /**
190    * Set the requested render mode to
191    * vtkAMRVolumeMapper::RayCastAndTextureRenderMode.
192    * This is a good option if you want to avoid using advanced OpenGL
193    * functionality, but would still like to used 3D texture mapping, if
194    * available, for interactive rendering.
195    */
SetRequestedRenderModeToRayCastAndTexture()196   void SetRequestedRenderModeToRayCastAndTexture()
197   {
198     this->SetRequestedRenderMode(vtkAMRVolumeMapper::RayCastAndTextureRenderMode);
199   }
200 
201   /**
202    * Set the requested render mode to vtkAMRVolumeMapper::RayCastRenderMode.
203    * This option will use software rendering exclusively. This is a good option
204    * if you know there is no hardware acceleration.
205    */
SetRequestedRenderModeToRayCast()206   void SetRequestedRenderModeToRayCast()
207   {
208     this->SetRequestedRenderMode(vtkAMRVolumeMapper::RayCastRenderMode);
209   }
210 
211   /**
212    * Set the requested render mode to
213    * vtkAMRVolumeMapper::TextureRenderMode.
214    * This is a good option if you want to use 3D texture mapping, if
215    * available, for interactive rendering.
216    */
SetRequestedRenderModeToTexture()217   void SetRequestedRenderModeToTexture()
218   {
219     this->SetRequestedRenderMode(vtkAMRVolumeMapper::TextureRenderMode);
220   }
221 
222   /**
223    * Set the requested render mode to
224    * vtkAMRVolumeMapper::GPURenderMode.
225    * This will do the volume rendering on the GPU
226    */
SetRequestedRenderModeToGPU()227   void SetRequestedRenderModeToGPU()
228   {
229     this->SetRequestedRenderMode(vtkAMRVolumeMapper::GPURenderMode);
230   }
231 
232   ///@{
233   /**
234    * Set interpolation mode for downsampling (lowres GPU)
235    * (initial value: cubic).
236    */
237   void SetInterpolationMode(int mode);
238   int GetInterpolationMode();
239   ///@}
240 
SetInterpolationModeToNearestNeighbor()241   void SetInterpolationModeToNearestNeighbor() { this->SetInterpolationMode(VTK_RESLICE_NEAREST); }
242 
SetInterpolationModeToLinear()243   void SetInterpolationModeToLinear() { this->SetInterpolationMode(VTK_RESLICE_LINEAR); }
244 
SetInterpolationModeToCubic()245   void SetInterpolationModeToCubic() { this->SetInterpolationMode(VTK_RESLICE_CUBIC); }
246 
247   ///@{
248   /**
249    * Set/Get the number of samples/cells along the i/j/k directions.
250    * The default is 128x128x128
251    */
252   vtkSetVector3Macro(NumberOfSamples, int);
253   vtkGetVector3Macro(NumberOfSamples, int);
254   ///@}
255 
256   /**
257    * WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
258    * DO NOT USE THIS METHOD OUTSIDE OF THE RENDERING PROCESS
259    * Render the volume
260    */
261   void Render(vtkRenderer* ren, vtkVolume* vol) override;
262 
263   /**
264    * WARNING: INTERNAL METHOD - NOT INTENDED FOR GENERAL USE
265    * Release any graphics resources that are being consumed by this mapper.
266    * The parameter window could be used to determine which graphic
267    * resources to release.
268    */
269   void ReleaseGraphicsResources(vtkWindow*) override;
270 
271   void ProcessUpdateExtentRequest(vtkRenderer* renderer, vtkInformation* info,
272     vtkInformationVector** inputVector, vtkInformationVector* outputVector);
273   void ProcessInformationRequest(vtkRenderer* renderer, vtkInformation* info,
274     vtkInformationVector** inputVector, vtkInformationVector* outputVector);
275   void UpdateResampler(vtkRenderer* ren, vtkOverlappingAMR* amr);
276   void UpdateResamplerFrustrumMethod(vtkRenderer* ren, vtkOverlappingAMR* amr);
277 
278   ///@{
279   /**
280    * Select the type of resampling techinque approach to use.
281    */
282   vtkSetMacro(RequestedResamplingMode, int);
283   vtkGetMacro(RequestedResamplingMode, int);
284   vtkSetMacro(FreezeFocalPoint, bool);
285   vtkGetMacro(FreezeFocalPoint, bool);
286   ///@}
287 
288   ///@{
289   /**
290    * Sets/Gets the tolerance used to determine if the resampler needs
291    * to be updated. Default is 10e-8
292    */
293   vtkSetMacro(ResamplerUpdateTolerance, double);
294   vtkGetMacro(ResamplerUpdateTolerance, double);
295   ///@}
296 
297   ///@{
298   /**
299    * Sets/Gets a flag that indicates the internal volume mapper
300    * should use the  default number of threads.  This is useful in applications
301    * such as ParaView that will turn off multiple threads by default. Default is false
302    */
303   vtkSetMacro(UseDefaultThreading, bool);
304   vtkGetMacro(UseDefaultThreading, bool);
305   ///@}
306 
307   /**
308    * Utility method used by UpdateResamplerFrustrumMethod() to compute the
309    * bounds.
310    */
311   static bool ComputeResamplerBoundsFrustumMethod(
312     vtkCamera* camera, vtkRenderer* renderer, const double data_bounds[6], double out_bounds[6]);
313 
314 protected:
315   vtkAMRVolumeMapper();
316   ~vtkAMRVolumeMapper() override;
317 
318   // see algorithm for more info
319   int FillInputPortInformation(int port, vtkInformation* info) override;
320   void UpdateGrid();
321 
322   vtkSmartVolumeMapper* InternalMapper;
323   vtkAMRResampleFilter* Resampler;
324   vtkUniformGrid* Grid;
325   int NumberOfSamples[3];
326 
327   // This indicates that the input has meta data for
328   // doing demand driven operations.
329   bool HasMetaData;
330   int RequestedResamplingMode;
331   bool FreezeFocalPoint;
332   // Cached values for camera focal point and
333   // the distance between the camera position and
334   // focal point
335   double LastFocalPointPosition[3];
336   double LastPostionFPDistance;
337   // This is used when determining if
338   // either the camera or focal point has
339   // move enough to cause the resampler to update
340   double ResamplerUpdateTolerance;
341   bool GridNeedsToBeUpdated;
342   bool UseDefaultThreading;
343 
344 private:
345   vtkAMRVolumeMapper(const vtkAMRVolumeMapper&) = delete;
346   void operator=(const vtkAMRVolumeMapper&) = delete;
347 };
348 
349 #endif
350