Welcome to mirror list, hosted at ThFree Co, Russian Federation.

view3d_edit.c « space_view3d « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98742bd0c7ce86af3c8304f305ff20954c510b7d (plain)
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
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 *
 *
 * Contributor(s): Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/space_view3d/view3d_edit.c
 *  \ingroup spview3d
 */


#include <string.h>
#include <stdio.h>
#include <math.h>
#include <float.h>

#include "DNA_armature_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_camera_types.h"

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_rand.h"
#include "BLI_utildefines.h"

#include "BKE_context.h"
#include "BKE_image.h"
#include "BKE_library.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_report.h"
#include "BKE_scene.h"


#include "BIF_gl.h"
#include "BIF_glutil.h"

#include "WM_api.h"
#include "WM_types.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "ED_particle.h"
#include "ED_screen.h"
#include "ED_transform.h"
#include "ED_mesh.h"
#include "ED_view3d.h"


#include "PIL_time.h" /* smoothview */

#include "view3d_intern.h"	// own include

/* ********************** view3d_edit: view manipulations ********************* */

/* ********************* box view support ***************** */

static void view3d_boxview_clip(ScrArea *sa)
{
	ARegion *ar;
	BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
	float clip[6][4];
	float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
	int val;

	/* create bounding box */
	for(ar= sa->regionbase.first; ar; ar= ar->next) {
		if(ar->regiontype==RGN_TYPE_WINDOW) {
			RegionView3D *rv3d= ar->regiondata;

			if(rv3d->viewlock & RV3D_BOXCLIP) {
				if(ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
					if(ar->winx>ar->winy) x1= rv3d->dist;
					else x1= ar->winx*rv3d->dist/ar->winy;

					if(ar->winx>ar->winy) y1= ar->winy*rv3d->dist/ar->winx;
					else y1= rv3d->dist;
					copy_v2_v2(ofs, rv3d->ofs);
				}
				else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
					ofs[2]= rv3d->ofs[2];

					if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
					else z1= rv3d->dist;
				}
			}
		}
	}

	for(val=0; val<8; val++) {
		if(ELEM4(val, 0, 3, 4, 7))
			bb->vec[val][0]= -x1 - ofs[0];
		else
			bb->vec[val][0]=  x1 - ofs[0];

		if(ELEM4(val, 0, 1, 4, 5))
			bb->vec[val][1]= -y1 - ofs[1];
		else
			bb->vec[val][1]=  y1 - ofs[1];

		if(val > 3)
			bb->vec[val][2]= -z1 - ofs[2];
		else
			bb->vec[val][2]=  z1 - ofs[2];
	}

	/* normals for plane equations */
	normal_tri_v3( clip[0],bb->vec[0], bb->vec[1], bb->vec[4]);
	normal_tri_v3( clip[1],bb->vec[1], bb->vec[2], bb->vec[5]);
	normal_tri_v3( clip[2],bb->vec[2], bb->vec[3], bb->vec[6]);
	normal_tri_v3( clip[3],bb->vec[3], bb->vec[0], bb->vec[7]);
	normal_tri_v3( clip[4],bb->vec[4], bb->vec[5], bb->vec[6]);
	normal_tri_v3( clip[5],bb->vec[0], bb->vec[2], bb->vec[1]);

	/* then plane equations */
	for(val=0; val<5; val++) {
		clip[val][3]= - clip[val][0]*bb->vec[val][0] - clip[val][1]*bb->vec[val][1] - clip[val][2]*bb->vec[val][2];
	}
	clip[5][3]= - clip[5][0]*bb->vec[0][0] - clip[5][1]*bb->vec[0][1] - clip[5][2]*bb->vec[0][2];

	/* create bounding box */
	for(ar= sa->regionbase.first; ar; ar= ar->next) {
		if(ar->regiontype==RGN_TYPE_WINDOW) {
			RegionView3D *rv3d= ar->regiondata;

			if(rv3d->viewlock & RV3D_BOXCLIP) {
				rv3d->rflag |= RV3D_CLIPPING;
				memcpy(rv3d->clip, clip, sizeof(clip));
				if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
				rv3d->clipbb= MEM_dupallocN(bb);
			}
		}
	}
	MEM_freeN(bb);
}

/* sync center/zoom view of region to others, for view transforms */
static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
{
	ARegion *artest;
	RegionView3D *rv3d= ar->regiondata;
	short clip= 0;

	for(artest= sa->regionbase.first; artest; artest= artest->next) {
		if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
			RegionView3D *rv3dtest= artest->regiondata;

			if(rv3dtest->viewlock) {
				rv3dtest->dist= rv3d->dist;

				if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
					if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
						rv3dtest->ofs[0]= rv3d->ofs[0];
					else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
						rv3dtest->ofs[1]= rv3d->ofs[1];
				}
				else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
					if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
						rv3dtest->ofs[0]= rv3d->ofs[0];
					else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
						rv3dtest->ofs[2]= rv3d->ofs[2];
				}
				else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
					if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
						rv3dtest->ofs[1]= rv3d->ofs[1];
					if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
						rv3dtest->ofs[2]= rv3d->ofs[2];
				}

				clip |= rv3dtest->viewlock & RV3D_BOXCLIP;

				ED_region_tag_redraw(artest);
			}
		}
	}

	if(clip) {
		view3d_boxview_clip(sa);
	}
}

/* for home, center etc */
void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
{
	ARegion *artest;
	RegionView3D *rv3d= ar->regiondata;
	short clip= 0;

	for(artest= sa->regionbase.first; artest; artest= artest->next) {
		if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
			RegionView3D *rv3dtest= artest->regiondata;

			if(rv3dtest->viewlock) {
				rv3dtest->dist= rv3d->dist;
				copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
				ED_region_tag_redraw(artest);

				clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
			}
		}
	}

	if(clip) {
		view3d_boxview_clip(sa);
	}
}

/* 'clip' is used to know if our clip setting has changed */
void ED_view3d_quadview_update(ScrArea *sa, ARegion *ar, short do_clip)
{
	ARegion *arsync= NULL;
	RegionView3D *rv3d= ar->regiondata;
	short viewlock;
	/* this function copies flags from the first of the 3 other quadview
	   regions to the 2 other, so it assumes this is the region whose
	   properties are always being edited, weak */
	viewlock= rv3d->viewlock;

	if((viewlock & RV3D_LOCKED)==0)
		viewlock= 0;
	else if((viewlock & RV3D_BOXVIEW)==0) {
		viewlock &= ~RV3D_BOXCLIP;
		do_clip= TRUE;
	}

	for(; ar; ar= ar->prev) {
		if(ar->alignment==RGN_ALIGN_QSPLIT) {
			rv3d= ar->regiondata;
			rv3d->viewlock= viewlock;

			if(do_clip && (viewlock & RV3D_BOXCLIP)==0) {
				rv3d->rflag &= ~RV3D_BOXCLIP;
			}

			/* use arsync so we sync with one of the aligned views below
			 * else the view jumps on changing view settings like 'clip'
			 * since it copies from the perspective view */
			arsync= ar;
		}
	}

	if(rv3d->viewlock & RV3D_BOXVIEW) {
		view3d_boxview_copy(sa, arsync ? arsync : sa->regionbase.last);
	}

	ED_area_tag_redraw(sa);
}

/* ************************** init for view ops **********************************/

typedef struct ViewOpsData {
	ScrArea *sa;
	ARegion *ar;
	RegionView3D *rv3d;

	/* needed for continuous zoom */
	wmTimer *timer;
	double timer_lastdraw;

	float oldquat[4];
	float trackvec[3];
	float reverse, dist0;
	float grid, far;
	short axis_snap; /* view rotate only */

	/* use for orbit selection and auto-dist */
	float ofs[3], dyn_ofs[3];
	short use_dyn_ofs;

	int origx, origy, oldx, oldy;
	int origkey; /* the key that triggered the operator */

} ViewOpsData;

#define TRACKBALLSIZE  (1.1)

static void calctrackballvec(rcti *rect, int mx, int my, float *vec)
{
	float x, y, radius, d, z, t;

	radius= TRACKBALLSIZE;

	/* normalize x and y */
	x= (rect->xmax + rect->xmin)/2 - mx;
	x/= (float)((rect->xmax - rect->xmin)/4);
	y= (rect->ymax + rect->ymin)/2 - my;
	y/= (float)((rect->ymax - rect->ymin)/2);

	d = sqrt(x*x + y*y);
	if (d < radius * (float)M_SQRT1_2)  	/* Inside sphere */
		z = sqrt(radius*radius - d*d);
	else
	{ 			/* On hyperbola */
		t = radius / (float)M_SQRT2;
		z = t*t / d;
	}

	vec[0]= x;
	vec[1]= y;
	vec[2]= -z;		/* yah yah! */
}


static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
{
	static float lastofs[3] = {0,0,0};
	View3D *v3d;
	RegionView3D *rv3d;
	ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");

	/* store data */
	op->customdata= vod;
	vod->sa= CTX_wm_area(C);
	vod->ar= CTX_wm_region(C);
	v3d= vod->sa->spacedata.first;
	vod->rv3d= rv3d= vod->ar->regiondata;
	vod->dist0= rv3d->dist;
	copy_qt_qt(vod->oldquat, rv3d->viewquat);
	vod->origx= vod->oldx= event->x;
	vod->origy= vod->oldy= event->y;
	vod->origkey= event->type; /* the key that triggered the operator.  */
	vod->use_dyn_ofs= (U.uiflag & USER_ORBIT_SELECTION) ? 1:0;

	if (vod->use_dyn_ofs) {
		copy_v3_v3(vod->ofs, rv3d->ofs);
		/* If there's no selection, lastofs is unmodified and last value since static */
		calculateTransformCenter(C, V3D_CENTROID, lastofs);
		negate_v3_v3(vod->dyn_ofs, lastofs);
	}
	else if (U.uiflag & USER_ORBIT_ZBUF) {

		view3d_operator_needs_opengl(C); /* needed for zbuf drawing */

		if((vod->use_dyn_ofs=view_autodist(CTX_data_scene(C), vod->ar, v3d, event->mval, vod->dyn_ofs))) {
			if (rv3d->persp==RV3D_PERSP) {
				float my_origin[3]; /* original G.vd->ofs */
				float my_pivot[3]; /* view */
				float dvec[3];

				// locals for dist correction
				float mat[3][3];
				float upvec[3];

				negate_v3_v3(my_origin, rv3d->ofs);				/* ofs is flipped */

				/* Set the dist value to be the distance from this 3d point */
				/* this means youll always be able to zoom into it and panning wont go bad when dist was zero */

				/* remove dist value */
				upvec[0] = upvec[1] = 0;
				upvec[2] = rv3d->dist;
				copy_m3_m4(mat, rv3d->viewinv);

				mul_m3_v3(mat, upvec);
				sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
				negate_v3(my_pivot);				/* ofs is flipped */

				/* find a new ofs value that is allong the view axis (rather then the mouse location) */
				closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
				vod->dist0 = rv3d->dist = len_v3v3(my_pivot, dvec);

				negate_v3_v3(rv3d->ofs, dvec);
			}
			negate_v3(vod->dyn_ofs);
			copy_v3_v3(vod->ofs, rv3d->ofs);
		} else {
			vod->ofs[0] = vod->ofs[1] = vod->ofs[2] = 0.0f;
		}
	}

	/* lookup, we dont pass on v3d to prevent confusement */
	vod->grid= v3d->grid;
	vod->far= v3d->far;

	calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);

	initgrabz(rv3d, -rv3d->ofs[0], -rv3d->ofs[1], -rv3d->ofs[2]);

	vod->reverse= 1.0f;
	if (rv3d->persmat[2][1] < 0.0f)
		vod->reverse= -1.0f;

	rv3d->rflag |= RV3D_NAVIGATING;
}

