1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkStreamingTessellator.h
5   Language:  C++
6   Date:      $Date$
7   Version:   $Revision$
8 
9   Copyright 2003 Sandia Corporation.
10   Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
11   license for use of this work by or on behalf of the
12   U.S. Government. Redistribution and use in source and binary forms, with
13   or without modification, are permitted provided that this Notice and any
14   statement of authorship are reproduced on all copies.
15 
16 =========================================================================*/
17 // .NAME vtkStreamingTessellator - An algorithm that refines an initial simplicial tessellation using edge subdivision
18 // .SECTION Description
19 // This class is a simple algorithm that takes a single starting simplex -- a
20 // tetrahedron, triangle, or line segment -- and calls a function you
21 // pass it with (possibly many times) tetrahedra, triangles, or lines
22 // adaptively sampled from the one you specified. It
23 // uses an algorithm you specify to control the level of adaptivity.
24 //
25 // This class does not create vtkUnstructuredGrid output because it is
26 // intended for use in mappers as well as filters. Instead, it
27 // calls the registered function with simplices as they are
28 // created.
29 //
30 // The subdivision algorithm should change the vertex
31 // coordinates (it must change both geometric and, if desired, parametric
32 // coordinates) of the midpoint. These coordinates need not be
33 // changed unless the EvaluateEdge() member returns true.
34 // The vtkStreamingTessellator itself has no way of creating
35 // a more accurate midpoint vertex.
36 //
37 // Here's how to use this class:
38 // - Call AdaptivelySample1Facet, AdaptivelySample2Facet, or
39 //   AdaptivelySample3Facet, with an edge, triangle, or
40 //   tetrahedron you want tessellated.
41 // - The adaptive tessellator classifies each edge by passing
42 //   the midpoint values to the vtkEdgeSubdivisionCriterion.
43 // - After each edge is classified, the tessellator subdivides
44 //   edges as required until the subdivision criterion is
45 //   satisfied or the maximum subdivision depth has been
46 //   reached.
47 // - Edges, triangles, or tetrahedra connecting the vertices
48 //   generated by the subdivision algorithm are processed by
49 //   calling the user-defined callback functions (set with
50 //   SetTetrahedronCallback(), SetTriangleCallback(),
51 //   or SetEdgeCallback() ).
52 //
53 // .SECTION Warning
54 // Note that the vertices passed to AdaptivelySample3Facet, AdaptivelySample2Facet,
55 // or AdaptivelySample1Facet must be at least 6, 5, or 4 entries long, respectively!
56 // This is because the <r,s,t>, <r,s>, or <r>
57 // parametric coordinates of the vertices are maintained as the
58 // facet is subdivided. This information is often
59 // required by the subdivision algorithm in order to compute
60 // an error metric. You may change the number of parametric coordinates
61 // associated with each vertex using vtkStreamingTessellator::SetEmbeddingDimension().
62 //
63 // .SECTION Interpolating Field Values
64 // If you wish, you may also use \p vtkStreamingTessellator to interpolate field
65 // values at newly created vertices. Interpolated field values are stored just beyond
66 // the parametric coordinates associated with a vertex. They will always be \p double
67 // values; it does not make sense to interpolate a boolean or string value and your
68 // output and subdivision subroutines may always cast to a \p float or use \p floor() to
69 // truncate an interpolated value to an integer.
70 //
71 // .SECTION See Also
72 // vtkEdgeSubdivisionCriterion
73 
74 #ifndef vtkStreamingTessellator_h
75 #define vtkStreamingTessellator_h
76 
77 #include "vtkFiltersCoreModule.h" // For export macro
78 #include "vtkObject.h"
79 
80 #undef PARAVIEW_DEBUG_TESSELLATOR
81 
82 class vtkEdgeSubdivisionCriterion;
83 
84 class VTKFILTERSCORE_EXPORT vtkStreamingTessellator : public vtkObject
85 {
86   public:
87     vtkTypeMacro(vtkStreamingTessellator,vtkObject);
88     static vtkStreamingTessellator* New();
89     virtual void PrintSelf( ostream& os, vtkIndent indent );
90 
91     //BTX
92     typedef void (*VertexProcessorFunction)( const double*, vtkEdgeSubdivisionCriterion*, void*, const void* );
93     typedef void (*EdgeProcessorFunction)( const double*, const double*, vtkEdgeSubdivisionCriterion*, void*, const void* );
94     typedef void (*TriangleProcessorFunction)( const double*, const double*, const double*, vtkEdgeSubdivisionCriterion*, void*, const void* );
95     typedef void (*TetrahedronProcessorFunction)( const double*, const double*, const double*, const double*, vtkEdgeSubdivisionCriterion*, void*, const void* );
96 
97     enum {MaxFieldSize = 18};
98 
99     // Description:
100     // Get/Set the function called for each output tetrahedron (3-facet).
101     virtual void SetTetrahedronCallback( TetrahedronProcessorFunction );
102     virtual TetrahedronProcessorFunction GetTetrahedronCallback() const;
103 
104     // Description:
105     // Get/Set the function called for each output triangle (2-facet).
106     virtual void SetTriangleCallback( TriangleProcessorFunction );
107     virtual TriangleProcessorFunction GetTriangleCallback() const;
108 
109     // Description:
110     // Get/Set the function called for each output line segment (1-facet).
111     virtual void SetEdgeCallback( EdgeProcessorFunction );
112     virtual EdgeProcessorFunction GetEdgeCallback() const;
113 
114     // Description:
115     // Get/Set the function called for each output line segment (1-facet).
116     virtual void SetVertexCallback( VertexProcessorFunction );
117     virtual VertexProcessorFunction GetVertexCallback() const;
118     //ETX
119 
120     // Description:
121     // Get/Set a void pointer passed to the triangle and edge output functions.
122     virtual void SetPrivateData( void* Private );
123     virtual void* GetPrivateData() const;
124 
125     // can't wrap const private data because python wrapper will try to cast it to void*, not const void*
126     //BTX
127     // Description:
128     // Get/Set a constant void pointer passed to the simplex output functions.
129     virtual void SetConstPrivateData( const void* ConstPrivate );
130     virtual const void* GetConstPrivateData() const;
131     //ETX
132 
133     // Description:
134     // Get/Set the algorithm used to determine whether an edge should be
135     // subdivided or left as-is. This is used once for each call to
136     // AdaptivelySample1Facet (which is recursive and will call itself
137     // resulting in additional edges to be checked) or three times for
138     // each call to AdaptivelySample2Facet (also recursive).
139     virtual void SetSubdivisionAlgorithm( vtkEdgeSubdivisionCriterion* );
140     virtual vtkEdgeSubdivisionCriterion* GetSubdivisionAlgorithm() ;
141     //BTX
142     virtual const vtkEdgeSubdivisionCriterion* GetSubdivisionAlgorithm() const;
143     //ETX
144 
145     // Description:
146     // Get/Set the number of parameter-space coordinates associated with each input and output point.
147     // The default is \a k for \a k -facets. You may
148     // specify a different dimension, \a d, for each type of \a k -facet to be processed.
149     // For example, \p SetEmbeddingDimension( \p 2, \p 3 ) would associate \a r, \a s, and
150     // \a t coordinates with each input and output point generated by \p AdaptivelySample2Facet
151     // but does not say anything about input or output points generated by
152     // \p AdaptivelySample1Facet.
153     // Call \p SetEmbeddingDimension( \p -1, \a d ) to specify the same dimension for
154     // all possible \a k values.
155     // \a d may not exceed 8, as that would be plain silly.
156     virtual void SetEmbeddingDimension( int k, int d );
157     int GetEmbeddingDimension( int k ) const;
158 
159     // Description:
160     // Get/Set the number of field value coordinates associated with each input and output point.
161     // The default is 0; no field values are interpolated.
162     // You may specify a different size, \a s, for each type of \a k -facet to be processed.
163     // For example, \p SetFieldSize( \p 2, \p 3 ) would associate 3 field value coordinates
164     // with each input and output point of an \p AdaptivelySample2Facet call,
165     // but does not say anything about input or output points of \p AdaptivelySample1Facet.
166     // Call \p SetFieldSize( \p -1, \a s ) to specify the same dimension for all possible \a k values.
167     // \a s may not exceed vtkStreamingTessellator::MaxFieldSize.
168     // This is a compile-time constant that defaults to 18, which is large enough for
169     // a scalar, vector, tensor, normal, and texture coordinate to be included at each point.
170     //
171     // Normally, you will not call \a SetFieldSize() directly; instead, subclasses of
172     // vtkEdgeSubdivisionCriterion, such as vtkShoeMeshSubdivisionAlgorithm, will call it
173     // for you.
174     //
175     // In any event, setting \a FieldSize to a non-zero value means you must pass field
176     // values to the \p AdaptivelySamplekFacet routines; For example,
177     // @verbatim
178     //    vtkStreamingTessellator* t = vtkStreamingTessellator::New();
179     //    t->SetFieldSize( 1, 3 );
180     //    t->SetEmbeddingDimension( 1, 1 ); // not really required, this is the default
181     //    double p0[3+1+3] = { x0, y0, z0, r0, fx0, fy0, fz0 };
182     //    double p1[3+1+3] = { x1, y1, z1, r1, fx1, fy1, fz1 };
183     //    t->AdaptivelySample1Facet( p0, p1 );
184     // @endverbatim
185     // This would adaptively sample an curve (1-facet) with geometry and
186     // a vector field at every output point on the curve.
187     virtual void SetFieldSize( int k, int s );
188     int GetFieldSize( int k ) const;
189 
190     // Description:
191     // Get/Set the maximum number of subdivisions that may occur.
192     virtual void SetMaximumNumberOfSubdivisions( int num_subdiv_in );
193     int GetMaximumNumberOfSubdivisions();
194 
195     // Description:
196     // This will adaptively subdivide the tetrahedron (3-facet),
197     // triangle (2-facet), or edge (1-facet) until the subdivision
198     // algorithm returns false for every edge or the maximum recursion
199     // depth is reached.
200     //
201     // Use \p SetMaximumNumberOfSubdivisions to change the maximum
202     // recursion depth.
203     //
204     // The AdaptivelySample0Facet method is provided as a convenience.
205     // Obviously, there is no way to adaptively subdivide a vertex.
206     // Instead the input vertex is passed unchanged to the output
207     // via a call to the registered VertexProcessorFunction callback.
208     //
209     // .SECTION Warning
210     // This assumes that you have called SetSubdivisionAlgorithm(),
211     // SetEdgeCallback(), SetTriangleCallback(), and SetTetrahedronCallback()
212     // with valid values!
213     void AdaptivelySample3Facet( double* v1, double* v2, double* v3, double* v4 ) const ;
214     void AdaptivelySample2Facet( double* v1, double* v2, double* v3 ) const ;
215     void AdaptivelySample1Facet( double* v1, double* v2 ) const ;
216     void AdaptivelySample0Facet( double* v1 ) const ;
217 
218     // Description:
219     // Reset/access the histogram of subdivision cases encountered.
220     // The histogram may be used to examine coverage during testing as well as characterizing the
221     // tessellation algorithm's performance.
222     // You should call ResetCounts() once, at the beginning of a stream of tetrahedra.
223     // It must be called before AdaptivelySample3Facet() to prevent uninitialized memory reads.
224     //
225     // These functions have no effect (and return 0) when PARAVIEW_DEBUG_TESSELLATOR has not been defined.
226     // By default, PARAVIEW_DEBUG_TESSELLATOR is not defined, and your code will be fast and efficient. Really!
ResetCounts()227     void ResetCounts()
228     {
229 #ifdef PARAVIEW_DEBUG_TESSELLATOR
230       for ( int i=0; i<11; ++i )
231         {
232         this->CaseCounts[i] = 0;
233         for ( int j=0; j<51; ++j )
234           {
235           this->SubcaseCounts[i][j] = 0;
236           }
237         }
238 #endif // PARAVIEW_DEBUG_TESSELLATOR
239     }
GetCaseCount(int c)240     vtkIdType GetCaseCount( int c )
241       {
242 #ifdef PARAVIEW_DEBUG_TESSELLATOR
243       return this->CaseCounts[c];
244 #else
245       (void)c;
246       return 0;
247 #endif // PARAVIEW_DEBUG_TESSELLATOR
248       }
GetSubcaseCount(int casenum,int sub)249     vtkIdType GetSubcaseCount( int casenum, int sub )
250       {
251 #ifdef PARAVIEW_DEBUG_TESSELLATOR
252       return this->SubcaseCounts[casenum][sub];
253 #else
254       (void)casenum;
255       (void)sub;
256       return 0;
257 #endif // PARAVIEW_DEBUG_TESSELLATOR
258       }
259 
260   protected:
261     //BTX
262     static int EdgeCodesToCaseCodesPlusPermutation[64][2];
263     static vtkIdType PermutationsFromIndex[24][14];
264     static vtkIdType TetrahedralDecompositions[];
265     //ETX
266 
267     void* PrivateData;
268     const void* ConstPrivateData;
269     vtkEdgeSubdivisionCriterion* Algorithm;
270     //BTX
271     VertexProcessorFunction Callback0;
272     EdgeProcessorFunction Callback1;
273     TriangleProcessorFunction Callback2;
274     TetrahedronProcessorFunction Callback3;
275 #ifdef PARAVIEW_DEBUG_TESSELLATOR
276     mutable vtkIdType CaseCounts[11];
277     mutable vtkIdType SubcaseCounts[11][51];
278 #endif // PARAVIEW_DEBUG_TESSELLATOR
279     //ETX
280 
281     // Description:
282     // PointDimension is the length of each \p double* array associated with
283     // each point passed to a subdivision algorithm:
284     //   PointDimension[i] = 3 + EmbeddingDimension[i] + FieldSize[i]
285     // We store this instead of FieldSize for speed.
286     // Only entries 1 through 3 are used; you can't subdivide 0-facets (points).
287     // Well, maybe <i>you</i> can, but <i>I</i> can't!
288     int PointDimension[4];
289 
290     // Description:
291     // The parametric dimension of each point passed to the subdivision algorithm.
292     // Only entries 1 through 3 are used; you can't subdivide 0-facets (points).
293     // Well, maybe <i>you</i> can, but <i>I</i> can't!
294     int EmbeddingDimension[4];
295 
296     // Description:
297     // The number of subdivisions allowed.
298     int MaximumNumberOfSubdivisions;
299 
300     vtkStreamingTessellator();
301     ~vtkStreamingTessellator();
302 
303     void AdaptivelySample3Facet( double* v1, double* v2, double* v3, double* v4, int maxDepth ) const ;
304     void AdaptivelySample2Facet( double* v1, double* v2, double* v3, int maxDepth, int move=7 ) const ;
305     void AdaptivelySample1Facet( double* v1, double* v2, int maxDepth ) const ;
306 
307     int BestTets( int*, double**, int, int ) const;
308 
309   private:
310     vtkStreamingTessellator( const vtkStreamingTessellator& ); // Not implemented.
311     void operator = ( const vtkStreamingTessellator& ); // Not implemented.
312 };
313 
314 //BTX
315 
AdaptivelySample3Facet(double * v1,double * v2,double * v3,double * v4)316 inline void vtkStreamingTessellator::AdaptivelySample3Facet( double* v1, double* v2, double* v3, double* v4 ) const
317 { this->AdaptivelySample3Facet( v1, v2, v3, v4, this->MaximumNumberOfSubdivisions ); }
AdaptivelySample2Facet(double * v1,double * v2,double * v3)318 inline void vtkStreamingTessellator::AdaptivelySample2Facet( double* v1, double* v2, double* v3 ) const
319 { this->AdaptivelySample2Facet( v1, v2, v3, this->MaximumNumberOfSubdivisions ); }
AdaptivelySample1Facet(double * v1,double * v2)320 inline void vtkStreamingTessellator::AdaptivelySample1Facet( double* v1, double* v2 ) const
321 { this->AdaptivelySample1Facet( v1, v2, this->MaximumNumberOfSubdivisions ); }
322 
GetEmbeddingDimension(int k)323 inline int vtkStreamingTessellator::GetEmbeddingDimension( int k ) const
324 { if ( k <= 0 || k >= 4 ) return -1; return this->EmbeddingDimension[k]; }
325 
GetFieldSize(int k)326 inline int vtkStreamingTessellator::GetFieldSize( int k ) const
327 { if ( k <= 0 || k >= 4 ) return -1; return this->PointDimension[k] - this->EmbeddingDimension[k] - 3; }
328 
GetMaximumNumberOfSubdivisions()329 inline int vtkStreamingTessellator::GetMaximumNumberOfSubdivisions() {return this->MaximumNumberOfSubdivisions;}
330 
331 //ETX
332 
333 #endif // vtkStreamingTessellator_h
334