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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
 *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include <Inventor/SbViewportRegion.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/draggers/SoDragger.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCamera.h>
#include <Inventor/nodes/SoOrthographicCamera.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/projectors/SbSphereSheetProjector.h>
#include <QAction>
#include <QActionGroup>
#include <QApplication>
#include <QByteArray>
#include <QCursor>
#include <QMenu>


#include <cmath>
#include <limits>

#include <Base/Interpreter.h>
#include <App/Application.h>

#include "Navigation/NavigationStyle.h"
#include "Navigation/NavigationStylePy.h"
#include "Application.h"
#include "Command.h"
#include "Action.h"
#include "Inventor/SoMouseWheelEvent.h"
#include "MenuManager.h"
#include "MouseSelection.h"
#include "Navigation/NavigationAnimator.h"
#include "Navigation/NavigationAnimation.h"
#include "View3DInventorViewer.h"

using namespace Gui;

class FCSphereSheetProjector: public SbSphereSheetProjector
{
    using inherited = SbSphereSheetProjector;

public:
    enum OrbitStyle
    {
        Turntable,
        Trackball,
        FreeTurntable,
        TrackballClassic,
        RoundedArcball
    };

    static constexpr float defaultSphereRadius = 0.8F;

    FCSphereSheetProjector(const SbSphere& sph, const SbBool orienttoeye = true)
        : SbSphereSheetProjector(sph, orienttoeye)
    {}

    void setViewVolume(const SbViewVolume& vol) override
    {
        inherited::setViewVolume(vol);
    }

    void setWorkingSpace(const SbMatrix& space) override
    {
        // inherited::setWorkingSpace(space);
        this->worldToScreen = space.inverse();
    }

    SbVec3f project(const SbVec2f& point) override
    {
        if (orbit != RoundedArcball) {
            return inherited::project(point);
        }

        // Rounded Arcball implementation
        // based on SbSphereSheetProjector in Open Inventor
        SbVec3f result;
        SbLine workingLine = getWorkingLine(point);

        if (needSetup) {
            setupPlane();
        }

        SbVec3f planeIntersection;

        SbVec3f sphereIntersection, dontCare;
        SbBool hitSphere;
        if (intersectFront == TRUE) {
            hitSphere = sphere.intersect(workingLine, sphereIntersection, dontCare);
        }
        else {
            hitSphere = sphere.intersect(workingLine, dontCare, sphereIntersection);
        }

        if (hitSphere) {
            // drop the sphere intersection onto the tolerance plane

            SbLine projectLine(sphereIntersection, sphereIntersection + planeDir);
            if (!tolPlane.intersect(projectLine, planeIntersection)) {
#ifdef FC_DEBUG
                SoDebugError::post(
                    "SbSphereSheetProjector::project",
                    "Could not intersect working line with plane"
                );
#endif
            }
        }
        else if (!tolPlane.intersect(workingLine, planeIntersection)) {
#ifdef FC_DEBUG
            SoDebugError::post("SbSphereSheetProjector::project", "Could not intersect with plane");
#endif
        }

        // Three possibilities:
        // (1) Intersection is on the sphere inside where the fillet
        //	    hits it
        // (2) Intersection is on the fillet
        // (3) Intersection is on the plane
        float distance = (planeIntersection - planePoint).length();

        // Amount of filleting
        // 0 = no fillet (just sphere and plane)
        // infinity = only fillet
        float border = 0.5;

        // Radius where the fillet meets the plane in "squished space"
        float r_a = 1.0F + border;
        // Radius where the sphere meets the fillet in "squished space"
        float r_i = 2.0 / (r_a + 1.0 / r_a);

        // Distance squared in "squished space"
        float d_2 = (distance * distance) * r_a * r_a;
        // Distance in "squished space"
        float d = std::sqrt(d_2);

        // Compute how far off the plane we are
        float offsetDist = 0.0;

        if (d > r_a) {
            // On the plane
            offsetDist = 0.0;
        }
        else if (d < r_i) {
            // On the sphere inside the fillet
            offsetDist = std::sqrt(1.0 - d_2);
        }
        else {
            // On the fillet
            float d_r = r_a - d;
            float a = border * (1.0 + border / 2.0);
            offsetDist = a - std::sqrt((a + d_r) * (a - d_r));
        }

        SbVec3f offset;
        if (orientToEye) {
            if (viewVol.getProjectionType() == SbViewVolume::PERSPECTIVE) {
                offset = workingProjPoint - planeIntersection;
            }
            else {
                worldToWorking.multDirMatrix(viewVol.zVector(), offset);
            }

            offset.normalize();
        }
        else {
            offset.setValue(0, 0, 1);
        }
        if (intersectFront == FALSE) {
            offset *= -1.0;
        }

        offset *= offsetDist;
        result = planeIntersection + offset;

        lastPoint = result;
        return result;
    }

    SbRotation getRotation(const SbVec3f& point1, const SbVec3f& point2) override
    {
        SbRotation rot = inherited::getRotation(point1, point2);
        if (orbit == Turntable || orbit == FreeTurntable) {
            return getTurntable(rot, point1, point2);
        }
        if (orbit == TrackballClassic) {
            return getTrackballClassic(point1, point2);
        }

        return rot;
    }

    void setOrbitStyle(OrbitStyle style)
    {
        this->orbit = style;
    }

    OrbitStyle getOrbitStyle() const
    {
        return this->orbit;
    }

private:
    SbRotation getTurntable(SbRotation rot, const SbVec3f& point1, const SbVec3f& point2) const
    {
        // 0000333: Turntable camera rotation
        SbVec3f axis;
        float angle {};
        rot.getValue(axis, angle);
        SbVec3f dif = point1 - point2;
        if (fabs(dif[1]) > fabs(dif[0])) {
            SbVec3f xaxis(1, 0, 0);
            if (dif[1] < 0) {
                angle = -angle;
            }
            rot.setValue(xaxis, angle);
        }
        else {
            SbVec3f zaxis(0, 0, 1);
            this->worldToScreen.multDirMatrix(zaxis, zaxis);
            if (zaxis[1] < 0) {
                if (dif[0] < 0) {
                    angle = -angle;
                }
            }
            else {
                if (dif[0] > 0) {
                    angle = -angle;
                }
            }
            rot.setValue(zaxis, angle);
        }

        return rot;
    }

    SbRotation getTrackballClassic(const SbVec3f& point1, const SbVec3f& point2) const
    {
        // Classic trackball
        SbRotation zrot;
        SbRotation yrot;
        SbVec3f dif = point1 - point2;

        SbVec3f zaxis(1, 0, 0);
        zrot.setValue(zaxis, dif[1]);

        SbVec3f yaxis(0, 1, 0);
        yrot.setValue(yaxis, -dif[0]);

        return zrot * yrot;
    }

private:
    SbMatrix worldToScreen;
    OrbitStyle orbit {RoundedArcball};
};

NavigationStyleEvent::NavigationStyleEvent(const Base::Type& s)
    : QEvent(QEvent::User)
    , t(s)
{}

NavigationStyleEvent::~NavigationStyleEvent() = default;

const Base::Type& NavigationStyleEvent::style() const
{
    return t;
}

TYPESYSTEM_SOURCE_ABSTRACT(Gui::NavigationStyle, Base::BaseClass)

NavigationStyle::NavigationStyle()
    : viewer(nullptr)
    , mouseSelection(nullptr)
    , pythonObject(nullptr)
{
    this->rotationCenterMode = NavigationStyle::RotationCenterMode::ScenePointAtCursor
        | NavigationStyle::RotationCenterMode::FocalPointAtCursor;
    initialize();
}

NavigationStyle::~NavigationStyle()
{
    finalize();
    delete this->animator;

    if (!pythonObject.is(nullptr)) {
        Base::PyGILStateLocker lock;
        Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(pythonObject.ptr());
        obj->setInvalid();
    }
}

NavigationStyle& NavigationStyle::operator=(const NavigationStyle& ns)
{
    this->panningplane = ns.panningplane;
    this->menuenabled = ns.menuenabled;
    this->animationEnabled = ns.animationEnabled;
    this->spinningAnimationEnabled = ns.spinningAnimationEnabled;
    static_cast<FCSphereSheetProjector*>(this->spinprojector)
        ->setOrbitStyle(static_cast<FCSphereSheetProjector*>(ns.spinprojector)->getOrbitStyle());
    return *this;
}

void NavigationStyle::setViewer(View3DInventorViewer* view)
{
    this->viewer = view;
}

void NavigationStyle::initialize()
{
    this->animator = new NavigationAnimator();

    this->sensitivity = 2.0f;
    this->resetcursorpos = false;
    this->currentmode = NavigationStyle::IDLE;
    this->animationEnabled = true;
    this->spinningAnimationEnabled = false;
    this->spinsamplecounter = 0;
    this->spinincrement = SbRotation::identity();
    this->rotationCenterFound = false;
    this->rotationCenterIsScenePointAtCursor = false;

    // FIXME: use a smaller sphere than the default one to have a larger
    // area close to the borders that gives us "z-axis rotation"?
    // 19990425 mortene.
    this->spinprojector = new FCSphereSheetProjector(
        SbSphere(SbVec3f(0, 0, 0), FCSphereSheetProjector::defaultSphereRadius)
    );
    SbViewVolume volume;
    volume.ortho(-1, 1, -1, 1, -1, 1);
    this->spinprojector->setViewVolume(volume);

    this->log.size = 16;
    this->log.position = new SbVec2s[16];
    this->log.time = new SbTime[16];
    this->log.historysize = 0;

    this->menuenabled = true;
    this->button1down = false;
    this->button2down = false;
    this->button3down = false;
    this->ctrldown = false;
    this->shiftdown = false;
    this->altdown = false;
    this->invertZoom = App::GetApplication()
                           .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
                           ->GetBool("InvertZoom", true);
    this->zoomAtCursor = App::GetApplication()
                             .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
                             ->GetBool("ZoomAtCursor", true);
    this->zoomStep = App::GetApplication()
                         .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
                         ->GetFloat("ZoomStep", 0.2f);
    long mode = App::GetApplication()
                    .GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
                    ->GetInt("RotationMode", 0);
    if (mode == 0) {
        setRotationCenterMode(NavigationStyle::RotationCenterMode::WindowCenter);
    }
    else if (mode == 1) {
        setRotationCenterMode(
            NavigationStyle::RotationCenterMode::ScenePointAtCursor
            | NavigationStyle::RotationCenterMode::FocalPointAtCursor
        );
    }
    else if (mode == 2) {
        setRotationCenterMode(
            NavigationStyle::RotationCenterMode::ScenePointAtCursor
            | NavigationStyle::RotationCenterMode::BoundingBoxCenter
        );
    }

    this->hasDragged = false;
    this->hasPanned = false;
    this->hasZoomed = false;
}

void NavigationStyle::finalize()
{
    delete this->spinprojector;
    delete[] this->log.position;
    delete[] this->log.time;
}

void NavigationStyle::interactiveCountInc()
{
    viewer->interactiveCountInc();
}

void NavigationStyle::interactiveCountDec()
{
    viewer->interactiveCountDec();
}

int NavigationStyle::getInteractiveCount() const
{
    return viewer->getInteractiveCount();
}

void NavigationStyle::setOrbitStyle(NavigationStyle::OrbitStyle style)
{
    auto projector = static_cast<FCSphereSheetProjector*>(this->spinprojector);
    projector->setOrbitStyle(FCSphereSheetProjector::OrbitStyle(style));
}

NavigationStyle::OrbitStyle NavigationStyle::getOrbitStyle() const
{
    auto projector = static_cast<FCSphereSheetProjector*>(this->spinprojector);
    return NavigationStyle::OrbitStyle(projector->getOrbitStyle());
}

SbBool NavigationStyle::isViewing() const
{
    return viewer->isViewing();
}

void NavigationStyle::setViewing(SbBool enable)
{
    viewer->setViewing(enable);
}

SbBool NavigationStyle::isSeekMode() const
{
    return viewer->isSeekMode();
}

void NavigationStyle::setSeekMode(SbBool enable)
{
    viewer->setSeekMode(enable);
}

SbBool NavigationStyle::seekToPoint(const SbVec2s screenpos)
{
    return viewer->seekToPoint(screenpos);
}

void NavigationStyle::seekToPoint(const SbVec3f& scenepos)
{
    viewer->seekToPoint(scenepos);
}

void NavigationStyle::lookAtPoint(const SbVec2s screenpos)
{
    const SoCamera* camera = viewer->getCamera();
    if (!camera) {
        return;
    }

    SoRayPickAction rpaction(viewer->getViewportRegion());
    rpaction.setPoint(screenpos);
    rpaction.setRadius(viewer->getPickRadius());
    rpaction.apply(viewer->getSoRenderManager()->getSceneGraph());

    const SoPickedPoint* picked = rpaction.getPickedPoint();

    // Point is either the hitpoint or the projected point on the panning plane
    SbVec3f point;
    if (picked) {
        point = picked->getPoint();
    }
    else {
        const SbViewportRegion& vp = viewer->getViewportRegion();
        const float aspectratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(aspectratio);

        // See note in Coin docs for SoCamera::getViewVolume re:viewport mapping
        if (aspectratio < 1.0) {
            vv.scale(1.0 / aspectratio);
        }

        SbLine line;
        vv.projectPointToLine(normalizePixelPos(screenpos), line);
        panningplane.intersect(line, point);
    }

    lookAtPoint(point);
}

void NavigationStyle::lookAtPoint(const SbVec3f& position)
{
    this->rotationCenterFound = false;
    translateCamera(position - getFocalPoint());
}

SoCamera* NavigationStyle::getCamera() const
{
    return this->viewer->getCamera();
}

std::shared_ptr<NavigationAnimation> NavigationStyle::setCameraOrientation(
    const SbRotation& orientation,
    const SbBool moveToCenter
) const
{
    SoCamera* camera = getCamera();
    if (!camera) {
        return {};
    }

    animator->stop();

    const SbVec3f focalPoint = getFocalPoint();
    SbVec3f translation(0, 0, 0);

    if (moveToCenter) {
        SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
        action.apply(viewer->getSceneGraph());
        SbBox3f box = action.getBoundingBox();
        if (!box.isEmpty()) {
            translation = box.getCenter() - focalPoint;
        }
    }

    // Start an animation and return it
    if (isAnimationEnabled()) {
        return viewer->startAnimation(orientation, focalPoint, translation);
    }

    // or set the pose directly

    // Distance from rotation center to camera position in camera coordinate system
    const SbVec3f rotationCenterDistanceCam = camera->focalDistance.getValue() * SbVec3f(0, 0, 1);

    // Set to the given orientation
    camera->orientation = orientation;

    // Distance from rotation center to new camera position in global coordinate system
    SbVec3f newRotationCenterDistance;
    camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance);

    // Reposition camera so the rotation center stays in the same place
    // Optionally add translation to move to center
    camera->position = focalPoint + newRotationCenterDistance + translation;

    return {};
}

std::shared_ptr<NavigationAnimation> NavigationStyle::translateCamera(const SbVec3f& translation) const
{
    SoCamera* camera = getCamera();
    if (!camera) {
        return {};
    }

    animator->stop();

    // Start an animation and return it
    if (isAnimationEnabled()) {
        return viewer->startAnimation(camera->orientation.getValue(), SbVec3f(0, 0, 0), translation);
    }

    // or set the pose directly

    camera->position = camera->position.getValue() + translation;

    return {};
}

void NavigationStyle::boxZoom(const SbBox2s& box)
{
    SoCamera* cam = viewer->getSoRenderManager()->getCamera();
    if (!cam) {  // no camera
        return;
    }
    const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
    SbViewVolume vv = cam->getViewVolume(vp.getViewportAspectRatio());

    short sizeX {}, sizeY {};
    box.getSize(sizeX, sizeY);
    SbVec2s size = vp.getViewportSizePixels();

    // The bbox must not be empty i.e. width and length is zero, but it is possible that
    // either width or length is zero
    if (sizeX == 0 && sizeY == 0) {
        return;
    }

    // Get the new center in normalized pixel coordinates
    short xmin {}, xmax {}, ymin {}, ymax {};
    box.getBounds(xmin, ymin, xmax, ymax);
    const SbVec2f center(
        (float)((xmin + xmax) / 2) / (float)std::max((int)(size[0] - 1), 1),
        (float)(size[1] - (ymin + ymax) / 2) / (float)std::max((int)(size[1] - 1), 1)
    );

    SbPlane plane = vv.getPlane(cam->focalDistance.getValue());
    panCamera(cam, vp.getViewportAspectRatio(), plane, SbVec2f(0.5, 0.5), center);

    // Set height or height angle of the camera
    float scaleX = (float)sizeX / (float)size[0];
    float scaleY = (float)sizeY / (float)size[1];
    float scaleFactor = std::max<float>(scaleX, scaleY);

    doScale(cam, scaleFactor);
}
void NavigationStyle::scale(float factor)
{
    SoCamera* cam = viewer->getSoRenderManager()->getCamera();
    if (!cam) {  // no camera
        return;
    }

    // Find the current center of the screen
    SbVec3f direction;
    cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
    SbVec3f initCenter = cam->position.getValue() + cam->focalDistance.getValue() * direction;

    // Move the camera to the origin for scaling
    cam->position = cam->position.getValue() - initCenter;

    // Scale the view
    doScale(cam, factor);

    // Move the camera back to it's initial position scaled
    cam->position = cam->position.getValue() + initCenter * factor;
}

