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