~centralelyon2010/inkscape/imagelinks2

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
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/*
 * Authors:
 *      Nathan Hurst <njh@njhurst.com
 *
 * Copyright 2009  authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */


#include <2geom/conicsec.h>
#include <2geom/conic_section_clipper.h>
#include <2geom/numeric/fitting-tool.h>
#include <2geom/numeric/fitting-model.h>


// File: convert.h
#include <utility>
#include <sstream>
#include <stdexcept>





namespace Geom
{

LineSegment intersection(Line l, Rect r) {
    Point p0, p1;
    double a,b,c;
    std::vector<double> ifc = l.coefficients();
    a = ifc[0];
    b = ifc[1];
    c = ifc[2];
    if (fabs(b) > fabs(a)) {
        p0 = Point(r[0][0], (-c - a*r[0][0])/b);
        if (p0[1] < r[1][0])
            p0 = Point((-c - b*r[1][0])/a, r[1][0]);
        if (p0[1] > r[1][1])
            p0 = Point((-c - b*r[1][1])/a, r[1][1]);
        p1 = Point(r[0][1], (-c - a*r[0][1])/b);
        if (p1[1] < r[1][0])
            p1 = Point((-c - b*r[1][0])/a, r[1][0]);
        if (p1[1] > r[1][1])
            p1 = Point((-c - b*r[1][1])/a, r[1][1]);
    } else {
        p0 = Point((-c - b*r[1][0])/a, r[1][0]);
        if (p0[0] < r[0][0])
            p0 = Point(r[0][0], (-c - a*r[0][0])/b);
        if (p0[0] > r[0][1])
            p0 = Point(r[0][1], (-c - a*r[0][1])/b);
        p1 = Point((-c - b*r[1][1])/a, r[1][1]);
        if (p1[0] < r[0][0])
            p1 = Point(r[0][0], (-c - a*r[0][0])/b);
        if (p1[0] > r[0][1])
            p1 = Point(r[0][1], (-c - a*r[0][1])/b);
    }
    return LineSegment(p0, p1);
}

static double det(Point a, Point b) {
    return a[0]*b[1] - a[1]*b[0];
}

template <typename T>
static T det(T a, T b, T c, T d) {
    return a*d - b*c;
}

template <typename T>
static T det(T M[2][2]) {
    return M[0][0]*M[1][1] - M[1][0]*M[0][1];
}

template <typename T>
static T det3(T M[3][3]) {
    return ( M[0][0] * det(M[1][1], M[1][2],
                           M[2][1], M[2][2])
             -M[1][0] * det(M[0][1], M[0][2],
                            M[2][1], M[2][2])
             +M[2][0] * det(M[0][1], M[0][2],
                            M[1][1], M[1][2]));
}

static double boxprod(Point a, Point b, Point c) {
    return det(a,b) - det(a,c) + det(b,c);
}


/**
 * Find the roots of (q2x + q1)x+q0 = 0
 * Tries to be numerically robust.
 */
template <typename T>
static std::vector<T> quadratic_roots(T q0, T q1, T q2) {
    std::vector<double> r;
    if(q2 == 0) {
        if(q1 == 0) { // zero or infinite roots
            return r;
        }
        r.push_back(-q0/q1);
    } else {
        double desc = q1*q1 - 4*q2*q0;
        /*cout << q2 << ", "
          << q1 << ", "
          << q0 << "; "
          << desc << "\n";*/
        if (desc < 0)
            return r;
        else if (desc == 0)
            r.push_back(-q1/(2*q2));
        else {
            desc = std::sqrt(desc);
            double t = -0.5*(q1+sgn(q1)*desc);
            r.push_back(t/q2);
            r.push_back(q0/t);
        }
    }
    return r;
}



class BadConversion : public std::runtime_error {
public:
    BadConversion(const std::string& s)
        : std::runtime_error(s)
    { }
};

template <typename T>
inline std::string stringify(T x)
{
    std::ostringstream o;
    if (!(o << x))
        throw BadConversion("stringify(T)");
    return o.str();
}

  /* A G4 continuous cubic parametric approximation for rational quadratics.
     See
  An analysis of cubic approximation schemes for conic sections
            Michael Floater
            SINTEF

     This is less accurate overall than some of his other schemes, but
     produces very smooth joins and is still optimally h^-6
     convergent.
  */

double RatQuad::lambda() const {
  return 2*(6*w*w +1 -std::sqrt(3*w*w+1))/(12*w*w+3);
}

RatQuad RatQuad::fromPointsTangents(Point P0, Point dP0,
                       Point P,
                       Point P2, Point dP2) {
  Line Line0 = Line::from_origin_and_versor(P0, dP0);
  Line Line2 = Line::from_origin_and_versor(P2, dP2);
  try {
    OptCrossing oc = intersection(Line0, Line2);
    if(!oc) // what to do?
        return RatQuad(Point(), Point(), Point(), 0); // need opt really
    //assert(0);
    Point P1 = Line0.pointAt((*oc).ta);
    double triarea = boxprod(P0, P1, P2);
//    std::cout << "RatQuad::fromPointsTangents: triarea = " << triarea << std::endl;
    if (triarea == 0)
    {
        return RatQuad(P0, 0.5*(P0+P2), P2, 1);
    }
    double tau0 = boxprod(P, P1, P2)/triarea;
    double tau1 = boxprod(P0, P, P2)/triarea;
    double tau2 = boxprod(P0, P1, P)/triarea;
    if (tau0 == 0 || tau1 == 0 || tau2 == 0)
    {
        return RatQuad(P0, 0.5*(P0+P2), P2, 1);
    }
    double w = tau1/(2*std::sqrt(tau0*tau2));
//    std::cout << "RatQuad::fromPointsTangents: tau0 = " << tau0 << std::endl;
//    std::cout << "RatQuad::fromPointsTangents: tau1 = " << tau1 << std::endl;
//    std::cout << "RatQuad::fromPointsTangents: tau2 = " << tau2 << std::endl;
//    std::cout << "RatQuad::fromPointsTangents: w = " << w << std::endl;
    return  RatQuad(P0, P1, P2, w);
  } catch(Geom::InfiniteSolutions) {
    return RatQuad(P0, 0.5*(P0+P2), P2, 1);
  }
  return RatQuad(Point(), Point(), Point(), 0); // need opt really
}

RatQuad RatQuad::circularArc(Point P0, Point P1, Point P2) {
    Line Line0 = Line::from_origin_and_versor(P0, P1 - P0);
    Line Line2 = Line::from_origin_and_versor(P2, P1 - P2);
    return RatQuad(P0, P1, P2, dot(unit_vector(P0 - P1), unit_vector(P0 - P2)));
}


CubicBezier RatQuad::toCubic() const {
    return toCubic(lambda());
}

CubicBezier RatQuad::toCubic(double lamb) const {
  return CubicBezier(P[0],
		     (1-lamb)*P[0] + lamb*P[1],
		     (1-lamb)*P[2] + lamb*P[1],
		     P[2]);
}

Point RatQuad::pointAt(double t) const {
  Bezier xt(P[0][0], P[1][0]*w, P[2][0]);
  Bezier yt(P[0][1], P[1][1]*w, P[2][1]);
  double wt = Bezier(1, w, 1).valueAt(t);
  return Point(xt.valueAt(t)/wt,
	       yt.valueAt(t)/wt);
}

void RatQuad::split(RatQuad &a, RatQuad &b) const {
  a.P[0] = P[0];
  b.P[2] = P[2];
  a.P[1] = (P[0]+w*P[1])/(1+w);
  b.P[1] = (w*P[1]+P[2])/(1+w);
  a.w = b.w = std::sqrt((1+w)/2);
  a.P[2] = b.P[0] = (0.5*a.P[1]+0.5*b.P[1]);
}


D2<SBasis> RatQuad::hermite() const {
  SBasis t = Linear(0, 1);
  SBasis omt = Linear(1, 0);

  D2<SBasis> out(omt*omt*P[0][0]+2*omt*t*P[1][0]*w+t*t*P[2][0],
		 omt*omt*P[0][1]+2*omt*t*P[1][1]*w+t*t*P[2][1]);
  for(int dim = 0; dim < 2; dim++) {
    out[dim] = divide(out[dim], (omt*omt+2*omt*t*w+t*t), 2);
  }
  return out;
}