void NavigationStyle::viewAll()
{
    // Get the bounding box of the scene
    SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
    action.apply(viewer->getSceneGraph());
    SbBox3f box = action.getBoundingBox();
    if (box.isEmpty()) {
        return;
    }


    SoCamera* cam = viewer->getSoRenderManager()->getCamera();
    if (!cam) {
        return;
    }

    SbViewVolume vol = cam->getViewVolume();
    if (vol.ulf == vol.llf) {
        return;  // empty frustum (no view up vector defined)
    }
    SbVec2f s = vol.projectBox(box);
    SbVec2s size = viewer->getSoRenderManager()->getSize();

    SbVec3f pt1, pt2, pt3, tmp;
    vol.projectPointToLine(SbVec2f(0.0f, 0.0f), pt1, tmp);
    vol.projectPointToLine(SbVec2f(s[0], 0.0f), pt2, tmp);
    vol.projectPointToLine(SbVec2f(0.0f, s[1]), pt3, tmp);

    float cam_width = (pt2 - pt1).length();
    float cam_height = (pt3 - pt1).length();

    // add a small border
    cam_height = 1.08f * std::max<float>((cam_width * (float)size[1]) / (float)size[0], cam_height);

    float aspect = cam->aspectRatio.getValue();

    if (cam->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
        auto ocam = static_cast<SoOrthographicCamera*>(cam);
        if (aspect < 1.0f) {
            ocam->height = cam_height / aspect;
        }
        else {
            ocam->height = cam_height;
        }
    }
}

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
void NavigationStyle::findBoundingSphere()
{
    // Find a bounding sphere for the scene
    SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
    action.apply(viewer->getSceneGraph());
    boundingSphere.circumscribe(action.getBoundingBox());
}
#endif

/** Rotate the camera by the given amount, then reposition it so we're still pointing at the same
 * focal point
 */
void NavigationStyle::reorientCamera(SoCamera* camera, const SbRotation& rotation)
{
    reorientCamera(camera, rotation, getFocalPoint());
}

/** Rotate the camera by the given amount, then reposition it so the rotation center stays in the
 * same place
 */
void NavigationStyle::reorientCamera(
    SoCamera* camera,
    const SbRotation& rotation,
    const SbVec3f& rotationCenter
)
{
    if (!camera) {
        return;
    }

    // Distance from rotation center to camera position in camera coordinate system
    SbVec3f rotationCenterDistanceCam;
    camera->orientation.getValue().inverse().multVec(
        camera->position.getValue() - rotationCenter,
        rotationCenterDistanceCam
    );

    // Set new orientation value by accumulating the new rotation
    camera->orientation = rotation * camera->orientation.getValue();

    // Distance from rotation center to new camera position in global coordinate system
    SbVec3f newRotationCenterDistance;
    camera->orientation.getValue().multVec(rotationCenterDistanceCam, newRotationCenterDistance);

    // Reposition camera so the rotation center stays in the same place
    camera->position = rotationCenter + newRotationCenterDistance;

    if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {
        // Adjust the camera position to keep the focal point in the same place while making sure
        // the focal distance stays small. This increases rotation stability for small and large
        // scenes

        SbVec3f direction;
        camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 405)
        // Large focal distance puts the camera far away which causes Coin's auto clipping
        // calculations to add more slack (more space between near and far plane) and thus reduces
        // chances or hidden geometry.
        constexpr float orthographicFocalDistance = 250;
#else
        constexpr float orthographicFocalDistance = 1;
#endif
        camera->position = getFocalPoint() - orthographicFocalDistance * direction;
        camera->focalDistance = orthographicFocalDistance;
    }

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
    // Fix issue with near clipping in orthogonal view
    if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {

        // The center of the bounding sphere in camera coordinate system
        SbVec3f center;
        camera->orientation.getValue().inverse().multVec(
            boundingSphere.getCenter() - camera->position.getValue(),
            center
        );

        SbVec3f dir;
        camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), dir);

        // Reposition the camera but keep the focal point the same
        // nearDistance is 0 and farDistance is the diameter of the bounding sphere
        float repositionDistance = -center.getValue()[2] - boundingSphere.getRadius();
        camera->position = camera->position.getValue() + repositionDistance * dir;
        camera->nearDistance = 0;
        camera->farDistance = 2 * boundingSphere.getRadius() + 1;
        camera->focalDistance = camera->focalDistance.getValue() - repositionDistance;
    }
#endif
}

void NavigationStyle::panCamera(
    SoCamera* cam,
    float aspectratio,
    const SbPlane& panplane,
    const SbVec2f& currpos,
    const SbVec2f& prevpos
)
{
    if (!cam) {  // can happen for empty scenegraph
        return;
    }
    if (currpos == prevpos) {  // useless invocation
        return;
    }


    // Find projection points for the last and current mouse coordinates.
    SbViewVolume vv = cam->getViewVolume(aspectratio);

    // See note in Coin docs for SoCamera::getViewVolume re:viewport mapping
    if (aspectratio < 1.0) {
        vv.scale(1.0 / aspectratio);
    }

    SbLine line;
    vv.projectPointToLine(currpos, line);
    SbVec3f current_planept;
    panplane.intersect(line, current_planept);
    vv.projectPointToLine(prevpos, line);
    SbVec3f old_planept;
    panplane.intersect(line, old_planept);

    // Reposition camera according to the vector difference between the
    // projected points.
    cam->position = cam->position.getValue() - (current_planept - old_planept);

    if (this->currentmode != NavigationStyle::IDLE) {
        hasPanned = true;
    }
}

void NavigationStyle::setupPanningPlane(const SoCamera* camera)
{
    // The plane we're projecting the mouse coordinates to get 3D
    // coordinates should stay the same during the whole pan
    // operation, so we should calculate this value here.
    if (!camera) {  // can happen for empty scenegraph
        this->panningplane = SbPlane(SbVec3f(0, 0, 1), 0);
    }
    else {
        const SbViewportRegion& vp = viewer->getViewportRegion();
        const float aspectratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(aspectratio);

        // See note in Coin docs for SoCamera::getViewVolume re:viewport mapping
        if (aspectratio < 1.0) {
            vv.scale(1.0 / aspectratio);
        }

        this->panningplane = vv.getPlane(camera->focalDistance.getValue());
    }
}

/** Dependent on the camera type this will either shrink or expand the
 * height of the viewport (orthogonal camera) or move the camera
 * closer or further away from the focal point in the scene.
 */
void NavigationStyle::zoom(SoCamera* cam, float diffvalue)
{
    if (!cam) {  // can happen for empty scenegraph
        return;
    }

    animator->stop();

    SoType t = cam->getTypeId();
    SbName tname = t.getName();

    // This will be in the range of <0, ->>.
    auto multiplicator = float(exp(diffvalue));

    if (t.isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {

        // Since there's no perspective, "zooming" in the original sense
        // of the word won't have any visible effect. So we just increase
        // or decrease the field-of-view values of the camera instead, to
        // "shrink" the projection size of the model / scene.

        auto oc = static_cast<SoOrthographicCamera*>(cam);
        oc->height = oc->height.getValue() * multiplicator;
    }
    else {
        // FrustumCamera can be found in the SmallChange CVS module (it's
        // a camera that lets you specify (for instance) an off-center
        // frustum (similar to glFrustum())
        if (!t.isDerivedFrom(SoPerspectiveCamera::getClassTypeId()) && tname != "FrustumCamera") {
#ifdef FC_DEBUG
            static SbBool first = true;
            if (first) {
                SoDebugError::postWarning(
                    "NavigationStyle::zoom",
                    "Unknown camera type, "
                    "will zoom by moving position, "
                    "but this might not be correct."
                );
                first = false;
            }
#endif
        }

        const float oldfocaldist = cam->focalDistance.getValue();
        const float newfocaldist = oldfocaldist * multiplicator;

        SbVec3f direction;
        cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);

        const SbVec3f oldpos = cam->position.getValue();
        const SbVec3f newpos = oldpos + (newfocaldist - oldfocaldist) * -direction;

        // This catches a rather common user interface "buglet": if the
        // user zooms the camera out to a distance from origo larger than
        // what we still can safely do floating point calculations on
        // (i.e. without getting NaN or Inf values), the faulty floating
        // point values will propagate until we start to get debug error
        // messages and eventually an assert failure from core Coin code.
        //
        // With the below bounds check, this problem is avoided.
        //
        // (But note that we depend on the input argument ''diffvalue'' to
        // be small enough that zooming happens gradually. Ideally, we
        // should also check distorigo with isinf() and isnan() (or
        // inversely; isinfite()), but those only became standardized with
        // C99.)
        const float distorigo = newpos.length();
        // sqrt(FLT_MAX) == ~ 1e+19, which should be both safe for further
        // calculations and ok for the end-user and app-programmer.
        float maxDistance = std::sqrt(std::numeric_limits<float>::max());
        if (distorigo > maxDistance) {
            // do nothing here
        }
        else {
            cam->position = newpos;
            cam->focalDistance = newfocaldist;
        }
    }

    if (this->currentmode != NavigationStyle::IDLE) {
        hasZoomed = true;
    }
}

