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)
4 #include "bbcreaVtkPolyDataNormals.h"
5 #include "bbcreaVtkPackage.h"
7 #include <vtkPolyDataNormals.h>
8 //#include <vtkTriangleMeshPointNormals.h>
10 #include <vtkPolyData.h>
11 #include <vtkPointData.h>
12 #include <vtkDataArray.h>
18 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,PolyDataNormals)
19 BBTK_BLACK_BOX_IMPLEMENTATION(PolyDataNormals,bbtk::AtomicBlackBox);
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)
23 void PolyDataNormals::Process()
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
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;
39 if (bbGetInputIn()!=NULL)
41 vtkPolyDataNormals* normal = vtkPolyDataNormals::New();
42 normal->SetInputData( bbGetInputIn() );
43 if (bbGetInputComputeType()==0)
45 normal->ComputeCellNormalsOn();
47 normal->ComputePointNormalsOn();
49 normal->SetFeatureAngle(360);
53 vtkTriangleMeshPointNormals* normal = vtkTriangleMeshPointNormals::New();
54 normal->SetInputData( bbGetInputIn() );
58 // >>>>>>>>>>>>>>>>>>>> Calculate the MeanNormal
59 vtkPolyData *polydata = normal->GetOutput();
60 vtkPointData *pointdata = polydata->GetPointData();
61 vtkDataArray *dataarray;
64 int i,size=pointdata->GetNumberOfArrays();
67 dataarray=pointdata->GetArray(i);
68 printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
71 dataarray = pointdata->GetNormals();
72 std::vector<double> meanNormal;
73 meanNormal.push_back(0);
74 meanNormal.push_back(0);
75 meanNormal.push_back(0);
78 int i , size=dataarray->GetNumberOfTuples();
79 for (i=0; i<size; i++)
81 pValue = dataarray->GetTuple3( i );
82 meanNormal[0] = meanNormal[0] + pValue[0];
83 meanNormal[1] = meanNormal[1] + pValue[1];
84 meanNormal[2] = meanNormal[2] + pValue[2];
89 meanNormal[0] = meanNormal[0] / size;
90 meanNormal[1] = meanNormal[1] / size;
91 meanNormal[2] = meanNormal[2] / size;
92 double magnitude = sqrt( meanNormal[0]*meanNormal[0] + meanNormal[1]*meanNormal[1] + meanNormal[2]*meanNormal[2] );
93 meanNormal[0] = meanNormal[0] / magnitude;
94 meanNormal[1] = meanNormal[1] / magnitude;
95 meanNormal[2] = meanNormal[2] / magnitude;
102 // <<<<<<<<<<<<<<<<<<<<< Calculate the MeanNormal
104 if ((bbGetInputType()==1) && (dataarray!=NULL) )
108 double meanNormalresult[3];
110 double crossResult[3];
111 meanN[0] = meanNormal[0];
112 meanN[1] = meanNormal[1];
113 meanN[2] = meanNormal[2];
115 int i , size=dataarray->GetNumberOfTuples();
116 for (i=0; i<size; i++)
118 pValue = dataarray->GetTuple3( i );
119 ang = vtkMath::AngleBetweenVectors( pValue, meanN );
120 vtkMath::Cross(meanN,pValue,crossResult);
121 wxyz[0] = ang * bbGetInputParam();
122 wxyz[1] = crossResult[0];
123 wxyz[2] = crossResult[1];
124 wxyz[3] = crossResult[2];
125 vtkMath::RotateVectorByWXYZ(meanN, wxyz ,meanNormalresult);
126 magnitude = sqrt( meanNormalresult[0]*meanNormalresult[0] + meanNormalresult[1]*meanNormalresult[1] + meanNormalresult[2]* meanNormalresult[2] );
127 meanNormalresult[0] = meanNormalresult[0] / magnitude;
128 meanNormalresult[1] = meanNormalresult[1] / magnitude;
129 meanNormalresult[2] = meanNormalresult[2] / magnitude;
130 dataarray->SetTuple3( i , meanNormalresult[0], meanNormalresult[1], meanNormalresult[2] );
132 } // if Type==1 && dataarray
133 bbSetOutputMeanNormal( meanNormal );
134 bbSetOutputOut( normal->GetOutput() );
136 bbSetOutputOut( NULL );
141 // 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 void PolyDataNormals::bbUserSetDefaultValues()
145 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
146 // Here we initialize the input 'In' to 0
148 bbSetOutputOut( NULL );
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)
154 void PolyDataNormals::bbUserInitializeProcessing()
156 // THE INITIALIZATION METHOD BODY :
158 // but this is where you should allocate the internal/output pointers
163 // 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)
165 void PolyDataNormals::bbUserFinalizeProcessing()
167 // THE FINALIZATION METHOD BODY :
169 // but this is where you should desallocate the internal/output pointers
173 }// EO namespace bbcreaVtk