  std::vector<SBasis> RatQuad::homogenous() const {
    std::vector<SBasis> res(3, SBasis());
  Bezier xt(P[0][0], P[1][0]*w, P[2][0]);
  bezier_to_sbasis(res[0],xt);
  Bezier yt(P[0][1], P[1][1]*w, P[2][1]);
  bezier_to_sbasis(res[1],yt);
  Bezier wt(1, w, 1);
  bezier_to_sbasis(res[2],wt);
  return res;
}

#if 0
  std::string xAx::categorise() const {
  double M[3][3] = {{c[0], c[1], c[3]},
		    {c[1], c[2], c[4]},
		    {c[3], c[4], c[5]}};
  double D = det3(M);
  if (c[0] == 0 && c[1] == 0 && c[2] == 0)
    return "line";
  std::string res = stringify(D);
  double descr = c[1]*c[1] - c[0]*c[2];
  if (descr < 0) {
    if (c[0] == c[2] && c[1] == 0)
      return res + "circle";
    return res + "ellipse";
  } else if (descr == 0) {
    return res + "parabola";
  } else if (descr > 0) {
    if (c[0] + c[2] == 0) {
      if (D == 0)
	return res + "two lines";
      return res + "rectangular hyperbola";
    }
    return res + "hyperbola";

  }
  return "no idea!";
}
#endif


std::vector<Point> decompose_degenerate(xAx const & C1, xAx const & C2, xAx const & xC0) {
    std::vector<Point> res;
    double A[2][2] = {{2*xC0.c[0], xC0.c[1]},
                      {xC0.c[1], 2*xC0.c[2]}};
//Point B0 = xC0.bottom();
    double const determ = det(A);
    //std::cout << determ << "\n";
    if (fabs(determ) >= 1e-20) { // hopeful, I know
        Geom::Coord const ideterm = 1.0 / determ;

        double b[2] = {-xC0.c[3], -xC0.c[4]};
        Point B0((A[1][1]*b[0]  -A[0][1]*b[1]),
                 (-A[1][0]*b[0] +  A[0][0]*b[1]));
        B0 *= ideterm;
        Point n0, n1;
        // Are these just the eigenvectors of A11?
        if(xC0.c[0] == xC0.c[2]) {
            double b = 0.5*xC0.c[1]/xC0.c[0];
            double c = xC0.c[2]/xC0.c[0];
            //assert(fabs(b*b-c) > 1e-10);
            double d =  std::sqrt(b*b-c);
            //assert(fabs(b-d) > 1e-10);
            n0 = Point(1, b+d);
            n1 = Point(1, b-d);
        } else if(fabs(xC0.c[0]) > fabs(xC0.c[2])) {
            double b = 0.5*xC0.c[1]/xC0.c[0];
            double c = xC0.c[2]/xC0.c[0];
            //assert(fabs(b*b-c) > 1e-10);
            double d =  std::sqrt(b*b-c);
            //assert(fabs(b-d) > 1e-10);
            n0 = Point(1, b+d);
            n1 = Point(1, b-d);
        } else {
            double b = 0.5*xC0.c[1]/xC0.c[2];
            double c = xC0.c[0]/xC0.c[2];
            //assert(fabs(b*b-c) > 1e-10);
            double d =  std::sqrt(b*b-c);
            //assert(fabs(b-d) > 1e-10);
            n0 = Point(b+d, 1);
            n1 = Point(b-d, 1);
        }

        Line L0 = Line::from_origin_and_versor(B0, rot90(n0));
        Line L1 = Line::from_origin_and_versor(B0, rot90(n1));

        std::vector<double> rts = C1.roots(L0);
        for(unsigned i = 0; i < rts.size(); i++) {
            Point P = L0.pointAt(rts[i]);
            res.push_back(P);
        }
        rts = C1.roots(L1);
        for(unsigned i = 0; i < rts.size(); i++) {
            Point P = L1.pointAt(rts[i]);
            res.push_back(P);
        }
    } else {
        // single or double line
        // check for completely zero case (what to do?)
        assert(xC0.c[0] || xC0.c[1] ||
               xC0.c[2] || xC0.c[3] ||
               xC0.c[4] || xC0.c[5]);
        Point trial_pt(0,0);
        Point g = xC0.gradient(trial_pt);
        if(L2sq(g) == 0) {
            trial_pt[0] += 1;
            g = xC0.gradient(trial_pt);
            if(L2sq(g) == 0) {
                trial_pt[1] += 1;
                g = xC0.gradient(trial_pt);
                if(L2sq(g) == 0) {
                    trial_pt[0] += 1;
                    g = xC0.gradient(trial_pt);
                    if(L2sq(g) == 0) {
                        trial_pt = Point(1.5,0.5);
                        g = xC0.gradient(trial_pt);
                    }
                }
            }
        }
        //std::cout << trial_pt << ", " << g << "\n";
        /**
         * At this point we have tried up to 4 points: 0,0, 1,0, 1,1, 2,1, 1.5,1.5
         *
         * No degenerate conic can pass through these points, so we can assume
         * that we've found a perpendicular to the double line.
         * Proof:
         *  any degenerate must consist of at most 2 lines.  1.5,0.5 is not on any pair of lines
         *  passing through the previous 4 trials.
         *
         * alternatively, there may be a way to determine this directly from xC0
         */
        assert(L2sq(g) != 0);

        Line Lx = Line::from_origin_and_versor(trial_pt, g); // a line along the gradient
        std::vector<double> rts = xC0.roots(Lx);
        for(unsigned i = 0; i < rts.size(); i++) {
            Point P0 = Lx.pointAt(rts[i]);
            //std::cout << P0 << "\n";
            Line L = Line::from_origin_and_versor(P0, rot90(g));
            std::vector<double> cnrts;
            // It's very likely that at least one of the conics is degenerate, this will hopefully pick the more generate of the two.
            if(fabs(C1.hessian().det()) > fabs(C2.hessian().det()))
                cnrts = C1.roots(L);
            else
                cnrts = C2.roots(L);
            for(unsigned j = 0; j < cnrts.size(); j++) {
                Point P = L.pointAt(cnrts[j]);
                res.push_back(P);
            }
        }
    }
    return res;
}

double xAx_descr(xAx const & C) {
    double mC[3][3] = {{C.c[0], (C.c[1])/2, (C.c[3])/2},
                       {(C.c[1])/2, C.c[2], (C.c[4])/2},
                       {(C.c[3])/2, (C.c[4])/2, C.c[5]}};

    return det3(mC);
}


std::vector<Point> intersect(xAx const & C1, xAx const & C2) {
    // You know, if either of the inputs are degenerate we should use them first!
    if(xAx_descr(C1) == 0) {
        return decompose_degenerate(C1, C2, C1);
    }
    if(xAx_descr(C2) == 0) {
        return decompose_degenerate(C1, C2, C2);
    }
    std::vector<Point> res;
    SBasis T(Linear(-1,1));
    SBasis S(Linear(1,1));
    SBasis C[3][3] = {{T*C1.c[0]+S*C2.c[0], (T*C1.c[1]+S*C2.c[1])/2, (T*C1.c[3]+S*C2.c[3])/2},
                      {(T*C1.c[1]+S*C2.c[1])/2, T*C1.c[2]+S*C2.c[2], (T*C1.c[4]+S*C2.c[4])/2},
                      {(T*C1.c[3]+S*C2.c[3])/2, (T*C1.c[4]+S*C2.c[4])/2, T*C1.c[5]+S*C2.c[5]}};

    SBasis D = det3(C);
    std::vector<double> rts = Geom::roots(D);
    if(rts.empty()) {
        T = Linear(1,1);
        S = Linear(-1,1);
        SBasis C[3][3] = {{T*C1.c[0]+S*C2.c[0], (T*C1.c[1]+S*C2.c[1])/2, (T*C1.c[3]+S*C2.c[3])/2},
                          {(T*C1.c[1]+S*C2.c[1])/2, T*C1.c[2]+S*C2.c[2], (T*C1.c[4]+S*C2.c[4])/2},
                          {(T*C1.c[3]+S*C2.c[3])/2, (T*C1.c[4]+S*C2.c[4])/2, T*C1.c[5]+S*C2.c[5]}};

        D = det3(C);
        rts = Geom::roots(D);
    }
    // at this point we have a T and S and perhaps some roots that represent our degenerate conic
    // Let's just pick one randomly (can we do better?)
    //for(unsigned i = 0; i < rts.size(); i++) {
    if(!rts.empty()) {
        unsigned i = 0;
        double t = T.valueAt(rts[i]);
        double s = S.valueAt(rts[i]);
        xAx xC0 = C1*t + C2*s;
        //::draw(cr, xC0, screen_rect); // degen

        return decompose_degenerate(C1, C2, xC0);


    } else {
        std::cout << "What?\n";
        ;//std::cout << D << "\n";
    }
    return res;
}


xAx xAx::fromPoint(Point p) {
  return xAx(1., 0, 1., -2*p[0], -2*p[1], dot(p,p));
}

xAx xAx::fromDistPoint(Point /*p*/, double /*d*/) {
    return xAx();//1., 0, 1., -2*(1+d)*p[0], -2*(1+d)*p[1], dot(p,p)+d*d);
}

xAx xAx::fromLine(Point n, double d) {
  return xAx(n[0]*n[0], 2*n[0]*n[1], n[1]*n[1], 2*d*n[0], 2*d*n[1], d*d);
}

xAx xAx::fromLine(Line l) {
  double dist;
  Point norm = l.normalAndDist(dist);

  return fromLine(norm, dist);
}

xAx xAx::fromPoints(std::vector<Geom::Point> const &pt) {
    Geom::NL::Vector V(pt.size(), -1.0);
    Geom::NL::Matrix M(pt.size(), 5);
    for(unsigned i = 0; i < pt.size(); i++) {
        Geom::Point P = pt[i];
        Geom::NL::VectorView vv = M.row_view(i);
        vv[0] = P[0]*P[0];
        vv[1] = P[0]*P[1];
        vv[2] = P[1]*P[1];
        vv[3] = P[0];
        vv[4] = P[1];
    }

    Geom::NL::LinearSystem ls(M, V);

    Geom::NL::Vector x = ls.SV_solve();
    return Geom::xAx(x[0], x[1], x[2], x[3], x[4], 1);

}



double xAx::valueAt(Point P) const {
  return evaluate_at(P[0], P[1]);
}

xAx xAx::scale(double sx, double sy) const {
  return xAx(c[0]*sx*sx, c[1]*sx*sy, c[2]*sy*sy,
	     c[3]*sx, c[4]*sy, c[5]);
}

Point xAx::gradient(Point p)  const{
  double x = p[0];
  double y = p[1];
  return Point(2*c[0]*x + c[1]*y + c[3],
	       c[1]*x + 2*c[2]*y + c[4]);
}

xAx xAx::operator-(xAx const &b) const {
  xAx res;
  for(int i = 0; i < 6; i++) {
    res.c[i] = c[i] - b.c[i];
  }
  return res;
}
xAx xAx::operator+(xAx const &b) const {
  xAx res;
  for(int i = 0; i < 6; i++) {
    res.c[i] = c[i] + b.c[i];
  }
  return res;
}
xAx xAx::operator+(double const &b) const {
  xAx res;
  for(int i = 0; i < 5; i++) {
    res.c[i] = c[i];
  }
  res.c[5] = c[5] + b;
  return res;
}

xAx xAx::operator*(double const &b) const {
  xAx res;
  for(int i = 0; i < 6; i++) {
    res.c[i] = c[i] * b;
  }
  return res;
}