// Calculate a zoom/dolly factor from the difference of the current
// cursor position and the last.
void NavigationStyle::zoomByCursor(const SbVec2f& thispos, const SbVec2f& prevpos)
{
    // There is no "geometrically correct" value, 20 just seems to give
    // about the right "feel".
    float value = (thispos[1] - prevpos[1]) * 10.0f /*20.0f*/;
    if (this->invertZoom) {
        value = -value;
    }
    zoom(viewer->getSoRenderManager()->getCamera(), value);
}

void NavigationStyle::zoomIn()
{
    zoom(viewer->getSoRenderManager()->getCamera(), -this->zoomStep);
}

void NavigationStyle::zoomOut()
{
    zoom(viewer->getSoRenderManager()->getCamera(), this->zoomStep);
}

/*!
 * Returns the steps if the mouse wheel is rotated
 */
int NavigationStyle::getDelta() const
{
    return 120;
}

void NavigationStyle::doZoom(SoCamera* camera, int wheeldelta, const SbVec2f& pos)
{
    float value = this->zoomStep * wheeldelta / float(getDelta());
    if (this->invertZoom) {
        value = -value;
    }
    doZoom(camera, value, pos);
}

/*!
 *\brief NavigationStyle::doZoom Zooms in or out by specified factor, keeping the point on screen
 * specified by parameter pos fixed or not according to user preference
 * (NavigationStyle::zoomAtCursor). Ignores invertZoom user preference.
 */
void NavigationStyle::doZoom(SoCamera* camera, float logfactor, const SbVec2f& pos)
{
    // something is asking for big zoom factor. This func is made for interactive zooming,
    // where the changes are per mouse move and thus are small.
    if (fabs(logfactor) > 4.0) {
        return;
    }
    SbBool zoomAtCur = this->zoomAtCursor;
    if (zoomAtCur) {
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio());
        SbPlane panplane = vv.getPlane(camera->focalDistance.getValue());
        panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, SbVec2f(0.5, 0.5), pos);
    }

    zoom(camera, logfactor);

    if (zoomAtCur) {
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio());
        SbPlane panplane = vv.getPlane(camera->focalDistance.getValue());
        panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, pos, SbVec2f(0.5, 0.5));

        // Change the position of the rotation center indicator after zooming at cursor
        // Rotation mode is WindowCenter
        if (!rotationCenterMode) {
            viewer->changeRotationCenterPosition(getFocalPoint());

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
            findBoundingSphere();
#endif
        }
    }
}
void NavigationStyle::doScale(SoCamera* cam, float factor)
{
    if (cam->getTypeId() == SoOrthographicCamera::getClassTypeId()) {
        float height = static_cast<SoOrthographicCamera*>(cam)->height.getValue() * factor;
        static_cast<SoOrthographicCamera*>(cam)->height = height;
    }
    else if (cam->getTypeId() == SoPerspectiveCamera::getClassTypeId()) {
        float height = static_cast<SoPerspectiveCamera*>(cam)->heightAngle.getValue() / 2.0f;
        height = 2.0f * atan(tan(height) * factor);
        static_cast<SoPerspectiveCamera*>(cam)->heightAngle = height;
    }
}
void NavigationStyle::doRotate(SoCamera* camera, float angle, const SbVec2f& pos)
{
    SbBool zoomAtCur = this->zoomAtCursor;
    if (zoomAtCur) {
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio());
        SbPlane panplane = vv.getPlane(camera->focalDistance.getValue());
        panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, SbVec2f(0.5, 0.5), pos);
    }

    SbRotation rotcam = camera->orientation.getValue();
    // get view direction
    SbVec3f vdir;
    rotcam.multVec(SbVec3f(0, 0, -1), vdir);
    // rotate
    SbRotation drot(vdir, angle);
    camera->orientation.setValue(rotcam * drot);

    if (zoomAtCur) {
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();
        SbViewVolume vv = camera->getViewVolume(vp.getViewportAspectRatio());
        SbPlane panplane = vv.getPlane(camera->focalDistance.getValue());
        panCamera(viewer->getSoRenderManager()->getCamera(), ratio, panplane, pos, SbVec2f(0.5, 0.5));
    }
}

SbVec3f NavigationStyle::getRotationCenter(SbBool& found) const
{
    found = this->rotationCenterFound;
    return this->rotationCenter;
}

std::optional<SbVec2s>& NavigationStyle::getRightClickPosition()
{
    return rightClickPosition;
}

void NavigationStyle::setRotationCenter(const SbVec3f& cnt)
{
    this->rotationCenter = cnt;
    this->rotationCenterFound = true;

    const auto camera = getCamera();
    if (camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
        SbVec3f direction;
        camera->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);

        // Calculate distance from camera to rotation center
        const auto rotationCenterDistance = rotationCenter - camera->position.getValue();
        const auto rotationCenterDepth = rotationCenterDistance.dot(direction);

        // Set focal distance to match rotation center depth so we can zoom at the new rotation
        // center with a perspective camera
        camera->focalDistance.setValue(rotationCenterDepth);
    }
}

SbVec3f NavigationStyle::getFocalPoint() const
{
    SoCamera* cam = viewer->getSoRenderManager()->getCamera();
    if (!cam) {
        return {0, 0, 0};
    }

    // Find global coordinates of focal point.
    SbVec3f direction;
    cam->orientation.getValue().multVec(SbVec3f(0, 0, -1), direction);
    SbVec3f focal = cam->position.getValue() + cam->focalDistance.getValue() * direction;
    return focal;
}

/** Uses the sphere sheet projector to map the mouse position onto
 * a 3D point and find a rotation from this and the last calculated point.
 */
void NavigationStyle::spin(const SbVec2f& pointerpos)
{
    if (this->log.historysize < 2) {
        return;
    }
    assert(this->spinprojector);

    const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
    SbVec2s glsize(vp.getViewportSizePixels());
    SbVec2f lastpos;
    lastpos[0] = float(this->log.position[1][0]) / float(std::max((int)(glsize[0] - 1), 1));
    lastpos[1] = float(this->log.position[1][1]) / float(std::max((int)(glsize[1] - 1), 1));

    if (getOrbitStyle() == FreeTurntable) {
        SbVec2f midpos(lastpos[0], pointerpos[1]);
        spinInternal(pointerpos, midpos);
        spinInternal(midpos, lastpos);
    }
    else {
        spinInternal(pointerpos, lastpos);
    }

    if (this->currentmode != NavigationStyle::IDLE) {
        hasDragged = true;
    }
}

