From d285a87d87c7f35aed3699c61bf859cdbaa92927 Mon Sep 17 00:00:00 2001 From: Alex Tsui Date: Mon, 20 Aug 2012 18:31:02 +0000 Subject: [PATCH] Various fixes/workarounds to fix crashing in algebraic traits arrangements. --- .../ArrangementDemoWindow.cpp | 16 +++++- .../ArrangementGraphicsItem.h | 16 ++++-- .../ArrangementPainterOstream.h | 7 +-- .../CurveGraphicsItem.h | 1 + .../EnvelopeCallback.h | 11 ++++ .../demo/Arrangement_on_surface_2/Utils.h | 51 ++++++++++++++++++- .../VerticalRayShootCallback.h | 2 +- 7 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.cpp b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.cpp index 64d22a4f32f..a9fea25a648 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.cpp +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.cpp @@ -631,11 +631,23 @@ on_actionOpen_triggered( ) QRectF bb = agi->boundingRect( ); QGraphicsView* view = currentTab->getView( ); std::cout << bb.left( ) << " " << bb.bottom( ) << ", " << bb.right( ) << " " << bb.top( ) << std::endl; + if ( std::isinf(bb.left( )) || + std::isinf(bb.right( )) || + std::isinf(bb.top( )) || + std::isinf(bb.bottom( )) ) + { + std::cout << "unbounded; using default bb" << std::endl; + bb = QRectF( -100, -100, 200, 200 ); + view->setSceneRect( bb ); + } + else + { + view->fitInView( bb, ::Qt::KeepAspectRatio ); + view->setSceneRect( bb ); + } #if 0 view->centerOn( bb.center( ) ); #endif - view->fitInView( bb, ::Qt::KeepAspectRatio ); - view->setSceneRect( bb ); } void diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h index 116c8820121..ee5b6b5561a 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h @@ -197,7 +197,16 @@ ArrangementGraphicsItem< Arr_, ArrTraits >:: paint( QPainter* painter, CGAL::Arr_algebraic_segment_traits_2< Coefficient_ > traits ) { painter->setPen( this->verticesPen ); - this->painterostream = ArrangementPainterOstream< Traits >( painter, this->boundingRect( ) ); + QRectF clipRect = this->boundingRect( ); + if ( std::isinf(clipRect.left( )) || + std::isinf(clipRect.right( )) || + std::isinf(clipRect.top( )) || + std::isinf(clipRect.bottom( )) ) + { + clipRect = this->getViewportRect( ); + } + + this->painterostream = ArrangementPainterOstream< Traits >( painter, clipRect ); this->painterostream.setScene( this->scene ); for ( Vertex_iterator it = this->arr->vertices_begin( ); it != this->arr->vertices_end( ); ++it ) @@ -270,8 +279,9 @@ updateBoundingBox( CGAL::Arr_algebraic_segment_traits_2< Coefficient_ > traits ) } else { - std::pair< double, double > approx = this->arr->vertices_begin( )->point( ).to_double( ); - this->bb = CGAL::Bbox_2( approx.first, approx.second, approx.first, approx.second ); + //std::pair< double, double > approx = this->arr->vertices_begin( )->point( ).to_double( ); + //this->bb = CGAL::Bbox_2( approx.first, approx.second, approx.first, approx.second ); + this->bb = CGAL::Bbox_2( 0, 0, 0, 0 ); this->bb_initialized = true; } typename Traits::Make_x_monotone_2 make_x_monotone_2 = traits.make_x_monotone_2_object( ); diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementPainterOstream.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementPainterOstream.h index 3ba78d557e8..6e082892f89 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementPainterOstream.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementPainterOstream.h @@ -773,7 +773,7 @@ public: // constructors public: // methods ArrangementPainterOstream& operator<<( const X_monotone_curve_2& curve ) { - std::cout << "paint curve stub (alg traits)" << std::endl; + //std::cout << "paint curve stub (alg traits)" << std::endl; typedef Curve_renderer_facade Facade; typedef std::pair< int, int > Coord_2; typedef std::vector< Coord_2 > Coord_vec_2; @@ -826,6 +826,7 @@ public: // methods vit++; coord = QPoint( vit->first, vit->second ); qpt = view->mapToScene( coord ); + //std::cout << vit->first << " " << vit->second << std::endl; } this->qp->drawPath( path ); @@ -854,7 +855,7 @@ public: // methods { typedef Curve_renderer_facade Facade; std::pair< int, int > coord; - std::cout << "draw point stub" << std::endl; + //std::cout << "draw point stub" << std::endl; this->setupFacade( ); @@ -863,7 +864,7 @@ public: // methods } else { - std::cout << coord.first << " " << coord.second << std::endl; + //std::cout << coord.first << " " << coord.second << std::endl; QPoint coords( coord.first, coord.second ); QGraphicsView* view = this->scene->views( ).first( ); QPointF qpt = view->mapToScene( coords ); diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h index 3ea08319ad0..b9484d3270a 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h @@ -60,6 +60,7 @@ paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widge painter->setPen( QPen( ::Qt::red, 0. ) ); QRectF clippingRectangle = this->viewportRect( ); this->painterOstream = ArrangementPainterOstream< Traits >( painter, clippingRectangle ); + this->painterOstream.setScene( this->getScene( ) ); for ( int i = 0; i < this->curves.size( ); ++i ) { X_monotone_curve_2 curve = this->curves[ i ]; diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.h index e8fb068faa6..fd7e47f1a9f 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.h @@ -74,6 +74,9 @@ protected: template < class CircularKernel > void updateEnvelope( bool lower, CGAL::Arr_circular_arc_traits_2< CircularKernel > traits ); + template < class Coefficient_ > + void updateEnvelope( bool lower, CGAL::Arr_algebraic_segment_traits_2< Coefficient_ > traits ); + Construct_x_monotone_subcurve_2< Traits > construct_x_monotone_subcurve_2; Arrangement* arr; CGAL::Qt::CurveGraphicsItem< Traits >* lowerEnvelope; @@ -272,6 +275,14 @@ updateEnvelope( bool lower, CGAL::Arr_circular_arc_traits_2< CircularKernel > tr envelopeToUpdate->modelChanged( ); } +template < class Arr_, class Traits > +template < class Coefficient_ > +void +EnvelopeCallback< Arr_, Traits >:: +updateEnvelope( bool lower, CGAL::Arr_algebraic_segment_traits_2< Coefficient_ > traits ) +{ + std::cout << "alg seg envelope stub" << std::endl; +} template < class Arr_, class Traits > void diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Utils.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Utils.h index a177b1d8ada..4c688840a63 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Utils.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Utils.h @@ -440,6 +440,11 @@ public: return this->operator()( curve, x, this->traits, category ); } + double approx( const X_monotone_curve_2& curve, const CoordinateType& x ) + { + return CGAL::to_double( (*this)( curve, x ) ); + } + protected: template < class TTraits > CoordinateType operator() ( const X_monotone_curve_2& curve, const CoordinateType& x, TTraits traits_, CGAL::Arr_oblivious_side_tag ) @@ -495,6 +500,7 @@ protected: return res; } + protected: Traits traits; Intersect_2 intersectCurves; @@ -543,6 +549,11 @@ public: return res; } + double approx( const X_monotone_curve_2& curve, const FT& x ) + { + return CGAL::to_double( (*this)( curve, x ) ); + } + // FIXME: inexact projection Root_of_2 operator() ( const X_monotone_curve_2& curve, const Root_of_2& x ) { @@ -550,6 +561,11 @@ public: return this->operator()( curve, approx ); } + double approx( const X_monotone_curve_2& curve, const Root_of_2& x ) + { + return CGAL::to_double( (*this)( curve, x ) ); + } + protected: Traits traits; Intersect_2 intersectCurves; @@ -575,8 +591,39 @@ public: X_monotone_curve_2 c2 = this->makeVerticalLine( x ); intersect( curve, c2, oi ); std::pair< Point_2, Multiplicity > res; - CGAL::assign( res, o ); // TODO: handle failure case - return res.first.y( ); + if ( CGAL::assign( res, o ) ) // TODO: handle failure case + { + Point_2 p = res.first; + std::cout << "approx y: " << p.to_double( ).second << std::endl; + CoordinateType coord = p.y( ); + return coord; + } + else + { + std::cout << "Warning: vertical projection failed" << std::endl; + return CoordinateType( 0 ); + } + } + + double approx( const X_monotone_curve_2& curve, const CoordinateType& x ) + { + CGAL::Object o; + CGAL::Oneset_iterator< CGAL::Object > oi( o ); + Intersect_2 intersect = traits.intersect_2_object( ); + X_monotone_curve_2 c2 = this->makeVerticalLine( x ); + intersect( curve, c2, oi ); + std::pair< Point_2, Multiplicity > res; + if ( CGAL::assign( res, o ) ) // TODO: handle failure case + { + Point_2 p = res.first; + std::pair< double, double > tmp = p.to_double(); + return tmp.second; + } + else + { + std::cout << "Warning: vertical projection failed" << std::endl; + return 0; + } } protected: diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/VerticalRayShootCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/VerticalRayShootCallback.h index 214f02294ec..abc1b851762 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/VerticalRayShootCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/VerticalRayShootCallback.h @@ -209,7 +209,7 @@ highlightPointLocation( QGraphicsSceneMouseEvent* event ) Arr_compute_y_at_x_2< Traits > compute_y_at_x_2; compute_y_at_x_2.setScene( this->getScene( ) ); CoordinateType x( this->queryPt.x( ) ); - double yApprox = CGAL::to_double( compute_y_at_x_2( halfedge->curve( ), x ) ); + double yApprox = CGAL::to_double( compute_y_at_x_2.approx( halfedge->curve( ), x ) ); FT yInt( yApprox ); Kernel_point_2 p2( this->queryPt.x( ), yInt ); Segment_2 seg( this->queryPt, p2 );