forked from hharrison/java3d-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBehaviorRetained.java
More file actions
525 lines (452 loc) · 15.2 KB
/
Copy pathBehaviorRetained.java
File metadata and controls
525 lines (452 loc) · 15.2 KB
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
/*
* 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 org.scijava.java3d;
/**
* Behavior is an abstract class that contains the framework for all
* behavioral components in Java 3D.
*/
class BehaviorRetained extends LeafRetained {
// These bitmasks are used to quickly tell what conditions this behavior
// is waiting for. Currently BehaviorStructure only used 4 of them.
static final int WAKEUP_ACTIVATE_INDEX = 0;
static final int WAKEUP_DEACTIVATE_INDEX = 1;
static final int WAKEUP_VP_ENTRY_INDEX = 2;
static final int WAKEUP_VP_EXIT_INDEX = 3;
static final int WAKEUP_TIME_INDEX = 4;
static final int NUM_WAKEUPS = 5;
static final int WAKEUP_ACTIVATE = 0x0001;
static final int WAKEUP_DEACTIVATE = 0x0002;
static final int WAKEUP_VP_ENTRY = 0x0004;
static final int WAKEUP_VP_EXIT = 0x0008;
static final int WAKEUP_TIME = 0x0010;
/**
* The number of scheduling intervals supported by this
* implementation. This is fixed for a particular implementation
* and must be at least 10.
*/
static final int NUM_SCHEDULING_INTERVALS = 10;
// different types of IndexedUnorderedSet that use in BehaviorStructure
static final int BEHAIVORS_IN_BS_LIST = 0;
static final int SCHEDULE_IN_BS_LIST = 1;
// total number of different IndexedUnorderedSet types
static final int TOTAL_INDEXED_UNORDER_SET_TYPES = 2;
/**
* The Boundary object defining the behavior's scheduling region.
*/
Bounds schedulingRegion = null;
/**
* The bounding leaf reference
*/
BoundingLeafRetained boundingLeaf = null;
/**
* The current wakeup condition.
*/
WakeupCondition wakeupCondition = null;
/**
* This is the new WakeupCondition to be set in
* initialize wakeupOn()
*/
WakeupCondition newWakeupCondition = null;
/**
* The current view platform for this behavior; this value is
* false until it comes into range of a view platform.
*/
ViewPlatformRetained vp = null;
/**
* The current activation status for this behavior; this value
* is false until it comes into range of a view platform.
*/
boolean active = false;
/**
* Flag indicating whether the behavior is enabled.
*/
boolean enable = true;
/**
* Current scheduling interval.
*/
int schedulingInterval = NUM_SCHEDULING_INTERVALS / 2;
/**
* This is a flag that tells the behavior scheduler whether the
* user-programmed process stimulus called wakeupOn, if it did
* not, then the wakeupCondition will be set to null.
*/
boolean conditionSet = false;
/**
* This is a flag that indicates whether we are in an initialize or
* processStimulus callback. If wakeupOn is called for this behavior
* when this flag is not set, an exception will be thrown.
*/
boolean inCallback = false;
/**
* This is a flag that indicates whether we are in initialize
* callback. If wakeupOn is called for this behavior when
* this flag is true, then its
* buildTree() will delay until insert nodes message
* is get. This is because some localToVworld[] that wakeup
* depends may not initialize when this behavior setLive().
*/
boolean inInitCallback = false;
/**
* The transformed schedulingRegion
*/
Bounds transformedRegion = null;
// A bitmask that indicates that the scheduling region has changed.
int isDirty = 0xffff;
/**
* A bitmask that represents all conditions that this behavior is waiting on.
*/
int wakeupMask = 0;
/**
* An array of ints that count how many of each wakup is present
*/
int[] wakeupArray = new int[NUM_WAKEUPS];
// use to post message when bounds change, always point to this
Object targets[] = new Object[1];
BehaviorRetained() {
this.nodeType = NodeRetained.BEHAVIOR;
localBounds = new BoundingBox((Bounds)null);
targets[0] = this;
IndexedUnorderSet.init(this, TOTAL_INDEXED_UNORDER_SET_TYPES);
}
/**
* Get the Behavior's scheduling region.
* @return this Behavior's scheduling region information
*/
Bounds getSchedulingBounds() {
Bounds b = null;
if (schedulingRegion != null) {
b = (Bounds) schedulingRegion.clone();
if (staticTransform != null) {
Transform3D invTransform = staticTransform.getInvTransform();
b.transform(invTransform);
}
}
return b;
}
/**
* Set the Behavior's scheduling region.
* @param region a region that contains the Behavior's new scheduling
* bounds
*/
synchronized void setSchedulingBounds(Bounds region) {
if (region != null) {
schedulingRegion = (Bounds) region.clone();
if (staticTransform != null) {
schedulingRegion.transform(staticTransform.transform);
}
} else {
schedulingRegion = null;
}
if (source != null && source.isLive()) {
sendMessage(J3dMessage.REGION_BOUND_CHANGED);
}
}
/**
* Set the Sound's scheduling region to the specified Leaf node.
*/
synchronized void setSchedulingBoundingLeaf(BoundingLeaf region) {
if (source != null && source.isLive()) {
if (boundingLeaf != null)
boundingLeaf.mirrorBoundingLeaf.removeUser(this);
}
if (region != null) {
boundingLeaf = (BoundingLeafRetained)region.retained;
} else {
boundingLeaf = null;
}
if (source != null && source.isLive()) {
if (boundingLeaf != null)
boundingLeaf.mirrorBoundingLeaf.addUser(this);
sendMessage(J3dMessage.REGION_BOUND_CHANGED);
}
}
/**
* Enables or disables this Behavior. The default state is enabled.
* @param state true or false to enable or disable this Behavior
*/
void setEnable(boolean state) {
if (enable != state) {
enable = state;
if (source != null && source.isLive()) {
sendMessage(state ? J3dMessage.BEHAVIOR_ENABLE:
J3dMessage.BEHAVIOR_DISABLE);
}
}
}
/**
* Retrieves the state of the Behavior enable flag.
* @return the Behavior enable state
*/
boolean getEnable() {
return enable;
}
/**
* Sets the scheduling interval of this Behavior node to the
* specified value.
* @param schedulingInterval the new scheduling interval
*/
void setSchedulingInterval(int schedulingInterval) {
if ((source != null) && source.isLive()
&& !inCallback) {
// avoid MT safe problem when user thread setting
// this while behavior scheduling using this.
sendMessage(J3dMessage.SCHEDULING_INTERVAL_CHANGED,
new Integer(schedulingInterval));
} else {
// garantee this setting reflect in next frame
this.schedulingInterval = schedulingInterval;
}
}
/**
* Retrieves the current scheduling interval of this Behavior
* node.
*
* @return the current scheduling interval
*/
int getSchedulingInterval() {
return schedulingInterval;
}
/**
* Get the Behavior's scheduling region
*/
BoundingLeaf getSchedulingBoundingLeaf() {
return (boundingLeaf != null ?
(BoundingLeaf)boundingLeaf.source : null);
}
/**
* This setLive routine first calls the superclass's method, then
* it activates all canvases that are associated with the attached
* view.
*/
@Override
synchronized void setLive(SetLiveState s) {
super.doSetLive(s);
if (inBackgroundGroup) {
throw new
IllegalSceneGraphException(J3dI18N.getString("BehaviorRetained0"));
}
if (inSharedGroup) {
throw new
IllegalSharingException(J3dI18N.getString("BehaviorRetained1"));
}
s.nodeList.add(this);
s.behaviorNodes.add(this);
s.notifyThreads |= J3dThread.UPDATE_BEHAVIOR;
// process switch leaf
if (s.switchTargets != null &&
s.switchTargets[0] != null) {
s.switchTargets[0].addNode(this, Targets.BEH_TARGETS);
}
switchState = s.switchStates.get(0);
if (boundingLeaf != null) {
boundingLeaf.mirrorBoundingLeaf.addUser(this);
}
if (s.transformTargets != null && s.transformTargets[0] != null) {
s.transformTargets[0].addNode(this, Targets.BEH_TARGETS);
s.notifyThreads |= J3dThread.UPDATE_TRANSFORM;
}
super.markAsLive();
}
/**
* This clearLive routine first calls the superclass's method, then
* it deactivates all canvases that are associated with the attached
* view.
*/
@Override
synchronized void clearLive(SetLiveState s) {
super.clearLive(s);
s.nodeList.add(this);
if (s.transformTargets != null && s.transformTargets[0] != null) {
s.transformTargets[0].addNode(this, Targets.BEH_TARGETS);
s.notifyThreads |= J3dThread.UPDATE_TRANSFORM;
}
s.notifyThreads |= J3dThread.UPDATE_BEHAVIOR;
if (s.switchTargets != null &&
s.switchTargets[0] != null) {
s.switchTargets[0].addNode(this, Targets.BEH_TARGETS);
}
if (boundingLeaf != null) {
boundingLeaf.mirrorBoundingLeaf.removeUser(this);
}
// BehaviorStructure removeBehavior() will do the
// wakeupCondition.cleanTree() over there.
}
/**
* This routine execute the user's initialize method
*/
void executeInitialize() {
synchronized (this) {
boolean inCallbackSaved = inCallback;
boolean inInitCallbackSaved = inInitCallback;
inCallback = true;
inInitCallback = true;
try {
((Behavior)this.source).initialize();
}
catch (RuntimeException e) {
System.err.println("Exception occurred during Behavior initialization:");
e.printStackTrace();
}
catch (Error e) {
// Issue 264 - catch Error
System.err.println("Error occurred during Behavior initialization:");
e.printStackTrace();
}
inCallback = inCallbackSaved;
inInitCallback = inInitCallbackSaved;
}
}
/**
* Defines this behavior's wakeup criteria.
* @param criteria The wakeup criterion for this object
*/
void wakeupOn(WakeupCondition criteria) {
// If not call by initialize(), buildTree will
// delay until insertNodes in BehaviorStructure
// Otherwise BehaviorScheduler will invoke
// handleLastWakeupOn()
if (criteria == null) {
throw new NullPointerException(J3dI18N.getString("BehaviorRetained2"));
}
if (!inInitCallback) {
conditionSet = true;
wakeupCondition = criteria;
} else {
// delay setting wakeup condition in BehaviorStructure
// activateBehaviors(). This is because there may have
// previously wakeupCondition attach to it and
// scheduling even after clearLive() due to message
// delay processing. It is not MT safe to set it
// in user thread.
newWakeupCondition = criteria;
}
}
// The above wakeupOn() just remember the reference
// We only need to handle (and ignore the rest) the
// last wakeupOn() condition set in the behavior.
// This handle the case when multiple wakeupOn()
// are invoked in the same processStimulus()
void handleLastWakeupOn(WakeupCondition prevWakeupCond,
BehaviorStructure bs) {
if (bs == universe.behaviorStructure) {
if (wakeupCondition == prevWakeupCond) {
// reuse the same wakeupCondition
wakeupCondition.resetTree();
} else {
if (prevWakeupCond != null) {
prevWakeupCond.cleanTree(bs);
}
wakeupCondition.buildTree(null, 0, this);
}
} else {
// No need to do prevWakeupCond.cleanTree(bs)
// since removeBehavior() will do so
}
}
/**
* Returns this behavior's wakeup criteria.
* @return criteria The wakeup criteria of this object
*/
WakeupCondition getWakeupCondition() {
return wakeupCondition;
}
/**
* Post the specified Id. Behaviors use this method to cause sequential
* scheduling of other behavior object.
* @param postId The Id being posted
*/
void postId(int postId){
if (source != null && source.isLive()) {
universe.behaviorStructure.handleBehaviorPost((Behavior) source, postId);
}
}
protected View getView() {
return (universe != null ?
universe.getCurrentView() : null);
}
synchronized void updateTransformRegion(Bounds bound) {
if (boundingLeaf == null) {
updateTransformRegion();
} else {
if (bound == null) {
transformedRegion = null;
} else {
transformedRegion = (Bounds) bound.clone();
transformedRegion.transform(
boundingLeaf.mirrorBoundingLeaf.getCurrentLocalToVworld());
}
}
}
synchronized void updateTransformRegion() {
if (boundingLeaf == null ||
!boundingLeaf.mirrorBoundingLeaf.switchState.currentSwitchOn) {
if (schedulingRegion == null) {
transformedRegion = null;
} else {
// use schedulingRegion
if (transformedRegion != null) {
transformedRegion.set(schedulingRegion);
} else {
transformedRegion = (Bounds) schedulingRegion.clone();
}
transformedRegion.transform(getCurrentLocalToVworld());
}
} else {
// use boundingLeaf
transformedRegion =
boundingLeaf.mirrorBoundingLeaf.transformedRegion;
}
}
// Note: This routine will only to update the object's
// transformed region
void updateBoundingLeaf(long refTime) {
transformedRegion = boundingLeaf.mirrorBoundingLeaf.transformedRegion;
}
void addWakeupCondition() {}
final void sendMessage(int mtype, Object arg) {
J3dMessage createMessage = new J3dMessage();
createMessage.threads = J3dThread.UPDATE_BEHAVIOR;
createMessage.type = mtype;
createMessage.universe = universe;
createMessage.args[0] = targets;
createMessage.args[1]= this;
createMessage.args[2]= arg;
VirtualUniverse.mc.processMessage(createMessage);
}
final void sendMessage(int mtype) {
sendMessage(mtype, null);
}
@Override
void mergeTransform(TransformGroupRetained xform) {
super.mergeTransform(xform);
if (schedulingRegion != null) {
schedulingRegion.transform(xform.transform);
}
if (source instanceof DistanceLOD) {
((DistanceLOD)source).mergeTransform(xform);
}
}
}