]> Creatis software - creaVtk.git/blobdiff - bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
#3507 Undo and Redo Meshes
[creaVtk.git] / bbtk_creaVtk_PKG / src / bbcreaVtkCreateMeshFromPoints.cxx
index 2c17fb7be02d511263fc4fd5f4531dd4d0bc56a7..abee3e687157ae877e78500b7fd828b79558fd1b 100644 (file)
@@ -5,6 +5,7 @@
 #include "bbcreaVtkPackage.h"
 
 #include "vtkTriangleStrip.h"
+#include "vtkTriangle.h"
 #include <vtkMath.h>
 
 namespace bbcreaVtk
@@ -37,9 +38,10 @@ 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()) )
+               double pointsCentroid[3];
+               if ( (lstIndexs.size()<1) || (lstX.size()==0) || (lstX.size()!=lstY.size()) || (lstY.size()!=lstZ.size()) )
                {
-                       printf("PG CreateMeshFromPoints::Process: List of points X Y Z  and LstIndexes is not correct\n");
+                       printf("Warning! CreateMeshFromPoints::Process: List of points X Y Z  and LstIndexes is not correct\n");
                        bbSetOutputOut(NULL);
                } else  {
                        int ii,sizeSegment1,sizeSegment2;
@@ -51,7 +53,65 @@ void CreateMeshFromPoints::Process()
                        for (i=0;i<sizeLstX;i++)
                        {
                                points->InsertNextPoint(lstX[i],lstY[i],lstZ[i]);
+                               pointsCentroid[0] += lstX[i];
+                               pointsCentroid[1] += lstY[i];
+                               pointsCentroid[2] += lstZ[i];
                        } // for i
+                       pointsCentroid[0] /= sizeLstX;
+                       pointsCentroid[1] /= sizeLstX;
+                       pointsCentroid[2] /= sizeLstX;
+                       
+                       if ((bbGetInputCloseSurface()==true)  && (lstIndexs.size()>=2) )
+            {  
+               //check if initial and end points are the same in all splines (splines are in line shape and not U shape)
+               //to not apply changes because clean polydata will take care of it.
+                               int count = 0;
+                               bool differentPoints = false;
+                               for(i=0; i < lstIndexs.size() && !differentPoints;i++){
+                                       bool sameStart = lstX[count] != lstX[count+lstIndexs[i]-1] && lstY[count] != lstY[count+lstIndexs[i]-1] && lstZ[count] != lstZ[count+lstIndexs[i]-1];
+                                       bool sameEnd = lstX[count] != lstX[count+lstIndexs[i]-1] && lstY[count] != lstY[count+lstIndexs[i]-1] && lstZ[count] != lstZ[count+lstIndexs[i]-1];
+                                       if(!(sameStart && sameEnd)) differentPoints = true;
+                                       count += lstIndexs[i];
+                               }
+                //Correct surface normals if needed
+                if(differentPoints)
+                       {
+                           double pointSurf1[3], pointSurf2[3], pointSurf3[3];
+                           double vect1[3], vect2[3];
+                           double surfNormal[3], vectorCenter[3];
+                           double dotNormalSurf = 0;
+                           for(int pIndex = 0; pIndex < lstIndexs[0]-1; pIndex++)
+                           {
+                                   pointSurf1[0] = lstX[pIndex];
+                                   pointSurf1[1] = lstY[pIndex];
+                                   pointSurf1[2] = lstZ[pIndex];
+                                   vtkMath::Subtract(pointsCentroid, pointSurf1, vectorCenter);
+                                   pointSurf2[0] = lstX[pIndex+lstIndexs[1]];
+                                   pointSurf2[1] = lstY[pIndex+lstIndexs[1]];
+                                   pointSurf2[2] = lstZ[pIndex+lstIndexs[1]];
+                                   pointSurf3[0] = lstX[pIndex+1];
+                                   pointSurf3[1] = lstY[pIndex+1];
+                                   pointSurf3[2] = lstZ[pIndex+1];
+                                   vtkMath::Subtract(pointSurf2, pointSurf1, vect1);
+                                   vtkMath::Subtract(pointSurf3, pointSurf1, vect2);
+                                   vtkMath::Cross(vect1, vect2, surfNormal);
+                                   dotNormalSurf += vtkMath::Dot(surfNormal, vectorCenter);
+                           } // for pIndex
+
+                                       if(dotNormalSurf > 0){
+                                               points->Delete();
+                                               points = vtkPoints::New();
+                                               for(int splineI = 0; splineI < lstIndexs.size(); splineI++){
+                                                       for (i=lstIndexs[splineI]-1; i >= 0;i--)
+                                                       {
+                                                               points->InsertNextPoint(lstX[splineI*lstIndexs[0]+i],lstY[splineI*lstIndexs[0]+i],lstZ[splineI*lstIndexs[0]+i]);
+                                                       } // for i
+                                               } // for splineI
+                                       } // if dotNormalSurf
+                               }// if differentPoints
+                       }
+                       //
+                       
 //                     vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
                        if (cells!=NULL) cells->Delete();
                        cells = vtkCellArray::New();
@@ -78,22 +138,42 @@ 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
                        
-                       //false = Open
-                       //true = Close
-                       if(bbGetInputOpenClose()){                      
-                               CloseContourSides(lstIndexs, true);
-                       }
-                       else{
-                               CloseOpenContourSurface(lstIndexs);
-                       }
                        
+                       if(bbGetInputCloseSurface())
+                       {
+                               int lastId1 = lstIndexs[0]-1;
+                               int lastId2 = sizeLstX - 1;
+                               int firstId2 = sizeLstX - lstIndexs[sizeLstIdexes - 1];
+                               bool face1open = std::fabs(lstX[0] - lstX[lastId1]) > 0.0001 && std::fabs(lstY[0] - lstY[lastId1]) > 0.0001 && std::fabs(lstZ[0] - lstZ[lastId1]) > 0.0001;
+                               bool face2open = std::fabs(lstX[firstId2] - lstX[lastId2]) > 0.0001 && std::fabs(lstY[firstId2] - lstY[lastId2]) > 0.0001 && std::fabs(lstZ[firstId2] - lstZ[lastId2]) > 0.0001;
+                               
+                               bool altFace1open = std::fabs(lstX[0] - lstX[firstId2]) > 0.0001 && std::fabs(lstY[0] - lstY[firstId2]) > 0.0001 && std::fabs(lstZ[0] - lstZ[firstId2]) > 0.0001;
+                               bool altFace2open = std::fabs(lstX[lastId1] - lstX[lastId2]) > 0.0001 && std::fabs(lstY[lastId1] - lstY[lastId2]) > 0.0001 && std::fabs(lstZ[lastId1] - lstZ[lastId2]) > 0.0001;
+
+                               //false = Open Contour
+                               //true = Closed Contour
+                               if(!face1open && !face2open)
+                               {
+//                                     isClosedCont = true;
+                                       CloseContourSides(lstIndexs, true, true);
+                               }
+                               else if(!altFace1open && !altFace2open)
+                               {
+//                                     isClosedCont = true;
+                                       CloseContourSides(lstIndexs, false, true);
+                               }
+                               else{
+                                       CloseOpenContourSurface(lstIndexs);
+                               }
+                       }
+                    
 //                     vtkPolyData *polydata = vtkPolyData::New();
                        if (polydata!=NULL) polydata->Delete();
                        polydata = vtkPolyData::New();
@@ -109,104 +189,225 @@ void CreateMeshFromPoints::Process()
                        triangle = vtkTriangleFilter::New();
                        triangle->SetInputData( clean->GetOutput() );
                        triangle->Update();
-                       bbSetOutputOut( clean->GetOutput() );
+            bbSetOutputOut( triangle->GetOutput() );
+            //            bbSetOutputOut( clean->GetOutput() );
                }// if listXYZ size
