// ====================================================================== // // Copyright (c) 1999 The CGAL Consortium // // This software and related documentation is part of an INTERNAL release // of the Computational Geometry Algorithms Library (CGAL). It is not // intended for general use. // // ---------------------------------------------------------------------- // // release : // release_date : // // file : LineH2.h // package : H2 // revision : $Revision$ // revision_date : $Date$ // author(s) : Stefan Schirra // // // coordinator : MPI, Saarbruecken () // ====================================================================== #ifndef CGAL_LINEH2_H #define CGAL_LINEH2_H #include CGAL_BEGIN_NAMESPACE template < class R_ > class LineH2 : public R_::Line_handle_2 { public: typedef R_ R; typedef typename R::FT FT; typedef typename R::RT RT; typedef typename R::Line_handle_2 Line_handle_2_; typedef typename Line_handle_2_::element_type Line_ref_2; LineH2(); LineH2(const PointH2& p, const PointH2& q); LineH2(const RT& a, const RT& b, const RT& c); LineH2(const SegmentH2& s); LineH2(const RayH2& r); LineH2(const PointH2& p, const DirectionH2& d); bool operator==(const LineH2& l) const ; bool operator!=(const LineH2& l) const ; const RT & a() const { return Ptr()->e0; } const RT & b() const { return Ptr()->e1; } const RT & c() const { return Ptr()->e2; } FT x_at_y(FT y) const; FT y_at_x(FT x) const; LineH2 perpendicular(const PointH2& p ) const; LineH2 opposite() const; PointH2 point() const; PointH2 point(int i) const; PointH2 projection(const PointH2& p) const; DirectionH2 direction() const; Oriented_side oriented_side( const PointH2& p ) const; bool has_on( const PointH2& p ) const; bool has_on_boundary( const PointH2& p ) const; bool has_on_positive_side( const PointH2& p ) const; bool has_on_negative_side( const PointH2& p ) const; bool is_horizontal() const; bool is_vertical() const; bool is_degenerate() const; LineH2 transform(const Aff_transformationH2&) const; }; #ifdef CGAL_CFG_TYPENAME_BUG #define typename #endif template < class R > CGAL_KERNEL_CTOR_INLINE LineH2::LineH2() : Line_handle_2_ ( Line_ref_2() ) {} template < class R > CGAL_KERNEL_CTOR_MEDIUM_INLINE LineH2::LineH2(const PointH2& p, const PointH2& q) : Line_handle_2_ ( Line_ref_2( // a() * X + b() * Y + c() * W() == 0 // | X Y W | // | p.hx() p.hy() p.hw() | // | q.hx() q.hy() q.hw() | p.hy()*q.hw() - p.hw()*q.hy(), p.hw()*q.hx() - p.hx()*q.hw(), p.hx()*q.hy() - p.hy()*q.hx() )) {} template < class R > CGAL_KERNEL_CTOR_INLINE LineH2::LineH2(const RT& a, const RT& b, const RT& c) : Line_handle_2_ ( Line_ref_2(a,b,c) ) {} template < class R > CGAL_KERNEL_CTOR_INLINE LineH2::LineH2(const SegmentH2& s) { PointH2 p = s.start(); PointH2 q = s.end(); initialize_with( Line_ref_2 ( p.hy()*q.hw() - p.hw()*q.hy(), p.hw()*q.hx() - p.hx()*q.hw(), p.hx()*q.hy() - p.hy()*q.hx() ) ); } template < class R > CGAL_KERNEL_CTOR_INLINE LineH2::LineH2(const RayH2& r) { PointH2 p = r.start(); PointH2 q = r.second_point(); initialize_with( Line_ref_2 ( p.hy()*q.hw() - p.hw()*q.hy(), p.hw()*q.hx() - p.hx()*q.hw(), p.hx()*q.hy() - p.hy()*q.hx() ) ); } template < class R > CGAL_KERNEL_CTOR_INLINE LineH2::LineH2(const PointH2& p, const DirectionH2& d) { PointH2 q = p + VectorH2(d); initialize_with( Line_ref_2 ( p.hy()*q.hw() - p.hw()*q.hy(), p.hw()*q.hx() - p.hx()*q.hw(), p.hx()*q.hy() - p.hy()*q.hx() ) ); } template < class R > CGAL_KERNEL_INLINE typename LineH2::FT LineH2::x_at_y(FT y) const { CGAL_kernel_precondition( !is_degenerate() ); return (FT(-b())*y - FT(c()) )/FT(a()); } template < class R > CGAL_KERNEL_INLINE typename LineH2::FT LineH2::y_at_x(FT x) const { CGAL_kernel_precondition( !is_degenerate() ); return (FT(-a())*x - FT(c()) )/FT(b()); } template < class R > CGAL_KERNEL_INLINE LineH2 LineH2::perpendicular(const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); return LineH2( -b()*p.hw(), a()*p.hw(), b()*p.hx() - a()*p.hy() ); } template < class R > inline LineH2 LineH2::opposite() const { return LineH2( -a(), -b(), -c() ); } template < class R > CGAL_KERNEL_INLINE PointH2 LineH2::point() const { CGAL_kernel_precondition( !is_degenerate() ); if (is_vertical() ) { return PointH2(-c(), RT(0) , a() ); } else { return PointH2(RT(0) , -c(), b() ); } } template < class R > CGAL_KERNEL_INLINE PointH2 LineH2::point(int i) const { return point() + RT(i) * (direction().to_vector()); } template < class R > CGAL_KERNEL_INLINE PointH2 LineH2::projection(const PointH2& p) const { CGAL_kernel_precondition( !is_degenerate() ); LineH2 l( p, DirectionH2( a(), b() )); return PointH2( b()*l.c() - l.b()*c(), l.a()*c() - a()*l.c(), a()*l.b() - l.a()*b() ); } template < class R > CGAL_KERNEL_INLINE DirectionH2 LineH2::direction() const { CGAL_kernel_precondition( !is_degenerate() ); return DirectionH2( b(), -a() ); } template < class R > CGAL_KERNEL_INLINE LineH2 LineH2::transform(const Aff_transformationH2& t) const { CGAL_kernel_precondition( !is_degenerate() ); PointH2 p = point() + direction().to_vector(); return LineH2( t.transform(point() ), t.transform(p) ); } #ifndef CGAL_NO_OSTREAM_INSERT_LINEH2 template < class R > std::ostream & operator<<(std::ostream &os, const LineH2 &l) { switch(os.iword(IO::mode)) { case IO::ASCII : return os << l.a() << ' ' << l.b() << ' ' << l.c(); case IO::BINARY : write(os, l.a()); write(os, l.b()); write(os, l.c()); return os; default: return os << "LineH2(" << l.a() << ", " << l.b() << ", " << l.c() <<')'; } } #endif // CGAL_NO_OSTREAM_INSERT_LINEH2 #ifndef CGAL_NO_ISTREAM_EXTRACT_LINEH2 template < class R > std::istream & operator>>(std::istream &is, LineH2 &p) { typename R::RT a, b, c; switch(is.iword(IO::mode)) { case IO::ASCII : is >> a >> b >> c; break; case IO::BINARY : read(is, a); read(is, b); read(is, c); break; default: std::cerr << "" << std::endl; std::cerr << "Stream must be in ascii or binary mode" << std::endl; break; } p = LineH2(a, b, c); return is; } #endif // CGAL_NO_ISTREAM_EXTRACT_LINEH2 template < class R > CGAL_KERNEL_INLINE bool LineH2::has_on( const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); return ( ( a()*p.hx() + b()*p.hy() + c()*p.hw() ) == RT(0) ); } template < class R > CGAL_KERNEL_INLINE bool LineH2::has_on_boundary( const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); return ( ( a()*p.hx() + b()*p.hy() + c()*p.hw() ) == RT(0) ); } template < class R > CGAL_KERNEL_INLINE bool LineH2::has_on_positive_side( const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); return ( ( a()*p.hx() + b()*p.hy() + c()*p.hw() ) > RT(0) ); } template < class R > CGAL_KERNEL_INLINE bool LineH2::has_on_negative_side( const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); return ( ( a()*p.hx() + b()*p.hy() + c()*p.hw() ) < RT(0) ); } template < class R > CGAL_KERNEL_INLINE Oriented_side LineH2::oriented_side( const PointH2& p ) const { CGAL_kernel_precondition( !is_degenerate() ); RT v = a()*p.hx() + b()*p.hy() + c()*p.hw(); if (v > RT(0) ) { return ON_POSITIVE_SIDE; } else { return (v < RT(0) ) ? ON_NEGATIVE_SIDE : ON_ORIENTED_BOUNDARY; } } template < class R > inline bool LineH2::is_horizontal() const { return ( a() == RT(0) ); } template < class R > inline bool LineH2::is_vertical() const { return ( b() == RT(0) ); } template < class R > inline bool LineH2::is_degenerate() const { return (a() == RT(0) )&&(b() == RT(0) ) ; } template < class R > CGAL_KERNEL_MEDIUM_INLINE bool LineH2::operator==(const LineH2& l) const { if ( (a() * l.c() != l.a() * c() ) ||(b() * l.c() != l.b() * c() ) ) { return false; } int sc = static_cast(CGAL_NTS sign(c())); int slc = static_cast(CGAL_NTS sign(l.c())); if ( sc == slc ) { if (sc == 0) { return ( (a()*l.b() == b()*l.a() ) &&(CGAL_NTS sign(a() )== CGAL_NTS sign( l.a() )) &&(CGAL_NTS sign(b() )== CGAL_NTS sign( l.b() )) ); } else { return true; } } else { return false; } } template < class R > inline bool LineH2::operator!=(const LineH2& l) const { return !(*this == l); } #ifdef CGAL_CFG_TYPENAME_BUG #undef typename #endif CGAL_END_NAMESPACE #endif // CGAL_LINEH2_H