]> Creatis software - creaVtk.git/commitdiff
#3501 new box GlyphPolyDataNormals (Pablo)
authorPablo Garzon <garzon@ei-pfe-706.creatis.insa-lyon.fr>
Mon, 20 Mar 2023 14:20:51 +0000 (15:20 +0100)
committerPablo Garzon <garzon@ei-pfe-706.creatis.insa-lyon.fr>
Mon, 20 Mar 2023 14:20:51 +0000 (15:20 +0100)
bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx
bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.h

index 2c17fb7be02d511263fc4fd5f4531dd4d0bc56a7..a6948c45ee344c4fe7d6f3966b8ec98ba0df7242 100644 (file)
@@ -84,16 +84,20 @@ void CreateMeshFromPoints::Process()
                                iGeneral=iGeneral+sizeSegment1;
                                cells->InsertNextCell(triangleStrip);
                        } //for  LstIndexs
+
                        
-                       //false = Open
-                       //true = Close
-                       if(bbGetInputOpenClose()){                      
-                               CloseContourSides(lstIndexs, true);
-                       }
-                       else{
-                               CloseOpenContourSurface(lstIndexs);
+                       if(bbGetInputCloseSurface()){                   
+                               //false = Open Contour
+                               //true = Closed Contour
+                               if(bbGetInputOpenClose()){                      
+                                       CloseContourSides(lstIndexs, true);
+                               }
+                               else{
+                                       CloseOpenContourSurface(lstIndexs);
+                               }
                        }
                        
+                       
 //                     vtkPolyData *polydata = vtkPolyData::New();
                        if (polydata!=NULL) polydata->Delete();
                        polydata = vtkPolyData::New();
@@ -111,16 +115,22 @@ void CreateMeshFromPoints::Process()
                        triangle->Update();
                        bbSetOutputOut( clean->GetOutput() );
                }// if listXYZ size
-               printf("CreateMeshFromPoints::Process: End\n");
+               //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;
+       int firstIndex, end, centroidId, numPointsFace, contraryId;
        int increment = uPointOrder?1:sizeLstIdexes;
-       double centroid[3], currentPoint[3] = {};
+       double centroid[3];
        
        for(int facesIdx = 0; facesIdx < 2; facesIdx++){
                std::fill(std::begin(centroid), std::end(centroid), 0);
@@ -128,85 +138,165 @@ void CreateMeshFromPoints::CloseContourSides(std::vector<int> lstIndexs, bool uP
                        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;
+                       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);
+                       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;
                                        }
-                                       triangleIndex+=2;
+                                       cells->InsertNextCell(triangleStrip1);
                                }
-                               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);
+                               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;
                                        }
-                                       triangleIndex+=2;
+                                       cells->InsertNextCell(triangleStrip1);
                                }
-                               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(&centroid)[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;
+}
+
+/**
+* Checks if points on each side of the shapes represent a curve.
+*/
+bool CreateMeshFromPoints::CheckLinePointOrder(){
+       int sizePoints = bbGetInputLstX().size();
+       std::vector<int> lstIndexs = bbGetInputLstIndexs();
+       double point1[3], point2[3], point3[3];
+       double center[3];
+       double firstRadiusSum = 0;
+       double secondRadiusSum = 0;
+       for(int i = 0; i < lstIndexs[0] && lstIndexs[0] > 3; i+=3){
+               if(i+3 <= lstIndexs[0]){
+                       points->GetPoint(i, point1);
+                       points->GetPoint(i+1, point2);
+                       points->GetPoint(i+2, point3);
+                       firstRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
                }
        }
-
+       
+       for(int i = 0; i < sizePoints && lstIndexs.size() > 3; i+=(lstIndexs[0]*3)){
+               if(i+(3*lstIndexs[0]) <= sizePoints){
+                       points->GetPoint(i, point1);
+                       points->GetPoint(i+lstIndexs[0], point2);
+                       points->GetPoint(i+(2*lstIndexs[0]), point3);
+                       secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center);
+               }
+       }
+       
+       return firstRadiusSum > secondRadiusSum;
 }
 