-               printf("CreateMeshFromPoints::Process: End\n");
+               //printf("PG CreateMeshFromPoints::Process: End\n");
 }
-
-void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder){
+/**
+* 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, bool isClosedCont){
        int     sizeLstIdexes = lstIndexs.size();
        int sizePoints = bbGetInputLstX().size();
 
-       int firstIndex, end, centroidId, numPointsFace;
+       int firstIndex, end, centroidId, numPointsFace, contraryId;
        int increment = uPointOrder?1:sizeLstIdexes;
-       double centroid[3], currentPoint[3] = {};
-       
-       for(int facesIdx = 0; facesIdx < 2; facesIdx++){
+       double centroid[3];
+       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){
+               if(facesIdx == 0)
+               {
                        firstIndex = 0;
                        numPointsFace = uPointOrder?lstIndexs[0]: sizeLstIdexes;
                        end = uPointOrder?firstIndex + numPointsFace:sizePoints - lstIndexs[sizeLstIdexes - 1] + 1;
-               }
-               else{
-                       firstIndex = uPointOrder?sizePoints - lstIndexs[sizeLstIdexes-1]:lstIndexs[0] - 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] = {};
-                       CalcCentroid(centroid, firstIndex, end, increment, numPointsFace);
-                       centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]);
-                       vtkSmartPointer<vtkTriangleStrip> triangleStrip1 = vtkSmartPointer<vtkTriangleStrip>::New();
-                       triangleStrip1->GetPointIds()->SetNumberOfIds(numPointsFace*2+1);
-                       if(facesIdx == 0){                              
-                               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);
+               if(numPointsFace > 1)
+               {
+                       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> triangleStrip = vtkSmartPointer<vtkTriangleStrip>::New();
+                               //triangleStrip->GetPointIds()->SetNumberOfIds(numPointsFace*2 + (!isClosedCont?2:0));
+                               //int triangleIndex = 0;
+                               if( normalOrder )
+                {
+                       int initial = firstIndex;
+                       for(int index = initial; index < end; index+=increment){
+                                               if(index+increment >= end && !isClosedCont){
+                                                       vtkNew<vtkTriangle> triangle;
+                                                       triangle->GetPointIds()->SetId(0, index);
+                                                       triangle->GetPointIds()->SetId(1, initial);
+                                                       triangle->GetPointIds()->SetId(2, centroidId);
+                                                       cells->InsertNextCell(triangle);
+                                               }else if(index+increment < end){
+                                                       vtkNew<vtkTriangle> triangle;
+                                                       triangle->GetPointIds()->SetId(0, index);
+                                                       triangle->GetPointIds()->SetId(1, index+increment);
+                                                       triangle->GetPointIds()->SetId(2, centroidId);
+                                                       cells->InsertNextCell(triangle);
+                                               }
+                                               /*
+                                               triangleStrip->GetPointIds()->SetId(triangleIndex,index);
+                                               triangleStrip->GetPointIds()->SetId(triangleIndex+1,centroidId);//1
+                                               if(index+increment >= end && !isClosedCont){
+                                                       triangleStrip->GetPointIds()->SetId(triangleIndex+2,initial);//2
+                                                       triangleStrip->GetPointIds()->SetId(triangleIndex+3,centroidId);//3
+                                               }
+                                               triangleIndex+=2;
+                                               */
                                        }