static void viewops_data_free(bContext *C, wmOperator *op)
{
	ARegion *ar;
	Paint *p = paint_get_active(CTX_data_scene(C));

	if(op->customdata) {
		ViewOpsData *vod= op->customdata;
		ar= vod->ar;
		vod->rv3d->rflag &= ~RV3D_NAVIGATING;

		if(vod->timer)
			WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);

		MEM_freeN(vod);
		op->customdata= NULL;
	}
	else {
		ar= CTX_wm_region(C);
	}

	if(p && (p->flags & PAINT_FAST_NAVIGATE))
		ED_region_tag_redraw(ar);
}

/* ************************** viewrotate **********************************/

static const float thres = 0.93f; //cos(20 deg);

#define COS45 0.70710678118654746
#define SIN45 COS45

static float snapquats[39][5] = {
	/*{q0, q1, q3, q4, view}*/
	{COS45, -SIN45, 0.0, 0.0, RV3D_VIEW_FRONT},  //front
	{0.0, 0.0, -SIN45, -SIN45, RV3D_VIEW_BACK}, //back
	{1.0, 0.0, 0.0, 0.0, RV3D_VIEW_TOP},       //top
	{0.0, -1.0, 0.0, 0.0, RV3D_VIEW_BOTTOM},      //bottom
	{0.5, -0.5, -0.5, -0.5, RV3D_VIEW_RIGHT},    //left
	{0.5, -0.5, 0.5, 0.5, RV3D_VIEW_LEFT},      //right

	/* some more 45 deg snaps */
	{0.65328145027160645, -0.65328145027160645, 0.27059805393218994, 0.27059805393218994, 0},
	{0.92387950420379639, 0.0, 0.0, 0.38268342614173889, 0},
	{0.0, -0.92387950420379639, 0.38268342614173889, 0.0, 0},
	{0.35355335474014282, -0.85355335474014282, 0.35355338454246521, 0.14644660055637360, 0},
	{0.85355335474014282, -0.35355335474014282, 0.14644660055637360, 0.35355338454246521, 0},
	{0.49999994039535522, -0.49999994039535522, 0.49999997019767761, 0.49999997019767761, 0},
	{0.27059802412986755, -0.65328145027160645, 0.65328145027160645, 0.27059802412986755, 0},
	{0.65328145027160645, -0.27059802412986755, 0.27059802412986755, 0.65328145027160645, 0},
	{0.27059799432754517, -0.27059799432754517, 0.65328139066696167, 0.65328139066696167, 0},
	{0.38268336653709412, 0.0, 0.0, 0.92387944459915161, 0},
	{0.0, -0.38268336653709412, 0.92387944459915161, 0.0, 0},
	{0.14644658565521240, -0.35355335474014282, 0.85355335474014282, 0.35355335474014282, 0},
	{0.35355335474014282, -0.14644658565521240, 0.35355335474014282, 0.85355335474014282, 0},
	{0.0, 0.0, 0.92387944459915161, 0.38268336653709412, 0},
	{-0.0, 0.0, 0.38268336653709412, 0.92387944459915161, 0},
	{-0.27059802412986755, 0.27059802412986755, 0.65328133106231689, 0.65328133106231689, 0},
	{-0.38268339633941650, 0.0, 0.0, 0.92387938499450684, 0},
	{0.0, 0.38268339633941650, 0.92387938499450684, 0.0, 0},
	{-0.14644658565521240, 0.35355338454246521, 0.85355329513549805, 0.35355332493782043, 0},
	{-0.35355338454246521, 0.14644658565521240, 0.35355332493782043, 0.85355329513549805, 0},
	{-0.49999991059303284, 0.49999991059303284, 0.49999985098838806, 0.49999985098838806, 0},
	{-0.27059799432754517, 0.65328145027160645, 0.65328139066696167, 0.27059799432754517, 0},
	{-0.65328145027160645, 0.27059799432754517, 0.27059799432754517, 0.65328139066696167, 0},
	{-0.65328133106231689, 0.65328133106231689, 0.27059793472290039, 0.27059793472290039, 0},
	{-0.92387932538986206, 0.0, 0.0, 0.38268333673477173, 0},
	{0.0, 0.92387932538986206, 0.38268333673477173, 0.0, 0},
	{-0.35355329513549805, 0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0},
	{-0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0.35355329513549805, 0},
	{-0.38268330693244934, 0.92387938499450684, 0.0, 0.0, 0},
	{-0.92387938499450684, 0.38268330693244934, 0.0, 0.0, 0},
	{-COS45, 0.0, 0.0, SIN45, 0},
	{COS45, 0.0, 0.0, SIN45, 0},
	{0.0, 0.0, 0.0, 1.0, 0}
};

enum {
	VIEW_PASS= 0,
	VIEW_APPLY,
	VIEW_CONFIRM
};

/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
#define VIEW_MODAL_CONFIRM				1 /* used for all view operations */
#define VIEWROT_MODAL_AXIS_SNAP_ENABLE	2
#define VIEWROT_MODAL_AXIS_SNAP_DISABLE	3
#define VIEWROT_MODAL_SWITCH_ZOOM		4
#define VIEWROT_MODAL_SWITCH_MOVE		5
#define VIEWROT_MODAL_SWITCH_ROTATE		6

/* called in transform_ops.c, on each regeneration of keymaps  */
void viewrotate_modal_keymap(wmKeyConfig *keyconf)
{
	static EnumPropertyItem modal_items[] = {
	{VIEW_MODAL_CONFIRM,	"CONFIRM", 0, "Confirm", ""},

	{VIEWROT_MODAL_AXIS_SNAP_ENABLE,	"AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
	{VIEWROT_MODAL_AXIS_SNAP_DISABLE,	"AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
		
	{VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
	{VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},

	{0, NULL, 0, NULL, NULL}};

	wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Rotate Modal");

	/* this function is called for each spacetype, only needs to add map once */
	if(keymap) return;

	keymap= WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);

	/* items for modal map */
	WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
	WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);

	WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
	WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);

	/* disabled mode switching for now, can re-implement better, later on
	WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
	WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
	WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
	*/
	
	/* assign map to operators */
	WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");

}

static void viewrotate_apply(ViewOpsData *vod, int x, int y)
{
	RegionView3D *rv3d= vod->rv3d;

	rv3d->view= 0; /* need to reset everytime because of view snapping */

	if (U.flag & USER_TRACKBALL) {
		float phi, si, q1[4], dvec[3], newvec[3];

		calctrackballvec(&vod->ar->winrct, x, y, newvec);

		sub_v3_v3v3(dvec, newvec, vod->trackvec);

		si= sqrt(dvec[0]*dvec[0]+ dvec[1]*dvec[1]+ dvec[2]*dvec[2]);
		si /= (float)(2.0 * TRACKBALLSIZE);

		cross_v3_v3v3(q1+1, vod->trackvec, newvec);
		normalize_v3(q1+1);

		/* Allow for rotation beyond the interval
			* [-pi, pi] */
		while (si > 1.0f)
			si -= 2.0f;

		/* This relation is used instead of
			* phi = asin(si) so that the angle
			* of rotation is linearly proportional
			* to the distance that the mouse is
			* dragged. */
		phi = si * (float)(M_PI / 2.0);

		q1[0]= cos(phi);
		mul_v3_fl(q1+1, sin(phi));
		mul_qt_qtqt(rv3d->viewquat, q1, vod->oldquat);

		if (vod->use_dyn_ofs) {
			/* compute the post multiplication quat, to rotate the offset correctly */
			copy_qt_qt(q1, vod->oldquat);
			conjugate_qt(q1);
			mul_qt_qtqt(q1, q1, rv3d->viewquat);

			conjugate_qt(q1); /* conj == inv for unit quat */
			copy_v3_v3(rv3d->ofs, vod->ofs);
			sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
			mul_qt_v3(q1, rv3d->ofs);
			add_v3_v3(rv3d->ofs, vod->dyn_ofs);
		}
	}
	else {
		/* New turntable view code by John Aughey */
		float phi, q1[4];
		float m[3][3];
		float m_inv[3][3];
		float xvec[3] = {1.0f, 0.0f, 0.0f};
		/* Sensitivity will control how fast the viewport rotates.  0.0035 was
			obtained experimentally by looking at viewport rotation sensitivities
			on other modeling programs. */
		/* Perhaps this should be a configurable user parameter. */
		const float sensitivity = 0.0035f;

		/* Get the 3x3 matrix and its inverse from the quaternion */
		quat_to_mat3( m,rv3d->viewquat);
		invert_m3_m3(m_inv,m);

		/* Determine the direction of the x vector (for rotating up and down) */
		/* This can likely be computed directly from the quaternion. */
		mul_m3_v3(m_inv,xvec);

		/* Perform the up/down rotation */
		phi = sensitivity * -(y - vod->oldy);
		q1[0] = cos(phi);
		mul_v3_v3fl(q1+1, xvec, sin(phi));
		mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);

		if (vod->use_dyn_ofs) {
			conjugate_qt(q1); /* conj == inv for unit quat */
			sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
			mul_qt_v3(q1, rv3d->ofs);
			add_v3_v3(rv3d->ofs, vod->dyn_ofs);
		}

		/* Perform the orbital rotation */
		phi = sensitivity * vod->reverse * (x - vod->oldx);
		q1[0] = cos(phi);
		q1[1] = q1[2] = 0.0;
		q1[3] = sin(phi);
		mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);

		if (vod->use_dyn_ofs) {
			conjugate_qt(q1);
			sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
			mul_qt_v3(q1, rv3d->ofs);
			add_v3_v3(rv3d->ofs, vod->dyn_ofs);
		}
	}

	/* check for view snap */
	if (vod->axis_snap){
		int i;
		float viewquat_inv[4];
		float zaxis[3]={0,0,1};
		invert_qt_qt(viewquat_inv, rv3d->viewquat);

		mul_qt_v3(viewquat_inv, zaxis);

		for (i = 0 ; i < 39; i++){

			float view = (int)snapquats[i][4];
			float viewquat_inv_test[4];
			float zaxis_test[3]={0,0,1};

			invert_qt_qt(viewquat_inv_test, snapquats[i]);
			mul_qt_v3(viewquat_inv_test, zaxis_test);
			
			if(angle_v3v3(zaxis_test, zaxis) < DEG2RADF(45/3)) {
				/* find the best roll */
				float quat_roll[4], quat_final[4], quat_best[4];
				float viewquat_align[4]; /* viewquat aligned to zaxis_test */
				float viewquat_align_inv[4]; /* viewquat aligned to zaxis_test */
				float best_angle = FLT_MAX;
				int j;

				/* viewquat_align is the original viewquat aligned to the snapped axis
				 * for testing roll */
				rotation_between_vecs_to_quat(viewquat_align, zaxis_test, zaxis);
				normalize_qt(viewquat_align);
				mul_qt_qtqt(viewquat_align, rv3d->viewquat, viewquat_align);
				normalize_qt(viewquat_align);
				invert_qt_qt(viewquat_align_inv, viewquat_align);

				/* find best roll */
				for(j= 0; j<8; j++) {
					float angle;
					float xaxis1[3]={1,0,0};
					float xaxis2[3]={1,0,0};
					float quat_final_inv[4];

					axis_angle_to_quat(quat_roll, zaxis_test, (float)j * DEG2RADF(45.0f));
					normalize_qt(quat_roll);

					mul_qt_qtqt(quat_final, snapquats[i], quat_roll);
					normalize_qt(quat_final);
					
					/* compare 2 vector angles to find the least roll */
					invert_qt_qt(quat_final_inv, quat_final);
					mul_qt_v3(viewquat_align_inv, xaxis1);
					mul_qt_v3(quat_final_inv, xaxis2);
					angle= angle_v3v3(xaxis1, xaxis2);

					if(angle <= best_angle) {
						best_angle= angle;
						copy_qt_qt(quat_best, quat_final);
						if(j) view= 0; /* view grid assumes certain up axis */
					}
				}

				copy_qt_qt(rv3d->viewquat, quat_best);
				rv3d->view= view; /* if we snap to a rolled camera the grid is invalid */

				break;
			}
		}
	}
	vod->oldx= x;
	vod->oldy= y;

	/* avoid precision loss over time */
	normalize_qt(rv3d->viewquat);

	ED_region_tag_redraw(vod->ar);
}

