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