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 "bbcreaVtkCreateMeshFromPoints.h"
5 #include "bbcreaVtkPackage.h"
7 #include "vtkTriangleStrip.h"
13 BBTK_ADD_BLACK_BOX_TO_PACKAGE(creaVtk,CreateMeshFromPoints)
14 BBTK_BLACK_BOX_IMPLEMENTATION(CreateMeshFromPoints,bbtk::AtomicBlackBox);
16 // 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)
18 void CreateMeshFromPoints::Process()
21 // THE MAIN PROCESSING METHOD BODY
22 // Here we simply set the input 'In' value to the output 'Out'
23 // And print out the output value
24 // INPUT/OUTPUT ACCESSORS ARE OF THE FORM :
25 // void bbSet{Input|Output}NAME(const TYPE&)
26 // const TYPE& bbGet{Input|Output}NAME() const
28 // * NAME is the name of the input/output
29 // (the one provided in the attribute 'name' of the tag 'input')
30 // * TYPE is the C++ type of the input/output
31 // (the one provided in the attribute 'type' of the tag 'input')
33 // bbSetOutputOut( bbGetInputIn() );
34 // std::cout << "Output value = " <<bbGetOutputOut() << std::endl;
36 std::vector<double> lstX = bbGetInputLstX();
37 std::vector<double> lstY = bbGetInputLstY();
38 std::vector<double> lstZ = bbGetInputLstZ();
39 std::vector<int> lstIndexs = bbGetInputLstIndexs();
40 if ( (lstIndexs.size()<=1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
42 printf("PG CreateMeshFromPoints::Process: List of points X Y Z and LstIndexes is not correct\n");
45 int ii,sizeSegment1,sizeSegment2;
47 // vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
48 if (points!=NULL) points->Delete();
49 points = vtkPoints::New();
50 int i,sizeLstX = lstX.size();
51 for (i=0;i<sizeLstX;i++)
53 points->InsertNextPoint(lstX[i],lstY[i],lstZ[i]);
55 // vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
56 if (cells!=NULL) cells->Delete();
57 cells = vtkCellArray::New();
59 int maxSegment1,maxSegment2;
62 int sizeLstIdexes=lstIndexs.size();
63 for (i=0; i<sizeLstIdexes-1; i++ )
65 sizeSegment1 = lstIndexs[i];
66 sizeSegment2 = lstIndexs[i+1];
67 vtkSmartPointer<vtkTriangleStrip> triangleStrip = vtkSmartPointer<vtkTriangleStrip>::New();
68 triangleStrip->GetPointIds()->SetNumberOfIds(sizeSegment1+sizeSegment2);
69 maxElements=sizeSegment1;
70 if (maxElements<sizeSegment2) maxElements=sizeSegment2;
71 maxSegment1 = iGeneral+sizeSegment1;
72 maxSegment2 = iGeneral+sizeSegment1+sizeSegment2;
74 iSeg2 = iGeneral+sizeSegment1;
75 for (ii=0; ii<maxElements; ii++)
77 triangleStrip->GetPointIds()->SetId(ii*2 ,iSeg1);
78 triangleStrip->GetPointIds()->SetId(ii*2+1,iSeg2);
81 if (iSeg1>=maxSegment1) { iSeg1=maxSegment1-1; }
82 if (iSeg2>=maxSegment2) { iSeg2=maxSegment2-1; }
84 iGeneral=iGeneral+sizeSegment1;
85 cells->InsertNextCell(triangleStrip);
90 if(bbGetInputOpenClose()){
91 CloseContourSides(lstIndexs, true);
94 CloseOpenContourSurface(lstIndexs);
97 // vtkPolyData *polydata = vtkPolyData::New();
98 if (polydata!=NULL) polydata->Delete();
99 polydata = vtkPolyData::New();
100 polydata->SetPoints(points);
101 polydata->SetStrips(cells);
102 // vtkCleanPolyData *clean=vtkCleanPolyData::New();
103 if (clean!=NULL) clean->Delete();
104 clean = vtkCleanPolyData::New();
105 clean->SetInputData(polydata);
107 // vtkTriangleFilter *triangle = vtkTriangleFilter::New();
108 if (triangle!=NULL) triangle->Delete();
109 triangle = vtkTriangleFilter::New();
110 triangle->SetInputData( clean->GetOutput() );
112 bbSetOutputOut( clean->GetOutput() );
114 printf("CreateMeshFromPoints::Process: End\n");
117 void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
118 int sizeLstIdexes = lstIndexs.size();
119 int sizePoints = bbGetInputLstX().size();
121 int firstIndex, end, centroidId, numPointsFace;
122 int increment = uPointOrder?1:sizeLstIdexes;
123 double centroid[3], currentPoint[3] = {};
125 for(int facesIdx = 0; facesIdx < 2; facesIdx++){
126 std::fill(std::begin(centroid), std::end(centroid), 0);
129 numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
130 end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
133 firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0] - 1;
134 numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
135 end = uPointOrder?firstIndex + numPointsFace:sizePoints;
137 if(numPointsFace > 1){
138 double lastPoint[3] = {};
139 CalcCentroid(centroid, firstIndex, end, increment, numPointsFace);
140 centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
141 vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
142 triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1);
144 int initial = firstIndex;
145 int triangleIndex = 0;
146 for(int index = initial; index < end; index+=increment){
147 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
148 if(index+increment >= end){
149 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,initial);
150 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
153 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
157 cells->InsertNextCell(triangleStrip1);
160 int initial = firstIndex-1;
161 int triangleIndex = 0;
162 int triangleStripStart = end-1;
163 for(int index = triangleStripStart; index > initial ; index-=increment){
164 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
165 if(index-increment <= initial){
166 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,triangleStripStart);
167 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
170 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
174 cells->InsertNextCell(triangleStrip1);
181 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
182 int sizeLstIdexes = lstIndexs.size();
183 int sizeLstX = bbGetInputLstX().size();
184 bool linePointOrder = false;
187 CloseContourSides(lstIndexs, false);
188 CloseContourBottom(false);
191 CloseContourSides(lstIndexs, true);
192 CloseContourBottom(true);
197 void CreateMeshFromPoints::CalcCentroid(double(¢roid)[3], int start, int end, int increment, int numPoints){
198 double currPoint[3] = {};
199 for(int i = start; i < end; i+=increment){
200 points->GetPoint(i, currPoint);
201 centroid[0] += currPoint[0];
202 centroid[1] += currPoint[1];
203 centroid[2] += currPoint[2];
205 centroid[0] /= numPoints;
206 centroid[1] /= numPoints;
207 centroid[2] /= numPoints;
210 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
211 std::vector<int> lstIndexs = bbGetInputLstIndexs();
212 int sizeLstIdexes = lstIndexs.size();
213 int sizeLstX = bbGetInputLstX().size();
215 vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
216 triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
217 int triangleIndex = 0, currentId = 0, nextId = 0;
218 for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
219 triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
220 nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
221 triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
223 currentId = uPointOrder?nextId + 1: splineIndex+1;
225 cells->InsertNextCell(triangleStripBottom);
229 // 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)
231 void CreateMeshFromPoints::bbUserSetDefaultValues()
234 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
235 // Here we initialize the input 'In' to 0
237 bbSetInputOpenClose(false);
245 // 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)
247 void CreateMeshFromPoints::bbUserInitializeProcessing()
250 // THE INITIALIZATION METHOD BODY :
252 // but this is where you should allocate the internal/output pointers
258 // 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)
260 void CreateMeshFromPoints::bbUserFinalizeProcessing()
263 // THE FINALIZATION METHOD BODY :
265 // but this is where you should desallocate the internal/output pointers
270 // EO namespace bbcreaVtk