]> Creatis software - creaVtk.git/blob - bbtk_creaVtk_PKG/src/bbcreaVtkPolyDataNormals.cxx
Clean code
[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     if (bbGetInputIn()!=NULL)
40     {
41         vtkPolyDataNormals* normal = vtkPolyDataNormals::New();
42 //        normal->SetAutoOrientNormals(false);
43 //        normal->SetConsistency(false);
44         normal->SetInputData( bbGetInputIn() );
45         if (bbGetInputComputeType()==0)
46         {
47             normal->ComputeCellNormalsOn();
48         } else {
49             normal->ComputePointNormalsOn();
50         }
51         normal->SetFeatureAngle(360);
52         normal->Update();
53     
54         /*
55         vtkTriangleMeshPointNormals* normal = vtkTriangleMeshPointNormals::New();
56         normal->SetInputData( bbGetInputIn() );
57         normal->Update();
58         */
59         
60         // >>>>>>>>>>>>>>>>>>>> Calculate the MeanNormal
61         vtkPolyData     *polydata   = normal->GetOutput();
62         vtkPointData    *pointdata  = polydata->GetPointData();
63         vtkDataArray    *dataarray;
64         double          *pValue;
65         /*
66         int i,size=pointdata->GetNumberOfArrays();
67         for(i=0;i<size;i++)
68         {
69             dataarray=pointdata->GetArray(i);
70             printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s  n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
71         } // for i
72         */
73         dataarray   = pointdata->GetNormals();
74         std::vector<double> meanNormal;
75         meanNormal.push_back(0);
76         meanNormal.push_back(0);
77         meanNormal.push_back(0);
78         if (dataarray!=NULL)
79         {
80             int i , size=dataarray->GetNumberOfTuples();
81             for (i=0; i<size; i++)
82             {
83                 pValue        = dataarray->GetTuple3( i );
84                 meanNormal[0] = meanNormal[0] + pValue[0];
85                 meanNormal[1] = meanNormal[1] + pValue[1];
86                 meanNormal[2] = meanNormal[2] + pValue[2];
87             } // for i
88             
89             if (size!=0)
90             {
91                 meanNormal[0] = meanNormal[0] / size;
92                 meanNormal[1] = meanNormal[1] / size;
93                 meanNormal[2] = meanNormal[2] / size;
94                 double magnitude = sqrt( meanNormal[0]*meanNormal[0] + meanNormal[1]*meanNormal[1]  + meanNormal[2]*meanNormal[2] );
95                 meanNormal[0] = meanNormal[0] / magnitude;
96                 meanNormal[1] = meanNormal[1] / magnitude;
97                 meanNormal[2] = meanNormal[2] / magnitude;
98             } else {
99                 meanNormal[0] = 1;
100                 meanNormal[1] = 0;
101                 meanNormal[2] = 0;
102             } // if size
103         } // if dataarray
104         // <<<<<<<<<<<<<<<<<<<<< Calculate the MeanNormal
105
106         if ((bbGetInputType()==1) && (dataarray!=NULL) )
107         {
108             double meanN[3];
109             double wxyz[4];
110             double meanNormalresult[3];
111             double ang;
112             double crossResult[3];
113             meanN[0] = meanNormal[0];
114             meanN[1] = meanNormal[1];
115             meanN[2] = meanNormal[2];
116             double magnitude;
117             int i , size=dataarray->GetNumberOfTuples();
118             for (i=0; i<size; i++)
119             {
120                 pValue  = dataarray->GetTuple3( i );
121                 ang     = vtkMath::AngleBetweenVectors( pValue, meanN );
122                 vtkMath::Cross(meanN,pValue,crossResult);
123                 wxyz[0] = ang * bbGetInputParam();
124                 wxyz[1] = crossResult[0];
125                 wxyz[2] = crossResult[1];
126                 wxyz[3] = crossResult[2];
127                 vtkMath::RotateVectorByWXYZ(meanN, wxyz ,meanNormalresult);
128                 magnitude = sqrt(   meanNormalresult[0]*meanNormalresult[0] +  meanNormalresult[1]*meanNormalresult[1] + meanNormalresult[2]* meanNormalresult[2]  );
129                 meanNormalresult[0] = meanNormalresult[0] / magnitude;
130                 meanNormalresult[1] = meanNormalresult[1] / magnitude;
131                 meanNormalresult[2] = meanNormalresult[2] / magnitude;
132                 dataarray->SetTuple3( i , meanNormalresult[0], meanNormalresult[1], meanNormalresult[2] );
133             } // for
134         } // if Type==1  &&  dataarray
135         bbSetOutputMeanNormal( meanNormal );
136         bbSetOutputOut( normal->GetOutput() );
137     } else {
138         bbSetOutputOut( NULL );
139     }// if In
140 }
141
142 //===== 
143 // 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)
144 //===== 
145 void PolyDataNormals::bbUserSetDefaultValues()
146 {
147 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
148 //    Here we initialize the input 'In' to 0
149     bbSetInputType(0);
150     bbSetInputComputeType(0);
151     bbSetInputIn(NULL);
152     bbSetOutputOut( NULL );
153 }
154
155 //===== 
156 // 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)
157 //===== 
158 void PolyDataNormals::bbUserInitializeProcessing()
159 {
160 //  THE INITIALIZATION METHOD BODY :
161 //    Here does nothing 
162 //    but this is where you should allocate the internal/output pointers 
163 //    if any
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 //  THE FINALIZATION METHOD BODY :
172 //    Here does nothing 
173 //    but this is where you should desallocate the internal/output pointers 
174 //    if any
175 }
176
177 }// EO namespace bbcreaVtk
178
179