summaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/PickInfo.java
blob: 7bf381c59e4ddfd5c31667f0ef07f3ea7bf076c0 (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
/*
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package javax.media.j3d;

import java.util.ArrayList;
import java.util.Vector;

import javax.vecmath.Point3d;
import javax.vecmath.Point4d;

/**
 * The PickInfo object contains the computed information about a pick hit.
 * The detailed information about each intersection of the PickShape
 * with the picked Node can be inquired.  The PickInfo object is constructed with
 * basic information and more detailed information can be generated by setting the
 * appropriate mask to the flag argument in the pick methods of BranchGroup and
 * Locale.
 * <p>
 *
 * @see Locale
 * @see BranchGroup
 *
 * @since Java 3D 1.4
 */


public class PickInfo extends Object {

    static final int PICK_ALL = 1;

    static final int PICK_ANY = 2;

    /* The SceneGraphPath of the intersected pickable item */
    private SceneGraphPath sgp;

    /* The intersected pickable node object */
    private  Node node;

    /* A copy of LocalToVworld transform of the pickable node */
    private Transform3D l2vw;

    /* The closest intersection point */
    private Point3d closestIntersectionPoint;

    /* Distance between start point of pickShape and closest intersection point */
    private double  closestDistance;

    /* An array to store intersection results */
    private IntersectionInfo[] intersectionInfoArr;

    /* The following references are for internal geometry computation use only */
    private ArrayList<IntersectionInfo> intersectionInfoList = new ArrayList<IntersectionInfo>();
    private boolean intersectionInfoListSorted = false;
    private Transform3D l2vwRef;
    private Node nodeRef;

    /**
     * Specifies a Pick using the bounds of the pickable nodes.
     */
    public static final int PICK_BOUNDS = 1;

    /**
     * Specifies a Pick using the geometry of the pickable nodes.
     */
    public static final int PICK_GEOMETRY = 2;

    /**
   * Specifies that this PickInfo returns the computed SceneGraphPath object.
   */
    public static final int SCENEGRAPHPATH  = 0x01;

    /**
     * Specifies that this PickInfo returns the computed intersected Node object.
     */
    public static final int NODE = 0x02;

    /**
     * Specifies that this PickInfo returns the computed local to vworld transform.
     */
    public static final int LOCAL_TO_VWORLD = 0x04;

    /**
     * Specifies that this PickInfo returns the closest intersection point.
     */
    public static final int CLOSEST_INTERSECTION_POINT = 0x08;

    /**
     * Specifies that this PickInfo returns the closest intersection distance.
     */
    public static final int CLOSEST_DISTANCE = 0x10;

    /**
     * Specifies that this PickInfo returns only the closest intersection
     * geometry information.
     */
    public static final int CLOSEST_GEOM_INFO = 0x20;

    /**
     * Specifies that this PickInfo returns all the closest intersection
     * geometry informations.
     */
    public static final int ALL_GEOM_INFO = 0x40;


    /** PickInfo Constructor */
    PickInfo() {

    }

    void setSceneGraphPath(SceneGraphPath sgp) {
        this.sgp = sgp;
    }

    void setNode(Node node) {
        this.node = node;
    }

    void setLocalToVWorld(Transform3D l2vw) {
        this.l2vw = l2vw;
    }

    void setClosestIntersectionPoint(Point3d cIPt) {
        this.closestIntersectionPoint = cIPt;
    }

    void setClosestDistance(double cDist) {
        this.closestDistance = cDist;
    }

    void setLocalToVWorldRef(Transform3D l2vwRef) {
        this.l2vwRef = l2vwRef;
    }

    void setNodeRef(Node nodeRef) {
        this.nodeRef = nodeRef;
    }

    IntersectionInfo createIntersectionInfo() {
        return new IntersectionInfo();
    }

    void insertIntersectionInfo(IntersectionInfo iInfo) {
        intersectionInfoList.add(iInfo);
        intersectionInfoListSorted = false;
    }

    void sortIntersectionInfoArray(IntersectionInfo[] iInfoArr) {

        class Sort {

	    IntersectionInfo iInfoArr[];

	    Sort(IntersectionInfo[] iInfoArr) {
                // System.err.println("Sort IntersectionInfo ...");
		this.iInfoArr = iInfoArr;
	    }

	    void sorting() {
		if (iInfoArr.length < 7) {
                    // System.err.println(" -- insertSort.");
		    insertSort();
	    	} else {
                    // System.err.println(" -- quicksort.");
		    quicksort(0, iInfoArr.length-1);
    		}
	    }

	    // Insertion sort on smallest arrays
	    final void insertSort() {
		for (int i=0; i<iInfoArr.length; i++) {
		    for (int j=i; j>0 &&
                             (iInfoArr[j-1].distance > iInfoArr[j].distance); j--) {
			IntersectionInfo iInfo = iInfoArr[j];
			iInfoArr[j] = iInfoArr[j-1];
			iInfoArr[j-1] = iInfo;
		    }
		}
	    }

            final void quicksort( int l, int r ) {
		int i = l;
		int j = r;
		double k = iInfoArr[(l+r) / 2].distance;

		do {
		    while (iInfoArr[i].distance<k) i++;
		    while (k<iInfoArr[j].distance) j--;
		    if (i<=j) {
			IntersectionInfo iInfo = iInfoArr[i];
			iInfoArr[i] = iInfoArr[j];
			iInfoArr[j] = iInfo;
			i++;
			j--;
		    }
		} while (i<=j);

		if (l<j) quicksort(l,j);
		if (l<r) quicksort(i,r);
	    }
	}

	(new Sort(iInfoArr)).sorting();
        intersectionInfoListSorted = true;
    }

    static void sortPickInfoArray(PickInfo[] pickInfoArr) {

        class Sort {

	    PickInfo pIArr[];

	    Sort(PickInfo[] pIArr) {
                // System.err.println("Sort PickInfo ...");
		this.pIArr = pIArr;
	    }

	    void sorting() {
		if (pIArr.length < 7) {
                    // System.err.println(" -- insertSort.");
		    insertSort();
	    	} else {
                    // System.err.println(" -- quicksort.");
		    quicksort(0, pIArr.length-1);
    		}
	    }

	    // Insertion sort on smallest arrays
	    final void insertSort() {
		for (int i=0; i<pIArr.length; i++) {
		    for (int j=i; j>0 &&
                             (pIArr[j-1].closestDistance > pIArr[j].closestDistance); j--) {
			PickInfo pI = pIArr[j];
			pIArr[j] = pIArr[j-1];
			pIArr[j-1] = pI;
		    }
		}
	    }

            final void quicksort( int l, int r ) {
		int i = l;
		int j = r;
		double k = pIArr[(l+r) / 2].closestDistance;

		do {
		    while (pIArr[i].closestDistance<k) i++;
		    while (k<pIArr[j].closestDistance) j--;
		    if (i<=j) {
			PickInfo pI = pIArr[i];
			pIArr[i] = pIArr[j];
			pIArr[j] = pI;
			i++;
			j--;
		    }
		} while (i<=j);

		if (l<j) quicksort(l,j);
		if (l<r) quicksort(i,r);
	    }
	}

	(new Sort(pickInfoArr)).sorting();

    }


    /**
     * Retrieves the reference to the SceneGraphPath in this PickInfo object.
     * @return the SceneGraphPath object, or null if  flag is not set with SCENEGRAPHPATH.
     * @see Locale
     * @see BranchGroup
     */
    public SceneGraphPath getSceneGraphPath() {
	return sgp;
    }

    /**
     * Retrieves the reference to the picked node, either a Shape3D or a Morph, in this PickInfo object.
     * @return the picked leaf node object, or null if  flag is not set with NODE.
     * @see Locale
     * @see BranchGroup
     */
    public Node getNode() {
	return node;
    }

    /**
     * Retrieves the reference to the LocalToVworld transform of the picked node in this PickInfo object.
     * @return the local to vworld transform, or null if  flag is not set with LOCAL_TO_VWORLD.
     * @see Locale
     * @see BranchGroup
     */
    public Transform3D getLocalToVWorld() {
	return l2vw;
    }

    /**
     * Retrieves the reference to the closest intersection point in this PickInfo object.
     * @return the closest intersection point, or null if  flag is not set with CLOSEST_INTERSECTION_POINT.
     * @see Locale
     * @see BranchGroup
     */
    public Point3d getClosestIntersectionPoint() {
	return closestIntersectionPoint;
    }

    /**
     * Retrieves the distance between the start point of the pickShape and the closest intersection point.
     * @return the closest distance in double, or NaN if  flag is not set with CLOSEST_INTERSECTION_POINT.
     * Note : If this PickInfo object is returned by either pickClosest or pickAllSorted method, the return
     * value is the closest distance in double even if flag is not set with CLOSET_INTERSECTION_POINT.
     * @see Locale
     * @see BranchGroup
     */
    public double getClosestDistance() {
	return closestDistance;
    }

    Transform3D getLocalToVWorldRef() {
        return l2vwRef;
    }

    Node getNodeRef() {
        return nodeRef;
    }

    /**
     * Retrieves the reference to the array of intersection results in this PickInfo object.
     * @return an array of 1 IntersectionInfo object if flag is to set  CLOSEST_GEOM_INFO,
     * or an array of <i>N</i> IntersectionInfo objects containing all intersections of
     * the picked node in sorted order if flag is to set ALL_GEOM_INFO, or null if neither
     * bit is set.
     * @see Locale
     * @see BranchGroup
     */
    public IntersectionInfo[] getIntersectionInfos() {
        if (intersectionInfoListSorted == false) {
            intersectionInfoArr = new IntersectionInfo[intersectionInfoList.size()];
            intersectionInfoArr = intersectionInfoList.toArray(intersectionInfoArr);

            sortIntersectionInfoArray(intersectionInfoArr);
         }

        return intersectionInfoArr;
    }

/**
 * Search the path from nodeR up to Locale.
 * Return the search path as ArrayList if found.
 * Note that the locale will not insert into path.
 */
static ArrayList<NodeRetained> initSceneGraphPath(NodeRetained nodeR) {
	ArrayList<NodeRetained> path = new ArrayList<NodeRetained>(5);

	do {
		if (nodeR.source.getCapability(Node.ENABLE_PICK_REPORTING)) {
			path.add(nodeR);
		}
		nodeR = nodeR.parent;
	} while (nodeR != null); // reach Locale

	return path;
}

    static private Node[] createPath(NodeRetained srcNode,
				     BranchGroupRetained bgRetained,
				     GeometryAtom geomAtom,
				     ArrayList<NodeRetained> initpath) {

        ArrayList<NodeRetained> path = retrievePath(srcNode, bgRetained,
				      geomAtom.source.key);
        assert(path != null);

        return mergePath(path, initpath);

    }


    /**
     * Return true if bg is inside cachedBG or bg is null
     */
    static private boolean inside(BranchGroupRetained bgArr[],
				  BranchGroupRetained bg) {

	if ((bg == null) || (bgArr == null)) {
	    return true;
	}

	for (int i=0; i < bgArr.length; i++) {
	    if (bgArr[i] == bg) {
		return true;
	    }
	}
	return false;
    }

    /**
     * search the full path from the bottom of the scene graph -
     * startNode, up to the Locale if endNode is null.
     * If endNode is not null, the path is found up to, but not
     * including, endNode or return null if endNode not hit
     * during the search.
     */
    static private ArrayList<NodeRetained> retrievePath(NodeRetained startNode,
					  NodeRetained endNode,
					  HashKey key) {

	ArrayList<NodeRetained> path = new ArrayList<NodeRetained>(5);
	NodeRetained nodeR = startNode;

	if (nodeR.inSharedGroup) {
	    // getlastNodeId() will destroy this key
	    key = new HashKey(key);
	}

	do {
	    if (nodeR == endNode) { // we found it !
		return path;
	    }

	    if (nodeR.source.getCapability(Node.ENABLE_PICK_REPORTING)) {
		path.add(nodeR);
	    }

	    if (nodeR instanceof SharedGroupRetained) {
		// retrieve the last node ID
		String nodeId = key.getLastNodeId();
		Vector<NodeRetained> parents = ((SharedGroupRetained)nodeR).parents;
		int sz = parents.size();
		NodeRetained prevNodeR = nodeR;
		for(int i=0; i< sz; i++) {
			NodeRetained linkR = parents.get(i);
		    if (linkR.nodeId.equals(nodeId)) {
			nodeR = linkR;
			// Need to add Link to the path report
			path.add(nodeR);
			// since !(endNode instanceof Link), we
			// can skip the check (nodeR == endNode) and
			// proceed to parent of link below
			break;
		    }
		}
		if (nodeR == prevNodeR) {
		    // branch is already detach
		    return null;
		}
	    }
	    nodeR = nodeR.parent;
	} while (nodeR != null); // reach Locale

	if (endNode == null) {
	    // user call pickxxx(Locale locale, PickShape shape)
	    return path;
	}

	// user call pickxxx(BranchGroup endNode, PickShape shape)
	// if locale is reached and endNode not hit, this is not
	// the path user want to select
	return null;
    }

    /**
     * copy p1, (follow by) p2 into a new array, p2 can be null
     * The path is then reverse before return.
     */
    static private Node[] mergePath(ArrayList<NodeRetained> p1, ArrayList<NodeRetained> p2) {
	int s = p1.size();
	int len;
	int i;
	int l;
	if (p2 == null) {
	    len = s;
	} else {
	    len = s + p2.size();
	}

	Node nodes[] = new Node[len];
	l = len-1;
	for (i=0; i < s; i++) {
	    nodes[l-i] = (Node)p1.get(i).source;
	}
	for (int j=0; i< len; i++, j++) {
	    nodes[l-i] = (Node)p2.get(j).source;
	}
	return nodes;
    }

    /**
     * Sort the GeometryAtoms distance from shape in ascending order
     * geomAtoms.length must be >= 1
     */
    static void sortGeomAtoms(GeometryAtom geomAtoms[],
				      PickShape shape) {

	final double distance[] = new double[geomAtoms.length];
	Point4d pickPos = new Point4d();

	for (int i=0; i < geomAtoms.length; i++) {
	    shape.intersect(geomAtoms[i].source.vwcBounds, pickPos);
	    distance[i] = pickPos.w;
	}

	class Sort {

	    GeometryAtom atoms[];

	    Sort(GeometryAtom[] atoms) {
		this.atoms = atoms;
	    }

	    void sorting() {
		if (atoms.length < 7) {
		    insertSort();
	    	} else {
		    quicksort(0, atoms.length-1);
    		}
	    }

	    // Insertion sort on smallest arrays
	    final void insertSort() {
		for (int i=0; i<atoms.length; i++) {
		    for (int j=i; j>0 &&
			     (distance[j-1] > distance[j]); j--) {
			double t = distance[j];
			distance[j] = distance[j-1];
			distance[j-1] = t;
			GeometryAtom p = atoms[j];
			atoms[j] = atoms[j-1];
			atoms[j-1] = p;
		    }
		}
	    }

            final void quicksort( int l, int r ) {
		int i = l;
		int j = r;
		double k = distance[(l+r) / 2];

		do {
		    while (distance[i]<k) i++;
		    while (k<distance[j]) j--;
		    if (i<=j) {
			double tmp = distance[i];
			distance[i] =distance[j];
			distance[j] = tmp;

			GeometryAtom p=atoms[i];
			atoms[i]=atoms[j];
			atoms[j]=p;
			i++;
			j--;
		    }
		} while (i<=j);

		if (l<j) quicksort(l,j);
		if (l<r) quicksort(i,r);
	    }
	}

	(new Sort(geomAtoms)).sorting();
    }


    /**
     * return all PickInfo[] of the geomAtoms.
     * If initpath is null, the path is search from
     * geomAtom Shape3D/Morph Node up to Locale
     * (assume the same locale).
     * Otherwise, the path is search up to node or
     * null is return if it is not hit.
     */
    static ArrayList<PickInfo> getPickInfos(ArrayList<NodeRetained> initpath,
                                  BranchGroupRetained bgRetained,
				  GeometryAtom geomAtoms[],
			          Locale locale, int flags, int pickType) {

        ArrayList<PickInfo> pickInfoList = new ArrayList<PickInfo>(5);
        NodeRetained srcNode;
        ArrayList text3dList = null;

        if ((geomAtoms == null) || (geomAtoms.length == 0)) {
            return null;
        }

	for (int i=0; i < geomAtoms.length; i++) {
            assert((geomAtoms[i] != null) &&
                    (geomAtoms[i].source != null));

	    PickInfo pickInfo = null;
            Shape3DRetained shape = geomAtoms[i].source;
            srcNode = shape.sourceNode;

            // Fix to Issue 274 : NPE With Simultaneous View and Content Side PickingBehaviors
            // This node isn't under the selected BG for pick operation.
            if (!inside(shape.branchGroupPath,bgRetained)) {
                continue;
            }

            if (srcNode == null) {
                // The node is just detach from branch so sourceNode = null
                continue;
            }


            // Special case, for Text3DRetained, it is possible
            // for different geomAtoms pointing to the same
            // source Text3DRetained. So we need to combine
            // those cases and report only once.
            if (srcNode instanceof Shape3DRetained) {
                Shape3DRetained s3dR = (Shape3DRetained) srcNode;
                GeometryRetained geomR = null;
                for(int cnt=0; cnt<s3dR.geometryList.size(); cnt++) {
				geomR = s3dR.geometryList.get(cnt);
                    if(geomR != null)
                        break;
                }

                if (geomR == null)
                    continue;

                if (geomR instanceof Text3DRetained) {
                    // assume this case is not frequent, we allocate
                    // ArrayList only when necessary and we use ArrayList
                    // instead of HashMap since the case of when large
                    // number of distingish Text3DRetained node hit is
                    // rare.
                    if (text3dList == null) {
                        text3dList = new ArrayList(3);
                    } else {
                        int size = text3dList.size();
                        boolean found = false;
                        for (int j=0; j < size; j++) {
                            if (text3dList.get(j) == srcNode) {
                                found = true;
                                break;
                            }
                        }
                        if (found) {
                            continue;  // try next geomAtom
                        }
                    }
                    text3dList.add(srcNode);
                }
            }

            // If srcNode is instance of compile retained, then loop thru
            // the entire source list and add it to the scene graph path
            if (srcNode instanceof Shape3DCompileRetained) {

                Shape3DCompileRetained s3dCR = (Shape3DCompileRetained)srcNode;

                Node[] mpath = null;
                boolean first = true;

                for (int n = 0; n < s3dCR.srcList.length; n++) {

                    pickInfo = null;

                    // PickInfo.SCENEGRAPHPATH - request for computed SceneGraphPath.
                    if ((flags & SCENEGRAPHPATH) != 0){

                        if(first) {
                            mpath = createPath(srcNode, bgRetained, geomAtoms[i], initpath);
                            first = false;
                        }

                        if(mpath != null) {
                            SceneGraphPath sgpath = new SceneGraphPath(locale,
                                    mpath, (Node) s3dCR.srcList[n]);
                            sgpath.setTransform(shape.getCurrentLocalToVworld(0));
			    if(pickInfo == null)
				pickInfo = new PickInfo();
                            pickInfo.setSceneGraphPath(sgpath);
                        }
                    }

                    // PickInfo.NODE - request for computed intersected Node.
                    if ((flags & NODE) != 0) {
			if(pickInfo == null)
			    pickInfo = new PickInfo();
                        pickInfo.setNode((Node) s3dCR.srcList[n]);
                    }

                    // PickInfo.LOCAL_TO_VWORLD
                    //    - request for computed local to virtual world transform.
                    if ((flags & LOCAL_TO_VWORLD) != 0) {
                        Transform3D l2vw = geomAtoms[i].source.getCurrentLocalToVworld();
			if(pickInfo == null)
			    pickInfo = new PickInfo();
                        pickInfo.setLocalToVWorld( new Transform3D(l2vw));
                    }

                    // NOTE : Piggy bag for geometry computation by caller.
                    if (((flags & CLOSEST_DISTANCE) != 0) ||
                        ((flags & CLOSEST_GEOM_INFO) != 0) ||
                        ((flags & CLOSEST_INTERSECTION_POINT) != 0) ||
                        ((flags & ALL_GEOM_INFO) != 0)) {
			if(pickInfo == null)
			    pickInfo = new PickInfo();
                        pickInfo.setNodeRef((Node) s3dCR.srcList[n]);
                        Transform3D l2vw = geomAtoms[i].source.getCurrentLocalToVworld();
			pickInfo.setLocalToVWorldRef(l2vw);
                    }

		    if(pickInfo != null)
			pickInfoList.add(pickInfo);
                    if(pickType == PICK_ANY) {
                        return pickInfoList;
                    }
                }
            }
            else {
                Node[] mpath = null;

                // PickInfo.SCENEGRAPHPATH - request for computed SceneGraphPath.
                if ((flags & SCENEGRAPHPATH) != 0) {

                    mpath = createPath(srcNode, bgRetained, geomAtoms[i], initpath);

                    if(mpath != null) {
                        SceneGraphPath sgpath = new SceneGraphPath(locale, mpath,
                                (Node) srcNode.source);
                        sgpath.setTransform(shape.getCurrentLocalToVworld(0));
                        if(pickInfo == null)
                            pickInfo = new PickInfo();
                        pickInfo.setSceneGraphPath(sgpath);
                    }
                }

                // PickInfo.NODE - request for computed intersected Node.
                if ((flags & NODE) != 0) {
		    if(pickInfo == null)
			pickInfo = new PickInfo();
                    pickInfo.setNode((Node) srcNode.source);
                }

                // PickInfo.LOCAL_TO_VWORLD
                //    - request for computed local to virtual world transform.
                if ((flags & LOCAL_TO_VWORLD) != 0) {
                    Transform3D l2vw = geomAtoms[i].source.getCurrentLocalToVworld();
		    if(pickInfo == null)
			pickInfo = new PickInfo();
                    pickInfo.setLocalToVWorld( new Transform3D(l2vw));
                }

                // NOTE : Piggy bag for geometry computation by caller.
                if (((flags & CLOSEST_DISTANCE) != 0) ||
                    ((flags & CLOSEST_GEOM_INFO) != 0) ||
                    ((flags & CLOSEST_INTERSECTION_POINT) != 0) ||
                    ((flags & ALL_GEOM_INFO) != 0)) {
		    if(pickInfo == null)
			pickInfo = new PickInfo();
                    pickInfo.setNodeRef((Node) srcNode.source);
                    Transform3D l2vw = geomAtoms[i].source.getCurrentLocalToVworld();
                    pickInfo.setLocalToVWorldRef(l2vw);
                }

		if(pickInfo != null)
		    pickInfoList.add(pickInfo);
                if(pickType == PICK_ANY) {
                    return pickInfoList;
                }
            }
        }

	return pickInfoList;
    }

    static PickInfo[] pick(Object node, GeometryAtom[] geomAtoms,
            int mode, int flags, PickShape pickShape, int pickType) {

        int pickInfoListSize;
        PickInfo[] pickInfoArr = null;
        Locale locale = null;
        BranchGroupRetained bgRetained = null;
        ArrayList<PickInfo> pickInfoList = null;

        if (node instanceof Locale) {
            locale = (Locale) node;
        }
        else if ( node instanceof BranchGroupRetained) {
            bgRetained = (BranchGroupRetained) node;
            locale = bgRetained.locale;
        }
        synchronized (locale.universe.sceneGraphLock) {
            ArrayList<NodeRetained> initPath = null;
            if ( bgRetained != null) {
                initPath = initSceneGraphPath(bgRetained);
            }
            pickInfoList = getPickInfos(initPath, bgRetained, geomAtoms,
                locale, flags, pickType);
        }

        // We're done with PICK_BOUNDS case, but there is still more work for PICK_GEOMETRY case.
        if((mode == PICK_GEOMETRY) && (pickInfoList != null) &&
	   ((pickInfoListSize = pickInfoList.size()) > 0)) {

            //System.err.println("PickInfo.pick() - In geometry case : pickInfoList.size() is " + pickInfoListSize);
            Node pickNode = null;

            // Order is impt. Need to do in reverse order.
            for(int i = pickInfoListSize - 1; i >= 0; i--) {
            	PickInfo pickInfo = pickInfoList.get(i);

                pickNode = pickInfo.getNode();
                if( pickNode == null) {
                    // Use the piggy reference from getPickInfos()
                    pickNode = pickInfo.getNodeRef();
                }

                if (pickNode instanceof Shape3D) {

		    /*
		     * @exception CapabilityNotSetException if the mode is
		     * PICK_GEOMETRY and the Geometry.ALLOW_INTERSECT capability bit
		     * is not set in any Geometry objects referred to by any shape
		     * node whose bounds intersects the PickShape.
		     *
                     * @exception CapabilityNotSetException if flags contains any of
		     * CLOSEST_INTERSECTION_POINT, CLOSEST_DISTANCE, CLOSEST_GEOM_INFO
		     * or ALL_GEOM_INFO, and the capability bits that control reading of
		     * coordinate data are not set in any GeometryArray object referred
		     * to by any shape node that intersects the PickShape.
		     * The capability bits that must be set to avoid this exception are
                     * as follows :
		     *
		     * By-copy geometry : GeometryArray.ALLOW_COORDINATE_READ
		     * By-reference geometry : GeometryArray.ALLOW_REF_DATA_READ
		     * Indexed geometry : IndexedGeometryArray.ALLOW_COORDINATE_INDEX_READ
		     * (in addition to one of the above)
		     *
		     */

                    if (!pickNode.getCapability(Shape3D.ALLOW_GEOMETRY_READ)) {
			throw new CapabilityNotSetException(J3dI18N.getString("PickInfo0"));
		    }

		    for (int j = 0; j < ((Shape3D)pickNode).numGeometries(); j++) {
			Geometry geo = ((Shape3D)pickNode).getGeometry(j);

			if(geo == null) {
			    continue;
			}

			if(!geo.getCapability(Geometry.ALLOW_INTERSECT)) {
			    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo1"));
			}

			if (geo instanceof GeometryArray) {
			    if(!geo.getCapability(GeometryArray.ALLOW_COORDINATE_READ))
				throw new CapabilityNotSetException(J3dI18N.getString("PickInfo2"));
			    if(!geo.getCapability(GeometryArray.ALLOW_COUNT_READ))
				throw new CapabilityNotSetException(J3dI18N.getString("PickInfo3"));
			    if(!geo.getCapability(GeometryArray.ALLOW_FORMAT_READ))
				throw new CapabilityNotSetException(J3dI18N.getString("PickInfo4"));
			    if (geo instanceof IndexedGeometryArray) {
				if(!geo.getCapability(IndexedGeometryArray.ALLOW_COORDINATE_INDEX_READ))
				    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo5"));
			    }
			} else if (geo instanceof CompressedGeometry) {
			    if(!geo.getCapability(CompressedGeometry.ALLOW_GEOMETRY_READ))
				throw new CapabilityNotSetException(J3dI18N.getString("PickInfo0"));
			}
		    }

		    if (((Shape3DRetained)(pickNode.retained)).intersect(pickInfo, pickShape, flags) == false) {
			// System.err.println("  ---- geom " + i + " not intersected");

                        pickInfoList.remove(i);

                    }
                    else if(pickType == PICK_ANY) {
                        pickInfoArr = new PickInfo[1];
                        pickInfoArr[0] = pickInfo;
                        return pickInfoArr;
                    }
                } else if (pickNode instanceof Morph) {

		    /*
		     * @exception CapabilityNotSetException if the mode is
		     * PICK_GEOMETRY and the Geometry.ALLOW_INTERSECT capability bit
		     * is not set in any Geometry objects referred to by any shape
		     * node whose bounds intersects the PickShape.
		     *
                     * @exception CapabilityNotSetException if flags contains any of
		     * CLOSEST_INTERSECTION_POINT, CLOSEST_DISTANCE, CLOSEST_GEOM_INFO
		     * or ALL_GEOM_INFO, and the capability bits that control reading of
		     * coordinate data are not set in any GeometryArray object referred
		     * to by any shape node that intersects the PickShape.
		     * The capability bits that must be set to avoid this exception are
                     * as follows :
		     *
		     * By-copy geometry : GeometryArray.ALLOW_COORDINATE_READ
		     * By-reference geometry : GeometryArray.ALLOW_REF_DATA_READ
		     * Indexed geometry : IndexedGeometryArray.ALLOW_COORDINATE_INDEX_READ
		     * (in addition to one of the above)
		     *
		     */

                    if (!pickNode.getCapability(Morph.ALLOW_GEOMETRY_ARRAY_READ)) {
			throw new CapabilityNotSetException(J3dI18N.getString("PickInfo6"));
		    }

		    int numGeo = ((MorphRetained)(pickNode.retained)).getNumGeometryArrays();
		    for (int j = 0; j < numGeo; j++) {
			GeometryArray geo = ((Morph)pickNode).getGeometryArray(j);

			if(geo == null) {
			    continue;
			}

			if(!geo.getCapability(Geometry.ALLOW_INTERSECT)) {
			    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo1"));
			}

			if(!geo.getCapability(GeometryArray.ALLOW_COORDINATE_READ))
			    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo2"));
			if(!geo.getCapability(GeometryArray.ALLOW_COUNT_READ))
			    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo3"));
			if(!geo.getCapability(GeometryArray.ALLOW_FORMAT_READ))
			    throw new CapabilityNotSetException(J3dI18N.getString("PickInfo4"));

			if (geo instanceof IndexedGeometryArray) {
			    if(!geo.getCapability(IndexedGeometryArray.ALLOW_COORDINATE_INDEX_READ))
				throw new CapabilityNotSetException(J3dI18N.getString("PickInfo5"));
			}
		    }

                    if (((MorphRetained)(pickNode.retained)).intersect(pickInfo, pickShape, flags) == false) {
                        pickInfoList.remove(i);
                    }
                    else if(pickType == PICK_ANY) {
                        pickInfoArr = new PickInfo[1];
                        pickInfoArr[0] = pickInfo;
                        return pickInfoArr;
                    }
                }
            }
        }

	// System.err.println("PickInfo : pickInfoList " + pickInfoList);

        if ((pickInfoList != null) && (pickInfoList.size() > 0)) {
	    // System.err.println("   ---  : pickInfoList.size() " + pickInfoList.size());
	    // System.err.println("   ---  : pickInfoList's sgp " +
	    // ((PickInfo)(pickInfoList.get(0))).getSceneGraphPath());
	    pickInfoArr = new PickInfo[pickInfoList.size()];
	    return pickInfoList.toArray(pickInfoArr);
	}

	return null;

    }

    /**
     * The IntersectionInfo object holds extra information about an intersection
     * of a PickShape with a Node as part of a PickInfo. Information such as
     * the intersected geometry, the intersected point, and the vertex indices
     * can be inquired.
     * The local coordinates, normal, color and texture coordiantes of at the
     * intersection can be computed, if they are present and readable, using the
     * interpolation weights and vertex indices.
     * <p>
     * If the Shape3D being picked has multiple geometry arrays, the possible arrays
     * of IntersectionInfo are stored in the PickInfo and referred to by a geometry
     * index. If the picked geometry is of type, Text3D or CompressGeometry,
     * getVertexIndices is invalid. If the picked Node is an Morph
     * object, the geometry used in pick computation is alway at index 0.
     * <p>
     *
     * @since Java 3D 1.4
     */

    public class IntersectionInfo extends Object {

	/* The index to the intersected geometry in the pickable node */
	private int geomIndex;

        /* The reference to the intersected geometry in the pickable object */
	private Geometry geom;

	/* The intersection point */
	private Point3d intersectionPoint;

	/* Distance between start point of pickShape and intersection point */
	private double  distance;

	/* The vertex indices of the intersected primitive in the geometry */
	private int[] vertexIndices;

	/* The interpolation weights for each of the verticies of the primitive */
	// private float[] weights;  Not supported. Should be done in util. package

	/** IntersectionInfo Constructor */
	IntersectionInfo() {

	}

        void setGeometryIndex(int geomIndex) {
            this.geomIndex = geomIndex;
        }

        void setGeometry(Geometry geom) {
            this.geom = geom;
        }

        void setIntersectionPoint(Point3d intersectionPoint) {
	    assert(intersectionPoint != null);
	    this.intersectionPoint = new Point3d(intersectionPoint);
        }

        void setDistance(double distance) {
            this.distance = distance;
        }

        void setVertexIndices(int[] vertexIndices) {
	    assert(vertexIndices != null);
	    this.vertexIndices = new int[vertexIndices.length];
	    for(int i=0; i<vertexIndices.length; i++) {
		this.vertexIndices[i] = vertexIndices[i];
	    }
	}


	/**
	 * Retrieves the index to the intersected geometry in the picked node, either a Shape3D or Morph.
	 * @return the index of the intersected geometry in the pickable node.
	 */
	public int getGeometryIndex() {
	    return geomIndex;
	}

	/**
	 * Retrieves the reference to the intersected geometry in the picked object, either a Shape3D or Morph.
	 * @return the intersected geometry in the pickable node.
	 */
	public Geometry getGeometry() {
	    return geom;
	}

	/**
	 * Retrieves the reference to the intersection point in the pickable node.
	 * @return the intersected point in the pickable node.
	 */
	public Point3d getIntersectionPoint() {
	    return intersectionPoint;
	}

	/**
	 * Retrieves the distance between the start point of the pickShape and the
	 * intersection point.
	 * @return distance between the start point of the pickShape and the
	 * intersection point.
	 */
	public double getDistance() {
	    return distance;
	}

	/**
	 * Retrieves the vertex indices of the intersected primitive in the geometry.
	 * @return the vertex indices of the intersected primitive.
	 */
	public int[] getVertexIndices() {
	    return vertexIndices;
	}

    }
}