  std::vector<Point> xAx::crossings(Rect r) const {
    std::vector<Point> res;
  for(int ei = 0; ei < 4; ei++) {
    Geom::LineSegment ls(r.corner(ei), r.corner(ei+1));
    D2<SBasis> lssb = ls.toSBasis();
    SBasis edge_curve = evaluate_at(lssb[0], lssb[1]);
    std::vector<double> rts = Geom::roots(edge_curve);
    for(unsigned eci = 0; eci < rts.size(); eci++) {
      res.push_back(lssb.valueAt(rts[eci]));
    }
  }
  return res;
}

  boost::optional<RatQuad> xAx::toCurve(Rect const & bnd) const {
  std::vector<Point> crs = crossings(bnd);
  if(crs.size() == 1) {
      Point A = crs[0];
      Point dA = rot90(gradient(A));
      if(L2sq(dA) <= 1e-10) { // perhaps a single point?
          return boost::optional<RatQuad> ();
      }
      LineSegment ls = intersection(Line::from_origin_and_versor(A, dA), bnd);
      return RatQuad::fromPointsTangents(A, dA, ls.pointAt(0.5), ls[1], dA);
  }
  else if(crs.size() >= 2 and crs.size() < 4) {
    Point A = crs[0];
    Point C = crs[1];
    if(crs.size() == 3) {
        if(distance(A, crs[2]) > distance(A, C))
            C = crs[2];
        else if(distance(C, crs[2]) > distance(A, C))
            A = crs[2];
    }
    Line bisector = make_bisector_line(LineSegment(A, C));
    std::vector<double> bisect_rts = this->roots(bisector);
    if(bisect_rts.size() > 0) {
      int besti = -1;
      for(unsigned i =0; i < bisect_rts.size(); i++) {
	Point p = bisector.pointAt(bisect_rts[i]);
	if(bnd.contains(p)) {
	  besti = i;
	}
      }
      if(besti >= 0) {
	Point B = bisector.pointAt(bisect_rts[besti]);

        Point dA = gradient(A);
        Point dC = gradient(C);
        if(L2sq(dA) <= 1e-10 or L2sq(dC) <= 1e-10) {
            return RatQuad::fromPointsTangents(A, C-A, B, C, A-C);
        }

	RatQuad rq = RatQuad::fromPointsTangents(A, rot90(dA),
						 B, C, rot90(dC));
	return rq;
	//std::vector<SBasis> hrq = rq.homogenous();
	/*SBasis vertex_poly = evaluate_at(hrq[0], hrq[1], hrq[2]);
	  std::vector<double> rts = roots(vertex_poly);
	  for(unsigned i = 0; i < rts.size(); i++) {
	  //draw_circ(cr, Point(rq.pointAt(rts[i])));
	  }*/
      }
    }
  }
  return boost::optional<RatQuad>();
}

