1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SALOME VTKViewer : build VTK viewer into Salome desktop
24 // File : SVTK_AreaPicker.cxx
28 #include "SVTK_AreaPicker.h"
32 #include <vtkObjectFactory.h>
33 #include <vtkCommand.h>
35 #include <vtkAbstractMapper3D.h>
36 #include <vtkMapper.h>
37 #include <vtkProperty.h>
39 #include <vtkAssemblyPath.h>
40 #include <vtkAssemblyNode.h>
42 #include <vtkRenderWindow.h>
43 #include <vtkMatrix4x4.h>
44 #include <vtkRenderer.h>
45 #include <vtkPoints.h>
46 #include <vtkCamera.h>
51 //----------------------------------------------------------------------------
53 double GetZ( float* theZPtr, int theSelection[4], int theDX, int theDY )
55 return theZPtr[theDX - theSelection[0]
56 + ( theDY - theSelection[1] )
57 * ( theSelection[2] - theSelection[0] + 1 )];
60 //----------------------------------------------------------------------------
62 int Check( float* theZPtr, int theSelection[4], double theTolerance,
63 double theDZ, int theDX, int theDY )
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;
80 //----------------------------------------------------------------------------
82 void GetCenter( const double theBounds[6], double theCenter[3] )
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;
89 //----------------------------------------------------------------------------
90 void CalculatePickPosition( vtkRenderer *theRenderer, double theSelectionX,
91 double theSelectionY, double theSelectionZ, double thePickPosition[3] )
93 // Convert the selection point into world coordinates.
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];
106 vtkStandardNewMacro( SVTK_AreaPicker )
109 SVTK_AreaPicker::SVTK_AreaPicker()
111 this->Tolerance = 0.005;
112 this->PickPoints = 1;
115 SVTK_AreaPicker::~SVTK_AreaPicker()
119 int SVTK_AreaPicker::Pick( double, double, double, vtkRenderer* )
124 int SVTK_AreaPicker::Pick( double theSelectionX, double theSelectionY,
125 double theSelectionX2, double theSelectionY2, vtkRenderer *theRenderer,
126 SelectionMode theMode )
128 QVector< QPoint > aPoints;
129 aPoints.append( QPoint( theSelectionX, theSelectionY ) );
130 aPoints.append( QPoint( theSelectionX2, theSelectionY2 ) );
131 return Pick( aPoints, theRenderer, theMode );
134 int SVTK_AreaPicker::Pick( QVector< QPoint >& thePoints,
135 vtkRenderer *theRenderer, SelectionMode theMode )
137 // Initialize picking process
139 myCellIdsMap.clear();
140 myPointIdsMap.clear();
141 this->Renderer = theRenderer;
143 if ( theMode == RectangleMode ) {
144 mySelection = {thePoints[0].x(), thePoints[0].y(), thePoints[1].x(), thePoints[1].y()};
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();
160 mySelection = {minX, minY, maxX, maxY};
163 // Invoke start pick method if defined
164 this->InvokeEvent( vtkCommand::StartPickEvent, NULL );
166 vtkPropCollection *aProps;
167 if ( this->PickFromList ) aProps = this->GetPickList();
169 aProps = theRenderer->GetViewProps();
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() ) {
181 anActor = vtkActor::SafeDownCast( aPropCandidate );
183 aMapper = anActor->GetMapper();
184 if ( anActor->GetProperty()->GetOpacity() <= 0.0 ) anIsPickable =
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 ) );
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 ) );
210 // Invoke end pick method if defined
211 this->InvokeEvent( vtkCommand::EndPickEvent, NULL );
213 return myPointIdsMap.empty() || myCellIdsMap.empty();
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 )
223 theVisibleIds.clear();
224 theInVisibleIds.clear();
226 vtkIdType aNumPts = theInput->GetNumberOfPoints();
227 if ( aNumPts < 1 ) return;
229 theVisibleIds.reserve( aNumPts / 2 + 1 );
230 theInVisibleIds.reserve( aNumPts / 2 + 1 );
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();
239 theRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix(
240 theRenderer->GetTiledAspectRatio(), 0, 1 ) );
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] );
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 );
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();
259 theRenderer->GetDisplayPoint( aDX );
262 if ( theMode == RectangleMode ) isInSelection = aDX[0] >= mySelection[0]
263 && aDX[0] <= mySelection[2] && aDX[1] >= mySelection[1]
264 && aDX[1] <= mySelection[3];
266 if ( theMode == PolygonMode ) isInSelection =
267 SVTK_AreaPicker::isPointInPolygon( QPoint( aDX[0], aDX[1] ),
270 // check whether visible and in selection window
271 if ( isInSelection ) {
272 int aDX0 = int( aDX[0] );
273 int aDX1 = int( aDX[1] );
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;
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;
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;
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;
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;
307 if ( false ) ADD_VISIBLE:theVisibleIds.push_back( aPntId );
308 if ( false ) ADD_INVISIBLE:theInVisibleIds.push_back( aPntId );
314 if ( aZPtr ) delete[] aZPtr;
317 void SVTK_AreaPicker::SelectVisibleCells( QVector< QPoint >& thePoints,
318 vtkRenderer *theRenderer, vtkDataSet *theInput,
319 SVTK_AreaPicker::TVectorIds& theVectorIds, double theTolerance,
320 SelectionMode theMode )
322 theVectorIds.clear();
324 vtkIdType aNumCells = theInput->GetNumberOfCells();
325 if ( aNumCells < 1 ) return;
327 theVectorIds.reserve( aNumCells / 2 + 1 );
329 SVTK_AreaPicker::TVectorIds aVisiblePntIds;
330 SVTK_AreaPicker::TVectorIds anInVisiblePntIds;
331 SelectVisiblePoints( thePoints, theRenderer, theInput, aVisiblePntIds,
332 anInVisiblePntIds, theTolerance, theMode );
334 typedef std::set< vtkIdType > TIdsSet;
335 TIdsSet aVisibleIds( aVisiblePntIds.begin(), aVisiblePntIds.end() );
336 TIdsSet anInVisibleIds( anInVisiblePntIds.begin(), anInVisiblePntIds.end() );
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();
345 theRenderer->GetActiveCamera()->GetCompositeProjectionTransformMatrix(
346 theRenderer->GetTiledAspectRatio(), 0, 1 ) );
348 for ( vtkIdType aCellId = 0; aCellId < aNumCells; aCellId++ ) {
349 vtkCell* aCell = theInput->GetCell( aCellId );
352 aCell->GetBounds( aBounds );
355 GetCenter( aBounds, aCenter );
358 double aX[4] = { aCenter[0], aCenter[1], aCenter[2], 1.0 };
359 aMatrix->MultiplyPoint( aX, aView );
361 if ( aView[3] == 0.0 ) continue;
363 theRenderer->SetViewPoint( aView[0] / aView[3], aView[1] / aView[3],
364 aView[2] / aView[3] );
365 theRenderer->ViewToDisplay();
368 theRenderer->GetDisplayPoint( aDX );
371 if ( theMode == RectangleMode ) isInSelection = aDX[0] >= mySelection[0]
372 && aDX[0] <= mySelection[2] && aDX[1] >= mySelection[1]
373 && aDX[1] <= mySelection[3];
375 if ( theMode == PolygonMode ) isInSelection =
376 SVTK_AreaPicker::isPointInPolygon( QPoint( aDX[0], aDX[1] ),
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;
387 if ( anIsVisible ) theVectorIds.push_back( aCellId );
392 bool SVTK_AreaPicker::isPointInPolygon( const QPoint& thePoint,
393 const QVector< QPoint >& thePolygon )
396 if ( thePolygon.size() < 3 ) return false;
398 QVector< QPoint >::const_iterator end = thePolygon.end();
399 QPoint last_pt = thePolygon.back();
401 last_pt.setX( last_pt.x() - thePoint.x() );
402 last_pt.setY( last_pt.y() - thePoint.y() );
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() );
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();
417 ( last_pt.x() * last_pt.x() + last_pt.y() * last_pt.y() - xy )
420 ( cur_pt.x() * cur_pt.x() + cur_pt.y() * cur_pt.y() - xy )
425 return fabs( sum ) > eps;
428 const SVTK_AreaPicker::TVectorIdsMap&
429 SVTK_AreaPicker::GetPointIdsMap() const
431 return myPointIdsMap;
434 const SVTK_AreaPicker::TVectorIdsMap&
435 SVTK_AreaPicker::GetCellIdsMap() const