From 0a440bf0f0f2934705dc3db5b7794ce2b4b56b04 Mon Sep 17 00:00:00 2001 From: Alex Tsui Date: Fri, 22 Jun 2012 16:19:35 +0000 Subject: [PATCH] Prepare to refactor arrangement graphics items. --- .gitattributes | 2 + .../ArrangementDemoTab.h | 2 +- .../ArrangementDemoWindow.h | 2 +- .../ArrangementGraphicsItem.h | 260 +++++++++++ .../CurveGraphicsItem.h | 132 ++++++ .../DeleteCurveCallback.h | 2 +- .../EnvelopeCallback.h | 2 +- .../MergeEdgeCallback.h | 2 +- .../PointLocationCallback.h | 3 +- .../SplitEdgeCallback.h | 2 +- .../demo/Arrangement_on_surface_2/Utils.h | 419 ++++++++++-------- .../VerticalRayShootCallback.h | 3 +- 12 files changed, 633 insertions(+), 198 deletions(-) create mode 100644 Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h create mode 100644 Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h diff --git a/.gitattributes b/.gitattributes index 1c2d29fafdb..d1460964d15 100644 --- a/.gitattributes +++ b/.gitattributes @@ -320,10 +320,12 @@ Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.cpp Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.qrc -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.ui -text +Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementSegmentInputCallback.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementTypes.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Callback.cpp -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/Callback.h -text +Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/DeleteCurveCallback.h -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.cpp -text Arrangement_on_surface_2/demo/Arrangement_on_surface_2/EnvelopeCallback.h -text diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoTab.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoTab.h index 57de2693409..10e17fb9624 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoTab.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoTab.h @@ -3,7 +3,7 @@ #include #include -#include +#include "ArrangementGraphicsItem.h" #include "ArrangementDemoGraphicsView.h" #include "ArrangementSegmentInputCallback.h" #include "DeleteCurveCallback.h" diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.h index 9725e6715ab..0a0dac98c6a 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementDemoWindow.h @@ -2,7 +2,7 @@ #define ARRANGEMENT_DEMO_WINDOW_H #include #include "ui_ArrangementDemoWindow.h" -#include +#include "ArrangementGraphicsItem.h" #include #include "ArrangementTypes.h" #include "ArrangementSegmentInputCallback.h" 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 new file mode 100644 index 00000000000..17b8cf8d52e --- /dev/null +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/ArrangementGraphicsItem.h @@ -0,0 +1,260 @@ +// Copyright (c) 2008 GeometryFactory Sarl (France). +// All rights reserved. +// +// This file is part of CGAL (www.cgal.org). +// You can redistribute it and/or modify it under the terms of the GNU +// General Public License as published by the Free Software Foundation, +// either version 3 of the License, or (at your option) any later version. +// +// Licensees holding a valid commercial license may use this file in +// accordance with the commercial license agreement provided with the software. +// +// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +#ifndef CGAL_QT_ARRANGEMENT_GRAPHICS_ITEM_H +#define CGAL_QT_ARRANGEMENT_GRAPHICS_ITEM_H + +#include +//#include +#include // TODO: should be included in PainterOstream.h +#include +#include +#include + +#include +#include +#include + +namespace CGAL { +namespace Qt { + +class ArrangementGraphicsItemBase : public GraphicsItem +{ + +}; + +template +class ArrangementGraphicsItem : public ArrangementGraphicsItemBase +{ + typedef typename TArr::Geometry_traits_2 Traits; + typedef typename TArr::Vertex_iterator Vertex_iterator; + typedef typename TArr::Edge_iterator Edge_iterator; + typedef typename Traits::Kernel Kernel; + typedef typename Kernel::Point_2 Point_2; + typedef typename Kernel::Segment_2 Segment_2; + +public: + ArrangementGraphicsItem(TArr* t_); + + void modelChanged(); + +public: + // QGraphicsItem overrides + QRectF boundingRect() const; + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + + const QPen& getVerticesPen() const + { + return this->verticesPen; + } + + const QPen& getEdgesPen() const + { + return this->edgesPen; + } + + void setVerticesPen(const QPen& pen) + { + this->verticesPen = pen; + } + + void setEdgesPen(const QPen& pen) + { + this->edgesPen = pen; + } + + bool visibleVertices() const + { + return this->visible_vertices; + } + + void setVisibleVertices(const bool b) + { + this->visible_vertices = b; + this->update(); + } + + bool visibleEdges() const + { + return this->visible_edges; + } + + void setVisibleEdges(const bool b) + { + this->visible_edges = b; + this->update(); + } + +protected: + void updateBoundingBox(); + + TArr* arr; + QPainter* m_painter; + ArrangementPainterOstream< Kernel > painterostream; + + //typename Traits::Point_2 p; + CGAL::Bbox_2 bb; + bool bb_initialized; + QRectF bounding_rect; + + QPen verticesPen; + QPen edgesPen; + bool visible_edges; + bool visible_vertices; + CGAL::Qt::Converter< Traits > convert; +}; + + +template < typename TArr > +ArrangementGraphicsItem< TArr >::ArrangementGraphicsItem( TArr * arr_ ) + : arr( arr_ ), painterostream( 0 ), + bb( 0, 0, 0, 0 ), bb_initialized( false ), + visible_edges( true ), visible_vertices( true ) +{ + this->setVerticesPen( QPen( ::Qt::black, 3. ) ); + this->setEdgesPen( QPen( ::Qt::black, 1. ) ); + if ( this->arr->number_of_vertices() == 0 ) { + this->hide( ); + } + //this->updateBoundingBox( ); + this->setZValue( 3 ); +} + +template +QRectF +ArrangementGraphicsItem< TArr >::boundingRect() const +{ + QRectF rect = this->convert( this->bb ); + return rect; +} + +template +void +ArrangementGraphicsItem< TArr >::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget * /*widget*/) +{ + //painter->drawRect( this->boundingRect( ) ); + + painter->setPen( this->verticesPen ); + this->painterostream = ArrangementPainterOstream< Kernel >( painter, this->boundingRect( ) ); + for ( Vertex_iterator it = this->arr->vertices_begin( ); it != this->arr->vertices_end( ); ++it ) + { + this->painterostream << it->point( ); + } + painter->setPen( this->edgesPen ); + for ( Edge_iterator it = this->arr->edges_begin( ); it != this->arr->edges_end( ); ++it ) + { + Point_2 p1 = it->source( )->point( ); + Point_2 p2 = it->target( )->point( ); + Segment_2 edge( p1, p2 ); + this->painterostream << edge; + } +#if 0 + painter->setPen(this->edgesPen()); +// painter->drawRect(boundingRect()); + if ( t->dimension()<2 || option->exposedRect.contains(boundingRect()) ) { + drawAll(painter); + } else { + m_painter = painter; + painterostream = PainterOstream(painter); + CGAL::apply_to_range (*t, + typename T::Point(option->exposedRect.left(), + option->exposedRect.bottom()), + typename T::Point(option->exposedRect.right(), + option->exposedRect.top()), + *this); + } +#endif +} + +// We let the bounding box only grow, so that when vertices get removed +// the maximal bbox gets refreshed in the GraphicsView +template +void +ArrangementGraphicsItem< TArr >::updateBoundingBox() +{ + this->prepareGeometryChange( ); + if ( this->arr->number_of_vertices( ) == 0 ) + { + this->bb = Bbox_2( 0, 0, 0, 0 ); + this->bb_initialized = false; + return; + } + else + { + this->bb = this->arr->vertices_begin( )->point( ).bbox( ); + this->bb_initialized = true; + } + + for ( Vertex_iterator it = this->arr->vertices_begin( ); + it != this->arr->vertices_end( ); + ++it ) + { + this->bb = this->bb + it->point( ).bbox( ); + } +#if 0 + prepareGeometryChange(); + if(t->number_of_vertices() == 0){ + bb = Bbox_2(0,0,0,0); + bb_initialized = false; + return; + } else if(! bb_initialized){ + bb = t->finite_vertices_begin()->point().bbox(); + bb_initialized = true; + } + + if(t->dimension() <2){ + for(typename T::Finite_vertices_iterator it = t->finite_vertices_begin(); + it != t->finite_vertices_end(); + ++it){ + bb = bb + it->point().bbox(); + } + } else { + typename T::Vertex_handle inf = t->infinite_vertex(); + typename T::Vertex_circulator vc = t->incident_vertices(inf), done(vc); + do { + bb = bb + vc->point().bbox(); + ++vc; + } while(vc != done); + } + bounding_rect = QRectF(bb.xmin(), + bb.ymin(), + bb.xmax()-bb.xmin(), + bb.ymax()-bb.ymin()); +#endif +} + + +template +void +ArrangementGraphicsItem< TArr >::modelChanged() +{ + if ( this->arr->is_empty( ) ) + { + this->hide( ); + } + else + { + this->show( ); + } + this->updateBoundingBox(); + this->update(); +} + + +} // namespace Qt +} // namespace CGAL + +#endif // CGAL_QT_ARRANGEMENT_GRAPHICS_ITEM_H 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 new file mode 100644 index 00000000000..509a8c34b47 --- /dev/null +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/CurveGraphicsItem.h @@ -0,0 +1,132 @@ +#ifndef CGAL_QT_CURVE_GRAPHICS_ITEM_H +#define CGAL_QT_CURVE_GRAPHICS_ITEM_H +#include +#include +#include +namespace CGAL { +namespace Qt { + +template < class ArrTraits > +class CurveGraphicsItem : public GraphicsItem +{ +public: + // known curve types + typedef ArrTraits Traits; + typedef typename Traits::Kernel Kernel; + typedef typename Traits::Curve_2 Curve_2; + typedef typename Traits::X_monotone_curve_2 X_monotone_curve_2; + + CurveGraphicsItem( ); + + virtual void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ); + void updateBoundingBox( ); + void insert( const X_monotone_curve_2& curve ); + void clear( ); + QRectF boundingRect( ) const; + +public slots: + void modelChanged( ); + +protected: + CGAL::Qt::Converter< Kernel > convert; + ArrangementPainterOstream< ArrTraits > painterOstream; + std::vector< X_monotone_curve_2 > curves; + CGAL::Bbox_2 boundingBox; + bool boundingBoxInitialized; +}; // class CurveGraphicsItem + +template < class ArrTraits > +CurveGraphicsItem< ArrTraits >:: +CurveGraphicsItem( ): + painterOstream( 0 ), + boundingBox( 0, 0, 0, 0 ), + boundingBoxInitialized( false ) +{ + this->setZValue( 4 ); +} + +template < class ArrTraits > +void +CurveGraphicsItem< ArrTraits >:: +paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) +{ + painter->setPen( QPen( ::Qt::red, 1. ) ); + this->painterOstream = ArrangementPainterOstream< ArrTraits >( painter/*, clippingRectangle */ ); + for ( int i = 0; i < this->curves.size( ); ++i ) + { + X_monotone_curve_2 curve = this->curves[ i ]; + this->painterOstream << curve; + } +} + +template < class ArrTraits > +void +CurveGraphicsItem< ArrTraits >:: +modelChanged( ) +{ + if ( this->curves.size( ) == 0 ) + { + this->hide( ); + } + else + { + this->show( ); + } + this->updateBoundingBox( ); + this->update( ); +} + +template < class ArrTraits > +void +CurveGraphicsItem< ArrTraits >:: +updateBoundingBox( ) +{ + this->prepareGeometryChange( ); + if ( this->curves.size( ) == 0 ) + { + this->boundingBox = Bbox_2( 0, 0, 0, 0 ); + this->boundingBoxInitialized = false; + return; + } + else + { + this->boundingBox = this->curves[ 0 ].bbox( ); + this->boundingBoxInitialized = true; + } + + for ( int i = 1; i < this->curves.size( ); ++i ) + { + this->boundingBox = this->boundingBox + this->curves[ i ].bbox( ); + } +} + +template < class ArrTraits > +void +CurveGraphicsItem< ArrTraits >:: +insert( const X_monotone_curve_2& segment ) +{ + this->curves.push_back( segment ); +} + +template < class ArrTraits > +void +CurveGraphicsItem< ArrTraits >:: +clear( ) +{ + this->curves.clear( ); +} + +template < class ArrTraits > +QRectF +CurveGraphicsItem< ArrTraits >:: +boundingRect( ) const +{ + QRectF boundingRectangle = this->convert( this->boundingBox ); + return boundingRectangle; +} + + +} // namespace Qt +} // namespace CGAL + +#endif // CGAL_QT_CURVE_GRAPHICS_ITEM_H diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/DeleteCurveCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/DeleteCurveCallback.h index 2e803a72792..c4725678715 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/DeleteCurveCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/DeleteCurveCallback.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include "CurveGraphicsItem.h" #include #include "Utils.h" 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 35cba4a57b9..3153a5c2896 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 @@ -1,7 +1,7 @@ #ifndef ENVELOPE_CALLBACK_H #define ENVELOPE_CALLBACK_H #include "Callback.h" -#include +#include "CurveGraphicsItem.h" #include #include #include diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/MergeEdgeCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/MergeEdgeCallback.h index 1f894c55480..f253e36a8e7 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/MergeEdgeCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/MergeEdgeCallback.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include "CurveGraphicsItem.h" #include #include "Utils.h" diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/PointLocationCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/PointLocationCallback.h index 325804c8f30..09ba55b49a7 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/PointLocationCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/PointLocationCallback.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include "CurveGraphicsItem.h" #include #include #include @@ -58,7 +58,6 @@ protected: CGAL::Object locate( const Point_2& point ); using Callback::scene; - Compute_squared_distance_2< Traits > squaredDistance; CGAL::Qt::Converter< Kernel > convert; CGAL::Object pointLocationStrategy; TArr* arr; diff --git a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/SplitEdgeCallback.h b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/SplitEdgeCallback.h index 5cb63fef56c..4afe4f70a99 100644 --- a/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/SplitEdgeCallback.h +++ b/Arrangement_on_surface_2/demo/Arrangement_on_surface_2/SplitEdgeCallback.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include "CurveGraphicsItem.h" #include #include "Utils.h" #include "ISnappable.h" 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 b240101d399..085a97ed19a 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 @@ -1,6 +1,7 @@ #ifndef CGAL_ARRANGEMENTS_DEMO_UTILS_H #define CGAL_ARRANGEMENTS_DEMO_UTILS_H #include +#include #include #include #include @@ -36,9 +37,9 @@ class Compute_squared_distance_2< CGAL::Arr_segment_traits_2< Kernel_ > > : public Compute_squared_distance_2_base< CGAL::Arr_segment_traits_2< Kernel_ > > { public: - typedef CGAL::Arr_segment_traits_2< Kernel_ > Traits; - typedef Compute_squared_distance_2_base< Traits > Superclass; typedef Kernel_ Kernel; + typedef CGAL::Arr_segment_traits_2< Kernel > Traits; + typedef Compute_squared_distance_2_base< Traits > Superclass; typedef typename Kernel::FT FT; typedef typename Kernel::Point_2 Point_2; typedef typename Kernel::Segment_2 Segment_2; @@ -54,115 +55,157 @@ public: } }; +template < class Kernel_ > +class Compute_squared_distance_2< CGAL::Arr_polyline_traits_2< Kernel_ > > : + public Compute_squared_distance_2_base< CGAL::Arr_polyline_traits_2< Kernel_ > > +{ +public: + typedef Kernel_ Kernel; + typedef CGAL::Arr_polyline_traits_2< Kernel > Traits; + typedef Compute_squared_distance_2_base< Traits > Superclass; + typedef typename Kernel::FT FT; + typedef typename Kernel::Point_2 Point_2; + typedef typename Kernel::Segment_2 Segment_2; + typedef typename Traits::Curve_2 Curve_2; + typedef typename Curve_2::const_iterator Curve_const_iterator; + typedef typename Traits::X_monotone_curve_2 X_monotone_curve_2; + + FT operator() ( const Point_2& p, const X_monotone_curve_2& c ) const + { + Curve_const_iterator ps = c.begin(); + Curve_const_iterator pt = ps; pt++; + bool first = true; + FT min_dist = 0; + + while ( pt != c.end() ) + { + const Point_2& source = *ps; + const Point_2& target = *pt; + Segment_2 seg( source, target ); + FT dist = this->squared_distance( p, seg ); + + if ( first || dist < min_dist ) + { + first = false; + min_dist = dist; + } + ps++; pt++; + } + + return min_dist; + } +}; + + template < class ArrTraits > class Construct_x_monotone_subcurve_2 { -public: - typedef typename ArrTraits::Kernel Kernel; - typedef typename ArrTraits::X_monotone_curve_2 X_monotone_curve_2; - typedef typename ArrTraits::Split_2 Split_2; - typedef typename ArrTraits::Intersect_2 Intersect_2; - typedef typename ArrTraits::Multiplicity Multiplicity; - typedef typename ArrTraits::Construct_x_monotone_curve_2 Construct_x_monotone_curve_2; - typedef typename ArrTraits::Construct_min_vertex_2 Construct_min_vertex_2; - typedef typename ArrTraits::Construct_max_vertex_2 Construct_max_vertex_2; - typedef typename ArrTraits::Compare_x_2 Compare_x_2; - typedef typename Kernel::FT FT; - typedef typename Kernel::Point_2 Point_2; - typedef typename Kernel::Line_2 Line_2; - typedef typename Kernel::Compute_y_at_x_2 Compute_y_at_x_2; + public: + typedef typename ArrTraits::Kernel Kernel; + typedef typename ArrTraits::X_monotone_curve_2 X_monotone_curve_2; + typedef typename ArrTraits::Split_2 Split_2; + typedef typename ArrTraits::Intersect_2 Intersect_2; + typedef typename ArrTraits::Multiplicity Multiplicity; + typedef typename ArrTraits::Construct_x_monotone_curve_2 Construct_x_monotone_curve_2; + typedef typename ArrTraits::Construct_min_vertex_2 Construct_min_vertex_2; + typedef typename ArrTraits::Construct_max_vertex_2 Construct_max_vertex_2; + typedef typename ArrTraits::Compare_x_2 Compare_x_2; + typedef typename Kernel::FT FT; + typedef typename Kernel::Point_2 Point_2; + typedef typename Kernel::Line_2 Line_2; + typedef typename Kernel::Compute_y_at_x_2 Compute_y_at_x_2; - /* - Return the subcurve of curve bracketed by pLeft and pRight. + /* + Return the subcurve of curve bracketed by pLeft and pRight. - We assume pLeft and pRight don't lie on the curve and always do a vertical - projection. - */ - X_monotone_curve_2 operator() ( const X_monotone_curve_2& curve, const Point_2& pLeft, const Point_2& pRight ) - { - Point_2 pMin = this->construct_min_vertex_2( curve ); - Point_2 pMax = this->construct_max_vertex_2( curve ); - X_monotone_curve_2 subcurve; - X_monotone_curve_2 unusedTrimmings; - X_monotone_curve_2 finalSubcurve; - if ( this->compare_x_2( pLeft, pMin ) == CGAL::LARGER ) + We assume pLeft and pRight don't lie on the curve and always do a vertical + projection. + */ + X_monotone_curve_2 operator() ( const X_monotone_curve_2& curve, const Point_2& pLeft, const Point_2& pRight ) { - // FIXME: handle vertical lines properly - CGAL::Bbox_2 c_bbox = curve.bbox( ); - FT splitLineYMin( c_bbox.ymin( ) - 1.0 ); - FT splitLineYMax( c_bbox.ymax( ) + 1.0 ); - Point_2 splitLinePBottom( pLeft.x( ), splitLineYMin ); - Point_2 splitLinePTop( pLeft.x( ), splitLineYMax ); - X_monotone_curve_2 splitLine = - this->construct_x_monotone_curve_2( splitLinePBottom, splitLinePTop ); - CGAL::Object res; - CGAL::Oneset_iterator< CGAL::Object > oi( res ); - this->intersect_2( splitLine, curve, oi ); - std::pair< Point_2, Multiplicity > pair; - if ( CGAL::assign( pair, res ) ) + Point_2 pMin = this->construct_min_vertex_2( curve ); + Point_2 pMax = this->construct_max_vertex_2( curve ); + X_monotone_curve_2 subcurve; + X_monotone_curve_2 unusedTrimmings; + X_monotone_curve_2 finalSubcurve; + if ( this->compare_x_2( pLeft, pMin ) == CGAL::LARGER ) { - Point_2 splitPoint = pair.first; - this->split_2( curve, splitPoint, unusedTrimmings, subcurve ); + // FIXME: handle vertical lines properly + CGAL::Bbox_2 c_bbox = curve.bbox( ); + FT splitLineYMin( c_bbox.ymin( ) - 1.0 ); + FT splitLineYMax( c_bbox.ymax( ) + 1.0 ); + Point_2 splitLinePBottom( pLeft.x( ), splitLineYMin ); + Point_2 splitLinePTop( pLeft.x( ), splitLineYMax ); + X_monotone_curve_2 splitLine = + this->construct_x_monotone_curve_2( splitLinePBottom, splitLinePTop ); + CGAL::Object res; + CGAL::Oneset_iterator< CGAL::Object > oi( res ); + this->intersect_2( splitLine, curve, oi ); + std::pair< Point_2, Multiplicity > pair; + if ( CGAL::assign( pair, res ) ) + { + Point_2 splitPoint = pair.first; + this->split_2( curve, splitPoint, unusedTrimmings, subcurve ); + } } - } - else - { - subcurve = curve; - } - - if ( this->compare_x_2( pRight, pMax ) == CGAL::SMALLER ) - { - CGAL::Bbox_2 c_bbox = subcurve.bbox( ); - FT splitLineYMin( c_bbox.ymin( ) - 1.0 ); - FT splitLineYMax( c_bbox.ymax( ) + 1.0 ); - Point_2 splitLinePBottom( pRight.x( ), splitLineYMin ); - Point_2 splitLinePTop( pRight.x( ), splitLineYMax ); - X_monotone_curve_2 splitLine = - this->construct_x_monotone_curve_2( splitLinePBottom, splitLinePTop ); - CGAL::Object res; - CGAL::Oneset_iterator< CGAL::Object > oi( res ); - this->intersect_2( splitLine, subcurve, oi ); - std::pair< Point_2, Multiplicity > pair; - if ( CGAL::assign( pair, res ) ) + else { - Point_2 splitPoint = pair.first; - //return X_monotone_curve_2( splitLinePBottom, splitPoint ); - this->split_2( subcurve, splitPoint, finalSubcurve, unusedTrimmings ); + subcurve = curve; } - } - else - { - finalSubcurve = subcurve; + + if ( this->compare_x_2( pRight, pMax ) == CGAL::SMALLER ) + { + CGAL::Bbox_2 c_bbox = subcurve.bbox( ); + FT splitLineYMin( c_bbox.ymin( ) - 1.0 ); + FT splitLineYMax( c_bbox.ymax( ) + 1.0 ); + Point_2 splitLinePBottom( pRight.x( ), splitLineYMin ); + Point_2 splitLinePTop( pRight.x( ), splitLineYMax ); + X_monotone_curve_2 splitLine = + this->construct_x_monotone_curve_2( splitLinePBottom, splitLinePTop ); + CGAL::Object res; + CGAL::Oneset_iterator< CGAL::Object > oi( res ); + this->intersect_2( splitLine, subcurve, oi ); + std::pair< Point_2, Multiplicity > pair; + if ( CGAL::assign( pair, res ) ) + { + Point_2 splitPoint = pair.first; + //return X_monotone_curve_2( splitLinePBottom, splitPoint ); + this->split_2( subcurve, splitPoint, finalSubcurve, unusedTrimmings ); + } + } + else + { + finalSubcurve = subcurve; + } + + return finalSubcurve; } - return finalSubcurve; - } + protected: -protected: - - Intersect_2 intersect_2; - Split_2 split_2; - Compare_x_2 compare_x_2; - Construct_x_monotone_curve_2 construct_x_monotone_curve_2; - Construct_min_vertex_2 construct_min_vertex_2; - Construct_max_vertex_2 construct_max_vertex_2; + Intersect_2 intersect_2; + Split_2 split_2; + Compare_x_2 compare_x_2; + Construct_x_monotone_curve_2 construct_x_monotone_curve_2; + Construct_min_vertex_2 construct_min_vertex_2; + Construct_max_vertex_2 construct_max_vertex_2; }; template < class K_ > class SnapStrategy { -public: - typedef K_ Kernel; - typedef typename Kernel::Point_2 Point_2; + public: + typedef K_ Kernel; + typedef typename Kernel::Point_2 Point_2; - virtual Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) = 0; - void setScene( QGraphicsScene* scene_ ); + virtual Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) = 0; + void setScene( QGraphicsScene* scene_ ); -protected: - SnapStrategy( QGraphicsScene* scene_ ); - QRectF viewportRect( ) const; + protected: + SnapStrategy( QGraphicsScene* scene_ ); + QRectF viewportRect( ) const; - QGraphicsScene* scene; + QGraphicsScene* scene; }; // class SnapStrategy template < class K_ > @@ -207,134 +250,134 @@ setScene( QGraphicsScene* scene_ ) template < class K_ > class SnapToGridStrategy : public SnapStrategy< K_ > { -public: - typedef K_ Kernel; - typedef typename Kernel::Point_2 Point_2; - typedef SnapStrategy< Kernel > Superclass; + public: + typedef K_ Kernel; + typedef typename Kernel::Point_2 Point_2; + typedef SnapStrategy< Kernel > Superclass; - SnapToGridStrategy( ): - Superclass( NULL ), - gridSize( 50 ) + SnapToGridStrategy( ): + Superclass( NULL ), + gridSize( 50 ) { } - SnapToGridStrategy( QGraphicsScene* scene ): - Superclass( scene ), - gridSize( 50 ) + SnapToGridStrategy( QGraphicsScene* scene ): + Superclass( scene ), + gridSize( 50 ) { } - Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) - { - QPointF clickedPoint = event->scenePos( ); - QRectF viewportRect = this->viewportRect( ); - if ( viewportRect == QRectF( ) ) + Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) { - return this->convert( event->scenePos( ) ); - } - - qreal d( this->gridSize / 2.0 ); - int left = int( viewportRect.left( ) ) - (int( viewportRect.left( ) ) % this->gridSize); - int right = int( viewportRect.right( ) ) + (this->gridSize - int( viewportRect.right( ) ) % this->gridSize); - int x = clickedPoint.x( ); - int y = clickedPoint.y( ); - for ( int i = left - this->gridSize; i <= right; i += this->gridSize ) - { - if ( i - d <= clickedPoint.x( ) && clickedPoint.x( ) <= i + d ) + QPointF clickedPoint = event->scenePos( ); + QRectF viewportRect = this->viewportRect( ); + if ( viewportRect == QRectF( ) ) { - x = i; - break; + return this->convert( event->scenePos( ) ); } - } - int top = int( viewportRect.top( ) ) - (int( viewportRect.top( ) ) % this->gridSize); - int bottom = int( viewportRect.bottom( ) ) + (this->gridSize - int( viewportRect.bottom( ) ) % this->gridSize); - for ( int i = top - this->gridSize; i <= bottom; i += this->gridSize ) - { - if ( i - d <= clickedPoint.y( ) && clickedPoint.y( ) <= i + d ) + + qreal d( this->gridSize / 2.0 ); + int left = int( viewportRect.left( ) ) - (int( viewportRect.left( ) ) % this->gridSize); + int right = int( viewportRect.right( ) ) + (this->gridSize - int( viewportRect.right( ) ) % this->gridSize); + int x = clickedPoint.x( ); + int y = clickedPoint.y( ); + for ( int i = left - this->gridSize; i <= right; i += this->gridSize ) { - y = i; - break; + if ( i - d <= clickedPoint.x( ) && clickedPoint.x( ) <= i + d ) + { + x = i; + break; + } } + int top = int( viewportRect.top( ) ) - (int( viewportRect.top( ) ) % this->gridSize); + int bottom = int( viewportRect.bottom( ) ) + (this->gridSize - int( viewportRect.bottom( ) ) % this->gridSize); + for ( int i = top - this->gridSize; i <= bottom; i += this->gridSize ) + { + if ( i - d <= clickedPoint.y( ) && clickedPoint.y( ) <= i + d ) + { + y = i; + break; + } + } + return this->convert( QPointF( x, y ) ); } - return this->convert( QPointF( x, y ) ); - } - void setGridSize( int size ) - { - this->gridSize = size; - } + void setGridSize( int size ) + { + this->gridSize = size; + } -protected: - int gridSize; - CGAL::Qt::Converter< Kernel > convert; + protected: + int gridSize; + CGAL::Qt::Converter< Kernel > convert; }; // class SnapToGridStrategy template < class Arr_ > class SnapToArrangementVertexStrategy: public SnapStrategy< typename Arr_::Geometry_traits_2::Kernel > { -public: - typedef Arr_ Arrangement; - typedef typename Arrangement::Geometry_traits_2 Traits; - typedef typename Traits::Kernel Kernel; - typedef SnapStrategy< Kernel > Superclass; - typedef typename Arrangement::Vertex_iterator Vertex_iterator; - typedef typename Kernel::Compute_squared_distance_2 Compute_squared_distance_2; - typedef typename Kernel::FT FT; - typedef typename Kernel::Point_2 Point_2; + public: + typedef Arr_ Arrangement; + typedef typename Arrangement::Geometry_traits_2 Traits; + typedef typename Traits::Kernel Kernel; + typedef SnapStrategy< Kernel > Superclass; + typedef typename Arrangement::Vertex_iterator Vertex_iterator; + typedef typename Kernel::Compute_squared_distance_2 Compute_squared_distance_2; + typedef typename Kernel::FT FT; + typedef typename Kernel::Point_2 Point_2; - SnapToArrangementVertexStrategy( ): - Superclass( NULL ), - arrangement( NULL ) + SnapToArrangementVertexStrategy( ): + Superclass( NULL ), + arrangement( NULL ) { } - SnapToArrangementVertexStrategy( Arrangement* arr, QGraphicsScene* scene_ ): - Superclass( scene_ ), - arrangement( arr ) + SnapToArrangementVertexStrategy( Arrangement* arr, QGraphicsScene* scene_ ): + Superclass( scene_ ), + arrangement( arr ) { } - Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) - { - Point_2 clickedPoint = this->convert( event->scenePos( ) ); - Point_2 closestPoint = clickedPoint; - bool first = true; - FT minDist( 0 ); - QRectF viewportRect = this->viewportRect( ); - if ( viewportRect == QRectF( ) ) + Point_2 snapPoint( QGraphicsSceneMouseEvent* event ) { - return this->convert( event->scenePos( ) ); - } - - FT maxDist( ( viewportRect.right( ) - viewportRect.left( ) ) / 4.0 ); - for ( Vertex_iterator vit = this->arrangement->vertices_begin( ); - vit != this->arrangement->vertices_end( ); ++vit ) - { - Point_2 point = vit->point( ); - FT dist = this->compute_squared_distance_2( clickedPoint, point ); - if ( first || ( dist < minDist ) ) + Point_2 clickedPoint = this->convert( event->scenePos( ) ); + Point_2 closestPoint = clickedPoint; + bool first = true; + FT minDist( 0 ); + QRectF viewportRect = this->viewportRect( ); + if ( viewportRect == QRectF( ) ) { - first = false; - minDist = dist; - closestPoint = point; + return this->convert( event->scenePos( ) ); + } + + FT maxDist( ( viewportRect.right( ) - viewportRect.left( ) ) / 4.0 ); + for ( Vertex_iterator vit = this->arrangement->vertices_begin( ); + vit != this->arrangement->vertices_end( ); ++vit ) + { + Point_2 point = vit->point( ); + FT dist = this->compute_squared_distance_2( clickedPoint, point ); + if ( first || ( dist < minDist ) ) + { + first = false; + minDist = dist; + closestPoint = point; + } + } + if ( ! first && minDist < maxDist ) + { + return closestPoint; + } + else + { + return this->convert( event->scenePos( ) ); } } - if ( ! first && minDist < maxDist ) - { - return closestPoint; - } - else - { - return this->convert( event->scenePos( ) ); - } - } - void setArrangement( Arrangement* arr ) - { - this->arrangement = arr; - } + void setArrangement( Arrangement* arr ) + { + this->arrangement = arr; + } -protected: - Arrangement* arrangement; - Compute_squared_distance_2 compute_squared_distance_2; - CGAL::Qt::Converter< Kernel > convert; + protected: + Arrangement* arrangement; + Compute_squared_distance_2 compute_squared_distance_2; + CGAL::Qt::Converter< Kernel > convert; }; // class SnapToArrangementVertexStrategy #endif // CGAL_ARRANGEMENTS_DEMO_UTILS_H 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 a4e5a7907ca..59612bbabf4 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 @@ -7,7 +7,7 @@ #include #include #include -#include +#include "CurveGraphicsItem.h" #include #include #include @@ -80,7 +80,6 @@ protected: using Superclass::scene; using Superclass::shootingUp; TArr* arr; - Compute_squared_distance_2< Traits > squaredDistance; Construct_x_monotone_curve_2 construct_x_monotone_curve_2; Intersect_2 intersectCurves; CGAL::Qt::Converter< Kernel > convert;