1 // Copyright (c) 1999
2 // Utrecht University (The Netherlands),
3 // ETH Zurich (Switzerland),
4 // INRIA Sophia-Antipolis (France),
5 // Max-Planck-Institute Saarbruecken (Germany),
6 // and Tel-Aviv University (Israel).  All rights reserved.
7 //
8 // This file is part of CGAL (www.cgal.org)
9 //
10 // $URL: https://github.com/CGAL/cgal/blob/v5.3/Homogeneous_kernel/include/CGAL/Homogeneous/VectorH2.h $
11 // $Id: VectorH2.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
12 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
13 //
14 //
15 // Author(s)     : Stefan Schirra
16 
17 
18 #ifndef CGAL_HOMOGENEOUS_VECTOR_2_h
19 #define CGAL_HOMOGENEOUS_VECTOR_2_h
20 
21 #include <CGAL/Origin.h>
22 #include <CGAL/array.h>
23 #include <CGAL/Kernel_d/Cartesian_const_iterator_d.h>
24 #include <CGAL/Handle_for.h>
25 
26 #include <boost/next_prior.hpp>
27 
28 namespace CGAL {
29 
30 template < class R_ >
31 class VectorH2
32 {
33   typedef VectorH2<R_>                      Self;
34   typedef typename R_::FT                   FT;
35   typedef typename R_::RT                   RT;
36   typedef typename R_::Point_2              Point_2;
37   typedef typename R_::Segment_2            Segment_2;
38   typedef typename R_::Ray_2                Ray_2;
39   typedef typename R_::Line_2               Line_2;
40   typedef typename R_::Direction_2          Direction_2;
41   typedef typename R_::Vector_2             Vector_2;
42 
43   typedef std::array<RT, 3>               Rep;
44   typedef typename R_::template Handle<Rep>::type  Base;
45 
46   typedef Rational_traits<FT>               Rat_traits;
47 
48   Base base;
49 
50 public:
51 
52   typedef const FT Cartesian_coordinate_type;
53   typedef const RT& Homogeneous_coordinate_type;
54   typedef Cartesian_const_iterator_d<typename Rep::const_iterator> Cartesian_const_iterator;
55 
56   typedef R_                                    R;
57 
VectorH2()58    VectorH2() {}
59 
60    template < typename Tx, typename Ty >
61    VectorH2(const Tx & x, const Ty & y,
62             typename boost::enable_if< boost::mpl::and_<boost::is_convertible<Tx, RT>,
63                                                         boost::is_convertible<Ty, RT> > >::type* = 0)
64       : base(CGAL::make_array<RT>(x, y, RT(1))) {}
65 
VectorH2(const FT & x,const FT & y)66    VectorH2(const FT& x, const FT& y)
67       : base(CGAL::make_array<RT>(
68              Rat_traits().numerator(x) * Rat_traits().denominator(y),
69              Rat_traits().numerator(y) * Rat_traits().denominator(x),
70              Rat_traits().denominator(x) * Rat_traits().denominator(y)))
71    {
72      CGAL_kernel_assertion(hw() > 0);
73    }
74 
VectorH2(const RT & x,const RT & y,const RT & w)75    VectorH2(const RT& x, const RT& y, const RT& w )
76      : base( w >= RT(0) ? CGAL::make_array( x,  y,  w)
77                         : CGAL::make_array<RT>(-x, -y, -w) ) {}
78 
79   const Self&
rep()80   rep() const
81   {
82     return static_cast<const Self& >(*this);
83   }
84 
85    bool    operator==( const VectorH2<R>& v) const;
86    bool    operator!=( const VectorH2<R>& v) const;
87    bool    operator==( const Null_vector&) const;
88    bool    operator!=( const Null_vector& v) const;
89 
hx()90    const RT & hx() const { return CGAL::get_pointee_or_identity(base)[0]; };
hy()91    const RT & hy() const { return CGAL::get_pointee_or_identity(base)[1]; };
hw()92    const RT & hw() const { return CGAL::get_pointee_or_identity(base)[2]; };
93 
x()94    FT      x()  const { return FT(hx()) / FT(hw()); };
y()95    FT      y()  const { return FT(hy()) / FT(hw()); };
96 
97    FT      cartesian(int i)   const;
98    const RT & homogeneous(int i) const;
99    FT      operator[](int i)  const;
100 
cartesian_begin()101    Cartesian_const_iterator cartesian_begin() const
102    {
103      return make_cartesian_const_iterator_begin(CGAL::get_pointee_or_identity(base).begin(),
104                                                 boost::prior(CGAL::get_pointee_or_identity(base).end()));
105    }
106 
cartesian_end()107    Cartesian_const_iterator cartesian_end() const
108    {
109      return make_cartesian_const_iterator_end(boost::prior(CGAL::get_pointee_or_identity(base).end()));
110    }
111 
112    int     dimension() const;
113    Direction_2 direction() const;
114    Vector_2 perpendicular(const Orientation& o ) const;
115 
116   //   Vector_2 operator+(const VectorH2 &v) const;
117    Vector_2 operator-(const VectorH2 &v) const;
118    Vector_2 operator-() const;
119    Vector_2 opposite() const;
120    FT squared_length() const;
121   //   Vector_2 operator/(const RT &f) const;
122   //Vector_2 operator/(const FT &f) const;
123 
124 // undocumented:
VectorH2(const Direction_2 & dir)125    VectorH2(const Direction_2 & dir)
126       : base ( dir) {}
127 
VectorH2(const Point_2 & p)128   VectorH2(const Point_2 & p)
129      : base ( p) {}
130 };
131 
132 template < class R >
133 inline
134 bool
135 VectorH2<R>::operator==( const Null_vector&) const
136 { return (hx() == RT(0)) && (hy() == RT(0)); }
137 
138 template < class R >
139 inline
140 bool
141 VectorH2<R>::operator!=( const Null_vector& v) const
142 { return !(*this == v); }
143 
144 template < class R >
145 CGAL_KERNEL_INLINE
146 bool
147 VectorH2<R>::operator==( const VectorH2<R>& v) const
148 {
149   return (  (hx() * v.hw() == v.hx() * hw() )
150           &&(hy() * v.hw() == v.hy() * hw() ) );
151 }
152 
153 template < class R >
154 inline
155 bool
156 VectorH2<R>::operator!=( const VectorH2<R>& v) const
157 { return !(*this == v); }  /* XXX */
158 
159 template < class R >
160 CGAL_KERNEL_INLINE
161 typename VectorH2<R>::FT
cartesian(int i)162 VectorH2<R>::cartesian(int i) const
163 {
164   CGAL_kernel_precondition( (i==0 || i==1) );
165   if (i==0)
166       return x();
167   return y();
168 }
169 
170 template < class R >
171 CGAL_KERNEL_INLINE
172 const typename VectorH2<R>::RT &
homogeneous(int i)173 VectorH2<R>::homogeneous(int i) const
174 {
175   CGAL_kernel_precondition( (i>=0) && (i<=2) );
176   return CGAL::get_pointee_or_identity(base)[i];
177 }
178 
179 template < class R >
180 inline
181 typename VectorH2<R>::FT
182 VectorH2<R>::operator[](int i) const
183 { return cartesian(i); }
184 
185 template < class R >
186 inline
187 int
dimension()188 VectorH2<R>::dimension() const
189 { return 2; }
190 
191 template < class R >
192 CGAL_KERNEL_INLINE
193 typename VectorH2<R>::Direction_2
direction()194 VectorH2<R>::direction() const
195 { return Direction_2(hx(), hy()); }
196 
197 template < class R >
198 inline
199 typename VectorH2<R>::Vector_2
200 VectorH2<R>::operator-() const
201 { return VectorH2<R>(- hx(), - hy(), hw() ); }
202 
203 template < class R >
204 inline
205 typename VectorH2<R>::Vector_2
opposite()206 VectorH2<R>::opposite() const
207 { return VectorH2<R>(- hx(), - hy(), hw() ); }
208 
209 
210 template <class R>
211 CGAL_KERNEL_INLINE
212 typename VectorH2<R>::Vector_2
213 VectorH2<R>::operator-(const VectorH2<R>& v) const
214 {
215   return VectorH2<R>( hx()*v.hw() - v.hx()*hw(),
216                       hy()*v.hw() - v.hy()*hw(),
217                       hw()*v.hw() );
218 }
219 
220 template <class R>
221 CGAL_KERNEL_INLINE
222 typename VectorH2<R>::FT
squared_length()223 VectorH2<R>::squared_length() const
224 {
225   typedef typename R::FT FT;
226   return
227     FT( CGAL_NTS square(hx()) + CGAL_NTS square(hy()) ) /
228     FT( CGAL_NTS square(hw()) );
229 }
230 
231 
232 template < class R >
233 CGAL_KERNEL_INLINE
234 typename R::Vector_2
perpendicular(const Orientation & o)235 VectorH2<R>::perpendicular(const Orientation& o) const
236 {
237   CGAL_kernel_precondition(o != COLLINEAR);
238   if (o == COUNTERCLOCKWISE)
239       return typename R::Vector_2(-hy(), hx(), hw());
240   else
241       return typename R::Vector_2(hy(), -hx(), hw());
242 }
243 
244 } //namespace CGAL
245 
246 #endif // CGAL_HOMOGENEOUS_VECTOR_2_h
247