]> Creatis software - creaVtk.git/blobdiff - bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
#3502 Bug close surface in CreateMeshFromPoints
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkCreateMeshFromPoints.cxx
index bfe74d4182d5d7abb6b93a90010012312ab2de9c..c794d404e66043e165af0fd6449a069061ad4448 100644 (file)
@@ -37,7 +37,7 @@ void CreateMeshFromPoints::Process()
                std::vector<double> lstY                = bbGetInputLstY();
                std::vector<double> lstZ                = bbGetInputLstZ();
                std::vector<int> lstIndexs              = bbGetInputLstIndexs();
-               if ( (lstIndexs.size()<=1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
+               if ( (lstIndexs.size()<1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
                {
                        printf("Warning! CreateMeshFromPoints::Process: List of points X Y Z  and LstIndexes is not correct\n");
                        bbSetOutputOut(NULL);
@@ -78,27 +78,38 @@ void CreateMeshFromPoints::Process()
                                        triangleStrip->GetPointIds()->SetId(ii*2+1,iSeg2);
                                        iSeg1++;
                                        iSeg2++;
-                    if (iSeg1>=maxSegment1) { iSeg1=maxSegment1-1; }
-                    if (iSeg2>=maxSegment2) { iSeg2=maxSegment2-1; }
+                       if (iSeg1>=maxSegment1) { iSeg1=maxSegment1-1; }
+                       if (iSeg2>=maxSegment2) { iSeg2=maxSegment2-1; }
                                } // for ii 
                                iGeneral=iGeneral+sizeSegment1;
                                cells->InsertNextCell(triangleStrip);
                        } //for  LstIndexs
 
-                       int lastId1 = lstIndexs[0]-1;
-                       int lastId2 = sizeLstX - 1;
-                       int firstId2 = sizeLstX - lstIndexs[sizeLstIdexes - 1];
-                       bool face1open = lstX[0] != lstX[lastId1] && lstY[0] != lstY[lastId1] && lstZ[0] != lstZ[lastId1];
-                       bool face2open = lstX[firstId2] != lstX[lastId2] && lstY[firstId2] != lstY[lastId2] && lstZ[firstId2] != lstZ[lastId2];
-                       
                        if(bbGetInputCloseSurface())
                        {                       
+                               int lastId1 = lstIndexs[0]-1;
+                               int lastId2 = sizeLstX - 1;
+                               int firstId2 = sizeLstX - lstIndexs[sizeLstIdexes - 1];
+                               bool face1open = lstX[0] != lstX[lastId1] && lstY[0] != lstY[lastId1] && lstZ[0] != lstZ[lastId1];
+                               bool face2open = lstX[firstId2] != lstX[lastId2] && lstY[firstId2] != lstY[lastId2] && lstZ[firstId2] != lstZ[lastId2];
+                               
+                               bool altFace1open = lstX[0] != lstX[firstId2] && lstY[0] != lstY[firstId2] && lstZ[0] != lstZ[firstId2];
+                               bool altFace2open = lstX[lastId1] != lstX[lastId2] && lstY[lastId1] != lstY[lastId2] && lstZ[lastId1] != lstZ[lastId2];
+                               
+                               isClosedCont = false;
                                //false = Open Contour
                                //true = Closed Contour
                                if(!face1open && !face2open)
-                               {               
+                               {
+                                       isClosedCont = true;
                                        CloseContourSides(lstIndexs, true);
-                               }else{
+                               }
+                               else if(!altFace1open && !altFace2open)
+                               {
+                                       isClosedCont = true;
+                                       CloseContourSides(lstIndexs, false);
+                               }
+                               else{
                                        CloseOpenContourSurface(lstIndexs);
                                }
                        }
@@ -123,7 +134,6 @@ void CreateMeshFromPoints::Process()
                }// if listXYZ size
                //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.
@@ -137,8 +147,8 @@ void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uP
        int firstIndex, end, centroidId, numPointsFace, contraryId;
        int increment = uPointOrder?1:sizeLstIdexes;
        double centroid[3];
-       
-       for(int facesIdx = 0; facesIdx < 2; facesIdx++){
+       int numProcessFaces = sizeLstIdexes > 1?2:1;
+       for(int facesIdx = 0; facesIdx < numProcessFaces; facesIdx++){
                std::fill(std::begin(centroid), std::end(centroid), 0);
                if(facesIdx == 0)
                {
@@ -160,19 +170,17 @@ void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uP
                                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);
+                               triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2 + (!isClosedCont?2:0));
                                if( normalOrder )
                 {
                                        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);//2
-                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+2,centroidId);//3
-                                               }
-                                               else{                                           
-                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);//1
+                                               triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);//1
+                                               if(index+increment >= end && !isClosedCont){
+                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+2,initial);//2
+                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+3,centroidId);//3
                                                }
                                                triangleIndex+=2;
                                        }
@@ -183,11 +191,10 @@ void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uP
                                        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);
+                                               triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
+                                               if(index-increment <= initial && !isClosedCont){
+                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+2,triangleStripStart);
+                                                       triangleStrip1->GetPointIds()->SetId(triangleIndex+3,centroidId);
                                                }
                                                triangleIndex+=2;
                                        }
@@ -252,8 +259,7 @@ bool CreateMeshFromPoints::CheckLinePointOrder(){
                        secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
                }
        }
-       //cout << firstRadiusSum << endl;
-       //cout << secondRadiusSum << endl;
+
        return firstRadiusSum > secondRadiusSum;
 }
 
@@ -283,11 +289,11 @@ bool CreateMeshFromPoints::CalcValidCentroid(double(&centroid)[3], int start, in
        bool samePoint = true;
        int splineMidPoint = numPoints/2;
        bool collinear = true;
-       
+
        points->GetPoint(start, firstPoint);
        points->GetPoint(splineMidPoint, middlePoint);
        vtkMath::Subtract(middlePoint, firstPoint, vector1);
-       
+
        for(int i = start; i < end; i+=increment){
                points->GetPoint(i, currPoint);
                if(samePoint && i > start && (currPoint[0] != prevPoint[0] || currPoint[1] != prevPoint[1] || currPoint[2] != prevPoint[2])){
@@ -346,6 +352,7 @@ void CreateMeshFromPoints::bbUserSetDefaultValues()
 //    Here we initialize the input 'In' to 0
 //   bbSetInputIn(0);
        bbSetInputCloseSurface(false);
+       isClosedCont = NULL;
        points          = NULL;
        cells           = NULL;
        polydata        = NULL;