aboutsummaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/BranchGroupRetained.java
blob: 47372d0e49c28c007c9fe1ce121e538143da9494 (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
/*
 * Copyright 1996-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;

/**
 * The BranchGroup node provides the ability to insert a branch of
 * a scene graph into the virtual world.
 */

class BranchGroupRetained extends GroupRetained {

    // This boolean is used in a moveTo operation to get transforms to update
    boolean isDirty = false;

    // This boolean is used when the BG is inserted into the scene
    boolean isNew = false;

    /**
     * True, if this branchGroup is directly attached to the locale
     */
    boolean attachedToLocale = false;


    BranchGroupRetained() {
        this.nodeType = NodeRetained.BRANCHGROUP;
    }

    /**
     * This sets the current locale.
     */
    void setLocale(Locale loc) {
         locale = loc;
    }

    /**
     * This gets the current locale
     */
    @Override
    Locale getLocale() {
         return locale;
    }

    /**
     * Detaches this BranchGroup from its parent.
     */
    void detach() {
        dirtyBoundsCache();
	if (universe != null) {
	    universe.resetWaitMCFlag();
	    synchronized (universe.sceneGraphLock) {
                boolean isLive = source.isLive();
		if (isLive) {
	            notifySceneGraphChanged(true);
		}
                GroupRetained oldParent = (GroupRetained)parent;
	        do_detach();
		universe.setLiveState.clear();
                if (isLive) {
                    if (oldParent==null) {
                        universe.notifyStructureChangeListeners(false,locale,(BranchGroup)this.source);
                    } else {
                        universe.notifyStructureChangeListeners(false,oldParent.source, (BranchGroup)this.source);
                    }
                }
	    }
	    universe.waitForMC();
	} else { // Not live yet, just do it.
	    this.do_detach();
	    if (universe != null) {
		synchronized (universe.sceneGraphLock) {
		    universe.setLiveState.clear();
		}
	    }
	}
    }

    // The method that does the work once the lock is acquired.
    void do_detach() {
	if (attachedToLocale) {
	    locale.doRemoveBranchGraph((BranchGroup)this.source, null, 0);
	} else if (parent != null) {
	    GroupRetained g = (GroupRetained)parent;
	    g.doRemoveChild(g.children.indexOf(this), null, 0);
	}
    }

    @Override
    void setNodeData(SetLiveState s) {
	// super.setNodeData will set branchGroupPaths
	// based on s.parentBranchGroupPaths, we need to
	// set it earlier in order to undo the effect.
	s.parentBranchGroupPaths = branchGroupPaths;

        super.setNodeData(s);

        if (!inSharedGroup) {
            setAuxData(s, 0, 0);
        } else {
            // For inSharedGroup case.
            int j, hkIndex;
            for(j=0; j<s.keys.length; j++) {
                hkIndex = s.keys[j].equals(localToVworldKeys, 0,
						localToVworldKeys.length);

                if(hkIndex >= 0) {
                    setAuxData(s, j, hkIndex);

                } else {
		    // XXXX: change this to an assertion exception
                    MasterControl.getCoreLogger().severe("Can't Find matching hashKey in setNodeData.");
                }
            }
        }
    }


    @Override
    void setAuxData(SetLiveState s, int index, int hkIndex) {
	super.setAuxData(s, index, hkIndex);

	BranchGroupRetained path[] = s.branchGroupPaths.get(index);
	BranchGroupRetained clonePath[] = new BranchGroupRetained[path.length + 1];
	System.arraycopy(path, 0, clonePath, 0, path.length);
	clonePath[path.length] = this;
	s.branchGroupPaths.set(index, clonePath);
	branchGroupPaths.add(hkIndex, clonePath);
    }


    /**
     * remove the localToVworld transform for this node.
     */
    @Override
    void removeNodeData(SetLiveState s) {

	if((!inSharedGroup) || (s.keys.length == localToVworld.length)) {
	    // restore to default and avoid calling clear()
	    // that may clear parent reference branchGroupPaths
	    branchGroupPaths = new ArrayList<BranchGroupRetained[]>(1);
	}
	else {
	    int i, index;
	    // Must be in reverse, to preserve right indexing.
	    for (i = s.keys.length-1; i >= 0; i--) {
		index = s.keys[i].equals(localToVworldKeys, 0, localToVworldKeys.length);
		if(index >= 0) {
		    branchGroupPaths.remove(index);
		}
	    }
	    // Set it back to its parent localToVworld data. This is b/c the parent has
	    // changed it localToVworld data arrays.
	}
        super.removeNodeData(s);
    }

    @Override
    void setLive(SetLiveState s) {
	// recursively call child
	super.doSetLive(s);
	super.markAsLive();
    }


    // Top level compile call
    void compile()  {

	if (source.isCompiled() || VirtualUniverse.mc.disableCompile)
	    return;

	if (J3dDebug.devPhase && J3dDebug.debug) {
	    J3dDebug.doDebug(J3dDebug.compileState, J3dDebug.LEVEL_3,
			"BranchGroupRetained.compile()....\n");
	}

	CompileState compState = new CompileState();

	isRoot = true;

	compile(compState);
	merge(compState);

	if (J3dDebug.devPhase && J3dDebug.debug) {
	    if (J3dDebug.doDebug(J3dDebug.compileState, J3dDebug.LEVEL_3)) {
		compState.printStats();
	    }
	    if (J3dDebug.doDebug(J3dDebug.compileState, J3dDebug.LEVEL_5)) {
		this.traverse(false, 1);
		System.err.println();
	    }
	}
    }

    @Override
    void compile(CompileState compState) {
	// if this branch group is previously compiled, don't
	// go any further. Mark the keepTG flag for now. Static
	// transform doesn't go beyond previously compiled group.

	if (mergeFlag == SceneGraphObjectRetained.MERGE_DONE) {
	    compState.keepTG = true;
	    return;
	}

	super.compile(compState);

	// don't remove this node because user can do pickAll on this node
	// without any capabilities set
	mergeFlag = SceneGraphObjectRetained.DONT_MERGE;
    }

    SceneGraphPath[] pickAll(PickShape pickShape) {

        PickInfo[] pickInfoArr = pickAll(PickInfo.PICK_BOUNDS,
					 PickInfo.SCENEGRAPHPATH, pickShape);

	if(pickInfoArr == null) {
            return null;
	}

        SceneGraphPath[] sgpArr = new SceneGraphPath[pickInfoArr.length];
        for( int i=0; i<sgpArr.length; i++) {
            sgpArr[i] = pickInfoArr[i].getSceneGraphPath();
        }

        return sgpArr;
    }

    PickInfo[] pickAll( int mode, int flags, PickShape pickShape ) {

	if (inSharedGroup) {
	    throw new RestrictedAccessException(J3dI18N.getString("BranchGroup9"));
	}

	GeometryAtom geomAtoms[] =
	    locale.universe.geometryStructure.pickAll(locale, pickShape);

        return PickInfo.pick(this, geomAtoms, mode, flags, pickShape, PickInfo.PICK_ALL);

    }

    SceneGraphPath[] pickAllSorted(PickShape pickShape) {

        PickInfo[] pickInfoArr = pickAllSorted(PickInfo.PICK_BOUNDS,
					       PickInfo.SCENEGRAPHPATH, pickShape);

	if(pickInfoArr == null) {
            return null;
	}

        SceneGraphPath[] sgpArr = new SceneGraphPath[pickInfoArr.length];
        for( int i=0; i<sgpArr.length; i++) {
            sgpArr[i] = pickInfoArr[i].getSceneGraphPath();
        }

        return sgpArr;

    }

    PickInfo[] pickAllSorted( int mode, int flags, PickShape pickShape ) {

	if (inSharedGroup) {
	    throw new RestrictedAccessException(J3dI18N.getString("BranchGroup9"));
	}

	GeometryAtom geomAtoms[] =
	    locale.universe.geometryStructure.pickAll(locale, pickShape);

        PickInfo[] pickInfoArr  = null;


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

	if (mode == PickInfo.PICK_GEOMETRY) {
            // Need to have closestDistance set
            flags |= PickInfo.CLOSEST_DISTANCE;
            pickInfoArr= PickInfo.pick(this, geomAtoms, mode, flags,
				       pickShape, PickInfo.PICK_ALL);
            if (pickInfoArr != null) {
                PickInfo.sortPickInfoArray(pickInfoArr);
            }
        }
        else {
            PickInfo.sortGeomAtoms(geomAtoms, pickShape);
            pickInfoArr= PickInfo.pick(this, geomAtoms, mode, flags,
				       pickShape, PickInfo.PICK_ALL);
        }

        return pickInfoArr;

    }

    SceneGraphPath pickClosest( PickShape pickShape ) {

        PickInfo pickInfo = pickClosest( PickInfo.PICK_BOUNDS,
					 PickInfo.SCENEGRAPHPATH, pickShape);

        if(pickInfo == null) {
            return null;
        }

        return pickInfo.getSceneGraphPath();

    }

    PickInfo pickClosest( int mode, int flags, PickShape pickShape ) {

        PickInfo[] pickInfoArr = null;

        pickInfoArr = pickAllSorted( mode, flags, pickShape );

        if(pickInfoArr == null) {
            return null;
        }

        return pickInfoArr[0];

    }

    SceneGraphPath pickAny( PickShape pickShape ) {

        PickInfo pickInfo = pickAny( PickInfo.PICK_BOUNDS,
				     PickInfo.SCENEGRAPHPATH, pickShape);

        if(pickInfo == null) {
            return null;
        }
        return pickInfo.getSceneGraphPath();
    }

    PickInfo pickAny( int mode, int flags, PickShape pickShape ) {

	if (inSharedGroup) {
	    throw new RestrictedAccessException(J3dI18N.getString("BranchGroup9"));
	}

	GeometryAtom geomAtoms[] =
	    locale.universe.geometryStructure.pickAll(locale, pickShape);

        PickInfo[] pickInfoArr = PickInfo.pick(this, geomAtoms, mode,
					       flags, pickShape, PickInfo.PICK_ANY);

        if(pickInfoArr == null) {
            return null;
        }

        return pickInfoArr[0];

    }
}