-                                       else{
-                                               triangleStrip1->GetPointIds()->SetId(triangleIndex+1,centroidId);
+                                       //cells->InsertNextCell(triangleStrip);
+                               } else {
+                                       int initial = firstIndex-1;
+                                       int triangleStripStart = end-1;
+                                       for(int index = triangleStripStart; index > initial; index-=increment){ 
+                                               if(index-increment <= initial && !isClosedCont){
+                                                       vtkNew<vtkTriangle> triangle;
+                                                       triangle->GetPointIds()->SetId(0, index);
+                                                       triangle->GetPointIds()->SetId(1, triangleStripStart);
+                                                       triangle->GetPointIds()->SetId(2, centroidId);
+                                                       cells->InsertNextCell(triangle);
+                                               }else if(index-increment > initial){
+                                                       vtkNew<vtkTriangle> triangle;
+                                                       triangle->GetPointIds()->SetId(0, index);
+                                                       triangle->GetPointIds()->SetId(1, index-increment);
+                                                       triangle->GetPointIds()->SetId(2, centroidId);
+                                                       cells->InsertNextCell(triangle);
+                                               }
                                        }
-                                       triangleIndex+=2;
-                               }
-                               cells->InsertNextCell(triangleStrip1);
-                       }
-               }
-       }
+                               }//if normalOrder
+                       }//if validCentroid
+               }//if numPointsFace
+       }//for facesIdx
 
 }
 
