+ if ( myRectBand ) {
+ myRectBand->clearGeometry();
+ myRectBand->hide();
+ }
+}
+
+bool isIntersect( const QPoint& theStart1, const QPoint& theEnd1,
+ const QPoint& theStart2, const QPoint& theEnd2 )
+{
+ if ( ( theStart1 == theStart2 && theEnd1 == theEnd2 ) ||
+ ( theStart1 == theEnd2 && theEnd1 == theStart2 ) )
+ return true;
+
+ if ( theStart1 == theStart2 || theStart2 == theEnd1 ||
+ theStart1 == theEnd2 || theEnd1 == theEnd2 )
+ return false;
+
+ double x11 = theStart1.x() * 1.0;
+ double x12 = theEnd1.x() * 1.0;
+ double y11 = theStart1.y() * 1.0;
+ double y12 = theEnd1.y() * 1.0;
+
+ double x21 = theStart2.x() * 1.0;
+ double x22 = theEnd2.x() * 1.0;
+ double y21 = theStart2.y() * 1.0;
+ double y22 = theEnd2.y() * 1.0;
+
+ double k1 = x12 == x11 ? 0 : ( y12 - y11 ) / ( x12 - x11 );
+ double k2 = x22 == x21 ? 0 : ( y22 - y21 ) / ( x22 - x21 );
+
+ double b1 = y11 - k1 * x11;
+ double b2 = y21 - k2 * x21;
+
+ if ( k1 == k2 )
+ {
+ if ( b1 != b2 )
+ return false;
+ else
+ return !( ( qMax( x11, x12 ) <= qMin( x21, x22 ) ||
+ qMin( x11, x12 ) >= qMax( x21, x22 ) ) &&
+ ( qMax( y11, y12 ) <= qMin( y21, y22 ) ||
+ qMin( y11, y12 ) >= qMax( y21, y22 ) ) );
+ }
+ else
+ {
+ double x0 = ( b2 - b1 ) / ( k1 - k2 );
+ double y0 = ( k1 * b2 - k2 * b1 ) / ( k1 - k2 );
+
+ if ( qMin( x11, x12 ) < x0 && x0 < qMax( x11, x12 ) &&
+ qMin( y11, y12 ) < y0 && y0 < qMax( y11, y12 ) &&
+ qMin( x21, x22 ) < x0 && x0 < qMax( x21, x22 ) &&
+ qMin( y21, y22 ) < y0 && y0 < qMax( y21, y22 ) )
+ return true;
+ }
+ return false;
+}
+
+bool isValid( const QPolygon* thePoints, const QPoint& theCurrent )
+{
+ if ( !thePoints->count() )
+ return true;
+
+ if ( thePoints->count() == 1 && thePoints->point( 0 ) == theCurrent )
+ return false;
+
+ const QPoint& aLast = thePoints->point( thePoints->count() - 1 );
+
+ if ( aLast == theCurrent )
+ return true;
+
+ bool res = true;
+ for ( uint i = 0; i < thePoints->count() - 1 && res; i++ )
+ {
+ const QPoint& aStart = thePoints->point( i );
+ const QPoint& anEnd = thePoints->point( i + 1 );
+ res = !isIntersect( aStart, anEnd, theCurrent, aLast );
+ }
+ return res;
+}
+
+/*!
+ Draws polygon
+*/
+void SVTK_InteractorStyle::drawPolygon()
+{
+ QSize aToler( 5, 5 );
+ if ( !myPolygonBand ) {
+ myPolygonBand = new QtxPolyRubberBand( GetRenderWidget() );
+ QPalette palette;
+ palette.setColor( myPolygonBand->foregroundRole(), Qt::white );
+ myPolygonBand->setPalette( palette );
+ myPolygonPoints.append( QPoint( myPoint.x(), myPoint.y() ) );
+ }
+ myPolygonBand->hide();
+
+ bool closed = false;
+ bool valid = GetRenderWidget()->rect().contains( QPoint( myOtherPoint.x(), myOtherPoint.y() ) );
+ if ( !myPolygonPoints.at(0).isNull() )
+ {
+ QRect aRect( myPolygonPoints.at(0).x() - aToler.width(), myPolygonPoints.at(0).y() - aToler.height(),
+ 2 * aToler.width(), 2 * aToler.height() );
+ closed = aRect.contains( QPoint( myOtherPoint.x(), myOtherPoint.y() ) );
+ }
+
+ QPolygon* points = new QPolygon( myPolygonPoints );
+ valid = valid && isValid( points, QPoint( myOtherPoint.x(), myOtherPoint.y() ) );
+ myPoligonState = valid ? InProcess : NotValid;
+ delete points;
+ if ( closed && !valid )
+ closed = false;
+
+ if ( closed && myPolygonPoints.size() > 2 ) {
+ GetRenderWidget()->setCursor( Qt::CrossCursor );
+ myPoligonState = Closed;
+ }
+ else if ( valid )
+ GetRenderWidget()->setCursor( Qt::PointingHandCursor );
+ else
+ GetRenderWidget()->setCursor( Qt::ForbiddenCursor );
+
+ myPolygonPoints.append( QPoint( myOtherPoint.x(), myOtherPoint.y() ) );
+
+ QPolygon aPolygon( myPolygonPoints );
+ myPolygonBand->initGeometry( aPolygon );
+ myPolygonBand->setVisible( true );
+
+ if ( myPolygonPoints.size() > 1 ) {
+ myPolygonPoints.remove( myPolygonPoints.size() - 1 );
+ }
+}
+
+/*!
+ \brief Delete rubber band on the end on the dragging operation.
+*/
+void SVTK_InteractorStyle::endDrawPolygon()
+{
+ if ( myPolygonBand ) myPolygonBand->hide();
+
+ delete myPolygonBand;
+ myPolygonBand = 0;