Salome HOME
0022077: EDF 2272 : Selection with the Paraview interaction mode in GEOM/SMESH
[modules/gui.git] / src / SVTK / SVTK_AreaPicker.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME VTKViewer : build VTK viewer into Salome desktop
24 //  File   : SVTK_AreaPicker.cxx
25 //  Author : 
26 //  Module : SALOME
27 //
28 #include "SVTK_AreaPicker.h"
29
30 #include <set>
31
32 #include <vtkObjectFactory.h>
33 #include <vtkCommand.h>
34
35 #include <vtkAbstractMapper3D.h>
36 #include <vtkMapper.h>
37 #include <vtkProperty.h>
38
39 #include <vtkAssemblyPath.h>
40 #include <vtkAssemblyNode.h>
41
42 #include <vtkRenderWindow.h>
43 #include <vtkMatrix4x4.h>
44 #include <vtkRenderer.h>
45 #include <vtkPoints.h>
46 #include <vtkCamera.h>
47 #include <vtkCell.h>
48
49 namespace
50 {
51   //----------------------------------------------------------------------------
52   inline
53   double GetZ( float* theZPtr, int theSelection[4], int theDX, int theDY )
54   {
55     return theZPtr[theDX - theSelection[0]
56         + ( theDY - theSelection[1] )
57             * ( theSelection[2] - theSelection[0] + 1 )];
58   }
59
60   //----------------------------------------------------------------------------
61   inline
62   int Check( float* theZPtr, int theSelection[4], double theTolerance,
63       double theDZ, int theDX, int theDY )
64   {
65     int aRet = 0;
66     double aZ = -1.0;
67     if ( theDX >= theSelection[0] && theDX <= theSelection[2]
68         && theDY >= theSelection[1] && theDY <= theSelection[3] ) {
69       // Access the value from the captured zbuffer.  Note, we only
70       // captured a portion of the zbuffer, so we need to offset dx by
71       // the selection window.
72       aZ = GetZ( theZPtr, theSelection, theDX, theDY );
73       if ( aZ > theTolerance && aZ < 1.0 - theTolerance ) {
74         aRet = fabs( aZ - theDZ ) <= theTolerance;
75       }
76     }
77     return aRet;
78   }
79
80   //----------------------------------------------------------------------------
81   inline
82   void GetCenter( const double theBounds[6], double theCenter[3] )
83   {
84     theCenter[0] = ( theBounds[1] + theBounds[0] ) / 2.0;
85     theCenter[1] = ( theBounds[3] + theBounds[2] ) / 2.0;
86     theCenter[2] = ( theBounds[5] + theBounds[4] ) / 2.0;
87   }
88
89   //----------------------------------------------------------------------------
90   void CalculatePickPosition( vtkRenderer *theRenderer, double theSelectionX,
91       double theSelectionY, double theSelectionZ, double thePickPosition[3] )
92   {
93     // Convert the selection point into world coordinates.
94     //
95     theRenderer->SetDisplayPoint( theSelectionX, theSelectionY, theSelectionZ );
96     theRenderer->DisplayToWorld();
97     double* aWorldCoords = theRenderer->GetWorldPoint();
98     if ( aWorldCoords[3] != 0.0 ) {
99       for ( int i = 0; i < 3; i++ ) {
100         thePickPosition[i] = aWorldCoords[i] / aWorldCoords[3];
101       }
102     }
103   }
104 }
105
106 vtkStandardNewMacro( SVTK_AreaPicker )
107 ;
108
109 SVTK_AreaPicker::SVTK_AreaPicker()
110 {
111   this->Tolerance = 0.005;
112   this->PickPoints = 1;
113 }
114
115 SVTK_AreaPicker::~SVTK_AreaPicker()
116 {
117 }
118
119 int SVTK_AreaPicker::Pick( double, double, double, vtkRenderer* )
120 {
121   return 0;
122 }
123
124 int SVTK_AreaPicker::Pick( double theSelectionX, double theSelectionY,
125     double theSelectionX2, double theSelectionY2, vtkRenderer *theRenderer,
126     SelectionMode theMode )
127 {
128   QVector< QPoint > aPoints;
129   aPoints.append( QPoint( theSelectionX, theSelectionY ) );
130   aPoints.append( QPoint( theSelectionX2, theSelectionY2 ) );
131   return Pick( aPoints, theRenderer, theMode );
132 }
133
134 int SVTK_AreaPicker::Pick( QVector< QPoint >& thePoints,
135     vtkRenderer *theRenderer, SelectionMode theMode )
136 {
137   //  Initialize picking process
138   this->Initialize();
139   myCellIdsMap.clear();
140   myPointIdsMap.clear();
141   this->Renderer = theRenderer;
142
143   if ( theMode == RectangleMode ) {
144     mySelection = {thePoints[0].x(), thePoints[0].y(), thePoints[1].x(), thePoints[1].y()};
145   }
146   else if( theMode == PolygonMode ) {
147     int minX, minY, maxX, maxY;
148     minX = maxX = thePoints[0].x();
149     minY = maxY = thePoints[0].y();
150     for ( int i=0; i < thePoints.size(); i++ ) {
151       if ( thePoints[i].x() < minX )
152       minX = thePoints[i].x();
153       if ( thePoints[i].x() > maxX )
154       maxX = thePoints[i].x();
155       if ( thePoints[i].y() < minY )
156       minY = thePoints[i].y();
157       if ( thePoints[i].y() > maxY )
158       maxY = thePoints[i].y();
159     }
160     mySelection = {minX, minY, maxX, maxY};
161   }
162
163   // Invoke start pick method if defined
164   this->InvokeEvent( vtkCommand::StartPickEvent, NULL );
165
166   vtkPropCollection *aProps;
167   if ( this->PickFromList ) aProps = this->GetPickList();
168   else
169     aProps = theRenderer->GetViewProps();
170
171   aProps->InitTraversal();
172   while( vtkProp* aProp = aProps->GetNextProp() ) {
173     aProp->InitPathTraversal();
174     while( vtkAssemblyPath* aPath = aProp->GetNextPath() ) {
175       vtkMapper *aMapper = NULL;
176       bool anIsPickable = false;
177       vtkActor* anActor = NULL;
178       vtkProp *aPropCandidate = aPath->GetLastNode()->GetViewProp();
179       if ( aPropCandidate->GetPickable() && aPropCandidate->GetVisibility() ) {
180         anIsPickable = true;
181         anActor = vtkActor::SafeDownCast( aPropCandidate );
182         if ( anActor ) {
183           aMapper = anActor->GetMapper();
184           if ( anActor->GetProperty()->GetOpacity() <= 0.0 ) anIsPickable =
185               false;
186         }
187       }
188       if ( anIsPickable && aMapper && aMapper->GetInput() ) {
189         if ( this->PickPoints ) {
190           TVectorIds& aVisibleIds = myPointIdsMap[anActor];
191           TVectorIds anInVisibleIds;
192           SelectVisiblePoints( thePoints, theRenderer, aMapper->GetInput(),
193               aVisibleIds, anInVisibleIds, this->Tolerance, theMode );
194           if ( aVisibleIds.empty() ) {
195             myPointIdsMap.erase( myPointIdsMap.find( anActor ) );
196           }
197         }
198         else {
199           TVectorIds& aVectorIds = myCellIdsMap[anActor];
200           SelectVisibleCells( thePoints, theRenderer, aMapper->GetInput(),
201               aVectorIds, this->Tolerance, theMode );
202           if ( aVectorIds.empty() ) {
203             myCellIdsMap.erase( myCellIdsMap.find( anActor ) );
204           }
205         }
206       }
207     }
208   }
209
210   // Invoke end pick method if defined
211   this->InvokeEvent( vtkCommand::EndPickEvent, NULL );
212
213   return myPointIdsMap.empty() || myCellIdsMap.empty();
214 }
215
216 //----------------------------------------------------------------------------
217 void SVTK_AreaPicker::SelectVisiblePoints( QVector< QPoint >& thePoints,
218     vtkRenderer *theRenderer, vtkDataSet *theInput,
219     SVTK_AreaPicker::TVectorIds& theVisibleIds,
220     SVTK_AreaPicker::TVectorIds& theInVisibleIds, double theTolerance,
221     SelectionMode theMode )
222 {
223   theVisibleIds.clear();
224   theInVisibleIds.clear();
225
226   vtkIdType aNumPts = theInput->GetNumberOfPoints();
227   if ( aNumPts < 1 ) return;
228
229   theVisibleIds.reserve( aNumPts / 2 + 1 );
230   theInVisibleIds.reserve( aNumPts / 2 + 1 );
231
232   // Grab the composite perspective transform.  This matrix is used to convert
233   // each point to view coordinates.  vtkRenderer provides a WorldToView()
234   // method but it computes the composite perspective transform each time
235   // WorldToView() is called.  This is expensive, so we get the matrix once
236   // and handle the transformation ourselves.
237   vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
238   aMatrix->DeepCopy(
239       theRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix(
240           theRenderer->GetTiledAspectRatio(), 0, 1 ) );
241
242   // We grab the z-buffer for the selection region all at once and probe the resulting array.
243   float *aZPtr = theRenderer->GetRenderWindow()->GetZbufferData( mySelection[0],
244       mySelection[1], mySelection[2], mySelection[3] );
245
246   for ( vtkIdType aPntId = 0; aPntId < aNumPts; aPntId++ ) {
247     // perform conversion
248     double aX[4] = { 1.0, 1.0, 1.0, 1.0 };
249     theInput->GetPoint( aPntId, aX );
250
251     double aView[4];
252     aMatrix->MultiplyPoint( aX, aView );
253     if ( aView[3] == 0.0 ) continue;
254     theRenderer->SetViewPoint( aView[0] / aView[3], aView[1] / aView[3],
255         aView[2] / aView[3] );
256     theRenderer->ViewToDisplay();
257
258     double aDX[3];
259     theRenderer->GetDisplayPoint( aDX );
260
261     bool isInSelection;
262     if ( theMode == RectangleMode ) isInSelection = aDX[0] >= mySelection[0]
263         && aDX[0] <= mySelection[2] && aDX[1] >= mySelection[1]
264         && aDX[1] <= mySelection[3];
265     else
266       if ( theMode == PolygonMode ) isInSelection =
267           SVTK_AreaPicker::isPointInPolygon( QPoint( aDX[0], aDX[1] ),
268               thePoints );
269
270     // check whether visible and in selection window
271     if ( isInSelection ) {
272       int aDX0 = int( aDX[0] );
273       int aDX1 = int( aDX[1] );
274
275       int aRet = Check( aZPtr, mySelection, theTolerance, aDX[2], aDX0, aDX1 );
276       if ( aRet > 0 ) goto ADD_VISIBLE;
277       if ( aRet < 0 ) goto ADD_INVISIBLE;
278
279       static int aMaxRadius = 5;
280       for ( int aRadius = 1; aRadius < aMaxRadius; aRadius++ ) {
281         int aStartDX[2] = { aDX0 - aRadius, aDX1 - aRadius };
282         for ( int i = 0; i <= aRadius; i++ ) {
283           int aRet = Check( aZPtr, mySelection, theTolerance, aDX[2],
284               aStartDX[0]++, aStartDX[1] );
285           if ( aRet > 0 ) goto ADD_VISIBLE;
286           if ( aRet < 0 ) goto ADD_INVISIBLE;
287         }
288         for ( int i = 0; i <= aRadius; i++ ) {
289           int aRet = Check( aZPtr, mySelection, theTolerance, aDX[2],
290               aStartDX[0], aStartDX[1]++ );
291           if ( aRet > 0 ) goto ADD_VISIBLE;
292           if ( aRet < 0 ) goto ADD_INVISIBLE;
293         }
294         for ( int i = 0; i <= aRadius; i++ ) {
295           int aRet = Check( aZPtr, mySelection, theTolerance, aDX[2],
296               aStartDX[0]--, aStartDX[1] );
297           if ( aRet > 0 ) goto ADD_VISIBLE;
298           if ( aRet < 0 ) goto ADD_INVISIBLE;
299         }
300         for ( int i = 0; i <= aRadius; i++ ) {
301           int aRet = Check( aZPtr, mySelection, theTolerance, aDX[2],
302               aStartDX[0], aStartDX[1]-- );
303           if ( aRet > 0 ) goto ADD_VISIBLE;
304           if ( aRet < 0 ) goto ADD_INVISIBLE;
305         }
306       }
307       if ( false ) ADD_VISIBLE:theVisibleIds.push_back( aPntId );
308       if ( false ) ADD_INVISIBLE:theInVisibleIds.push_back( aPntId );
309     }
310   }  //for all points
311
312   aMatrix->Delete();
313
314   if ( aZPtr ) delete[] aZPtr;
315 }
316
317 void SVTK_AreaPicker::SelectVisibleCells( QVector< QPoint >& thePoints,
318     vtkRenderer *theRenderer, vtkDataSet *theInput,
319     SVTK_AreaPicker::TVectorIds& theVectorIds, double theTolerance,
320     SelectionMode theMode )
321 {
322   theVectorIds.clear();
323
324   vtkIdType aNumCells = theInput->GetNumberOfCells();
325   if ( aNumCells < 1 ) return;
326
327   theVectorIds.reserve( aNumCells / 2 + 1 );
328
329   SVTK_AreaPicker::TVectorIds aVisiblePntIds;
330   SVTK_AreaPicker::TVectorIds anInVisiblePntIds;
331   SelectVisiblePoints( thePoints, theRenderer, theInput, aVisiblePntIds,
332       anInVisiblePntIds, theTolerance, theMode );
333
334   typedef std::set< vtkIdType > TIdsSet;
335   TIdsSet aVisibleIds( aVisiblePntIds.begin(), aVisiblePntIds.end() );
336   TIdsSet anInVisibleIds( anInVisiblePntIds.begin(), anInVisiblePntIds.end() );
337
338   // Grab the composite perspective transform.  This matrix is used to convert
339   // each point to view coordinates.  vtkRenderer provides a WorldToView()
340   // method but it computes the composite perspective transform each time
341   // WorldToView() is called.  This is expensive, so we get the matrix once
342   // and handle the transformation ourselves.
343   vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
344   aMatrix->DeepCopy(
345       theRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix(
346           theRenderer->GetTiledAspectRatio(), 0, 1 ) );
347
348   for ( vtkIdType aCellId = 0; aCellId < aNumCells; aCellId++ ) {
349     vtkCell* aCell = theInput->GetCell( aCellId );
350
351     double aBounds[6];
352     aCell->GetBounds( aBounds );
353
354     double aCenter[3];
355     GetCenter( aBounds, aCenter );
356
357     double aView[4];
358     double aX[4] = { aCenter[0], aCenter[1], aCenter[2], 1.0 };
359     aMatrix->MultiplyPoint( aX, aView );
360
361     if ( aView[3] == 0.0 ) continue;
362
363     theRenderer->SetViewPoint( aView[0] / aView[3], aView[1] / aView[3],
364         aView[2] / aView[3] );
365     theRenderer->ViewToDisplay();
366
367     double aDX[3];
368     theRenderer->GetDisplayPoint( aDX );
369
370     bool isInSelection;
371     if ( theMode == RectangleMode ) isInSelection = aDX[0] >= mySelection[0]
372         && aDX[0] <= mySelection[2] && aDX[1] >= mySelection[1]
373         && aDX[1] <= mySelection[3];
374     else
375       if ( theMode == PolygonMode ) isInSelection =
376           SVTK_AreaPicker::isPointInPolygon( QPoint( aDX[0], aDX[1] ),
377               thePoints );
378     // check whether visible and in selection window
379     if ( isInSelection ) {
380       vtkIdType aNumPts = aCell->GetNumberOfPoints();
381       bool anIsVisible = true;
382       for ( vtkIdType anId = 0; anId < aNumPts; anId++ ) {
383         vtkIdType aPntId = aCell->GetPointId( anId );
384         anIsVisible = aVisibleIds.find( aPntId ) != aVisibleIds.end();
385         if ( !anIsVisible ) break;
386       }
387       if ( anIsVisible ) theVectorIds.push_back( aCellId );
388     }
389   }  //for all parts
390 }
391
392 bool SVTK_AreaPicker::isPointInPolygon( const QPoint& thePoint,
393     const QVector< QPoint >& thePolygon )
394 {
395   double eps = 1.0;
396   if ( thePolygon.size() < 3 ) return false;
397
398   QVector< QPoint >::const_iterator end = thePolygon.end();
399   QPoint last_pt = thePolygon.back();
400
401   last_pt.setX( last_pt.x() - thePoint.x() );
402   last_pt.setY( last_pt.y() - thePoint.y() );
403
404   double sum = 0.0;
405
406   for ( QVector< QPoint >::const_iterator iter = thePolygon.begin();
407       iter != end; ++iter ) {
408     QPoint cur_pt = *iter;
409     cur_pt.setX( cur_pt.x() - thePoint.x() );
410     cur_pt.setY( cur_pt.y() - thePoint.y() );
411
412     double del = last_pt.x() * cur_pt.y() - cur_pt.x() * last_pt.y();
413     double xy = cur_pt.x() * last_pt.x() + cur_pt.y() * last_pt.y();
414
415     sum +=
416         ( atan(
417             ( last_pt.x() * last_pt.x() + last_pt.y() * last_pt.y() - xy )
418                 / del )
419             + atan(
420                 ( cur_pt.x() * cur_pt.x() + cur_pt.y() * cur_pt.y() - xy )
421                     / del ) );
422
423     last_pt = cur_pt;
424   }
425   return fabs( sum ) > eps;
426 }
427
428 const SVTK_AreaPicker::TVectorIdsMap&
429 SVTK_AreaPicker::GetPointIdsMap() const
430 {
431   return myPointIdsMap;
432 }
433
434 const SVTK_AreaPicker::TVectorIdsMap&
435 SVTK_AreaPicker::GetCellIdsMap() const
436 {
437   return myCellIdsMap;
438 }