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/distance_predicatesH2.h $
11 // $Id: distance_predicatesH2.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_DISTANCE_PREDICATESH2_H
19 #define CGAL_DISTANCE_PREDICATESH2_H
20 
21 #include <CGAL/determinant.h>
22 
23 namespace CGAL {
24 
25 template < class R>
26 CGAL_KERNEL_MEDIUM_INLINE
27 bool
has_larger_distance_to_point(const PointH2<R> & p,const PointH2<R> & q,const PointH2<R> & r)28 has_larger_distance_to_point(const PointH2<R>& p,
29                               const PointH2<R>& q,
30                               const PointH2<R>& r)
31 {
32   typedef typename R::RT RT;
33 
34   const RT phx = p.hx();
35   const RT phy = p.hy();
36   const RT phw = p.hw();
37   const RT qhx = q.hx();
38   const RT qhy = q.hy();
39   const RT qhw = q.hw();
40   const RT rhx = r.hx();
41   const RT rhy = r.hy();
42   const RT rhw = r.hw();
43   const RT RT0 = RT(0);
44   const RT RT2 = RT(2);
45 
46   RT dosd =   // difference of squared distances
47 
48   //            phx * phx   *   qhw * qhw * rhw * rhw
49   //   -RT(2) * phx * qhx   *   phw * qhw * rhw * rhw
50   //   +        qhx * qhx   *   phw * phw * rhw * rhw
51   //
52   //   +        phy * phy   *   qhw * qhw * rhw * rhw
53   //   -RT(2) * phy * qhy   *   phw * qhw * rhw * rhw
54   //   +        qhy * qhy   *   phw * phw * rhw * rhw
55   //
56   // - (        phx * phx   *   qhw * qhw * rhw * rhw
57   //   -RT(2) * phx * rhx   *   phw * qhw * qhw * rhw
58   //   +        rhx * rhx   *   phw * phw * qhw * qhw
59   //
60   //   +        phy * phy   *   qhw * qhw * rhw * rhw
61   //   -RT(2) * phy * rhy   *   phw * qhw * qhw * rhw
62   //   +        rhy * rhy   *   phw * phw * qhw * qhw
63 
64     rhw*rhw * (         phw * ( qhx*qhx + qhy*qhy )
65                 - RT2 * qhw * ( phx*qhx + phy*qhy )
66               )
67   - qhw*qhw * (         phw * ( rhx*rhx + rhy*rhy )
68                 - RT2 * rhw * ( phx*rhx + phy*rhy )
69               );
70 
71 
72   return ( dosd > RT0 );
73 }
74 
75 template < class R>
76 CGAL_KERNEL_INLINE
77 Comparison_result
compare_signed_distance_to_line(const LineH2<R> & l,const PointH2<R> & p,const PointH2<R> & q)78 compare_signed_distance_to_line(const LineH2<R>&  l,
79                                 const PointH2<R>& p,
80                                 const PointH2<R>& q)
81 {
82   typedef typename R::RT RT;
83 
84   const RT la = l.a();
85   const RT lb = l.b();
86   const RT phx= p.hx();
87   const RT phy= p.hy();
88   const RT phw= p.hw();
89   const RT qhx= q.hx();
90   const RT qhy= q.hy();
91   const RT qhw= q.hw();
92   const RT RT0 = RT(0);
93 
94   RT scaled_dist_p_minus_scaled_dist_q =
95       la*( phx*qhw - qhx*phw )
96     + lb*( phy*qhw - qhy*phw );
97 
98 
99 
100   if ( scaled_dist_p_minus_scaled_dist_q < RT0 )
101   {
102       return SMALLER;
103   }
104   else
105   {
106       return ( RT0 < scaled_dist_p_minus_scaled_dist_q ) ?
107              LARGER : EQUAL;
108   }
109 }
110 
111 
112 template < class R>
113 CGAL_KERNEL_INLINE
114 bool
has_larger_signed_distance_to_line(const LineH2<R> & l,const PointH2<R> & p,const PointH2<R> & q)115 has_larger_signed_distance_to_line(const LineH2<R>&  l,
116                                     const PointH2<R>& p,
117                                     const PointH2<R>& q)
118 {
119   typedef typename R::RT RT;
120 
121   const RT la = l.a();
122   const RT lb = l.b();
123   const RT phx= p.hx();
124   const RT phy= p.hy();
125   const RT phw= p.hw();
126   const RT qhx= q.hx();
127   const RT qhy= q.hy();
128   const RT qhw= q.hw();
129   const RT RT0 = RT(0);
130 
131   RT scaled_dist_p_minus_scaled_dist_q =
132       la*( phx*qhw - qhx*phw )
133     + lb*( phy*qhw - qhy*phw );
134 
135 
136 
137   return ( scaled_dist_p_minus_scaled_dist_q > RT0 );
138 }
139 
140 
141 template < class R>
142 CGAL_KERNEL_INLINE
143 bool
has_smaller_signed_distance_to_line(const LineH2<R> & l,const PointH2<R> & p,const PointH2<R> & q)144 has_smaller_signed_distance_to_line(const LineH2<R>&  l,
145                                     const PointH2<R>& p,
146                                     const PointH2<R>& q)
147 {
148   typedef typename R::RT RT;
149 
150   const RT la = l.a();
151   const RT lb = l.b();
152   const RT phx= p.hx();
153   const RT phy= p.hy();
154   const RT phw= p.hw();
155   const RT qhx= q.hx();
156   const RT qhy= q.hy();
157   const RT qhw= q.hw();
158   const RT RT0 = RT(0);
159 
160   RT scaled_dist_p_minus_scaled_dist_q =
161       la*( phx*qhw - qhx*phw )
162     + lb*( phy*qhw - qhy*phw );
163 
164   return ( scaled_dist_p_minus_scaled_dist_q < RT0 );
165 }
166 
167 
168 template < class R>
169 CGAL_KERNEL_MEDIUM_INLINE
170 Comparison_result
compare_signed_distance_to_line(const PointH2<R> & p,const PointH2<R> & q,const PointH2<R> & r,const PointH2<R> & s)171 compare_signed_distance_to_line(const PointH2<R>& p,
172                                 const PointH2<R>& q,
173                                 const PointH2<R>& r,
174                                 const PointH2<R>& s)
175 {
176   typedef typename R::RT RT;
177 
178   const RT phx= p.hx();
179   const RT phy= p.hy();
180   const RT phw= p.hw();
181   const RT qhx= q.hx();
182   const RT qhy= q.hy();
183   const RT qhw= q.hw();
184   const RT rhx= r.hx();
185   const RT rhy= r.hy();
186   const RT rhw= r.hw();
187   const RT shx= s.hx();
188   const RT shy= s.hy();
189   const RT shw= s.hw();
190   const RT RT0  = RT(0);
191 
192   RT  scaled_dist_r_minus_scaled_dist_s =
193          ( rhx*shw - shx*rhw ) * (phy*qhw - qhy*phw)
194        - ( rhy*shw - shy*rhw ) * (phx*qhw - qhx*phw);
195 
196 
197   if ( scaled_dist_r_minus_scaled_dist_s < RT0 )
198   {
199       return SMALLER;
200   }
201   else
202   {
203       return (scaled_dist_r_minus_scaled_dist_s > RT0 ) ?
204              LARGER : EQUAL;
205   }
206 }
207 
208 
209 template < class R>
210 CGAL_KERNEL_MEDIUM_INLINE
211 bool
has_smaller_signed_distance_to_line(const PointH2<R> & p,const PointH2<R> & q,const PointH2<R> & r,const PointH2<R> & s)212 has_smaller_signed_distance_to_line(const PointH2<R>& p,
213                                     const PointH2<R>& q,
214                                     const PointH2<R>& r,
215                                     const PointH2<R>& s)
216 {
217   typedef typename R::RT RT;
218 
219   const RT phx= p.hx();
220   const RT phy= p.hy();
221   const RT phw= p.hw();
222   const RT qhx= q.hx();
223   const RT qhy= q.hy();
224   const RT qhw= q.hw();
225   const RT rhx= r.hx();
226   const RT rhy= r.hy();
227   const RT rhw= r.hw();
228   const RT shx= s.hx();
229   const RT shy= s.hy();
230   const RT shw= s.hw();
231   const RT RT0  = RT(0);
232 
233   RT  scaled_dist_r_minus_scaled_dist_s =
234          ( rhx*shw - shx*rhw ) * (phy*qhw - qhy*phw)
235        - ( rhy*shw - shy*rhw ) * (phx*qhw - qhx*phw);
236 
237   return ( scaled_dist_r_minus_scaled_dist_s < RT0 );
238 }
239 
240 
241 
242 template < class R>
243 CGAL_KERNEL_MEDIUM_INLINE
244 bool
has_larger_signed_distance_to_line(const PointH2<R> & p,const PointH2<R> & q,const PointH2<R> & r,const PointH2<R> & s)245 has_larger_signed_distance_to_line(const PointH2<R>& p,
246                                     const PointH2<R>& q,
247                                     const PointH2<R>& r,
248                                     const PointH2<R>& s)
249 {
250   typedef typename R::RT RT;
251 
252   const RT phx= p.hx();
253   const RT phy= p.hy();
254   const RT phw= p.hw();
255   const RT qhx= q.hx();
256   const RT qhy= q.hy();
257   const RT qhw= q.hw();
258   const RT rhx= r.hx();
259   const RT rhy= r.hy();
260   const RT rhw= r.hw();
261   const RT shx= s.hx();
262   const RT shy= s.hy();
263   const RT shw= s.hw();
264   const RT RT0  = RT(0);
265 
266   RT  scaled_dist_r_minus_scaled_dist_s =
267          ( rhx*shw - shx*rhw ) * (phy*qhw - qhy*phw)
268        - ( rhy*shw - shy*rhw ) * (phx*qhw - qhx*phw);
269 
270 
271   return ( scaled_dist_r_minus_scaled_dist_s > RT0 );
272 }
273 
274 } //namespace CGAL
275 
276 #endif //CGAL_DISTANCE_PREDICATESH2_H
277