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