void NavigationStyle::spinInternal(const SbVec2f& pointerpos, const SbVec2f& lastpos)
{
    float sensitivity = getSensitivity();

    // Adjust the spin projector sphere to the screen position of the rotation center when the mouse
    // intersects an object
    if ((getOrbitStyle() == Trackball || getOrbitStyle() == TrackballClassic
         || getOrbitStyle() == RoundedArcball)
        && rotationCenterMode & RotationCenterMode::ScenePointAtCursor && rotationCenterFound
        && rotationCenterIsScenePointAtCursor) {
        const auto pointOnScreen = viewer->getPointOnViewport(rotationCenter);
        const auto sphereCenter = 2 * normalizePixelPos(pointOnScreen) - SbVec2f {1, 1};

        float x, y;
        sphereCenter.getValue(x, y);

        const float sphereScale = 1 + sphereCenter.length();
        const float radius = FCSphereSheetProjector::defaultSphereRadius * sphereScale;
        sensitivity *= sphereScale;

        spinprojector->setSphere(SbSphere {SbVec3f {x, y, 0}, radius});
    }
    else {
        spinprojector->setSphere(
            SbSphere {SbVec3f {0, 0, 0}, FCSphereSheetProjector::defaultSphereRadius}
        );
    }

    // 0000333: Turntable camera rotation
    SbMatrix mat;
    viewer->getSoRenderManager()->getCamera()->orientation.getValue().getValue(mat);
    this->spinprojector->setWorkingSpace(mat);

    this->spinprojector->project(lastpos);
    SbRotation r;
    this->spinprojector->projectAndGetRotation(pointerpos, r);
    if (sensitivity > 1.0f) {
        SbVec3f axis;
        float radians {};
        r.getValue(axis, radians);
        radians = sensitivity * radians;
        r.setValue(axis, radians);
    }
    r.invert();

    if (this->rotationCenterMode && this->rotationCenterFound) {
        this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r, rotationCenter);
    }
    else {
        this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r);
    }

    // Calculate an average angle magnitude value to make the transition
    // to a possible spin animation mode appear smooth.

    SbVec3f dummy_axis, newaxis;
    float acc_angle {}, newangle {};
    this->spinincrement.getValue(dummy_axis, acc_angle);
    acc_angle *= this->spinsamplecounter;  // weight
    r.getValue(newaxis, newangle);
    acc_angle += newangle;

    this->spinsamplecounter++;
    acc_angle /= this->spinsamplecounter;
    // FIXME: accumulate and average axis vectors as well? 19990501 mortene.
    this->spinincrement.setValue(newaxis, acc_angle);

    // Don't carry too much baggage, as that'll give unwanted results
    // when the user quickly trigger (as in "click-drag-release") a spin
    // animation.
    if (this->spinsamplecounter > 3) {
        this->spinsamplecounter = 3;
    }
}

/*!
 * \brief NavigationStyle::spin_simplified is a simplified version of
 * NavigationStyle::spin(..), which uses less global variables. Doesn't support
 * starting an animated spinning.
 *
 * \param cam the camera to affect. The rotation amount is determined by delta
 * (curpos-prevpos), and rotation axis is also affected by average pos.
 * \param curpos  current normalized position or mouse pointer
 * \param prevpos  previous normalized position of mouse pointer
 */
void NavigationStyle::spin_simplified(SbVec2f curpos, SbVec2f prevpos)
{
    assert(this->spinprojector);

    if (getOrbitStyle() == FreeTurntable) {
        SbVec2f midpos(prevpos[0], curpos[1]);
        spinSimplifiedInternal(curpos, midpos);
        spinSimplifiedInternal(midpos, prevpos);
    }
    else {
        spinSimplifiedInternal(curpos, prevpos);
    }

    hasDragged = true;
}

void NavigationStyle::spinSimplifiedInternal(SbVec2f curpos, SbVec2f prevpos)
{
    // 0000333: Turntable camera rotation
    SbMatrix mat;
    viewer->getSoRenderManager()->getCamera()->orientation.getValue().getValue(mat);
    this->spinprojector->setWorkingSpace(mat);

    this->spinprojector->project(prevpos);
    SbRotation r;
    this->spinprojector->projectAndGetRotation(curpos, r);
    float sensitivity = getSensitivity();
    if (sensitivity > 1.0f) {
        SbVec3f axis;
        float radians {};
        r.getValue(axis, radians);
        radians = sensitivity * radians;
        r.setValue(axis, radians);
    }
    r.invert();

    if (this->rotationCenterMode && this->rotationCenterFound) {
        this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r, rotationCenter);
    }
    else {
        this->reorientCamera(viewer->getSoRenderManager()->getCamera(), r);
    }
}

SbBool NavigationStyle::doSpin()
{
    if (this->log.historysize >= 3) {
        SbTime stoptime = (SbTime::getTimeOfDay() - this->log.time[0]);
        if (isSpinningAnimationEnabled() && stoptime.getValue() < 0.100) {
            const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
            const SbVec2s glsize(vp.getViewportSizePixels());
            SbVec3f from = this->spinprojector->project(SbVec2f(
                float(this->log.position[2][0]) / float(std::max(glsize[0] - 1, 1)),
                float(this->log.position[2][1]) / float(std::max(glsize[1] - 1, 1))
            ));
            SbVec3f to = this->spinprojector->project(this->lastmouseposition);
            SbRotation rot = this->spinprojector->getRotation(from, to);

            SbTime delta = (this->log.time[0] - this->log.time[2]);
            double deltatime = delta.getValue();
            rot.invert();
            rot.scaleAngle(float(0.200 / deltatime));

            SbVec3f axis;
            float radians {};
            rot.getValue(axis, radians);
            if ((radians > 0.01f) && (deltatime < 0.300)) {
                viewer->startSpinningAnimation(axis, radians * 5);
                return true;
            }
        }
    }

    return false;
}

void NavigationStyle::saveCursorPosition(const SoEvent* const ev)
{
    this->globalPos.setValue(QCursor::pos().x(), QCursor::pos().y());
    this->localPos = ev->getPosition();
    rotationCenterIsScenePointAtCursor = false;

    // mode is WindowCenter
    if (!this->rotationCenterMode) {
        setRotationCenter(getFocalPoint());
    }

    // Option to get point on model (slow) or always on focal plane (fast)
    //
    //  mode is ScenePointAtCursor to get exact point if possible
    if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::ScenePointAtCursor) {
        SoRayPickAction rpaction(viewer->getSoRenderManager()->getViewportRegion());
        rpaction.setPoint(this->localPos);
        rpaction.setRadius(viewer->getPickRadius());
        rpaction.apply(viewer->getSoRenderManager()->getSceneGraph());

        SoPickedPoint* picked = rpaction.getPickedPoint();
        if (picked) {
            setRotationCenter(picked->getPoint());
            rotationCenterIsScenePointAtCursor = true;
            return;
        }
    }

    // mode is FocalPointAtCursor or a ScenePointAtCursor failed
    if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::FocalPointAtCursor) {
        // get the intersection point of the ray and the focal plane
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();

        SoCamera* cam = viewer->getSoRenderManager()->getCamera();
        if (!cam) {  // no camera
            return;
        }
        SbViewVolume vv = cam->getViewVolume(ratio);

        SbLine line;
        SbVec2f currpos = ev->getNormalizedPosition(vp);
        vv.projectPointToLine(currpos, line);
        SbVec3f current_planept;
        SbPlane panplane = vv.getPlane(cam->focalDistance.getValue());
        panplane.intersect(line, current_planept);

        setRotationCenter(current_planept);
    }

    // mode is BoundingBoxCenter or a ScenePointAtCursor failed
    if (this->rotationCenterMode & NavigationStyle::RotationCenterMode::BoundingBoxCenter) {
        const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
        float ratio = vp.getViewportAspectRatio();

        SoCamera* cam = viewer->getSoRenderManager()->getCamera();
        if (!cam) {  // no camera
            return;
        }

        // Get the bounding box center of the physical object group
        SoGetBoundingBoxAction action(viewer->getSoRenderManager()->getViewportRegion());
        action.apply(viewer->objectGroup);
        SbBox3f boundingBox = action.getBoundingBox();
        SbVec3f boundingBoxCenter = boundingBox.getCenter();
        setRotationCenter(boundingBoxCenter);

        // To drag around the center point of the bbox we have to determine
        // its projection on the screen because this information is used in
        // NavigationStyle::spin() for the panning
        SbViewVolume vv = cam->getViewVolume(ratio);
        vv.projectToScreen(boundingBoxCenter, boundingBoxCenter);
        SbVec2s size = vp.getViewportSizePixels();
        auto tox = static_cast<short>(boundingBoxCenter[0] * size[0]);
        auto toy = static_cast<short>(boundingBoxCenter[1] * size[1]);
        this->localPos.setValue(tox, toy);
    }
}

SbVec2f NavigationStyle::normalizePixelPos(SbVec2s pixpos)
{
    const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
    const SbVec2s size(vp.getViewportSizePixels());
    return {
        (float)pixpos[0] / (float)std::max((int)(size[0] - 1), 1),
        (float)pixpos[1] / (float)std::max((int)(size[1] - 1), 1)
    };
}