+/**
+* Closes an open contour
+* lstIndexs: number of points on each spline
+*/
 void CreateMeshFromPoints::CloseOpenContourSurface(std::vector<int> lstIndexs){
        int     sizeLstIdexes = lstIndexs.size();
        int sizeLstX = bbGetInputLstX().size();
-       bool linePointOrder = false;
-
-       if(linePointOrder){
-               CloseContourSides(lstIndexs, false);
-               CloseContourBottom(false);
-       }
-       else{
-               CloseContourSides(lstIndexs, true);
-               CloseContourBottom(true);
-       }
-               
+       bool linePointOrder = CheckLinePointOrder();
+       CloseContourSides(lstIndexs, !linePointOrder);
+       CloseContourBottom(!linePointOrder);
 }
 
-void CreateMeshFromPoints::CalcCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints){
-       double currPoint[3] = {};
+/**
+* Calculates centroid.
+* 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] = {};
+       bool samePoint = true;
        for(int i = start; i < end; i+=increment){
                points->GetPoint(i, currPoint);
+               if(samePoint && (currPoint[0] != prevPoint[0] || currPoint[1] != prevPoint[1] || currPoint[2] != prevPoint[2])){
+                       samePoint = 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;
 }
 
+/**
+* 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();
@@ -234,6 +324,7 @@ void CreateMeshFromPoints::bbUserSetDefaultValues()
 //  SET HERE THE DEFAULT INPUT/OUTPUT VALUES OF YOUR BOX 
 //    Here we initialize the input 'In' to 0
 //   bbSetInputIn(0);
+       bbSetInputCloseSurface(false);
        bbSetInputOpenClose(false);
        points          = NULL;
        cells           = NULL;
index b103d3e3a1c8b79b478f8d4206a606fca524b1e0..f67c312e61cd365e1610672f5e63d8055604a1d9 100644 (file)
@@ -31,6 +31,7 @@ class bbcreaVtk_EXPORT CreateMeshFromPoints
   BBTK_DECLARE_INPUT(LstZ,std::vector<double>);
   BBTK_DECLARE_INPUT(LstIndexs,std::vector<int>);
   BBTK_DECLARE_INPUT(OpenClose, bool);
+  BBTK_DECLARE_INPUT(CloseSurface, bool);
   BBTK_DECLARE_OUTPUT(Out,vtkPolyData*);
   BBTK_PROCESS(Process);
   void Process();
@@ -41,7 +42,9 @@ class bbcreaVtk_EXPORT CreateMeshFromPoints
        vtkCleanPolyData        *clean;
        vtkTriangleFilter       *triangle;
        
-       void CalcCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints);
+       bool CalcValidCentroid(double(&centroid)[3], int start, int end, int increment, int numPoints);
+       bool CheckLinePointOrder();
+       bool isPointingCorrectly( int firstPointId, int secPointId, double(&centroid)[3], int contrPointId);
        void CloseContourBottom(bool uPointOrder);
        void CloseContourSides(std::vector<int> lstIndexs, bool uPointOrder);
        void CloseOpenContourSurface(std::vector<int> lstIndexs);
@@ -62,6 +65,7 @@ BBTK_BEGIN_DESCRIBE_BLACK_BOX(CreateMeshFromPoints,bbtk::AtomicBlackBox);
   BBTK_INPUT(CreateMeshFromPoints,LstZ,"List Z point",std::vector<double>,"");
   BBTK_INPUT(CreateMeshFromPoints,LstIndexs,"Number of points by segment",std::vector<int>,"");
   BBTK_INPUT(CreateMeshFromPoints,OpenClose,"(default false) Type of Contour: false=Open, true=Close",bool,"");
+  BBTK_INPUT(CreateMeshFromPoints,CloseSurface,"(default false) Add the caps to close the surface",bool,"");
 
   BBTK_OUTPUT(CreateMeshFromPoints,Out,"vtkPolyData",vtkPolyData*,"");