  std::vector<double> xAx::roots(Point d, Point o) const {
  // Find the roots on line l
  // form the quadratic Q(t) = 0 by composing l with xAx
  double q2 = c[0]*d[0]*d[0] + c[1]*d[0]*d[1] + c[2]*d[1]*d[1];
  double q1 = (2*c[0]*d[0]*o[0] +
	       c[1]*(d[0]*o[1]+d[1]*o[0]) +
	       2*c[2]*d[1]*o[1] +
	       c[3]*d[0] + c[4]*d[1]);
  double q0 = c[0]*o[0]*o[0] + c[1]*o[0]*o[1] + c[2]*o[1]*o[1] + c[3]*o[0] + c[4]*o[1] + c[5];
  std::vector<double> r;
  if(q2 == 0) {
    if(q1 == 0) {
      return r;
    }
    r.push_back(-q0/q1);
  } else {
    double desc = q1*q1 - 4*q2*q0;
    /*std::cout << q2 << ", "
      << q1 << ", "
      << q0 << "; "
      << desc << "\n";*/
    if (desc < 0)
      return r;
    else if (desc == 0)
      r.push_back(-q1/(2*q2));
    else {
      desc = std::sqrt(desc);
      double t;
      if (q1 == 0)
      {
          t = -0.5 * desc;
      }
      else
      {
          t = -0.5 * (q1 + sgn(q1) * desc);
      }
      r.push_back(t/q2);
      r.push_back(q0/t);
    }
  }
  return r;
}

std::vector<double> xAx::roots(Line const &l) const {
  return roots(l.versor(), l.origin());
}

Interval xAx::quad_ex(double a, double b, double c, Interval ivl) {
  double cx = -b*0.5/a;
  Interval bnds((a*ivl[0]+b)*ivl[0]+c, (a*ivl[1]+b)*ivl[1]+c);
  if(ivl.contains(cx))
    bnds.expandTo((a*cx+b)*cx+c);
  return bnds;
}

Geom::Affine xAx::hessian() const {
  Geom::Affine m(2*c[0], c[1],
		c[1], 2*c[2],
		0, 0);
  return m;
}


boost::optional<Point> solve(double A[2][2], double b[2]) {
    double const determ = det(A);
    if (determ !=  0.0) { // hopeful, I know
        Geom::Coord const ideterm = 1.0 / determ;

        return Point ((A[1][1]*b[0]  -A[0][1]*b[1]),
                      (-A[1][0]*b[0] +  A[0][0]*b[1]))* ideterm;
    } else {
        return boost::optional<Point>();
    }
}

boost::optional<Point> xAx::bottom() const {
    double A[2][2] = {{2*c[0], c[1]},
                      {c[1], 2*c[2]}};
    double b[2] = {-c[3], -c[4]};
    return solve(A, b);
    //return Point(-c[3], -c[4])*hessian().inverse();
}

Interval xAx::extrema(Rect r) const {
  if (c[0] == 0 and c[1] == 0 and c[2] == 0) {
    Interval ext(valueAt(r.corner(0)));
    for(int i = 1; i < 4; i++)
      ext |= Interval(valueAt(r.corner(i)));
    return ext;
  }
  double k = r[0][0];
  Interval ext = quad_ex(c[2], c[1]*k+c[4],  (c[0]*k + c[3])*k + c[5], r[1]);
  k = r[0][1];
  ext |= quad_ex(c[2], c[1]*k+c[4],  (c[0]*k + c[3])*k + c[5], r[1]);
  k = r[1][0];
  ext |= quad_ex(c[0], c[1]*k+c[3],  (c[2]*k + c[4])*k + c[5], r[0]);
  k = r[1][1];
  ext |= quad_ex(c[0], c[1]*k+c[3],  (c[2]*k + c[4])*k + c[5], r[0]);
  boost::optional<Point> B0 = bottom();
  if (B0 and r.contains(*B0))
    ext.expandTo(0);
  return ext;
}









/*
 *  helper functions
 */

bool at_infinity (Point const& p)
{
    if (p[X] == infinity() || p[X] == -infinity()
        || p[Y] == infinity() || p[Y] == -infinity())
    {
        return true;
    }
    return false;
}

inline
double signed_triangle_area (Point const& p1, Point const& p2, Point const& p3)
{
    return (cross(p3, p2) - cross(p3, p1) + cross(p2, p1));
}



/*
 *  Define a conic section by computing the one that fits better with
 *  N points.
 *
 *  points: points to fit
 *
 *  precondition: there must be at least 5 non-overlapping points
 */
void xAx::set(std::vector<Point> const& points)
{
    size_t sz = points.size();
    if (sz < 5)
    {
        THROW_RANGEERROR("fitting error: too few points passed");
    }
    NL::LFMConicSection model;
    NL::least_squeares_fitter<NL::LFMConicSection> fitter(model, sz);

    for (size_t i = 0; i < sz; ++i)
    {
        fitter.append(points[i]);
    }
    fitter.update();

    NL::Vector z(sz, 0.0);
    model.instance(*this, fitter.result(z));
}

/*
 *  Define a section conic by providing the coordinates of one of its vertex,
 *  the major axis inclination angle and the coordinates of its foci
 *  with respect to the unidimensional system defined by the major axis with
 *  origin set at the provided vertex.
 *
 *  _vertex :   section conic vertex V
 *  _angle :    section conic major axis angle
 *  _dist1:     +/-distance btw V and nearest focus
 *  _dist2:     +/-distance btw V and farest focus
 *
 *  prerequisite: _dist1 <= _dist2
 */
void xAx::set (const Point& _vertex, double _angle, double _dist1, double _dist2)
{
    if (_dist2 == infinity() || _dist2 == -infinity())  // parabola
    {
        if (_dist1 == infinity()) // degenerate to a line
        {
            Line l(_vertex, _angle);
            std::vector<double> lcoeff = l.coefficients();
            coeff(3) = lcoeff[0];
            coeff(4) = lcoeff[1];
            coeff(5) = lcoeff[2];
            return;
        }

        // y^2 - 4px == 0
        double cD = -4 * _dist1;

        double cosa = std::cos (_angle);
        double sina = std::sin (_angle);
        double cca = cosa * cosa;
        double ssa = sina * sina;
        double csa = cosa * sina;

        coeff(0) = ssa;
        coeff(1) = -2 * csa;
        coeff(2) = cca;
        coeff(3) = cD * cosa;
        coeff(4) = cD * sina;

        double VxVx = _vertex[X] * _vertex[X];
        double VxVy = _vertex[X] * _vertex[Y];
        double VyVy = _vertex[Y] * _vertex[Y];

        coeff(5) = coeff(0) * VxVx + coeff(1) * VxVy + coeff(2) * VyVy
               - coeff(3) * _vertex[X] - coeff(4) * _vertex[Y];
        coeff(3) -= (2 * coeff(0) * _vertex[X] + coeff(1) * _vertex[Y]);
        coeff(4) -= (2 * coeff(2) * _vertex[Y] + coeff(1) * _vertex[X]);

        return;
    }

    if (std::fabs(_dist1) > std::fabs(_dist2))
    {
        std::swap (_dist1, _dist2);
    }
    if (_dist1 < 0)
    {
        _angle -= M_PI;
        _dist1 = -_dist1;
        _dist2 = -_dist2;
    }

    // ellipse and hyperbola
    double lin_ecc = (_dist2 - _dist1) / 2;
    double rx = (_dist2 + _dist1) / 2;

    double cA = rx * rx - lin_ecc * lin_ecc;
    double cC = rx * rx;
    double cF = - cA * cC;
//    std::cout << "cA: " << cA << std::endl;
//    std::cout << "cC: " << cC << std::endl;
//    std::cout << "cF: " << cF << std::endl;

    double cosa = std::cos (_angle);
    double sina = std::sin (_angle);
    double cca = cosa * cosa;
    double ssa = sina * sina;
    double csa = cosa * sina;

    coeff(0) = cca * cA + ssa * cC;
    coeff(2) = ssa * cA + cca * cC;
    coeff(1) = 2 * csa * (cA - cC);

    Point C (rx * cosa + _vertex[X], rx * sina + _vertex[Y]);
    double CxCx = C[X] * C[X];
    double CxCy = C[X] * C[Y];
    double CyCy = C[Y] * C[Y];

    coeff(3) = -2 * coeff(0) * C[X] - coeff(1) * C[Y];
    coeff(4) = -2 * coeff(2) * C[Y] - coeff(1) * C[X];
    coeff(5) = cF + coeff(0) * CxCx + coeff(1) * CxCy + coeff(2) * CyCy;
}

/*
 *  Define a conic section by providing one of its vertex and its foci.
 *
 *  _vertex: section conic vertex
 *  _focus1: section conic focus
 *  _focus2: section conic focus
 */
void xAx::set (const Point& _vertex, const Point& _focus1, const Point& _focus2)
{
    if (at_infinity(_vertex))
    {
        THROW_RANGEERROR("case not handled: vertex at infinity");
    }
    if (at_infinity(_focus2))
    {
        if (at_infinity(_focus1))
        {
            THROW_RANGEERROR("case not handled: both focus at infinity");
        }
        Point VF = _focus1 - _vertex;
        double dist1 = L2(VF);
        double angle = atan2(VF);
        set(_vertex, angle, dist1, infinity());
        return;
    }
    else if (at_infinity(_focus1))
    {
        Point VF = _focus2 - _vertex;
        double dist1 = L2(VF);
        double angle = atan2(VF);
        set(_vertex, angle, dist1, infinity());
        return;
    }
    assert (are_collinear (_vertex, _focus1, _focus2));
    if (!are_near(_vertex, _focus1))
    {
        Point VF = _focus1 - _vertex;
        Line axis(_vertex, _focus1);
        double angle = atan2(VF);
        double dist1 = L2(VF);
        double dist2 = distance (_vertex, _focus2);
        double t = axis.timeAt(_focus2);
        if (t < 0)  dist2 = -dist2;
//        std::cout << "t = " << t << std::endl;
//        std::cout << "dist2 = " << dist2 << std::endl;
        set (_vertex, angle, dist1, dist2);
    }
    else if (!are_near(_vertex, _focus2))
    {
        Point VF = _focus2 - _vertex;
        double angle = atan2(VF);
        double dist1 = 0;
        double dist2 = L2(VF);
        set (_vertex, angle, dist1, dist2);
    }
    else
    {
        coeff(0) = coeff(2) = 1;
        coeff(1) = coeff(3) = coeff(4) = coeff(5) = 0;
    }
}

/*
 *  Define a conic section by passing a focus, the related directrix,
 *  and the eccentricity (e)
 *  (e < 1 -> ellipse; e = 1 -> parabola; e > 1 -> hyperbola)
 *
 *  _focus:         a focus of the conic section
 *  _directrix:     the directrix related to the given focus
 *  _eccentricity:  the eccentricity parameter of the conic section
 */
void xAx::set (const Point & _focus, const Line & _directrix, double _eccentricity)
{
    Point O = _directrix.pointAt (_directrix.timeAtProjection (_focus));
    //std::cout << "O = " << O << std::endl;
    Point OF = _focus - O;
    double p = L2(OF);

    coeff(0) = 1 - _eccentricity * _eccentricity;
    coeff(1) = 0;
    coeff(2) = 1;
    coeff(3) = -2 * p;
    coeff(4) = 0;
    coeff(5) = p * p;

    double angle = atan2 (OF);

    (*this) = rotate (angle);
    //std::cout << "O = " << O << std::endl;
    (*this) = translate (O);
}

/*
 *  Made up a degenerate conic section as a pair of lines
 *
 *  l1, l2: lines that made up the conic section
 */
void xAx::set (const Line& l1, const Line& l2)
{
    std::vector<double> cl1 = l1.coefficients();
    std::vector<double> cl2 = l2.coefficients();

    coeff(0) = cl1[0] * cl2[0];
    coeff(2) = cl1[1] * cl2[1];
    coeff(5) = cl1[2] * cl2[2];
    coeff(1) = cl1[0] * cl2[1] + cl1[1] * cl2[0];
    coeff(3) = cl1[0] * cl2[2] + cl1[2] * cl2[0];
    coeff(4) = cl1[1] * cl2[2] + cl1[2] * cl2[1];
}



/*
 *   Return the section conic kind
 */
xAx::kind_t xAx::kind () const
{

    xAx conic(*this);
    NL::SymmetricMatrix<3> C = conic.get_matrix();
    NL::ConstSymmetricMatrixView<2> A = C.main_minor_const_view();

    double t1 = trace(A);
    double t2 = det(A);
    //double T3 = det(C);
    int st1 = trace_sgn(A);
    int st2 = det_sgn(A);
    int sT3 = det_sgn(C);

    //std::cout << "T3 = " << T3 << std::endl;
    //std::cout << "sT3 = " << sT3 << std::endl;
    //std::cout << "t2 = " << t2 << std::endl;
    //std::cout << "t1 = " << t1 << std::endl;
    //std::cout << "st2 = " << st2 << std::endl;

    if (sT3 != 0)
    {
        if (st2 == 0)
        {
            return PARABOLA;
        }
        else if (st2 == 1)
        {

            if (sT3 * st1 < 0)
            {
                NL::SymmetricMatrix<2> discr;
                discr(0,0) = 4; discr(1,1) = t2; discr(1,0) = t1;
                int discr_sgn = - det_sgn (discr);
                //std::cout << "t1 * t1 - 4 * t2 = "
                //          << (t1 * t1 - 4 * t2) << std::endl;
                //std::cout << "discr_sgn = " << discr_sgn << std::endl;
                if (discr_sgn == 0)
                {
                    return CIRCLE;
                }
                else
                {
                    return REAL_ELLIPSE;
                }
            }
            else // sT3 * st1 > 0
            {
                return IMAGINARY_ELLIPSE;
            }
        }
        else // t2 < 0
        {
            if (st1 == 0)
            {
                return RECTANGULAR_HYPERBOLA;
            }
            else
            {
                return HYPERBOLA;
            }
        }
    }
    else // T3 == 0
    {
        if (st2 == 0)
        {
            //double T2 = NL::trace<2>(C);
            int sT2 = NL::trace_sgn<2>(C);
            //std::cout << "T2 = " << T2 << std::endl;
            //std::cout << "sT2 = " << sT2 << std::endl;

            if (sT2 == 0)
            {
                return DOUBLE_LINE;
            }
            if (sT2 == -1)
            {
                return TWO_REAL_PARALLEL_LINES;
            }
            else // T2 > 0
            {
                return TWO_IMAGINARY_PARALLEL_LINES;
            }
        }
        else if (st2 == -1)
        {
            return TWO_REAL_CROSSING_LINES;
        }
        else // t2 > 0
        {
            return TWO_IMAGINARY_CROSSING_LINES;
        }
    }
    return UNKNOWN;
}

/*
 *  Return a string representing the conic section kind
 */
std::string xAx::categorise() const
{
    kind_t KIND = kind();

    switch (KIND)
    {
        case PARABOLA :
            return "parabola";
        case CIRCLE :
            return "circle";
        case REAL_ELLIPSE :
            return "real ellispe";
        case IMAGINARY_ELLIPSE :
            return "imaginary ellispe";
        case RECTANGULAR_HYPERBOLA :
            return "rectangular hyperbola";
        case HYPERBOLA :
            return "hyperbola";
        case DOUBLE_LINE :
            return "double line";
        case TWO_REAL_PARALLEL_LINES :
            return "two real parallel lines";
        case TWO_IMAGINARY_PARALLEL_LINES :
            return "two imaginary parallel lines";
        case TWO_REAL_CROSSING_LINES :
            return "two real crossing lines";
        case TWO_IMAGINARY_CROSSING_LINES :
            return "two imaginary crossing lines";
        default :
            return "unknown";
    }
}

/*
 *  Compute the solutions of the conic section algebraic equation with respect to
 *  one coordinate after substituting to the other coordinate the passed value
 *
 *  sol: the computed solutions
 *  v:   the provided value
 *  d:   the index of the coordinate the passed value have to be substituted to
 */
void xAx::roots (std::vector<double>& sol, Coord v, Dim2 d) const
{
    sol.clear();
    if (d < 0 || d > Y)
    {
        THROW_RANGEERROR("dimension parameter out of range");
    }

    // p*t^2 + q*t + r = 0;
    double p, q, r;

    if (d == X)
    {
        p = coeff(2);
        q = coeff(4) + coeff(1) * v;
        r = coeff(5) + (coeff(0) * v + coeff(3)) * v;
    }
    else
    {
        p = coeff(0);
        q = coeff(3) + coeff(1) * v;
        r = coeff(5) + (coeff(2) * v + coeff(4)) * v;
    }

    if (p == 0)
    {
        if (q == 0)  return;
        double t = -r/q;
        sol.push_back(t);
        return;
    }

    if (q == 0)
    {
        if ((p > 0 && r > 0) || (p < 0 && r < 0))  return;
        double t = -r / p;
        t = std::sqrt (t);
        sol.push_back(-t);
        sol.push_back(t);
        return;
    }

    if (r == 0)
    {
        double t = -q/p;
        sol.push_back(0);
        sol.push_back(t);
        return;
    }


    //std::cout << "p = " << p <<  ", q = " << q <<  ", r = " << r << std::endl;
    double delta = q * q - 4 * p * r;
    if (delta < 0)  return;
    if (delta == 0)
    {
        double t = -q / (2 * p);
        sol.push_back(t);
        return;
    }
    // else
    double srd = std::sqrt(delta);
    double t = - (q + sgn(q) * srd) / 2;
    sol.push_back (t/p);
    sol.push_back (r/t);

}

/*
 *  Return the inclination angle of the major axis of the conic section
 */
double xAx::axis_angle() const
{
    if (coeff(0) == 0 && coeff(1) == 0 && coeff(2) == 0)
    {
        Line l (coeff(3), coeff(4), coeff(5));
        return l.angle();
    }
    if (coeff(1) == 0 && (coeff(0) == coeff(2)))  return 0;

    double angle;

    int sgn_discr = det_sgn (get_matrix().main_minor_const_view());
    if (sgn_discr == 0)
    {
        //std::cout << "rotation_angle: sgn_discr = "
        //          << sgn_discr << std::endl;
        angle = std::atan2 (-coeff(1), 2 * coeff(2));
        if (angle < 0)  angle += 2*M_PI;
        if (angle >= M_PI) angle -= M_PI;

    }
    else
    {
        angle = std::atan2 (coeff(1), coeff(0) - coeff(2));
        if (angle < 0)  angle += 2*M_PI;
        angle -= M_PI;
        if (angle < 0)  angle += 2*M_PI;
        angle /= 2;
        if (angle >= M_PI) angle -= M_PI;
    }
    //std::cout <<  "rotation_angle : angle = "  << angle << std::endl;
    return angle;
}

/*
 *  Translate the conic section by the given vector offset
 *
 *  _offset: represent the vector offset
 */
xAx xAx::translate (const Point & _offset) const
{
    double B = coeff(1) / 2;
    double D = coeff(3) / 2;
    double E = coeff(4) / 2;

    Point T = - _offset;

    xAx cs;
    cs.coeff(0) = coeff(0);
    cs.coeff(1) = coeff(1);
    cs.coeff(2) = coeff(2);

    Point DE;
    DE[0] = coeff(0) * T[0] + B * T[1];
    DE[1] = B * T[0] + coeff(2) * T[1];

    cs.coeff(3) = (DE[0] + D) * 2;
    cs.coeff(4) = (DE[1] + E) * 2;

    cs.coeff(5) = dot (T,  DE) + 2 * (T[0] * D + T[1] * E) + coeff(5);

    return cs;
}


/*
 *  Rotate the conic section by the given angle wrt the point (0,0)
 *
 *  angle: represent the rotation angle
 */
xAx xAx::rotate (double angle) const
{
    double c = std::cos(-angle);
    double s = std::sin(-angle);
    double cc = c * c;
    double ss = s * s;
    double cs = c * s;

    xAx result;
    result.coeff(5) = coeff(5);

    // quadratic terms
    double Bcs = coeff(1) * cs;

    result.coeff(0) = coeff(0) * cc + Bcs + coeff(2) * ss;
    result.coeff(2) = coeff(0) * ss - Bcs + coeff(2) * cc;
    result.coeff(1) = coeff(1) * (cc - ss) + 2 * (coeff(2) - coeff(0)) * cs;

    // linear terms
    result.coeff(3) = coeff(3) * c + coeff(4) * s;
    result.coeff(4) = coeff(4) * c - coeff(3) * s;

    return result;
}


/*
 * Decompose a degenerate conic in two lines the conic section is made by.
 * Return true if the decomposition is successfull, else if it fails.
 *
 * l1, l2: out parameters where the decomposed conic section is returned
 */
bool xAx::decompose (Line& l1, Line& l2) const
{
    NL::SymmetricMatrix<3> C = get_matrix();
    if (!is_quadratic() || !isDegenerate())
    {
        return false;
    }
    NL::Matrix M(C);
    NL::SymmetricMatrix<3> D = -adj(C);

    if (!D.is_zero())  // D == 0 <=> rank(C) < 2
    {

        //if (D.get<0,0>() < 0 || D.get<1,1>() < 0 || D.get<2,2>() < 0)
        //{
            //std::cout << "C: \n" << C << std::endl;
            //std::cout << "D: \n" << D << std::endl;

            /*
             *  This case should be impossible because any diagonal element
             *  of D is a square, but due to non exact aritmethic computation
             *  it can actually happen; however the algorithm seems to work
             *  correctly even if some diagonal term is negative, the only
             *  difference is that we should compute the absolute value of
             *  diagonal elements. So until we elaborate a better degenerate
             *  test it's better not rising exception when we have a negative
             *  diagonal element.
             */
        //}

        NL::Vector d(3);
        d[0] = std::fabs (D.get<0,0>());
        d[1] = std::fabs (D.get<1,1>());
        d[2] = std::fabs (D.get<2,2>());

        size_t idx = d.max_index();
        if (d[idx] == 0)
        {
            THROW_LOGICALERROR ("xAx::decompose: "
                                "rank 2 but adjoint with null diagonal");
        }
        d[0] = D(idx,0); d[1] = D(idx,1); d[2] = D(idx,2);
        d.scale (1 / std::sqrt (std::fabs (D(idx,idx))));
        M(1,2) += d[0]; M(2,1) -= d[0];
        M(0,2) -= d[1]; M(2,0) += d[1];
        M(0,1) += d[2]; M(1,0) -= d[2];

        //std::cout << "C: \n" << C << std::endl;
        //std::cout << "D: \n" << D << std::endl;
        //std::cout << "d = " << d << std::endl;
        //std::cout << "M = " << M << std::endl;
    }

    std::pair<size_t, size_t> max_ij = M.max_index();
    std::pair<size_t, size_t> min_ij = M.min_index();
    double abs_max = std::fabs (M(max_ij.first, max_ij.second));
    double abs_min = std::fabs (M(min_ij.first, min_ij.second));
    size_t i_max, j_max;
    if (abs_max > abs_min)
    {
        i_max = max_ij.first;
        j_max = max_ij.second;
    }
    else
    {
        i_max = min_ij.first;
        j_max = min_ij.second;
    }
    l1.setCoefficients (M(i_max,0), M(i_max,1), M(i_max,2));
    l2.setCoefficients (M(0, j_max), M(1,j_max), M(2,j_max));

    return true;
}


/*
 *  Return the rectangle that bound the conic section arc characterized by
 *  the passed points.
 *
 *  P1:  the initial point of the arc
 *  Q:   the inner point of the arc
 *  P2:  the final point of the arc
 *
 *  prerequisite: the passed points must lie on the conic
 */
Rect xAx::arc_bound (const Point & P1, const Point & Q, const Point & P2) const
{
    //std::cout << "BOUND: P1 = " << P1 << std::endl;
    //std::cout << "BOUND: Q = " << Q << std::endl;
    //std::cout << "BOUND: P2 = " << P2 << std::endl;

    Rect B(P1, P2);
    double Qside = signed_triangle_area (P1, Q, P2);
    //std::cout << "BOUND: Qside = " << Qside << std::endl;

    Line gl[2];
    bool empty[2] = {false, false};

    try // if the passed coefficients lead to an equation 0x + 0y + c == 0,
    {   // with c != 0 the setCoefficients rise an exception
        gl[0].setCoefficients (coeff(1), 2 * coeff(2), coeff(4));
    }
    catch(Geom::LogicalError e)
    {
        empty[0] = true;
    }

    try
    {
        gl[1].setCoefficients (2 * coeff(0), coeff(1), coeff(3));
    }
    catch(Geom::LogicalError e)
    {
        empty[1] = true;
    }

    std::vector<double> rts;
    std::vector<Point> M;
    for (size_t dim = 0; dim < 2; ++dim)
    {
        if (empty[dim])  continue;
        rts = roots (gl[dim]);
        M.clear();
        for (size_t i = 0; i < rts.size(); ++i)
            M.push_back (gl[dim].pointAt (rts[i]));
        if (M.size() == 1)
        {
            double Mside = signed_triangle_area (P1, M[0], P2);
            if (sgn(Mside) == sgn(Qside))
            {
                //std::cout << "BOUND: M.size() == 1" << std::endl;
                if (M[0][dim] > B[dim][1])
                    B[dim][1] = M[0][dim];
                else if (M[0][dim] < B[dim][0])
                    B[dim][0] = M[0][dim];
            }
        }
        else if (M.size() == 2)
        {
            //std::cout << "BOUND: M.size() == 2" << std::endl;
            if (M[0][dim] > M[1][dim])
                std::swap (M[0], M[1]);

            if (M[0][dim] > B[dim][1])
            {
                double Mside = signed_triangle_area (P1, M[0], P2);
                if (sgn(Mside) == sgn(Qside))
                    B[dim][1] = M[0][dim];
            }
            else if (M[1][dim] < B[dim][0])
            {
                double Mside = signed_triangle_area (P1, M[1], P2);
                if (sgn(Mside) == sgn(Qside))
                    B[dim][0] = M[1][dim];
            }
            else
            {
                double Mside = signed_triangle_area (P1, M[0], P2);
                if (sgn(Mside) == sgn(Qside))
                    B[dim][0] = M[0][dim];
                Mside = signed_triangle_area (P1, M[1], P2);
                if (sgn(Mside) == sgn(Qside))
                    B[dim][1] = M[1][dim];
            }
        }
    }

    return B;
}

/*
 *  Return all points on the conic section nearest to the passed point "P".
 *
 *  P: the point to compute the nearest one
 */
std::vector<Point> xAx::allNearestPoints (const Point P) const
{
    // TODO: manage the circle - centre case
    std::vector<Point> points;

    // named C the conic we look for points (x,y) on C such that
    // dot (grad (C(x,y)), rot90 (P -(x,y))) == 0; the set of points satisfying
    // this equation is still a conic G, so the wanted points can be found by
    // intersecting C with G
    xAx G (-coeff(1),
           2 * (coeff(0) - coeff(2)),
           coeff(1),
           -coeff(4) + coeff(1) * P[X] - 2 * coeff(0) * P[Y],
           coeff(3) - coeff(1) * P[Y] + 2 * coeff(2) * P[X],
           -coeff(3) * P[Y] + coeff(4) * P[X]);

    std::vector<Point> crs = intersect (*this, G);

    //std::cout << "NEAREST POINT: crs.size = " << crs.size() << std::endl;
    if (crs.size() == 0)  return points;

    size_t idx = 0;
    double mindist = distanceSq (crs[0], P);
    std::vector<double> dist;
    dist.push_back (mindist);

    for (size_t i = 1; i < crs.size(); ++i)
    {
        dist.push_back (distanceSq (crs[i], P));
        if (mindist > dist.back())
        {
            idx = i;
            mindist = dist.back();
        }
    }

    points.push_back (crs[idx]);
    for (size_t i = 0; i < crs.size(); ++i)
    {
        if (i == idx) continue;
        if (dist[i] == mindist)
            points.push_back (crs[i]);
    }

    return points;
}



bool clip (std::vector<RatQuad> & rq, const xAx & cs, const Rect & R)
{
    clipper aclipper (cs, R);
    return aclipper.clip (rq);
}


} // end namespace Geom




/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :