File: camera_impl.h

package info (click to toggle)
cgal 6.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (2253 lines) | stat: -rw-r--r-- 81,510 bytes parent folder | download | duplicates (2)
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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
/****************************************************************************

 Copyright (c) 2018  GeometryFactory Sarl (France).
 Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.

 This file is part of a fork of the QGLViewer library version 2.7.0.

*****************************************************************************/
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/GraphicsView/include/CGAL/Qt/camera_impl.h $
// $Id: include/CGAL/Qt/camera_impl.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-only

#ifdef CGAL_HEADER_ONLY
#define CGAL_INLINE_FUNCTION inline

#include <CGAL/license/GraphicsView.h>

#else
#define CGAL_INLINE_FUNCTION
#endif

#include <CGAL/Qt/camera.h>
#include <CGAL/Qt/manipulatedCameraFrame.h>
#include <CGAL/Qt/keyFrameInterpolator.h>

namespace CGAL{
namespace qglviewer{

/*! Default constructor.

 sceneCenter() is set to (0,0,0) and sceneRadius() is set to 1.0. type() is
 Camera::PERSPECTIVE, with a \c M_PI/4 fieldOfView().

 See IODistance(), physicalDistanceToScreen(), physicalScreenWidth() and
 focusDistance(). */
CGAL_INLINE_FUNCTION
Camera::Camera(QObject *parent)
    : frame_(nullptr), fieldOfView_(CGAL_PI / 4.0), modelViewMatrixIsUpToDate_(false),
      projectionMatrixIsUpToDate_(false) {
  m_zMin = 0;
  setParent(parent);
  // #CONNECTION# Camera copy constructor
  interpolationKfi_ = new KeyFrameInterpolator;
  // Requires the interpolationKfi_
  setFrame(new ManipulatedCameraFrame());


  // Requires fieldOfView() to define focusDistance()
  setSceneRadius(1.0);

  // Initial value (only scaled after this)
  orthoCoef_ = tan(fieldOfView() / 2.0);

  // Also defines the pivotPoint(), which changes orthoCoef_. Requires a
  // frame().
  setSceneCenter(Vec(0.0, 0.0, 0.0));

  // Requires fieldOfView() when called with ORTHOGRAPHIC. Attention to
  // projectionMatrix_ below.
  setType(PERSPECTIVE);

  setZNearCoefficient(0.005);
  setZClippingCoefficient(sqrt(3.0));

  // Dummy values
  setScreenWidthAndHeight(600, 400);


  // focusDistance is set from setFieldOfView()

  // #CONNECTION# Camera copy constructor
  for (unsigned short j = 0; j < 16; ++j) {
    modelViewMatrix_[j] = ((j % 5 == 0) ? 1.0 : 0.0);
    // #CONNECTION# computeProjectionMatrix() is lazy and assumes 0.0 almost
    // everywhere.
    projectionMatrix_[j] = 0.0;
  }
  computeProjectionMatrix();
}

/*! Virtual destructor.

 The frame() is deleted, but the different keyFrameInterpolator() are \e not
 deleted (in case they are shared). */

CGAL_INLINE_FUNCTION
Camera::~Camera() {
  delete frame_;
  delete interpolationKfi_;
}

/*! Copy constructor. Performs a deep copy using operator=(). */
CGAL_INLINE_FUNCTION
Camera::Camera(const Camera &camera) : QObject(), frame_(nullptr) {
  // #CONNECTION# Camera constructor
  interpolationKfi_ = new KeyFrameInterpolator;
  // Requires the interpolationKfi_
  setFrame(new ManipulatedCameraFrame(*camera.frame()));

  for (unsigned short j = 0; j < 16; ++j) {
    modelViewMatrix_[j] = ((j % 5 == 0) ? 1.0 : 0.0);
    // #CONNECTION# computeProjectionMatrix() is lazy and assumes 0.0 almost
    // everywhere.
    projectionMatrix_[j] = 0.0;
  }

  (*this) = camera;
}

/*! Equal operator.

 All the parameters of \p camera are copied. The frame() pointer is not
 modified, but its Frame::position() and Frame::orientation() are set to those
 of \p camera.

 \attention The Camera screenWidth() and screenHeight() are set to those of \p
 camera. If your Camera is associated with a CGAL::QGLViewer, you should update these
 value after the call to this method: \code
 *(camera()) = otherCamera;
 camera()->setScreenWidthAndHeight(width(), height());
 \endcode
 The same applies to sceneCenter() and sceneRadius(), if needed. */
CGAL_INLINE_FUNCTION
Camera &Camera::operator=(const Camera &camera) {
  setScreenWidthAndHeight(camera.screenWidth(), camera.screenHeight());
  setFieldOfView(camera.fieldOfView());
  setSceneRadius(camera.sceneRadius());
  setSceneCenter(camera.sceneCenter());
  setZNearCoefficient(camera.zNearCoefficient());
  setZClippingCoefficient(camera.zClippingCoefficient());
  setType(camera.type());

  orthoCoef_ = camera.orthoCoef_;
  projectionMatrixIsUpToDate_ = false;

  // frame_ and interpolationKfi_ pointers are not shared.
  frame_->setReferenceFrame(nullptr);
  frame_->setPosition(camera.position());
  frame_->setOrientation(camera.orientation());

  interpolationKfi_->resetInterpolation();

  kfi_ = camera.kfi_;

  computeProjectionMatrix();
  computeModelViewMatrix();

  return *this;
}

/*! Sets Camera screenWidth() and screenHeight() (expressed in pixels).

You should not call this method when the Camera is associated with a CGAL::QGLViewer,
since the latter automatically updates these values when it is resized (hence
overwriting your values).

Non-positive dimension are silently replaced by a 1 pixel value to ensure
frustum coherence.

If your Camera is used without a CGAL::QGLViewer (offscreen rendering, shadow maps),
use setAspectRatio() instead to define the projection matrix. */
CGAL_INLINE_FUNCTION
void Camera::setScreenWidthAndHeight(int width, int height, qreal devicePixelRatio) {
  // Prevent negative and zero dimensions that would cause divisions by zero.
  screenWidth_ = width > 0 ? width : 1;
  screenHeight_ = height > 0 ? height : 1;
  devicePixelRatio_ = devicePixelRatio;
  projectionMatrixIsUpToDate_ = false;
}

/*! Returns the near clipping plane distance used by the Camera projection
 matrix.

 The clipping planes' positions depend on the sceneRadius() and sceneCenter()
 rather than being fixed small-enough and large-enough values. A good scene
 dimension approximation will hence result in an optimal precision of the
 z-buffer.

 The near clipping plane is positioned at a distance equal to
 zClippingCoefficient() * sceneRadius() in front of the sceneCenter(): \code
 zNear = distanceToSceneCenter() - zClippingCoefficient()*sceneRadius();
 \endcode

 In order to prevent negative or too small zNear() values (which would degrade
 the z precision), zNearCoefficient() is used when the Camera is inside the
 sceneRadius() sphere: \code const qreal zMin = zNearCoefficient() *
 zClippingCoefficient() * sceneRadius(); if (zNear < zMin) zNear = zMin;
 // With an ORTHOGRAPHIC type, the value is simply clamped to 0.0
 \endcode

 See also the zFar(), zClippingCoefficient() and zNearCoefficient()
 documentations.

 If you need a completely different zNear computation, overload the zNear() and
 zFar() methods in a new class that publicly inherits from Camera and use
 CGAL::QGLViewer::setCamera(): \code class myCamera :: public CGAL::qglviewer::Camera
 {
   virtual qreal Camera::zNear() const { return 0.001; };
   virtual qreal Camera::zFar() const { return 100.0; };
 }
 \endcode

 See the <a href="../examples/standardCamera.html">standardCamera example</a>
 for an application.

 \attention The value is always positive although the clipping plane is
 positioned at a negative z value in the Camera coordinate system. This follows
 the \c gluPerspective standard. */
CGAL_INLINE_FUNCTION
qreal Camera::zNear() const {
  const qreal zNearScene = zClippingCoefficient() * sceneRadius();
  qreal z = distanceToSceneCenter() - zNearScene;

  // Prevents negative or null zNear values.
  const qreal zMin = zNearCoefficient() * zNearScene;
  if (z < zMin)
    switch (type()) {
    case Camera::PERSPECTIVE:
      z = zMin;
      break;
    case Camera::ORTHOGRAPHIC:
      z = m_zMin;
      break;
    }
  return z;
}

/*! Returns the far clipping plane distance used by the Camera projection
matrix.

The far clipping plane is positioned at a distance equal to
zClippingCoefficient() * sceneRadius() behind the sceneCenter(): \code zFar =
distanceToSceneCenter() + zClippingCoefficient()*sceneRadius(); \endcode

See the zNear() documentation for details. */
CGAL_INLINE_FUNCTION
qreal Camera::zFar() const {
  return distanceToSceneCenter() + zClippingCoefficient() * sceneRadius();
}

/*! Sets the vertical fieldOfView() of the Camera (in radians).

Note that focusDistance() is set to sceneRadius() / tan(fieldOfView()/2) by this
method. */
CGAL_INLINE_FUNCTION
void Camera::setFieldOfView(qreal fov) {
  fieldOfView_ = fov;
  projectionMatrixIsUpToDate_ = false;
}

/*! Defines the Camera type().

Changing the camera Type alters the viewport and the objects' sizes can be
changed. This method guarantees that the two frustum match in a plane normal to
viewDirection(), passing through the pivotPoint().

Prefix the type with \c Camera if needed, as in:
\code
camera()->setType(Camera::ORTHOGRAPHIC);
// or even CGAL::qglviewer::Camera::ORTHOGRAPHIC if you do not use namespace
\endcode */
CGAL_INLINE_FUNCTION
void Camera::setType(Type type) {
  // make ORTHOGRAPHIC frustum fit PERSPECTIVE (at least in plane normal to
  // viewDirection(), passing through RAP). Done only when CHANGING type since
  // orthoCoef_ may have been changed with a setPivotPoint() in the meantime.
  if ((type == Camera::ORTHOGRAPHIC) && (type_ == Camera::PERSPECTIVE))
    orthoCoef_ = tan(fieldOfView() / 2.0);
  type_ = type;
  projectionMatrixIsUpToDate_ = false;
}

/*! Sets the Camera frame().

If you want to move the Camera, use setPosition() and setOrientation() or one of
the Camera positioning methods (lookAt(), fitSphere(), showEntireScene()...)
instead.

If you want to save the Camera position(), there's no need to call this method
either. Use addKeyFrameToPath() and playPath() instead.

This method is actually mainly useful if you derive the ManipulatedCameraFrame
class and want to use an instance of your new class to move the Camera.

A \c nullptr \p mcf pointer will silently be ignored. The calling method is
responsible for deleting the previous frame() pointer if needed in order to
prevent memory leaks. */
CGAL_INLINE_FUNCTION
void Camera::setFrame(ManipulatedCameraFrame *const mcf) {
  if (!mcf)
    return;

  if (frame_) {
    disconnect(frame_, SIGNAL(modified()), this, SLOT(onFrameModified()));
  }

  frame_ = mcf;
  interpolationKfi_->setFrame(frame());

  connect(frame_, SIGNAL(modified()), this, SLOT(onFrameModified()));
  onFrameModified();
}

/*! Returns the distance from the Camera center to sceneCenter(), projected
  along the Camera Z axis. Used by zNear() and zFar() to optimize the Z range.
*/
CGAL_INLINE_FUNCTION
qreal Camera::distanceToSceneCenter() const {
  return fabs((frame()->coordinatesOf(sceneCenter())).z);
}

/*! Returns the \p halfWidth and \p halfHeight of the Camera orthographic
 frustum.

 These values are only valid and used when the Camera is of type()
 Camera::ORTHOGRAPHIC. They are expressed in OpenGL units and are used by
 loadProjectionMatrix() to define the projection matrix using: \code glOrtho(
 -halfWidth, halfWidth, -halfHeight, halfHeight, zNear(), zFar() ) \endcode

 These values are proportional to the Camera (z projected) distance to the
 pivotPoint(). When zooming on the object, the Camera is translated forward \e
 and its frustum is narrowed, making the object appear bigger on screen, as
 intuitively expected.

 Overload this method to change this behavior if desired, as is done in the
 <a href="../examples/standardCamera.html">standardCamera example</a>. */
CGAL_INLINE_FUNCTION
void Camera::getOrthoWidthHeight(GLdouble &halfWidth,
                                 GLdouble &halfHeight) const {
  const qreal dist = orthoCoef_ * fabs(cameraCoordinatesOf(pivotPoint()).z);
  //#CONNECTION# fitScreenRegion
  halfWidth = dist * ((aspectRatio() < 1.0) ? 1.0 : aspectRatio());
  halfHeight = dist * ((aspectRatio() < 1.0) ? 1.0 / aspectRatio() : 1.0);
}

/*! Computes the projection matrix associated with the Camera.

 If type() is Camera::PERSPECTIVE, defines a \c GL_PROJECTION matrix similar to
 what would \c gluPerspective() do using the fieldOfView(), window
 aspectRatio(), zNear() and zFar() parameters.

 If type() is Camera::ORTHOGRAPHIC, the projection matrix is as what \c
 glOrtho() would do. Frustum's width and height are set using
 getOrthoWidthHeight().

 Both types use zNear() and zFar() to place clipping planes. These values are
 determined from sceneRadius() and sceneCenter() so that they best fit the scene
 size.

 Use getProjectionMatrix() to retrieve this matrix. Overload
 loadProjectionMatrix() if you want your Camera to use an exotic projection
 matrix.

 \note You must call this method if your Camera is not associated with a
 CGAL::QGLViewer and is used for offscreen computations (using
 (un)projectedCoordinatesOf() for instance). loadProjectionMatrix() does it
 otherwise. */
CGAL_INLINE_FUNCTION
void Camera::computeProjectionMatrix() const {
  if (projectionMatrixIsUpToDate_)
    return;

  const qreal ZNear = zNear();
  const qreal ZFar = zFar();

  switch (type()) {
  case Camera::PERSPECTIVE: {
    // #CONNECTION# all non null coefficients were set to 0.0 in constructor.
    const qreal f = 1.0 / tan(fieldOfView() / 2.0);
    projectionMatrix_[0] = f / aspectRatio();
    projectionMatrix_[5] = f;
    projectionMatrix_[10] = (ZNear + ZFar) / (ZNear - ZFar);
    projectionMatrix_[11] = -1.0;
    projectionMatrix_[14] = 2.0 * ZNear * ZFar / (ZNear - ZFar);
    projectionMatrix_[15] = 0.0;
    // same as gluPerspective( 180.0*fieldOfView()/CGAL_PI, aspectRatio(), zNear(),
    // zFar() );
    break;
  }
  case Camera::ORTHOGRAPHIC: {
    GLdouble w, h;
    getOrthoWidthHeight(w, h);
    projectionMatrix_[0] = 1.0 / w;
    projectionMatrix_[5] = 1.0 / h;
    projectionMatrix_[10] = -2.0 / (ZFar - ZNear);
    projectionMatrix_[11] = 0.0;
    projectionMatrix_[14] = -(ZFar + ZNear) / (ZFar - ZNear);
    projectionMatrix_[15] = 1.0;
    // same as glOrtho( -w, w, -h, h, zNear(), zFar() );
    break;
  }
  }

  projectionMatrixIsUpToDate_ = true;
}

/*! Computes the modelView matrix associated with the Camera's position() and
 orientation().

 This matrix converts from the world coordinates system to the Camera
 coordinates system, so that coordinates can then be projected on screen using
 the projection matrix (see computeProjectionMatrix()).

 Use getModelViewMatrix() to retrieve this matrix.

 \note You must call this method if your Camera is not associated with a
 CGAL::QGLViewer and is used for offscreen computations (using
 (un)projectedCoordinatesOf() for instance). loadModelViewMatrix() does it
 otherwise. */
CGAL_INLINE_FUNCTION
void Camera::computeModelViewMatrix() const {
  if (modelViewMatrixIsUpToDate_)
    return;

  const Quaternion q = frame()->orientation();

  const qreal q00 = 2.0 * q[0] * q[0];
  const qreal q11 = 2.0 * q[1] * q[1];
  const qreal q22 = 2.0 * q[2] * q[2];

  const qreal q01 = 2.0 * q[0] * q[1];
  const qreal q02 = 2.0 * q[0] * q[2];
  const qreal q03 = 2.0 * q[0] * q[3];

  const qreal q12 = 2.0 * q[1] * q[2];
  const qreal q13 = 2.0 * q[1] * q[3];

  const qreal q23 = 2.0 * q[2] * q[3];

  modelViewMatrix_[0] = 1.0 - q11 - q22;
  modelViewMatrix_[1] = q01 - q23;
  modelViewMatrix_[2] = q02 + q13;
  modelViewMatrix_[3] = 0.0;

  modelViewMatrix_[4] = q01 + q23;
  modelViewMatrix_[5] = 1.0 - q22 - q00;
  modelViewMatrix_[6] = q12 - q03;
  modelViewMatrix_[7] = 0.0;

  modelViewMatrix_[8] = q02 - q13;
  modelViewMatrix_[9] = q12 + q03;
  modelViewMatrix_[10] = 1.0 - q11 - q00;
  modelViewMatrix_[11] = 0.0;

  const Vec t = q.inverseRotate(frame()->position());

  modelViewMatrix_[12] = -t.x;
  modelViewMatrix_[13] = -t.y;
  modelViewMatrix_[14] = -t.z;
  modelViewMatrix_[15] = 1.0;

  modelViewMatrixIsUpToDate_ = true;
}

/*! Loads the OpenGL \c GL_PROJECTION matrix with the Camera projection matrix.

 The Camera projection matrix is computed using computeProjectionMatrix().

 When \p reset is \c true (default), the method clears the previous projection
 matrix by calling \c glLoadIdentity before setting the matrix. Setting \p reset
 to \c false is useful for \c GL_SELECT mode, to combine the pushed matrix with
 a picking matrix. See CGAL::QGLViewer::beginSelection() for details.

 This method is used by CGAL::QGLViewer::preDraw() (called before user's
 CGAL::QGLViewer::draw() method) to set the \c GL_PROJECTION matrix according to the
 viewer's CGAL::QGLViewer::camera() settings.

 Use getProjectionMatrix() to retrieve this matrix. Overload this method if you
 want your Camera to use an exotic projection matrix. See also
 loadModelViewMatrix().

 \attention \c glMatrixMode is set to \c GL_PROJECTION.

 \attention If you use several OpenGL contexts and bypass the Qt main refresh
 loop, you should call QOpenGLWidget::makeCurrent() before this method in order
 to activate the right OpenGL context. */
CGAL_INLINE_FUNCTION
void Camera::loadProjectionMatrix(bool ) const {
  // WARNING: makeCurrent must be called by every calling method
  computeProjectionMatrix();
}

/*! Loads the OpenGL \c GL_MODELVIEW matrix with the modelView matrix
 corresponding to the Camera.

 Calls computeModelViewMatrix() to compute the Camera's modelView matrix.

 This method is used by CGAL::QGLViewer::preDraw() (called before user's
 CGAL::QGLViewer::draw() method) to set the \c GL_MODELVIEW matrix according to the
 viewer's CGAL::QGLViewer::camera() position() and orientation().

 As a result, the vertices used in CGAL::QGLViewer::draw() can be defined in the so
 called world coordinate system. They are multiplied by this matrix to get
 converted to the Camera coordinate system, before getting projected using the
 \c GL_PROJECTION matrix (see loadProjectionMatrix()).

 When \p reset is \c true (default), the method loads (overwrites) the \c
 GL_MODELVIEW matrix. Setting \p reset to \c false simply calls \c glMultMatrixd
 (might be useful for some applications).

 Overload this method or simply call glLoadMatrixd() at the beginning of
 CGAL::QGLViewer::draw() if you want your Camera to use an exotic modelView matrix.
 See also loadProjectionMatrix().

 getModelViewMatrix() returns the 4x4 modelView matrix.

 \attention glMatrixMode is set to \c GL_MODELVIEW

 \attention If you use several OpenGL contexts and bypass the Qt main refresh
 loop, you should call QOpenGLWidget::makeCurrent() before this method in order
 to activate the right OpenGL context. */
CGAL_INLINE_FUNCTION
void Camera::loadModelViewMatrix(bool ) const {
  // WARNING: makeCurrent must be called by every calling method
  computeModelViewMatrix();
}




/*! Fills \p m with the Camera projection matrix values.

 Based on computeProjectionMatrix() to make sure the Camera projection matrix is
 up to date.

 This matrix only reflects the Camera's internal parameters and it may differ
 from the \c GL_PROJECTION matrix retrieved using \c
 glGetDoublev(GL_PROJECTION_MATRIX, m). It actually represents the state of the
 \c GL_PROJECTION after CGAL::QGLViewer::preDraw(), at the beginning of
 CGAL::QGLViewer::draw(). If you modified the \c GL_PROJECTION matrix (for instance
 using CGAL::QGLViewer::startScreenCoordinatesSystem()), the two results differ.

 The result is an OpenGL 4x4 matrix, which is given in \e column-major order
 (see \c glMultMatrix man page for details).

 See also getModelViewMatrix() and setFromProjectionMatrix(). */
CGAL_INLINE_FUNCTION
void Camera::getProjectionMatrix(GLdouble m[16]) const {
  computeProjectionMatrix();
  for (unsigned short i = 0; i < 16; ++i)
    m[i] = projectionMatrix_[i];
}

/*! Overloaded getProjectionMatrix(GLdouble m[16]) method using a \c GLfloat
 * array instead. */
CGAL_INLINE_FUNCTION
void Camera::getProjectionMatrix(GLfloat m[16]) const {
  static GLdouble mat[16];
  getProjectionMatrix(mat);
  for (unsigned short i = 0; i < 16; ++i)
    m[i] = float(mat[i]);
}

/*! Fills \p m with the Camera modelView matrix values.

 First calls computeModelViewMatrix() to define the Camera modelView matrix.

 Note that this matrix may \e not be the one you would get from a \c
 glGetDoublev(GL_MODELVIEW_MATRIX, m). It actually represents the state of the
 \c GL_MODELVIEW after CGAL::QGLViewer::preDraw(), at the \e beginning of
 CGAL::QGLViewer::draw(). It converts from the world to the Camera coordinate system.
 As soon as you modify the \c GL_MODELVIEW in your CGAL::QGLViewer::draw() method
 (using glTranslate, glRotate... or similar methods), the two matrices differ.

 The result is an OpenGL 4x4 matrix, which is given in \e column-major order
 (see \c glMultMatrix man page for details).

 See also getProjectionMatrix() and setFromModelViewMatrix(). */
CGAL_INLINE_FUNCTION
void Camera::getModelViewMatrix(GLdouble m[16]) const {
  // May not be needed, but easier like this.
  computeModelViewMatrix();
  for (unsigned short i = 0; i < 16; ++i)
    m[i] = modelViewMatrix_[i];
}

/*! Overloaded getModelViewMatrix(GLdouble m[16]) method using a \c GLfloat
 * array instead. */
CGAL_INLINE_FUNCTION
void Camera::getModelViewMatrix(GLfloat m[16]) const {
  static GLdouble mat[16];
  getModelViewMatrix(mat);
  for (unsigned short i = 0; i < 16; ++i)
    m[i] = float(mat[i]);
}

/*! Fills \p m with the product of the ModelView and Projection matrices.

  Calls getModelViewMatrix() and getProjectionMatrix() and then fills \p m with
  the product of these two matrices. */
CGAL_INLINE_FUNCTION
void Camera::getModelViewProjectionMatrix(GLdouble m[16]) const {
  GLdouble mv[16];
  GLdouble proj[16];
  getModelViewMatrix(mv);
  getProjectionMatrix(proj);

  for (unsigned short i = 0; i < 4; ++i) {
    for (unsigned short j = 0; j < 4; ++j) {
      qreal sum = 0.0;
      for (unsigned short k = 0; k < 4; ++k)
        sum += proj[i + 4 * k] * mv[k + 4 * j];
      m[i + 4 * j] = sum;
    }
  }
}

/*! Overloaded getModelViewProjectionMatrix(GLdouble m[16]) method using a \c
 * GLfloat array instead. */
CGAL_INLINE_FUNCTION
void Camera::getModelViewProjectionMatrix(GLfloat m[16]) const {
  static GLdouble mat[16];
  getModelViewProjectionMatrix(mat);
  for (unsigned short i = 0; i < 16; ++i)
    m[i] = float(mat[i]);
}

/*! Sets the sceneRadius() value. Negative values are ignored.

\attention This methods also sets focusDistance() to sceneRadius() /
tan(fieldOfView()/2) and flySpeed() to 1% of sceneRadius(). */
CGAL_INLINE_FUNCTION
void Camera::setSceneRadius(qreal radius) {
  if (radius <= 0.0) {
    qWarning("Scene radius must be positive - Ignoring value");
    return;
  }

  sceneRadius_ = radius;
  projectionMatrixIsUpToDate_ = false;

  frame()->setFlySpeed(0.01 * sceneRadius());
}

/*! Similar to setSceneRadius() and setSceneCenter(), but the scene limits are
  defined by a (world axis aligned) bounding box. */
CGAL_INLINE_FUNCTION
void Camera::setSceneBoundingBox(const Vec &min, const Vec &max) {
  setSceneCenter((min + max) / 2.0);
  setSceneRadius(0.5 * (max - min).norm());
}

/*! Sets the sceneCenter().

 \attention This method also sets the pivotPoint() to sceneCenter(). */
CGAL_INLINE_FUNCTION
void Camera::setSceneCenter(const Vec &center) {
  sceneCenter_ = center;
  setPivotPoint(sceneCenter());
  projectionMatrixIsUpToDate_ = false;
}

/*! setSceneCenter() to the result of pointUnderPixel(\p pixel).

  Returns \c true if a pointUnderPixel() was found and sceneCenter() was
  actually changed.

  See also setPivotPointFromPixel(). See the pointUnderPixel() documentation. */
CGAL_INLINE_FUNCTION
bool Camera::setSceneCenterFromPixel(const QPoint &pixel) {
  bool found;
  Vec point = pointUnderPixel(pixel, found);
  if (found)
    setSceneCenter(point);
  return found;
}

/*! Changes the pivotPoint() to \p point (defined in the world coordinate
 * system). */
CGAL_INLINE_FUNCTION
void Camera::setPivotPoint(const Vec &point) {
  const qreal prevDist = fabs(cameraCoordinatesOf(pivotPoint()).z);

  // If frame's RAP is set directly, projectionMatrixIsUpToDate_ should also be
  // set to false to ensure proper recomputation of the ORTHO projection matrix.
  frame()->setPivotPoint(point);

  // orthoCoef_ is used to compensate for changes of the pivotPoint, so that the
  // image does not change when the pivotPoint is changed in ORTHOGRAPHIC mode.
  const qreal newDist = fabs(cameraCoordinatesOf(pivotPoint()).z);
  // Prevents division by zero when rap is set to camera position
  if ((prevDist > 1E-9) && (newDist > 1E-9))
    orthoCoef_ *= prevDist / newDist;
  projectionMatrixIsUpToDate_ = false;
}

/*! The pivotPoint() is set to the point located under \p pixel on screen.

Returns \c true if a pointUnderPixel() was found. If no point was found under \p
pixel, the pivotPoint() is left unchanged.

\p pixel is expressed in Qt format (origin in the upper left corner of the
window). See pointUnderPixel().

See also setSceneCenterFromPixel(). */
CGAL_INLINE_FUNCTION
bool Camera::setPivotPointFromPixel(const QPoint &pixel) {
  bool found;
  Vec point = pointUnderPixel(pixel, found);
  if (found)
    setPivotPoint(point);
  return found;
}

/*! Returns the ratio between pixel and OpenGL units at \p position.

 A line of \c n * pixelGLRatio() OpenGL units, located at \p position in the
 world coordinates system, will be projected with a length of \c n pixels on
 screen.

 Use this method to scale objects so that they have a constant pixel size on
 screen. The following code will draw a 20 pixel line, starting at sceneCenter()
 and always directed along the screen vertical direction: \code
 glBegin(GL_LINES);
 glVertex3fv(sceneCenter());
 glVertex3fv(sceneCenter() + 20 * pixelGLRatio(sceneCenter()) *
 camera()->upVector()); glEnd(); \endcode */
CGAL_INLINE_FUNCTION
qreal Camera::pixelGLRatio(const Vec &position) const {
  switch (type()) {
  case Camera::PERSPECTIVE:
    return 2.0 * fabs((frame()->coordinatesOf(position)).z) *
           tan(fieldOfView() / 2.0) / screenHeight();
  case Camera::ORTHOGRAPHIC: {
    GLdouble w, h;
    getOrthoWidthHeight(w, h);
    return 2.0 * h / screenHeight();
  }
  }
  // Bad compilers complain
  return 1.0;
}

/*! Changes the Camera fieldOfView() so that the entire scene (defined by
 CGAL::QGLViewer::sceneCenter() and CGAL::QGLViewer::sceneRadius()) is visible from the
 Camera position().

 The position() and orientation() of the Camera are not modified and you first
 have to orientate the Camera in order to actually see the scene (see lookAt(),
 showEntireScene() or fitSphere()).

 This method is especially useful for \e shadow \e maps computation. Use the
 Camera positioning tools (setPosition(), lookAt()) to position a Camera at the
 light position. Then use this method to define the fieldOfView() so that the
 shadow map resolution is optimally used: \code
 // The light camera needs size hints in order to optimize its fieldOfView
 lightCamera->setSceneRadius(sceneRadius());
 lightCamera->setSceneCenter(sceneCenter());

 // Place the light camera.
 lightCamera->setPosition(lightFrame->position());
 lightCamera->lookAt(sceneCenter());
 lightCamera->setFOVToFitScene();
 \endcode

 See the (soon available) shadowMap contribution example for a practical
 implementation.

 \attention The fieldOfView() is clamped to CGAL_PI/2.0. This happens when the
 Camera is at a distance lower than sqrt(2.0) * sceneRadius() from the
 sceneCenter(). It optimizes the shadow map resolution, although it may miss
 some parts of the scene. */
CGAL_INLINE_FUNCTION
void Camera::setFOVToFitScene() {
  if (distanceToSceneCenter() > sqrt(2.0) * sceneRadius())
    setFieldOfView(2.0 * asin(sceneRadius() / distanceToSceneCenter()));
  else
    setFieldOfView(CGAL_PI / 2.0);
}

/*! Makes the Camera smoothly zoom on the pointUnderPixel() \p pixel.

 Nothing happens if no pointUnderPixel() is found. Otherwise a
 KeyFrameInterpolator is created that animates the Camera on a one second path
 that brings the Camera closer to the point under \p pixel.

 See also interpolateToFitScene(). */
CGAL_INLINE_FUNCTION
void Camera::interpolateToZoomOnPixel(const QPoint &pixel) {
  const qreal coef = 0.1;

  bool found;
  Vec target = pointUnderPixel(pixel, found);

  if (!found)
    return;

  if (interpolationKfi_->interpolationIsStarted())
    interpolationKfi_->stopInterpolation();

  interpolationKfi_->deletePath();
  interpolationKfi_->addKeyFrame(*(frame()));

  interpolationKfi_->addKeyFrame(
      Frame(0.3 * frame()->position() + 0.7 * target, frame()->orientation()),
      0.4);

  // Small hack: attach a temporary frame to take advantage of lookAt without
  // modifying frame
  static ManipulatedCameraFrame *tempFrame = new ManipulatedCameraFrame();
  ManipulatedCameraFrame *const originalFrame = frame();
  tempFrame->setPosition(coef * frame()->position() + (1.0 - coef) * target);
  tempFrame->setOrientation(frame()->orientation());
  setFrame(tempFrame);
  lookAt(target);
  setFrame(originalFrame);

  interpolationKfi_->addKeyFrame(*(tempFrame), 1.0);

  interpolationKfi_->startInterpolation();
}

/*! Interpolates the Camera on a one second KeyFrameInterpolator path so that
 the entire scene fits the screen at the end.

 The scene is defined by its sceneCenter() and its sceneRadius(). See
 showEntireScene().

 The orientation() of the Camera is not modified. See also
 interpolateToZoomOnPixel(). */
CGAL_INLINE_FUNCTION
void Camera::interpolateToFitScene() {
  if (interpolationKfi_->interpolationIsStarted())
    interpolationKfi_->stopInterpolation();

  interpolationKfi_->deletePath();
  interpolationKfi_->addKeyFrame(*(frame()));

  // Small hack:  attach a temporary frame to take advantage of lookAt without
  // modifying frame
  static ManipulatedCameraFrame *tempFrame = new ManipulatedCameraFrame();
  ManipulatedCameraFrame *const originalFrame = frame();
  tempFrame->setPosition(frame()->position());
  tempFrame->setOrientation(frame()->orientation());
  setFrame(tempFrame);
  showEntireScene();
  setFrame(originalFrame);

  interpolationKfi_->addKeyFrame(*(tempFrame));

  interpolationKfi_->startInterpolation();
}

/*! Smoothly interpolates the Camera on a KeyFrameInterpolator path so that it
  goes to \p fr.

  \p fr is expressed in world coordinates. \p duration tunes the interpolation
  speed (default is 1 second).

  See also interpolateToFitScene() and interpolateToZoomOnPixel(). */
CGAL_INLINE_FUNCTION
void Camera::interpolateTo(const Frame &fr, qreal duration) {
  if (interpolationKfi_->interpolationIsStarted())
    interpolationKfi_->stopInterpolation();

  interpolationKfi_->deletePath();
  interpolationKfi_->addKeyFrame(*(frame()));
  interpolationKfi_->addKeyFrame(fr, duration);

  interpolationKfi_->startInterpolation();
}

/*! Returns the coordinates of the 3D point located at pixel (x,y) on screen.

 Calls a \c glReadPixel to get the pixel depth and applies an
 unprojectedCoordinatesOf() to the result. \p found indicates whether a point
 was found or not (i.e. background pixel, result's depth is zFar() in that
 case).

 \p x and \p y are expressed in pixel units with an origin in the upper left
 corner. Use screenHeight() - y to convert to OpenGL standard.

 \attention This method assumes that a GL context is available, and that its
 content was drawn using the Camera (i.e. using its projection and modelview
 matrices). This method hence cannot be used for offscreen Camera computations.
 Use cameraCoordinatesOf() and worldCoordinatesOf() to perform similar
 operations in that case.

 \note The precision of the z-Buffer highly depends on how the zNear() and
 zFar() values are fitted to your scene. Loose boundaries will result in
 imprecision along the viewing direction. */
CGAL_INLINE_FUNCTION
Vec Camera::pointUnderPixel(const QPoint &pixel, bool &found) const {
  float depth = 2.0;
  // Qt uses upper corner for its origin while GL uses the lower corner.
  if(auto p = dynamic_cast<QOpenGLFunctions*>(parent()))
  {
    depth = read_depth_under_pixel(pixel, p, this);
  }
  found = depth < 1.0;
  Vec point(pixel.x(), pixel.y(), depth);
  point = unprojectedCoordinatesOf(point);
  return point;
}

/*! Moves the Camera so that the entire scene is visible.

 Calls fitSphere() on a sphere defined by sceneCenter() and
 sceneRadius(), and resets the default FOV.

 You will typically use this method in CGAL::QGLViewer::init() after you defined a new
 sceneRadius(). */
CGAL_INLINE_FUNCTION
void Camera::showEntireScene()
{
  setFieldOfView(CGAL_PI/4.0);
  fitSphere(sceneCenter(), sceneRadius());
}

/*! Moves the Camera so that its sceneCenter() is projected on the center of the
 window. The orientation() and fieldOfView() are unchanged.

 Simply projects the current position on a line passing through sceneCenter().
 See also showEntireScene().*/
CGAL_INLINE_FUNCTION
void Camera::centerScene() {
  frame()->projectOnLine(sceneCenter(), viewDirection());
}

/*! Sets the Camera orientation(), so that it looks at point \p target (defined
 in the world coordinate system).

 The Camera position() is not modified. Simply setViewDirection().

 See also setUpVector(), setOrientation(), showEntireScene(), fitSphere() and
 fitBoundingBox(). */
CGAL_INLINE_FUNCTION
void Camera::lookAt(const Vec &target) {
  setViewDirection(target - position());
}

/*! Moves the Camera so that the sphere defined by (\p center, \p radius) is
 visible and fits in the frustum.

 The Camera is simply translated to center the sphere in the screen and make it
 fit the frustum. Its orientation() and its fieldOfView() are unchanged.

 You should therefore orientate the Camera before you call this method. See
 lookAt(), setOrientation() and setUpVector(). */
CGAL_INLINE_FUNCTION
void Camera::fitSphere(const Vec &center, qreal radius) {
  qreal distance = 0.0;
  switch (type()) {
  case Camera::PERSPECTIVE: {
    const qreal yview = radius / sin(fieldOfView() / 2.0);
    const qreal xview = radius / sin(horizontalFieldOfView() / 2.0);
    distance = qMax(xview, yview);
    break;
  }
  case Camera::ORTHOGRAPHIC: {
    distance =
        ((center - pivotPoint()) * viewDirection()) + (radius / orthoCoef_);
    break;
  }
  }
  Vec newPos(center - distance * viewDirection());
  frame()->setPositionWithConstraint(newPos);
}

/*! Moves the Camera so that the (world axis aligned) bounding box (\p min, \p
  max) is entirely visible, using fitSphere(). */
CGAL_INLINE_FUNCTION
void Camera::fitBoundingBox(const Vec &min, const Vec &max) {
  qreal diameter = qMax(fabs(max[1] - min[1]), fabs(max[0] - min[0]));
  diameter = qMax(fabs(max[2] - min[2]), diameter);
  fitSphere(0.5 * (min + max), 0.5 * diameter);
}

/*! Moves the Camera so that the rectangular screen region defined by \p
  rectangle (pixel units, with origin in the upper left corner) fits the screen.

  The Camera is translated (its orientation() is unchanged) so that \p rectangle
  is entirely visible. Since the pixel coordinates only define a \e frustum in
  3D, it's the intersection of this frustum with a plane (orthogonal to the
  viewDirection() and passing through the sceneCenter()) that is used to define
  the 3D rectangle that is eventually fitted. */
CGAL_INLINE_FUNCTION
void Camera::fitScreenRegion(const QRect &rectangle) {
  const Vec vd = viewDirection();
  const qreal distToPlane = distanceToSceneCenter();
  const QPoint center = rectangle.center();

  Vec orig, dir;
  convertClickToLine(center, orig, dir);
  Vec newCenter = orig + distToPlane / (dir * vd) * dir;

  convertClickToLine(QPoint(rectangle.x(), center.y()), orig, dir);
  const Vec pointX = orig + distToPlane / (dir * vd) * dir;

  convertClickToLine(QPoint(center.x(), rectangle.y()), orig, dir);
  const Vec pointY = orig + distToPlane / (dir * vd) * dir;

  qreal distance = 0.0;
  switch (type()) {
  case Camera::PERSPECTIVE: {
    const qreal distX =
        (pointX - newCenter).norm() / sin(horizontalFieldOfView() / 2.0);
    const qreal distY = (pointY - newCenter).norm() / sin(fieldOfView() / 2.0);
    distance = qMax(distX, distY);
    break;
  }
  case Camera::ORTHOGRAPHIC: {
    const qreal dist = ((newCenter - pivotPoint()) * vd);
    //#CONNECTION# getOrthoWidthHeight
    const qreal distX = (pointX - newCenter).norm() / orthoCoef_ /
                        ((aspectRatio() < 1.0) ? 1.0 : aspectRatio());
    const qreal distY = (pointY - newCenter).norm() / orthoCoef_ /
                        ((aspectRatio() < 1.0) ? 1.0 / aspectRatio() : 1.0);
    distance = dist + qMax(distX, distY);
    break;
  }
  }

  Vec newPos(newCenter - distance * vd);
  frame()->setPositionWithConstraint(newPos);
}

/*! Rotates the Camera so that its upVector() becomes \p up (defined in the
 world coordinate system).

 The Camera is rotated around an axis orthogonal to \p up and to the current
 upVector() direction. Use this method in order to define the Camera horizontal
 plane.

 When \p noMove is set to \c false, the orientation modification is compensated
 by a translation, so that the pivotPoint() stays projected at the same position
 on screen. This is especially useful when the Camera is used as an observer of
 the scene (default mouse binding).

 When \p noMove is \c true (default), the Camera position() is left unchanged,
 which is an intuitive behavior when the Camera is in a walkthrough fly mode
 (see the CGAL::QGLViewer::MOVE_FORWARD and CGAL::QGLViewer::MOVE_BACKWARD
 CGAL::QGLViewer::MouseAction).

 The frame()'s ManipulatedCameraFrame::sceneUpVector() is set accordingly.

 See also setViewDirection(), lookAt() and setOrientation(). */
CGAL_INLINE_FUNCTION
void Camera::setUpVector(const Vec &up, bool noMove) {
  Quaternion q(Vec(0.0, 1.0, 0.0), frame()->transformOf(up));

  if (!noMove)
    frame()->setPosition(pivotPoint() -
                         (frame()->orientation() * q)
                             .rotate(frame()->coordinatesOf(pivotPoint())));

  frame()->rotate(q);

  // Useful in fly mode to keep the horizontal direction.
  frame()->updateSceneUpVector();
}

/*! Sets the orientation() of the Camera using polar coordinates.

 \p theta rotates the Camera around its Y axis, and \e then \p phi rotates it
 around its X axis. The polar coordinates are defined in the world coordinates
 system: \p theta = \p phi = 0 means that the Camera is directed towards the
 world Z axis. Both angles are expressed in radians.

 See also setUpVector(). The position() of the Camera is unchanged, you may want
 to call showEntireScene() after this method to move the Camera.

 This method can be useful to create Quicktime VR panoramic sequences, see the
 CGAL::QGLViewer::saveSnapshot() documentation for details. */
CGAL_INLINE_FUNCTION
void Camera::setOrientation(qreal theta, qreal phi) {
  Vec axis(0.0, 1.0, 0.0);
  const Quaternion rot1(axis, theta);
  axis = Vec(-cos(theta), 0.0, sin(theta));
  const Quaternion rot2(axis, phi);
  setOrientation(rot1 * rot2);
}

/*! Sets the Camera orientation(), defined in the world coordinate system. */
CGAL_INLINE_FUNCTION
void Camera::setOrientation(const Quaternion &q) {
  frame()->setOrientation(q);
  frame()->updateSceneUpVector();
}

/*! Rotates the Camera so that its viewDirection() is \p direction (defined in
 the world coordinate system).

 The Camera position() is not modified. The Camera is rotated so that the
 horizon (defined by its upVector()) is preserved. See also lookAt() and
 setUpVector(). */
CGAL_INLINE_FUNCTION
void Camera::setViewDirection(const Vec &direction) {
  if (direction.squaredNorm() < 1E-10)
    return;

  Vec xAxis = direction ^ upVector();
  if (xAxis.squaredNorm() < 1E-10) {
    // target is aligned with upVector, this means a rotation around X axis
    // X axis is then unchanged, let's keep it !
    xAxis = frame()->inverseTransformOf(Vec(1.0, 0.0, 0.0));
  }

  Quaternion q;
  q.setFromRotatedBasis(xAxis, xAxis ^ direction, -direction);
  frame()->setOrientationWithConstraint(q);
}

// Compute a 3 by 3 determinant.
static qreal det(qreal m00, qreal m01, qreal m02, qreal m10, qreal m11,
                 qreal m12, qreal m20, qreal m21, qreal m22) {
  return m00 * m11 * m22 + m01 * m12 * m20 + m02 * m10 * m21 - m20 * m11 * m02 -
         m10 * m01 * m22 - m00 * m21 * m12;
}

// Computes the index of element [i][j] in a \c qreal matrix[3][4].
static inline unsigned int ind(unsigned int i, unsigned int j) {
  return (i * 4 + j);
}

/*! Returns the Camera position (the eye), defined in the world coordinate
system.

Use setPosition() to set the Camera position. Other convenient methods are
showEntireScene() or fitSphere(). Actually returns \c frame()->position().

This position corresponds to the projection center of a Camera::PERSPECTIVE
Camera. It is not located in the image plane, which is at a zNear() distance
ahead. */
CGAL_INLINE_FUNCTION
Vec Camera::position() const { return frame()->position(); }

/*! Returns the normalized up vector of the Camera, defined in the world
coordinate system.

Set using setUpVector() or setOrientation(). It is orthogonal to viewDirection()
and to rightVector().

It corresponds to the Y axis of the associated frame() (actually returns
frame()->inverseTransformOf(Vec(0.0, 1.0, 0.0)) ). */
CGAL_INLINE_FUNCTION
Vec Camera::upVector() const {
  return frame()->inverseTransformOf(Vec(0.0, 1.0, 0.0));
}
/*! Returns the normalized view direction of the Camera, defined in the world
coordinate system.

Change this value using setViewDirection(), lookAt() or setOrientation(). It is
orthogonal to upVector() and to rightVector().

This corresponds to the negative Z axis of the frame() (
frame()->inverseTransformOf(Vec(0.0, 0.0, -1.0)) ). */
CGAL_INLINE_FUNCTION
Vec Camera::viewDirection() const {
  return frame()->inverseTransformOf(Vec(0.0, 0.0, -1.0));
}

/*! Returns the normalized right vector of the Camera, defined in the world
coordinate system.

This vector lies in the Camera horizontal plane, directed along the X axis
(orthogonal to upVector() and to viewDirection()). Set using setUpVector(),
lookAt() or setOrientation().

Simply returns frame()->inverseTransformOf(Vec(1.0, 0.0, 0.0)). */
CGAL_INLINE_FUNCTION
Vec Camera::rightVector() const {
  return frame()->inverseTransformOf(Vec(1.0, 0.0, 0.0));
}

/*! Returns the Camera orientation, defined in the world coordinate system.

Actually returns \c frame()->orientation(). Use setOrientation(), setUpVector()
or lookAt() to set the Camera orientation. */
CGAL_INLINE_FUNCTION
Quaternion Camera::orientation() const { return frame()->orientation(); }

/*! Sets the Camera position() (the eye), defined in the world coordinate
 * system. */
CGAL_INLINE_FUNCTION
void Camera::setPosition(const Vec &pos) { frame()->setPosition(pos); }

/*! Returns the Camera frame coordinates of a point \p src defined in world
coordinates.

worldCoordinatesOf() performs the inverse transformation.

Note that the point coordinates are simply converted in a different coordinate
system. They are not projected on screen. Use projectedCoordinatesOf() for that.
*/
CGAL_INLINE_FUNCTION
Vec Camera::cameraCoordinatesOf(const Vec &src) const {
  return frame()->coordinatesOf(src);
}

/*! Returns the world coordinates of the point whose position \p src is defined
in the Camera coordinate system.

cameraCoordinatesOf() performs the inverse transformation. */
CGAL_INLINE_FUNCTION
Vec Camera::worldCoordinatesOf(const Vec &src) const {
  return frame()->inverseCoordinatesOf(src);
}

/*! Returns the fly speed of the Camera.

Simply returns frame()->flySpeed(). See the ManipulatedCameraFrame::flySpeed()
documentation. This value is only meaningful when the MouseAction bindings is
CGAL::QGLViewer::MOVE_FORWARD or CGAL::QGLViewer::MOVE_BACKWARD.

Set to 1% of the sceneRadius() by setSceneRadius(). See also setFlySpeed(). */
CGAL_INLINE_FUNCTION
qreal Camera::flySpeed() const { return frame()->flySpeed(); }

/*! Sets the Camera flySpeed().

\attention This value is modified by setSceneRadius(). */
CGAL_INLINE_FUNCTION
void Camera::setFlySpeed(qreal speed) { frame()->setFlySpeed(speed); }

/*! The point the Camera pivots around with the CGAL::QGLViewer::ROTATE mouse binding.
Defined in world coordinate system.

Default value is the sceneCenter().

\attention setSceneCenter() changes this value. */
CGAL_INLINE_FUNCTION
Vec Camera::pivotPoint() const { return frame()->pivotPoint(); }

/*! Sets the Camera's position() and orientation() from an OpenGL ModelView
matrix.

This enables a Camera initialization from an other OpenGL application. \p
modelView is a 16 GLdouble vector representing a valid OpenGL ModelView matrix,
such as one can get using: \code GLdouble mvm[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvm);
myCamera->setFromModelViewMatrix(mvm);
\endcode

After this method has been called, getModelViewMatrix() returns a matrix
equivalent to \p modelView.

Only the orientation() and position() of the Camera are modified.

\note If you defined your matrix as \c GLdouble \c mvm[4][4], pass \c
&(mvm[0][0]) as a parameter. */
CGAL_INLINE_FUNCTION
void Camera::setFromModelViewMatrix(const GLdouble *const modelViewMatrix) {
  // Get upper left (rotation) matrix
  qreal upperLeft[3][3];
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
      upperLeft[i][j] = modelViewMatrix[i * 4 + j];

  // Transform upperLeft into the associated Quaternion
  Quaternion q;
  q.setFromRotationMatrix(upperLeft);

  setOrientation(q);
  setPosition(-q.rotate(
      Vec(modelViewMatrix[12], modelViewMatrix[13], modelViewMatrix[14])));
}

/*! Defines the Camera position(), orientation() and fieldOfView() from a
 projection matrix.

 \p matrix has to be given in the format used by vision algorithm. It has 3
 lines and 4 columns. It transforms a point from the world homogeneous
 coordinate system (4 coordinates: \c sx, \c sy, \c sz and \c s) into a point in
 the screen homogeneous coordinate system (3 coordinates: \c sx, \c sy, and \c
 s, where \c x and \c y are the pixel coordinates on the screen).

 Its three lines correspond to the homogeneous coordinates of the normals to the
 planes x=0, y=0 and z=0, defined in the Camera coordinate system.

 The elements of the matrix are ordered in line major order: you can call \c
 setFromProjectionMatrix(&(matrix[0][0])) if you defined your matrix as a \c
 qreal \c matrix[3][4].

 \attention Passing the result of getProjectionMatrix() or getModelViewMatrix()
 to this method is not possible (purposefully incompatible matrix dimensions).
 \p matrix is more likely to be the product of these two matrices, without the
 last line.

 Use setFromModelViewMatrix() to set position() and orientation() from a \c
 GL_MODELVIEW matrix. fieldOfView() can also be retrieved from a \e perspective
 \c GL_PROJECTION matrix using 2.0 * atan(1.0/projectionMatrix[5]).

 This code was written by Sylvain Paris. */
CGAL_INLINE_FUNCTION
void Camera::setFromProjectionMatrix(const qreal matrix[12]) {
  // The 3 lines of the matrix are the normals to the planes x=0, y=0, z=0
  // in the camera CS. As we normalize them, we do not need the 4th coordinate.
  Vec line_0(matrix[ind(0, 0)], matrix[ind(0, 1)], matrix[ind(0, 2)]);
  Vec line_1(matrix[ind(1, 0)], matrix[ind(1, 1)], matrix[ind(1, 2)]);
  Vec line_2(matrix[ind(2, 0)], matrix[ind(2, 1)], matrix[ind(2, 2)]);

  line_0.normalize();
  line_1.normalize();
  line_2.normalize();

  // The camera position is at (0,0,0) in the camera CS so it is the
  // intersection of the 3 planes. It can be seen as the kernel
  // of the 3x4 projection matrix. We calculate it through 4 dimensional
  // vectorial product. We go directly into 3D that is to say we directly
  // divide the first 3 coordinates by the 4th one.

  // We derive the 4 dimensional vectorial product formula from the
  // computation of a 4x4 determinant that is developed according to
  // its 4th column. This implies some 3x3 determinants.
  const Vec cam_pos =
      Vec(det(matrix[ind(0, 1)], matrix[ind(0, 2)], matrix[ind(0, 3)],
              matrix[ind(1, 1)], matrix[ind(1, 2)], matrix[ind(1, 3)],
              matrix[ind(2, 1)], matrix[ind(2, 2)], matrix[ind(2, 3)]),

          -det(matrix[ind(0, 0)], matrix[ind(0, 2)], matrix[ind(0, 3)],
               matrix[ind(1, 0)], matrix[ind(1, 2)], matrix[ind(1, 3)],
               matrix[ind(2, 0)], matrix[ind(2, 2)], matrix[ind(2, 3)]),

          det(matrix[ind(0, 0)], matrix[ind(0, 1)], matrix[ind(0, 3)],
              matrix[ind(1, 0)], matrix[ind(1, 1)], matrix[ind(1, 3)],
              matrix[ind(2, 0)], matrix[ind(2, 1)], matrix[ind(2, 3)])) /

      (-det(matrix[ind(0, 0)], matrix[ind(0, 1)], matrix[ind(0, 2)],
            matrix[ind(1, 0)], matrix[ind(1, 1)], matrix[ind(1, 2)],
            matrix[ind(2, 0)], matrix[ind(2, 1)], matrix[ind(2, 2)]));

  // We compute the rotation matrix column by column.

  // GL Z axis is front facing.
  Vec column_2 = -line_2;

  // X-axis is almost like line_0 but should be orthogonal to the Z axis.
  Vec column_0 = ((column_2 ^ line_0) ^ column_2);
  column_0.normalize();

  // Y-axis is almost like line_1 but should be orthogonal to the Z axis.
  // Moreover line_1 is downward oriented as the screen CS.
  Vec column_1 = -((column_2 ^ line_1) ^ column_2);
  column_1.normalize();

  qreal rot[3][3];
  rot[0][0] = column_0[0];
  rot[1][0] = column_0[1];
  rot[2][0] = column_0[2];

  rot[0][1] = column_1[0];
  rot[1][1] = column_1[1];
  rot[2][1] = column_1[2];

  rot[0][2] = column_2[0];
  rot[1][2] = column_2[1];
  rot[2][2] = column_2[2];

  // We compute the field of view

  // line_1^column_0 -> vector of intersection line between
  // y_screen=0 and x_camera=0 plane.
  // column_2*(...)  -> cos of the angle between Z vector et y_screen=0 plane
  // * 2 -> field of view = 2 * half angle

  // We need some intermediate values.
  Vec dummy = line_1 ^ column_0;
  dummy.normalize();
  qreal fov = acos(column_2 * dummy) * 2.0;

  // We set the camera.
  Quaternion q;
  q.setFromRotationMatrix(rot);
  setOrientation(q);
  setPosition(cam_pos);
  setFieldOfView(fov);
}

/*
  // persp : projectionMatrix_[0]  = f/aspectRatio();
CGAL_INLINE_FUNCTION
void Camera::setFromProjectionMatrix(const GLdouble* projectionMatrix)
{
  QString message;
  if ((fabs(projectionMatrix[1]) > 1E-3) ||
    (fabs(projectionMatrix[2]) > 1E-3) ||
    (fabs(projectionMatrix[3]) > 1E-3) ||
    (fabs(projectionMatrix[4]) > 1E-3) ||
    (fabs(projectionMatrix[6]) > 1E-3) ||
    (fabs(projectionMatrix[7]) > 1E-3) ||
    (fabs(projectionMatrix[8]) > 1E-3) ||
    (fabs(projectionMatrix[9]) > 1E-3))
  message = "Non null coefficient in projection matrix - Aborting";
  else
  if ((fabs(projectionMatrix[11]+1.0) < 1E-5) && (fabs(projectionMatrix[15]) <
1E-5))
    {
  if (projectionMatrix[5] < 1E-4)
    message="Negative field of view in Camera::setFromProjectionMatrix";
  else
    setType(Camera::PERSPECTIVE);
    }
  else
    if ((fabs(projectionMatrix[11]) < 1E-5) && (fabs(projectionMatrix[15]-1.0) <
1E-5)) setType(Camera::ORTHOGRAPHIC); else message = "Unable to determine camera
type in setFromProjectionMatrix - Aborting";

  if (!message.isEmpty())
  {
    qWarning(message);
    return;
  }

  switch (type())
  {
  case Camera::PERSPECTIVE:
    {
  setFieldOfView(2.0 * atan(1.0/projectionMatrix[5]));
  const qreal far = projectionMatrix[14] / (2.0 * (1.0 + projectionMatrix[10]));
  const qreal near = (projectionMatrix[10]+1.0) / (projectionMatrix[10]-1.0) *
far; setSceneRadius((far-near)/2.0); setSceneCenter(position() + (near +
sceneRadius())*viewDirection()); break;
    }
  case Camera::ORTHOGRAPHIC:
    {
  GLdouble w, h;
  getOrthoWidthHeight(w,h);
  projectionMatrix_[0]  = 1.0/w;
  projectionMatrix_[5]  = 1.0/h;
  projectionMatrix_[10] = -2.0/(ZFar - ZNear);
  projectionMatrix_[11] = 0.0;
  projectionMatrix_[14] = -(ZFar + ZNear)/(ZFar - ZNear);
  projectionMatrix_[15] = 1.0;
  // same as glOrtho( -w, w, -h, h, zNear(), zFar() );
  break;
    }
  }
}
*/

///////////////////////// Camera to world transform ///////////////////////

/*! Same as cameraCoordinatesOf(), but with \c qreal[3] parameters (\p src and
 * \p res may be identical pointers). */
CGAL_INLINE_FUNCTION
void Camera::getCameraCoordinatesOf(const qreal src[3], qreal res[3]) const {
  Vec r = cameraCoordinatesOf(Vec(src));
  for (int i = 0; i < 3; ++i)
    res[i] = r[i];
}

/*! Same as worldCoordinatesOf(), but with \c qreal[3] parameters (\p src and \p
 * res may be identical pointers). */
CGAL_INLINE_FUNCTION
void Camera::getWorldCoordinatesOf(const qreal src[3], qreal res[3]) const {
  Vec r = worldCoordinatesOf(Vec(src));
  for (int i = 0; i < 3; ++i)
    res[i] = r[i];
}

/*! Fills \p viewport with the Camera OpenGL viewport.

This method is mainly used in conjunction with \c gluProject, which requires
such a viewport. Returned values are (0, screenHeight(), screenWidth(), -
screenHeight()), so that the origin is located in the \e upper left corner of
the window (Qt style coordinate system). */
CGAL_INLINE_FUNCTION
void Camera::getViewport(GLint viewport[4]) const {
  viewport[0] = 0;
  viewport[1] = screenHeight();
  viewport[2] = screenWidth();
  viewport[3] = -screenHeight();
}

//source code of GluProject and GluUnproject, imported here to avoid the dependency to Glu
CGAL_INLINE_FUNCTION
int project(qreal objx, qreal objy, qreal objz, GLdouble *modelview,
            GLdouble *projection, int *viewport, GLdouble*winX, GLdouble *winY,GLdouble *winZ)
  {
      //Transformation vectors
      GLdouble fTempo[8];
      //Modelview transform
      fTempo[0]=modelview[0]*objx+modelview[4]*objy+modelview[8]*objz+modelview[12];  //w is always 1
      fTempo[1]=modelview[1]*objx+modelview[5]*objy+modelview[9]*objz+modelview[13];
      fTempo[2]=modelview[2]*objx+modelview[6]*objy+modelview[10]*objz+modelview[14];
      fTempo[3]=modelview[3]*objx+modelview[7]*objy+modelview[11]*objz+modelview[15];
      fTempo[4]=projection[0]*fTempo[0]+projection[4]*fTempo[1]+projection[8]*fTempo[2]+projection[12]*fTempo[3];
      fTempo[5]=projection[1]*fTempo[0]+projection[5]*fTempo[1]+projection[9]*fTempo[2]+projection[13]*fTempo[3];
      fTempo[6]=projection[2]*fTempo[0]+projection[6]*fTempo[1]+projection[10]*fTempo[2]+projection[14]*fTempo[3];
      fTempo[7]=projection[3]*fTempo[0]+projection[7]*fTempo[1]+projection[11]*fTempo[2]+projection[15]*fTempo[3];
      //The result normalizes between -1 and 1
      if(fTempo[7]==0.0)        //The w value
         return 0;
      fTempo[7]=1.0/fTempo[7];
      //Perspective division
      fTempo[4]*=fTempo[7];
      fTempo[5]*=fTempo[7];
      fTempo[6]*=fTempo[7];
      //Window coordinates
      //Map x, y to range 0-1
      *winX=(fTempo[4]*0.5+0.5)*viewport[2]+viewport[0];
      *winY=(fTempo[5]*0.5+0.5)*viewport[3]+viewport[1];
      //This is only correct when glDepthRange(0.0, 1.0)
      *winZ=(1.0+fTempo[6])*0.5;        //Between 0 and 1
      return 1;
}

CGAL_INLINE_FUNCTION
void MultiplyMatrices4by4OpenGL_GLdouble(GLdouble *result, GLdouble *matrix1, GLdouble *matrix2)
 {
   result[0]=matrix1[0]*matrix2[0]+
     matrix1[4]*matrix2[1]+
     matrix1[8]*matrix2[2]+
     matrix1[12]*matrix2[3];
   result[4]=matrix1[0]*matrix2[4]+
     matrix1[4]*matrix2[5]+
     matrix1[8]*matrix2[6]+
     matrix1[12]*matrix2[7];
   result[8]=matrix1[0]*matrix2[8]+
     matrix1[4]*matrix2[9]+
     matrix1[8]*matrix2[10]+
     matrix1[12]*matrix2[11];
   result[12]=matrix1[0]*matrix2[12]+
     matrix1[4]*matrix2[13]+
     matrix1[8]*matrix2[14]+
     matrix1[12]*matrix2[15];
   result[1]=matrix1[1]*matrix2[0]+
     matrix1[5]*matrix2[1]+
     matrix1[9]*matrix2[2]+
     matrix1[13]*matrix2[3];
   result[5]=matrix1[1]*matrix2[4]+
     matrix1[5]*matrix2[5]+
     matrix1[9]*matrix2[6]+
     matrix1[13]*matrix2[7];
   result[9]=matrix1[1]*matrix2[8]+
     matrix1[5]*matrix2[9]+
     matrix1[9]*matrix2[10]+
     matrix1[13]*matrix2[11];
   result[13]=matrix1[1]*matrix2[12]+
     matrix1[5]*matrix2[13]+
     matrix1[9]*matrix2[14]+
     matrix1[13]*matrix2[15];
   result[2]=matrix1[2]*matrix2[0]+
     matrix1[6]*matrix2[1]+
     matrix1[10]*matrix2[2]+
     matrix1[14]*matrix2[3];
   result[6]=matrix1[2]*matrix2[4]+
     matrix1[6]*matrix2[5]+
     matrix1[10]*matrix2[6]+
     matrix1[14]*matrix2[7];
   result[10]=matrix1[2]*matrix2[8]+
     matrix1[6]*matrix2[9]+
     matrix1[10]*matrix2[10]+
     matrix1[14]*matrix2[11];
   result[14]=matrix1[2]*matrix2[12]+
     matrix1[6]*matrix2[13]+
     matrix1[10]*matrix2[14]+
     matrix1[14]*matrix2[15];
   result[3]=matrix1[3]*matrix2[0]+
     matrix1[7]*matrix2[1]+
     matrix1[11]*matrix2[2]+
     matrix1[15]*matrix2[3];
   result[7]=matrix1[3]*matrix2[4]+
     matrix1[7]*matrix2[5]+
     matrix1[11]*matrix2[6]+
     matrix1[15]*matrix2[7];
   result[11]=matrix1[3]*matrix2[8]+
     matrix1[7]*matrix2[9]+
     matrix1[11]*matrix2[10]+
     matrix1[15]*matrix2[11];
   result[15]=matrix1[3]*matrix2[12]+
     matrix1[7]*matrix2[13]+
     matrix1[11]*matrix2[14]+
     matrix1[15]*matrix2[15];
 }

CGAL_INLINE_FUNCTION
 void MultiplyMatrixByVector4by4OpenGL_GLdouble(GLdouble *resultvector, const GLdouble *matrix, const GLdouble *pvector)
 {
   resultvector[0]=matrix[0]*pvector[0]+matrix[4]*pvector[1]+matrix[8]*pvector[2]+matrix[12]*pvector[3];
   resultvector[1]=matrix[1]*pvector[0]+matrix[5]*pvector[1]+matrix[9]*pvector[2]+matrix[13]*pvector[3];
   resultvector[2]=matrix[2]*pvector[0]+matrix[6]*pvector[1]+matrix[10]*pvector[2]+matrix[14]*pvector[3];
   resultvector[3]=matrix[3]*pvector[0]+matrix[7]*pvector[1]+matrix[11]*pvector[2]+matrix[15]*pvector[3];
 }

   #define SWAP_ROWS_DOUBLE(a, b) { double *_tmp = a; (a)=(b); (b)=_tmp; }
   #define SWAP_ROWS_GLdouble(a, b) { GLdouble *_tmp = a; (a)=(b); (b)=_tmp; }
   #define MAT(m,r,c) (m)[(c)*4+(r)]
   //This code comes directly from GLU except that it is for GLdouble
 CGAL_INLINE_FUNCTION
 int glhInvertMatrixf2(GLdouble *m, GLdouble *out)
  {
   GLdouble wtmp[4][8];
   GLdouble m0, m1, m2, m3, s;
   GLdouble *r0, *r1, *r2, *r3;
   r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
   r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
      r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
      r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
      r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
      r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
      r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
      r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
      r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
      r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
      r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
      r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
      r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
   /* choose pivot - or die */
   if (fabs(r3[0]) > fabs(r2[0]))
      SWAP_ROWS_GLdouble(r3, r2);
   if (fabs(r2[0]) > fabs(r1[0]))
      SWAP_ROWS_GLdouble(r2, r1);
   if (fabs(r1[0]) > fabs(r0[0]))
      SWAP_ROWS_GLdouble(r1, r0);
   if (0.0 == r0[0])
      return 0;
   /* eliminate first variable     */
   m1 = r1[0] / r0[0];
   m2 = r2[0] / r0[0];
   m3 = r3[0] / r0[0];
   s = r0[1];
   r1[1] -= m1 * s;
   r2[1] -= m2 * s;
   r3[1] -= m3 * s;
   s = r0[2];
   r1[2] -= m1 * s;
   r2[2] -= m2 * s;
   r3[2] -= m3 * s;
   s = r0[3];
   r1[3] -= m1 * s;
   r2[3] -= m2 * s;
   r3[3] -= m3 * s;
   s = r0[4];
   if (s != 0.0) {
      r1[4] -= m1 * s;
      r2[4] -= m2 * s;
      r3[4] -= m3 * s;
   }
   s = r0[5];
   if (s != 0.0) {
      r1[5] -= m1 * s;
      r2[5] -= m2 * s;
      r3[5] -= m3 * s;
   }
   s = r0[6];
   if (s != 0.0) {
      r1[6] -= m1 * s;
      r2[6] -= m2 * s;
      r3[6] -= m3 * s;
   }
   s = r0[7];
   if (s != 0.0) {
      r1[7] -= m1 * s;
      r2[7] -= m2 * s;
      r3[7] -= m3 * s;
   }
   /* choose pivot - or die */
   if (fabs(r3[1]) > fabs(r2[1]))
      SWAP_ROWS_GLdouble(r3, r2);
   if (fabs(r2[1]) > fabs(r1[1]))
      SWAP_ROWS_GLdouble(r2, r1);
   if (0.0 == r1[1])
      return 0;
   /* eliminate second variable */
   m2 = r2[1] / r1[1];
   m3 = r3[1] / r1[1];
   r2[2] -= m2 * r1[2];
   r3[2] -= m3 * r1[2];
   r2[3] -= m2 * r1[3];
   r3[3] -= m3 * r1[3];
   s = r1[4];
   if (0.0 != s) {
      r2[4] -= m2 * s;
      r3[4] -= m3 * s;
   }
   s = r1[5];
   if (0.0 != s) {
      r2[5] -= m2 * s;
      r3[5] -= m3 * s;
   }
   s = r1[6];
   if (0.0 != s) {
      r2[6] -= m2 * s;
      r3[6] -= m3 * s;
   }
   s = r1[7];
   if (0.0 != s) {
      r2[7] -= m2 * s;
      r3[7] -= m3 * s;
   }
   /* choose pivot - or die */
   if (fabs(r3[2]) > fabs(r2[2]))
      SWAP_ROWS_GLdouble(r3, r2);
   if (0.0 == r2[2])
      return 0;
   /* eliminate third variable */
   m3 = r3[2] / r2[2];
   r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
      r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
   /* last check */
   if (0.0 == r3[3])
      return 0;
   s = 1.0 / r3[3];                /* now back substitute row 3 */
   r3[4] *= s;
   r3[5] *= s;
   r3[6] *= s;
   r3[7] *= s;
   m2 = r2[3];                        /* now back substitute row 2 */
   s = 1.0 / r2[2];
   r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
      r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
   m1 = r1[3];
   r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
      r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
   m0 = r0[3];
   r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
      r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
   m1 = r1[2];                        /* now back substitute row 1 */
   s = 1.0 / r1[1];
   r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
      r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
   m0 = r0[2];
   r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
      r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
   m0 = r0[1];                        /* now back substitute row 0 */
   s = 1.0 / r0[0];
   r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
      r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
   MAT(out, 0, 0) = r0[4];
   MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
   MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
   MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
   MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
   MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
   MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
   MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
   MAT(out, 3, 3) = r3[7];
   return 1;
  }

#undef MAT
#undef SWAP_ROWS_GLdouble
#undef SWAP_ROWS_DOUBLE

CGAL_INLINE_FUNCTION
int unProject(GLdouble winx, GLdouble winy, GLdouble winz, GLdouble *modelview, GLdouble *projection, int *viewport,
              GLdouble *objX,GLdouble *objY,GLdouble *objZ)
 {
     //Transformation matrices
     GLdouble m[16], A[16];
     GLdouble in[4], out[4];
     //Calculation for inverting a matrix, compute projection x modelview
     //and store in A[16]
     MultiplyMatrices4by4OpenGL_GLdouble(A, projection, modelview);
     //Now compute the inverse of matrix A
     if(glhInvertMatrixf2(A, m)==0)
        return 0;
     //Transformation of normalized coordinates between -1 and 1
     in[0]=(winx-(GLdouble)viewport[0])/(GLdouble)viewport[2]*2.0-1.0;
     in[1]=(winy-(GLdouble)viewport[1])/(GLdouble)viewport[3]*2.0-1.0;
     in[2]=2.0*winz-1.0;
     in[3]=1.0;
     //Objects coordinates
     MultiplyMatrixByVector4by4OpenGL_GLdouble(out, m, in);
     if(out[3]==0.0)
        return 0;
     out[3]=1.0/out[3];
     *objX=out[0]*out[3];
     *objY=out[1]*out[3];
     *objZ=out[2]*out[3];
     return 1;
 }

/*! Returns the screen projected coordinates of a point \p src defined in the \p frame coordinate
 system.
 When \p frame in \c nullptr (default), \p src is expressed in the world coordinate system.
 The x and y coordinates of the returned Vec are expressed in pixel, (0,0) being the \e upper left
 corner of the window. The z coordinate ranges between 0.0 (near plane) and 1.0 (excluded, far
 plane). See the \c gluProject man page for details.
 unprojectedCoordinatesOf() performs the inverse transformation.
 See the <a href="../examples/screenCoordSystem.html">screenCoordSystem example</a>.
 This method only uses the intrinsic Camera parameters (see getModelViewMatrix(),
 getProjectionMatrix() and getViewport()) and is completely independent of the OpenGL \c
 GL_MODELVIEW, \c GL_PROJECTION and viewport matrices. You can hence define a virtual Camera and use
 this method to compute projections out of a classical rendering context.
 \attention However, if your Camera is not attached to a CGAL::QGLViewer (used for offscreen computations
 for instance), make sure the Camera matrices are updated before calling this method. Call
 computeModelViewMatrix() and computeProjectionMatrix() to do so.
 If you call this method several times with no change in the matrices, consider precomputing the
 projection times modelview matrix to save computation time if required (\c P x \c M in the \c
 gluProject man page).
 Here is the code corresponding to what this method does (kindly submitted by Robert W. Kuhn) :
 \code
 Vec project(Vec point)
 {
    GLint    Viewport[4];
    GLdouble Projection[16], Modelview[16];
    GLdouble matrix[16];
    // Precomputation begin
    glGetIntegerv(GL_VIEWPORT         , Viewport);
    glGetDoublev (GL_MODELVIEW_MATRIX , Modelview);
    glGetDoublev (GL_PROJECTION_MATRIX, Projection);
    for (unsigned short m=0; m<4; ++m)
    {
        for (unsigned short l=0; l<4; ++l)
        {
            qreal sum = 0.0;
            for (unsigned short k=0; k<4; ++k)
                sum += Projection[l+4*k]*Modelview[k+4*m];
            matrix[l+4*m] = sum;
        }
    }
    // Precomputation end
    GLdouble v[4], vs[4];
    v[0]=point[0]; v[1]=point[1]; v[2]=point[2]; v[3]=1.0;
    vs[0]=matrix[0 ]*v[0] + matrix[4 ]*v[1] + matrix[8 ]*v[2] + matrix[12 ]*v[3];
    vs[1]=matrix[1 ]*v[0] + matrix[5 ]*v[1] + matrix[9 ]*v[2] + matrix[13 ]*v[3];
    vs[2]=matrix[2 ]*v[0] + matrix[6 ]*v[1] + matrix[10]*v[2] + matrix[14 ]*v[3];
    vs[3]=matrix[3 ]*v[0] + matrix[7 ]*v[1] + matrix[11]*v[2] + matrix[15 ]*v[3];
    vs[0] /= vs[3];
    vs[1] /= vs[3];
    vs[2] /= vs[3];
    vs[0] = vs[0] * 0.5 + 0.5;
    vs[1] = vs[1] * 0.5 + 0.5;
    vs[2] = vs[2] * 0.5 + 0.5;
    vs[0] = vs[0] * Viewport[2] + Viewport[0];
    vs[1] = vs[1] * Viewport[3] + Viewport[1];
    return Vec(vs[0], Viewport[3]-vs[1], vs[2]);
  }
 \endcode
 */
CGAL_INLINE_FUNCTION
Vec Camera::projectedCoordinatesOf(const Vec& src, const Frame* frame) const
{
    GLdouble x = 0.f, y = 0.f, z = 0.f;
    static GLint viewport[4];
    getViewport(viewport);

    if (frame)
    {
        const Vec tmp = frame->inverseCoordinatesOf(src);
        project(tmp.x,tmp.y,tmp.z, modelViewMatrix_, projectionMatrix_, viewport,  &x,&y,&z);
    }
    else
        project(src.x,src.y,src.z, modelViewMatrix_, projectionMatrix_, viewport,  &x,&y,&z);

    return Vec(x,y,z);
}

/*! Returns the world unprojected coordinates of a point \p src defined in the screen coordinate
 system.
 The \p src.x and \p src.y input values are expressed in pixels, (0,0) being the \e upper left corner
 of the window. \p src.z is a depth value ranging in [0..1[ (respectively corresponding to the near
 and far planes). Note that src.z is \e not a linear interpolation between zNear and zFar.
 /code
 src.z = zFar() / (zFar() - zNear()) * (1.0 - zNear() / z);
 /endcode
 Where z is the distance from the point you project to the camera, along the viewDirection().
 See the \c gluUnProject man page for details.
 The result is expressed in the \p frame coordinate system. When \p frame is \c nullptr (default), the
 result is expressed in the world coordinates system. The possible \p frame Frame::referenceFrame()
 are taken into account.
 projectedCoordinatesOf() performs the inverse transformation.
 This method only uses the intrinsic Camera parameters (see getModelViewMatrix(),
 getProjectionMatrix() and getViewport()) and is completely independent of the OpenGL \c
 GL_MODELVIEW, \c GL_PROJECTION and viewport matrices. You can hence define a virtual Camera and use
 this method to compute un-projections out of a classical rendering context.
 \attention However, if your Camera is not attached to a CGAL::QGLViewer (used for offscreen computations
 for instance), make sure the Camera matrices are updated before calling this method (use
 computeModelViewMatrix(), computeProjectionMatrix()). See also setScreenWidthAndHeight().
 This method is not computationally optimized. If you call it several times with no change in the
 matrices, you should buffer the entire inverse projection matrix (modelview, projection and then
 viewport) to speed-up the queries. See the \c gluUnProject man page for details. */
CGAL_INLINE_FUNCTION
Vec Camera::unprojectedCoordinatesOf(const Vec& src, const Frame* frame) const
{
    GLdouble x = 0.f, y = 0.f, z = 0.f;
    static GLint viewport[4];
    getViewport(viewport);
    unProject(src.x,src.y,src.z, modelViewMatrix_,  projectionMatrix_,  viewport,  &x,&y,&z);
    if (frame)
        return frame->coordinatesOf(Vec(x,y,z));
    else
        return Vec(x,y,z);
}

/*! Same as projectedCoordinatesOf(), but with \c qreal parameters (\p src and
 * \p res can be identical pointers). */
CGAL_INLINE_FUNCTION
void Camera::getProjectedCoordinatesOf(const qreal src[3], qreal res[3],
                                       const Frame *frame) const {
  Vec r = projectedCoordinatesOf(Vec(src), frame);
  for (int i = 0; i < 3; ++i)
    res[i] = r[i];
}

/*! Same as unprojectedCoordinatesOf(), but with \c qreal parameters (\p src and
 * \p res can be identical pointers). */
CGAL_INLINE_FUNCTION
void Camera::getUnprojectedCoordinatesOf(const qreal src[3], qreal res[3],
                                         const Frame *frame) const {
  Vec r = unprojectedCoordinatesOf(Vec(src), frame);
  for (int i = 0; i < 3; ++i)
    res[i] = r[i];
}

/////////////////////////////////////  KFI
////////////////////////////////////////////

/*! Returns the KeyFrameInterpolator that defines the Camera path number \p i.

If path \p i is not defined for this index, the method returns a \c nullptr
pointer. */
CGAL_INLINE_FUNCTION
KeyFrameInterpolator *Camera::keyFrameInterpolator(unsigned int i) const {
  if (kfi_.contains(i))
    return kfi_[i];
  else
    return nullptr;
}

/*! Sets the KeyFrameInterpolator that defines the Camera path of index \p i.

 The previous keyFrameInterpolator() is lost and should be deleted by the
 calling method if needed.

 The KeyFrameInterpolator::interpolated() signal of \p kfi probably needs to be
 connected to the Camera's associated CGAL::QGLViewer::update() slot, so that when the
 Camera position is interpolated using \p kfi, every interpolation step updates
 the display: \code myViewer.camera()->deletePath(3);
 myViewer.camera()->setKeyFrameInterpolator(3, myKeyFrameInterpolator);
 connect(myKeyFrameInterpolator, SIGNAL(interpolated()), myViewer,
 SLOT(update()); \endcode

 \note These connections are done automatically when a Camera is attached to a
 CGAL::QGLViewer, or when a new KeyFrameInterpolator is defined using the
 CGAL::QGLViewer::addKeyFrameKeyboardModifiers() and CGAL::QGLViewer::pathKey() (default is
 Alt+F[1-12]). See the <a href="../keyboard.html">keyboard page</a> for details.
 */
CGAL_INLINE_FUNCTION
void Camera::setKeyFrameInterpolator(unsigned int i,
                                     KeyFrameInterpolator *const kfi) {
  if (kfi)
    kfi_[i] = kfi;
  else
    kfi_.remove(i);
}

/*! Adds the current Camera position() and orientation() as a keyFrame to the
path number \p i.

This method can also be used if you simply want to save a Camera point of view
(a path made of a single keyFrame). Use playPath() to make the Camera play the
keyFrame path (resp. restore the point of view). Use deletePath() to clear the
path.

The default keyboard shortcut for this method is Alt+F[1-12]. Set
CGAL::QGLViewer::pathKey() and CGAL::QGLViewer::addKeyFrameKeyboardModifiers().

If you use directly this method and the keyFrameInterpolator(i) does not exist,
a new one is created. Its KeyFrameInterpolator::interpolated() signal should
then be connected to the CGAL::QGLViewer::update() slot (see
setKeyFrameInterpolator()). */
CGAL_INLINE_FUNCTION
void Camera::addKeyFrameToPath(unsigned int i) {
  if (!kfi_.contains(i))
    setKeyFrameInterpolator(i, new KeyFrameInterpolator(frame()));

  kfi_[i]->addKeyFrame(*(frame()));
}

/*! Makes the Camera follow the path of keyFrameInterpolator() number \p i.

 If the interpolation is started, it stops it instead.

 This method silently ignores undefined (empty) paths (see
 keyFrameInterpolator()).

 The default keyboard shortcut for this method is F[1-12]. Set
 CGAL::QGLViewer::pathKey() and CGAL::QGLViewer::playPathKeyboardModifiers(). */
CGAL_INLINE_FUNCTION
void Camera::playPath(unsigned int i) {
  if (kfi_.contains(i)) {
    if (kfi_[i]->interpolationIsStarted())
      kfi_[i]->stopInterpolation();
    else
      kfi_[i]->startInterpolation();
  }
}

/*! Resets the path of the keyFrameInterpolator() number \p i.

If this path is \e not being played (see playPath() and
KeyFrameInterpolator::interpolationIsStarted()), resets it to its starting
position (see KeyFrameInterpolator::resetInterpolation()). If the path is
played, simply stops interpolation. */
CGAL_INLINE_FUNCTION
void Camera::resetPath(unsigned int i) {
  if (kfi_.contains(i)) {
    if ((kfi_[i]->interpolationIsStarted()))
      kfi_[i]->stopInterpolation();
    else {
      kfi_[i]->resetInterpolation();
      kfi_[i]->interpolateAtTime(kfi_[i]->interpolationTime());
    }
  }
}

/*! Deletes the keyFrameInterpolator() of index \p i.

Disconnect the keyFrameInterpolator() KeyFrameInterpolator::interpolated()
signal before deleting the keyFrameInterpolator() if needed: \code
disconnect(camera()->keyFrameInterpolator(i), SIGNAL(interpolated()), this,
SLOT(update())); camera()->deletePath(i); \endcode */
CGAL_INLINE_FUNCTION
void Camera::deletePath(unsigned int i) {
  if (kfi_.contains(i)) {
    kfi_[i]->stopInterpolation();
    delete kfi_[i];
    kfi_.remove(i);
  }
}


////////////////////////////////////////////////////////////////////////////////

/*! Gives the coefficients of a 3D half-line passing through the Camera eye and
 pixel (x,y).

 The origin of the half line (eye position) is stored in \p orig, while \p dir
 contains the properly oriented and normalized direction of the half line.

 \p x and \p y are expressed in Qt format (origin in the upper left corner). Use
 screenHeight() - y to convert to OpenGL units.

 This method is useful for analytical intersection in a selection method.

 See the <a href="../examples/select.html">select example</a> for an
 illustration. */
CGAL_INLINE_FUNCTION
void Camera::convertClickToLine(const QPoint &pixel, Vec &orig,
                                Vec &dir) const {
  switch (type()) {
  case Camera::PERSPECTIVE:
    orig = position();
    dir = Vec(((2.0 * pixel.x() / screenWidth()) - 1.0) *
                  tan(fieldOfView() / 2.0) * aspectRatio(),
              ((2.0 * (screenHeight() - pixel.y()) / screenHeight()) - 1.0) *
                  tan(fieldOfView() / 2.0),
              -1.0);
    dir = worldCoordinatesOf(dir) - orig;
    dir.normalize();
    break;

  case Camera::ORTHOGRAPHIC: {
    GLdouble w, h;
    getOrthoWidthHeight(w, h);
    orig = Vec((2.0 * pixel.x() / screenWidth() - 1.0) * w,
               -(2.0 * pixel.y() / screenHeight() - 1.0) * h, 0.0);
    orig = worldCoordinatesOf(orig);
    dir = viewDirection();
    break;
  }
  }
}


/*! Returns the 6 plane equations of the Camera frustum.

The six 4-component vectors of \p coef respectively correspond to the left,
right, near, far, top and bottom Camera frustum planes. Each vector holds a
plane equation of the form: \code a*x + b*y + c*z + d = 0 \endcode where \c a,
\c b, \c c and \c d are the 4 components of each vector, in that order.

See the <a href="../examples/frustumCulling.html">frustumCulling example</a> for
an application.

This format is compatible with the \c glClipPlane() function. One camera frustum
plane can hence be applied in an other viewer to visualize the culling results:
\code
 // Retrieve plane equations
 GLdouble coef[6][4];
 mainViewer->camera()->getFrustumPlanesCoefficients(coef);

 // These two additional clipping planes (which must have been enabled)
 // will reproduce the mainViewer's near and far clipping.
 glClipPlane(GL_CLIP_PLANE0, coef[2]);
 glClipPlane(GL_CLIP_PLANE1, coef[3]);
\endcode */
CGAL_INLINE_FUNCTION
void Camera::getFrustumPlanesCoefficients(GLdouble coef[6][4]) const {
  // Computed once and for all
  const Vec pos = position();
  const Vec viewDir = viewDirection();
  const Vec up = upVector();
  const Vec right = rightVector();
  const qreal posViewDir = pos * viewDir;

  static Vec normal[6];
  static GLdouble dist[6];

  switch (type()) {
  case Camera::PERSPECTIVE: {
    const qreal hhfov = horizontalFieldOfView() / 2.0;
    const qreal chhfov = cos(hhfov);
    const qreal shhfov = sin(hhfov);
    normal[0] = -shhfov * viewDir;
    normal[1] = normal[0] + chhfov * right;
    normal[0] = normal[0] - chhfov * right;

    normal[2] = -viewDir;
    normal[3] = viewDir;

    const qreal hfov = fieldOfView() / 2.0;
    const qreal chfov = cos(hfov);
    const qreal shfov = sin(hfov);
    normal[4] = -shfov * viewDir;
    normal[5] = normal[4] - chfov * up;
    normal[4] = normal[4] + chfov * up;

    for (int i = 0; i < 2; ++i)
      dist[i] = pos * normal[i];
    for (int j = 4; j < 6; ++j)
      dist[j] = pos * normal[j];

    // Natural equations are:
    // dist[0,1,4,5] = pos * normal[0,1,4,5];
    // dist[2] = (pos + zNear() * viewDir) * normal[2];
    // dist[3] = (pos + zFar()  * viewDir) * normal[3];

    // 2 times less computations using expanded/merged equations. Dir vectors
    // are normalized.
    const qreal posRightCosHH = chhfov * pos * right;
    dist[0] = -shhfov * posViewDir;
    dist[1] = dist[0] + posRightCosHH;
    dist[0] = dist[0] - posRightCosHH;
    const qreal posUpCosH = chfov * pos * up;
    dist[4] = -shfov * posViewDir;
    dist[5] = dist[4] - posUpCosH;
    dist[4] = dist[4] + posUpCosH;

    break;
  }
  case Camera::ORTHOGRAPHIC:
    normal[0] = -right;
    normal[1] = right;
    normal[4] = up;
    normal[5] = -up;

    GLdouble hw, hh;
    getOrthoWidthHeight(hw, hh);
    dist[0] = (pos - hw * right) * normal[0];
    dist[1] = (pos + hw * right) * normal[1];
    dist[4] = (pos + hh * up) * normal[4];
    dist[5] = (pos - hh * up) * normal[5];
    break;
  }

  // Front and far planes are identical for both camera types.
  normal[2] = -viewDir;
  normal[3] = viewDir;
  dist[2] = -posViewDir - zNear();
  dist[3] = posViewDir + zFar();

  for (int i = 0; i < 6; ++i) {
    coef[i][0] = GLdouble(normal[i].x);
    coef[i][1] = GLdouble(normal[i].y);
    coef[i][2] = GLdouble(normal[i].z);
    coef[i][3] = dist[i];
  }
}

CGAL_INLINE_FUNCTION
void Camera::onFrameModified() {
  projectionMatrixIsUpToDate_ = false;
  modelViewMatrixIsUpToDate_ = false;
}

CGAL_INLINE_FUNCTION
void Camera::setHorizontalFieldOfView(qreal hfov) {
  setFieldOfView(2.0 * atan(tan(hfov / 2.0) / aspectRatio()));
}

CGAL_INLINE_FUNCTION
qreal Camera::horizontalFieldOfView() const {
  return 2.0 * atan(tan(fieldOfView() / 2.0) * aspectRatio());
}



CGAL_INLINE_FUNCTION
void Camera::setFrustum(double frustum[6])
{
  double l(frustum[0]),r(frustum[1]),t(frustum[2]),
      b(frustum[3]),n(frustum[4]),f(frustum[5]);
  if(type() == PERSPECTIVE)
  {
    double A = 2*n/(r-l);
    double B = (r+l)/(r-l);
    double C = 2*n/(t-b);
    double D = (t+b)/(t-b);
    double E = -(f+n)/(f-n);
    double F = -2*(f*n)/(f-n);
    projectionMatrix_[0] = A; projectionMatrix_[4] = 0; projectionMatrix_[8] = B ; projectionMatrix_[12] = 0;
    projectionMatrix_[1] = 0; projectionMatrix_[5] = C; projectionMatrix_[9] = D ; projectionMatrix_[13] = 0;
    projectionMatrix_[2] = 0; projectionMatrix_[6] = 0; projectionMatrix_[10] = E ; projectionMatrix_[14] = F;
    projectionMatrix_[3] =0; projectionMatrix_[7] =0; projectionMatrix_[11] =-1; projectionMatrix_[15] =0;
  }
  else
  {
    double A = 2/(r-l);
    double B = -(r+l)/(r-l);
    double C = 2/(t-b);
    double D = -(t+b)/(t-b);
    double E = -(f+n)/(f-n);
    double F = -2/(f-n);
    projectionMatrix_[0] = A; projectionMatrix_[1] = 0; projectionMatrix_[2] = 0 ; projectionMatrix_[3] = 0;
    projectionMatrix_[4] = 0; projectionMatrix_[5] = C; projectionMatrix_[6] = 0 ; projectionMatrix_[7] = 0;
    projectionMatrix_[8] = 0; projectionMatrix_[9] = 0; projectionMatrix_[10] = F ; projectionMatrix_[11] = 0;
    projectionMatrix_[12] = B; projectionMatrix_[13] = D; projectionMatrix_[14] = E; projectionMatrix_[15] = 1;
  }
  projectionMatrixIsUpToDate_ = true;
}

CGAL_INLINE_FUNCTION
void Camera::getFrustum(double frustum[6])
{
  double l,r,t,b,n,f;
  if(type() == PERSPECTIVE)
  {
  n = projectionMatrix_[14]/2*((projectionMatrix_[10]+1)/(projectionMatrix_[10]-1)-1);
  f = n*(projectionMatrix_[10]-1)/(projectionMatrix_[10]+1);
  l = ((2*n/projectionMatrix_[0])*(projectionMatrix_[8]-1)/(projectionMatrix_[8]+1))/(1-(projectionMatrix_[8]-1)/(projectionMatrix_[8]+1));
  r = 2*n/projectionMatrix_[0]+l;
  b=(-2*n/projectionMatrix_[5]*(1-projectionMatrix_[9])/(1+projectionMatrix_[9]))/(1+(1-projectionMatrix_[9])/(1+projectionMatrix_[9]));
  t = 2*n/projectionMatrix_[5]+b;
  }
  else
  {
    double A(projectionMatrix_[0]),B(projectionMatrix_[12]),
        C(projectionMatrix_[5]),D(projectionMatrix_[13]),
        E(projectionMatrix_[14]),F(projectionMatrix_[10]);
    double B1 = (B+1)/(1-B), D1 = (1-D)/(D+1),
        E1=(E+1)/(1-E);

    l = -2*B1/(1+B1*A);
    r = 2+A*l;
    t = 2*D1/(C*(1+D1));
    b =t -2/C;
    n = -2/(F*(1+E1));
    f=n-2/F;

  }
  frustum[0] = l;
  frustum[1] = r;
  frustum[2] = t;
  frustum[3] = b;
  frustum[4] = n;
  frustum[5] = f;
}
}}//end of namespace