SbVec2f NavigationStyle::normalizePixelPos(SbVec2f pixpos)
{
    const SbViewportRegion& vp = viewer->getSoRenderManager()->getViewportRegion();
    const SbVec2s size(vp.getViewportSizePixels());
    return {
        pixpos[0] / (float)std::max((int)(size[0] - 1), 1),
        pixpos[1] / (float)std::max((int)(size[1] - 1), 1)
    };
}

void NavigationStyle::moveCursorPosition()
{
    if (!isResetCursorPosition()) {
        return;
    }

    QPoint cpos = QCursor::pos();
    if (abs(cpos.x() - globalPos[0]) > 10 || abs(cpos.y() - globalPos[1]) > 10) {
        QCursor::setPos(globalPos[0], globalPos[1] - 1);
        this->log.position[0] = localPos;
    }
}


SbBool NavigationStyle::handleEventInForeground(const SoEvent* const e)
{
    SoHandleEventAction action(viewer->getSoRenderManager()->getViewportRegion());
    action.setEvent(e);
    action.setPickRadius(viewer->getPickRadius());
    action.apply(viewer->foregroundroot);
    return action.isHandled();
}

/**
 * @brief Decide if it should be possible to start any animation
 *
 * If the enable flag is false and we're currently animating, the animation will be stopped
 */
void NavigationStyle::setAnimationEnabled(const SbBool enable)
{
    animationEnabled = enable;
    if (!enable && isAnimating()) {
        animator->stop();
    }
}

/**
 * @brief Decide if it should be possible to start a spin animation of the model in the viewer by
 * releasing the mouse button while dragging
 *
 * If the enable flag is false and we're currently animating, the spin animation will be stopped
 */
void NavigationStyle::setSpinningAnimationEnabled(const SbBool enable)
{
    spinningAnimationEnabled = enable;
    if (!enable && isSpinning()) {
        animator->stop();
    }
}

/**
 * @return Whether or not it is possible to start any animation
 */
SbBool NavigationStyle::isAnimationEnabled() const
{
    return animationEnabled;
}

/**
 * @return Whether or not it is possible to start a spinning animation e.g. after dragging
 */
SbBool NavigationStyle::isSpinningAnimationEnabled() const
{
    return animationEnabled && spinningAnimationEnabled;
}

/**
 * @return Whether or not any animation is currently active
 */
SbBool NavigationStyle::isAnimating() const
{
    return animator->isAnimating();
}

/**
 * @return Whether or not a spinning animation is currently active e.g. after a user drag
 */
SbBool NavigationStyle::isSpinning() const
{
    return currentmode == NavigationStyle::SPINNING;
}

void NavigationStyle::startAnimating(const std::shared_ptr<NavigationAnimation>& animation, bool wait) const
{
    if (wait) {
        animator->startAndWait(animation);
    }
    else {
        animator->start(animation);
    }
}

void NavigationStyle::stopAnimating() const
{
    animator->stop();
}

void NavigationStyle::setSensitivity(float val)
{
    this->sensitivity = val;
}

float NavigationStyle::getSensitivity() const
{
    return this->sensitivity;
}

void NavigationStyle::setResetCursorPosition(SbBool on)
{
    this->resetcursorpos = on;
}

SbBool NavigationStyle::isResetCursorPosition() const
{
    return this->resetcursorpos;
}

void NavigationStyle::setZoomInverted(SbBool on)
{
    this->invertZoom = on;
}

SbBool NavigationStyle::isZoomInverted() const
{
    return this->invertZoom;
}

void NavigationStyle::setZoomStep(float val)
{
    this->zoomStep = val;
}

void NavigationStyle::setZoomAtCursor(SbBool on)
{
    this->zoomAtCursor = on;
}

SbBool NavigationStyle::isZoomAtCursor() const
{
    return this->zoomAtCursor;
}

void NavigationStyle::setRotationCenterMode(NavigationStyle::RotationCenterModes mode)
{
    this->rotationCenterMode = mode;
}

NavigationStyle::RotationCenterModes NavigationStyle::getRotationCenterMode() const
{
    return this->rotationCenterMode;
}

void NavigationStyle::startSelection(AbstractMouseSelection* mouse)
{
    if (!mouse) {
        return;
    }

    if (mouseSelection) {
        SoDebugError::postWarning(
            "NavigationStyle::startSelection",
            "Set new mouse selection while an old is still active."
        );
    }

    mouseSelection = mouse;
    mouseSelection->grabMouseModel(viewer);
}

void NavigationStyle::startSelection(NavigationStyle::SelectionMode mode)
{
    if (mouseSelection) {
        return;
    }
    if (isSelecting()) {
        stopSelection();
    }

    switch (mode) {
        case Lasso:
            mouseSelection = new PolyPickerSelection();
            break;
        case Rectangle:
            mouseSelection = new RectangleSelection();
            break;
        case Rubberband:
            mouseSelection = new RubberbandSelection();
            break;
        case BoxZoom:
            mouseSelection = new BoxZoomSelection();
            break;
        case Clip:
            mouseSelection = new PolyClipSelection();
            break;
        default:
            break;
    }

    if (mouseSelection) {
        mouseSelection->grabMouseModel(viewer);
    }
}

void NavigationStyle::abortSelection()
{
    pcPolygon.clear();
    if (mouseSelection) {
        mouseSelection->releaseMouseModel(true);
        delete mouseSelection;
        mouseSelection = nullptr;
    }
}

void NavigationStyle::stopSelection()
{
    pcPolygon.clear();
    if (mouseSelection) {
        mouseSelection->releaseMouseModel();
        delete mouseSelection;
        mouseSelection = nullptr;
    }
}

SbBool NavigationStyle::isSelecting() const
{
    return (mouseSelection ? true : false);
}

const std::vector<SbVec2s>& NavigationStyle::getPolygon(SelectionRole* role) const
{
    if (role) {
        *role = this->selectedRole;
    }
    return pcPolygon;
}

bool NavigationStyle::isDraggerUnderCursor(const SbVec2s pos) const
{
    SoRayPickAction rp(this->viewer->getSoRenderManager()->getViewportRegion());
    rp.setRadius(viewer->getPickRadius());
    rp.setPoint(pos);
    rp.apply(this->viewer->getSoRenderManager()->getSceneGraph());
    SoPickedPoint* pick = rp.getPickedPoint();
    if (pick) {
        const auto fullpath = static_cast<const SoFullPath*>(pick->getPath());
        for (int i = 0; i < fullpath->getLength(); ++i) {
            if (fullpath->getNode(i)->isOfType(SoDragger::getClassTypeId())) {
                return true;
            }
        }
        return false;
    }
    return false;
}

// This method adds another point to the mouse location log, used for spin
// animation calculations.
void NavigationStyle::addToLog(const SbVec2s pos, const SbTime time)
{
    // In case someone changes the const size setting at the top of this
    // file too small.
    assert(this->log.size > 2 && "mouse log too small!");

    if (this->log.historysize > 0 && pos == this->log.position[0]) {
        return;
    }

    int lastidx = this->log.historysize;
    // If we've filled up the log, we should throw away the last item:
    if (lastidx == this->log.size) {
        lastidx--;
    }

    assert(lastidx < this->log.size);
    for (int i = lastidx; i > 0; i--) {
        this->log.position[i] = this->log.position[i - 1];
        this->log.time[i] = this->log.time[i - 1];
    }

    this->log.position[0] = pos;
    this->log.time[0] = time;
    if (this->log.historysize < this->log.size) {
        this->log.historysize += 1;
    }
}

// This method "clears" the mouse location log, used for spin
// animation calculations.
void NavigationStyle::clearLog()
{
    this->log.historysize = 0;
}

void NavigationStyle::syncModifierKeys(const SoEvent* const ev)
{
    // Mismatches in state of the modifier keys happens if the user
    // presses or releases them outside the viewer window.
    if (this->ctrldown != ev->wasCtrlDown()) {
        this->ctrldown = ev->wasCtrlDown();
    }
    if (this->shiftdown != ev->wasShiftDown()) {
        this->shiftdown = ev->wasShiftDown();
    }
    if (this->altdown != ev->wasAltDown()) {
        this->altdown = ev->wasAltDown();
    }
}

