]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkPolyDataNormals.cxx
e36998924ed266a6a1ebef233de5849ce13297e6
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkPolyDataNormals.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbcreaVtkPolyDataNormals.h"
5 #include "bbcreaVtkPackage.h"
6
7 #include <vtkPolyDataNormals.h>
8 //#include <vtkTriangleMeshPointNormals.h>
9
10 #include <vtkPolyData.h>
11 #include <vtkPointData.h>
12 #include <vtkDataArray.h>
13 #include <vtkMath.h>
14
15 namespace bbcreaVtk
16 {
17
18 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,PolyDataNormals)
19 BBTK_BLACK_BOX_IMPLEMENTATION(PolyDataNormals,bbtk::AtomicBlackBox);
20 //===== 
21 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
22 //===== 
23 void PolyDataNormals::Process()
24 {
25 // THE MAIN PROCESSING METHOD BODY
26 //   Here we simply set the input 'In' value to the output 'Out'
27 //   And print out the output value
28 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
29 //    void bbSet{Input|Output}NAME(const TYPE&)
30 //    const TYPE& bbGet{Input|Output}NAME() const 
31 //    Where :
32 //    * NAME is the name of the input/output
33 //      (the one provided in the attribute 'name' of the tag 'input')
34 //    * TYPE is the C++ type of the input/output
35 //      (the one provided in the attribute 'type' of the tag 'input')
36 //    bbSetOutputOut( bbGetInputIn() );
37 //    std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
38     
39     printf("EED  PolyDataNormals::Process In=%p \n", bbGetInputIn() );
40
41     if (bbGetInputIn()!=NULL)
42     {
43   
44         vtkPolyDataNormals* normal = vtkPolyDataNormals::New();
45         normal->SetInputData( bbGetInputIn() );
46         if (bbGetInputComputeType()==0)
47         {
48             normal->ComputeCellNormalsOn();
49         } else {
50             normal->ComputePointNormalsOn();
51         }
52         normal->SetFeatureAngle(360);
53         normal->Update();
54     
55         /*
56         vtkTriangleMeshPointNormals* normal = vtkTriangleMeshPointNormals::New();
57         normal->SetInputData( bbGetInputIn() );
58         normal->Update();
59         */
60         
61         // >>>>>>>>>>>>>>>>>>>> Calculate the MeanNormal
62         vtkPolyData     *polydata   = normal->GetOutput();
63         vtkPointData    *pointdata  = polydata->GetPointData();
64         vtkDataArray    *dataarray;
65         double          *pValue;
66         /*
67         int i,size=pointdata->GetNumberOfArrays();
68         for(i=0;i<size;i++)
69         {
70             dataarray=pointdata->GetArray(i);
71             printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s  n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
72         } // for i
73         */
74         dataarray   = pointdata->GetNormals();
75         std::vector<double> meanNormal;
76         meanNormal.push_back(0);
77         meanNormal.push_back(0);
78         meanNormal.push_back(0);
79         if (dataarray!=NULL)
80         {
81             int i , size=dataarray->GetNumberOfTuples();
82             for (i=0; i<size; i++)
83             {
84                 pValue        = dataarray->GetTuple3( i );
85                 meanNormal[0] = meanNormal[0] + pValue[0];
86                 meanNormal[1] = meanNormal[1] + pValue[1];
87                 meanNormal[2] = meanNormal[2] + pValue[2];
88             } // for i
89             
90             if (size!=0)
91             {
92                 meanNormal[0] = meanNormal[0] / size;
93                 meanNormal[1] = meanNormal[1] / size;
94                 meanNormal[2] = meanNormal[2] / size;
95                 double magnitude = sqrt( meanNormal[0]*meanNormal[0] + meanNormal[1]*meanNormal[1]  + meanNormal[2]*meanNormal[2] );
96                 meanNormal[0] = meanNormal[0] / magnitude;
97                 meanNormal[1] = meanNormal[1] / magnitude;
98                 meanNormal[2] = meanNormal[2] / magnitude;
99             } else {
100                 meanNormal[0] = 1;
101                 meanNormal[1] = 0;
102                 meanNormal[2] = 0;
103             } // if size
104         } // if dataarray
105         // <<<<<<<<<<<<<<<<<<<<< Calculate the MeanNormal
106
107         if ((bbGetInputType()==1) && (dataarray!=NULL) )
108         {
109             double meanN[3];
110             double wxyz[4];
111             double meanNormalresult[3];
112             double ang;
113             double crossResult[3];
114             meanN[0] = meanNormal[0];
115             meanN[1] = meanNormal[1];
116             meanN[2] = meanNormal[2];
117             double magnitude;
118             int i , size=dataarray->GetNumberOfTuples();
119             for (i=0; i<size; i++)
120             {
121                 pValue  = dataarray->GetTuple3( i );
122                 ang     = vtkMath::AngleBetweenVectors( pValue, meanN );
123                 vtkMath::Cross(meanN,pValue,crossResult);
124                 wxyz[0] = ang * bbGetInputParam();
125                 wxyz[1] = crossResult[0];
126                 wxyz[2] = crossResult[1];
127                 wxyz[3] = crossResult[2];
128                 vtkMath::RotateVectorByWXYZ(meanN, wxyz ,meanNormalresult);
129                 magnitude = sqrt(   meanNormalresult[0]*meanNormalresult[0] +  meanNormalresult[1]*meanNormalresult[1] + meanNormalresult[2]* meanNormalresult[2]  );
130                 meanNormalresult[0] = meanNormalresult[0] / magnitude;
131                 meanNormalresult[1] = meanNormalresult[1] / magnitude;
132                 meanNormalresult[2] = meanNormalresult[2] / magnitude;
133                 dataarray->SetTuple3( i , meanNormalresult[0], meanNormalresult[1], meanNormalresult[2] );
134             } // for
135         } // if Type==1  &&  dataarray
136         bbSetOutputMeanNormal( meanNormal );
137         bbSetOutputOut( normal->GetOutput() );
138     } else {
139         bbSetOutputOut( NULL );
140     }// if In
141     
142 }
143 //===== 
144 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
145 //===== 
146 void PolyDataNormals::bbUserSetDefaultValues()
147 {
148 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
149 //    Here we initialize the input 'In' to 0
150     bbSetInputIn(NULL);
151     bbSetOutputOut( NULL );
152 }
153 //===== 
154 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
155 //===== 
156 void PolyDataNormals::bbUserInitializeProcessing()
157 {
158
159 //  THE INITIALIZATION METHOD BODY :
160 //    Here does nothing 
161 //    but this is where you should allocate the internal/output pointers 
162 //    if any 
163
164   
165 }
166 //===== 
167 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
168 //===== 
169 void PolyDataNormals::bbUserFinalizeProcessing()
170 {
171
172 //  THE FINALIZATION METHOD BODY :
173 //    Here does nothing 
174 //    but this is where you should desallocate the internal/output pointers 
175 //    if any
176   
177 }
178 }
179 // EO namespace bbcreaVtk
180
181