-void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
-       int     sizeLstIdexes = lstIndexs.size();
-       int sizeLstX = bbGetInputLstX().size();
-       bool linePointOrder = false;
+/**
+* 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(&centroid)[3], int contrPointId) {
 
-       if(linePointOrder){
-               CloseContourSides(lstIndexs, false);
-               CloseContourBottom(false);
+       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;
+}
+
+/**
+* Checks if the order of the points represent a curved spline (U shape) or the points resemble a straight spline.
+* Now it checks the angle between each point and the vector that goes from the last point to the first.
+*
+* Previous version checked the curvature between 3 points in the spline, but this created problems when the straight lines
+* had curves in the middle, increasing the curvature although they are not in the U shape.
+*/
+bool CreateMeshFromPoints::CheckLinePointOrder(){
+       int sizePoints = bbGetInputLstX().size();
+       std::vector<int> lstIndexs = bbGetInputLstIndexs();
+       double point1[3], point2[3], point3[3];
+       double center[3];
+       double firstAngleSum = 0;
+       double secondAngleSum = 0;
+
+       points->GetPoint(0, point1);
+       points->GetPoint((lstIndexs[0]-1), point3);
+       double firstVect[3];
+       double secVect[3];
+       vtkMath::Subtract(point3, point1, firstVect);
+       for(int i = 0; i < lstIndexs[0]; i++){
+               points->GetPoint(i, point2);
+               vtkMath::Subtract(point2, point1, secVect);
+               firstAngleSum += vtkMath::SignedAngleBetweenVectors(firstVect, secVect, firstVect);
        }
-       else{
-               CloseContourSides(lstIndexs, true);
-               CloseContourBottom(true);
+       points->GetPoint((sizePoints-lstIndexs[0]), point3);
+       vtkMath::Subtract(point3, point1, firstVect);
+       for(int i = 0; i < sizePoints; i+=lstIndexs.size()){
+               points->GetPoint(i, point2);
+               vtkMath::Subtract(point2, point1, secVect);
+               secondAngleSum += vtkMath::SignedAngleBetweenVectors(firstVect, secVect, firstVect);
        }
-               
+
+       return firstAngleSum < secondAngleSum;
 }
 
-void CreateMeshFromPoints::CalcCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints){
-       double currPoint[3] = {};
+/**
+* Closes an open contour
+* lstIndexs: number of points on each spline
+*/
+void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
+       bool linePointOrder = CheckLinePointOrder();
+       CloseContourSides(lstIndexs, !linePointOrder, false);
+       CloseContourBottom(!linePointOrder);
+}
+
+/**
+* Calculates centroid and checks if points are collinear.
+* centroid: array to store calculation
+* start: start index of points to use
+* end: end index of points to use
+* increment: increment to be used in point iteration
+* numPoints: number of points used to calculate the centroid.
+* Returns a bool indicating the validity of the centroid calculated.
+* False = invalid centroid = all points are the same.
+*/
+bool CreateMeshFromPoints::CalcValidCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints){
+       double currPoint[3] = {}, prevPoint[3] = {}, middlePoint[3] = {}, firstPoint[3] = {};
+       double vector1[3], vector2[3];
+       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])){
+                       samePoint = false;
+               }
+
+               vtkMath::Subtract(currPoint, firstPoint, vector2);
+               double angle = vtkMath::AngleBetweenVectors(vector1, vector2);
+               if(angle > 0.0001 && collinear){
+                       collinear = false;
+               }
+               
                centroid[0] += currPoint[0];
                centroid[1] += currPoint[1];
                centroid[2] += currPoint[2];
+               std::copy(std::begin(currPoint), std::end(currPoint), prevPoint);
        }
+       
        centroid[0] /= numPoints;
        centroid[1] /= numPoints;
        centroid[2] /= numPoints;
+       
+       return !samePoint && !collinear;
 }
 
+/**
+* Closes the bottom of the given countour.
+* Should only be used when its an open contour.
+* uPointOrder: points are ordered in U shape
+*/
 void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
        std::vector<int> lstIndexs = bbGetInputLstIndexs();
        int sizeLstIdexes = lstIndexs.size();
@@ -214,13 +415,27 @@ void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){
        
        vtkSmartPointer<vtkTriangleStrip> triangleStripBottom = vtkSmartPointer<vtkTriangleStrip>::New();
        triangleStripBottom->GetPointIds()->SetNumberOfIds(sizeLstIdexes*2);
+       
+       double originPoint[3];
+       points->GetPoint(0, originPoint);
+       int middleMeshPoint = uPointOrder?lstIndexs[0]/2:lstIndexs[0]*sizeLstIdexes/2;
+       
+       bool normalOrder = isPointingCorrectly(uPointOrder?lstIndexs[0]-1:sizeLstX-lstIndexs[0], uPointOrder?lstIndexs[0]:1, originPoint, middleMeshPoint);
+       
        int triangleIndex = 0, currentId = 0, nextId = 0;
        for(int splineIndex = 0; splineIndex < sizeLstIdexes;splineIndex++){
-               triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
                nextId = uPointOrder?currentId + lstIndexs[splineIndex] - 1:sizeLstX - sizeLstIdexes + splineIndex;
-               triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
-               triangleIndex+=2;
+               if(normalOrder)
+               {
+                       triangleStripBottom->GetPointIds()->SetId(triangleIndex, currentId);
+                       triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, nextId);
+               }
+               else{
+                       triangleStripBottom->GetPointIds()->SetId(triangleIndex, nextId);
+                       triangleStripBottom->GetPointIds()->SetId(triangleIndex+1, currentId);
+               }
                currentId = uPointOrder?nextId + 1: splineIndex+1;
+               triangleIndex+=2;
        }
        cells->InsertNextCell(triangleStripBottom);
 }
@@ -234,7 +449,7 @@ void CreateMeshFromPoints::bbUserSetDefaultValues()
 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
 //    Here we initialize the input 'In' to 0
 //   bbSetInputIn(0);
-       bbSetInputOpenClose(false);
+       bbSetInputCloseSurface(false);
        points          = NULL;
        cells           = NULL;
        polydata        = NULL;