static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
{
	ViewOpsData *vod= op->customdata;
	short event_code= VIEW_PASS;

	/* execute the events */
	if(event->type==MOUSEMOVE) {
		event_code= VIEW_APPLY;
	}
	else if(event->type==EVT_MODAL_MAP) {
		switch (event->val) {
			case VIEW_MODAL_CONFIRM:
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
				vod->axis_snap= TRUE;
				event_code= VIEW_APPLY;
				break;
			case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
				vod->axis_snap= FALSE;
				event_code= VIEW_APPLY;
				break;
			case VIEWROT_MODAL_SWITCH_ZOOM:
				WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_SWITCH_MOVE:
				WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
		}
	}
	else if(event->type==vod->origkey && event->val==KM_RELEASE) {
		event_code= VIEW_CONFIRM;
	}

	if(event_code==VIEW_APPLY) {
		viewrotate_apply(vod, event->x, event->y);
	}
	else if (event_code==VIEW_CONFIRM) {
		request_depth_update(vod->rv3d);
		viewops_data_free(C, op);

		return OPERATOR_FINISHED;
	}

	return OPERATOR_RUNNING_MODAL;
}

static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ViewOpsData *vod;
	RegionView3D *rv3d;

	/* makes op->customdata */
	viewops_data_create(C, op, event);
	vod= op->customdata;
	rv3d= vod->rv3d;

	if(rv3d->viewlock) { /* poll should check but in some cases fails, see poll func for details */
		viewops_data_free(C, op);
		return OPERATOR_PASS_THROUGH;
	}

	/* switch from camera view when: */
	if(rv3d->persp != RV3D_PERSP) {

		if (U.uiflag & USER_AUTOPERSP) {
			rv3d->persp= RV3D_PERSP;
		}
		else if(rv3d->persp==RV3D_CAMOB) {

			/* changed since 2.4x, use the camera view */
			View3D *v3d = vod->sa->spacedata.first;

			if(v3d->camera) {
				view3d_settings_from_ob(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
			}

			if(rv3d->persp==RV3D_CAMOB) {
				rv3d->persp= rv3d->lpersp;
			}
		}
		ED_region_tag_redraw(vod->ar);
	}
	
	if (event->type == MOUSEPAN) {
		viewrotate_apply(vod, event->prevx, event->prevy);
		request_depth_update(rv3d);
		
		viewops_data_free(C, op);
		
		return OPERATOR_FINISHED;
	}
	else if (event->type == MOUSEROTATE) {
		/* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
		viewrotate_apply(vod, event->prevx, event->y);
		request_depth_update(rv3d);
		
		viewops_data_free(C, op);
		
		return OPERATOR_FINISHED;
	}
	else {		
		/* add temp handler */
		WM_event_add_modal_handler(C, op);

		return OPERATOR_RUNNING_MODAL;
	}
}

static int view3d_camera_active_poll(bContext *C)
{
	if(ED_operator_view3d_active(C)) {
		RegionView3D *rv3d= CTX_wm_region_view3d(C);
		if(rv3d && rv3d->persp==RV3D_CAMOB) {
			return 1;
		}
	}

	return 0;
}

void VIEW3D_OT_rotate(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "Rotate view";
	ot->description = "Rotate the view";
	ot->idname= "VIEW3D_OT_rotate";

	/* api callbacks */
	ot->invoke= viewrotate_invoke;
	ot->modal= viewrotate_modal;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
}

/* ************************ viewmove ******************************** */


/* NOTE: these defines are saved in keymap files, do not change values but just add new ones */

/* called in transform_ops.c, on each regeneration of keymaps  */
void viewmove_modal_keymap(wmKeyConfig *keyconf)
{
	static EnumPropertyItem modal_items[] = {
	{VIEW_MODAL_CONFIRM,	"CONFIRM", 0, "Confirm", ""},
		
	{VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
	{VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},

	{0, NULL, 0, NULL, NULL}};

	wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Move Modal");

	/* this function is called for each spacetype, only needs to add map once */
	if(keymap) return;

	keymap= WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);

	/* items for modal map */
	WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
	WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);

	/* disabled mode switching for now, can re-implement better, later on
	WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
	WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
	WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
	*/
	
	/* assign map to operators */
	WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
}


static void viewmove_apply(ViewOpsData *vod, int x, int y)
{
	if(vod->rv3d->persp==RV3D_CAMOB) {
		float zoomfac= ((float)M_SQRT2 + (float)vod->rv3d->camzoom / 50.0f);
		zoomfac= (zoomfac * zoomfac) * 0.5f;

		vod->rv3d->camdx += (vod->oldx - x)/(vod->ar->winx * zoomfac);
		vod->rv3d->camdy += (vod->oldy - y)/(vod->ar->winy * zoomfac);
		CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
		CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
// XXX		preview3d_event= 0;
	}
	else {
		float dvec[3];

		window_to_3d_delta(vod->ar, dvec, x-vod->oldx, y-vod->oldy);
		add_v3_v3(vod->rv3d->ofs, dvec);

		if(vod->rv3d->viewlock & RV3D_BOXVIEW)
			view3d_boxview_sync(vod->sa, vod->ar);
	}

	vod->oldx= x;
	vod->oldy= y;

	ED_region_tag_redraw(vod->ar);
}


static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
{

	ViewOpsData *vod= op->customdata;
	short event_code= VIEW_PASS;

	/* execute the events */
	if(event->type==MOUSEMOVE) {
		event_code= VIEW_APPLY;
	}
	else if(event->type==EVT_MODAL_MAP) {
		switch (event->val) {
			case VIEW_MODAL_CONFIRM:
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_SWITCH_ZOOM:
				WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_SWITCH_ROTATE:
				WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
		}
	}
	else if(event->type==vod->origkey && event->val==KM_RELEASE) {
		event_code= VIEW_CONFIRM;
	}

	if(event_code==VIEW_APPLY) {
		viewmove_apply(vod, event->x, event->y);
	}
	else if (event_code==VIEW_CONFIRM) {
		request_depth_update(vod->rv3d);

		viewops_data_free(C, op);

		return OPERATOR_FINISHED;
	}

	return OPERATOR_RUNNING_MODAL;
}

static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	/* makes op->customdata */
	viewops_data_create(C, op, event);

	if (event->type == MOUSEPAN) {
		ViewOpsData *vod= op->customdata;
		viewmove_apply(vod, event->prevx, event->prevy);
		request_depth_update(vod->rv3d);
		
		viewops_data_free(C, op);		
		
		return OPERATOR_FINISHED;
	}
	else {
		/* add temp handler */
		WM_event_add_modal_handler(C, op);

		return OPERATOR_RUNNING_MODAL;
	}
}

void VIEW3D_OT_move(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "Move view";
	ot->description = "Move the view";
	ot->idname= "VIEW3D_OT_move";

	/* api callbacks */
	ot->invoke= viewmove_invoke;
	ot->modal= viewmove_modal;
	ot->poll= ED_operator_view3d_active;

	/* flags */
	ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
}

/* ************************ viewzoom ******************************** */

/* called in transform_ops.c, on each regeneration of keymaps  */
void viewzoom_modal_keymap(wmKeyConfig *keyconf)
{
	static EnumPropertyItem modal_items[] = {
	{VIEW_MODAL_CONFIRM,	"CONFIRM", 0, "Confirm", ""},
		
	{VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
	{VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},

	{0, NULL, 0, NULL, NULL}};

	wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Zoom Modal");

	/* this function is called for each spacetype, only needs to add map once */
	if(keymap) return;

	keymap= WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);

	/* items for modal map */
	WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
	WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);

	/* disabled mode switching for now, can re-implement better, later on
	WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
	WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
	WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
	 */
	
	/* assign map to operators */
	WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
}

static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
{
	RegionView3D *rv3d= ar->regiondata;

	if(U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
		float dvec[3];
		float tvec[3];
		float tpos[3];
		float new_dist;
		short vb[2], mouseloc[2];

		mouseloc[0]= mx - ar->winrct.xmin;
		mouseloc[1]= my - ar->winrct.ymin;

		/* find the current window width and height */
		vb[0] = ar->winx;
		vb[1] = ar->winy;

		negate_v3_v3(tpos, rv3d->ofs);

		/* Project cursor position into 3D space */
		initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
		window_to_3d_delta(ar, dvec, mouseloc[0]-vb[0]/2, mouseloc[1]-vb[1]/2);

		/* Calculate view target position for dolly */
		add_v3_v3v3(tvec, tpos, dvec);
		negate_v3(tvec);

		/* Offset to target position and dolly */
		new_dist = rv3d->dist * dfac;

		copy_v3_v3(rv3d->ofs, tvec);
		rv3d->dist = new_dist;

		/* Calculate final offset */
		madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
	} else {
		rv3d->dist *= dfac;
	}
}


static void viewzoom_apply(ViewOpsData *vod, int x, int y, short viewzoom)
{
	float zfac=1.0;

	if(viewzoom==USER_ZOOM_CONT) {
		double time= PIL_check_seconds_timer();
		float time_step= (float)(time - vod->timer_lastdraw);

		// oldstyle zoom
		zfac = 1.0f + (((float)(vod->origx - x + vod->origy - y) / 20.0f) * time_step);
		vod->timer_lastdraw= time;
	}
	else if(viewzoom==USER_ZOOM_SCALE) {
		int ctr[2], len1, len2;
		// method which zooms based on how far you move the mouse

		ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin)/2;
		ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin)/2;

		len1 = (int)sqrt((ctr[0] - x)*(ctr[0] - x) + (ctr[1] - y)*(ctr[1] - y)) + 5;
		len2 = (int)sqrt((ctr[0] - vod->origx)*(ctr[0] - vod->origx) + (ctr[1] - vod->origy)*(ctr[1] - vod->origy)) + 5;

		zfac = vod->dist0 * ((float)len2/len1) / vod->rv3d->dist;
	}
	else {	/* USER_ZOOM_DOLLY */
		float len1, len2;
		
		if (U.uiflag & USER_ZOOM_DOLLY_HORIZ) {
			len1 = (vod->ar->winrct.xmax - x) + 5;
			len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
		}
		else {
			len1 = (vod->ar->winrct.ymax - y) + 5;
			len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
		}
		if (U.uiflag & USER_ZOOM_INVERT)
			SWAP(float, len1, len2);
		
		zfac = vod->dist0 * (2.0f * ((len2/len1)-1.0f) + 1.0f) / vod->rv3d->dist;
	}

	if(zfac != 1.0f && zfac*vod->rv3d->dist > 0.001f * vod->grid &&
				zfac * vod->rv3d->dist < 10.0f * vod->far)
		view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);


	if ((U.uiflag & USER_ORBIT_ZBUF) && (viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==RV3D_PERSP)) {
		float upvec[3], mat[3][3];

		/* Secret apricot feature, translate the view when in continues mode */
		upvec[0] = upvec[1] = 0.0f;
		upvec[2] = (vod->dist0 - vod->rv3d->dist) * vod->grid;
		vod->rv3d->dist = vod->dist0;
		copy_m3_m4(mat, vod->rv3d->viewinv);
		mul_m3_v3(mat, upvec);
		add_v3_v3(vod->rv3d->ofs, upvec);
	} else {
		/* these limits were in old code too */
		if(vod->rv3d->dist<0.001f * vod->grid) vod->rv3d->dist= 0.001f * vod->grid;
		if(vod->rv3d->dist>10.0f * vod->far) vod->rv3d->dist=10.0f * vod->far;
	}

