forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkUnstructuredGridBunykRayCastFunction.h
284 lines (230 loc) · 9.56 KB
/
vtkUnstructuredGridBunykRayCastFunction.h
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkUnstructuredGridBunykRayCastFunction.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.
=========================================================================*/
/**
* @class vtkUnstructuredGridBunykRayCastFunction
* @brief a superclass for ray casting functions
*
*
* vtkUnstructuredGridBunykRayCastFunction is a concrete implementation of a
* ray cast function for unstructured grid data. This class was based on the
* paper "Simple, Fast, Robust Ray Casting of Irregular Grids" by Paul Bunyk,
* Arie Kaufmna, and Claudio Silva. This method is quite memory intensive
* (with extra explicit copies of the data) and therefore should not be used
* for very large data. This method assumes that the input data is composed
* entirely of tetras - use vtkDataSetTriangleFilter before setting the input
* on the mapper.
*
* The basic idea of this method is as follows:
*
* 1) Enumerate the triangles. At each triangle have space for some
* information that will be used during rendering. This includes
* which tetra the triangles belong to, the plane equation and the
* Barycentric coefficients.
*
* 2) Keep a reference to all four triangles for each tetra.
*
* 3) At the beginning of each render, do the precomputation. This
* includes creating an array of transformed points (in view
* coordinates) and computing the view dependent info per triangle
* (plane equations and barycentric coords in view space)
*
* 4) Find all front facing boundary triangles (a triangle is on the
* boundary if it belongs to only one tetra). For each triangle,
* find all pixels in the image that intersect the triangle, and
* add this to the sorted (by depth) intersection list at each
* pixel.
*
* 5) For each ray cast, traverse the intersection list. At each
* intersection, accumulate opacity and color contribution
* per tetra along the ray until you reach an exiting triangle
* (on the boundary).
*
*
* @sa
* vtkUnstructuredGridVolumeRayCastMapper
*/
#ifndef vtkUnstructuredGridBunykRayCastFunction_h
#define vtkUnstructuredGridBunykRayCastFunction_h
#include "vtkRenderingVolumeModule.h" // For export macro
#include "vtkUnstructuredGridVolumeRayCastFunction.h"
class vtkRenderer;
class vtkVolume;
class vtkUnstructuredGridVolumeRayCastMapper;
class vtkMatrix4x4;
class vtkPiecewiseFunction;
class vtkColorTransferFunction;
class vtkUnstructuredGridBase;
class vtkIdList;
class vtkDoubleArray;
class vtkDataArray;
// We manage the memory for the list of intersections ourself - this is the
// storage used. We keep 10,000 elements in each array, and we can have up to
// 1,000 arrays.
#define VTK_BUNYKRCF_MAX_ARRAYS 10000
#define VTK_BUNYKRCF_ARRAY_SIZE 10000
class VTKRENDERINGVOLUME_EXPORT vtkUnstructuredGridBunykRayCastFunction : public vtkUnstructuredGridVolumeRayCastFunction
{
public:
static vtkUnstructuredGridBunykRayCastFunction *New();
vtkTypeMacro(vtkUnstructuredGridBunykRayCastFunction,vtkUnstructuredGridVolumeRayCastFunction);
void PrintSelf(ostream& os, vtkIndent indent) override;
/**
* Called by the ray cast mapper at the start of rendering
*/
void Initialize( vtkRenderer *ren, vtkVolume *vol ) override;
/**
* Called by the ray cast mapper at the end of rendering
*/
void Finalize() override;
VTK_NEWINSTANCE
vtkUnstructuredGridVolumeRayCastIterator *NewIterator() override;
// Used to store each triangle - made public because of the
// templated function
class Triangle {
public:
vtkIdType PointIndex[3];
vtkIdType ReferredByTetra[2];
double P1X, P1Y;
double P2X, P2Y;
double Denominator;
double A, B, C, D;
Triangle *Next;
};
// Used to store each intersection for the pixel rays - made
// public because of the templated function
class Intersection {
public:
Triangle *TriPtr;
double Z;
Intersection *Next;
};
/**
* Is the point x, y, in the given triangle? Public for
* access from the templated function.
*/
int InTriangle( double x, double y,
Triangle *triPtr );
/**
* Access to an internal structure for the templated method.
*/
double *GetPoints() {return this->Points;}
//@{
/**
* Access to an internal structure for the templated method.
*/
vtkGetObjectMacro( ViewToWorldMatrix, vtkMatrix4x4 );
//@}
//@{
/**
* Access to an internal structure for the templated method.
*/
vtkGetVectorMacro( ImageOrigin, int, 2 );
//@}
//@{
/**
* Access to an internal structure for the templated method.
*/
vtkGetVectorMacro( ImageViewportSize, int, 2 );
//@}
/**
* Access to an internal structure for the templated method.
*/
Triangle **GetTetraTriangles () {return this->TetraTriangles;}
/**
* Access to an internal structure for the templated method.
*/
Intersection *GetIntersectionList( int x, int y ) { return this->Image[y*this->ImageSize[0] + x]; }
protected:
vtkUnstructuredGridBunykRayCastFunction();
~vtkUnstructuredGridBunykRayCastFunction() override;
// These are cached during the initialize method so that they do not
// need to be passed into subsequent CastRay calls.
vtkRenderer *Renderer;
vtkVolume *Volume;
vtkUnstructuredGridVolumeRayCastMapper *Mapper;
// Computed during the initialize method - if something is
// wrong (no mapper, no volume, no input, etc.) then no rendering
// will actually be performed.
int Valid;
// These are the transformed points
int NumberOfPoints;
double *Points;
// This is the matrix that will take a transformed point back
// to world coordinates
vtkMatrix4x4 *ViewToWorldMatrix;
// This is the intersection list per pixel in the image
Intersection **Image;
// This is the size of the image we are computing (which does
// not need to match the screen size)
int ImageSize[2];
// Since we may only be computing a subregion of the "full" image,
// this is the origin of the region we are computing. We must
// subtract this origin from any pixel (x,y) locations before
// accessing the pixel in this->Image (which represents only the
// subregion)
int ImageOrigin[2];
// This is the full size of the image
int ImageViewportSize[2];
// These are values saved for the building of the TriangleList. Basically
// we need to check if the data has changed in some way.
vtkUnstructuredGridBase *SavedTriangleListInput;
vtkTimeStamp SavedTriangleListMTime;
// This is a memory intensive algorithm! For each tetra in the
// input data we create up to 4 triangles (we don't create duplicates)
// This is the TriangleList. Then, for each tetra we keep track of
// the pointer to each of its four triangles - this is the
// TetraTriangles. We also keep a duplicate list of points
// (transformed into view space) - these are the Points.
Triangle **TetraTriangles;
vtkIdType TetraTrianglesSize;
Triangle *TriangleList;
// Compute whether a boundary triangle is front facing by
// looking at the fourth point in the tetra to see if it is
// in front (triangle is backfacing) or behind (triangle is
// front facing) the plane containing the triangle.
int IsTriangleFrontFacing( Triangle *triPtr, vtkIdType tetraIndex );
// The image contains lists of intersections per pixel - we
// need to clear this during the initialization phase for each
// render.
void ClearImage();
// This is the memory buffer used to build the intersection
// lists. We do our own memory management here because allocating
// a bunch of small elements during rendering is too slow.
Intersection *IntersectionBuffer[VTK_BUNYKRCF_MAX_ARRAYS];
int IntersectionBufferCount[VTK_BUNYKRCF_MAX_ARRAYS];
// This method replaces new for creating a new element - it
// returns one from the big block already allocated (it
// allocates another big block if necessary)
void *NewIntersection();
// This method is used during the initialization process to
// check the validity of the objects - missing information
// such as the volume, renderer, mapper, etc. will be flagged
// and reported.
int CheckValidity(vtkRenderer *ren,
vtkVolume *vol);
// This method is used during the initialization process to
// transform the points to view coordinates
void TransformPoints();
// This method is used during the initialization process to
// create the list of triangles if the data has changed
void UpdateTriangleList();
// This method is used during the initialization process to
// update the view dependent information in the triangle list
void ComputeViewDependentInfo();
// This method is used during the initialization process to
// compute the intersections for each pixel with the boundary
// triangles.
void ComputePixelIntersections();
private:
vtkUnstructuredGridBunykRayCastFunction(const vtkUnstructuredGridBunykRayCastFunction&) = delete;
void operator=(const vtkUnstructuredGridBunykRayCastFunction&) = delete;
};
#endif