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("Warning! 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);
88 int lastId1 = lstIndexs[0]-1;
89 int lastId2 = sizeLstX - 1;
90 int firstId2 = sizeLstX - lstIndexs[sizeLstIdexes - 1];
91 bool face1open = lstX[0] != lstX[lastId1] && lstY[0] != lstY[lastId1] && lstZ[0] != lstZ[lastId1];
92 bool face2open = lstX[firstId2] != lstX[lastId2] && lstY[firstId2] != lstY[lastId2] && lstZ[firstId2] != lstZ[lastId2];
94 bool altFace1open = lstX[0] != lstX[firstId2] && lstY[0] != lstY[firstId2] && lstZ[0] != lstZ[firstId2];
95 bool altFace2open = lstX[lastId1] != lstX[lastId2] && lstY[lastId1] != lstY[lastId2] && lstZ[lastId1] != lstZ[lastId2];
98 if(bbGetInputCloseSurface())
100 //false = Open Contour
101 //true = Closed Contour
102 if(!face1open && !face2open)
105 CloseContourSides(lstIndexs, true);
107 else if(!altFace1open && !altFace2open)
110 CloseContourSides(lstIndexs, false);
113 CloseOpenContourSurface(lstIndexs);
117 // vtkPolyData *polydata = vtkPolyData::New();
118 if (polydata!=NULL) polydata->Delete();
119 polydata = vtkPolyData::New();
120 polydata->SetPoints(points);
121 polydata->SetStrips(cells);
122 // vtkCleanPolyData *clean=vtkCleanPolyData::New();
123 if (clean!=NULL) clean->Delete();
124 clean = vtkCleanPolyData::New();
125 clean->SetInputData(polydata);
127 // vtkTriangleFilter *triangle = vtkTriangleFilter::New();
128 if (triangle!=NULL) triangle->Delete();
129 triangle = vtkTriangleFilter::New();
130 triangle->SetInputData( clean->GetOutput() );
132 bbSetOutputOut( triangle->GetOutput() );
133 // bbSetOutputOut( clean->GetOutput() );
135 //printf("PG CreateMeshFromPoints::Process: End\n");
139 * Closes the sides of the contour
140 * iterates in one way or the other, depending on the order of the points and calculated vectors.
141 * uPointOrder: Points are order in a U shape
142 * lstIndexs: number of points on each spline
144 void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
145 int sizeLstIdexes = lstIndexs.size();
146 int sizePoints = bbGetInputLstX().size();
148 int firstIndex, end, centroidId, numPointsFace, contraryId;
149 int increment = uPointOrder?1:sizeLstIdexes;
151 int numProcessFaces = sizeLstIdexes > 1?2:1;
153 for(int facesIdx = 0; facesIdx < numProcessFaces; facesIdx++){
154 std::fill(std::begin(centroid), std::end(centroid), 0);
158 numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
159 end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
160 contraryId = sizePoints-1;
162 firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0]-1;
163 numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
164 end = uPointOrder?firstIndex + numPointsFace:sizePoints;
167 if(numPointsFace > 1)
169 bool validCentroid = CalcValidCentroid(centroid, firstIndex, end, increment, numPointsFace);
172 bool normalOrder = isPointingCorrectly(firstIndex, firstIndex+increment, centroid, contraryId);
173 centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
174 vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
175 triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2 + (!isClosedCont?2:0));
178 int initial = firstIndex;
179 int triangleIndex = 0;
180 for(int index = initial; index < end; index+=increment){
181 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
182 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);//1
183 if(index+increment >= end && !isClosedCont){
184 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,initial);//2
185 triangleStrip1->GetPointIds()->SetId(triangleIndex+3,centroidId);//3
189 cells->InsertNextCell(triangleStrip1);
191 int initial = firstIndex-1;
192 int triangleIndex = 0;
193 int triangleStripStart = end-1;
194 for(int index = triangleStripStart; index > initial; index-=increment){
195 triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
196 triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
197 if(index-increment <= initial && !isClosedCont){
198 triangleStrip1->GetPointIds()->SetId(triangleIndex+2,triangleStripStart);
199 triangleStrip1->GetPointIds()->SetId(triangleIndex+3,centroidId);
203 cells->InsertNextCell(triangleStrip1);
212 * Checks if the normal from firstPointId, secPointId and centroid points away
213 * from the vector centroid to contrPointId.
214 * Used to check that the order used to create the new polygons is correct.
216 bool CreateMeshFromPoints::isPointingCorrectly( int firstPointId, int secPointId, double(¢roid)[3], int contrPointId) {
218 double firstPoint[3], secPoint[3], contrPoint[3];
219 points->GetPoint(firstPointId, firstPoint);
220 points->GetPoint(secPointId, secPoint);
222 double firstVect[3], secVect[3], normal[3], contrVect[3];
224 vtkMath::Subtract(firstPoint, centroid, firstVect);
225 vtkMath::Subtract(secPoint, centroid, secVect);
227 points->GetPoint(contrPointId, contrPoint);
228 vtkMath::Subtract(contrPoint, centroid, contrVect);
230 vtkMath::Cross(firstVect, secVect, normal);
232 dotCalc = vtkMath::Dot(normal, contrVect);
238 * Checks if points on each side of the shapes represent a curve.
240 bool CreateMeshFromPoints::CheckLinePointOrder(){
241 int sizePoints = bbGetInputLstX().size();
242 std::vector<int> lstIndexs = bbGetInputLstIndexs();
243 double point1[3], point2[3], point3[3];
245 double firstRadiusSum = 0;
246 double secondRadiusSum = 0;
247 for(int i = 0; i < lstIndexs[0] && lstIndexs[0] > 9; i+=5){
248 if(i+10 < lstIndexs[0]){
249 points->GetPoint(i, point1);
250 points->GetPoint(i+5, point2);
251 points->GetPoint(i+10, point3);
252 firstRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
256 for(int i = 0; i < sizePoints && lstIndexs.size() > 9; i+=(lstIndexs.size()*5)){
257 if(i+(10*lstIndexs.size()) < sizePoints){
258 points->GetPoint(i, point1);
259 points->GetPoint(i+(5*lstIndexs.size()), point2);
260 points->GetPoint(i+(10*lstIndexs.size()), point3);
261 secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
265 return firstRadiusSum > secondRadiusSum;
269 * Closes an open contour
270 * lstIndexs: number of points on each spline
272 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
273 bool linePointOrder = CheckLinePointOrder();
274 CloseContourSides(lstIndexs, !linePointOrder);
275 CloseContourBottom(!linePointOrder);
279 * Calculates centroid and checks if points are collinear.
280 * centroid: array to store calculation
281 * start: start index of points to use
282 * end: end index of points to use
283 * increment: increment to be used in point iteration
284 * numPoints: number of points used to calculate the centroid.
285 * Returns a bool indicating the validity of the centroid calculated.
286 * False = invalid centroid = all points are the same.
288 bool CreateMeshFromPoints::CalcValidCentroid(double(¢roid)[3], int start, int end, int increment, int numPoints){
289 double currPoint[3] = {}, prevPoint[3] = {}, middlePoint[3] = {}, firstPoint[3] = {};
290 double vector1[3], vector2[3];
291 bool samePoint = true;
292 int splineMidPoint = numPoints/2;
293 bool collinear = true;
295 points->GetPoint(start, firstPoint);
296 points->GetPoint(splineMidPoint, middlePoint);
297 vtkMath::Subtract(middlePoint, firstPoint, vector1);
299 for(int i = start; i < end; i+=increment){
300 points->GetPoint(i, currPoint);
301 if(samePoint && i > start && (currPoint[0] != prevPoint[0] || currPoint[1] != prevPoint[1] || currPoint[2] != prevPoint[2])){
305 vtkMath::Subtract(currPoint, firstPoint, vector2);
306 double angle = vtkMath::AngleBetweenVectors(vector1, vector2);
307 if(angle > 0.0001 && collinear){
311 centroid[0] += currPoint[0];
312 centroid[1] += currPoint[1];
313 centroid[2] += currPoint[2];
314 std::copy(std::begin(currPoint), std::end(currPoint), prevPoint);
317 centroid[0] /= numPoints;
318 centroid[1] /= numPoints;
319 centroid[2] /= numPoints;
321 return !samePoint && !collinear;
325 * Closes the bottom of the given countour.
326 * Should only be used when its an open contour.
327 * uPointOrder: points are ordered in U shape
329 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
330 std::vector<int> lstIndexs = bbGetInputLstIndexs();
331 int sizeLstIdexes = lstIndexs.size();
332 int sizeLstX = bbGetInputLstX().size();
334 vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
335 triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
336 int triangleIndex = 0, currentId = 0, nextId = 0;
337 for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
338 triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
339 nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
340 triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
342 currentId = uPointOrder?nextId + 1: splineIndex+1;
344 cells->InsertNextCell(triangleStripBottom);
348 // 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)
350 void CreateMeshFromPoints::bbUserSetDefaultValues()
353 // SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX
354 // Here we initialize the input 'In' to 0
356 bbSetInputCloseSurface(false);
365 // 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)
367 void CreateMeshFromPoints::bbUserInitializeProcessing()
370 // THE INITIALIZATION METHOD BODY :
372 // but this is where you should allocate the internal/output pointers
378 // 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)
380 void CreateMeshFromPoints::bbUserFinalizeProcessing()
383 // THE FINALIZATION METHOD BODY :
385 // but this is where you should desallocate the internal/output pointers
390 // EO namespace bbcreaVtk