File: Shear_transformation.h

package info (click to toggle)
cgal 4.0-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 65,068 kB
  • sloc: cpp: 500,870; ansic: 102,544; sh: 321; python: 92; makefile: 75; xml: 2
file content (1185 lines) | stat: -rw-r--r-- 41,734 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
// Copyright (c) 2006-2009 Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Algebraic_kernel_d/include/CGAL/Algebraic_kernel_d/Shear_transformation.h $
// $Id: Shear_transformation.h 67093 2012-01-13 11:22:39Z lrineau $
// 
//
// Author(s)     : Michael Kerber <mkerber@mpi-inf.mpg.de>
//
// ============================================================================

#ifndef CGAL_ACK_SHEAR_TRANSFORMATION
#define CGAL_ACK_SHEAR_TRANSFORMATION 1

#include <CGAL/basic.h>

#include <vector>

#include <CGAL/Algebraic_kernel_d/macros.h>
#include <CGAL/Algebraic_kernel_d/Bitstream_descartes.h>
#include <CGAL/Algebraic_kernel_d/Bitstream_descartes_rndl_tree_traits.h>
#include <CGAL/Algebraic_kernel_d/Bitstream_coefficient_kernel_at_alpha.h>
#include <CGAL/Algebraic_kernel_d/shear.h>

