1 // Copyright (c) 2000
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/Cartesian_kernel/include/CGAL/Cartesian/Vector_2.h $
11 // $Id: Vector_2.h 8bb22d5 2020-03-26T14:23:37+01:00 Sébastien Loriot
12 // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
13 //
14 //
15 // Author(s)     : Andreas Fabri, Herve Bronnimann
16 
17 #ifndef CGAL_CARTESIAN_VECTOR_2_H
18 #define CGAL_CARTESIAN_VECTOR_2_H
19 
20 #include <CGAL/Origin.h>
21 #include <CGAL/array.h>
22 #include <CGAL/constant.h>
23 #include <CGAL/Handle_for.h>
24 
25 namespace CGAL {
26 
27 template < class R_ >
28 class VectorC2
29 {
30   typedef VectorC2<R_>                      Self;
31   typedef typename R_::FT                   FT;
32   typedef typename R_::Point_2              Point_2;
33   typedef typename R_::Vector_2             Vector_2;
34   typedef typename R_::Segment_2            Segment_2;
35   typedef typename R_::Ray_2                Ray_2;
36   typedef typename R_::Line_2               Line_2;
37   typedef typename R_::Direction_2          Direction_2;
38 
39   typedef std::array<FT, 2>               Rep;
40   typedef typename R_::template Handle<Rep>::type  Base;
41 
42   Base base;
43 
44 public:
45 
46   typedef typename Rep::const_iterator      Cartesian_const_iterator;
47 
48   typedef R_                                R;
49 
VectorC2()50   VectorC2() {}
51 
VectorC2(const FT & x,const FT & y)52   VectorC2(const FT &x, const FT &y)
53     : base(CGAL::make_array(x, y)) {}
54 
VectorC2(const FT & hx,const FT & hy,const FT & hw)55   VectorC2(const FT &hx, const FT &hy, const FT &hw)
56     : base( hw != FT(1) ? CGAL::make_array<FT>(hx/hw, hy/hw)
57                         : CGAL::make_array(hx, hy) ) {}
58 
swap(Self & a,Self & b)59   friend void swap(Self& a, Self& b)
60 #ifdef __cpp_lib_is_swappable
61     noexcept(std::is_nothrow_swappable_v<Base>)
62 #endif
63   {
64     using std::swap;
65     swap(a.base, b.base);
66   }
67 
x()68   const FT & x() const
69   {
70       return CGAL::get_pointee_or_identity(base)[0];
71   }
72 
y()73   const FT & y() const
74   {
75       return CGAL::get_pointee_or_identity(base)[1];
76   }
77 
hx()78   const FT & hx() const
79   {
80       return x();
81   }
82 
hy()83   const FT & hy() const
84   {
85       return y();
86   }
87 
hw()88   const FT& hw() const
89   {
90     return constant<FT, 1>();
91   }
92 
cartesian_begin()93   Cartesian_const_iterator cartesian_begin() const
94   {
95     return CGAL::get_pointee_or_identity(base).begin();
96   }
97 
cartesian_end()98   Cartesian_const_iterator cartesian_end() const
99   {
100     return CGAL::get_pointee_or_identity(base).end();
101   }
102 
103 };
104 
105 template < class R >
106 CGAL_KERNEL_INLINE
107 bool
108 operator==(const VectorC2<R> &v, const VectorC2<R> &w)
109 {
110   return w.x() == v.x() && w.y() == v.y();
111 }
112 
113 template < class R >
114 inline
115 bool
116 operator!=(const VectorC2<R> &v, const VectorC2<R> &w)
117 {
118   return !(v == w);
119 }
120 
121 template < class R >
122 inline
123 bool
124 operator==(const VectorC2<R> &v, const Null_vector &)
125 {
126   return CGAL_NTS is_zero(v.x()) && CGAL_NTS is_zero(v.y());
127 }
128 
129 template < class R >
130 inline
131 bool
132 operator==(const Null_vector &n, const VectorC2<R> &v)
133 {
134   return v == n;
135 }
136 
137 template < class R >
138 inline
139 bool
140 operator!=(const VectorC2<R> &v, const Null_vector &n)
141 {
142   return !(v == n);
143 }
144 
145 template < class R >
146 inline
147 bool
148 operator!=(const Null_vector &n, const VectorC2<R> &v)
149 {
150   return !(v == n);
151 }
152 
153 } //namespace CGAL
154 
155 #endif // CGAL_CARTESIAN_VECTOR_2_H
156