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 printf("EED PolyDataNormals::Process In=%p \n", bbGetInputIn() );
41 if (bbGetInputIn()!=NULL)
44 vtkPolyDataNormals* normal = vtkPolyDataNormals::New();
45 normal->SetInputData( bbGetInputIn() );
46 if (bbGetInputComputeType()==0)
48 normal->ComputeCellNormalsOn();
50 normal->ComputePointNormalsOn();
52 normal->SetFeatureAngle(360);
56 vtkTriangleMeshPointNormals* normal = vtkTriangleMeshPointNormals::New();
57 normal->SetInputData( bbGetInputIn() );
61 // >>>>>>>>>>>>>>>>>>>> Calculate the MeanNormal
62 vtkPolyData *polydata = normal->GetOutput();
63 vtkPointData *pointdata = polydata->GetPointData();
64 vtkDataArray *dataarray;
67 int i,size=pointdata->GetNumberOfArrays();
70 dataarray=pointdata->GetArray(i);
71 printf("EED creaVtkCallbackPointPicker::Execute dataarray=%s n=%ld p=%ld\n", dataarray->GetName(),dataarray->GetNumberOfValues() ,polydata->GetNumberOfPoints() );
74 dataarray = pointdata->GetNormals();
75 std::vector<double> meanNormal;
76 meanNormal.push_back(0);
77 meanNormal.push_back(0);
78 meanNormal.push_back(0);
81 int i , size=dataarray->GetNumberOfTuples();
82 for (i=0; i<size; i++)
84 pValue = dataarray->GetTuple3( i );
85 meanNormal[0] = meanNormal[0] + pValue[0];
86 meanNormal[1] = meanNormal[1] + pValue[1];
87 meanNormal[2] = meanNormal[2] + pValue[2];
92 meanNormal[0] = meanNormal[0] / size;
93 meanNormal[1] = meanNormal[1] / size;
94 meanNormal[2] = meanNormal[2] / size;
95 double magnitude = sqrt( meanNormal[0]*meanNormal[0] + meanNormal[1]*meanNormal[1] + meanNormal[2]*meanNormal[2] );
96 meanNormal[0] = meanNormal[0] / magnitude;
97 meanNormal[1] = meanNormal[1] / magnitude;
98 meanNormal[2] = meanNormal[2] / magnitude;
105 // <<<<<<<<<<<<<<<<<<<<< Calculate the MeanNormal
107 if ((bbGetInputType()==1) && (dataarray!=NULL) )
111 double meanNormalresult[3];
113 double crossResult[3];
114 meanN[0] = meanNormal[0];
115 meanN[1] = meanNormal[1];
116 meanN[2] = meanNormal[2];
118 int i , size=dataarray->GetNumberOfTuples();
119 for (i=0; i<size; i++)
121 pValue = dataarray->GetTuple3( i );
122 ang = vtkMath::AngleBetweenVectors( pValue, meanN );
123 vtkMath::Cross(meanN,pValue,crossResult);
124 wxyz[0] = ang * bbGetInputParam();
125 wxyz[1] = crossResult[0];
126 wxyz[2] = crossResult[1];
127 wxyz[3] = crossResult[2];
128 vtkMath::RotateVectorByWXYZ(meanN, wxyz ,meanNormalresult);
129 magnitude = sqrt( meanNormalresult[0]*meanNormalresult[0] + meanNormalresult[1]*meanNormalresult[1] + meanNormalresult[2]* meanNormalresult[2] );
130 meanNormalresult[0] = meanNormalresult[0] / magnitude;
131 meanNormalresult[1] = meanNormalresult[1] / magnitude;
132 meanNormalresult[2] = meanNormalresult[2] / magnitude;
133 dataarray->SetTuple3( i , meanNormalresult[0], meanNormalresult[1], meanNormalresult[2] );
135 } // if Type==1 && dataarray
136 bbSetOutputMeanNormal( meanNormal );
137 bbSetOutputOut( normal->GetOutput() );
139 bbSetOutputOut( NULL );
144 // 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)
146 void PolyDataNormals::bbUserSetDefaultValues()
148 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
149 // Here we initialize the input 'In' to 0
151 bbSetOutputOut( NULL );
154 // 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 void PolyDataNormals::bbUserInitializeProcessing()
159 // THE INITIALIZATION METHOD BODY :
161 // but this is where you should allocate the internal/output pointers
167 // 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)
169 void PolyDataNormals::bbUserFinalizeProcessing()
172 // THE FINALIZATION METHOD BODY :
174 // but this is where you should desallocate the internal/output pointers
179 // EO namespace bbcreaVtk