// XXX	if(vod->rv3d->persp==RV3D_ORTHO || vod->rv3d->persp==RV3D_CAMOB) preview3d_event= 0;

	if(vod->rv3d->viewlock & RV3D_BOXVIEW)
		view3d_boxview_sync(vod->sa, vod->ar);

	ED_region_tag_redraw(vod->ar);
}


static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
{
	ViewOpsData *vod= op->customdata;
	short event_code= VIEW_PASS;

	/* execute the events */
	if (event->type == TIMER && event->customdata == vod->timer) {
		/* continuous zoom */
		event_code= VIEW_APPLY;
	}
	else if(event->type==MOUSEMOVE) {
		event_code= VIEW_APPLY;
	}
	else if(event->type==EVT_MODAL_MAP) {
		switch (event->val) {
			case VIEW_MODAL_CONFIRM:
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_SWITCH_MOVE:
				WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
			case VIEWROT_MODAL_SWITCH_ROTATE:
				WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
				event_code= VIEW_CONFIRM;
				break;
		}
	}
	else if(event->type==vod->origkey && event->val==KM_RELEASE) {
		event_code= VIEW_CONFIRM;
	}

	if(event_code==VIEW_APPLY) {
		viewzoom_apply(vod, event->x, event->y, U.viewzoom);
	}
	else if (event_code==VIEW_CONFIRM) {
		request_depth_update(vod->rv3d);
		viewops_data_free(C, op);

		return OPERATOR_FINISHED;
	}

	return OPERATOR_RUNNING_MODAL;
}

static int viewzoom_exec(bContext *C, wmOperator *op)
{
	View3D *v3d;
	RegionView3D *rv3d;
	ScrArea *sa;
	ARegion *ar;

	int delta= RNA_int_get(op->ptr, "delta");
	int mx, my;

	if(op->customdata) {
		ViewOpsData *vod= op->customdata;

		sa= vod->sa;
		ar= vod->ar;
	}
	else {
		sa= CTX_wm_area(C);
		ar= CTX_wm_region(C);
	}

	v3d= sa->spacedata.first;
	rv3d= ar->regiondata;

	mx= RNA_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
	my= RNA_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;

	if(delta < 0) {
		/* this min and max is also in viewmove() */
		if(rv3d->persp==RV3D_CAMOB) {
			rv3d->camzoom-= 10;
			if(rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
		}
		else if(rv3d->dist < 10.0f * v3d->far) {
			view_zoom_mouseloc(ar, 1.2f, mx, my);
		}
	}
	else {
		if(rv3d->persp==RV3D_CAMOB) {
			rv3d->camzoom+= 10;
			if(rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
		}
		else if(rv3d->dist> 0.001f * v3d->grid) {
			view_zoom_mouseloc(ar, .83333f, mx, my);
		}
	}

	if(rv3d->viewlock & RV3D_BOXVIEW)
		view3d_boxview_sync(sa, ar);

	request_depth_update(rv3d);
	ED_region_tag_redraw(ar);

	viewops_data_free(C, op);

	return OPERATOR_FINISHED;
}

static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	/* if one or the other zoom position aren't set, set from event */
	if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my"))
	{
		RNA_int_set(op->ptr, "mx", event->x);
		RNA_int_set(op->ptr, "my", event->y);
	}

	if(RNA_property_is_set(op->ptr, "delta")) {
		/* makes op->customdata */
		viewops_data_create(C, op, event);
		viewzoom_exec(C, op);
	}
	else {
		ViewOpsData *vod;

		/* makes op->customdata */
		viewops_data_create(C, op, event);

		vod= op->customdata;

		if (event->type == MOUSEZOOM) {
			if (U.uiflag & USER_ZOOM_INVERT) /* Bypass Zoom invert flag */
				SWAP(int, event->x, event->prevx);

			if (U.uiflag & USER_ZOOM_DOLLY_HORIZ) {
				vod->origx = vod->oldx = event->x;
				viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY);
			}
			else {
				
				/* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
				vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
				viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY);
			}
			request_depth_update(vod->rv3d);
			
			viewops_data_free(C, op);
			return OPERATOR_FINISHED;
		}
		else {
			if(U.viewzoom == USER_ZOOM_CONT) {
				/* needs a timer to continue redrawing */
				vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
				vod->timer_lastdraw= PIL_check_seconds_timer();
			}

			/* add temp handler */
			WM_event_add_modal_handler(C, op);

			return OPERATOR_RUNNING_MODAL;
		}
	}
	return OPERATOR_FINISHED;
}


void VIEW3D_OT_zoom(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Zoom View";
	ot->description = "Zoom in/out in the view";
	ot->idname= "VIEW3D_OT_zoom";

	/* api callbacks */
	ot->invoke= viewzoom_invoke;
	ot->exec= viewzoom_exec;
	ot->modal= viewzoom_modal;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;

	RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
	RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
}

static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
{
	ARegion *ar= CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	Scene *scene= CTX_data_scene(C);
	Base *base;
	float *curs;

	int center= RNA_boolean_get(op->ptr, "center");

	float size, min[3], max[3], afm[3];
	int ok= 1, onedone=0;

	if(center) {
		/* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
		curs= give_cursor(scene, v3d);
		zero_v3(min);
		zero_v3(max);
		zero_v3(curs);
	}
	else {
		INIT_MINMAX(min, max);
	}

	for(base= scene->base.first; base; base= base->next) {
		if(BASE_VISIBLE(v3d, base)) {
			onedone= 1;
			minmax_object(base->object, min, max);
		}
	}
	if(!onedone) {
		ED_region_tag_redraw(ar);
		/* TODO - should this be cancel?
		 * I think no, because we always move the cursor, with or without
		 * object, but in this case there is no change in the scene,
		 * only the cursor so I choice a ED_region_tag like
		 * smooth_view do for the center_cursor.
		 * See bug #22640
		 */
		return OPERATOR_FINISHED;
	}

	sub_v3_v3v3(afm, max, min);
	size= 0.7f*MAX3(afm[0], afm[1], afm[2]);
	if(size == 0.0f) ok= 0;

	if(ok) {
		float new_dist;
		float new_ofs[3];

		new_dist = size;
		new_ofs[0]= -(min[0]+max[0])/2.0f;
		new_ofs[1]= -(min[1]+max[1])/2.0f;
		new_ofs[2]= -(min[2]+max[2])/2.0f;

		// correction for window aspect ratio
		if(ar->winy>2 && ar->winx>2) {
			size= (float)ar->winx/(float)ar->winy;
			if(size < 1.0f) size= 1.0f/size;
			new_dist*= size;
		}

		if (rv3d->persp==RV3D_CAMOB) {
			rv3d->persp= RV3D_PERSP;
			smooth_view(C, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
		}
		else {
			smooth_view(C, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
		}
	}
// XXX	BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);

	WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);

	return OPERATOR_FINISHED;
}


void VIEW3D_OT_view_all(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View All";
	ot->description = "View all objects in scene";
	ot->idname= "VIEW3D_OT_view_all";

	/* api callbacks */
	ot->exec= view3d_all_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;

	RNA_def_boolean(ot->srna, "center", 0, "Center", "");
}


static int viewselected_exec(bContext *C, wmOperator *UNUSED(op)) /* like a localview without local!, was centerview() in 2.4x */
{
	ARegion *ar= CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	Scene *scene= CTX_data_scene(C);
	Object *ob= OBACT;
	Object *obedit= CTX_data_edit_object(C);
	float size, min[3], max[3], afm[3];
	int ok=0, ok_dist=1;

	/* SMOOTHVIEW */
	float new_ofs[3];
	float new_dist;

	INIT_MINMAX(min, max);

	if (ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
		/* hardcoded exception, we look for the one selected armature */
		/* this is weak code this way, we should make a generic active/selection callback interface once... */
		Base *base;
		for(base=scene->base.first; base; base= base->next) {
			if(TESTBASELIB(v3d, base)) {
				if(base->object->type==OB_ARMATURE)
					if(base->object->mode & OB_MODE_POSE)
						break;
			}
		}
		if(base)
			ob= base->object;
	}


	if(obedit) {
		ok = minmax_verts(obedit, min, max);	/* only selected */
	}
	else if(ob && (ob->mode & OB_MODE_POSE)) {
		if(ob->pose) {
			bArmature *arm= ob->data;
			bPoseChannel *pchan;
			float vec[3];

			for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
				if(pchan->bone->flag & BONE_SELECTED) {
					if(pchan->bone->layer & arm->layer) {
						bPoseChannel *pchan_tx= pchan->custom_tx ? pchan->custom_tx : pchan;
						ok= 1;
						mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
						DO_MINMAX(vec, min, max);
						mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
						DO_MINMAX(vec, min, max);
					}
				}
			}
		}
	}
	else if (paint_facesel_test(ob)) {
		ok= paintface_minmax(ob, min, max);
	}
	else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
		ok= PE_minmax(scene, min, max);
	}
	else {
		Base *base= FIRSTBASE;
		while(base) {
			if(TESTBASE(v3d, base))  {

				/* account for duplis */
				if (minmax_object_duplis(scene, base->object, min, max)==0)
					minmax_object(base->object, min, max); /* use if duplis not found */

				ok= 1;
			}
			base= base->next;
		}
	}

	if(ok==0) return OPERATOR_FINISHED;

	sub_v3_v3v3(afm, max, min);
	size= MAX3(afm[0], afm[1], afm[2]);

	if(rv3d->persp==RV3D_ORTHO) {
		if(size < 0.0001f) { /* if its a sinble point. dont even re-scale */
			ok_dist= 0;
		}
		else {
			/* perspective should be a bit farther away to look nice */
			size*= 0.7f;
		}
	}
	else {
		if(size <= v3d->near*1.5f) {
			size= v3d->near*1.5f;
		}
	}

	add_v3_v3v3(new_ofs, min, max);
	mul_v3_fl(new_ofs, -0.5f);

	new_dist = size;

	/* correction for window aspect ratio */
	if(ar->winy>2 && ar->winx>2) {
		size= (float)ar->winx/(float)ar->winy;
		if(size<1.0f) size= 1.0f/size;
		new_dist*= size;
	}

	if (rv3d->persp==RV3D_CAMOB) {
		rv3d->persp= RV3D_PERSP;
		smooth_view(C, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
	}
	else {
		smooth_view(C, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
	}

	/* smooth view does viewlock RV3D_BOXVIEW copy */
	
// XXX	BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_view_selected(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "View Selected";
	ot->description = "Move the view to the selection center";
	ot->idname= "VIEW3D_OT_view_selected";

	/* api callbacks */
	ot->exec= viewselected_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;
}

static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
{
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	Scene *scene= CTX_data_scene(C);
	
	if (rv3d) {
		/* non camera center */
		float new_ofs[3];
		negate_v3_v3(new_ofs, give_cursor(scene, v3d));
		smooth_view(C, NULL, NULL, new_ofs, NULL, NULL, NULL);
		
		/* smooth view does viewlock RV3D_BOXVIEW copy */
	}
	
	return OPERATOR_FINISHED;
}

void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Center View to Cursor";
	ot->description= "Centers the view so that the cursor is in the middle of the view";
	ot->idname= "VIEW3D_OT_view_center_cursor";
	
	/* api callbacks */
	ot->exec= viewcenter_cursor_exec;
	ot->poll= ED_operator_view3d_active;
	
	/* flags */
	ot->flag= 0;
}

