1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkPolyDataNormals.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 // .NAME vtkPolyDataNormals - compute normals for polygonal mesh
16 // .SECTION Description
17 // vtkPolyDataNormals is a filter that computes point and/or cell normals
18 // for a polygonal mesh. The user specifies if they would like the point
19 // and/or cell normals to be computed by setting the ComputeCellNormals
20 // and ComputePointNormals flags.
21 //
22 // The computed normals (a vtkFloatArray) are set to be the active normals
23 // (using SetNormals()) of the PointData and/or the CellData (respectively)
24 // of the output PolyData. The name of these arrays is "Normals", so they
25 // can be retrieved either with
26 // vtkFloatArray::SafeDownCast(output->GetPointData()->GetNormals())
27 // or with
28 // vtkFloatArray::SafeDownCast(output->GetPointData()->GetArray("Normals"))
29 //
30 // The filter can reorder polygons to insure consistent
31 // orientation across polygon neighbors. Sharp edges can be split and points
32 // duplicated with separate normals to give crisp (rendered) surface definition.
33 // It is also possible to globally flip the normal orientation.
34 //
35 // The algorithm works by determining normals for each polygon and then
36 // averaging them at shared points. When sharp edges are present, the edges
37 // are split and new points generated to prevent blurry edges (due to
38 // Gouraud shading).
39 
40 // .SECTION Caveats
41 // Normals are computed only for polygons and triangle strips. Normals are
42 // not computed for lines or vertices.
43 //
44 // Triangle strips are broken up into triangle polygons. You may want to
45 // restrip the triangles.
46 
47 #ifndef vtkPolyDataNormals_h
48 #define vtkPolyDataNormals_h
49 
50 #include "vtkFiltersCoreModule.h" // For export macro
51 #include "vtkPolyDataAlgorithm.h"
52 
53 class vtkFloatArray;
54 class vtkIdList;
55 class vtkPolyData;
56 
57 class VTKFILTERSCORE_EXPORT vtkPolyDataNormals : public vtkPolyDataAlgorithm
58 {
59 public:
60   vtkTypeMacro(vtkPolyDataNormals,vtkPolyDataAlgorithm);
61   void PrintSelf(ostream& os, vtkIndent indent);
62 
63   // Description:
64   // Construct with feature angle=30, splitting and consistency turned on,
65   // flipNormals turned off, and non-manifold traversal turned on.
66   // ComputePointNormals is on and ComputeCellNormals is off.
67   static vtkPolyDataNormals *New();
68 
69   // Description:
70   // Specify the angle that defines a sharp edge. If the difference in
71   // angle across neighboring polygons is greater than this value, the
72   // shared edge is considered "sharp".
73   vtkSetClampMacro(FeatureAngle,double,0.0,180.0);
74   vtkGetMacro(FeatureAngle,double);
75 
76   // Description:
77   // Turn on/off the splitting of sharp edges.
78   vtkSetMacro(Splitting,int);
79   vtkGetMacro(Splitting,int);
80   vtkBooleanMacro(Splitting,int);
81 
82   // Description:
83   // Turn on/off the enforcement of consistent polygon ordering.
84   vtkSetMacro(Consistency,int);
85   vtkGetMacro(Consistency,int);
86   vtkBooleanMacro(Consistency,int);
87 
88   // Description:
89   // Turn on/off the automatic determination of correct normal
90   // orientation. NOTE: This assumes a completely closed surface
91   // (i.e. no boundary edges) and no non-manifold edges. If these
92   // constraints do not hold, all bets are off. This option adds some
93   // computational complexity, and is useful if you don't want to have
94   // to inspect the rendered image to determine whether to turn on the
95   // FlipNormals flag. However, this flag can work with the FlipNormals
96   // flag, and if both are set, all the normals in the output will
97   // point "inward".
98   vtkSetMacro(AutoOrientNormals, int);
99   vtkGetMacro(AutoOrientNormals, int);
100   vtkBooleanMacro(AutoOrientNormals, int);
101 
102   // Description:
103   // Turn on/off the computation of point normals.
104   vtkSetMacro(ComputePointNormals,int);
105   vtkGetMacro(ComputePointNormals,int);
106   vtkBooleanMacro(ComputePointNormals,int);
107 
108   // Description:
109   // Turn on/off the computation of cell normals.
110   vtkSetMacro(ComputeCellNormals,int);
111   vtkGetMacro(ComputeCellNormals,int);
112   vtkBooleanMacro(ComputeCellNormals,int);
113 
114   // Description:
115   // Turn on/off the global flipping of normal orientation. Flipping
116   // reverves the meaning of front and back for Frontface and Backface
117   // culling in vtkProperty.  Flipping modifies both the normal
118   // direction and the order of a cell's points.
119   vtkSetMacro(FlipNormals,int);
120   vtkGetMacro(FlipNormals,int);
121   vtkBooleanMacro(FlipNormals,int);
122 
123   // Description:
124   // Turn on/off traversal across non-manifold edges. This will prevent
125   // problems where the consistency of polygonal ordering is corrupted due
126   // to topological loops.
127   vtkSetMacro(NonManifoldTraversal,int);
128   vtkGetMacro(NonManifoldTraversal,int);
129   vtkBooleanMacro(NonManifoldTraversal,int);
130 
131   // Description:
132   // Set/get the desired precision for the output types. See the documentation
133   // for the vtkAlgorithm::DesiredOutputPrecision enum for an explanation of
134   // the available precision settings.
135   vtkSetClampMacro(OutputPointsPrecision, int, SINGLE_PRECISION, DEFAULT_PRECISION);
136   vtkGetMacro(OutputPointsPrecision, int);
137 
138 protected:
139   vtkPolyDataNormals();
~vtkPolyDataNormals()140   ~vtkPolyDataNormals() {}
141 
142   // Usual data generation method
143   int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);
144 
145   double FeatureAngle;
146   int Splitting;
147   int Consistency;
148   int FlipNormals;
149   int AutoOrientNormals;
150   int NonManifoldTraversal;
151   int ComputePointNormals;
152   int ComputeCellNormals;
153   int NumFlips;
154   int OutputPointsPrecision;
155 
156 private:
157   vtkIdList *Wave;
158   vtkIdList *Wave2;
159   vtkIdList *CellIds;
160   vtkIdList *Map;
161   vtkPolyData *OldMesh;
162   vtkPolyData *NewMesh;
163   int *Visited;
164   vtkFloatArray *PolyNormals;
165   double CosAngle;
166 
167   // Uses the list of cell ids (this->Wave) to propagate a wave of
168   // checked and properly ordered polygons.
169   void TraverseAndOrder(void);
170 
171   // Check the point id give to see whether it lies on a feature
172   // edge. If so, split the point (i.e., duplicate it) to topologically
173   // separate the mesh.
174   void MarkAndSplit(vtkIdType ptId);
175 
176 private:
177   vtkPolyDataNormals(const vtkPolyDataNormals&);  // Not implemented.
178   void operator=(const vtkPolyDataNormals&);  // Not implemented.
179 };
180 
181 #endif
182