File: SoDragger.c%2B%2B

package info (click to toggle)
inventor 2.1.5-10-21
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,312 kB
  • sloc: ansic: 33,864; lisp: 7,361; cpp: 3,874; yacc: 369; sh: 359; perl: 234; awk: 141; makefile: 81; csh: 35; sed: 11
file content (2053 lines) | stat: -rw-r--r-- 62,901 bytes parent folder | download | duplicates (10)
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
/*
 *
 *  Copyright (C) 2000 Silicon Graphics, Inc.  All Rights Reserved. 
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  Further, this software is distributed without any warranty that it is
 *  free of the rightful claim of any third person regarding infringement
 *  or the like.  Any license provided herein, whether implied or
 *  otherwise, applies only to this software file.  Patent licenses, if
 *  any, provided herein do not apply to combinations of this program with
 *  other software, or any other product whatsoever.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 *  Mountain View, CA  94043, or:
 * 
 *  http://www.sgi.com 
 * 
 *  For further information regarding this notice, see: 
 * 
 *  http://oss.sgi.com/projects/GenInfo/NoticeExplan/
 *
 */

/*

 * Copyright (C) 1990,91   Silicon Graphics, Inc.
 *
 _______________________________________________________________________
 ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
 |
 |   $Revision: 1.2 $
 |
 |   Classes:
 |	SoDragger
 |
 |   Author(s): Paul Isaacs, David Mott, Howard Look
 |
 ______________  S I L I C O N   G R A P H I C S   I N C .  ____________
 _______________________________________________________________________
 */

#include <machine.h>
#include <stdio.h>

#include <Inventor/SbLinear.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoPickedPoint.h>
#include <Inventor/SoPath.h>

#include <Inventor/fields/SoSFRotation.h>
#include <Inventor/fields/SoSFVec3f.h>

#include <Inventor/errors/SoDebugError.h>
#include <Inventor/events/SoEvent.h>
#include <Inventor/events/SoKeyboardEvent.h>
#include <Inventor/events/SoLocation2Event.h>
#include <Inventor/events/SoMouseButtonEvent.h>

#include <Inventor/actions/SoHandleEventAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/actions/SoSearchAction.h>

#include <Inventor/nodes/SoCamera.h>
#include <Inventor/nodes/SoMatrixTransform.h>

#include <Inventor/misc/SoState.h>
#include <Inventor/misc/SoTempPath.h>
#include <Inventor/misc/SoAuditorList.h>
#include <Inventor/misc/SoChildList.h>
#include <Inventor/elements/SoViewVolumeElement.h>
#include <Inventor/elements/SoViewportRegionElement.h>

#include <Inventor/draggers/SoDragger.h>

// The smallest scale that any dragger will write. If the user attempts
// to go below this amount, the dragger will set it to this minimum.
// Default is .001
float SoDragger::minScale = .001;

