X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=bbtk_creaVtk_PKG%2Fsrc%2FbbcreaVtkCreateMeshFromPoints.cxx;h=b804dff795705d81e803d1bcdd80e364db1799e4;hb=b00e934a843a756fb0d6d5f763642816df0aaff4;hp=79b999135824b68eafc2a10a76d7e737a2ccad67;hpb=44e81956772de9b82c9c670ce043a557f9307c3d;p=creaVtk.git diff --git a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx index 79b9991..b804dff 100644 --- a/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx +++ b/bbtk_creaVtk_PKG/src/bbcreaVtkCreateMeshFromPoints.cxx @@ -38,6 +38,7 @@ void CreateMeshFromPoints::Process() std::vector lstY = bbGetInputLstY(); std::vector lstZ = bbGetInputLstZ(); std::vector lstIndexs = bbGetInputLstIndexs(); + double pointsCentroid[3]; 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"); @@ -52,7 +53,52 @@ void CreateMeshFromPoints::Process() for (i=0;iInsertNextPoint(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()) + { + //Correct surface normals if needed + 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); + } + 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]); + } + } + } + } + // + // vtkSmartPointer cells = vtkSmartPointer::New(); if (cells!=NULL) cells->Delete(); cells = vtkCellArray::New(); @@ -85,7 +131,8 @@ void CreateMeshFromPoints::Process() iGeneral=iGeneral+sizeSegment1; cells->InsertNextCell(triangleStrip); } //for LstIndexs - + + if(bbGetInputCloseSurface()) { int lastId1 = lstIndexs[0]-1; @@ -169,6 +216,9 @@ void CreateMeshFromPoints::CloseContourSides(std::vector lstIndexs, bool uP { bool normalOrder = isPointingCorrectly(firstIndex, firstIndex+increment, centroid, contraryId); centroidId = points->InsertNextPoint(centroid[0], centroid[1], centroid[2]); + //vtkSmartPointer triangleStrip = vtkSmartPointer::New(); + //triangleStrip->GetPointIds()->SetNumberOfIds(numPointsFace*2 + (!isClosedCont?2:0)); + //int triangleIndex = 0; if( normalOrder ) { int initial = firstIndex; @@ -186,11 +236,21 @@ void CreateMeshFromPoints::CloseContourSides(std::vector lstIndexs, bool uP 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; + */ } + //cells->InsertNextCell(triangleStrip); } else { int initial = firstIndex-1; int triangleStripStart = end-1; - for(int index = triangleStripStart; index > initial; index-=increment){ + for(int index = triangleStripStart; index > initial; index-=increment){ if(index-increment <= initial && !isClosedCont){ vtkNew triangle; triangle->GetPointIds()->SetId(0, index); @@ -205,7 +265,7 @@ void CreateMeshFromPoints::CloseContourSides(std::vector lstIndexs, bool uP cells->InsertNextCell(triangle); } } - } + }//if normalOrder }//if validCentroid }//if numPointsFace }//for facesIdx @@ -239,34 +299,39 @@ bool CreateMeshFromPoints::isPointingCorrectly( int firstPointId, int secPointId } /** -* Checks if points on each side of the shapes represent a curve. +* 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 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] > 9; i+=5){ - if(i+10 < lstIndexs[0]){ - points->GetPoint(i, point1); - points->GetPoint(i+5, point2); - points->GetPoint(i+10, point3); - firstRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center); - } + 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); } - - for(int i = 0; i < sizePoints && lstIndexs.size() > 9; i+=(lstIndexs.size()*5)){ - if(i+(10*lstIndexs.size()) < sizePoints){ - points->GetPoint(i, point1); - points->GetPoint(i+(5*lstIndexs.size()), point2); - points->GetPoint(i+(10*lstIndexs.size()), point3); - secondRadiusSum += vtkMath::Solve3PointCircle(point1, point2, point3, center); - } + 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 firstRadiusSum > secondRadiusSum; + return firstAngleSum < secondAngleSum; } /** @@ -337,13 +402,27 @@ void CreateMeshFromPoints::CloseContourBottom(bool uPointOrder){ vtkSmartPointer triangleStripBottom = vtkSmartPointer::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); }