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