SO_KIT_SOURCE(SoDragger);

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Constructor
//
SoDragger::SoDragger()
//
////////////////////////////////////////////////////////////////////////
{
    SO_KIT_CONSTRUCTOR(SoDragger);

    isBuiltIn = TRUE;

    SO_KIT_ADD_CATALOG_ENTRY(motionMatrix, SoMatrixTransform, FALSE, 
				topSeparator, geomSeparator,FALSE);

    // initialize field.
    SO_KIT_ADD_FIELD(isActive,(0));

    SO_KIT_INIT_INSTANCE();

    // init local variables
    startingWorldPoint = SbVec3f(0,0,0);

    // callback lists
    startCallbacks = new SoCallbackList;
    motionCallbacks = new SoCallbackList;
    finishCallbacks = new SoCallbackList;

    valueChangedCallbacks = new SoCallbackList;
    valueChangedCallbacksEnabled = TRUE;
    activeChildDragger = NULL;

    otherEventCallbacks = new SoCallbackList;

    // initialize tempPathToThis, pickPath
    tempPathToThis = NULL;
    tempPathNumKidsHack = NULL;
    pickPath = NULL;

    // initialize surrogatePick info.
    surrogateNameInPickOwner = "";
    pathToSurrogatePickOwner = NULL;
    surrogatePathInPickOwner = NULL;

    // The matrix cache starts as invalid
    cachedPathToThisValid = FALSE;
    cachedMotionMatrixValid = FALSE;

    // By default, do include this dragger in bounding box calculations.
    // This is temporarily overridden when the dragger itself applies
    // a bbox action so that it doesn't include itself.
    ignoreInBbox = FALSE;

    // Minimum amount to move before choosing a constraint based
    // on the user's gesture.
    minGesture = 8; // pixels

    setHandleEventAction( NULL );
    setCameraInfo( NULL );

    // By default, projectors for rotation will call setFront() based
    // on where the mouse went down: on the front or rear of the virtual sphere
    // or cylinder.
    setFrontOnProjector( USE_PICK );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Destructor
//
SoDragger::~SoDragger()
//
////////////////////////////////////////////////////////////////////////
{
    if ( pickPath != NULL )
	 pickPath->unref();

    // tempPaths actually get deleted, not unref'ed
    if ( tempPathToThis != NULL ) {
	delete tempPathToThis;
	tempPathToThis = NULL;
    }
    if ( tempPathNumKidsHack != NULL ) {
	delete tempPathNumKidsHack;
	tempPathNumKidsHack = NULL;
    }

    if ( activeChildDragger )
	activeChildDragger->unref();

    setNoPickedSurrogate();

    delete startCallbacks;
    delete motionCallbacks;
    delete finishCallbacks;

    delete valueChangedCallbacks;

    delete otherEventCallbacks;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This attempts to handle the passed event. It will call methods on
// the SoHandleEventAction if it wants to grab and if it handles the event.
//
// Use: public
//
void
SoDragger::handleEvent(SoHandleEventAction *ha)
//
////////////////////////////////////////////////////////////////////////
{
    // let base class traverse the children
    if ( ha->getGrabber() != this ) 
	SoBaseKit::handleEvent( ha );

    // if no children handled the event, we will!
    if ( ha->isHandled() )
	return;

    setHandleEventAction( ha );

    // get event and window size from the action
    const SoEvent *event = ha->getEvent();

    if (SO_MOUSE_PRESS_EVENT(event, BUTTON1)) {

	SbBool happyPath = FALSE;

	const SoPickedPoint *pp = ha->getPickedPoint();
	SoPath *pPath = ( pp != NULL ) ? pp->getPath() : NULL; 

	if ( pPath ) {

	    // Is our current traversal path part of the pickPath?
	    if ( pPath->containsPath(ha->getCurPath())) {
	        happyPath = TRUE;
	    }
	    // Is the path a surrogate path for us or any draggers 
	    // contained within us?
	    else {
		SoPath *pathToOwner, *surrogatePath;
		SbName  surrogateName;

		if (isPathSurrogateInMySubgraph( pPath, pathToOwner, 
					surrogateName, surrogatePath) ) {
		    pathToOwner->ref();
		    surrogatePath->ref();
		    if (shouldGrabBasedOnSurrogate( pPath, surrogatePath )) {
		        setPickedSurrogate( pathToOwner, surrogateName, 
					    surrogatePath );
		        happyPath = TRUE;
		    }
		    surrogatePath->unref();
		    pathToOwner->unref();
	        }
	    }
	}

	if ( happyPath ) {

	    setStartingPoint( pp );
		
	    // Since the pick path may be on a surrogate object, use
	    // the current action path to get a path to this node.
	    setTempPathToThis( ha->getCurPath() );
	    setCameraInfo( ha );

	    setPickPath( pPath );

	    ha->setGrabber(this);
	    ha->setHandled();
	}		    
	else 
	    otherEventCallbacks->invokeCallbacks(this);
    }
    else if ( event->isOfType(SoLocation2Event::getClassTypeId() ) && 
    	      ha->getGrabber() == this ) {

	    mouseMovedYet = TRUE;
	    motionCallbacks->invokeCallbacks(this);

	    ha->setHandled();
    }
    else if ( SO_MOUSE_RELEASE_EVENT(event, BUTTON1) &&
              ha->getGrabber() == this ) {

	    // Releasing the grabber will result in a call to 
	    // grabEventsCleanup(), which will do some important things...
	    ha->releaseGrabber();

	    if ( mouseMovedYet == TRUE ) {
		// If the mouse didn't move, then don't handle the event.
		// Let some other node make use of it (for example, the 
		// selection node might want to de-select what's inside
		// the dragger.
		ha->setHandled();
	    }
    }
    else
	otherEventCallbacks->invokeCallbacks(this);
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This virtual function is called when a dragger gains
//    status as "grabber" of events.
//
// Use: public
//
void
SoDragger::grabEventsSetup()
//
////////////////////////////////////////////////////////////////////////
{
    renderCaching = OFF;

    setStartLocaterPosition( getLocaterPosition() );
    saveStartParameters();

    mouseMovedYet = FALSE;

    isActive.setValue( TRUE );
    startCallbacks->invokeCallbacks(this);
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This virtual function is called when a dragger loses
//    status as "grabber" of events.
//
// Use: public
//
void
SoDragger::grabEventsCleanup()
//
////////////////////////////////////////////////////////////////////////
{
    isActive.setValue( FALSE );
    finishCallbacks->invokeCallbacks(this);

    setPickPath( NULL );
    setNoPickedSurrogate();

    renderCaching = AUTO;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//  Returns the event carried by the current handleEventAction,
//  if any.
//
// Use: protected
//
const SoPath *
SoDragger::getPickPath() const
//
////////////////////////////////////////////////////////////////////////
{
    const SoDragger *subDragger = getActiveChildDragger();
    if (subDragger != NULL)
	return subDragger->getPickPath();
    else
	return pickPath;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//  Returns the event carried by the current handleEventAction,
//  if any.
//
// Use: protected
//
const SoEvent *
SoDragger::getEvent() const
//
////////////////////////////////////////////////////////////////////////
{
    if ( getHandleEventAction()  == NULL ) {
#ifdef DEBUG
	SoDebugError::post("SoDragger::getEvent", "HandleEvent action is NULL");
#endif
	return NULL;
    }

    return ( getHandleEventAction()->getEvent() );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//  Returns the point in space that begins the dragging gesture.
//  Usually this is just the picked point, but in special
//  cases, the point is set without picking. 
//  For example, when a modifier key goes down, a dragger will often
//  start a new drag gesture without doing a pick. Instead, the previously
//  used point will be chosen to start a new gesture.
//
// Use: protected
//
SbVec3f
SoDragger::getWorldStartingPoint()
//
////////////////////////////////////////////////////////////////////////
{
    return startingWorldPoint;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//  Transforms the starting point into local space and returns it.
//
// Use: protected
//
SbVec3f
SoDragger::getLocalStartingPoint()
//
////////////////////////////////////////////////////////////////////////
{
    SbVec3f thePoint = getWorldStartingPoint();

    getWorldToLocalMatrix().multVecMatrix(thePoint, thePoint);
    return thePoint;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This is called to get the position of the locater in 
//    normalized (0.0 to 1.0) screen space.  (0,0) is lower left
//    the event. 
//
// Use: protected
//
SbVec2f
SoDragger::getNormalizedLocaterPosition()
//
////////////////////////////////////////////////////////////////////////
{
    return( getEvent()->getNormalizedPosition( getViewportRegion() ));
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This is called to get the position of the locater in 
//    non-normalized screen space.  (0,0) is lower left
//    the event. 
//
// Use: protected
//
SbVec2s
SoDragger::getLocaterPosition()
//
////////////////////////////////////////////////////////////////////////
{
    return( getEvent()->getPosition( getViewportRegion()) );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//  Returns whether the locater has moved enough to assume a
//  constraint gesture.
//
// Use: private
//
SbBool
SoDragger::isAdequateConstraintMotion()
//
////////////////////////////////////////////////////////////////////////
{
    SbVec2s moved = (getStartLocaterPosition() - getLocaterPosition());
    short lengthSquared = moved[0]*moved[0] + moved[1]*moved[1];
    return (SbBool) (lengthSquared >= minGesture*minGesture);
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This sets the viewport and view volume information 
//    explicitly to given values.
//
// Use: protected
//
void
SoDragger::setCameraInfo(SoAction *action)
//
////////////////////////////////////////////////////////////////////////
{
    // If NULL, use default values
    if (action == NULL ) {

	viewVolume.ortho(-1, 1, -1, 1, 1, 10);
	vpRegion = SbViewportRegion(1,1);
    }
    // If we've got an action...
    else {
	// Get the viewport and viewVolume...
	SoState *state = action->getState();
	viewVolume = SoViewVolumeElement::get( state );
	vpRegion   = SoViewportRegionElement::get(state);
    }

    // Here's a chance to set up a good tempPathToThis if for some reason
    // we don't already have one.
    SoPath *pathToMe = createPathToThis();
    if (pathToMe)  {
	// We've can create a path. Just get rid of the one it gave us.
	pathToMe->ref();
	pathToMe->unref();
    }
    else if (action != NULL )
	setTempPathToThis( action->getCurPath() );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This sets the handleEventAction
//
// Use: protected
//
void
SoDragger::setHandleEventAction( SoHandleEventAction *newAction )
//
////////////////////////////////////////////////////////////////////////
{
    handleEventAction = newAction;
}


////////////////////////////////////////////////////////////////////////
//
// Description:
//
// Use: private
//
void
SoDragger::setTempPathToThis( const SoPath *somethingClose )
//
////////////////////////////////////////////////////////////////////////
{
    // Check that we're not doing something unnecessary...
    // If paths don't fork until 'this', we can keep the path we've got.
    // But even if we've already got an okay path, we should invalidate
    // here, since some matrix has probably changed.
    if ( tempPathToThis != NULL && somethingClose != NULL ) {
	int forkInd = tempPathToThis->findFork( somethingClose );
	if ( forkInd == (tempPathToThis->getLength() - 1)) {
	    cachedPathToThisValid	    = FALSE;
	    return;
        }
    }

    // We can not keep our old tempPathToThis. Delete it now. 
    // ( you delete tempPaths, not ref/unref)
    if (tempPathToThis != NULL) {
	delete tempPathToThis;
	tempPathToThis = NULL;
    }
    if (tempPathNumKidsHack != NULL) {
	delete tempPathNumKidsHack;
	tempPathNumKidsHack = NULL;
    }

    // Figure out tempPathToThis from somethingClose, which may 
    // not be exactly what we need...
    if ( somethingClose != NULL && somethingClose->containsNode(this) ) {
	// Create tempPathToThis. Copy from something close.
	tempPathToThis 
	    = createTempPathFromFullPath( (const SoFullPath *)somethingClose );

	// Then pop off  'til the you get to this.
	for (SoNode *n = tempPathToThis->getTail(); n != this; ) {
	    tempPathToThis->pop();
	    n = tempPathToThis->getTail();
	}
    }
    else if ( somethingClose != NULL && somethingClose->getLength() > 0 ) {
	// Start at the root and search for 'this' underneath.
	// Turn on nodekit searching for the duration...
	static SoSearchAction *sa = NULL;
	if (sa == NULL)
	    sa = new SoSearchAction;
	else
	    sa->reset();

	SbBool oldBool = SoBaseKit::isSearchingChildren();
	SoBaseKit::setSearchingChildren( TRUE );
	    sa->setNode( this );
	    sa->apply( somethingClose->getHead() );
	SoBaseKit::setSearchingChildren( oldBool );
	if ( sa->getPath() != NULL )
	    tempPathToThis 
		= createTempPathFromFullPath((const SoFullPath *)sa->getPath());
    }

    // Create the tempPathNumKidsHack.  This is an sbPlist that records
    // how many children are under each node along the path.
    // We use it later to reconstruct the path if the parent/child pairs
    // are still okay, but the indices in the path have changed.
    if ( tempPathToThis != NULL ) {
	SoNode *pathNode;

	tempPathNumKidsHack = new SbPList( tempPathToThis->getLength() );

	for ( int i = 0; i < tempPathToThis->getLength(); i++ ) {
	    pathNode = tempPathToThis->getNode(i);
	    if ( !pathNode  || !pathNode->getChildren() )
		tempPathNumKidsHack->append( (void *) 0 );
	    else
		tempPathNumKidsHack->append( 
			    (void *) (unsigned long) pathNode->getChildren()->getLength() );
	}
    }

    // In both cases, the cachedPath has changed...
    cachedPathToThisValid	    = FALSE;
}

SoPath *
SoDragger::createPathToThis() 
{
    if ( isTempPathToThisOk() )
	return tempPathToThis->copy();
    else
	return NULL;
}

SbBool
SoDragger::isTempPathToThisOk()
{
    // ??? Hack alert! ???
    //     This whole method is a hack!
    // 

    if ( tempPathToThis == NULL || tempPathNumKidsHack == NULL)
	return FALSE;

    SbBool isOkay = TRUE;

    SoNode *actualNode = NULL;
    SoNode *nodeInPath = NULL;
    
    for(int numFmTail = 0; numFmTail < tempPathToThis->getLength();numFmTail++){

	int numFmHead = tempPathToThis->getLength() - numFmTail -1;
	nodeInPath    = tempPathToThis->getNode( numFmHead );

	if (numFmTail == 0) {
	    if (this != nodeInPath) {
		isOkay = FALSE;
		break;
	    }
	}
	else {
	    // actual node is still node from last time.
	    // See if you can find 'nodeInPath' as a parent in the
	    // auditor list of actualNode.
	    const SoAuditorList aList = actualNode->getAuditors();
	    if ( aList.find( nodeInPath, SoNotRec::PARENT ) == -1 ) {
		isOkay = FALSE;
		break;
	    }
	    else {
		// Since this is a temp path, it does not get notified 
		// when children are added or deleted. So we should
		// check and adjust the indices in the path if they have
		// gotten screwed up...

		// Make sure that the
		// index is correct for this parent/child pair.
		SoNode *parent = nodeInPath;
		SoNode *child  = actualNode;

		SoChildList *children = parent->getChildren();
		if (children == NULL) {
		    isOkay = FALSE;
		    break;
		}

		int indexInPath = tempPathToThis->getIndex(numFmHead+1);

		int numKidsNow    = children->getLength();
#if (USE_64BIT_HACKS)
		int numKidsBefore = (int) ((long) (*tempPathNumKidsHack)[numFmHead]);
#else
		int numKidsBefore = (int) (*tempPathNumKidsHack)[numFmHead];
#endif

		// To be correct, the childNode has to be the correct numbered
		// child under the parent, and the parent should still
		// have the same number of children it did before. 
		if (   (numKidsNow != numKidsBefore)
		    || (numKidsNow <= indexInPath)
		    || ((*children)[indexInPath] != child) ) {

		    // We have a problem. Try to fix it.

		    // First, figure out where it is most likely to belong.
		    // Also, update the tempPathNumKidsHack
		    int bestSpot = indexInPath;
		    if ( numKidsNow != numKidsBefore ) {
			// update the number of kids.
			tempPathNumKidsHack->remove(numFmHead);
			tempPathNumKidsHack->insert( (void *) (unsigned long) numKidsNow,
						    numFmHead);

			bestSpot = indexInPath + (numKidsNow - numKidsBefore);
		    }

		    // Look forward and back from bestSpot and try to find the
		    // node as a child. See which way we come to the node first.
		    // (we have to do this because of instancing.)
		    int early;
		    int late;
		    SbBool newSpot = -1;
		    for ( early = bestSpot, late = bestSpot; 
			  early >= 0 || late < numKidsNow;
			  early--, late++ ) {
			if (early >= 0 && early < numKidsNow &&
			    (child == (*children)[early])) {
				newSpot = early;
				break;
			}
			if (late >= 0 && late < numKidsNow &&
			    (child == (*children)[late])) {
				newSpot = late;
				break;
			}
		    }

		    if ( newSpot == -1 ) {
			isOkay = FALSE;
			break;
		    }

		    if ( newSpot != indexInPath ) {

			// We need to alter the path so we increment or
			// decrement the index.

			// Increments or decrements index of the child, since
			// some children may have been added or deleted.
			if (newSpot < indexInPath) {
			    for (int j = newSpot; j < indexInPath; j++ )
				tempPathToThis->removeIndex(nodeInPath, 0 );
			}
			else if (newSpot > indexInPath) {
			    for (int j = newSpot; j > indexInPath; j-- )
				tempPathToThis->insertIndex(nodeInPath, 0 );
			}
		    }
		}
	    }
	}
	actualNode = nodeInPath;
    }

    if (isOkay == FALSE) {
	if (tempPathToThis != NULL) {
	    delete tempPathToThis;
	    tempPathToThis = NULL;
	}
	if (tempPathNumKidsHack != NULL) {
	    delete tempPathNumKidsHack;
	    tempPathNumKidsHack = NULL;
	}
    }

    return isOkay;
}

SoTempPath *
SoDragger::createTempPathFromFullPath( const SoFullPath *fp ) const
{
    SoTempPath *answer = new SoTempPath( fp->getLength() );

    for ( int i = 0; i < fp->getLength(); i++ )
	answer->append( fp->getNode(i) );

    return answer;
}


////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the pickPath to be the given path.
//
void
SoDragger::setPickPath( SoPath *newPickPath )
//
////////////////////////////////////////////////////////////////////////
{
    // Set the pickPath. This should just equal the input path.
    // ref the input before unref'ing the old path, in case they're the same
	if ( newPickPath != NULL )
	    newPickPath->ref();

	// get rid of old path if it exists
	if ( pickPath != NULL ) {
	    pickPath->unref();
	    pickPath = NULL;
	}
	// add new path if it exists
	if ( newPickPath != NULL )
	    pickPath = (SoPath *) newPickPath;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the surrogatePick information to initial values.
//
void
SoDragger::setNoPickedSurrogate()
//
////////////////////////////////////////////////////////////////////////
{
    // Set the name to be empty
    surrogateNameInPickOwner = "";

    // get rid of old paths if they exist.
    if ( pathToSurrogatePickOwner != NULL ) {
	 pathToSurrogatePickOwner->unref();
	 pathToSurrogatePickOwner = NULL;
    }
    if ( surrogatePathInPickOwner != NULL ) {
	 surrogatePathInPickOwner->unref();
	 surrogatePathInPickOwner = NULL;
    }
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//
SbBool
SoDragger::shouldGrabBasedOnSurrogate( const SoPath *pickPath, 
				const SoPath *surrogatePath )
//
////////////////////////////////////////////////////////////////////////
{
    // The pickPath must contain the surrogatePath
    if ( pickPath->containsPath( surrogatePath ) == FALSE )
	return FALSE;

    const SoFullPath *fullPick = (const SoFullPath *) pickPath;
    const SoFullPath *fullSurr = (const SoFullPath *) surrogatePath;

    // Find the tail of surrogatePath.
    SoNode *surrTail = fullSurr->getTail();

    // Go from the tail of pickPath backwards.
    // If you find a dragger before you find surrTail, return FALSE.
    // Otherwise, return TRUE.
    SoNode *pickNode;
    for (int i = fullPick->getLength() - 1; i >= 0; i-- ) {
	pickNode = fullPick->getNode(i);
	if (pickNode == surrTail)
	    return TRUE;
	if (pickNode->isOfType( SoDragger::getClassTypeId() ))
	    return FALSE;
    }
    // Should never get here...
    return FALSE;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the surrogatePick information.
//
void
SoDragger::setPickedSurrogate( SoPath *pathToOwner, SbName &nameUsedByOwner,
			       SoPath *pathUsedByOwner )
//
////////////////////////////////////////////////////////////////////////
{
    // Set the pathToSurrogatePickOwner. 
	if ( pathToOwner != pathToSurrogatePickOwner ) {
	    if ( pathToOwner != NULL )
		pathToOwner->ref();

	    if ( pathToSurrogatePickOwner != NULL )
		 pathToSurrogatePickOwner->unref();

	    pathToSurrogatePickOwner = pathToOwner;
	}

    // Set the surrogatePathInPickOwner. 
	if ( pathUsedByOwner != surrogatePathInPickOwner ) {
	    if ( pathUsedByOwner != NULL )
		pathUsedByOwner->ref();

	    if ( surrogatePathInPickOwner != NULL )
		 surrogatePathInPickOwner->unref();

	    surrogatePathInPickOwner = pathUsedByOwner;
	}

    // Set the name...
	surrogateNameInPickOwner = nameUsedByOwner;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the point in space where dragger is to begin.
//    The pickedPoint version would usually be the result of a pick.
//    Assumes that 'newPoint' is in world space.
//
void
SoDragger::setStartingPoint( const SoPickedPoint *newPoint )
//
////////////////////////////////////////////////////////////////////////
{
    startingWorldPoint = newPoint->getPoint();
}
////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the point in space where dragger is to begin.
//    The SbVec3f version would usually be a piont saved from the end of 
//    another gesture. For example, when a modifier key goes down, we might
//    save the current position and use it to begin another connected gesture.
//    Assumes that 'newPoint' is in world space.
//
void
SoDragger::setStartingPoint( const SbVec3f &newPoint )
//
////////////////////////////////////////////////////////////////////////
{
    startingWorldPoint = newPoint;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    This implements the SoGetBoundingBoxAction.
//
// Use: protected
//
void
SoDragger::getBoundingBox(SoGetBoundingBoxAction *action)
//
////////////////////////////////////////////////////////////////////////
{
    if ( ! ignoreInBbox )
	SoBaseKit::getBoundingBox( action );
}

const SbMatrix &
SoDragger::getMotionMatrix()
//
////////////////////////////////////////////////////////////////////////
{
    // Try the fast way to access this node first...
    SoMatrixTransform *mm = (SoMatrixTransform *) motionMatrix.getValue();

    // If that fails, then make a new part...
    if (mm == NULL)
	mm = (SoMatrixTransform *) getAnyPart("motionMatrix", TRUE);

    return ( mm->matrix.getValue() );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Sets the motion matrix to be an explicit matrix. Triggers value
//    changed callbacks, but only if (newMatrix != the current motionMatrix)
//
void
SoDragger::setMotionMatrix( const SbMatrix &newMatrix )
//
////////////////////////////////////////////////////////////////////////
{
    // Return if no change...
	if ( getMotionMatrix() == newMatrix )
	    return;

    // Set motion matrix (the field will be non-null, 
    // since 'getMotionMatrix()' was just called
	((SoMatrixTransform *)motionMatrix.getValue())->matrix = newMatrix;

    // We'll need to recalculate the conversion matrices.
	cachedMotionMatrixValid = FALSE;

    // Invokes the value changed callbacks
	valueChanged();
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//     Invokes the valueChangedCallbacks, if they are enabled.
//     These are invoked whenever setMotionMatrix() changes the motion matrix.
//     If a subclass wishes to invoke the valueChanged callbacks for some
//     other reason, they may call valueChanged(). Example: SoSpotLightDragger
//     changes its angle field without altering the motionMatrix. So it
//     calls valueChanged() to invoke callbacks.
//
void
SoDragger::valueChanged()
//
////////////////////////////////////////////////////////////////////////
{
    if (valueChangedCallbacksEnabled == TRUE)
        valueChangedCallbacks->invokeCallbacks(this);
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Saves the motionMatrix as the startMotionMatrix.
//    The startMotionMatrix is used to remember where the dragger was
//    at the beginning of dragging.
//
void
SoDragger::saveStartParameters()
//
////////////////////////////////////////////////////////////////////////
{
    startMotionMatrix = getMotionMatrix();
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//     This sets the 'activeChildDragger.'
//     Called by childStartCB(), childFinishCB(),
//     childValueChangedCB(), etc.
//     Establishes which dragger is the child, information
//     needed by the parent in order to do what it needs to do.
//
void
SoDragger::setActiveChildDragger( SoDragger *newChildDragger )
//
////////////////////////////////////////////////////////////////////////
{
    if (newChildDragger)
	newChildDragger->ref();

    if (activeChildDragger)
	activeChildDragger->unref();

    activeChildDragger = newChildDragger;
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//     This function is invoked by child draggers 
//     when they change their value.
//
void 
SoDragger::transferMotion( SoDragger *childDragger)
//
////////////////////////////////////////////////////////////////////////
{
    // Get the motion matrix from the child
	SbMatrix childMotion = childDragger->getMotionMatrix();

    // There's a lot we don't need to bother with if the childMotion is
    // identity...
	SbBool childIdent = ( childMotion == SbMatrix::identity() );
    
    // Return if childMotion is identity and our motionMatrix already 
    // matches our saved startMatrix.
	if ( childIdent && (getMotionMatrix() == getStartMotionMatrix()))
	    return;

	if ( !childIdent ) {
	    // First, set the childDragger matrix to identity.
	    childDragger->setMotionMatrix( SbMatrix::identity() );

	    // Convert the childMotion from child LOCAL space to  world space.
	    childDragger->transformMatrixLocalToWorld(childMotion,childMotion);

	    // Convert the childMotion from world space to our LOCAL space.
	    transformMatrixWorldToLocal(childMotion,childMotion);
	}

    // Append this transformed child motion to our saved start matrix.
	SbMatrix newMotion = getStartMotionMatrix();
	if ( !childIdent )
	    newMotion.multLeft( childMotion );

	setMotionMatrix( newMotion );

    // Changing the parent matrix invalidates the matrix cache of the
    // childDragger
	childDragger->cachedPathToThisValid = FALSE;
}

////////////////////////////////////////////////////////////////////////
//
// Use: private
//
void
SoDragger::validateMatrices()
//
////////////////////////////////////////////////////////////////////////
{
    // If both aspects of the matrices are still okay, then we can
    // continue to use the cached values.
    if ( cachedPathToThisValid && cachedMotionMatrixValid )
	return;

    // If the tempPathToThis is no longer valid, then we've got to run a 
    // getMatrix action to find the preMotionToWorld and 
    // worldToPreMotion matrices.
    if ( cachedPathToThisValid == FALSE ) {

	// Do a get matrix action from world space to this node.
	SoPath *pathToMe = createPathToThis();
	if ( pathToMe != NULL ) {
	    pathToMe->ref();
	    static SoGetMatrixAction *ma = NULL;
	    if (ma == NULL)
		ma = new SoGetMatrixAction( getViewportRegion() );
	    else
		ma->setViewportRegion( getViewportRegion() );

	    ma->apply( pathToMe );
	    preMotionToWorldMatrix = ma->getMatrix();
	    worldToPreMotionMatrix = ma->getInverse();
	    pathToMe->unref();
	}
	else {
	    preMotionToWorldMatrix.makeIdentity();
	    worldToPreMotionMatrix.makeIdentity();
	}
    }

    // If the cachedMotionMatrix is no longer valid, then figure 'em out...
    if ( cachedMotionMatrixValid == FALSE ) {
	cachedMotionMatrix = getMotionMatrix();
    }

    // Last, we append the motion matrix to the preMotion matrix to get
    // the postMotion matrix.
    postMotionToWorldMatrix = preMotionToWorldMatrix;
    postMotionToWorldMatrix.multLeft( cachedMotionMatrix );

    worldToPostMotionMatrix = worldToPreMotionMatrix;
    worldToPostMotionMatrix.multRight( cachedMotionMatrix.inverse() );

    cachedMotionMatrixValid = TRUE;
    cachedPathToThisValid = TRUE;
}
    
    
////////////////////////////////////////////////////////////////////////
//
// Use: protected
//
SbMatrix
SoDragger::getLocalToWorldMatrix()
//
////////////////////////////////////////////////////////////////////////
{
    // Make sure everything is kosher
    validateMatrices();

    return postMotionToWorldMatrix;
}

////////////////////////////////////////////////////////////////////////
//
// Use: protected
//
SbMatrix
SoDragger::getWorldToLocalMatrix()
//
////////////////////////////////////////////////////////////////////////
{
    // Make sure everything is kosher
    validateMatrices();

    return worldToPostMotionMatrix;
}

////////////////////////////////////////////////////////////////////////
//
// Use: protected
//
void
SoDragger::transformMatrixLocalToWorld( const SbMatrix   &fromMatrix, 
			                  SbMatrix         &toMatrix)
//
////////////////////////////////////////////////////////////////////////
{
    toMatrix = fromMatrix;

    SbMatrix forward = getLocalToWorldMatrix();
    SbMatrix backward = getWorldToLocalMatrix();

    toMatrix.multRight( forward );
    toMatrix.multLeft( backward );
}

////////////////////////////////////////////////////////////////////////
//
// Use: protected
//
void
SoDragger::transformMatrixWorldToLocal( const SbMatrix   &fromMatrix, 
			                  SbMatrix         &toMatrix)
//
////////////////////////////////////////////////////////////////////////
{
    toMatrix = fromMatrix;

    SbMatrix forward = getWorldToLocalMatrix();
    SbMatrix backward = getLocalToWorldMatrix();

    toMatrix.multRight( forward );
    toMatrix.multLeft( backward );
}


////////////////////////////////////////////////////////////////////////
//
// Use: EXTENDER public
//
// Description:
// Get the matrix which converts from the space of one part into
// local space. Good to use if transforms occur between 'motionMatrix'
// and the space you want to work in.
// Note: This routine will try not to create parts that don't exist.
//       Instead it finds the existing part that precedes it in traversal.
//       But this only works if the partName is in this nodekit's catalog.
//       If the part is nested within another kit below this one or 
//       sitting inside a list part, the part will be created when it 
//       doesn't exist.
//
void 
SoDragger::getPartToLocalMatrix( const SbName &partName, 
	    SbMatrix &partToLocalMatrix, SbMatrix &localToPartMatrix)
//
////////////////////////////////////////////////////////////////////////
{
    // We need to temporarily ref ourself, since we build paths
    // and stuff...
    ref();

    SoPath *pathToMe = createPathToThis();
    if (pathToMe)
	pathToMe->ref();

    SoPath *pathToPart;

    // We want to figure this out without creating any parts unnecessarily.
    // So, instead of forcing creation, do a check.
        pathToPart = createPathToAnyPart(partName,FALSE,FALSE,FALSE,pathToMe);

    // If we didn't find a path that already exists:
	if (pathToPart == NULL) {
    	    const SoNodekitCatalog *cat = getNodekitCatalog();
	    int pNum = cat->getPartNumber(partName);
	    if ( pNum != SO_CATALOG_NAME_NOT_FOUND ) {

		// If it fails and the part is in this catalog, then traverse 
		// backwards until we can find the last part before partName 
		// and after "motionMatrix" If we hit "motionMatrix", or 
		// "this", then just set path to NULL
		int thisPnum   = cat->getPartNumber("this");
		int motMatPnum = cat->getPartNumber("motionMatrix");

		while ((pathToPart == NULL) &&
		       (pNum != thisPnum) && (pNum != motMatPnum)) 
		{
		    // Find left sibling or parent.  Can check 'em together
		    // since left sibling never precedes parent.
		    for (int i = pNum-1; i >= 0; i-- ) {
			if ((cat->getRightSiblingPartNumber(i) == pNum) ||
			    (cat->getParentPartNumber(pNum) == i)) {
			    pNum = i;
			    break;
			}
		    }
		    if ( pNum != thisPnum && pNum != motMatPnum ) {
			pathToPart = createPathToAnyPart(cat->getName(pNum),
				FALSE, FALSE, FALSE, pathToMe);
		    }
		}

	    }
	    else {
		// If the part doesn't exist yet and the partName
		// is not in this catalog, we're sort of stuck. Force 
		// it and create the part anyway. (2nd arg == TRUE)
		pathToPart = createPathToAnyPart(partName,TRUE,
						 FALSE,FALSE,pathToMe);
	    }
	}

    // We don't need this path anymore.
    if (pathToMe)
	pathToMe->unref();

    if ( pathToPart == NULL ) {
	partToLocalMatrix = localToPartMatrix = SbMatrix::identity();
        // Undo temporary ref on ourself.
	unrefNoDelete();
	return;
    }

    pathToPart->ref();

    static SoGetMatrixAction *ma = NULL;
    if (ma == NULL)
	ma = new SoGetMatrixAction( getViewportRegion() );
    else
	ma->setViewportRegion( getViewportRegion() );

    ma->apply( pathToPart );
    SbMatrix partToWorld = ma->getMatrix();
    SbMatrix worldToPart = ma->getInverse();

    pathToPart->unref();

    partToLocalMatrix = partToWorld;
    partToLocalMatrix.multRight( getWorldToLocalMatrix() );

    localToPartMatrix = getLocalToWorldMatrix();
    localToPartMatrix.multRight( worldToPart);

    // Undo temporary ref on ourself.
    unrefNoDelete();
}

void 
SoDragger::transformMatrixToLocalSpace( const SbMatrix &fromMatrix,
	    SbMatrix &toMatrix, const SbName &fromSpacePartName)
{
    SbMatrix fromToLocalM, localToFromM;

    getPartToLocalMatrix( fromSpacePartName, fromToLocalM, localToFromM);

    toMatrix = fromMatrix;
    toMatrix.multRight( fromToLocalM );
    toMatrix.multLeft( localToFromM );
}

////////////////////////////////////////////////////////////////////////
//
// Description:
//    Simple functions for adding to and removing from
//    the different callback lists.
//
////////////////////////////////////////////////////////////////////////
/////////
// START
/////////
void SoDragger::addStartCallback( SoDraggerCB *f, void *d )
{  startCallbacks->addCallback( (SoCallbackListCB *)f, d ); }

void SoDragger::removeStartCallback( SoDraggerCB *f, void *d )
{  startCallbacks->removeCallback( (SoCallbackListCB *)f, d ); }

/////////
// MOTION
/////////
void SoDragger::addMotionCallback( SoDraggerCB *f, void *d )
{  motionCallbacks->addCallback( (SoCallbackListCB *)f, d ); }

void SoDragger::removeMotionCallback( SoDraggerCB *f, void *d )
{  motionCallbacks->removeCallback( (SoCallbackListCB *)f, d ); }

/////////
// DONE
/////////
void SoDragger::addFinishCallback( SoDraggerCB *f, void *d )
{  finishCallbacks->addCallback( (SoCallbackListCB *)f, d ); }

void SoDragger::removeFinishCallback( SoDraggerCB *f, void *d )
{  finishCallbacks->removeCallback( (SoCallbackListCB *)f, d ); }

/////////
// VALUECHANGED
/////////
void SoDragger::addValueChangedCallback( SoDraggerCB *f, void *d )
{  valueChangedCallbacks->addCallback( (SoCallbackListCB *)f, d ); }

void SoDragger::removeValueChangedCallback( SoDraggerCB *f, void *d )
{  valueChangedCallbacks->removeCallback( (SoCallbackListCB *)f, d ); }

SbBool SoDragger::enableValueChangedCallbacks( SbBool newVal )
{
    SbBool oldVal = valueChangedCallbacksEnabled;
    valueChangedCallbacksEnabled = newVal;
    return oldVal;
}

/////////
// OTHEREVENT
/////////
void SoDragger::addOtherEventCallback( SoDraggerCB *f, void *d )
{  otherEventCallbacks->addCallback( (SoCallbackListCB *)f, d ); }

void SoDragger::removeOtherEventCallback( SoDraggerCB *f, void *d )
{  otherEventCallbacks->removeCallback( (SoCallbackListCB *)f, d ); }


void SoDragger::registerChildDragger(SoDragger *child)
{
    // This calls transferMotion, followed by the callbacks for this node.
    child->addValueChangedCallback( 
			SoDragger::childTransferMotionAndValueChangedCB, this );

    child->addStartCallback(SoDragger::childStartCB, this);
    child->addMotionCallback(SoDragger::childMotionCB, this);
    child->addFinishCallback(SoDragger::childFinishCB, this);
    child->addOtherEventCallback(SoDragger::childOtherEventCB, this);
}

void SoDragger::unregisterChildDragger(SoDragger *child)
{
    child->removeValueChangedCallback( 
			SoDragger::childTransferMotionAndValueChangedCB, this );

    child->removeStartCallback(SoDragger::childStartCB, this);
    child->removeMotionCallback(SoDragger::childMotionCB, this);
    child->removeFinishCallback(SoDragger::childFinishCB, this);
    child->removeOtherEventCallback(SoDragger::childOtherEventCB, this);
}

void SoDragger::registerChildDraggerMovingIndependently(SoDragger *child)
{
    child->addValueChangedCallback( SoDragger::childValueChangedCB, this );

    child->addStartCallback(SoDragger::childStartCB, this);
    child->addMotionCallback(SoDragger::childMotionCB, this);
    child->addFinishCallback(SoDragger::childFinishCB, this);
    child->addOtherEventCallback(SoDragger::childOtherEventCB, this);
}

void SoDragger::unregisterChildDraggerMovingIndependently(SoDragger *child)
{
    child->removeValueChangedCallback( SoDragger::childValueChangedCB, this );

    child->removeStartCallback(SoDragger::childStartCB, this);
    child->removeMotionCallback(SoDragger::childMotionCB, this);
    child->removeFinishCallback(SoDragger::childFinishCB, this);
    child->removeOtherEventCallback(SoDragger::childOtherEventCB, this);
}

void SoDragger::childTransferMotionAndValueChangedCB(void      *parentAsVoid, 
						     SoDragger *childDragger)
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

	// Before calling the other valueChanged callbacks, transfer the
	// motion of the childDragger into our own motion matrix.
	    // We do not want to trigger any of our other valueChanged callbacks
	    // while this is being done...
	    SbBool saveEnabled = parent->enableValueChangedCallbacks( FALSE );
	    parent->transferMotion( childDragger );
	    parent->enableValueChangedCallbacks( saveEnabled );

	parent->valueChanged();
    parent->setActiveChildDragger( savedChild );

	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void SoDragger::childValueChangedCB(void *parentAsVoid, SoDragger *childDragger)
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

	parent->valueChanged();
    parent->setActiveChildDragger( savedChild );
	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void SoDragger::childStartCB(void *parentAsVoid, SoDragger *childDragger )
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	parent->saveStartParameters();

	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

        parent->setStartingPoint( childDragger->getWorldStartingPoint() );

	// While the child is manipulating, we should not bother caching here.
	parent->renderCaching = OFF;
	parent->startCallbacks->invokeCallbacks(parent);
    parent->setActiveChildDragger( savedChild );
	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void SoDragger::childMotionCB(void *parentAsVoid, SoDragger *childDragger )
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

	parent->motionCallbacks->invokeCallbacks(parent);
    parent->setActiveChildDragger( savedChild );
	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void SoDragger::childFinishCB(void *parentAsVoid, SoDragger *childDragger )
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

	// When child is finished manipulating, we resume caching.
	parent->renderCaching = AUTO;
	parent->finishCallbacks->invokeCallbacks(parent);
    parent->setActiveChildDragger( savedChild );
	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void SoDragger::childOtherEventCB(void *parentAsVoid, SoDragger *childDragger )
{
    SoDragger *parent = (SoDragger *) parentAsVoid;

    SoDragger *savedChild = parent->getActiveChildDragger();
    if (savedChild) savedChild->ref();
    parent->setActiveChildDragger( childDragger );
	// Save these variables to we can put 'em back when we're done.
	SoHandleEventAction *oldHa  = parent->getHandleEventAction();
	SbViewVolume         oldVV  = parent->getViewVolume();
	SbViewportRegion     oldVPR = parent->getViewportRegion();

	parent->setHandleEventAction(childDragger->getHandleEventAction());
	parent->setViewVolume(childDragger->getViewVolume());
	parent->setViewportRegion(childDragger->getViewportRegion());

	SoPath *pathToKid = childDragger->createPathToThis();
	if (pathToKid) pathToKid->ref();
	parent->setTempPathToThis( pathToKid );
	if (pathToKid) pathToKid->unref();

	parent->otherEventCallbacks->invokeCallbacks(parent);
    parent->setActiveChildDragger( savedChild );
	// Restore saved values of our variables
	parent->setHandleEventAction(oldHa);
	parent->setViewVolume(oldVV);
	parent->setViewportRegion(oldVPR);

    if (savedChild) savedChild->unref();
}

void
SoDragger::workFieldsIntoTransform( SbMatrix &mtx )
{
    SbVec3f    trans,  *translationPtr = NULL;
    SbRotation rot,    *rotationPtr = NULL;
    SbVec3f    scale,  *scaleFactorPtr = NULL;
    SbRotation orient, *scaleOrientationPtr = NULL;
    SbVec3f    center, *centerPtr = NULL;
    SoField    *f;

    // Assign values from any fields you might find...
	if ( (f = getField( "translation" )) != NULL ) {
	    trans =  ((SoSFVec3f *)f)->getValue();
	    translationPtr = &trans;
	}
	if ( (f = getField( "rotation" )) != NULL ) {
	    rot =  ((SoSFRotation *)f)->getValue();
	    rotationPtr = &rot;
	}
	if ( (f = getField( "scaleFactor" )) != NULL ) {
	    scale =  ((SoSFVec3f *)f)->getValue();
	    scaleFactorPtr = &scale;
	}
	if ( (f = getField( "scaleOrientation" )) != NULL ) {
	    orient =  ((SoSFRotation *)f)->getValue();
	    scaleOrientationPtr = &orient;
	}
	if ( (f = getField( "center" )) != NULL ) {
	    center =  ((SoSFVec3f *)f)->getValue();
	    centerPtr = &center;
	}

    workValuesIntoTransform( mtx, translationPtr,
	rotationPtr, scaleFactorPtr, scaleOrientationPtr, centerPtr );
}

void
SoDragger::workValuesIntoTransform( SbMatrix &mtx, 
			    const SbVec3f *translationPtr,
			    const SbRotation *rotationPtr, 
			    const SbVec3f *scaleFactorPtr,
			    const SbRotation *scaleOrientationPtr, 
			    const SbVec3f *centerPtr )
{
    SbVec3f    trans, scale;
    SbRotation rot, scaleOrient;
    SbVec3f    center(0,0,0);

    // To begin with, get the values currently in the matrix. If we were 
    // given a center, use it for the calculations.
	if ( centerPtr != NULL )
	    center =  *centerPtr;
	SoDragger::getTransformFast( mtx, trans,rot,scale,scaleOrient,center);

    // Now, replace any values which should be dictated by our input.
    // Don't need to do center again, since it should remain unchanged.
	if ( translationPtr != NULL )
	    trans = *translationPtr; 
	if ( rotationPtr != NULL )
	    rot = *rotationPtr; 
	if ( scaleFactorPtr != NULL )
	    scale = *scaleFactorPtr; 
	if ( scaleOrientationPtr != NULL )
	    scaleOrient = *scaleOrientationPtr; 

    // Finally, construct a new transform with these values.
	mtx.setTransform( trans, rot, scale, scaleOrient, center );
}

void
SoDragger::getTransformFast( SbMatrix &mtx, SbVec3f &translation,
			     SbRotation &rotation, SbVec3f &scaleFactor,
			     SbRotation &scaleOrientation, 
			     const SbVec3f &center )
{
    if (center != SbVec3f(0,0,0)) {
	// to get fields for a non-0 center, we
	// need to decompose a new matrix "m" such
	// that [-center][m][center] = [this]
	// i.e., [m] = [center][this][-center]
	// (this trick stolen from Showcase code)
        SbMatrix m,c;
	m.setTranslate(-center);
	m.multLeft(mtx);
	c.setTranslate(center);
	m.multLeft(c);
	SoDragger::getTransformFast( m, translation, rotation, scaleFactor,
			       scaleOrientation);
    }
    else
	SoDragger::getTransformFast( mtx, translation, rotation, scaleFactor,
			       scaleOrientation);
}

void
SoDragger::getTransformFast(  SbMatrix &mtx, SbVec3f &translation,
			SbRotation &rotation, SbVec3f &scaleFactor,
			SbRotation &scaleOrientation)
{
    SbBool canDoFast = TRUE;

    // If the last column is (0,0,0,1), then we don't have to worry 
    // about projection matrix.  If not, we need to call SbMatrix::factor
    if ( mtx[0][3] != 0 || mtx[1][3] != 0 || mtx[2][3] != 0 || mtx[3][3] != 1)
	canDoFast = FALSE;

    // You get maxXVec, matYVec, and matZVec if you send the xVec,yVec,zVec,
    // through the matrix.  
    SbVec3f xVec(1,0,0), yVec(0,1,0), zVec(0,0,1);
    SbVec3f matXVec( mtx[0][0], mtx[0][1], mtx[0][2] );
    SbVec3f matYVec( mtx[1][0], mtx[1][1], mtx[1][2] );
    SbVec3f matZVec( mtx[2][0], mtx[2][1], mtx[2][2] );
    // If they are orthogonal, that means the 
    // scaleOrientation is identity() and we are free to factor the matrix.
    // Only need to test two sets, since 3rd answer is implicit.
#define TINY 0.00001
    if (       fabs( matXVec.dot( matYVec )) > TINY )
	canDoFast = FALSE;
    else if (  fabs( matYVec.dot( matZVec )) > TINY )
	canDoFast = FALSE;
#undef TINY

    if ( canDoFast == TRUE ) {

	scaleOrientation.setValue(0,0,0,1);

	// Translation is just the first three entries in bottom row.
	translation.setValue( mtx[3][0], mtx[3][1], mtx[3][2] );

	// scaleFactor is scale of the three transformed axes.
	// Kill two birds with one stone and normalize to get the sizes...
	scaleFactor.setValue( matXVec.normalize(),
			      matYVec.normalize(),
			      matZVec.normalize());

	rotation = SbMatrix( matXVec[0], matXVec[1], matXVec[2], 0,
			     matYVec[0], matYVec[1], matYVec[2], 0,
			     matZVec[0], matZVec[1], matZVec[2], 0,
			     0,          0,          0,          1 );
    }
    else {
        // If 'canDoFast' == FALSE, send the info to SbMatrix::factor
#ifdef DEBUG
#if 0
	SoDebugError::post("SoDragger::getTransformFast", 
		      "This is a tricky matrix. Giving it to SbMatrix::factor");
#endif
#endif

	SbMatrix proj, rotMatrix, scaleOrientMatrix;
	mtx.factor(scaleOrientMatrix, scaleFactor, rotMatrix, translation,proj);
	rotation = rotMatrix;
	// have to transpose because factor gives transpose of correct answer
        scaleOrientation = scaleOrientMatrix.transpose();  
    }
}

SbMatrix
SoDragger::appendTranslation( const SbMatrix &mtx, 
			const SbVec3f &translation, 
			const SbMatrix *conversion )
{
    SbBool  isCnvIdent = (conversion == NULL 
			  || (*conversion) == SbMatrix::identity());
    SbBool  isMtxIdent = (mtx        == SbMatrix::identity());

    // Get motion into local Space.
    // Local space for translation is at the beginning of the matrix.
    // Convert using multDirMatrix, not multVecMatrix, since this is motion.
    SbVec3f lclMotion = translation;
    if ( !isCnvIdent )
	conversion->multDirMatrix( lclMotion, lclMotion );
    if ( !isMtxIdent )
	mtx.multDirMatrix( lclMotion, lclMotion );

    SbVec3f startTranslate = mtx[3];
    SbVec3f newTranslate = startTranslate + lclMotion;

    SbMatrix answer = mtx;
    answer[3][0] = newTranslate[0];
    answer[3][1] = newTranslate[1];
    answer[3][2] = newTranslate[2];

    return answer;
}

SbMatrix 
SoDragger::appendScale( const SbMatrix &mtx, 
		  const SbVec3f &scale, const SbVec3f &scaleCenter,
		  const SbMatrix *conversion )
{
    SbBool  isCnvIdent = (conversion == NULL 
			  || (*conversion) == SbMatrix::identity());
    SbBool  isMtxIdent = (mtx == SbMatrix::identity());
    SbMatrix  convInverse, mtxInverse;


    if ( !isCnvIdent )
	convInverse = conversion->inverse();
    if ( !isMtxIdent )
	mtxInverse = mtx.inverse();

    // Calculate 'matrixWithScale.' 
    // [matrixWithScale] = [convInverse][scaleMtx][conversion][mtx]
	// Create a scaling matrix;
	    SbMatrix scaleMtx;
	    scaleMtx.setScale( scale );
	// convert it to space at end of matrix, 
	    // [scaleMtx] = [convInverse][scaleMtx][conversion]
	    if ( !isCnvIdent ) {
		scaleMtx.multRight( (*conversion) );
		scaleMtx.multLeft( convInverse );
	    }
	// Append this scaling to mtx.
	// [mtxWithScale] = [scaleMtx][mtx]
	    SbMatrix mtxWithScale;
	    if ( !isMtxIdent ) {
	        mtxWithScale = mtx;
		mtxWithScale.multLeft( scaleMtx );
	    }
	    else
		mtxWithScale = scaleMtx;

    // Extract the new values from the merged matrix.
	SbVec3f    mrgTrans, mrgScale;
	SbRotation mrgRot, mrgScaleOrient;
	getTransformFast(mtxWithScale,mrgTrans,mrgRot, mrgScale,mrgScaleOrient);

    // Constrain the scaling to be greater than getMinScale().
	SbVec3f okayMrgScale = mrgScale;
	for (int i = 0; i < 3; i++ ) {
	    if (okayMrgScale[i] <= getMinScale() )
		okayMrgScale[i] = getMinScale();
	}

    SbVec3f okayScale;
    if ( okayMrgScale == mrgScale )
	okayScale = scale;
    else {
	// If we needed to constrain, figure out 'okayScale.' 

	// First, construct 'okayMtxWithScale.'
	// This is a version of 'matrixWithScale' where we replace
	// 'mrgScale' with 'okayMrgScale.'
	    SbMatrix okayMtxWithScale;
	    okayMtxWithScale.setTransform( mrgTrans, mrgRot, 
					okayMrgScale, mrgScaleOrient );

	// Using the same relationship as earlier:
        // [okayMtxWithScale] = [convInverse][okayScaleMtx][conversion][mtx]
	// Solve for 'okayScaleMtx' to find the scale matrix that gives
	// us the desired result.
	    SbMatrix okayScaleMtx = okayMtxWithScale;
	    if ( !isMtxIdent )
		okayScaleMtx.multRight( mtxInverse );
	    if ( !isCnvIdent ) {
		okayScaleMtx.multRight( convInverse );
		okayScaleMtx.multLeft( (*conversion) );
	    }

	// Get the okayScale from its matrix.
	    okayScale[0] = okayScaleMtx[0][0];
	    okayScale[1] = okayScaleMtx[1][1];
	    okayScale[2] = okayScaleMtx[2][2];
    }

    // Now we've got a scale (okayScale) and scaleCenter we know we can use.
    // Create the right matrix and append it to get our answer.
    // [answer] = [convInvserse][scaleAboutCenter][conversion][mtx]
    // where: [scaleAboutCenter] = [centerInverse][okayScale][center]
	SbMatrix scaleAboutCenter;
	scaleAboutCenter.setScale( okayScale );
	if ( scaleCenter != SbVec3f(0,0,0) ) {
	    SbMatrix tm;
	    tm.setTranslate( scaleCenter );
	    scaleAboutCenter.multRight( tm );
	    tm.setTranslate( -scaleCenter );
	    scaleAboutCenter.multLeft( tm );
	}

        SbMatrix answer = scaleAboutCenter;
	if ( !isCnvIdent ) {
	    answer.multLeft( convInverse );
	    answer.multRight( (*conversion) );
	}
	if ( !isMtxIdent )
	    answer.multRight( mtx );

    return answer;
}

SbMatrix
SoDragger::appendRotation( const SbMatrix &mtx,
		    const SbRotation &rot, const SbVec3f &rotCenter,
		    const SbMatrix *conversion )
{
    SbBool  isCnvIdent = (conversion == NULL 
			  || (*conversion) == SbMatrix::identity());
    SbBool  isMtxIdent = (mtx == SbMatrix::identity());
    SbMatrix  convInverse, mtxInverse;


    if ( !isCnvIdent )
	convInverse = conversion->inverse();
    if ( !isMtxIdent )
	mtxInverse = mtx.inverse();

    // Create a matrix for rotating about the rotCenter and append it to our
    // mtx.
    // [answer] = [convInvserse][rotateAboutCenter][conversion][mtx]
    // where: [rotateAboutCenter] = [centerInverse][rotMat][center]
	SbMatrix rotateAboutCenter;
	rotateAboutCenter.setRotate( rot );
	if ( rotCenter != SbVec3f(0,0,0) ) {
	    SbMatrix tm;
	    tm.setTranslate( rotCenter );
	    rotateAboutCenter.multRight( tm );
	    tm.setTranslate( -rotCenter );
	    rotateAboutCenter.multLeft( tm );
	}

        SbMatrix answer = rotateAboutCenter;
	if ( !isCnvIdent ) {
	    answer.multLeft( convInverse );
	    answer.multRight( (*conversion) );
	}
	if ( !isMtxIdent )
	    answer.multRight( mtx );

    return answer;
}

// Called by the SoBaseKit::write() method. 
//
// Draggers don't want to write out fields if they have default vals.
//
// sets isActive to default if default and not connected.
//
// Looks for fields named: 
//    'rotation'    where value is within TINY of SbRotation::identity()
//    'translation' where value is within TINY of (0,0,0)
//    'center'      where value is within TINY of (0,0,0)
//    'scaleFactor' where value is within TINY of (1,1,1)
// and sets them to default if they are not connected from a field.
// (note that most draggers are missing at least some of these, but thats okay)
//
// Then calls the method for SoInteractionKit.
//
// NOTE: Parts which are set to default may still wind up writing to file 
//       if, for example, they lie on a path.
void
SoDragger::setDefaultOnNonWritingFields()
{
    // We don't write out isActive if it has default value.
    if ( ! (isActive.isConnected() && isActive.isConnectionEnabled())
         && isActive.getValue() == FALSE )
		      isActive.setDefault(TRUE);

    // Since so many draggers have fields named 'rotation',
    // 'translation', 'scaleFactor', and 'center', we'll check for them
    // in the base class and set them to default if we can...
#define TINY 0.00001
    SoField *f;
    if ( (f = getField( "rotation" )) != NULL ) {
	if ( !(f->isConnected() && f->isConnectionEnabled())
	     && (((SoSFRotation *)f)->getValue()).equals(
			SbRotation::identity(),TINY))
	f->setDefault(TRUE);
    }
    if ( (f = getField( "translation" )) != NULL ) {
	if ( !(f->isConnected() && f->isConnectionEnabled())
	     && (((SoSFVec3f *)f)->getValue()).equals(SbVec3f(0,0,0),TINY))
	f->setDefault(TRUE);
    }
    if ( (f = getField( "scaleFactor" )) != NULL ) {
	if ( !(f->isConnected() && f->isConnectionEnabled())
	     && (((SoSFVec3f *)f)->getValue()).equals(SbVec3f(1,1,1),TINY))
	f->setDefault(TRUE);
    }
    if ( (f = getField( "center" )) != NULL ) {
	if ( !(f->isConnected() && f->isConnectionEnabled())
	     && (((SoSFVec3f *)f)->getValue()).equals(SbVec3f(0,0,0),TINY))
	f->setDefault(TRUE);
    }
#undef TINY

    // This node may change after construction, but we still
    // don't want to write it out.
    motionMatrix.setDefault(TRUE);

    // Call the base class...
    SoInteractionKit::setDefaultOnNonWritingFields();
}