+ //printf("PG CreateMeshFromPoints::Process: End\n");
+}
+
+/**
+* Closes the sides of the contour
+* iterates in one way or the other, depending on the order of the points and calculated vectors.
+* uPointOrder: Points are order in a U shape
+* lstIndexs: number of points on each spline
+*/
+void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
+ int sizeLstIdexes = lstIndexs.size();
+ int sizePoints = bbGetInputLstX().size();
+
+ int firstIndex, end, centroidId, numPointsFace, contraryId;
+ int increment = uPointOrder?1:sizeLstIdexes;
+ double centroid[3];
+
+ for(int facesIdx = 0; facesIdx < 2; facesIdx++){
+ std::fill(std::begin(centroid), std::end(centroid), 0);
+ if(facesIdx == 0){
+ firstIndex = 0;
+ numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
+ end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
+ contraryId = sizePoints-1;
+ }
+ else{
+ firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0]-1;
+ numPointsFace = uPointOrder?lstIndexs[sizeLstIdexes-1]:sizeLstIdexes;
+ end = uPointOrder?firstIndex + numPointsFace:sizePoints;
+ contraryId = 0;
+ }
+ if(numPointsFace > 1){
+ double lastPoint[3] = {};
+ bool validCentroid = CalcValidCentroid(centroid, firstIndex, end, increment, numPointsFace);
+ if(validCentroid){
+ bool normalOrder = isPointingCorrectly(firstIndex, firstIndex+increment, centroid, contraryId);
+ centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
+ vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
+ triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1);
+ if( !normalOrder )
+ { //(facesIdx == 0 && uPointOrder) || (facesIdx == 1 && !uPointOrder)){
+ int initial = firstIndex;
+ int triangleIndex = 0;
+ for(int index = initial; index < end; index+=increment){
+ triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
+ if(index+increment >= end){
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+1,initial);
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
+ }
+ else{
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
+ }
+ triangleIndex+=2;
+ }
+ cells->InsertNextCell(triangleStrip1);
+ } else {
+ int initial = firstIndex-1;
+ int triangleIndex = 0;
+ int triangleStripStart = end-1;
+ for(int index = triangleStripStart; index > initial; index-=increment){
+ triangleStrip1->GetPointIds()->SetId(triangleIndex,index);
+ if(index-increment <= initial){
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+1,triangleStripStart);
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);
+ }
+ else{
+ triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
+ }
+ triangleIndex+=2;
+ }
+ cells->InsertNextCell(triangleStrip1);
+ }
+ }//if validCentroid
+ }//if numPointsFace
+ }//for facesIdx
+
+}
+
+/**
+* Checks if the normal from firstPointId, secPointId and centroid points away
+* from the vector centroid to contrPointId.
+* Used to check that the order used to create the new polygons is correct.
+*/
+bool CreateMeshFromPoints::isPointingCorrectly( int firstPointId, int secPointId, double(¢roid)[3], int contrPointId) {
+
+ double firstPoint[3], secPoint[3], contrPoint[3];
+ points->GetPoint(firstPointId, firstPoint);
+ points->GetPoint(secPointId, secPoint);
+
+ double firstVect[3], secVect[3], normal[3], contrVect[3];
+
+ vtkMath::Subtract(firstPoint, centroid, firstVect);
+ vtkMath::Subtract(secPoint, centroid, secVect);
+
+ points->GetPoint(contrPointId, contrPoint);
+ vtkMath::Subtract(contrPoint, centroid, contrVect);
+
+ vtkMath::Cross(firstVect, secVect, normal);
+ double dotCalc;
+ dotCalc = vtkMath::Dot(normal, contrVect);
+
+ return dotCalc<0;