// The viewer is a state machine, and all changes to the current state
// are made through this call.
void NavigationStyle::setViewingMode(const ViewerMode newmode)
{
    const ViewerMode oldmode = this->currentmode;

    // Reset flags when changing from IDLE to another mode or if the mode is IDLE and the buttons
    // are released
    if ((oldmode == IDLE && newmode != IDLE)
        || (newmode == IDLE && !button1down && !button2down && !button3down)) {
        hasPanned = false;
        hasDragged = false;
        hasZoomed = false;
    }

    if (newmode == oldmode) {

        // The rotation center could have been changed even if the mode has not changed
        if (newmode == NavigationStyle::DRAGGING && rotationCenterFound) {
            viewer->changeRotationCenterPosition(rotationCenter);
        }

        return;
    }

    switch (newmode) {
        case DRAGGING:
            // Set up initial projection point for the projector object when
            // first starting a drag operation.
            animator->stop();
            viewer->showRotationCenter(true);

#if (COIN_MAJOR_VERSION * 100 + COIN_MINOR_VERSION * 10 + COIN_MICRO_VERSION < 403)
            findBoundingSphere();
#endif

            this->spinprojector->project(this->lastmouseposition);
            this->interactiveCountInc();
            this->clearLog();
            break;

        case SPINNING:
            this->interactiveCountInc();
            viewer->getSoRenderManager()->scheduleRedraw();
            break;

        case PANNING:
            animator->stop();
            setupPanningPlane(viewer->getSoRenderManager()->getCamera());
            this->interactiveCountInc();
            break;

        case ZOOMING:
            animator->stop();
            this->interactiveCountInc();
            break;

        case BOXZOOM:
            animator->stop();
            this->interactiveCountInc();
            break;

        default:  // include default to avoid compiler warnings.
            break;
    }

    switch (oldmode) {
        case SPINNING:
        case DRAGGING:
            viewer->showRotationCenter(false);
            [[fallthrough]];
        case PANNING:
        case ZOOMING:
        case BOXZOOM:
            this->interactiveCountDec();
            break;

        default:
            break;
    }

    viewer->setCursorRepresentation(newmode);
    this->currentmode = newmode;
}

int NavigationStyle::getViewingMode() const
{
    return (int)this->currentmode;
}

SbBool NavigationStyle::processEvent(const SoEvent* const ev)
{
    // If we're in picking mode then all events must be redirected to the
    // appropriate mouse model.
    if (mouseSelection) {
        int hd = mouseSelection->handleEvent(ev, viewer->getSoRenderManager()->getViewportRegion());
        if (hd == AbstractMouseSelection::Continue || hd == AbstractMouseSelection::Restart) {
            return true;
        }
        else if (hd == AbstractMouseSelection::Finish) {
            pcPolygon = mouseSelection->getPositions();
            selectedRole = mouseSelection->selectedRole();
            delete mouseSelection;
            mouseSelection = nullptr;
            syncWithEvent(ev);
            return NavigationStyle::processSoEvent(ev);
        }
        else if (hd == AbstractMouseSelection::Cancel) {
            pcPolygon.clear();
            delete mouseSelection;
            mouseSelection = nullptr;
            syncWithEvent(ev);
            return NavigationStyle::processSoEvent(ev);
        }
    }

    const ViewerMode curmode = this->currentmode;

    SbBool processed = false;
    processed = this->processSoEvent(ev);

    // check for left click without selecting something
    if ((curmode == NavigationStyle::SELECTION || curmode == NavigationStyle::IDLE) && !processed) {
        if (SoMouseButtonEvent::isButtonReleaseEvent(ev, SoMouseButtonEvent::BUTTON1)) {
            if (!ev->wasCtrlDown()) {
                Gui::Selection().clearSelection();
            }
        }
    }

    return processed;
}

SbBool NavigationStyle::processSoEvent(const SoEvent* const ev)
{
    bool processed = false;
    bool offeredtoViewerEventBase = false;

    // handle mouse wheel zoom
    if (ev->isOfType(SoMouseWheelEvent::getClassTypeId())) {
        auto const event = static_cast<const SoMouseWheelEvent*>(ev);
        processed = processWheelEvent(event);
        viewer->processSoEventBase(ev);
        offeredtoViewerEventBase = true;
    }

    if (!processed && !offeredtoViewerEventBase) {
        processed = viewer->processSoEventBase(ev);
    }

    return processed;
}

void NavigationStyle::syncWithEvent(const SoEvent* const ev)
{
    // Events when in "ready-to-seek" mode are ignored, except those
    // which influence the seek mode itself -- these are handled further
    // up the inheritance hierarchy.
    if (this->isSeekMode()) {
        return;
    }

    const SoType type(ev->getTypeId());

    // Mismatches in state of the modifier keys happens if the user
    // presses or releases them outside the viewer window.
    syncModifierKeys(ev);

    // Keyboard handling
    if (type.isDerivedFrom(SoKeyboardEvent::getClassTypeId())) {
        auto const event = static_cast<const SoKeyboardEvent*>(ev);
        const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
        switch (event->getKey()) {
            case SoKeyboardEvent::LEFT_CONTROL:
            case SoKeyboardEvent::RIGHT_CONTROL:
                this->ctrldown = press;
                break;
            case SoKeyboardEvent::LEFT_SHIFT:
            case SoKeyboardEvent::RIGHT_SHIFT:
                this->shiftdown = press;
                break;
            case SoKeyboardEvent::LEFT_ALT:
            case SoKeyboardEvent::RIGHT_ALT:
                this->altdown = press;
                break;
            default:
                break;
        }
    }

    // Mouse Button / Spaceball Button handling
    if (type.isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
        auto const event = static_cast<const SoMouseButtonEvent*>(ev);
        const int button = event->getButton();
        const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
#ifdef FC_DEBUG
        SoDebugError::postInfo("processSoEvent", "button = %d", button);
#endif
        switch (button) {
            case SoMouseButtonEvent::BUTTON1:
                this->button1down = press;
                break;
            case SoMouseButtonEvent::BUTTON2:
                this->button2down = press;
                break;
            case SoMouseButtonEvent::BUTTON3:
                this->button3down = press;
                break;
            default:
                break;
        }
    }
}

SbBool NavigationStyle::processMotionEvent(const SoMotion3Event* const ev)
{
    SoCamera* const camera = viewer->getSoRenderManager()->getCamera();
    if (!camera) {
        return false;
    }

    SbViewVolume volume(camera->getViewVolume());
    SbVec3f center(volume.getSightPoint(camera->focalDistance.getValue()));
    float scale(volume.getWorldToScreenScale(center, 1.0));
    float translationFactor = scale * .0001;

    SbVec3f dir = ev->getTranslation();

    if (camera->getTypeId().isDerivedFrom(SoOrthographicCamera::getClassTypeId())) {
        auto oCam = static_cast<SoOrthographicCamera*>(camera);
        oCam->scaleHeight(1.0 + (dir[2] * 0.0001));
        dir[2] = 0.0;  // don't move the cam for z translation.
    }

    SbRotation newRotation(ev->getRotation() * camera->orientation.getValue());
    SbVec3f newPosition, newDirection;
    newRotation.multVec(SbVec3f(0.0, 0.0, -1.0), newDirection);
    newPosition = center - (newDirection * camera->focalDistance.getValue());

    camera->orientation.setValue(newRotation);
    camera->orientation.getValue().multVec(dir, dir);
    camera->position = newPosition + (dir * translationFactor);

    return true;
}

SbBool NavigationStyle::processKeyboardEvent(const SoKeyboardEvent* const event)
{
    SbBool processed = false;
    const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
    switch (event->getKey()) {
        case SoKeyboardEvent::LEFT_CONTROL:
        case SoKeyboardEvent::RIGHT_CONTROL:
            this->ctrldown = press;
            break;
        case SoKeyboardEvent::LEFT_SHIFT:
        case SoKeyboardEvent::RIGHT_SHIFT:
            this->shiftdown = press;
            break;
        case SoKeyboardEvent::LEFT_ALT:
        case SoKeyboardEvent::RIGHT_ALT:
            this->altdown = press;
            break;
        case SoKeyboardEvent::S:
        case SoKeyboardEvent::HOME:
        case SoKeyboardEvent::LEFT_ARROW:
        case SoKeyboardEvent::UP_ARROW:
        case SoKeyboardEvent::RIGHT_ARROW:
        case SoKeyboardEvent::DOWN_ARROW:
            if (!this->isViewing()) {
                this->setViewing(true);
            }
            break;
        case SoKeyboardEvent::PAGE_UP: {
            processed = true;
            const SbVec2f posn = normalizePixelPos(event->getPosition());
            doZoom(viewer->getSoRenderManager()->getCamera(), getDelta(), posn);
            break;
        }
        case SoKeyboardEvent::PAGE_DOWN: {
            processed = true;
            const SbVec2f posn = normalizePixelPos(event->getPosition());
            doZoom(viewer->getSoRenderManager()->getCamera(), -getDelta(), posn);
            break;
        }
        default:
            break;
    }

    return processed;
}