static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
{
	RegionView3D *rv3d= CTX_wm_region_view3d(C);

	rv3d->camdx= rv3d->camdy= 0.0f;

	WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View Camera Center";
	ot->description = "Center the camera view";
	ot->idname= "VIEW3D_OT_view_center_camera";

	/* api callbacks */
	ot->exec= view3d_center_camera_exec;
	ot->poll= view3d_camera_active_poll;

	/* flags */
	ot->flag= 0;
}

/* ********************* Set render border operator ****************** */

static int render_border_exec(bContext *C, wmOperator *op)
{
	View3D *v3d = CTX_wm_view3d(C);
	ARegion *ar= CTX_wm_region(C);
	RegionView3D *rv3d= ED_view3d_context_rv3d(C);
	Scene *scene= CTX_data_scene(C);

	rcti rect;
	rctf vb;

	/* get border select values using rna */
	rect.xmin= RNA_int_get(op->ptr, "xmin");
	rect.ymin= RNA_int_get(op->ptr, "ymin");
	rect.xmax= RNA_int_get(op->ptr, "xmax");
	rect.ymax= RNA_int_get(op->ptr, "ymax");

	/* calculate range */
	view3d_calc_camera_border(scene, ar, rv3d, v3d, &vb, FALSE);

	scene->r.border.xmin= ((float)rect.xmin-vb.xmin)/(vb.xmax-vb.xmin);
	scene->r.border.ymin= ((float)rect.ymin-vb.ymin)/(vb.ymax-vb.ymin);
	scene->r.border.xmax= ((float)rect.xmax-vb.xmin)/(vb.xmax-vb.xmin);
	scene->r.border.ymax= ((float)rect.ymax-vb.ymin)/(vb.ymax-vb.ymin);

	/* actually set border */
	CLAMP(scene->r.border.xmin, 0.0f, 1.0f);
	CLAMP(scene->r.border.ymin, 0.0f, 1.0f);
	CLAMP(scene->r.border.xmax, 0.0f, 1.0f);
	CLAMP(scene->r.border.ymax, 0.0f, 1.0f);

	/* drawing a border surrounding the entire camera view switches off border rendering
	 * or the border covers no pixels */
	if ((scene->r.border.xmin <= 0.0f && scene->r.border.xmax >= 1.0f &&
		scene->r.border.ymin <= 0.0f && scene->r.border.ymax >= 1.0f) ||
	   (scene->r.border.xmin == scene->r.border.xmax ||
		scene->r.border.ymin == scene->r.border.ymax ))
	{
		scene->r.mode &= ~R_BORDER;
	} else {
		scene->r.mode |= R_BORDER;
	}
	
	WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, NULL);

	return OPERATOR_FINISHED;

}

void VIEW3D_OT_render_border(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Set Render Border";
	ot->description = "Set the boundaries of the border render and enables border render ";
	ot->idname= "VIEW3D_OT_render_border";

	/* api callbacks */
	ot->invoke= WM_border_select_invoke;
	ot->exec= render_border_exec;
	ot->modal= WM_border_select_modal;

	ot->poll= view3d_camera_active_poll;

	/* flags */
	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;

	/* rna */
	RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);

}
/* ********************* Border Zoom operator ****************** */

static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
{
	ARegion *ar= CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	Scene *scene= CTX_data_scene(C);

	/* Zooms in on a border drawn by the user */
	rcti rect;
	float dvec[3], vb[2], xscale, yscale, scale;

	/* SMOOTHVIEW */
	float new_dist;
	float new_ofs[3];

	/* ZBuffer depth vars */
	bglMats mats;
	float depth_close= FLT_MAX;
	double cent[2],  p[3];

	/* note; otherwise opengl won't work */
	view3d_operator_needs_opengl(C);

	/* get border select values using rna */
	rect.xmin= RNA_int_get(op->ptr, "xmin");
	rect.ymin= RNA_int_get(op->ptr, "ymin");
	rect.xmax= RNA_int_get(op->ptr, "xmax");
	rect.ymax= RNA_int_get(op->ptr, "ymax");

	/* Get Z Depths, needed for perspective, nice for ortho */
	bgl_get_mats(&mats);
	draw_depth(scene, ar, v3d, NULL);
	
	{
		/* avoid allocating the whole depth buffer */
		ViewDepths depth_temp= {0};

		/* avoid view3d_update_depths() for speed. */
		view3d_update_depths_rect(ar, &depth_temp, &rect);
	
		/* find the closest Z pixel */
		depth_close= view3d_depth_near(&depth_temp);
	
		MEM_freeN(depth_temp.depths);
	}

	cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
	cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;

	if (rv3d->persp==RV3D_PERSP) {
		double p_corner[3];

		/* no depths to use, we cant do anything! */
		if (depth_close==FLT_MAX){
			BKE_report(op->reports, RPT_ERROR, "Depth Too Large");
			return OPERATOR_CANCELLED;
		}
		/* convert border to 3d coordinates */
		if ((	!gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) ||
			(	!gluUnProject((double)rect.xmin, (double)rect.ymin, depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p_corner[0], &p_corner[1], &p_corner[2])))
			return OPERATOR_CANCELLED;

		dvec[0] = p[0]-p_corner[0];
		dvec[1] = p[1]-p_corner[1];
		dvec[2] = p[2]-p_corner[2];

		new_dist = len_v3(dvec);
		if(new_dist <= v3d->near * 1.5f) new_dist= v3d->near * 1.5f;

		new_ofs[0] = -p[0];
		new_ofs[1] = -p[1];
		new_ofs[2] = -p[2];

	} else { /* othographic */
		/* find the current window width and height */
		vb[0] = ar->winx;
		vb[1] = ar->winy;

		new_dist = rv3d->dist;

		/* convert the drawn rectangle into 3d space */
		if (depth_close!=FLT_MAX && gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) {
			new_ofs[0] = -p[0];
			new_ofs[1] = -p[1];
			new_ofs[2] = -p[2];
		} else {
			/* We cant use the depth, fallback to the old way that dosnt set the center depth */
			copy_v3_v3(new_ofs, rv3d->ofs);

			initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);

			window_to_3d_delta(ar, dvec, (rect.xmin+rect.xmax-vb[0])/2, (rect.ymin+rect.ymax-vb[1])/2);
			/* center the view to the center of the rectangle */
			sub_v3_v3(new_ofs, dvec);
		}

		/* work out the ratios, so that everything selected fits when we zoom */
		xscale = ((rect.xmax-rect.xmin)/vb[0]);
		yscale = ((rect.ymax-rect.ymin)/vb[1]);
		scale = (xscale >= yscale)?xscale:yscale;

		/* zoom in as required, or as far as we can go */
		new_dist = ((new_dist*scale) >= 0.001f * v3d->grid)? new_dist*scale:0.001f * v3d->grid;
	}

	smooth_view(C, NULL, NULL, new_ofs, NULL, &new_dist, NULL);

	if(rv3d->viewlock & RV3D_BOXVIEW)
		view3d_boxview_sync(CTX_wm_area(C), ar);

	return OPERATOR_FINISHED;
}

static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	RegionView3D *rv3d= CTX_wm_region_view3d(C);

	/* if in camera view do not exec the operator so we do not conflict with set render border*/
	if (rv3d->persp != RV3D_CAMOB)
		return WM_border_select_invoke(C, op, event);
	else
		return OPERATOR_PASS_THROUGH;
}

void VIEW3D_OT_zoom_border(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Border Zoom";
	ot->description = "Zoom in the view to the nearest object contained in the border";
	ot->idname= "VIEW3D_OT_zoom_border";

	/* api callbacks */
	ot->invoke= view3d_zoom_border_invoke;
	ot->exec= view3d_zoom_border_exec;
	ot->modal= WM_border_select_modal;

	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;

	/* rna */
	RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);

}

