From dfac601d231b7c14cdca3069cc817504ff949957 Mon Sep 17 00:00:00 2001 From: ouv Date: Wed, 3 Aug 2005 06:58:01 +0000 Subject: [PATCH] Rectangle selection --- src/SVTK/SALOME_Actor.cxx | 325 +++++++++++++++++------ src/SVTK/SALOME_Actor.h | 15 +- src/SVTK/SVTK_InteractorStyle.cxx | 159 ++--------- src/SVTK/SVTK_InteractorStyle.h | 9 - src/SVTK/SVTK_RenderWindowInteractor.cxx | 6 +- src/SVTK/SVTK_RenderWindowInteractor.h | 3 +- 6 files changed, 274 insertions(+), 243 deletions(-) diff --git a/src/SVTK/SALOME_Actor.cxx b/src/SVTK/SALOME_Actor.cxx index b5caa8e5d..2148371cc 100644 --- a/src/SVTK/SALOME_Actor.cxx +++ b/src/SVTK/SALOME_Actor.cxx @@ -38,7 +38,8 @@ #include "VTKViewer_TransformFilter.h" #include "VTKViewer_PassThroughFilter.h" #include "VTKViewer_GeometryFilter.h" - +#include "VTKViewer_CellRectPicker.h" + #include "SVTK_Actor.h" #include "SVTK_InteractorStyle.h" @@ -57,8 +58,10 @@ #include #include -//using namespace std; +#include +#include +//using namespace std; #if defined __GNUC__ #if __GNUC__ == 2 @@ -398,62 +401,35 @@ bool SALOME_Actor::Highlight( SVTK_InteractorStyle* theInteractorStyle, bool theIsUpdate ) { Selection_Mode aSelectionMode = theSelectionEvent.SelectionMode; - float x = theSelectionEvent.X; - float y = theSelectionEvent.Y; - float z = 0.0; + float x1 = theSelectionEvent.X; + float y1 = theSelectionEvent.Y; + float z1 = 0.0; + float x2 = theSelectionEvent.LastX; + float y2 = theSelectionEvent.LastY; + float z2 = 0.0; bool isShift = theSelectionEvent.IsShift; - - if( aSelectionMode == NodeSelection ) + bool isRectangle = theSelectionEvent.IsRectangle; + /* + cout << x1 << " " << y1; + if( isRectangle ) + cout << " " << x2 << " " << y2; + cout << endl; + */ + if( !isRectangle ) { - //vtkSmartPointer picker = VTKViewer_CellRectPicker::New(); - vtkPointPicker* aPicker = vtkPointPicker::New(); - - aPicker->Pick( x, y, z, theRenderer ); - - int aVtkId = aPicker->GetPointId(); - if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId, true ) ) + if( aSelectionMode == NodeSelection ) { - int anObjId = GetNodeObjId( aVtkId ); - if( anObjId >= 0 ) - { - if( theSelector->IsSelected( myIO ) ) - { - // This IO is already in the selection - theSelector->AddOrRemoveIndex( myIO, anObjId, isShift ); - } - else - { - if( !isShift ) - { - theInteractorStyle->HighlightProp( NULL ); - theSelector->ClearIObjects(); - } - theSelector->AddOrRemoveIndex( myIO, anObjId, isShift ); - theSelector->AddIObject( this ); - } - } - } - aPicker->Delete(); - } - else if( aSelectionMode == CellSelection ) - { - vtkCellPicker* aPicker = vtkCellPicker::New(); + vtkPointPicker* aPicker = vtkPointPicker::New(); + aPicker->Pick( x1, y1, z1, theRenderer ); - aPicker->Pick( x, y, z, theRenderer ); - - int aVtkId = aPicker->GetCellId(); - if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId ) ) - { - int anObjId = GetElemObjId( aVtkId ); - if( anObjId >= 0 ) + int aVtkId = aPicker->GetPointId(); + if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId, true ) ) { - //if( theInteractorStyle->CheckDimensionId( aSelectionMode, this, anObjId ) ) + int anObjId = GetNodeObjId( aVtkId ); + if( anObjId >= 0 ) { if( theSelector->IsSelected( myIO ) ) - { - // This IO is already in the selection theSelector->AddOrRemoveIndex( myIO, anObjId, isShift ); - } else { if( !isShift ) @@ -466,57 +442,174 @@ bool SALOME_Actor::Highlight( SVTK_InteractorStyle* theInteractorStyle, } } } + aPicker->Delete(); } - aPicker->Delete(); - } - else if( aSelectionMode == EdgeOfCellSelection ) - { - vtkCellPicker* aPicker = vtkCellPicker::New(); - - aPicker->Pick( x, y, z, theRenderer ); - - int aVtkId = aPicker->GetCellId(); - if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId ) ) + else if( aSelectionMode == CellSelection ) { - int anObjId = GetElemObjId( aVtkId ); - if( anObjId >= 0 ) + vtkCellPicker* aPicker = vtkCellPicker::New(); + aPicker->Pick( x1, y1, z1, theRenderer ); + + int aVtkId = aPicker->GetCellId(); + if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId ) ) { - if( !isShift ) + int anObjId = GetElemObjId( aVtkId ); + if( anObjId >= 0 ) { - theInteractorStyle->HighlightProp( NULL ); - theSelector->ClearIObjects(); + //if( theInteractorStyle->CheckDimensionId( aSelectionMode, this, anObjId ) ) + { + if( theSelector->IsSelected( myIO ) ) + theSelector->AddOrRemoveIndex( myIO, anObjId, isShift ); + else + { + if( !isShift ) + { + theInteractorStyle->HighlightProp( NULL ); + theSelector->ClearIObjects(); + } + theSelector->AddOrRemoveIndex( myIO, anObjId, isShift ); + theSelector->AddIObject( this ); + } + } + } + } + aPicker->Delete(); + } + else if( aSelectionMode == EdgeOfCellSelection ) + { + vtkCellPicker* aPicker = vtkCellPicker::New(); + aPicker->Pick( x1, y1, z1, theRenderer ); + + int aVtkId = aPicker->GetCellId(); + if( aVtkId >= 0 && hasIO() && theInteractorStyle->IsValid( this, aVtkId ) ) + { + int anObjId = GetElemObjId( aVtkId ); + if( anObjId >= 0 ) + { + if( !isShift ) + { + theInteractorStyle->HighlightProp( NULL ); + theSelector->ClearIObjects(); + } + int anEdgeId = 0;//theInteractorStyle->GetEdgeId( aPicker, this, anObjId ); + if( anEdgeId >= 0 ) + { + theSelector->AddOrRemoveIndex( myIO, anObjId, false ); + theSelector->AddOrRemoveIndex( myIO, -anEdgeId-1, true ); + theSelector->AddIObject( this ); + } } - int anEdgeId = 0;//theInteractorStyle->GetEdgeId( aPicker, this, anObjId ); - if( anEdgeId >= 0 ) + } + aPicker->Delete(); + } + else if( aSelectionMode == ActorSelection ) + { + if ( hasIO() ) + { + //theInteractorStyle->PropPicked++; + if( theSelector->IsSelected( myIO ) && isShift ) + theSelector->RemoveIObject( this ); + else { - theSelector->AddOrRemoveIndex( myIO, anObjId, false ); - theSelector->AddOrRemoveIndex( myIO, -anEdgeId-1, true ); + if( !isShift ) + { + theInteractorStyle->HighlightProp( NULL ); + theSelector->ClearIObjects(); + } theSelector->AddIObject( this ); - } + } } } - aPicker->Delete(); } - else if( aSelectionMode == ActorSelection ) + else if( isRectangle ) { - if ( hasIO() ) + if( aSelectionMode == NodeSelection && hasIO() && !myIO.IsNull() ) { - //theInteractorStyle->PropPicked++; - if( theSelector->IsSelected( myIO ) ) + vtkPointPicker* aPicker = vtkPointPicker::New(); + if( vtkDataSet* aDataSet = GetInput() ) { - // This IO is already in the selection - if( isShift ) + TColStd_MapOfInteger anIndices; + for( int i = 0; i < aDataSet->GetNumberOfPoints(); i++) + { + float aPoint[3]; + aDataSet->GetPoint( i, aPoint ); + + float aPnt[3]; + theRenderer->SetWorldPoint( aPoint[0], aPoint[1], aPoint[2], 1.0 ); + theRenderer->WorldToDisplay(); + theRenderer->GetDisplayPoint( aPnt ); + + if( aPnt[0] > x2 && aPnt[0] < x1 && aPnt[1] > y1 && aPnt[1] < y2 ) + //if( theInteractorStyle->IsInRect( aPoint, x2, y2, x1, y1 ) ) + { + float aDisp[3]; + //theInteractorStyle->ComputeWorldToDisplay( aPoint[0], aPoint[1] ,aPoint[2], aDisp ); + theRenderer->SetWorldPoint( aPoint[0], aPoint[1], aPoint[2], 1.0 ); + theRenderer->WorldToDisplay(); + theRenderer->GetDisplayPoint( aDisp ); + + if( aPicker->Pick( aDisp[0], aDisp[1], 0.0, theRenderer ) ) + { + if( vtkActorCollection* anActorCollection = aPicker->GetActors() ) + { + if( anActorCollection->IsItemPresent( this ) ) + { + float aPickedPoint[3]; + aPicker->GetMapperPosition( aPickedPoint ); + vtkIdType aVtkId = aDataSet->FindPoint( aPickedPoint ); + if( aVtkId >= 0 && theInteractorStyle->IsValid( this, aVtkId, true ) ) + { + int anObjId = GetNodeObjId( aVtkId ); + anIndices.Add( anObjId ); + } + } + } + } + } + } + if( !anIndices.IsEmpty() ) + { + theSelector->AddOrRemoveIndex( myIO, anIndices, false ); + theSelector->AddIObject( this ); + anIndices.Clear(); + } + else theSelector->RemoveIObject( this ); } - else + aPicker->Delete(); + } + else if( aSelectionMode != ActorSelection && hasIO() ) + { + VTKViewer_CellRectPicker* aPicker = VTKViewer_CellRectPicker::New(); + aPicker->SetTolerance( 0.001 ); + aPicker->Pick( x2, y2, z2, x1, y1, z1, theRenderer ); + + VTKViewer_CellDataSet cellList = aPicker->GetCellData( this ); + TColStd_MapOfInteger anIndexes; + if( !cellList.empty() ) { - if( !isShift ) + VTKViewer_CellDataSet::iterator it; + for( it = cellList.begin(); it != cellList.end(); ++it ) { - theInteractorStyle->HighlightProp( NULL ); - theSelector->ClearIObjects(); + int aCellId = (*it).cellId; + if ( !theInteractorStyle->IsValid( this, aCellId ) ) + continue; + + int anObjId = GetElemObjId( aCellId ); + if( anObjId != -1 ) + { + //if( CheckDimensionId( aSelectionMode, this ,anObjId ) ) + anIndexes.Add( anObjId ); + } } - theSelector->AddIObject( this ); } + theSelector->AddOrRemoveIndex( myIO, anIndexes, true ); + theSelector->AddIObject( this ); + + aPicker->Delete(); + } + else if( aSelectionMode == ActorSelection && hasIO() ) + { + theSelector->AddIObject( this ); } } @@ -535,10 +628,10 @@ bool SALOME_Actor::Highlight( SVTK_InteractorStyle* theInteractorStyle, case ActorSelection: cout << "Actor" << endl; break; default: cout << "Other" << endl; break; } + cout << "IsRectangle : " << ( isRectangle ? "true" : "false" ) << endl; cout << "IsHighlight : " << ( theIsHighlight ? "true" : "false" ) << endl; cout << "IsUpdate : " << ( theIsUpdate ? "true" : "false" ) << endl; */ - if( GetVisibility() && theIsHighlight ) theInteractorStyle->HighlightProp( this ); else if( !theIsHighlight ) @@ -584,3 +677,69 @@ bool SALOME_Actor::Unhighlight( SVTK_InteractorStyle* theInteractorStyle, theInteractorStyle->HighlightProp( NULL ); theSelector->ClearIObjects(); } + +/* +/// Checks: is the given Actor within display coordinates? +bool +SVTK_InteractorStyle +::IsInRect(vtkActor* theActor, + const int left, const int top, + const int right, const int bottom) +{ + float* aBounds = theActor->GetBounds(); + float aMin[3], aMax[3]; + ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin); + ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax); + if (aMin[0] > aMax[0]) { + float aBuf = aMin[0]; + aMin[0] = aMax[0]; + aMax[0] = aBuf; + } + if (aMin[1] > aMax[1]) { + float aBuf = aMin[1]; + aMin[1] = aMax[1]; + aMax[1] = aBuf; + } + + return ((aMin[0]>left) && (aMax[0]bottom) && (aMax[1]GetBounds(); + float aMin[3], aMax[3]; + ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin); + ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax); + if (aMin[0] > aMax[0]) { + float aBuf = aMin[0]; + aMin[0] = aMax[0]; + aMax[0] = aBuf; + } + if (aMin[1] > aMax[1]) { + float aBuf = aMin[1]; + aMin[1] = aMax[1]; + aMax[1] = aBuf; + } + + return ((aMin[0]>left) && (aMax[0]bottom) && (aMax[1]left) && (aPnt[0]bottom) && (aPnt[1] - #include class vtkRenderer; @@ -205,7 +203,18 @@ class SALOME_OBJECT_EXPORT SALOME_Actor : public VTKViewer_Actor { virtual bool Highlight( SVTK_InteractorStyle*, SVTK_Selector*, vtkRenderer*, SVTK_SelectionEvent, bool, bool ); virtual bool Unhighlight( SVTK_InteractorStyle*, SVTK_Selector*, bool ); - + /* + protected: + bool IsInRect(vtkActor* theActor, + const int left, const int top, + const int right, const int bottom); + bool IsInRect(vtkCell* theCell, + const int left, const int top, + const int right, const int bottom); + bool IsInRect(float* thePoint, + const int left, const int top, + const int right, const int bottom); + */ protected: vtkProperty* myPointProperty; vtkProperty* myCellProperty; diff --git a/src/SVTK/SVTK_InteractorStyle.cxx b/src/SVTK/SVTK_InteractorStyle.cxx index 6627d9fb3..89e522ea3 100644 --- a/src/SVTK/SVTK_InteractorStyle.cxx +++ b/src/SVTK/SVTK_InteractorStyle.cxx @@ -907,6 +907,7 @@ SVTK_InteractorStyle SVTK_SelectionEvent aSelectionEvent = myInteractor->GetSelectionEvent(); aSelectionEvent.SelectionMode = aSelectionMode; + aSelectionEvent.IsRectangle = false; if( aSActor ) aSActor->Highlight( this, GetSelector(), this->CurrentRenderer, aSelectionEvent, true, true ); @@ -940,6 +941,12 @@ SVTK_InteractorStyle x2 = rect.right(); y2 = h - rect.bottom() - 1; + SVTK_SelectionEvent aSelectionEvent = myInteractor->GetSelectionEvent(); + aSelectionEvent.SelectionMode = aSelectionMode; + aSelectionEvent.IsRectangle = true; + aSelectionEvent.LastX = x1; + aSelectionEvent.LastY = y1; + switch (aSelectionMode) { case NodeSelection: { if ( vtkPointPicker* aPointPicker = vtkPointPicker::SafeDownCast(myInteractor->GetPicker()) ) { @@ -948,46 +955,8 @@ SVTK_InteractorStyle while (vtkActor* aActor = aListActors->GetNextActor()) { if (!aActor->GetVisibility()) continue; - if(SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) { - if (aSActor->hasIO()) { - Handle(SALOME_InteractiveObject) anIO = aSActor->getIO(); - if (anIO.IsNull()) - continue; - if (aSelActiveCompOnly && aComponentDataType != anIO->getComponentDataType()) - continue; - if (vtkDataSet* aDataSet = aSActor->GetInput()) { - TColStd_MapOfInteger anIndices; - for(int i = 0; i < aDataSet->GetNumberOfPoints(); i++) { - float aPoint[3]; - aDataSet->GetPoint(i,aPoint); - if (IsInRect(aPoint,x1,y1,x2,y2)){ - float aDisp[3]; - ComputeWorldToDisplay(aPoint[0],aPoint[1],aPoint[2],aDisp); - if(aPointPicker->Pick(aDisp[0],aDisp[1],0.0,CurrentRenderer)){ - if(vtkActorCollection *anActorCollection = aPointPicker->GetActors()){ - if(anActorCollection->IsItemPresent(aSActor)){ - float aPickedPoint[3]; - aPointPicker->GetMapperPosition(aPickedPoint); - vtkIdType aVtkId = aDataSet->FindPoint(aPickedPoint); - if ( aVtkId >= 0 && IsValid( aSActor, aVtkId, true ) ){ - int anObjId = aSActor->GetNodeObjId(aVtkId); - anIndices.Add(anObjId); - } - } - } - } - } - } - if (!anIndices.IsEmpty()) { - GetSelector()->AddOrRemoveIndex(anIO,anIndices,false); - GetSelector()->AddIObject(aSActor); - anIndices.Clear(); - }else{ - GetSelector()->RemoveIObject(aSActor); - } - } - } - } + if(SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) + aSActor->Highlight( this, GetSelector(), this->CurrentRenderer, aSelectionEvent, true, true ); } } break; @@ -1004,36 +973,9 @@ SVTK_InteractorStyle vtkActorCollection* aListActors = picker->GetActors(); aListActors->InitTraversal(); - while(vtkActor* aActor = aListActors->GetNextActor()) { - if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) { - if (aSActor->hasIO()) { - Handle(SALOME_InteractiveObject) anIO = aSActor->getIO(); - if (aSelActiveCompOnly && aComponentDataType != anIO->getComponentDataType()) - continue; - VTKViewer_CellDataSet cellList = picker->GetCellData(aActor); - if ( !cellList.empty() ) { - if(MYDEBUG) INFOS ( " NAME Actor : " << aSActor->getName() ); - TColStd_MapOfInteger anIndexes; - VTKViewer_CellDataSet::iterator it; - for ( it = cellList.begin(); it != cellList.end(); ++it ) { - int aCellId = (*it).cellId; - - if ( !IsValid( aSActor, aCellId ) ) - continue; - - int anObjId = aSActor->GetElemObjId(aCellId); - if (anObjId != -1){ - if ( CheckDimensionId(aSelectionMode,aSActor,anObjId) ) { - anIndexes.Add(anObjId); - } - } - } - GetSelector()->AddOrRemoveIndex(anIO,anIndexes,true); - GetSelector()->AddIObject(aSActor); - } - } - } - } + while(vtkActor* aActor = aListActors->GetNextActor()) + if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) + aSActor->Highlight( this, GetSelector(), this->CurrentRenderer, aSelectionEvent, true, true ); } break; case ActorSelection: // objects selection @@ -1044,15 +986,9 @@ SVTK_InteractorStyle vtkActorCollection* aListActors = picker->GetActors(); aListActors->InitTraversal(); - while(vtkActor* aActor = aListActors->GetNextActor()) { - if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) { - if (aSActor->hasIO()) { - Handle(SALOME_InteractiveObject) anIO = aSActor->getIO(); - GetSelector()->AddIObject(aSActor); - this->PropPicked++; - } - } - } + while(vtkActor* aActor = aListActors->GetNextActor()) + if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) + aSActor->Highlight( this, GetSelector(), this->CurrentRenderer, aSelectionEvent, true, true ); } // end case 4 } //end switch myInteractor->EndPickCallback(); @@ -1357,71 +1293,6 @@ SVTK_InteractorStyle motionVector[2] + viewPoint[2]); } - -/// Checks: is the given Actor within display coordinates? -bool -SVTK_InteractorStyle -::IsInRect(vtkActor* theActor, - const int left, const int top, - const int right, const int bottom) -{ - float* aBounds = theActor->GetBounds(); - float aMin[3], aMax[3]; - ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin); - ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax); - if (aMin[0] > aMax[0]) { - float aBuf = aMin[0]; - aMin[0] = aMax[0]; - aMax[0] = aBuf; - } - if (aMin[1] > aMax[1]) { - float aBuf = aMin[1]; - aMin[1] = aMax[1]; - aMax[1] = aBuf; - } - - return ((aMin[0]>left) && (aMax[0]bottom) && (aMax[1]GetBounds(); - float aMin[3], aMax[3]; - ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin); - ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax); - if (aMin[0] > aMax[0]) { - float aBuf = aMin[0]; - aMin[0] = aMax[0]; - aMax[0] = aBuf; - } - if (aMin[1] > aMax[1]) { - float aBuf = aMin[1]; - aMin[1] = aMax[1]; - aMax[1] = aBuf; - } - - return ((aMin[0]>left) && (aMax[0]bottom) && (aMax[1]left) && (aPnt[0]bottom) && (aPnt[1]OnSelectionModeChanged(); } -/* + void SVTK_RenderWindowInteractor ::SetSelectionProp(const double& theRed, @@ -202,11 +202,13 @@ SVTK_RenderWindowInteractor const double& theBlue, const int& theWidth) { + /* myCellActor->GetProperty()->SetColor(theRed, theGreen, theBlue); myCellActor->GetProperty()->SetLineWidth(theWidth); myPointActor->GetProperty()->SetColor(theRed, theGreen, theBlue); myPointActor->GetProperty()->SetPointSize(theWidth); + */ } void @@ -222,7 +224,7 @@ SVTK_RenderWindowInteractor myPointPicker->SetTolerance(myTolNodes); } -*/ + // ================================== void SVTK_RenderWindowInteractor diff --git a/src/SVTK/SVTK_RenderWindowInteractor.h b/src/SVTK/SVTK_RenderWindowInteractor.h index d37b1c6ca..61ad36d5c 100644 --- a/src/SVTK/SVTK_RenderWindowInteractor.h +++ b/src/SVTK/SVTK_RenderWindowInteractor.h @@ -110,14 +110,13 @@ public: void rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName); void SetSelectionMode(Selection_Mode mode); - /* void SetSelectionProp(const double& theRed = 1, const double& theGreen = 1, const double& theBlue = 0, const int& theWidth = 5); void SetSelectionTolerance(const double& theTolNodes = 0.025, const double& theTolCell = 0.001); - */ + // Displaymode management int GetDisplayMode(); void SetDisplayMode(int); -- 2.39.2