-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathfunction_objects.h
More file actions
4939 lines (4211 loc) · 142 KB
/
function_objects.h
File metadata and controls
4939 lines (4211 loc) · 142 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 1999-2004
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL$
// $Id$
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Stefan Schirra, Sylvain Pion, Michael Hoffmann
#ifndef CGAL_HOMOGENEOUS_FUNCTION_OBJECTS_H
#define CGAL_HOMOGENEOUS_FUNCTION_OBJECTS_H
#include <CGAL/Kernel/function_objects.h>
#include <CGAL/Cartesian/function_objects.h>
#include <CGAL/Kernel/Return_base_tag.h>
#include <CGAL/predicates/sign_of_determinant.h>
#include <CGAL/Homogeneous/predicates_on_pointsH2.h>
#include <CGAL/Homogeneous/predicates_on_pointsH3.h>
namespace CGAL {
namespace HomogeneousKernelFunctors {
using namespace CommonKernelFunctors;
// For lazyness...
using CartesianKernelFunctors::Are_parallel_2;
using CartesianKernelFunctors::Are_parallel_3;
using CartesianKernelFunctors::Compute_squared_area_3;
using CartesianKernelFunctors::Compare_squared_radius_2;
using CartesianKernelFunctors::Compare_squared_radius_3;
using CartesianKernelFunctors::Collinear_3;
using CartesianKernelFunctors::Construct_line_3;
using CartesianKernelFunctors::Construct_equidistant_line_3;
using CartesianKernelFunctors::Construct_barycenter_2;
using CartesianKernelFunctors::Construct_barycenter_3;
using CartesianKernelFunctors::Compute_approximate_area_3;
using CartesianKernelFunctors::Compute_approximate_squared_length_3;
using CartesianKernelFunctors::Compute_area_divided_by_pi_3;
using CartesianKernelFunctors::Compute_squared_length_divided_by_pi_square_3;
using CartesianKernelFunctors::Construct_radical_plane_3;
template <typename K>
class Angle_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Vector_2 Vector_2;
typedef typename K::Construct_vector_2 Construct_vector_2;
Construct_vector_2 c;
public:
typedef typename K::Angle result_type;
Angle_2() {}
Angle_2(const Construct_vector_2& c_) : c(c_) {}
result_type
operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
{ return operator()(c(q,p), c(q,r)); }
result_type
operator()(const Point_2& p, const Point_2& q,
const Point_2& r, const Point_2& s) const
{ return operator()(c(q,p), c(s,r)); }
result_type
operator()(const Vector_2& u, const Vector_2& v) const
{ return enum_cast<Angle>(CGAL_NTS sign(u * v)); }
// FIXME: scalar product
};
template <typename K>
class Angle_3
{
typedef typename K::Point_3 Point_3;
typedef typename K::Vector_3 Vector_3;
typedef typename K::Construct_vector_3 Construct_vector_3;
Construct_vector_3 c;
public:
typedef typename K::Angle result_type;
Angle_3() {}
Angle_3(const Construct_vector_3& c_) : c(c_) {}
result_type
operator()(const Vector_3& u, const Vector_3& v) const
{ return enum_cast<Angle>(CGAL_NTS sign(u * v)); }
// FIXME: scalar product
result_type
operator()(const Point_3& p, const Point_3& q, const Point_3& r) const
{ return enum_cast<Angle>(CGAL_NTS sign(c(q,p) * c(q,r))); }
// FIXME: scalar product
result_type
operator()(const Point_3& p, const Point_3& q,
const Point_3& r, const Point_3& s) const
{ return enum_cast<Angle>(CGAL_NTS sign(c(q,p) * c(s,r))); }
// FIXME: scalar product
result_type
operator()(const Point_3& p, const Point_3& q,
const Point_3& r, const Vector_3& n) const
{
return enum_cast<Angle>(orientation(p,q,r,r+n));
}
};
template <typename K>
class Bounded_side_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Circle_2 Circle_2;
typedef typename K::Triangle_2 Triangle_2;
typedef typename K::Iso_rectangle_2 Iso_rectangle_2;
public:
typedef typename K::Bounded_side result_type;
result_type
operator()( const Circle_2& c, const Point_2& p) const
{
typename K::Compute_squared_distance_2 squared_distance;
return enum_cast<Bounded_side>(CGAL::compare(c.squared_radius(),
squared_distance(c.center(),p)));
}
result_type
operator()( const Triangle_2& t, const Point_2& p) const
{
typename K::Collinear_are_ordered_along_line_2
collinear_are_ordered_along_line;
typename K::Orientation_2 orientation;
typename K::Orientation o1 = orientation(t.vertex(0), t.vertex(1), p),
o2 = orientation(t.vertex(1), t.vertex(2), p),
o3 = orientation(t.vertex(2), t.vertex(3), p);
if (o2 == o1 && o3 == o1)
return ON_BOUNDED_SIDE;
return
(o1 == COLLINEAR
&& collinear_are_ordered_along_line(t.vertex(0), p, t.vertex(1))) ||
(o2 == COLLINEAR
&& collinear_are_ordered_along_line(t.vertex(1), p, t.vertex(2))) ||
(o3 == COLLINEAR
&& collinear_are_ordered_along_line(t.vertex(2), p, t.vertex(3)))
? ON_BOUNDARY
: ON_UNBOUNDED_SIDE;
}
result_type
operator()( const Iso_rectangle_2& r, const Point_2& p) const
{
return r.rep().bounded_side(p);
}
};
template <typename K>
class Bounded_side_3
{
typedef typename K::RT RT;
typedef typename K::Point_3 Point_3;
typedef typename K::Vector_3 Vector_3;
typedef typename K::Sphere_3 Sphere_3;
typedef typename K::Tetrahedron_3 Tetrahedron_3;
typedef typename K::Iso_cuboid_3 Iso_cuboid_3;
public:
typedef typename K::Bounded_side result_type;
result_type
operator()( const Sphere_3& s, const Point_3& p) const
{ return s.rep().bounded_side(p); }
result_type
operator()( const Tetrahedron_3& t, const Point_3& p) const
{
Vector_3 v1 = t.vertex(1)-t.vertex(0);
Vector_3 v2 = t.vertex(2)-t.vertex(0);
Vector_3 v3 = t.vertex(3)-t.vertex(0);
Vector_3 vp = p - t.vertex(0);
// want to solve alpha*v1 + beta*v2 + gamma*v3 == vp
// let vi' == vi*vi.hw()
// we solve alpha'*v1' + beta'*v2' + gamma'*v3' == vp' / vp.hw()
// muliplied by vp.hw()
// then we have alpha = alpha'*v1.hw() / vp.hw()
// and beta = beta' *v2.hw() / vp.hw()
// and gamma = gamma'*v3.hw() / vp.hw()
const RT & v1x = v1.hx();
const RT & v1y = v1.hy();
const RT & v1z = v1.hz();
const RT & v2x = v2.hx();
const RT & v2y = v2.hy();
const RT & v2z = v2.hz();
const RT & v3x = v3.hx();
const RT & v3y = v3.hy();
const RT & v3z = v3.hz();
const RT & vpx = vp.hx();
const RT & vpy = vp.hy();
const RT & vpz = vp.hz();
RT alpha = determinant( vpx, v2x, v3x,
vpy, v2y, v3y,
vpz, v2z, v3z );
RT beta = determinant( v1x, vpx, v3x,
v1y, vpy, v3y,
v1z, vpz, v3z );
RT gamma = determinant( v1x, v2x, vpx,
v1y, v2y, vpy,
v1z, v2z, vpz );
RT det = determinant( v1x, v2x, v3x,
v1y, v2y, v3y,
v1z, v2z, v3z );
CGAL_kernel_assertion( det != 0 );
if (det < 0 )
{
alpha = - alpha;
beta = - beta ;
gamma = - gamma;
det = - det ;
}
bool t1 = ( alpha < 0 );
bool t2 = ( beta < 0 );
bool t3 = ( gamma < 0 );
// t1 || t2 || t3 == not contained in cone
RT lhs = alpha*v1.hw() + beta*v2.hw() + gamma*v3.hw();
RT rhs = det * vp.hw();
bool t4 = ( lhs > rhs );
// alpha + beta + gamma > 1 ?
bool t5 = ( lhs < rhs );
// alpha + beta + gamma < 1 ?
bool t6 = ( (alpha > 0) && (beta > 0) && (gamma > 0) );
if ( t1 || t2 || t3 || t4 )
{
return ON_UNBOUNDED_SIDE;
}
return (t5 && t6) ? ON_BOUNDED_SIDE : ON_BOUNDARY;
}
result_type
operator()( const Iso_cuboid_3& c, const Point_3& p) const
{ return c.rep().bounded_side(p); }
};
template <typename K>
class Collinear_are_ordered_along_line_2
{
typedef typename K::RT RT;
typedef typename K::Point_2 Point_2;
#ifdef CGAL_kernel_exactness_preconditions
typedef typename K::Collinear_2 Collinear_2;
Collinear_2 c;
#endif // CGAL_kernel_exactness_preconditions
public:
typedef typename K::Boolean result_type;
#ifdef CGAL_kernel_exactness_preconditions
Collinear_are_ordered_along_line_2() {}
Collinear_are_ordered_along_line_2(const Collinear_2& c_) : c(c_) {}
#endif // CGAL_kernel_exactness_preconditions
result_type
operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
{
CGAL_kernel_exactness_precondition( c(p, q, r) );
const RT& phx = p.hx();
const RT& phy = p.hy();
const RT& phw = p.hw();
const RT& qhx = q.hx();
const RT& qhy = q.hy();
const RT& qhw = q.hw();
const RT& rhx = r.hx();
const RT& rhy = r.hy();
const RT& rhw = r.hw();
if ( !(phx * rhw == rhx * phw ) ) // non-vertical ?
{
return !( ( ( phx * qhw < qhx * phw)
&&( rhx * qhw < qhx * rhw))
||( ( qhx * phw < phx * qhw)
&&( qhx * rhw < rhx * qhw)) );
}
else if ( !(phy * rhw == rhy * phw ) )
{
return !( ( ( phy * qhw < qhy * phw)
&&( rhy * qhw < qhy * rhw))
||( ( qhy * phw < phy * qhw)
&&( qhy * rhw < rhy * qhw)) );
}
else
return (( phx*qhw == qhx*phw) && ( phy*qhw == qhy*phw));
}
};
template <typename K>
class Collinear_are_ordered_along_line_3
{
typedef typename K::Point_3 Point_3;
#ifdef CGAL_kernel_exactness_preconditions
typedef typename K::Collinear_3 Collinear_3;
Collinear_3 c;
#endif // CGAL_kernel_exactness_preconditions
public:
typedef typename K::Boolean result_type;
#ifdef CGAL_kernel_exactness_preconditions
Collinear_are_ordered_along_line_3() {}
Collinear_are_ordered_along_line_3(const Collinear_3& c_) : c(c_) {}
#endif // CGAL_kernel_exactness_preconditions
result_type
operator()(const Point_3& p, const Point_3& q, const Point_3& r) const
{
CGAL_kernel_exactness_precondition( c(p, q, r) );
typedef typename K::RT RT;
const RT & phx = p.hx();
const RT & phw = p.hw();
const RT & qhx = q.hx();
const RT & qhw = q.hw();
const RT & rhx = r.hx();
const RT & rhw = r.hw();
const RT pqx = phx*qhw;
const RT qpx = qhx*phw;
const RT prx = phx*rhw;
const RT qrx = qhx*rhw;
const RT rqx = rhx*qhw;
const RT rpx = rhx*phw;
if ( prx != rpx ) // px != rx
{
// (px <= qx)&&(qx <= rx) || (px >= qx)&&(qx >= rx)
// !(((qx < px)||(rx < qx))&&((px < qx)||(qx < rx)))
return ! ( ((qpx < pqx) || (rqx < qrx))
&& ((pqx < qpx) || (qrx < rqx)) );
}
const RT & phy = p.hy();
const RT & qhy = q.hy();
const RT & rhy = r.hy();
const RT pqy = phy*qhw;
const RT qpy = qhy*phw;
const RT pry = phy*rhw;
const RT qry = qhy*rhw;
const RT rqy = rhy*qhw;
const RT rpy = rhy*phw;
if ( pry != rpy )
{
return ! ( ((qpy < pqy) || (rqy < qry))
&& ((pqy < qpy) || (qry < rqy)) );
}
const RT & phz = p.hz();
const RT & qhz = q.hz();
const RT & rhz = r.hz();
const RT pqz = phz*qhw;
const RT qpz = qhz*phw;
const RT prz = phz*rhw;
const RT qrz = qhz*rhw;
const RT rqz = rhz*qhw;
const RT rpz = rhz*phw;
if ( prz != rpz )
{
return ! ( ((qpz < pqz) || (rqz < qrz))
&& ((pqz < qpz) || (qrz < rqz)) );
}
// p == r
return ((rqx == qrx) && (rqy == qry) && (rqz == qrz));
}
};
template <typename K>
class Collinear_are_strictly_ordered_along_line_2
{
typedef typename K::Point_2 Point_2;
#ifdef CGAL_kernel_exactness_preconditions
typedef typename K::Collinear_2 Collinear_2;
Collinear_2 c;
#endif // CGAL_kernel_exactness_preconditions
public:
typedef typename K::Boolean result_type;
#ifdef CGAL_kernel_exactness_preconditions
Collinear_are_strictly_ordered_along_line_2() {}
Collinear_are_strictly_ordered_along_line_2(const Collinear_2& c_) : c(c_)
{}
#endif // CGAL_kernel_exactness_preconditions
result_type
operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
{
CGAL_kernel_exactness_precondition( c(p, q, r) );
typedef typename K::RT RT;
const RT& phx = p.hx();
const RT& phy = p.hy();
const RT& phw = p.hw();
const RT& qhx = q.hx();
const RT& qhy = q.hy();
const RT& qhw = q.hw();
const RT& rhx = r.hx();
const RT& rhy = r.hy();
const RT& rhw = r.hw();
if ( !(phx * rhw == rhx * phw ) )
{
return ( ( phx * qhw < qhx * phw)
&&( qhx * rhw < rhx * qhw))
||( ( qhx * phw < phx * qhw) // ( phx * qhw > qhx * phw)
&&( rhx * qhw < qhx * rhw)); // ( qhx * rhw > rhx * qhw)
}
else
{
return ( ( phy * qhw < qhy * phw)
&&( qhy * rhw < rhy * qhw))
||( ( qhy * phw < phy * qhw) // ( phy * qhw > qhy * phw)
&&( rhy * qhw < qhy * rhw)); // ( qhy * rhw > rhy * qhw)
}
}
};
template <typename K>
class Collinear_are_strictly_ordered_along_line_3
{
typedef typename K::Point_3 Point_3;
typedef typename K::Direction_3 Direction_3;
#ifdef CGAL_kernel_exactness_preconditions
typedef typename K::Collinear_3 Collinear_3;
Collinear_3 c;
#endif // CGAL_kernel_exactness_preconditions
public:
typedef typename K::Boolean result_type;
#ifdef CGAL_kernel_exactness_preconditions
Collinear_are_strictly_ordered_along_line_3() {}
Collinear_are_strictly_ordered_along_line_3(const Collinear_3& c_) : c(c_)
{}
#endif // CGAL_kernel_exactness_preconditions
result_type
operator()(const Point_3& p, const Point_3& q, const Point_3& r) const
{
CGAL_kernel_exactness_precondition( c(p, q, r) );
if ( p == r) return false;
Direction_3 dir_pq = (p - q).direction();
Direction_3 dir_rq = (r - q).direction();
return (dir_pq == -dir_rq);
} // FIXME
};
template <typename K>
class Collinear_has_on_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Direction_2 Direction_2;
typedef typename K::Ray_2 Ray_2;
typedef typename K::Segment_2 Segment_2;
typedef typename K::Construct_point_on_2 Construct_point_on_2;
typedef typename K::Compare_xy_2 Compare_xy_2;
typedef typename K::Collinear_are_ordered_along_line_2
Collinear_are_ordered_along_line_2;
Collinear_are_ordered_along_line_2 co;
Construct_point_on_2 cp;
Compare_xy_2 cxy;
public:
typedef typename K::Boolean result_type;
Collinear_has_on_2() {}
Collinear_has_on_2(const Construct_point_on_2& cp_,
const Compare_xy_2& cxy_)
: cp(cp_), cxy(cxy_)
{}
result_type
operator()( const Ray_2& r, const Point_2& p) const
{
const Point_2 & source = cp(r,0);
return p == source || Direction_2(p - source) == r.direction();
} // FIXME
result_type
operator()( const Segment_2& s, const Point_2& p) const
{
return co(cp(s,0), p, cp(s,1));
}
};
template <typename K>
class Collinear_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Orientation_2 Orientation_2;
Orientation_2 o;
public:
typedef typename K::Boolean result_type;
Collinear_2() {}
Collinear_2(const Orientation_2 o_) : o(o_) {}
result_type
operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
{
typedef typename K::RT RT;
const RT& phx = p.hx();
const RT& phy = p.hy();
const RT& phw = p.hw();
const RT& qhx = q.hx();
const RT& qhy = q.hy();
const RT& qhw = q.hw();
const RT& rhx = r.hx();
const RT& rhy = r.hy();
const RT& rhw = r.hw();
// | A B |
// | C D |
RT A = phx*rhw - phw*rhx;
RT B = phy*rhw - phw*rhy;
RT C = qhx*rhw - qhw*rhx;
RT D = qhy*rhw - qhw*rhy;
RT det = A*D - B*C;
/*
RT det_old = p.hx() * (q.hy()*r.hw() - q.hw()*r.hy() )
+ p.hy() * (q.hw()*r.hx() - q.hx()*r.hw() )
+ p.hw() * (q.hx()*r.hy() - q.hy()*r.hx() );
if ( !(CGAL_NTS sign(det) == CGAL_NTS sign(det_old)) )
{
std::cerr << "det: " << det << " det_old: " << det_old << flush;
}
*/
return CGAL_NTS is_zero(det);
}
};
template <typename K>
class Compare_angle_with_x_axis_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Vector_2 Vector_2;
typedef typename K::Direction_2 Direction_2;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()(const Direction_2& d1, const Direction_2& d2) const
{
typedef typename K::RT RT;
CGAL_kernel_precondition(
static_cast<int>(COUNTERCLOCKWISE) == static_cast<int>(LARGER)
&& static_cast<int>(COLLINEAR) == static_cast<int>(EQUAL)
&& static_cast<int>(CLOCKWISE) == static_cast<int>(SMALLER) );
const RT RT0(0);
Vector_2 dirvec1(d1.x(), d1.y()); // Added
Point_2 p1 = CGAL::ORIGIN + dirvec1; // Added
Vector_2 dirvec2(d2.x(), d2.y()); // Added
Point_2 p2 = ORIGIN + dirvec2; // Added
// Point_2 p1 = ORIGIN + d1.vector(); // Commented out
// Point_2 p2 = ORIGIN + d2.vector(); // Commented out
CGAL_kernel_precondition( RT0 < p1.hw() );
CGAL_kernel_precondition( RT0 < p2.hw() );
int x_sign1 = static_cast<int>(CGAL_NTS sign( p1.hx() ));
int x_sign2 = static_cast<int>(CGAL_NTS sign( p2.hx() ));
int y_sign1 = static_cast<int>(CGAL_NTS sign( p1.hy() ));
int y_sign2 = static_cast<int>(CGAL_NTS sign( p2.hy() ));
if ( y_sign1 * y_sign2 < 0)
{
return (0 < y_sign1 ) ? SMALLER : LARGER;
}
Point_2 origin( RT0 , RT0 );
if ( 0 < y_sign1 * y_sign2 )
{
return orientation(origin, p2, p1);
// Precondition on the enums:
// COUNTERCLOCKWISE == LARGER ( == 1 )
// COLLINEAR == EQUAL ( == 0 )
// CLOCKWISE == SMALLER ( == -1 )
}
// ( y_sign1 * y_sign2 == 0 )
bool b1 = (y_sign1 == 0) && (x_sign1 >= 0);
bool b2 = (y_sign2 == 0) && (x_sign2 >= 0);
if ( b1 ) { return b2 ? EQUAL : SMALLER; }
if ( b2 ) { return b1 ? EQUAL : LARGER; }
if ( y_sign1 == y_sign2 ) // == 0
return EQUAL;
else
return (orientation(origin, p1, p2) == COUNTERCLOCKWISE) ?
SMALLER : LARGER;
}
};
template <typename K>
class Compare_distance_2
{
typedef typename K::Point_2 Point_2;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
{
typedef typename K::RT RT;
const RT & phx = p.hx();
const RT & phy = p.hy();
const RT & phw = p.hw();
const RT & qhx = q.hx();
const RT & qhy = q.hy();
const RT & qhw = q.hw();
const RT & rhx = r.hx();
const RT & rhy = r.hy();
const RT & rhw = r.hw();
RT dosd = // difference of squared distances
// phx * phx * qhw * qhw * rhw * rhw
// -RT(2) * phx * qhx * phw * qhw * rhw * rhw
// + qhx * qhx * phw * phw * rhw * rhw
//
// + phy * phy * qhw * qhw * rhw * rhw
// -RT(2) * phy * qhy * phw * qhw * rhw * rhw
// + qhy * qhy * phw * phw * rhw * rhw
//
// - ( phx * phx * qhw * qhw * rhw * rhw
// -RT(2) * phx * rhx * phw * qhw * qhw * rhw
// + rhx * rhx * phw * phw * qhw * qhw
//
// + phy * phy * qhw * qhw * rhw * rhw
// -RT(2) * phy * rhy * phw * qhw * qhw * rhw
// + rhy * rhy * phw * phw * qhw * qhw
rhw*rhw * ( phw * ( qhx*qhx + qhy*qhy )
- 2 * qhw * ( phx*qhx + phy*qhy )
)
- qhw*qhw * ( phw * ( rhx*rhx + rhy*rhy )
- 2 * rhw * ( phx*rhx + phy*rhy )
);
return CGAL_NTS sign(dosd);
}
template <class T1, class T2, class T3>
result_type
operator()(const T1& p, const T2& q, const T3& r) const
{
return CGAL::compare(squared_distance(p, q), squared_distance(p, r));
}
template <class T1, class T2, class T3, class T4>
result_type
operator()(const T1& p, const T2& q, const T3& r, const T4& s) const
{
return CGAL::compare(squared_distance(p, q), squared_distance(r, s));
}
};
template <typename K>
class Compare_distance_3
{
typedef typename K::Point_3 Point_3;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()(const Point_3& p, const Point_3& q, const Point_3& r) const
{
typedef typename K::RT RT;
const RT & phx = p.hx();
const RT & phy = p.hy();
const RT & phz = p.hz();
const RT & phw = p.hw();
const RT & qhx = q.hx();
const RT & qhy = q.hy();
const RT & qhz = q.hz();
const RT & qhw = q.hw();
const RT & rhx = r.hx();
const RT & rhy = r.hy();
const RT & rhz = r.hz();
const RT & rhw = r.hw();
RT dosd = // difference of squared distances
rhw*rhw * ( phw * ( qhx*qhx + qhy*qhy + qhz*qhz )
- 2 * qhw * ( phx*qhx + phy*qhy + phz*qhz )
)
- qhw*qhw * ( phw * ( rhx*rhx + rhy*rhy + rhz*rhz )
- 2 * rhw * ( phx*rhx + phy*rhy + phz*rhz )
);
return CGAL_NTS sign(dosd);
}
template <class T1, class T2, class T3>
result_type
operator()(const T1& p, const T2& q, const T3& r) const
{
return CGAL::compare(squared_distance(p, q), squared_distance(p, r));
}
template <class T1, class T2, class T3, class T4>
result_type
operator()(const T1& p, const T2& q, const T3& r, const T4& s) const
{
return CGAL::compare(squared_distance(p, q), squared_distance(r, s));
}
};
template < typename K >
class Compare_power_distance_2
{
public:
typedef typename K::Weighted_point_2 Weighted_point_2;
typedef typename K::Point_2 Point_2;
typedef typename K::Comparison_result Comparison_result;
typedef Comparison_result result_type;
Comparison_result operator()(const Point_2& r,
const Weighted_point_2& p,
const Weighted_point_2& q) const
{
return CGAL::compare_power_distanceH2(p.hx(), p.hy(), p.hw(), p.weight(),
q.hx(), q.hy(), q.hw(), q.weight(),
r.hx(), r.hy(), r.hw());
}
};
template <typename K>
class Compare_signed_distance_to_line_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Line_2 Line_2;
typedef typename K::Less_signed_distance_to_line_2 Less_signed_distance_to_line_2;
public:
typedef Comparison_result result_type;
result_type
operator()(const Point_2& p, const Point_2& q,
const Point_2& r, const Point_2& s) const
{
typedef typename K::RT RT;
const RT & phx = p.hx();
const RT & phy = p.hy();
const RT & phw = p.hw();
const RT & qhx = q.hx();
const RT & qhy = q.hy();
const RT & qhw = q.hw();
const RT & rhx = r.hx();
const RT & rhy = r.hy();
const RT & rhw = r.hw();
const RT & shx = s.hx();
const RT & shy = s.hy();
const RT & shw = s.hw();
RT scaled_dist_r_minus_scaled_dist_s =
( rhx*shw - shx*rhw ) * (phy*qhw - qhy*phw)
- ( rhy*shw - shy*rhw ) * (phx*qhw - qhx*phw);
return compare(scaled_dist_r_minus_scaled_dist_s, 0);
}
result_type
operator()(const Line_2& l, const Point_2& p, const Point_2& q) const
{
Less_signed_distance_to_line_2 less = K().less_signed_distance_to_line_2_object();
if (less(l, p, q)) return SMALLER;
if (less(l, q, p)) return LARGER;
return EQUAL;
}
};
template <typename K>
class Compare_slope_2
{
typedef typename K::Line_2 Line_2;
typedef typename K::Segment_2 Segment_2;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()(const Line_2& l1, const Line_2& l2) const
{
if (l1.is_horizontal())
return l2.is_vertical() ?
SMALLER : CGAL_NTS sign(l2.a()) * CGAL_NTS sign(l2.b());
if (l2.is_horizontal())
return l1.is_vertical() ?
LARGER : - CGAL_NTS sign(l1.a()) * CGAL_NTS sign(l1.b());
if (l1.is_vertical()) return l2.is_vertical() ? EQUAL : LARGER;
if (l2.is_vertical()) return SMALLER;
int l1_sign = CGAL_NTS sign(-l1.a() * l1.b());
int l2_sign = CGAL_NTS sign(-l2.a() * l2.b());
if (l1_sign < l2_sign) return SMALLER;
if (l1_sign > l2_sign) return LARGER;
if (l1_sign > 0)
return CGAL::compare( CGAL::abs(l1.a() * l2.b()),
CGAL::abs(l2.a() * l1.b()) );
return CGAL::compare( CGAL::abs(l2.a() * l1.b()),
CGAL::abs(l1.a() * l2.b()) );
} // FIXME
result_type
operator()(const Segment_2& s1, const Segment_2& s2) const
{
typedef typename K::FT FT;
typename K::Comparison_result cmp_y1 = compare_y(s1.source(), s1.target());
if (cmp_y1 == EQUAL) // horizontal
{
typename K::Comparison_result cmp_x2 = compare_x(s2.source(), s2.target());
if (cmp_x2 == EQUAL) return SMALLER;
FT s_hw = s2.source().hw();
FT t_hw = s2.target().hw();
return - CGAL_NTS sign(s2.source().hy()*t_hw - s2.target().hy()*s_hw) *
CGAL_NTS sign(s2.source().hx()*t_hw - s2.target().hx()*s_hw);
}
typename K::Comparison_result cmp_y2 = compare_y(s2.source(), s2.target());
if (cmp_y2 == EQUAL)
{
typename K::Comparison_result cmp_x1 = compare_x(s1.source(), s1.target());
if (cmp_x1 == EQUAL) return LARGER;
FT s_hw = s1.source().hw();
FT t_hw = s1.target().hw();
return CGAL_NTS sign(s1.source().hy()*t_hw - s1.target().hy()*s_hw) *
CGAL_NTS sign(s1.source().hx()*t_hw - s1.target().hx()*s_hw);
}
typename K::Comparison_result cmp_x1 = compare_x(s1.source(), s1.target());
typename K::Comparison_result cmp_x2 = compare_x(s2.source(), s2.target());
if (cmp_x1 == EQUAL)
return cmp_x2 == EQUAL ? EQUAL : LARGER;
if (cmp_x2 == EQUAL) return SMALLER;
FT s1_s_hw = s1.source().hw();
FT s1_t_hw = s1.target().hw();
FT s2_s_hw = s2.source().hw();
FT s2_t_hw = s2.target().hw();
FT s1_xdiff = s1.source().hx()*s1_t_hw - s1.target().hx()*s1_s_hw;
FT s1_ydiff = s1.source().hy()*s1_t_hw - s1.target().hy()*s1_s_hw;
FT s2_xdiff = s2.source().hx()*s2_t_hw - s2.target().hx()*s2_s_hw;
FT s2_ydiff = s2.source().hy()*s2_t_hw - s2.target().hy()*s2_s_hw;
typename K::Sign s1_sign = CGAL_NTS sign(s1_ydiff * s1_xdiff);
typename K::Sign s2_sign = CGAL_NTS sign(s2_ydiff * s2_xdiff);
if (s1_sign < s2_sign) return SMALLER;
if (s1_sign > s2_sign) return LARGER;
if (s1_sign > 0)
return CGAL_NTS sign(CGAL_NTS abs(s1_ydiff * s2_xdiff) -
CGAL_NTS abs(s2_ydiff * s1_xdiff));
return CGAL_NTS sign(CGAL_NTS abs(s2_ydiff * s1_xdiff) -
CGAL_NTS abs(s1_ydiff * s2_xdiff));
}
};
template <typename K>
class Compare_x_at_y_2
{
typedef typename K::Point_2 Point_2;
typedef typename K::Line_2 Line_2;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()( const Point_2& p, const Line_2& h) const
{
typedef typename K::RT RT;
CGAL_kernel_precondition( ! h.is_horizontal() );
typename K::Oriented_side ors = h.oriented_side( p );
if ( h.a() < RT(0) )
ors = -ors;
if ( ors == ON_POSITIVE_SIDE )
return LARGER;
return ( ors == ON_NEGATIVE_SIDE ) ? SMALLER : EQUAL;
} // FIXME
result_type
operator()( const Point_2& p, const Line_2& h1, const Line_2& h2) const
{ return CGAL::compare(h1.x_at_y( p.y() ), h2.x_at_y( p.y() )); }
// FIXME
result_type
operator()( const Line_2& l1, const Line_2& l2, const Line_2& h) const
{ return compare_x_at_y( gp_linear_intersection( l1, l2 ), h); }
// FIXME
result_type
operator()( const Line_2& l1, const Line_2& l2,
const Line_2& h1, const Line_2& h2) const
{ return compare_x_at_y( gp_linear_intersection( l1, l2 ), h1, h2 ); }
// FIXME
};
template <typename K>
class Compare_xyz_3
{
typedef typename K::Point_3 Point_3;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()( const Point_3& p, const Point_3& q) const
{
typedef typename K::RT RT;
RT pV = p.hx()*q.hw();
RT qV = q.hx()*p.hw();
if ( pV < qV )
{
return SMALLER;
}
if ( qV < pV ) // ( pV > qV )
{
return LARGER;
}
// same x
pV = p.hy()*q.hw();
qV = q.hy()*p.hw();
if ( pV < qV )
{
return SMALLER;
}
if ( qV < pV ) // ( pV > qV )
{
return LARGER;
}
// same x and y
pV = p.hz()*q.hw();
qV = q.hz()*p.hw();
return CGAL::compare(pV, qV);
}
};
template <typename K>
class Compare_xy_2
{
typedef typename K::Point_2 Point_2;
public:
typedef typename K::Comparison_result result_type;
result_type
operator()( const Point_2& p, const Point_2& q) const
{
typedef typename K::RT RT;
const RT& phx = p.hx();
const RT& phy = p.hy();
const RT& phw = p.hw();
const RT& qhx = q.hx();
const RT& qhy = q.hy();
const RT& qhw = q.hw();
RT pV = phx*qhw;
RT qV = qhx*phw;