SbBool NavigationStyle::processClickEvent(const SoMouseButtonEvent* const event)
{
    // issue #0002433: avoid to swallow the UP event if down the
    // scene graph somewhere a dialog gets opened
    SbBool processed = false;
    const SbBool press = event->getState() == SoButtonEvent::DOWN ? true : false;
    if (press) {
        SbTime tmp = (event->getTime() - mouseDownConsumedEvent.getTime());
        float dci = (float)QApplication::doubleClickInterval() / 1000.0f;
        // a double-click?
        if (tmp.getValue() < dci) {
            mouseDownConsumedEvent = *event;
            mouseDownConsumedEvent.setTime(event->getTime());
            processed = true;
        }
        else {
            mouseDownConsumedEvent.setTime(event->getTime());
            // 'ANY' is used to mark that we don't know yet if it will
            // be a double-click event.
            mouseDownConsumedEvent.setButton(SoMouseButtonEvent::ANY);
        }
    }
    else if (!press) {
        if (mouseDownConsumedEvent.getButton() == SoMouseButtonEvent::BUTTON1) {
            // now handle the postponed event
            NavigationStyle::processSoEvent(&mouseDownConsumedEvent);
            mouseDownConsumedEvent.setButton(SoMouseButtonEvent::ANY);
        }
    }

    return processed;
}

SbBool NavigationStyle::processWheelEvent(const SoMouseWheelEvent* const event)
{
    const SbVec2s pos(event->getPosition());
    const SbVec2f posn = normalizePixelPos(pos);

    // handle mouse wheel zoom
    doZoom(viewer->getSoRenderManager()->getCamera(), event->getDelta(), posn);
    return true;
}

void NavigationStyle::setPopupMenuEnabled(const SbBool on)
{
    this->menuenabled = on;
}

SbBool NavigationStyle::isPopupMenuEnabled() const
{
    return this->menuenabled;
}

bool NavigationStyle::isNavigationStyleAction(QAction* action, QActionGroup* navMenuGroup) const
{
    return action && navMenuGroup->actions().indexOf(action) >= 0 && action->isChecked();
}

QWidget* NavigationStyle::findView3DInventorWidget() const
{
    QWidget* widget = viewer->getWidget();
    while (widget && !widget->inherits("Gui::View3DInventor")) {
        widget = widget->parentWidget();
    }
    return widget;
}

void NavigationStyle::applyNavigationStyleChange(QAction* selectedAction)
{
    QByteArray navigationStyleTypeName = selectedAction->data().toByteArray();
    QWidget* view3DWidget = findView3DInventorWidget();

    if (view3DWidget) {
        Base::Type newNavigationStyle = Base::Type::fromName(navigationStyleTypeName.constData());
        if (newNavigationStyle != this->getTypeId()) {
            QEvent* navigationChangeEvent = new NavigationStyleEvent(newNavigationStyle);
            QApplication::postEvent(view3DWidget, navigationChangeEvent);
        }
    }
}

void NavigationStyle::openPopupMenu(const SbVec2s& position)
{
    // store the right-click position for potential use by Clarify Selection
    rightClickPosition = position;

    // ask workbenches and view provider, ...
    MenuItem view;
    Gui::Application::Instance->setupContextMenu("View", &view);

    auto contextMenu = new QMenu(viewer->getGLWidget());
    MenuManager::getInstance()->setupContextMenu(&view, *contextMenu);
    contextMenu->setAttribute(Qt::WA_DeleteOnClose);

    auto navMenu = contextMenu->addMenu(QObject::tr("Navigation styles"));
    auto navMenuGroup = new QActionGroup(navMenu);

    // add submenu at the end to select navigation style
    const std::map<Base::Type, std::string> styles = UserNavigationStyle::getUserFriendlyNames();
    for (const auto& style : styles) {
        const QString name = QApplication::translate(style.first.getName(), style.second.c_str());
        QAction* item = navMenuGroup->addAction(name);
        navMenu->addAction(item);
        item->setCheckable(true);
        item->setData(QByteArray(style.first.getName()));

        if (const Base::Type item_style = style.first; item_style != this->getTypeId()) {
            auto triggeredFun = [this, item_style]() {
                QWidget* widget = viewer->getWidget();
                while (widget && !widget->inherits("Gui::View3DInventor")) {
                    widget = widget->parentWidget();
                }
                if (widget) {
                    // this is the widget where the viewer is embedded
                    QEvent* ns_event = new NavigationStyleEvent(item_style);
                    QApplication::postEvent(widget, ns_event);
                }
            };
            item->connect(item, &QAction::triggered, triggeredFun);
        }
        else {
            item->setChecked(true);
        }
    }

    // Add Clarify Selection option if there are objects under cursor
    bool separator = false;
    auto posAction = !contextMenu->actions().empty() ? contextMenu->actions().front() : nullptr;

    // Get picked objects at position
    SoRayPickAction rp(viewer->getSoRenderManager()->getViewportRegion());
    rp.setPoint(position);
    rp.setRadius(viewer->getPickRadius());
    rp.setPickAll(true);
    rp.apply(viewer->getSoRenderManager()->getSceneGraph());

    const SoPickedPointList& pplist = rp.getPickedPointList();
    QAction* pickAction = nullptr;

    if (pplist.getLength() > 0) {
        separator = true;
        if (auto cmd
            = Application::Instance->commandManager().getCommandByName("Std_ClarifySelection")) {
            pickAction = new QAction(cmd->getAction()->text(), contextMenu);
            pickAction->setShortcut(cmd->getAction()->shortcut());
        }
        else {
            pickAction = new QAction(QObject::tr("Clarify Selection"), contextMenu);
        }
        if (posAction) {
            contextMenu->insertAction(posAction, pickAction);
            contextMenu->insertSeparator(posAction);
        }
        else {
            contextMenu->addAction(pickAction);
        }
    }

    if (separator && posAction) {
        contextMenu->insertSeparator(posAction);
    }

    auto navigationFunction = [this, navMenuGroup](QAction* selectedAction) {
        // handle navigation style change if user selected a navigation style option
        if (isNavigationStyleAction(selectedAction, navMenuGroup)) {
            applyNavigationStyleChange(selectedAction);
            rightClickPosition.reset();
        }
    };

    auto clarifyFunction = [pickAction](QAction* selectedAction) {
        if (selectedAction == pickAction) {
            // Execute the Clarify Selection command at this position
            auto cmd = Application::Instance->commandManager().getCommandByName("Std_ClarifySelection");
            if (cmd && cmd->isActive()) {
                cmd->invoke(0);  // required placeholder value - we don't use group command
            }
        }
    };

    QObject::connect(contextMenu, &QMenu::triggered, navigationFunction);
    QObject::connect(contextMenu, &QMenu::triggered, clarifyFunction);

    contextMenu->popup(QCursor::pos());

    rightClickPosition.reset();
}

PyObject* NavigationStyle::getPyObject()
{
    if (pythonObject.is(nullptr)) {
        // ref counter is set to 1
        pythonObject = Py::asObject(new NavigationStylePy(this));
    }
    return Py::new_reference_to(pythonObject);
}

// ----------------------------------------------------------------------------------

TYPESYSTEM_SOURCE_ABSTRACT(Gui::UserNavigationStyle, Gui::NavigationStyle)

std::string UserNavigationStyle::userFriendlyName() const
{
    std::string name = this->getTypeId().getName();
    // remove namespaces
    std::size_t pos = name.rfind("::");
    if (pos != std::string::npos) {
        name = name.substr(pos + 2);
    }

    // remove 'NavigationStyle'
    pos = name.find("NavigationStyle");
    if (pos != std::string::npos) {
        name = name.substr(0, pos);
    }
    return name;
}

std::map<Base::Type, std::string> UserNavigationStyle::getUserFriendlyNames()
{
    std::map<Base::Type, std::string> names;
    std::vector<Base::Type> types;
    Base::Type::getAllDerivedFrom(UserNavigationStyle::getClassTypeId(), types);

    for (auto& type : types) {
        if (type != UserNavigationStyle::getClassTypeId()) {
            std::unique_ptr<UserNavigationStyle> inst(
                static_cast<UserNavigationStyle*>(type.createInstance())
            );
            if (inst) {
                names[type] = inst->userFriendlyName();
            }
        }
    }
    return names;
}