1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkFixedPointRayCastImage.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 /**
17  * @class   vtkFixedPointRayCastImage
18  * @brief   helper class for a ray cast image
19  *
20  * This is a helper class for storing the ray cast image including the
21  * underlying data and the size of the image. This class is not intended
22  * to be used directly - just as an internal class in the
23  * vtkFixedPointVolumeRayCastMapper so that multiple mappers can share
24  * the same image. This class also stored the ZBuffer (if necessary due
25  * to intermixed geometry). Perhaps this class could be generalized
26  * in the future to be used for other ray cast methods other than the
27  * fixed point method.
28  *
29  * @sa
30  * vtkFixedPointVolumeRayCastMapper
31  */
32 
33 #ifndef vtkFixedPointRayCastImage_h
34 #define vtkFixedPointRayCastImage_h
35 
36 #include "vtkObject.h"
37 #include "vtkRenderingVolumeModule.h" // For export macro
38 
39 class VTKRENDERINGVOLUME_EXPORT vtkFixedPointRayCastImage : public vtkObject
40 {
41 public:
42   static vtkFixedPointRayCastImage* New();
43   vtkTypeMacro(vtkFixedPointRayCastImage, vtkObject);
44   void PrintSelf(ostream& os, vtkIndent indent) override;
45 
46   /**
47    * Get the internal storage for the image. It is a pointer to
48    * unsigned short with four components (RGBA) per pixel. This
49    * memory is allocated when the AllocateImage method is called.
50    */
GetImage()51   unsigned short* GetImage() { return this->Image; }
52 
53   ///@{
54   /**
55    * Set / Get the ImageViewportSize. This is the size of the
56    * whole viewport in pixels.
57    */
58   vtkSetVector2Macro(ImageViewportSize, int);
59   vtkGetVectorMacro(ImageViewportSize, int, 2);
60   ///@}
61 
62   ///@{
63   /**
64    * Set / Get the ImageMemorySize. This is the size in pixels
65    * of the Image ivar. This will be a power of two in order
66    * to ensure that the texture can be rendered by graphics
67    * hardware that requires power of two textures.
68    */
69   vtkSetVector2Macro(ImageMemorySize, int);
70   vtkGetVectorMacro(ImageMemorySize, int, 2);
71   ///@}
72 
73   ///@{
74   /**
75    * Set / Get the size of the image we are actually using. As
76    * long as the memory size is big enough, but not too big,
77    * we won't bother deleting and re-allocated, we'll just
78    * continue to use the memory size we have. This size will
79    * always be equal to or less than the ImageMemorySize.
80    */
81   vtkSetVector2Macro(ImageInUseSize, int);
82   vtkGetVectorMacro(ImageInUseSize, int, 2);
83   ///@}
84 
85   ///@{
86   /**
87    * Set / Get the origin of the image. This is the starting
88    * pixel within the whole viewport that our Image starts on.
89    * That is, we could be generating just a subregion of the
90    * whole viewport due to the fact that our volume occupies
91    * only a portion of the viewport. The Image pixels will
92    * start from this location.
93    */
94   vtkSetVector2Macro(ImageOrigin, int);
95   vtkGetVectorMacro(ImageOrigin, int, 2);
96   ///@}
97 
98   ///@{
99   /**
100    * Set / Get the ImageSampleDistance that will be used for
101    * rendering. This is a copy of the value stored in the
102    * mapper. It is stored here for sharing between all mappers
103    * that are participating in the creation of this image.
104    */
105   vtkSetMacro(ImageSampleDistance, float);
106   vtkGetMacro(ImageSampleDistance, float);
107   ///@}
108 
109   /**
110    * Call this method once the ImageMemorySize has been set
111    * the allocate the image. If an image already exists,
112    * it will be deleted first.
113    */
114   void AllocateImage();
115 
116   /**
117    * Clear the image to (0,0,0,0) for each pixel.
118    */
119   void ClearImage();
120 
121   ///@{
122   /**
123    * Set / Get the size of the ZBuffer in pixels. The zbuffer will
124    * be captured for the region of the screen covered by the
125    * ImageInUseSize image. However, due to subsampling, the size
126    * of the ImageInUseSize image may be smaller than this ZBuffer
127    * image which will be captured at screen resolution.
128    */
129   vtkSetVector2Macro(ZBufferSize, int);
130   vtkGetVectorMacro(ZBufferSize, int, 2);
131   ///@}
132 
133   ///@{
134   /**
135    * Set / Get the origin of the ZBuffer. This is the distance
136    * from the lower left corner of the viewport where the ZBuffer
137    * started (multiply the ImageOrigin by the ImageSampleDistance)
138    * This is the pixel location on the full resolution viewport
139    * where the ZBuffer capture will start. These values are used
140    * to convert the (x,y) pixel location within the ImageInUseSize
141    * image into a ZBuffer location.
142    */
143   vtkSetVector2Macro(ZBufferOrigin, int);
144   vtkGetVectorMacro(ZBufferOrigin, int, 2);
145   ///@}
146 
147   ///@{
148   /**
149    * The UseZBuffer flag indicates whether the ZBuffer is in use.
150    * The ZBuffer is captured and used when IntermixIntersectingGeometry
151    * is on in the mapper, and when there are props that have been
152    * rendered before the current volume.
153    */
154   vtkSetClampMacro(UseZBuffer, vtkTypeBool, 0, 1);
155   vtkGetMacro(UseZBuffer, vtkTypeBool);
156   vtkBooleanMacro(UseZBuffer, vtkTypeBool);
157   ///@}
158 
159   /**
160    * Get the ZBuffer value corresponding to location (x,y) where (x,y)
161    * are indexing into the ImageInUse image. This must be converted to
162    * the zbuffer image coordinates. Nearest neighbor value is returned.
163    * If UseZBuffer is off, then 1.0 is always returned.
164    */
165   float GetZBufferValue(int x, int y);
166 
167   /**
168    * Get the ZBuffer. The size of the ZBuffer should be specific
169    * with SetZBufferSize, and AllocateZBuffer method should be called
170    * before getting the ZBuffer.
171    */
GetZBuffer()172   float* GetZBuffer() { return this->ZBuffer; }
173 
174   // Descipriotn:
175   // Allocate the space for the ZBuffer according to the size.
176   void AllocateZBuffer();
177 
178 protected:
179   vtkFixedPointRayCastImage();
180   ~vtkFixedPointRayCastImage() override;
181 
182   // This is how big the image would be if it covered the entire viewport
183   int ImageViewportSize[2];
184 
185   // This is how big the allocated memory for image is. This may be bigger
186   // or smaller than ImageFullSize - it will be bigger if necessary to
187   // ensure a power of 2, it will be smaller if the volume only covers a
188   // small region of the viewport
189   int ImageMemorySize[2];
190 
191   // This is the size of subregion in ImageSize image that we are using for
192   // the current image. Since ImageSize is a power of 2, there is likely
193   // wasted space in it. This number will be used for things such as clearing
194   // the image if necessary.
195   int ImageInUseSize[2];
196 
197   // This is the location in ImageFullSize image where our ImageSize image
198   // is located.
199   int ImageOrigin[2];
200 
201   // This is a copy of the ImageSampleDistance from the mapper - copied here
202   // in order to share among all mappers contributing to this image
203   float ImageSampleDistance;
204 
205   // This is the allocated image
206   unsigned short* Image;
207 
208   // This is the size of the zbuffer in pixels
209   int ZBufferSize[2];
210 
211   // This is the size of the memory for the zbuffer - this can be
212   // bigger than the size of the zbuffer since we will allocate enough
213   // space for the whole viewport to avoid re-allocating over and over
214   int ZBufferMemorySize;
215 
216   // This is the distance from the lower left corner of the viewport
217   // where the ZBuffer starts
218   int ZBufferOrigin[2];
219 
220   // This is the flag that indicate whether the ZBuffer is in use
221   vtkTypeBool UseZBuffer;
222 
223   // This is the actual ZBuffer data in floats
224   float* ZBuffer;
225 
226 private:
227   vtkFixedPointRayCastImage(const vtkFixedPointRayCastImage&) = delete;
228   void operator=(const vtkFixedPointRayCastImage&) = delete;
229 };
230 
231 #endif
232