/* sets the view to 1:1 camera/render-pixel */
static void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
{
	RegionView3D *rv3d= ar->regiondata;
	float size[2];
	int im_width= (scene->r.size*scene->r.xsch)/100;
	
	view3d_viewborder_size_get(scene, ar, size);
	
	rv3d->camzoom= (sqrtf(4.0f * (float)im_width/size[0]) - (float)M_SQRT2) * 50.0f;
	rv3d->camzoom= CLAMPIS(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
}

static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
{
	Scene *scene= CTX_data_scene(C);
	ARegion *ar= CTX_wm_region(C);

	view3d_set_1_to_1_viewborder(scene, ar);

	WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Zoom Camera 1:1";
	ot->description = "Match the camera to 1:1 to the render output";
	ot->idname= "VIEW3D_OT_zoom_camera_1_to_1";

	/* api callbacks */
	ot->exec= view3d_zoom_1_to_1_camera_exec;
	ot->poll= view3d_camera_active_poll;

	/* flags */
	ot->flag= 0;
}

/* ********************* Changing view operator ****************** */

static EnumPropertyItem prop_view_items[] = {
	{RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
	{RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
	{RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
	{RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
	{RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
	{RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
	{RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
	{0, NULL, 0, NULL, NULL}};


/* would like to make this a generic function - outside of transform */

static void axis_set_view(bContext *C, float q1, float q2, float q3, float q4, short view, int perspo, int align_active)
{
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	float new_quat[4];

	new_quat[0]= q1; new_quat[1]= q2;
	new_quat[2]= q3; new_quat[3]= q4;

	if(align_active) {
		/* align to active object */
		Object *obact= CTX_data_active_object(C);
		if (obact==NULL) {
			/* no active object, ignore this option */
			align_active= FALSE;
		}
		else {
			float obact_quat[4];
			float twmat[3][3];

			/* same as transform manipulator when normal is set */
			ED_getTransformOrientationMatrix(C, twmat, FALSE);

			mat3_to_quat( obact_quat,twmat);
			invert_qt(obact_quat);
			mul_qt_qtqt(new_quat, new_quat, obact_quat);

			rv3d->view= view= 0;
		}
	}

	if(align_active==FALSE) {
		/* normal operation */
		if(rv3d->viewlock) {
			/* only pass on if */
			if(rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
			else if(rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
			else if(rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
			else if(rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
			else if(rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
			else if(rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
			else return;
		}

		rv3d->view= view;
	}

	if(rv3d->viewlock) {
		ED_region_tag_redraw(CTX_wm_region(C));
		return;
	}

	if (rv3d->persp==RV3D_CAMOB && v3d->camera) {

		if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
		else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;

		smooth_view(C, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
	}
	else {

		if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
		else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;

		smooth_view(C, NULL, NULL, NULL, new_quat, NULL, NULL);
	}

}

static int viewnumpad_exec(bContext *C, wmOperator *op)
{
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	Scene *scene= CTX_data_scene(C);
	static int perspo=RV3D_PERSP;
	int viewnum, align_active, nextperspo;

	viewnum = RNA_enum_get(op->ptr, "type");
	align_active = RNA_boolean_get(op->ptr, "align_active");

	/* set this to zero, gets handled in axis_set_view */
	if(rv3d->viewlock)
		align_active= 0;

	/* Use this to test if we started out with a camera */

	if (rv3d->persp == RV3D_CAMOB) {
		nextperspo= rv3d->lpersp;
	} else {
		nextperspo= perspo;
	}

	switch (viewnum) {
		case RV3D_VIEW_BOTTOM :
			axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_BACK:
			axis_set_view(C, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_LEFT:
			axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_TOP:
			axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_FRONT:
			axis_set_view(C, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_RIGHT:
			axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, nextperspo, align_active);
			break;

		case RV3D_VIEW_CAMERA:
			if(rv3d->viewlock==0) {
				/* lastview -  */

				if(rv3d->persp != RV3D_CAMOB) {
					Object *ob= OBACT;

					if (!rv3d->smooth_timer) {
						/* store settings of current view before allowing overwriting with camera view
						 * only if we're not currently in a view transition */
						copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
						rv3d->lview= rv3d->view;
						rv3d->lpersp= rv3d->persp;
					}

	#if 0
					if(G.qual==LR_ALTKEY) {
						if(oldcamera && is_an_active_object(oldcamera)) {
							v3d->camera= oldcamera;
						}
						handle_view3d_lock();
					}
	#endif
					
					/* first get the default camera for the view lock type */
					if(v3d->scenelock) {
						/* sets the camera view if available */
						v3d->camera= scene->camera;						
					}
					else {
						/* use scene camera if one is not set (even though we're unlocked) */
						if(v3d->camera==NULL) {
							v3d->camera= scene->camera;
						}
					}

					/* if the camera isnt found, check a number of options */
					if(v3d->camera==NULL && ob && ob->type==OB_CAMERA)
						v3d->camera= ob;
					
					if(v3d->camera==NULL)
						v3d->camera= scene_find_camera(scene);		

					/* couldnt find any useful camera, bail out */
					if(v3d->camera==NULL)
						return OPERATOR_CANCELLED;
					
					/* important these dont get out of sync for locked scenes */
					if(v3d->scenelock)
						scene->camera= v3d->camera;

					/* finally do snazzy view zooming */
					rv3d->persp= RV3D_CAMOB;
					smooth_view(C, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);

				}
				else{
					/* return to settings of last view */
					/* does smooth_view too */
					axis_set_view(C, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp, 0);
				}
			}
			break;

		default :
			break;
	}

	if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;

	return OPERATOR_FINISHED;
}


void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View numpad";
	ot->description = "Set the view";
	ot->idname= "VIEW3D_OT_viewnumpad";

	/* api callbacks */
	ot->exec= viewnumpad_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;

	RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
	RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active objects axis");
}

static EnumPropertyItem prop_view_orbit_items[] = {
	{V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
	{V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
	{V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
	{V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
	{0, NULL, 0, NULL, NULL}};

static int vieworbit_exec(bContext *C, wmOperator *op)
{
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	float phi, q1[4], new_quat[4];
	int orbitdir;

	orbitdir = RNA_enum_get(op->ptr, "type");

	if(rv3d->viewlock==0) {

		if(rv3d->persp != RV3D_CAMOB) {
			if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
				float si;
				/* z-axis */
				phi= (float)(M_PI/360.0)*U.pad_rot_angle;
				if(orbitdir == V3D_VIEW_STEPRIGHT) phi= -phi;
				si= (float)sin(phi);
				q1[0]= (float)cos(phi);
				q1[1]= q1[2]= 0.0;
				q1[3]= si;
				mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
				rv3d->view= 0;
			}
			else if(orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
				/* horizontal axis */
				copy_v3_v3(q1+1, rv3d->viewinv[0]);

				normalize_v3(q1+1);
				phi= (float)(M_PI/360.0)*U.pad_rot_angle;
				if(orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
				q1[0]= (float)cos(phi);
				mul_v3_fl(q1+1, sin(phi));
				mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
				rv3d->view= 0;
			}

			smooth_view(C, NULL, NULL, NULL, new_quat, NULL, NULL);
		}
	}

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_view_orbit(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View Orbit";
	ot->description = "Orbit the view";
	ot->idname= "VIEW3D_OT_view_orbit";

	/* api callbacks */
	ot->exec= vieworbit_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;
	RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
}

static EnumPropertyItem prop_view_pan_items[] = {
	{V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
	{V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
	{V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
	{V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
	{0, NULL, 0, NULL, NULL}};

static int viewpan_exec(bContext *C, wmOperator *op)
{
	ARegion *ar= CTX_wm_region(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	float vec[3];
	int pandir;

	pandir = RNA_enum_get(op->ptr, "type");

	initgrabz(rv3d, 0.0, 0.0, 0.0);
	if(pandir == V3D_VIEW_PANRIGHT) window_to_3d_delta(ar, vec, -32, 0);
	else if(pandir == V3D_VIEW_PANLEFT) window_to_3d_delta(ar, vec, 32, 0);
	else if(pandir == V3D_VIEW_PANUP) window_to_3d_delta(ar, vec, 0, -25);
	else if(pandir == V3D_VIEW_PANDOWN) window_to_3d_delta(ar, vec, 0, 25);
	add_v3_v3(rv3d->ofs, vec);

	if(rv3d->viewlock & RV3D_BOXVIEW)
		view3d_boxview_sync(CTX_wm_area(C), ar);

	ED_region_tag_redraw(ar);

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_view_pan(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View Pan";
	ot->description = "Pan the view";
	ot->idname= "VIEW3D_OT_view_pan";

	/* api callbacks */
	ot->exec= viewpan_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;
	RNA_def_enum(ot->srna, "type", prop_view_pan_items, 0, "Pan", "Direction of View Pan");
}

static int viewpersportho_exec(bContext *C, wmOperator *UNUSED(op))
{
	ARegion *ar= CTX_wm_region(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);

	if(rv3d->viewlock==0) {
		if(rv3d->persp!=RV3D_ORTHO)
			rv3d->persp=RV3D_ORTHO;
		else rv3d->persp=RV3D_PERSP;
		ED_region_tag_redraw(ar);
	}

	return OPERATOR_FINISHED;

}

void VIEW3D_OT_view_persportho(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "View Persp/Ortho";
	ot->description = "Switch the current view from perspective/orthographic";
	ot->idname= "VIEW3D_OT_view_persportho";

	/* api callbacks */
	ot->exec= viewpersportho_exec;
	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;
}

/* ******************** add background image operator **************** */

static BGpic *background_image_add(bContext *C)
{
	View3D *v3d= CTX_wm_view3d(C);
	
	BGpic *bgpic= MEM_callocN(sizeof(BGpic), "Background Image");
	bgpic->size= 5.0;
	bgpic->blend= 0.5;
	bgpic->iuser.fie_ima= 2;
	bgpic->iuser.ok= 1;
	bgpic->view= 0; /* 0 for all */
	
	BLI_addtail(&v3d->bgpicbase, bgpic);
	
	return bgpic;
}

static int background_image_add_exec(bContext *C, wmOperator *UNUSED(op))
{
	background_image_add(C);

	return OPERATOR_FINISHED;
}

static int background_image_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	View3D *v3d= CTX_wm_view3d(C);
	Image *ima= NULL;
	BGpic *bgpic;
	char name[32];
	
	/* check input variables */
	if(RNA_property_is_set(op->ptr, "filepath")) {
		char path[FILE_MAX];
		
		RNA_string_get(op->ptr, "filepath", path);
		ima= BKE_add_image_file(path);
	}
	else if(RNA_property_is_set(op->ptr, "name")) {
		RNA_string_get(op->ptr, "name", name);
		ima= (Image *)find_id("IM", name);
	}
	
	bgpic = background_image_add(C);
	
	if (ima) {
		bgpic->ima = ima;
		
		if(ima->id.us==0) id_us_plus(&ima->id);
		else id_lib_extern(&ima->id);
		
		if (!(v3d->flag & V3D_DISPBGPICS))
			v3d->flag |= V3D_DISPBGPICS;
	}
	
	WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
	
	return OPERATOR_FINISHED;
}

void VIEW3D_OT_background_image_add(wmOperatorType *ot)
{
	/* identifiers */
	ot->name   = "Add Background Image";
	ot->description= "Add a new background image";
	ot->idname = "VIEW3D_OT_background_image_add";

	/* api callbacks */
	ot->invoke = background_image_add_invoke;
	ot->exec   = background_image_add_exec;
	ot->poll   = ED_operator_view3d_active;

	/* flags */
	ot->flag   = 0;
	
	/* properties */
	RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
	RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
}


/* ***** remove image operator ******* */
static int background_image_remove_exec(bContext *C, wmOperator *op)
{
	View3D *vd = CTX_wm_view3d(C);
	int index = RNA_int_get(op->ptr, "index");
	BGpic *bgpic_rem= BLI_findlink(&vd->bgpicbase, index);

	if(bgpic_rem) {
		BLI_remlink(&vd->bgpicbase, bgpic_rem);
		if(bgpic_rem->ima) bgpic_rem->ima->id.us--;
		MEM_freeN(bgpic_rem);
		WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, vd);
		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}

}

void VIEW3D_OT_background_image_remove(wmOperatorType *ot)
{
	/* identifiers */
	ot->name   = "Remove Background Image";
	ot->description= "Remove a background image from the 3D view";
	ot->idname = "VIEW3D_OT_background_image_remove";

	/* api callbacks */
	ot->exec   = background_image_remove_exec;
	ot->poll   = ED_operator_view3d_active;

	/* flags */
	ot->flag   = 0;

	RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove ", 0, INT_MAX);
}

/* ********************* set clipping operator ****************** */

static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
{
	int val;

	for(val=0; val<4; val++) {

		normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);

		clip[val][3]=
			- clip[val][0]*clipbb->vec[val][0]
			- clip[val][1]*clipbb->vec[val][1]
			- clip[val][2]*clipbb->vec[val][2];
	}
}

static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
{
	BoundBox clipbb_local;
	float imat[4][4];
	int i;

	invert_m4_m4(imat, mat);

	for(i=0; i<8; i++) {
		mul_v3_m4v3(clipbb_local.vec[i], imat, clipbb->vec[i]);
	}

	calc_clipping_plane(clip_local, &clipbb_local);
}

void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4])
{
	if(rv3d->rflag & RV3D_CLIPPING)
		calc_local_clipping(rv3d->clip_local, rv3d->clipbb, mat);
}

static int view3d_clipping_exec(bContext *C, wmOperator *op)
{
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	ViewContext vc;
	bglMats mats;
	rcti rect;

	rect.xmin= RNA_int_get(op->ptr, "xmin");
	rect.ymin= RNA_int_get(op->ptr, "ymin");
	rect.xmax= RNA_int_get(op->ptr, "xmax");
	rect.ymax= RNA_int_get(op->ptr, "ymax");

	rv3d->rflag |= RV3D_CLIPPING;
	rv3d->clipbb= MEM_callocN(sizeof(BoundBox), "clipbb");

	/* note; otherwise opengl won't work */
	view3d_operator_needs_opengl(C);

	view3d_set_viewcontext(C, &vc);
	view3d_get_transformation(vc.ar, vc.rv3d, NULL, &mats); /* NULL because we don't want it in object space */
	view3d_calculate_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect);

	return OPERATOR_FINISHED;
}

static int view3d_clipping_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	ARegion *ar= CTX_wm_region(C);

	if(rv3d->rflag & RV3D_CLIPPING) {
		rv3d->rflag &= ~RV3D_CLIPPING;
		ED_region_tag_redraw(ar);
		if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
		rv3d->clipbb= NULL;
		return OPERATOR_FINISHED;
	}
	else {
		return WM_border_select_invoke(C, op, event);
	}
}

/* toggles */
void VIEW3D_OT_clip_border(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "Clipping Border";
	ot->description = "Set the view clipping border";
	ot->idname= "VIEW3D_OT_clip_border";

	/* api callbacks */
	ot->invoke= view3d_clipping_invoke;
	ot->exec= view3d_clipping_exec;
	ot->modal= WM_border_select_modal;

	ot->poll= ED_operator_region_view3d_active;

	/* flags */
	ot->flag= 0;

	/* rna */
	RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
}

/* ***************** 3d cursor cursor op ******************* */

/* mx my in region coords */
static int set_3dcursor_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
{
	Scene *scene= CTX_data_scene(C);
	ARegion *ar= CTX_wm_region(C);
	View3D *v3d = CTX_wm_view3d(C);
	RegionView3D *rv3d= CTX_wm_region_view3d(C);
	float dx, dy, fz, *fp = NULL, dvec[3], oldcurs[3];
	short mx, my, mval[2];
//	short ctrl= 0; // XXX
	int flip;
	fp= give_cursor(scene, v3d);

//	if(obedit && ctrl) lr_click= 1;
	copy_v3_v3(oldcurs, fp);

	mx= event->x - ar->winrct.xmin;
	my= event->y - ar->winrct.ymin;

	project_short_noclip(ar, fp, mval);
	flip= initgrabz(rv3d, fp[0], fp[1], fp[2]);
	
	/* reset the depth based on the view offset */
	if(flip) {
		negate_v3_v3(fp, rv3d->ofs);

		/* re initialize */
		project_short_noclip(ar, fp, mval);
		flip= initgrabz(rv3d, fp[0], fp[1], fp[2]);
	}

	if(mval[0]!=IS_CLIPPED) {
		short depth_used = 0;

		if (U.uiflag & USER_ORBIT_ZBUF) { /* maybe this should be accessed some other way */
			short mval_depth[2];
			mval_depth[0]= mx;
			mval_depth[1]= my;
			view3d_operator_needs_opengl(C);
			if (view_autodist(scene, ar, v3d, mval_depth, fp))
				depth_used= 1;
		}

		if(depth_used==0) {
			window_to_3d_delta(ar, dvec, mval[0]-mx, mval[1]-my);
			sub_v3_v3(fp, dvec);
		}
	}
	else {

		dx= ((float)(mx-(ar->winx/2)))*rv3d->zfac/(ar->winx/2);
		dy= ((float)(my-(ar->winy/2)))*rv3d->zfac/(ar->winy/2);

		fz= rv3d->persmat[0][3]*fp[0]+ rv3d->persmat[1][3]*fp[1]+ rv3d->persmat[2][3]*fp[2]+ rv3d->persmat[3][3];
		fz= fz/rv3d->zfac;

		fp[0]= (rv3d->persinv[0][0]*dx + rv3d->persinv[1][0]*dy+ rv3d->persinv[2][0]*fz)-rv3d->ofs[0];
		fp[1]= (rv3d->persinv[0][1]*dx + rv3d->persinv[1][1]*dy+ rv3d->persinv[2][1]*fz)-rv3d->ofs[1];
		fp[2]= (rv3d->persinv[0][2]*dx + rv3d->persinv[1][2]*dy+ rv3d->persinv[2][2]*fz)-rv3d->ofs[2];
	}

	if(v3d && v3d->localvd)
		WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
	else
		WM_event_add_notifier(C, NC_SCENE|NA_EDITED, scene);

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_cursor3d(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "Set 3D Cursor";
	ot->description = "Set the location of the 3D cursor";
	ot->idname= "VIEW3D_OT_cursor3d";

	/* api callbacks */
	ot->invoke= set_3dcursor_invoke;

	ot->poll= ED_operator_view3d_active;

	/* flags */
//	ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;

	/* rna later */

}

/* ***************** manipulator op ******************* */


static int manipulator_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	View3D *v3d = CTX_wm_view3d(C);

	if(!(v3d->twflag & V3D_USE_MANIPULATOR)) return OPERATOR_PASS_THROUGH;
	if(!(v3d->twflag & V3D_DRAW_MANIPULATOR)) return OPERATOR_PASS_THROUGH;

	/* only no modifier or shift */
	if(event->keymodifier != 0 && event->keymodifier != KM_SHIFT) return OPERATOR_PASS_THROUGH;

	/* note; otherwise opengl won't work */
	view3d_operator_needs_opengl(C);

	if(0==BIF_do_manipulator(C, event, op))
		return OPERATOR_PASS_THROUGH;

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_manipulator(wmOperatorType *ot)
{

	/* identifiers */
	ot->name= "3D Manipulator";
	ot->description = "Manipulate selected item by axis";
	ot->idname= "VIEW3D_OT_manipulator";

	/* api callbacks */
	ot->invoke= manipulator_invoke;

	ot->poll= ED_operator_view3d_active;

	/* properties to pass to transform */
	Transform_Properties(ot, P_CONSTRAINT);
}

static int enable_manipulator_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
{
	View3D *v3d = CTX_wm_view3d(C);

	v3d->twtype=0;
	
	if (RNA_boolean_get(op->ptr, "translate"))
		v3d->twtype |= V3D_MANIP_TRANSLATE;
	if (RNA_boolean_get(op->ptr, "rotate"))
		v3d->twtype |= V3D_MANIP_ROTATE;
	if (RNA_boolean_get(op->ptr, "scale"))
		v3d->twtype |= V3D_MANIP_SCALE;
		
	WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);

	return OPERATOR_FINISHED;
}

void VIEW3D_OT_enable_manipulator(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Enable 3D Manipulator";
	ot->description = "Enable the transform manipulator for use";
	ot->idname= "VIEW3D_OT_enable_manipulator";
	
	/* api callbacks */
	ot->invoke= enable_manipulator_invoke;
	ot->poll= ED_operator_view3d_active;
	
	/* rna later */
	RNA_def_boolean(ot->srna, "translate", 0, "Translate", "Enable the translate manipulator");
	RNA_def_boolean(ot->srna, "rotate", 0, "Rotate", "Enable the rotate manipulator");
	RNA_def_boolean(ot->srna, "scale", 0, "Scale", "Enable the scale manipulator");
}

/* ************************* below the line! *********************** */


static float view_autodist_depth_margin(ARegion *ar, short mval[2], int margin)
{
	ViewDepths depth_temp= {0};
	rcti rect;
	float depth_close;

	if(margin==0) {
		/* Get Z Depths, needed for perspective, nice for ortho */
		rect.xmin= mval[0];
		rect.ymin= mval[1];
		rect.xmax= mval[0] + 1;
		rect.ymax= mval[1] + 1;
	}
	else {
		rect.xmax = mval[0] + margin;
		rect.ymax = mval[1] + margin;

		rect.xmin = mval[0] - margin;
		rect.ymin = mval[1] - margin;
	}

	view3d_update_depths_rect(ar, &depth_temp, &rect);
	depth_close= view3d_depth_near(&depth_temp);
	if(depth_temp.depths) MEM_freeN(depth_temp.depths);
	return depth_close;
}

/* XXX todo Zooms in on a border drawn by the user */
int view_autodist(Scene *scene, ARegion *ar, View3D *v3d, short *mval, float mouse_worldloc[3] ) //, float *autodist )
{
	bglMats mats; /* ZBuffer depth vars */
	float depth_close= FLT_MAX;
	double cent[2],  p[3];

	/* Get Z Depths, needed for perspective, nice for ortho */
	bgl_get_mats(&mats);
	draw_depth(scene, ar, v3d, NULL);

	depth_close= view_autodist_depth_margin(ar, mval, 4);

	if (depth_close==FLT_MAX)
		return 0;

	cent[0] = (double)mval[0];
	cent[1] = (double)mval[1];

	if (!gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2]))
		return 0;

	mouse_worldloc[0] = (float)p[0];
	mouse_worldloc[1] = (float)p[1];
	mouse_worldloc[2] = (float)p[2];
	return 1;
}

int view_autodist_init(Scene *scene, ARegion *ar, View3D *v3d, int mode) //, float *autodist )
{
	/* Get Z Depths, needed for perspective, nice for ortho */
	switch(mode) {
	case 0:
		draw_depth(scene, ar, v3d, NULL);
		break;
	case 1:
		draw_depth_gpencil(scene, ar, v3d);
		break;
	}

	return 1;
}

// no 4x4 sampling, run view_autodist_init first
int view_autodist_simple(ARegion *ar, short *mval, float mouse_worldloc[3], int margin, float *force_depth) //, float *autodist )
{
	bglMats mats; /* ZBuffer depth vars, could cache? */
	float depth;
	double cent[2],  p[3];

	/* Get Z Depths, needed for perspective, nice for ortho */
	if(force_depth)
		depth= *force_depth;
	else
		depth= view_autodist_depth_margin(ar, mval, margin);

	if (depth==FLT_MAX)
		return 0;

	cent[0] = (double)mval[0];
	cent[1] = (double)mval[1];

	bgl_get_mats(&mats);
	if (!gluUnProject(cent[0], cent[1], depth, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2]))
		return 0;

	mouse_worldloc[0] = (float)p[0];
	mouse_worldloc[1] = (float)p[1];
	mouse_worldloc[2] = (float)p[2];
	return 1;
}

int view_autodist_depth(struct ARegion *ar, short mval[2], int margin, float *depth)
{
	*depth= view_autodist_depth_margin(ar, mval, margin);

	return (*depth==FLT_MAX) ? 0:1;
}

static int depth_segment_cb(int x, int y, void *userData)
{
	struct { struct ARegion *ar; int margin; float depth; } *data = userData;
	short mval[2];
	float depth;

	mval[0]= (short)x;
	mval[1]= (short)y;

	depth= view_autodist_depth_margin(data->ar, mval, data->margin);

	if(depth != FLT_MAX) {
		data->depth= depth;
		return 0;
	}
	else {
		return 1;
	}
}

int view_autodist_depth_segment(struct ARegion *ar, short mval_sta[2], short mval_end[2], int margin, float *depth)
{
	struct { struct ARegion *ar; int margin; float depth; } data = {NULL};
	int p1[2];
	int p2[2];

	data.ar= ar;
	data.margin= margin;
	data.depth= FLT_MAX;

	VECCOPY2D(p1, mval_sta);
	VECCOPY2D(p2, mval_end);

	plot_line_v2v2i(p1, p2, depth_segment_cb, &data);

	*depth= data.depth;

	return (*depth==FLT_MAX) ? 0:1;
}

/* ********************* NDOF ************************ */
/* note: this code is confusing and unclear... (ton) */
/* **************************************************** */

// ndof scaling will be moved to user setting.
// In the mean time this is just a place holder.

// Note: scaling in the plugin and ghostwinlay.c
// should be removed. With driver default setting,
// each axis returns approx. +-200 max deflection.

// The values I selected are based on the older
// polling i/f. With event i/f, the sensistivity
// can be increased for improved response from
// small deflections of the device input.


// lukep notes : i disagree on the range.
// the normal 3Dconnection driver give +/-400
// on defaut range in other applications
// and up to +/- 1000 if set to maximum
// because i remove the scaling by delta,
// which was a bad idea as it depend of the system
// speed and os, i changed the scaling values, but
// those are still not ok

#if 0
static float ndof_axis_scale[6] = {
	+0.01,	// Tx
	+0.01,	// Tz
	+0.01,	// Ty
	+0.0015,	// Rx
	+0.0015,	// Rz
	+0.0015	// Ry
};

static void filterNDOFvalues(float *sbval)
{
	int i=0;
	float max  = 0.0;

	for (i =0; i<6;i++)
		if (fabs(sbval[i]) > max)
			max = fabs(sbval[i]);
	for (i =0; i<6;i++)
		if (fabs(sbval[i]) != max )
			sbval[i]=0.0;
}

// statics for controlling rv3d->dist corrections.
// viewmoveNDOF zeros and adjusts rv3d->ofs.
// viewmove restores based on dz_flag state.

int dz_flag = 0;
float m_dist;

void viewmoveNDOFfly(ARegion *ar, View3D *v3d, int UNUSED(mode))
{
	RegionView3D *rv3d= ar->regiondata;
	int i;
	float phi;
	float dval[7];
	// static fval[6] for low pass filter; device input vector is dval[6]
	static float fval[6];
	float tvec[3],rvec[3];
	float q1[4];
	float mat[3][3];
	float upvec[3];


	/*----------------------------------------------------
	 * sometimes this routine is called from headerbuttons
	 * viewmove needs to refresh the screen
	 */
// XXX	areawinset(ar->win);


	// fetch the current state of the ndof device
// XXX	getndof(dval);

	if (v3d->ndoffilter)
		filterNDOFvalues(fval);

	// Scale input values

//	if(dval[6] == 0) return; // guard against divide by zero

	for(i=0;i<6;i++) {

		// user scaling
		dval[i] = dval[i] * ndof_axis_scale[i];
	}


	// low pass filter with zero crossing reset

	for(i=0;i<6;i++) {
		if((dval[i] * fval[i]) >= 0)
			dval[i] = (fval[i] * 15 + dval[i]) / 16;
		else
			fval[i] = 0;
	}


	// force perspective mode. This is a hack and is
	// incomplete. It doesn't actually effect the view
	// until the first draw and doesn't update the menu
	// to reflect persp mode.

	rv3d->persp = RV3D_PERSP;


	// Correct the distance jump if rv3d->dist != 0

	// This is due to a side effect of the original
	// mouse view rotation code. The rotation point is
	// set a distance in front of the viewport to
	// make rotating with the mouse look better.
	// The distance effect is written at a low level
	// in the view management instead of the mouse
	// view function. This means that all other view
	// movement devices must subtract this from their
	// view transformations.

	if(rv3d->dist != 0.0) {
		dz_flag = 1;
		m_dist = rv3d->dist;
		upvec[0] = upvec[1] = 0;
		upvec[2] = rv3d->dist;
		copy_m3_m4(mat, rv3d->viewinv);
		mul_m3_v3(mat, upvec);
		sub_v3_v3(rv3d->ofs, upvec);
		rv3d->dist = 0.0;
	}


	// Apply rotation
	// Rotations feel relatively faster than translations only in fly mode, so
	// we have no choice but to fix that here (not in the plugins)
	rvec[0] = -0.5 * dval[3];
	rvec[1] = -0.5 * dval[4];
	rvec[2] = -0.5 * dval[5];

	// rotate device x and y by view z

	copy_m3_m4(mat, rv3d->viewinv);
	mat[2][2] = 0.0f;
	mul_m3_v3(mat, rvec);

	// rotate the view

	phi = normalize_v3(rvec);
	if(phi != 0) {
		axis_angle_to_quat(q1,rvec,phi);
		mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
	}


	// Apply translation

	tvec[0] = dval[0];
	tvec[1] = dval[1];
	tvec[2] = -dval[2];

	// the next three lines rotate the x and y translation coordinates
	// by the current z axis angle

	copy_m3_m4(mat, rv3d->viewinv);
	mat[2][2] = 0.0f;
	mul_m3_v3(mat, tvec);

	// translate the view

	sub_v3_v3(rv3d->ofs, tvec);


	/*----------------------------------------------------
	 * refresh the screen XXX
	  */

	// update render preview window

// XXX	BIF_view3d_previewrender_signal(ar, PR_DBASE|PR_DISPRECT);
}

void viewmoveNDOF(Scene *scene, ARegion *ar, View3D *v3d, int UNUSED(mode))
{
	RegionView3D *rv3d= ar->regiondata;
	float fval[7];
	float dvec[3];
	float sbadjust = 1.0f;
	float len;
	short use_sel = 0;
	Object *ob = OBACT;
	float m[3][3];
	float m_inv[3][3];
	float xvec[3] = {1,0,0};
	float yvec[3] = {0,-1,0};
	float zvec[3] = {0,0,1};
	float phi;
	float q1[4];
	float obofs[3];
	float reverse;
	//float diff[4];
	float d, curareaX, curareaY;
	float mat[3][3];
	float upvec[3];

	/* Sensitivity will control how fast the view rotates.  The value was
	 * obtained experimentally by tweaking until the author didn't get dizzy watching.
	 * Perhaps this should be a configurable user parameter.
	 */
	float psens = 0.005f * (float) U.ndof_pan;   /* pan sensitivity */
	float rsens = 0.005f * (float) U.ndof_rotate;  /* rotate sensitivity */
	float zsens = 0.3f;   /* zoom sensitivity */

	const float minZoom = -30.0f;
	const float maxZoom = 300.0f;

	//reset view type
	rv3d->view = 0;
//printf("passing here \n");
//
	if (scene->obedit==NULL && ob && !(ob->mode & OB_MODE_POSE)) {
		use_sel = 1;
	}

	if((dz_flag)||rv3d->dist==0) {
		dz_flag = 0;
		rv3d->dist = m_dist;
		upvec[0] = upvec[1] = 0;
		upvec[2] = rv3d->dist;
		copy_m3_m4(mat, rv3d->viewinv);
		mul_m3_v3(mat, upvec);
		add_v3_v3(rv3d->ofs, upvec);
	}

	/*----------------------------------------------------
	 * sometimes this routine is called from headerbuttons
	 * viewmove needs to refresh the screen
	 */
// XXX	areawinset(curarea->win);

	/*----------------------------------------------------
	 * record how much time has passed. clamp at 10 Hz
	 * pretend the previous frame occurred at the clamped time
	 */
//    now = PIL_check_seconds_timer();
 //   frametime = (now - prevTime);
 //   if (frametime > 0.1f){        /* if more than 1/10s */
 //       frametime = 1.0f/60.0;      /* clamp at 1/60s so no jumps when starting to move */
//    }
//    prevTime = now;
 //   sbadjust *= 60 * frametime;             /* normalize ndof device adjustments to 100Hz for framerate independence */

	/* fetch the current state of the ndof device & enforce dominant mode if selected */
// XXX    getndof(fval);
	if (v3d->ndoffilter)
		filterNDOFvalues(fval);


	// put scaling back here, was previously in ghostwinlay
	fval[0] = fval[0] * (1.0f/600.0f);
	fval[1] = fval[1] * (1.0f/600.0f);
	fval[2] = fval[2] * (1.0f/1100.0f);
	fval[3] = fval[3] * 0.00005f;
	fval[4] =-fval[4] * 0.00005f;
	fval[5] = fval[5] * 0.00005f;
	fval[6] = fval[6] / 1000000.0f;

	// scale more if not in perspective mode
	if (rv3d->persp == RV3D_ORTHO) {
		fval[0] = fval[0] * 0.05f;
		fval[1] = fval[1] * 0.05f;
		fval[2] = fval[2] * 0.05f;
		fval[3] = fval[3] * 0.9f;
		fval[4] = fval[4] * 0.9f;
		fval[5] = fval[5] * 0.9f;
		zsens *= 8;
	}

	/* set object offset */
	if (ob) {
		obofs[0] = -ob->obmat[3][0];
		obofs[1] = -ob->obmat[3][1];
		obofs[2] = -ob->obmat[3][2];
	}
	else {
		copy_v3_v3(obofs, rv3d->ofs);
	}

	/* calc an adjustment based on distance from camera
	   disabled per patch 14402 */
	 d = 1.0f;

/*    if (ob) {
		sub_v3_v3v3(diff, obofs, rv3d->ofs);
		d = len_v3(diff);
	}
*/

	reverse = (rv3d->persmat[2][1] < 0.0f) ? -1.0f : 1.0f;

	/*----------------------------------------------------
	 * ndof device pan
	 */
	psens *= 1.0f + d;
	curareaX = sbadjust * psens * fval[0];
	curareaY = sbadjust * psens * fval[1];
	dvec[0] = curareaX * rv3d->persinv[0][0] + curareaY * rv3d->persinv[1][0];
	dvec[1] = curareaX * rv3d->persinv[0][1] + curareaY * rv3d->persinv[1][1];
	dvec[2] = curareaX * rv3d->persinv[0][2] + curareaY * rv3d->persinv[1][2];
	add_v3_v3(rv3d->ofs, dvec);

	/*----------------------------------------------------
	 * ndof device dolly
	 */
	len = zsens * sbadjust * fval[2];

	if (rv3d->persp==RV3D_CAMOB) {
		if(rv3d->persp==RV3D_CAMOB) { /* This is stupid, please fix - TODO */
			rv3d->camzoom+= 10.0f * -len;
		}
		if (rv3d->camzoom < minZoom) rv3d->camzoom = minZoom;
		else if (rv3d->camzoom > maxZoom) rv3d->camzoom = maxZoom;
	}
	else if ((rv3d->dist> 0.001*v3d->grid) && (rv3d->dist<10.0*v3d->far)) {
		rv3d->dist*=(1.0 + len);
	}


	/*----------------------------------------------------
	 * ndof device turntable
	 * derived from the turntable code in viewmove
	 */

	/* Get the 3x3 matrix and its inverse from the quaternion */
	quat_to_mat3( m,rv3d->viewquat);
	invert_m3_m3(m_inv,m);

	/* Determine the direction of the x vector (for rotating up and down) */
	/* This can likely be compuated directly from the quaternion. */
	mul_m3_v3(m_inv,xvec);
	mul_m3_v3(m_inv,yvec);
	mul_m3_v3(m_inv,zvec);

	/* Perform the up/down rotation */
	phi = sbadjust * rsens * /*0.5f * */ fval[3]; /* spin vertically half as fast as horizontally */
	q1[0] = cos(phi);
	mul_v3_v3fl(q1+1, xvec, sin(phi));
	mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);

	if (use_sel) {
		conjugate_qt(q1); /* conj == inv for unit quat */
		sub_v3_v3(rv3d->ofs, obofs);
		mul_qt_v3(q1, rv3d->ofs);
		add_v3_v3(rv3d->ofs, obofs);
	}

	/* Perform the orbital rotation */
	/* Perform the orbital rotation
	   If the seen Up axis is parallel to the zoom axis, rotation should be
	   achieved with a pure Roll motion (no Spin) on the device. When you start
	   to tilt, moving from Top to Side view, Spinning will increasingly become
	   more relevant while the Roll component will decrease. When a full
	   Side view is reached, rotations around the world's Up axis are achieved
	   with a pure Spin-only motion.  In other words the control of the spinning
	   around the world's Up axis should move from the device's Spin axis to the
	   device's Roll axis depending on the orientation of the world's Up axis
	   relative to the screen. */
	//phi = sbadjust * rsens * reverse * fval[4];  /* spin the knob, y axis */
	phi = sbadjust * rsens * (yvec[2] * fval[4] + zvec[2] * fval[5]);
	q1[0] = cos(phi);
	q1[1] = q1[2] = 0.0;
	q1[3] = sin(phi);
	mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);

	if (use_sel) {
		conjugate_qt(q1);
		sub_v3_v3(rv3d->ofs, obofs);
		mul_qt_v3(q1, rv3d->ofs);
		add_v3_v3(rv3d->ofs, obofs);
	}

	/*----------------------------------------------------
	 * refresh the screen
	 */
// XXX    scrarea_do_windraw(curarea);
}
#endif // if 0, unused NDof code

/* give a 4x4 matrix from a perspective view, only needs viewquat, ofs and dist
 * basically the same as...
 *     rv3d->persp= RV3D_PERSP
 *     setviewmatrixview3d(scene, v3d, rv3d);
 *     setcameratoview3d(v3d, rv3d, v3d->camera);
 * ...but less of a hassle
 * */
void view3d_persp_mat4(RegionView3D *rv3d, float mat[][4])
{
	float qt[4], dvec[3];
	copy_qt_qt(qt, rv3d->viewquat);
	qt[0]= -qt[0];
	quat_to_mat4(mat, qt);
	mat[3][2] -= rv3d->dist;
	translate_m4(mat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
	mul_v3_v3fl(dvec, mat[2], -rv3d->dist);
	sub_v3_v3v3(mat[3], dvec, rv3d->ofs);
}