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