@@ -13,17 +13,24 @@ function vtkPolyDataNormals(publicAPI, model) {
13
13
// Set our className
14
14
model . classHierarchy . push ( 'vtkPolyDataNormals' ) ;
15
15
16
- publicAPI . vtkPolyDataNormalsExecute = ( pointsData , polysData ) => {
16
+ publicAPI . vtkPolyDataNormalsExecute = (
17
+ numberOfPolys ,
18
+ polysData ,
19
+ pointsData
20
+ ) => {
17
21
if ( ! pointsData ) {
18
22
return null ;
19
23
}
20
24
21
- const normalsData = new Float32Array ( pointsData . length ) ;
25
+ const pointNormals = new Float32Array ( pointsData . length ) ;
26
+ const cellNormals = new Float32Array ( 3 * numberOfPolys ) ;
27
+ let cellNormalComponent = 0 ;
22
28
23
29
let numberOfPoints = 0 ;
24
30
const polysDataLength = polysData . length ;
25
31
26
32
const cellPointIds = [ 0 , 0 , 0 ] ;
33
+ const cellNormal = [ 0 , 0 , 0 ] ;
27
34
28
35
for ( let c = 0 ; c < polysDataLength ; c += numberOfPoints + 1 ) {
29
36
numberOfPoints = polysData [ c ] ;
@@ -36,37 +43,46 @@ function vtkPolyDataNormals(publicAPI, model) {
36
43
cellPointIds [ i - 1 ] = 3 * polysData [ c + i ] ;
37
44
}
38
45
39
- const cellNormal = [ ] ;
40
-
41
46
vtkTriangle . computeNormal (
42
47
pointsData . slice ( cellPointIds [ 0 ] , cellPointIds [ 0 ] + 3 ) ,
43
48
pointsData . slice ( cellPointIds [ 1 ] , cellPointIds [ 1 ] + 3 ) ,
44
49
pointsData . slice ( cellPointIds [ 2 ] , cellPointIds [ 2 ] + 3 ) ,
45
50
cellNormal
46
51
) ;
47
52
48
- for ( let i = 1 ; i <= numberOfPoints ; ++ i ) {
49
- let pointId = 3 * polysData [ c + i ] ;
53
+ cellNormals [ cellNormalComponent ++ ] = cellNormal [ 0 ] ;
54
+ cellNormals [ cellNormalComponent ++ ] = cellNormal [ 1 ] ;
55
+ cellNormals [ cellNormalComponent ++ ] = cellNormal [ 2 ] ;
56
+
57
+ if ( model . computePointNormals ) {
58
+ for ( let i = 1 ; i <= numberOfPoints ; ++ i ) {
59
+ let pointId = 3 * polysData [ c + i ] ;
50
60
51
- normalsData [ pointId ] += cellNormal [ 0 ] ;
52
- normalsData [ ++ pointId ] += cellNormal [ 1 ] ;
53
- normalsData [ ++ pointId ] += cellNormal [ 2 ] ;
61
+ pointNormals [ pointId ] += cellNormal [ 0 ] ;
62
+ pointNormals [ ++ pointId ] += cellNormal [ 1 ] ;
63
+ pointNormals [ ++ pointId ] += cellNormal [ 2 ] ;
64
+ }
54
65
}
55
66
}
56
67
57
- /* Normalize normals */
68
+ // Normalize point normals.
69
+ // A point normal is the sum of all the cell normals the point belongs to
70
+ if ( model . computePointNormals ) {
71
+ const pointNormal = [ 0 , 0 , 0 ] ;
72
+ for ( let i = 0 ; i < pointsData . length ; ) {
73
+ pointNormal [ 0 ] = pointNormals [ i ] ;
74
+ pointNormal [ 1 ] = pointNormals [ i + 1 ] ;
75
+ pointNormal [ 2 ] = pointNormals [ i + 2 ] ;
58
76
59
- for ( let i = 0 ; i < pointsData . length ; ) {
60
- const pointNormal = normalsData . slice ( i , i + 3 ) ;
77
+ vtkMath . normalize ( pointNormal ) ;
61
78
62
- vtkMath . normalize ( pointNormal ) ;
63
-
64
- normalsData [ i ++ ] = pointNormal [ 0 ] ;
65
- normalsData [ i ++ ] = pointNormal [ 1 ] ;
66
- normalsData [ i ++ ] = pointNormal [ 2 ] ;
79
+ pointNormals [ i ++ ] = pointNormal [ 0 ] ;
80
+ pointNormals [ i ++ ] = pointNormal [ 1 ] ;
81
+ pointNormals [ i ++ ] = pointNormal [ 2 ] ;
82
+ }
67
83
}
68
84
69
- return normalsData ;
85
+ return [ cellNormals , pointNormals ] ;
70
86
} ;
71
87
72
88
publicAPI . requestData = ( inData , outData ) => {
@@ -82,18 +98,8 @@ function vtkPolyDataNormals(publicAPI, model) {
82
98
return ;
83
99
}
84
100
85
- const outputNormalsData = publicAPI . vtkPolyDataNormalsExecute (
86
- input . getPoints ( ) . getData ( ) ,
87
- input . getPolys ( ) . getData ( )
88
- ) ;
89
-
90
101
const output = vtkPolyData . newInstance ( ) ;
91
102
92
- const outputNormals = vtkDataArray . newInstance ( {
93
- numberOfComponents : 3 ,
94
- values : outputNormalsData ,
95
- } ) ;
96
-
97
103
output . setPoints ( input . getPoints ( ) ) ;
98
104
output . setVerts ( input . getVerts ( ) ) ;
99
105
output . setLines ( input . getLines ( ) ) ;
@@ -104,7 +110,29 @@ function vtkPolyDataNormals(publicAPI, model) {
104
110
output . getCellData ( ) . passData ( input . getCellData ( ) ) ;
105
111
output . getFieldData ( ) . passData ( input . getFieldData ( ) ) ;
106
112
107
- output . getPointData ( ) . setNormals ( outputNormals ) ;
113
+ const [ cellNormals , pointNormals ] = publicAPI . vtkPolyDataNormalsExecute (
114
+ input . getNumberOfPolys ( ) ,
115
+ input . getPolys ( ) . getData ( ) ,
116
+ input . getPoints ( ) . getData ( )
117
+ ) ;
118
+
119
+ if ( model . computePointNormals ) {
120
+ const outputPointNormals = vtkDataArray . newInstance ( {
121
+ numberOfComponents : 3 ,
122
+ name : 'Normals' ,
123
+ values : pointNormals ,
124
+ } ) ;
125
+ output . getPointData ( ) . setNormals ( outputPointNormals ) ;
126
+ }
127
+
128
+ if ( model . computeCellNormals ) {
129
+ const outputCellNormals = vtkDataArray . newInstance ( {
130
+ numberOfComponents : 3 ,
131
+ name : 'Normals' ,
132
+ values : cellNormals ,
133
+ } ) ;
134
+ output . getCellData ( ) . setNormals ( outputCellNormals ) ;
135
+ }
108
136
109
137
outData [ 0 ] = output ;
110
138
} ;
@@ -115,6 +143,8 @@ function vtkPolyDataNormals(publicAPI, model) {
115
143
// ----------------------------------------------------------------------------
116
144
function defaultValues ( initialValues ) {
117
145
return {
146
+ computeCellNormals : false ,
147
+ computePointNormals : true ,
118
148
...initialValues ,
119
149
} ;
120
150
}
@@ -131,6 +161,8 @@ export function extend(publicAPI, model, initialValues = {}) {
131
161
132
162
macro . algo ( publicAPI , model , 1 , 1 ) ;
133
163
164
+ macro . setGet ( publicAPI , model , [ 'computeCellNormals' , 'computePointNormals' ] ) ;
165
+
134
166
/* Object specific methods */
135
167
136
168
vtkPolyDataNormals ( publicAPI , model ) ;
0 commit comments