namespace CGAL {

/*!
 * The class is a functor, getting an algebraic curve and some
 * shear factor, and returning the sheared curve.
 */
template<typename AlgebraicKernelWithAnalysis_2> class Shear_transformation {

public:
      
    typedef AlgebraicKernelWithAnalysis_2  Algebraic_kernel_with_analysis_2;

    typedef typename Algebraic_kernel_with_analysis_2::Curve_analysis_2 
        Curve_analysis_2;

    typedef typename  AlgebraicKernelWithAnalysis_2::Polynomial_traits_2 
        Polynomial_traits_2;

    CGAL_ACK_SNAP_ALGEBRAIC_CURVE_KERNEL_2_TYPEDEFS(Curve_analysis_2);

    typedef std::pair<Bound,Bound> Point;

    typedef std::vector< Algebraic_real_1 > Root_container;

    typedef typename Root_container::iterator Root_iterator;
    
    typedef typename Curve_analysis_2::Event_line_iterator 
    Status_line_1_iterator;

private:

    struct Y_structure_element;

    typedef std::list<Y_structure_element> Y_structure;

    // TODO replace by something that we already have
    enum Coor_type { MINUS_INFTY,FINITE,PLUS_INFTY};

public:

    Shear_transformation(Algebraic_kernel_with_analysis_2* kernel)
        : _m_kernel(kernel),
          x_extreme_index_counter(0),
          disc_roots_computed(false),
          sh_disc_roots_computed(false)
    {}

    template<typename InputIterator>      
    void report_sheared_disc_roots(InputIterator begin,
                                   InputIterator end) {
        std::copy(begin,end,std::back_inserter(sh_disc_roots));
        sh_disc_roots_computed=true;
    }

    Curve_analysis_2 operator() (const Curve_analysis_2& C, Integer s,
                      bool use_primitive_curve=true) {
        Curve_analysis_2 D;
        this->operator() (C,s,D,use_primitive_curve);
        return D;
    }

    void operator() (const Curve_analysis_2& C, Integer s, Curve_analysis_2& D, 
                     bool use_primitive_curve=true) {
        this->C=C;
        this->s=s;
        this->use_primitive_curve = use_primitive_curve;
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Curve_analysis_2: " 
                             << C.polynomial_2() << std::endl;
        CGAL_ACK_DEBUG_PRINT << "num events: " 
                             << C.number_of_status_lines_with_event() 
                             << std::endl;
        CGAL_ACK_DEBUG_PRINT << "s: " << s << std::endl;
#endif
*/
        x_structure.clear();
        /*
          sh_disc_roots.clear();
          x_structure_info.clear();

          ev_res_roots_mults.clear();
          sh_ev_indices.clear();
          stripe_values.clear();
          pre_vert_lines.clear();
          sh_intermediate_lines.clear();
        */
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Compute the polynomials.." << std::flush;
#endif

        if(this->use_primitive_curve) {
            pol = C.primitive_polynomial_2();
        } else {
            pol=C.polynomial_2();
        }
        sh_pol=CGAL::internal::shear(pol,Coefficient(s));
        if(CGAL::degree(typename Polynomial_traits_2
                  ::Univariate_content_up_to_constant_factor()( sh_pol ))>0) {
            throw CGAL::internal::Non_generic_position_exception();
        }
        if(! D.has_defining_polynomial()) {
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "set f.." << std::flush;
#endif
            D.set_f(sh_pol);
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "done.." << std::flush;
#endif
        }
        
        der_sh_pol = typename Polynomial_traits_2::Differentiate() (sh_pol,1);
        sh_der_sh_pol = CGAL::internal::shear(der_sh_pol,Coefficient(-s));
      
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif

        Solve_1 solve_1;
        
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Store the discriminant roots.." 
                             << std::flush;
#endif

        Root_container disc_roots;
        for(Status_line_1_iterator it=C.event_begin();
            it!=C.event_end();
            it++) {
            disc_roots.push_back(it->x());
        }
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif

        if(! sh_disc_roots_computed) {
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "Compute the sheared discriminant.." 
                                 << std::flush;
#endif

            if(typename Polynomial_traits_2::Degree() (sh_pol) > 0) {
            

                Polynomial_1 sh_disc 
                    = CGAL::resultant(sh_pol,der_sh_pol);
                
#if CGAL_ACK_DEBUG_FLAG
                CGAL_ACK_DEBUG_PRINT << "root isolation.." << std::flush;
#endif
                solve_1(sh_disc,std::back_inserter(sh_disc_roots),false);
                
            }
             
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif
            sh_disc_roots_computed=true;
        }

#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Compute the event resultant.." << std::flush;
#endif
        Root_container ev_res_roots;
        if(typename Polynomial_traits_2::Degree() (sh_pol) > 0) {
            Polynomial_1 ev_res = CGAL::resultant(pol,sh_der_sh_pol);
            
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "root isolation.." << std::flush;
#endif
            solve_1(ev_res,std::back_inserter(ev_res_roots),false);
            
        }
        
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done, " << ev_res_roots.size() 
                             << " roots found" << std::endl;
#endif

#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Merge both root sets..." << std::flush;
#endif
        typename CGAL::Real_embeddable_traits<Algebraic_real_1>::Compare 
            x_compare;

        CGAL::internal::set_union_with_source
            (disc_roots.begin(),
             disc_roots.end(),
             ev_res_roots.begin(),
             ev_res_roots.end(),
             std::back_inserter(x_structure),
             std::back_inserter(x_structure_info),
             x_compare);
 
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "Take the stripe values..." << std::flush;
#endif

        CGAL::internal::find_intermediate_values
	  (kernel(),
	   sh_disc_roots.begin(),
	   sh_disc_roots.end(),
	   std::back_inserter(stripe_values));
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "Search sheared event points..." << std::flush;
#endif
        find_sheared_event_points();

#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "Find start- and endpoints for sweep.." 
                             << std::flush;
#endif
        find_far_points(D);

#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "Start sweep..." << std::flush;
#endif
        
        sweep();
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "vert lines info:" << std::endl;

        for(int i=0;i<static_cast<int>(pre_vert_lines.size());i++) {
            
            CGAL_ACK_DEBUG_PRINT << "At: " 
                                 << CGAL::to_double(sh_disc_roots[i]) << ", "
                                 << pre_vert_lines[i].number_of_non_event_roots
                                 << " non-event-roots, and " 
                                 << pre_vert_lines[i].event_points.size()
                                 << std::endl;  
        }
        CGAL_ACK_DEBUG_PRINT << "Vert_lines.." << std::flush;
#endif

        CGAL_assertion(sh_disc_roots.size()==pre_vert_lines.size());
        std::vector<Status_line_1> sh_ev_lines;
        for(int i=0;i<static_cast<int>(pre_vert_lines.size());i++) {
            sh_ev_lines.push_back(create_event_line(D,i));
        }
        D.set_event_lines(sh_ev_lines.begin(),sh_ev_lines.end(),
                          sh_intermediate_lines.begin(),
                          sh_intermediate_lines.end());
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif

    }

private:
    
    // X-coordinate of the shear of p
    Bound x_sheared(Point p,Integer sh) {
        return p.first-s*p.second;
    }
    Bound x_sheared(Bound x,Bound y,Integer sh) {
        return x-sh*y;
    }

    int compute_stripe(Status_line_1& ev, int index) {
        int left_index = -1, 
            right_index = static_cast<int>(stripe_values.size()-1);
        Algebraic_real_1 xv = ev.x();
        Bound lx = xv.low(), rx=xv.high(),
            x_iv_size = rx-lx;
        Bound ly = ev.lower_bound(index),
            ry = ev.upper_bound(index);;
        while(left_index < right_index) {
            if(x_iv_size > ry-ly) {
                xv.refine();
                lx = xv.low();
                rx=xv.high();
                x_iv_size=rx-lx;
                continue;
            }
            ev.refine(index);
            ly = ev.lower_bound(index);
            ry = ev.upper_bound(index);
            Bound right(0), left(0);
            left  = (s>0) ? x_sheared(lx,ry,s) : x_sheared(lx,ly,s);
            right = (s>0) ? x_sheared(rx,ly,s) : x_sheared(rx,ry,s);
            CGAL_assertion(left<right);
            while(left_index<right_index && stripe_values[left_index+1]<left) {
                ++left_index;
            }
            while(left_index<right_index && right<stripe_values[right_index]) {
                --right_index;
            }
        }
        CGAL_assertion(left_index==right_index);
        return left_index;

    }

    void find_far_points(Curve_analysis_2& D) {
        int n = static_cast<int>(stripe_values.size());
        Bound upper_bound,lower_bound;
        Bound left_bound = stripe_values[0],
            right_bound=stripe_values[n-1];
        lower_bound = upper_bound = Bound(0);
        for(int i=0;i<n;i++) {
            Algebraic_real_1 curr_bound(stripe_values[i]);
            Bitstream_traits traits(Bitstream_coefficient_kernel
                                    (kernel(),curr_bound));
            CGAL::internal::Square_free_descartes_tag tag;
            Bitstream_descartes descartes(tag,sh_pol,traits);
            int m = descartes.number_of_real_roots();
            if(m>0) {
                if(descartes.left_bound(0)<lower_bound) {
                    lower_bound = descartes.left_bound(0);
                }
                if(descartes.right_bound(m-1) 
                   > upper_bound) {
                    upper_bound = descartes.right_bound(m-1);
                }
            }
            // Create intermediate line for later use
            Algebraic_real_1 xval(curr_bound);
            Status_line_1 inter_line(xval,i,D,m);
            inter_line.set_isolator(descartes);
            sh_intermediate_lines.push_back(inter_line);
        }
        far_left=(s<0) ? x_sheared(left_bound,upper_bound,-s)
            : x_sheared(left_bound,lower_bound,-s)-1;
        far_right=(s<0) ? x_sheared(right_bound,lower_bound,-s)
            : x_sheared(right_bound,upper_bound,-s)+1;
        if(C.number_of_status_lines_with_event()>0) {
            if(far_left>C.status_line_at_event(0).x().low()) {
                far_left = C.status_line_at_event(0).x().low();
            }   
            if(far_right<C.status_line_at_event
               (C.number_of_status_lines_with_event()-1).x().high()) {
                far_right = C.status_line_at_event
                    (C.number_of_status_lines_with_event()-1).x().high();
            }
        }
        // just to be sure...
        far_left = far_left-1;
        far_right = far_right+1;
        y_in_box = (upper_bound + lower_bound)/2;
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "far_left: " << far_left << "\nfar_right: " 
                             << far_right << "\ny_in_box: " << y_in_box 
                             << std::endl;
#endif
*/
    }

    template<class OutputIterator>
    void find_sheared_event_points_at_x(const Status_line_1& ev,
                                        Status_line_1& left,
                                        Status_line_1& right,
                                        OutputIterator out) {

        int ev_id = 0, left_id=0, right_id=0;
        int ev_n = ev.number_of_events(),
            left_n = left.number_of_events(),
            right_n = right.number_of_events();
        (void)left_n;
        (void)right_n;
        // Simple if a vertical line exists
        if(ev.covers_line() && ! use_primitive_curve) {
            for(int i=0;i<ev_n;i++) {
                out++=i;
            }
            return;
        }

        // left side
        
        CGAL_assertion(left.x().is_rational());
        Bound left_x = left.x().rational();
        
	Polynomial_1 left_pol = kernel()->evaluate_utcf_2_object()
	  (typename Polynomial_traits_2::Swap() (pol, 0, 1),
	   left_x);
        Polynomial_1 left_sh_der_sh_pol = kernel()->evaluate_utcf_2_object()
	  (typename Polynomial_traits_2::Swap() (sh_der_sh_pol, 0, 1),
	   left_x);

        // right side

      
        CGAL_assertion(right.x().is_rational());
        Bound right_x = right.x().rational();

	Polynomial_1 right_pol = kernel()->evaluate_utcf_2_object()
	  (typename Polynomial_traits_2::Swap() (pol, 0, 1),
	   right_x);
        Polynomial_1 right_sh_der_sh_pol = kernel()->evaluate_utcf_2_object()
	  (typename Polynomial_traits_2::Swap() (sh_der_sh_pol, 0, 1),
	   right_x);

        int asym_left_minus,asym_left_plus,asym_right_minus,asym_right_plus;
        
        typedef typename Status_line_1::Arc_pair Arc_pair;
        
        Arc_pair apair1 = ev.number_of_branches_approaching_minus_infinity();
        Arc_pair apair2 = ev.number_of_branches_approaching_plus_infinity();

        asym_left_minus = apair1.first;
        asym_right_minus = apair1.second;

        asym_left_plus = apair2.first;
        asym_right_plus = apair2.second;

        left_id += asym_left_minus;
        right_id += asym_right_minus;
        while(ev_id != ev_n) {
        
            typename Status_line_1::Arc_pair arc_pair = 
                ev.number_of_incident_branches(ev_id);

            int arcs_left = arc_pair.first;
            int arcs_right = arc_pair.second;
            if(arcs_left+arcs_right!=2) {
/*
#if CGAL_ACK_DEBUG_FLAG
                CGAL_ACK_DEBUG_PRINT << "Sheared event point found at " 
                                     << ev.x().to_double() << ", index " 
                                     << ev_id << std::endl;
#endif
*/
                out++=ev_id;
                left_id+=arcs_left;
                right_id+=arcs_right;
                ev_id++;
            }
            else {
                if(arcs_left==1 && arcs_right==1) {

                    Algebraic_real_1 left_y(left_pol,
                                          left.lower_bound(left_id),
                                          left.upper_bound(left_id));

                    CGAL::Sign left_sign 
		      = kernel()->sign_at_1_object()
		          (left_sh_der_sh_pol,left_y,true);

                    Algebraic_real_1 right_y(right_pol,
                                           right.lower_bound(right_id),
                                           right.upper_bound(right_id));

                    CGAL::Sign right_sign 
		      = kernel()->sign_at_1_object()
		          (right_sh_der_sh_pol,right_y,true);

                    if(left_sign!=right_sign) {
/*
#if CGAL_ACK_DEBUG_FLAG
                        CGAL_ACK_DEBUG_PRINT << "Sheared ev point found at " 
                                             << ev.x().to_double() 
                                             << ", index " << ev_id 
                                             << std::endl;
#endif
*/
                        out++=ev_id;
                    }
                    ev_id++;
                    left_id++;
                    right_id++;
                }
                else if(arcs_left==2 && arcs_right==0) {
                    Algebraic_real_1 left_y_1(left_pol,
                                          left.lower_bound(left_id),
                                          left.upper_bound(left_id));

                    CGAL::Sign left_sign_1 
		      = kernel()->sign_at_1_object()
		          (left_sh_der_sh_pol,left_y_1,true);

                    left_id++;
                    Algebraic_real_1 left_y_2(left_pol,
                                          left.lower_bound(left_id),
                                          left.upper_bound(left_id));
                    CGAL::Sign left_sign_2 
		      = kernel()->sign_at_1_object()
		          (left_sh_der_sh_pol,left_y_2,true);
                    if(left_sign_1!=left_sign_2) {
/*
#if CGAL_ACK_DEBUG_FLAG
                        CGAL_ACK_DEBUG_PRINT << "Sheared ev point found at " 
                                             << ev.x().to_double() 
                                             << ", index " << ev_id 
                                             << std::endl;
#endif
*/
                        out++=ev_id;
                    }
                    ev_id++;
                    left_id++;
                }
                else if(arcs_left==0 && arcs_right==2) {
                    Algebraic_real_1 right_y_1(right_pol,
                                               right.lower_bound(right_id),
                                               right.upper_bound(right_id));

                    CGAL::Sign right_sign_1 
		      = kernel()->sign_at_1_object()
		          (right_sh_der_sh_pol,right_y_1,true);
                    right_id++;
                    Algebraic_real_1 right_y_2(right_pol,
                                               right.lower_bound(right_id),
                                               right.upper_bound(right_id));
                    CGAL::Sign right_sign_2 
		      = kernel()->sign_at_1_object()
		          (right_sh_der_sh_pol,right_y_2,true);
                    if(right_sign_1!=right_sign_2) {
/*
#if CGAL_ACK_DEBUG_FLAG
                        CGAL_ACK_DEBUG_PRINT << "Sheared ev point found at " 
                                             << ev.x().to_double()  
                                             << ", index " << ev_id 
                                             << std::endl;
#endif
*/
                        out++=ev_id;
                    }
                    ev_id++;
                    right_id++;
                }
            }
        }
        left_id += asym_left_plus;
        right_id += asym_right_plus;
        CGAL_assertion(ev_id==ev_n);
        CGAL_assertion(left_id==left_n);
        CGAL_assertion(right_id==right_n);
    }

    void find_sheared_event_points() {

        sh_ev_indices.resize(x_structure.size());
        std::vector<Bound> intermediate_values;
        find_intermediate_values(kernel(),
				 x_structure.begin(),
                                 x_structure.end(),
                                 std::back_inserter(intermediate_values));
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "interline.." << std::flush;
#endif
*/
        std::vector<Status_line_1> intermediate_lines(intermediate_values.size());

        int i=0;
        for(typename std::vector<Bound>::iterator it 
                = intermediate_values.begin();
            it!=intermediate_values.end();it++) {
            intermediate_lines[i]=C.status_line_at_exact_x(*it);
            i++;
        }
        int event_count=0;
/*
#if CGAL_ACK_DEBUG_FLAG        
        CGAL_ACK_DEBUG_PRINT << "at some x.." << std::flush;
#endif
*/
        for(int i=0;i<static_cast<int>(x_structure.size());i++) {
            CGAL::internal::Three_valued info = x_structure_info[i];
/*
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT <<  i << "th of " << x_structure.size() 
                                 << std::endl;
            CGAL_ACK_DEBUG_PRINT << "To_double approx" << std::endl;
            CGAL_ACK_DEBUG_PRINT << "x_struct: " 
                                 << CGAL::to_double(x_structure[i]) 
                                 << std::endl;
            CGAL_ACK_DEBUG_PRINT << "Info: " << info << std::endl;
#endif
*/
            if(info==CGAL::internal::ROOT_OF_SECOND_SET || 
               info==CGAL::internal::ROOT_OF_BOTH_SETS) {
                const Status_line_1& event_line_at_x = 
                    (info==CGAL::internal::ROOT_OF_BOTH_SETS)
                    ? C.status_line_at_event(event_count) : C.status_line_at_exact_x(x_structure[i]);
/*
#if CGAL_ACK_DEBUG_FLAG
                CGAL_ACK_DEBUG_PRINT << "now really at x.." << std::flush;
#endif
*/
                find_sheared_event_points_at_x(event_line_at_x,
                                               intermediate_lines[i],
                                               intermediate_lines[i+1],
                                               std::back_inserter
                                               (sh_ev_indices[i]));

/*
#if CGAL_ACK_DEBUG_FLAG
                CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif
*/
            }
            if(info==CGAL::internal::ROOT_OF_FIRST_SET || 
               info==CGAL::internal::ROOT_OF_BOTH_SETS) {
                event_count++;
            }
        }

        CGAL_assertion(event_count==C.number_of_status_lines_with_event());
    }
    
    struct Sh_ev_point_info {

        Sh_ev_point_info(Status_line_1 ev,int index) 
            : ev(ev),index(index),
              incident_left(0),
              incident_right(0)
        {}

        Status_line_1 ev;
        int index;
        int incident_left;
        int incident_right;
    };

    struct Sh_ev_line_info {
        int asym_left_plus,asym_left_minus,asym_right_plus,asym_right_minus;
        int number_of_non_event_roots;
        std::vector<Sh_ev_point_info> event_points;
      
        Bound lower_bound(int i) {
            Sh_ev_point_info p= event_points[i];
            return p.ev.lower_bound(p.index);
        }
        Bound upper_bound(int i) {
            Sh_ev_point_info p= event_points[i];
            return p.ev.upper_bound(p.index);
        }
        void refine(int i) {
            Sh_ev_point_info p= event_points[i];
            p.ev.refine(p.index);
        }

        int num_arcs_left() {
            int sum=0;
            sum+=asym_left_plus+asym_left_minus;
            sum+=number_of_non_event_roots;
            for(int k=0;k<static_cast<int>(event_points.size());k++) {
                sum+=event_points[k].incident_left;
            }
            return sum;
        }

        int num_arcs_right() {
            int sum=0;
            sum+=asym_right_plus+asym_right_minus;
            sum+=number_of_non_event_roots ;
            for(int k=0;k<static_cast<int>(event_points.size());k++) {
                sum+=event_points[k].incident_right;
            }
            return sum;
        }

    };

    struct Y_structure_element {
        bool one_event_known;
        Coor_type x_type;
        int x_index;
        Coor_type y_type;
        int y_index;
    };

    Y_structure_element create_unbounded_element(Status_line_1& ev, int i) {
        int n = static_cast<int>(stripe_values.size());
        int stripe = compute_stripe(ev,i);
        Y_structure_element y_el;
        y_el.one_event_known=true;
        if(stripe==-1) {
            y_el.x_type=MINUS_INFTY;
        } else if(stripe==n-1) {
            y_el.x_type=PLUS_INFTY;
        } else {
            y_el.x_type=FINITE;
            y_el.x_index=stripe;
            while((ev.upper_bound(i)>y_in_box)  &&
                  (ev.lower_bound(i)<y_in_box)) {
                ev.refine(i);
            }
            if(ev.upper_bound(i)<y_in_box) {
                y_el.y_type=MINUS_INFTY;
            }
            else {
                y_el.y_type=PLUS_INFTY;
            }
        }
        return y_el;
    }

    Y_structure_element create_event(Status_line_1& ev, int i) {
        int n = static_cast<int>(stripe_values.size());
        (void)n;
        int stripe = compute_stripe(ev,i);
        CGAL_assertion(stripe>=0 && stripe<=n);
        Y_structure_element y_el;
        y_el.one_event_known=true;
        y_el.x_type=FINITE;
        y_el.x_index=stripe;
        Sh_ev_point_info ev_info(ev,i);
        pre_vert_lines[stripe].event_points.push_back(ev_info);
        y_el.y_type=FINITE;
        y_el.y_index=static_cast<int>
            (pre_vert_lines[stripe].event_points.size()-1);
        return y_el;
    }

    void start_sweep() {
        y_structure.clear();
        for(int i=0;i<static_cast<int>(sh_disc_roots.size());i++) {
            Sh_ev_line_info info;
            info.number_of_non_event_roots=0;
            info.asym_left_plus=info.asym_left_minus=
                info.asym_right_plus=info.asym_right_minus=0;
            pre_vert_lines.push_back(info);
        }
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "X-coordinate (far left) " 
                             << CGAL::to_double(far_left) << std::endl;
#endif
*/
        Status_line_1 far_left_line 
            = C.status_line_at_exact_x(Algebraic_real_1(far_left));
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "No. arcs " 
                             << far_left_line.number_of_events() << std::endl;
#endif
*/
        for(int i=0;i<far_left_line.number_of_events();i++) {
            y_structure.push_back(create_unbounded_element(far_left_line,i));
        }
    }

    
    void end_sweep() {
        Status_line_1 far_right_line 
            = C.status_line_at_exact_x(Algebraic_real_1(far_right));
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "X-coordinate (far right) " 
                             << CGAL::to_double(far_right) << std::endl;
#endif
*/
        CGAL_assertion(far_right_line.number_of_events()
                       ==static_cast<int>(y_structure.size()));
      
        typename Y_structure::iterator y_it=y_structure.begin();
        for(int i=0;i<far_right_line.number_of_events();i++) {
            Y_structure_element y_el 
                = create_unbounded_element(far_right_line,i);
            handle_edge(*y_it,y_el);
            y_it++;
        }
    }

    void sweep_at_x_coordinate(int index) {
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "X-coordinate " 
                             << CGAL::to_double(x_structure[index]) 
                             << std::endl;
#endif
*/
        std::vector<int>::iterator sh_ev_it = sh_ev_indices[index].begin();
        Status_line_1 ev=C.status_line_at_exact_x(x_structure[index]);
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "EV: " << std::endl << ev << std::endl;
#endif
*/
        int ev_id=0, ev_n=ev.number_of_events();
        typename Y_structure::iterator y_it=y_structure.begin();
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Y-structure has " << y_structure.size() 
                             << " elements" << std::endl;
#endif
*/
        // needed for vertical components
        std::vector<Y_structure_element> events_at_x; 
        bool vert=ev.covers_line() && ! this->use_primitive_curve;
        Y_structure_element below,above;
        Y_structure_element minus_x_inf,plus_x_inf;
        minus_x_inf.one_event_known = plus_x_inf.one_event_known=true;
        minus_x_inf.x_type=MINUS_INFTY;
        plus_x_inf.x_type = PLUS_INFTY;
        below = (s>0) ? plus_x_inf : minus_x_inf;
        above = (s>0) ? minus_x_inf: plus_x_inf;
        events_at_x.push_back(below);
        int minus_left,minus_right,plus_left,plus_right;
        
        typedef typename Status_line_1::Arc_pair Arc_pair;
        
        Arc_pair apair1 = ev.number_of_branches_approaching_minus_infinity();
        Arc_pair apair2 = ev.number_of_branches_approaching_plus_infinity();

        minus_left = apair1.first;
        minus_right = apair1.second;

        plus_left = apair2.first;
        plus_right = apair2.second;

        for(int i=0;i<minus_left;i++) {
            handle_edge(*y_it,below);
            y_it=y_structure.erase(y_it);
        }
        for(int i=0;i<minus_right;i++) {
            y_it=y_structure.insert(y_it,below);
            y_it++;
        }
        while(ev_id<ev_n) {
            typename Status_line_1::Arc_pair arc_pair 
                = ev.number_of_incident_branches(ev_id);
            int left_arcs=arc_pair.first;
            int right_arcs=arc_pair.second;
            if(sh_ev_it!=sh_ev_indices[index].end() && ev_id==*sh_ev_it) {
                Y_structure_element y_ev = create_event(ev,ev_id);
                events_at_x.push_back(y_ev);
                //y_struct_info(y_ev);
                for(int i=0;i<left_arcs;i++) {
                    handle_edge(*y_it,y_ev);
                    y_it=y_structure.erase(y_it);
                }
                for(int i=0;i<right_arcs;i++) {
                    y_it=y_structure.insert(y_it,y_ev);
                    y_it++;
                }
                sh_ev_it++;
            } else {
                CGAL_assertion(left_arcs+right_arcs==2);
                if(left_arcs==1 && right_arcs==1) {
                    y_it++;
                }
                else if(left_arcs==2 && right_arcs==0) {
                    Y_structure_element y1,y2;
                    y1=*y_it;
                    y_it=y_structure.erase(y_it);
                    y2=*y_it;
                    y_it=y_structure.erase(y_it);
                    handle_edge(y1,y2);
                }
                else {
                    Y_structure_element new_y;
                    new_y.one_event_known=false;
                    new_y.x_index=x_extreme_index_counter;
            
                    // Prevent compiler warnings:
                    new_y.y_index=-1;
                    new_y.y_type=FINITE;
                    new_y.x_type=FINITE;
            
                    y_it=y_structure.insert(y_it,new_y);
                    y_it++;
                    y_it=y_structure.insert(y_it,new_y);
                    y_it++;
                    x_extreme_index_counter++;
                }     
            }
            ev_id++;
        }
        for(int i=0;i<plus_left;i++) {
            handle_edge(*y_it,above);
            y_it=y_structure.erase(y_it);
        }
        for(int i=0;i<plus_right;i++) {
            y_it=y_structure.insert(y_it,above);
            y_it++;
        }
        CGAL_assertion(ev_id==ev_n);
        CGAL_assertion(y_it==y_structure.end());
        CGAL_assertion(sh_ev_it==sh_ev_indices[index].end());
        if(vert) {
            events_at_x.push_back(above);
            // edges corresponding to vertical segments...
            for(int i=1;i<static_cast<int>(events_at_x.size());i++) {
                handle_edge(events_at_x[i-1],events_at_x[i]);
            }
        }
    }

    void y_struct_info(Y_structure_element e1,
                       std::ostream& out) {
        if(!e1.one_event_known) {
            out << "dummy node with id " << e1.x_index << std::endl;
        } else {
            if(e1.x_type==MINUS_INFTY) {
                out << "Point at -infty" << std::endl;
            } else if(e1.x_type==PLUS_INFTY) {
                out << "point at +infty" << std::endl;
            } else {
                out << "point at index" << e1.x_index;
                if(e1.y_type==MINUS_INFTY) {
                    out << " y-coor: -infty" << std::endl;
                } else if(e1.y_type==PLUS_INFTY) {
                    out << " y-coor: +infty" << std::endl;
                } else {
                    out << " y-id: " << e1.y_index << std::endl;
                }

            }
        }
    }
    
    void handle_edge(Y_structure_element& e1,
                     Y_structure_element& e2) {
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Y-STRUCT: " << std::endl;
        for(typename Y_structure::iterator it=y_structure.begin();
            it!=y_structure.end();it++) {
            y_struct_info(*it,CGAL_ACK_DEBUG_PRINT);
        }
        CGAL_ACK_DEBUG_PRINT << "Y-STRUCT done" << std::endl;
      
        CGAL_ACK_DEBUG_PRINT << "handle edge..." << std::flush;
        CGAL_ACK_DEBUG_PRINT << "info for e1: ";
        y_struct_info(e1,CGAL_ACK_DEBUG_PRINT);
        CGAL_ACK_DEBUG_PRINT << "info for e2: ";
        y_struct_info(e2,CGAL_ACK_DEBUG_PRINT);
#endif
*/
        if(! e1.one_event_known) {
            int id = e1.x_index;
            typename Y_structure::iterator it=y_structure.begin();
            while(it!=y_structure.end()) {
                if((! it->one_event_known) && it->x_index==id) {
                    //it=y_structure.erase(it);
                    //it=y_structure.insert(it,e2);
                    *it=e2;
                }
                it++;
            }
        } else if(! e2.one_event_known) {
            int id = e2.x_index;
            typename Y_structure::iterator it=y_structure.begin();
            while(it!=y_structure.end()) {
                if((! it->one_event_known) && it->x_index==id) {
                    //it=y_structure.erase(it);
                    //it=y_structure.insert(it,e1);
                    *it=e1;
                }
                it++;
            }
        } else {
            CGAL_assertion(e1.x_type!=e2.x_type || 
                           e1.x_type==FINITE || 
                           e2.x_type==FINITE);
            if(e2.x_type==MINUS_INFTY || e1.x_type==PLUS_INFTY) {
                handle_edge(e2,e1);
                return;
            } else if(e1.x_type==FINITE && e2.x_type==FINITE) {
                CGAL_assertion(e1.x_index!=e2.x_index);
                if(e1.x_index>e2.x_index) {
                    handle_edge(e2,e1);
                    return;
                }
            }
            int left_stripe = (e1.x_type==MINUS_INFTY) ? -1 : e1.x_index;
            int right_stripe = (e2.x_type==PLUS_INFTY) 
                ? static_cast<int>(stripe_values.size()-1) : e2.x_index;
            for(int i=left_stripe+1;i<right_stripe;i++) {
                pre_vert_lines[i].number_of_non_event_roots++;
            }
            if(e1.x_type==FINITE) {
                switch(e1.y_type) {
                case(PLUS_INFTY) : {
                    pre_vert_lines[e1.x_index].asym_right_plus++;
                    break;
                }
                case(MINUS_INFTY) : {
                    pre_vert_lines[e1.x_index].asym_right_minus++;
                    break;
                }
                case(FINITE): {
                    pre_vert_lines[e1.x_index].
                        event_points[e1.y_index].incident_right++;
                    break;
                }
                }
            }
            if(e2.x_type==FINITE) {
                switch(e2.y_type) {
                case(PLUS_INFTY) : {
                    pre_vert_lines[e2.x_index].asym_left_plus++;
                    break;
                }
                case(MINUS_INFTY) : {
                    pre_vert_lines[e2.x_index].asym_left_minus++;
                    break;
                }
                case(FINITE): {
                    pre_vert_lines[e2.x_index].
                        event_points[e2.y_index].incident_left++;
                    break;
                }
                }
            }   
        }
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "handle edge done" << std::endl;
#endif
*/
      
    }

    void sweep() {
/*
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Init.." << std::flush;
#endif
*/
        start_sweep();
      
/*
#if CGAL_ACK_DEBUG_FLAG
        for(typename Y_structure::iterator it=y_structure.begin();
            it!=y_structure.end();
            it++) {
            y_struct_info(*it,CGAL_ACK_DEBUG_PRINT);
          }

        CGAL_ACK_DEBUG_PRINT << "End of y-struct" << std::endl;
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif
*/
        for(int i=0;i<static_cast<int>(x_structure.size());i++) {
/*
#if CGAL_ACK_DEBUG_FLAG
            CGAL_ACK_DEBUG_PRINT << "Coordinate.." 
                                 << CGAL::to_double(x_structure[i]) 
                                 << ", index " << i << std::endl;
            CGAL_ACK_DEBUG_PRINT <<  << "Y-struct before x" << std::endl;
            for(typename Y_structure::iterator it=y_structure.begin();
                it!=y_structure.end();
                it++) {
                y_struct_info(*it,CGAL_ACK_DEBUG_PRINT);
            }
            
            CGAL_ACK_DEBUG_PRINT << "End of y-struct" << std::endl;
#endif
*/
            sweep_at_x_coordinate(i);
        }
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "Terminate.." << std::flush;
#endif
        end_sweep();
#if CGAL_ACK_DEBUG_FLAG
        CGAL_ACK_DEBUG_PRINT << "done" << std::endl;
#endif
    }

    Status_line_1 create_event_line(Curve_analysis_2& D,int i) {
        Algebraic_real_1 xval = sh_disc_roots[i];
        Bitstream_traits traits(Bitstream_coefficient_kernel(kernel(),xval));
        int number_of_events 
            = static_cast<int>(pre_vert_lines[i].event_points.size());
        int number_of_roots 
            = pre_vert_lines[i].number_of_non_event_roots+number_of_events; 
        Polynomial_2 sh_pol_with_correct_degree 
            = CGAL::internal::poly_non_vanish_leading_term(kernel(),sh_pol,xval);
        Bitstream_descartes descartes(CGAL::internal::Backshear_descartes_tag(),
                                      sh_pol_with_correct_degree,
                                      number_of_roots,
                                      number_of_events,
                                      pre_vert_lines[i],
                                      traits);
        typename Status_line_1::Arc_container arc_container;
        for(int j=0;j<descartes.number_of_real_roots();j++) {
            if(descartes.is_certainly_multiple_root(j)) {
                int n = static_cast<int>(pre_vert_lines[i].event_points.size());
                int k=0;
                while(k<n) {
                    if((pre_vert_lines[i].lower_bound(k)
                        <=descartes.right_bound(j)) &&
                       (pre_vert_lines[i].upper_bound(k)
                        >=descartes.left_bound(j))) {
                        break;
                    }
                    else {
                        k++;
                    }
                }
                CGAL_assertion(k<n);
                int left_arcs=pre_vert_lines[i].event_points[k].incident_left;
                int right_arcs=pre_vert_lines[i].event_points[k].incident_right;
                arc_container.push_back(std::make_pair(left_arcs,right_arcs));
            } else {
                arc_container.push_back(std::make_pair(1,1));
            }
        }
        Status_line_1 ev(xval,i,D,
                         pre_vert_lines[i].num_arcs_left(),
                         pre_vert_lines[i].num_arcs_right(),
                         arc_container);
        ev.set_isolator(descartes);
        ev._set_number_of_branches_approaching_infinity
            (std::make_pair(pre_vert_lines[i].asym_left_minus,
                            pre_vert_lines[i].asym_right_minus),
             std::make_pair(pre_vert_lines[i].asym_left_plus,
                            pre_vert_lines[i].asym_right_plus));
        return ev;
    }

    Algebraic_kernel_with_analysis_2* kernel() const {
        return _m_kernel;
    }

    Algebraic_kernel_with_analysis_2* _m_kernel;
  
    Curve_analysis_2 C;

    Integer s;

    Polynomial_2 pol, sh_pol, der_sh_pol,sh_der_sh_pol;

    Root_container sh_disc_roots,x_structure;
    std::vector<CGAL::internal::Three_valued> x_structure_info;
    
    std::vector<std::vector<int> > sh_ev_indices;

    std::vector<Bound> stripe_values;                   

    Bound far_left, far_right, y_in_box;

    Y_structure y_structure;

    std::vector<Sh_ev_line_info> pre_vert_lines;

    int x_extreme_index_counter;

    std::vector<Status_line_1> sh_intermediate_lines;

    bool use_primitive_curve;

    bool disc_roots_computed,sh_disc_roots_computed;

};
 
} //namespace CGAL

#endif