From 1e22762b5656c8325e9256ea41cbe522c86480a0 Mon Sep 17 00:00:00 2001 From: apo Date: Wed, 12 Sep 2007 14:36:42 +0000 Subject: [PATCH] Fix for Bug NPAL16814 SIGSEGV on animation when field name is long (final version) --- src/VTKViewer/VTKViewer_ConvexTool.cxx | 358 +++++++++++-------------- src/VTKViewer/VTKViewer_ConvexTool.h | 103 ++----- 2 files changed, 193 insertions(+), 268 deletions(-) diff --git a/src/VTKViewer/VTKViewer_ConvexTool.cxx b/src/VTKViewer/VTKViewer_ConvexTool.cxx index 1e1f4b09b..04c68d127 100644 --- a/src/VTKViewer/VTKViewer_ConvexTool.cxx +++ b/src/VTKViewer/VTKViewer_ConvexTool.cxx @@ -80,31 +80,115 @@ namespace typedef std::vector TPolygons; } -/*! - Constructor -*/ + +//---------------------------------------------------------------------------- VTKViewer_Triangulator ::VTKViewer_Triangulator(): - myInput(NULL), - myCellId(-1), - myShowInside(-1), - myAllVisible(-1), - myCellsVisibility(NULL), - myCellIds(vtkIdList::New()) + myCellIds(vtkIdList::New()), + myFaceIds(vtkIdList::New()), + myPoints(vtkPoints::New()), + myPointIds(NULL) {} -/*! - Destructor -*/ +//---------------------------------------------------------------------------- VTKViewer_Triangulator ::~VTKViewer_Triangulator() { myCellIds->Delete(); + myFaceIds->Delete(); + myPoints->Delete(); +} + + +//---------------------------------------------------------------------------- +vtkPoints* +VTKViewer_Triangulator +::InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId) +{ + myPoints->Reset(); + vtkIdType aNumPts; + theInput->GetCellPoints(theCellId, aNumPts, myPointIds); + if ( aNumPts > 0 ) { + vtkFloatingPointType anAbsoluteCoord[3]; + myPoints->SetNumberOfPoints(aNumPts); + vtkPoints *anInputPoints = theInput->GetPoints(); + for (int aPntId = 0; aPntId < aNumPts; aPntId++) { + anInputPoints->GetPoint(myPointIds[aPntId], anAbsoluteCoord); + myPoints->SetPoint(aPntId, anAbsoluteCoord); + } + } + + return myPoints; +} + + +//---------------------------------------------------------------------------- +vtkIdType +VTKViewer_Triangulator +::GetNbOfPoints() +{ + return myPoints->GetNumberOfPoints(); +} + + +//---------------------------------------------------------------------------- +vtkIdType +VTKViewer_Triangulator +::GetPointId(vtkIdType thePointId) +{ + return thePointId; +} + + +//---------------------------------------------------------------------------- +vtkFloatingPointType +VTKViewer_Triangulator +::GetCellLength() +{ + vtkFloatingPointType aBounds[6]; + myPoints->GetBounds(aBounds); + + vtkFloatingPointType aCoordDiff[3]; + aCoordDiff[0] = (aBounds[1] - aBounds[0]); + aCoordDiff[1] = (aBounds[3] - aBounds[2]); + aCoordDiff[2] = (aBounds[5] - aBounds[4]); + + return sqrt(aCoordDiff[0]*aCoordDiff[0] + + aCoordDiff[1]*aCoordDiff[1] + + aCoordDiff[2]*aCoordDiff[2]); +} + + +//---------------------------------------------------------------------------- +void +VTKViewer_Triangulator +::GetCellNeighbors(vtkUnstructuredGrid *theInput, + vtkIdType theCellId, + vtkCell* theFace, + vtkIdList* theCellIds) +{ + myFaceIds->Reset(); + vtkIdList *anIdList = theFace->PointIds; + myFaceIds->InsertNextId(myPointIds[anIdList->GetId(0)]); + myFaceIds->InsertNextId(myPointIds[anIdList->GetId(1)]); + myFaceIds->InsertNextId(myPointIds[anIdList->GetId(2)]); + + theInput->GetCellNeighbors(theCellId, myFaceIds, theCellIds); } +//---------------------------------------------------------------------------- +vtkIdType +VTKViewer_Triangulator +::GetConnectivity(vtkIdType thePntId) +{ + return myPointIds[thePntId]; +} + +//---------------------------------------------------------------------------- bool VTKViewer_Triangulator ::Execute(vtkUnstructuredGrid *theInput, @@ -119,13 +203,7 @@ VTKViewer_Triangulator std::vector& theVTK2ObjIds, bool theIsCheckConvex) { - myInput = theInput; - myCellId = theCellId; - myShowInside = theShowInside; - myAllVisible = theAllVisible; - myCellsVisibility = theCellsVisibility; - - vtkPoints *aPoints = InitPoints(); + vtkPoints *aPoints = InitPoints(theInput, theCellId); vtkIdType aNumPts = GetNbOfPoints(); if(DEBUG_TRIA_EXECUTE) cout<<"Triangulator - aNumPts = "<GetId(0)]) || - myCellIds->GetNumberOfIds() <= 0 || myShowInside) + GetCellNeighbors(theInput, theCellId, aFace, myCellIds); + if((!theAllVisible && !theCellsVisibility[myCellIds->GetId(0)]) || + myCellIds->GetNumberOfIds() <= 0 || theShowInside) { TPointIds aPointIds; vtkIdList *anIdList = aFace->PointIds; @@ -511,231 +589,144 @@ VTKViewer_Triangulator return true; } -/*! - Constructor -*/ + +//---------------------------------------------------------------------------- VTKViewer_OrderedTriangulator ::VTKViewer_OrderedTriangulator(): - myCell(vtkGenericCell::New()), + myTriangulator(vtkOrderedTriangulator::New()), myBoundaryTris(vtkCellArray::New()), - myTriangle(vtkTriangle::New()), - myTriangulator(vtkOrderedTriangulator::New()) + myTriangle(vtkTriangle::New()) { - myBoundaryTris->Allocate(100); + myBoundaryTris->Allocate(VTK_CELL_SIZE); myTriangulator->PreSortedOff(); - //myTriangulator->UseTemplatesOn(); } -/*! - Destructor -*/ + +//---------------------------------------------------------------------------- VTKViewer_OrderedTriangulator ::~VTKViewer_OrderedTriangulator() { - myCell->Delete(); - myBoundaryTris->Delete(); myTriangle->Delete(); + myBoundaryTris->Delete(); myTriangulator->Delete(); } + +//---------------------------------------------------------------------------- vtkPoints* VTKViewer_OrderedTriangulator -::InitPoints() +::InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId) { - myInput->GetCell(myCellId,myCell); + myBoundaryTris->Reset(); - int numPts = myCell->GetNumberOfPoints(); - if ( numPts > 0 ) - { - myTriangulator->InitTriangulation(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, numPts); + vtkPoints* aPoints = VTKViewer_Triangulator::InitPoints(theInput, theCellId); + vtkIdType aNumPts = myPoints->GetNumberOfPoints(); + if ( aNumPts > 0 ) { + myTriangulator->InitTriangulation(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, aNumPts); - vtkFloatingPointType x[3], p[3]; - vtkIdType ptId; - vtkFloatingPointType *bounds = myCell->GetBounds(); + vtkFloatingPointType aBounds[6]; + myPoints->GetBounds(aBounds); - // Inject cell points into triangulation - for (int i=0; iPoints->GetPoint(i, x); - for (int j=0; j<3; j++) - p[j] = (x[j] - bounds[2*j]) / (bounds[2*j+1] - bounds[2*j]); - ptId = myCell->PointIds->GetId(i); - // myTriangulator->InsertPoint(ptId, x, x, 0); - myTriangulator->InsertPoint(ptId, x, p, 0); - }//for all points - - -// if ( myCell->IsPrimaryCell() ) //use templates if topology is fixed -// { -// int numEdges=myCell->GetNumberOfEdges(); -// myTriangulator->TemplateTriangulate(myCell->GetCellType(), -// numPts,numEdges); -// } -// else //use ordered triangulator - { - myTriangulator->Triangulate(); + vtkFloatingPointType anAbsoluteCoord[3]; + vtkFloatingPointType aParamentrucCoord[3]; + for (int aPntId = 0; aPntId < aNumPts; aPntId++) { + myPoints->GetPoint(aPntId, anAbsoluteCoord); + aParamentrucCoord[0] = (anAbsoluteCoord[0] - aBounds[0]) / (aBounds[1] - aBounds[0]); + aParamentrucCoord[1] = (anAbsoluteCoord[1] - aBounds[2]) / (aBounds[3] - aBounds[2]); + aParamentrucCoord[2] = (anAbsoluteCoord[2] - aBounds[4]) / (aBounds[5] - aBounds[4]); + myTriangulator->InsertPoint(aPntId, anAbsoluteCoord, aParamentrucCoord, 0); } + + myTriangulator->Triangulate(); + myTriangulator->AddTriangles(myBoundaryTris); } - return myInput->GetPoints(); -} -vtkIdType -VTKViewer_OrderedTriangulator -::GetNbOfPoints() -{ - return myCell->GetNumberOfPoints(); + return aPoints; } -vtkIdType -VTKViewer_OrderedTriangulator -::GetPointId(vtkIdType thePointId) -{ - return myCell->GetPointId(thePointId); -} - -vtkFloatingPointType -VTKViewer_OrderedTriangulator -::GetCellLength() -{ - return sqrt(myCell->GetLength2()); -} +//---------------------------------------------------------------------------- vtkIdType VTKViewer_OrderedTriangulator ::GetNumFaces() { - myBoundaryTris->Reset(); - myTriangulator->AddTriangles(myBoundaryTris); return myBoundaryTris->GetNumberOfCells(); - //return myCell->GetNumberOfFaces(); } + +//---------------------------------------------------------------------------- vtkCell* VTKViewer_OrderedTriangulator ::GetFace(vtkIdType theFaceId) { - int numCells = myBoundaryTris->GetNumberOfCells(); - if ( theFaceId < 0 || theFaceId >=numCells ) {return NULL;} + vtkIdType aNumCells = myBoundaryTris->GetNumberOfCells(); + if ( theFaceId < 0 || theFaceId >= aNumCells ) + return NULL; - vtkIdType *cells = myBoundaryTris->GetPointer(); + vtkIdType *aCells = myBoundaryTris->GetPointer(); // Each triangle has three points plus number of points - vtkIdType *cptr = cells + 4*theFaceId; - for (int i=0; i<3; i++) - { - myTriangle->PointIds->SetId(i,cptr[i+1]); - myTriangle->Points->SetPoint(i,myInput->GetPoints()->GetPoint(cptr[i+1])); - } + vtkIdType *aCellPtr = aCells + 4*theFaceId; + + myTriangle->PointIds->SetId(0, aCellPtr[1]); + myTriangle->Points->SetPoint(0, myPoints->GetPoint(aCellPtr[1])); - return myTriangle; - //return myCell->GetFace(theFaceId); -} + myTriangle->PointIds->SetId(1, aCellPtr[2]); + myTriangle->Points->SetPoint(1, myPoints->GetPoint(aCellPtr[2])); -void -VTKViewer_OrderedTriangulator -::GetCellNeighbors(vtkIdType theCellId, - vtkCell* theFace, - vtkIdList* theCellIds) -{ - vtkIdList *anIdList = theFace->PointIds; - myInput->GetCellNeighbors(theCellId, anIdList, theCellIds); -} + myTriangle->PointIds->SetId(2, aCellPtr[3]); + myTriangle->Points->SetPoint(2, myPoints->GetPoint(aCellPtr[3])); -vtkIdType -VTKViewer_OrderedTriangulator -::GetConnectivity(vtkIdType thePntId) -{ - return thePntId; + return myTriangle; } -/*! - Constructor -*/ + +//---------------------------------------------------------------------------- VTKViewer_DelaunayTriangulator ::VTKViewer_DelaunayTriangulator(): myUnstructuredGrid(vtkUnstructuredGrid::New()), myGeometryFilter(vtkGeometryFilter::New()), myDelaunay3D(vtkDelaunay3D::New()), - myFaceIds(vtkIdList::New()), - myPoints(vtkPoints::New()), - myPolyData(NULL), - myPointIds(NULL) + myPolyData(NULL) { + myUnstructuredGrid->Initialize(); + myUnstructuredGrid->Allocate(); + myUnstructuredGrid->SetPoints(myPoints); + myDelaunay3D->SetInput(myUnstructuredGrid); myGeometryFilter->SetInput(myDelaunay3D->GetOutput()); + myPolyData = myGeometryFilter->GetOutput(); } - -/*! - Destructor -*/ +//---------------------------------------------------------------------------- VTKViewer_DelaunayTriangulator ::~VTKViewer_DelaunayTriangulator() { myUnstructuredGrid->Delete(); myGeometryFilter->Delete(); myDelaunay3D->Delete(); - myFaceIds->Delete(); - myPoints->Delete(); } +//---------------------------------------------------------------------------- vtkPoints* VTKViewer_DelaunayTriangulator -::InitPoints() +::InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId) { - myPoints->Reset(); - myUnstructuredGrid->Initialize(); - myUnstructuredGrid->Allocate(); - myUnstructuredGrid->SetPoints(myPoints); - - vtkIdType aNumPts; - myInput->GetCellPoints(myCellId,aNumPts,myPointIds); - - if ( aNumPts < myPoints->GetNumberOfPoints() ) - myPoints->Reset(); - - { - vtkFloatingPointType aPntCoord[3]; - myPoints->SetNumberOfPoints(aNumPts); - vtkPoints *anInputPoints = myInput->GetPoints(); - for (int aPntId = 0; aPntId < aNumPts; aPntId++) { - anInputPoints->GetPoint(myPointIds[aPntId],aPntCoord); - myPoints->SetPoint(aPntId,aPntCoord); - } - } + vtkPoints* aPoints = VTKViewer_Triangulator::InitPoints(theInput, theCellId); myPoints->Modified(); myUnstructuredGrid->Modified(); - myGeometryFilter->Update(); - myPolyData = myGeometryFilter->GetOutput(); - return myPoints; -} - -vtkIdType -VTKViewer_DelaunayTriangulator -::GetNbOfPoints() -{ - return myPoints->GetNumberOfPoints(); + return aPoints; } -vtkIdType -VTKViewer_DelaunayTriangulator -::GetPointId(vtkIdType thePointId) -{ - return thePointId; -} - -vtkFloatingPointType -VTKViewer_DelaunayTriangulator -::GetCellLength() -{ - return myPolyData->GetLength(); -} +//---------------------------------------------------------------------------- vtkIdType VTKViewer_DelaunayTriangulator ::GetNumFaces() @@ -743,32 +734,11 @@ VTKViewer_DelaunayTriangulator return myPolyData->GetNumberOfCells(); } + +//---------------------------------------------------------------------------- vtkCell* VTKViewer_DelaunayTriangulator ::GetFace(vtkIdType theFaceId) { return myPolyData->GetCell(theFaceId); } - -void -VTKViewer_DelaunayTriangulator -::GetCellNeighbors(vtkIdType theCellId, - vtkCell* theFace, - vtkIdList* theCellIds) -{ - myFaceIds->Reset(); - vtkIdList *anIdList = theFace->PointIds; - myFaceIds->InsertNextId(myPointIds[anIdList->GetId(0)]); - myFaceIds->InsertNextId(myPointIds[anIdList->GetId(1)]); - myFaceIds->InsertNextId(myPointIds[anIdList->GetId(2)]); - - myInput->GetCellNeighbors(theCellId, myFaceIds, theCellIds); -} - - -vtkIdType -VTKViewer_DelaunayTriangulator -::GetConnectivity(vtkIdType thePntId) -{ - return myPointIds[thePntId]; -} diff --git a/src/VTKViewer/VTKViewer_ConvexTool.h b/src/VTKViewer/VTKViewer_ConvexTool.h index a9e837838..5e391d976 100644 --- a/src/VTKViewer/VTKViewer_ConvexTool.h +++ b/src/VTKViewer/VTKViewer_ConvexTool.h @@ -40,6 +40,8 @@ class vtkCellArray; class vtkTriangle; class vtkOrderedTriangulator; + +//---------------------------------------------------------------------------- class VTKVIEWER_EXPORT VTKViewer_Triangulator { public: @@ -60,51 +62,48 @@ class VTKVIEWER_EXPORT VTKViewer_Triangulator std::vector& theVTK2ObjIds, bool theIsCheckConvex); - protected: + private: vtkIdList* myCellIds; - vtkUnstructuredGrid *myInput; - vtkIdType myCellId; - int myShowInside; - int myAllVisible; - const char* myCellsVisibility; + protected: + vtkIdType *myPointIds; + vtkIdList* myFaceIds; + vtkPoints* myPoints; virtual vtkPoints* - InitPoints() = 0; + InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId); virtual vtkIdType - GetNbOfPoints() = 0; + GetNumFaces() = 0; virtual - vtkIdType - GetPointId(vtkIdType thePointId) = 0; + vtkCell* + GetFace(vtkIdType theFaceId) = 0; - virtual - vtkFloatingPointType - GetCellLength() = 0; + vtkIdType + GetNbOfPoints(); - virtual vtkIdType - GetNumFaces() = 0; + GetPointId(vtkIdType thePointId); - virtual - vtkCell* - GetFace(vtkIdType theFaceId) = 0; + vtkFloatingPointType + GetCellLength(); - virtual void - GetCellNeighbors(vtkIdType theCellId, + GetCellNeighbors(vtkUnstructuredGrid *theInput, + vtkIdType theCellId, vtkCell* theFace, - vtkIdList* theCellIds) = 0; + vtkIdList* theCellIds); - virtual vtkIdType - GetConnectivity(vtkIdType thePntId) = 0; + GetConnectivity(vtkIdType thePntId); }; +//---------------------------------------------------------------------------- class VTKVIEWER_EXPORT VTKViewer_OrderedTriangulator : public VTKViewer_Triangulator { public: @@ -114,25 +113,14 @@ class VTKVIEWER_EXPORT VTKViewer_OrderedTriangulator : public VTKViewer_Triangul ~VTKViewer_OrderedTriangulator(); protected: - vtkGenericCell *myCell; - vtkCellArray *myBoundaryTris; - vtkTriangle *myTriangle; vtkOrderedTriangulator *myTriangulator; + vtkCellArray *myBoundaryTris; + vtkTriangle *myTriangle; virtual vtkPoints* - InitPoints(); - - virtual - vtkIdType - GetNbOfPoints(); - - vtkIdType - GetPointId(vtkIdType thePointId); - - virtual - vtkFloatingPointType - GetCellLength(); + InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId); virtual vtkIdType @@ -141,19 +129,10 @@ class VTKVIEWER_EXPORT VTKViewer_OrderedTriangulator : public VTKViewer_Triangul virtual vtkCell* GetFace(vtkIdType theFaceId); - - virtual - void - GetCellNeighbors(vtkIdType theCellId, - vtkCell* theFace, - vtkIdList* theCellIds); - - virtual - vtkIdType - GetConnectivity(vtkIdType thePntId); }; +//---------------------------------------------------------------------------- class VTKVIEWER_EXPORT VTKViewer_DelaunayTriangulator : public VTKViewer_Triangulator { public: @@ -167,24 +146,11 @@ class VTKVIEWER_EXPORT VTKViewer_DelaunayTriangulator : public VTKViewer_Triangu vtkGeometryFilter* myGeometryFilter; vtkDelaunay3D* myDelaunay3D; vtkPolyData* myPolyData; - vtkIdType *myPointIds; - vtkIdList* myFaceIds; - vtkPoints* myPoints; virtual vtkPoints* - InitPoints(); - - virtual - vtkIdType - GetNbOfPoints(); - - vtkIdType - GetPointId(vtkIdType thePointId); - - virtual - vtkFloatingPointType - GetCellLength(); + InitPoints(vtkUnstructuredGrid *theInput, + vtkIdType theCellId); virtual vtkIdType @@ -193,17 +159,6 @@ class VTKVIEWER_EXPORT VTKViewer_DelaunayTriangulator : public VTKViewer_Triangu virtual vtkCell* GetFace(vtkIdType theFaceId); - - virtual - void - GetCellNeighbors(vtkIdType theCellId, - vtkCell* theFace, - vtkIdList* theCellIds); - - virtual - vtkIdType - GetConnectivity(vtkIdType thePntId); - }; -- 2.39.2