1
package jp.go.aist.hrp.simulator;
3
import javax.media.j3d.TransformGroup;
4
import javax.media.j3d.BranchGroup;
5
import java.util.Hashtable;
6
import java.util.Enumeration;
7
import com.sun.j3d.loaders.vrml97.node.*;
8
import com.sun.j3d.loaders.vrml97.impl.Route;
9
//import com.sun.j3d.loaders.vrml97.impl.*;
12
import java.util.StringTokenizer;
15
// ���Υ��饹�� VrmlScene �Τޤޥ��ԡ��������
16
// ����� getField �� getNodeTypeString ���ɲä�����ΤǤ���
17
// ��äư����Υ����ȤϤ�Ȥ� VrmlScene �Τ�ΤǤ���
20
public class VrmlSceneEx implements com.sun.j3d.loaders.Scene {
21
com.sun.j3d.loaders.vrml97.impl.Scene base;
25
Viewpoint[] viewpoints;
27
Background[] backgrounds;
30
Hashtable<Object, Object> defTable;
31
BranchGroup scene = null;
32
// proto �����Ƥ���¸���Ƥ���
35
// ���β�����Ĥ���ʬ���ɲä�����åɤǤ��롣
36
// ���Υ�åɤ� def ��̾���� field ��̾������ꤷ�� Field ���֤����Ȥ������ΤޤޤΥ�åɤǤ���
37
// �֤��ʤ����ϤȤꤢ���� null ���֤���
38
public com.sun.j3d.loaders.vrml97.impl.Field getField(String defName,String fieldName)
40
// �ޤ� getNamedObjects �� def ̾�ΰ������������¸���Ƥ���
44
com.sun.j3d.loaders.vrml97.impl.BaseNode node = base.use(defName);
45
return node.getField(fieldName);
52
// def ��̾������ꤷ�Ƥ���̾���ȥ���֤��Ȥ������ΤޤޤΥ�åɤǤ���
53
public String getNodeTypeString(String defName)
57
com.sun.j3d.loaders.vrml97.impl.BaseNode node = base.use(defName);
58
return node.wrap().getType();
65
VrmlSceneEx(com.sun.j3d.loaders.vrml97.impl.Scene base) {
67
description = base.description;
68
numTris = base.numTris;
70
objects = new BaseNode[base.objects.size()];
71
Enumeration elems = base.objects.elements();
72
for (int i = 0; i < objects.length; i++) {
73
com.sun.j3d.loaders.vrml97.impl.BaseNode node =
74
(com.sun.j3d.loaders.vrml97.impl.BaseNode)elems.nextElement();
75
objects[i] = node.wrap();
78
viewpoints = new Viewpoint[base.viewpoints.size()];
79
elems = base.viewpoints.elements();
80
for (int i = 0; i < viewpoints.length; i++) {
81
viewpoints[i] = (com.sun.j3d.loaders.vrml97.node.Viewpoint)
82
((com.sun.j3d.loaders.vrml97.impl.BaseNode)
83
elems.nextElement()).wrap();
86
navInfos = new Node[base.navInfos.size()];
87
elems = base.navInfos.elements();
88
for (int i = 0; i < navInfos.length; i++) {
89
navInfos[i] = (vrml.node.Node)
90
((com.sun.j3d.loaders.vrml97.impl.BaseNode)
91
elems.nextElement()).wrap();
94
backgrounds = new Background[base.backgrounds.size()];
95
elems = base.backgrounds.elements();
96
for (int i = 0; i < backgrounds.length; i++) {
97
backgrounds[i] = (com.sun.j3d.loaders.vrml97.node.Background)
98
((com.sun.j3d.loaders.vrml97.impl.BaseNode)
99
elems.nextElement()).wrap();
102
fogs = new Fog[base.fogs.size()];
103
elems = base.fogs.elements();
104
for (int i = 0; i < fogs.length; i++) {
105
fogs[i] = (com.sun.j3d.loaders.vrml97.node.Fog)
106
((com.sun.j3d.loaders.vrml97.impl.BaseNode)
107
elems.nextElement()).wrap();
110
lights = new Light[base.lights.size()];
111
elems = base.lights.elements();
112
for (int i = 0; i < lights.length; i++) {
113
lights[i] = (com.sun.j3d.loaders.vrml97.node.Light)
114
((com.sun.j3d.loaders.vrml97.impl.BaseNode)
115
elems.nextElement()).wrap();
118
defTable = new Hashtable<Object, Object>();
119
for (elems = base.defTable.keys(); elems.hasMoreElements();){
120
Object key = elems.nextElement();
121
Object value = ((com.sun.j3d.loaders.vrml97.impl.BaseNode)
122
base.defTable.get(key)).wrap();
123
defTable.put(key, value);
128
* Returns the root nodes of the VRML scene, gathered into a BranchGroup
130
public BranchGroup getSceneGroup() {
132
scene = new BranchGroup();
133
for (int i = 0; i < objects.length; i++) {
134
javax.media.j3d.Node j3dNode;
135
if ((j3dNode = objects[i].getImplNode()) != null) {;
136
scene.addChild(j3dNode);
144
* Returns the TransformGroups associated with the VRML Viewpoints.
145
* The TransformGroups returned will be parented withing the SceneGroup.
146
* The ViewPlatform will be the child of the TransformGroup
148
public TransformGroup[] getViewGroups() {
149
TransformGroup[] views = new TransformGroup[viewpoints.length];
150
for (int i = 0; i < viewpoints.length; i++) {
151
views[i] = (TransformGroup)viewpoints[i].getImplNode();
157
* Returns the horizontal field of view values for the Viewpoints in
160
public float[] getHorizontalFOVs() {
161
float[] fovs = new float[viewpoints.length];
162
for (int i = 0; i < viewpoints.length; i++) {
163
fovs[i] = viewpoints[i].getFOV();
169
* Returns Java3D Light nodes in the scene.
170
* The Light nodes returned will be parented within the SceneGroup
172
public javax.media.j3d.Light[] getLightNodes() {
173
javax.media.j3d.Light[] j3dLights =
174
new javax.media.j3d.Light[lights.length * 2];
175
for (int i = 0; i < lights.length; i++) {
176
j3dLights[i*2] = lights[i].getAmbientLight();
177
j3dLights[i*2 + 1] = lights[i].getLight();
183
* Gets a Hashtable containing the VRML DEF table, with the key being the
184
* DEF name and the value being the Java3D SceneGraphObject associated
185
* with the VRML Node.
187
public Hashtable<Object, javax.media.j3d.SceneGraphObject> getNamedObjects() {
188
Hashtable<Object, javax.media.j3d.SceneGraphObject> j3dDefTable
189
= new Hashtable<Object, javax.media.j3d.SceneGraphObject>();
190
for (Enumeration elems = defTable.keys();
191
elems.hasMoreElements();){
192
Object key = elems.nextElement();
193
BaseNode node = (BaseNode)defTable.get(key);
194
javax.media.j3d.SceneGraphObject value = node.getImplObject();
196
j3dDefTable.put(key, value);
203
* Returns the J3D Background nodes in the scene.
204
* The Background nodes returned will be parented within the SceneGroup
206
public javax.media.j3d.Background[] getBackgroundNodes() {
207
javax.media.j3d.Background[] j3dBackgrounds =
208
new javax.media.j3d.Background[backgrounds.length];
209
for (int i = 0; i < backgrounds.length; i++) {
210
j3dBackgrounds[i] = backgrounds[i].getBackgroundImpl();
212
return j3dBackgrounds;
216
* Returns the J3D Fog nodes in the scene.
217
* The Fog nodes returned will be parented within the SceneGroup
219
public javax.media.j3d.Fog[] getFogNodes() {
220
javax.media.j3d.Fog[] j3dFogs = new javax.media.j3d.Fog[fogs.length];
221
for (int i = 0; i < fogs.length; i++) {
222
j3dFogs[i] = fogs[i].getFogImpl();
228
* The VRML loader does not support loading behaviors, this method
231
public javax.media.j3d.Behavior[] getBehaviorNodes() {
237
* The VRML loader does not support loading sounds, this method
240
public javax.media.j3d.Sound[] getSoundNodes() {
245
* Returns the description (if any) from the first WorldInfo node
246
* read. If there is no description specified, null will be returned
248
public String getDescription() {
253
// the VRML specific methods start here
256
* Scans the subgraph, clearing the pickable and collidable flags on
257
* the Shape3Ds in the subgraph to allow compilation. The pickable
258
* flag will be set to false if the Shape3D does not have an ancestor
259
* which sets the ALLOW_PICK_REPORTING bit. The collidable flag will
260
* always be set to false.
262
public void cleanForCompile(javax.media.j3d.Node root) {
263
com.sun.j3d.loaders.vrml97.impl.TreeCleaner.cleanSubgraph(root);
267
* Returns the base level VRML nodes
269
public BaseNode[] getObjects() {
270
BaseNode nodes[] = new BaseNode[objects.length];
271
for (int i = 0; i < objects.length; i++) {
272
nodes[i] = objects[i];
278
* Returns the Viewpoint nodes in the scene
280
public Viewpoint[] getViewpoints() {
281
Viewpoint[] vps = new Viewpoint[viewpoints.length];
282
for (int i = 0; i < viewpoints.length; i++) {
283
vps[i] = viewpoints[i];
289
* Returns the a Hashtable which associated DEF names with Nodes
291
public Hashtable getDefineTable() {
292
Hashtable<Object, Object> userDefTable = new Hashtable<Object, Object>();
293
for (Enumeration elems = defTable.keys();
294
elems.hasMoreElements();){
295
Object key = elems.nextElement();
296
Object value = defTable.get(key);
297
userDefTable.put(key, value);
303
* Returns the approximate number of triangles in the Scene. For Switch
304
* and LOD nodes, only the triangles on the first child of the node are
307
public int getNumTris() {
311
public void writeX3D(java.io.Writer w,String systemId) throws java.io.IOException {
312
// will need to surround with <Scene> and </Scene>
313
w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
314
w.write("<!DOCTYPE X3D SYSTEM \""+systemId+"\">\n");
315
w.write("<X3D>\n <Scene>\n");
316
// �����ȥ����Ȥ��Ƥ���ΤϤ����� On �ˤ�����ɤ�ʬ����ʤ����顼����Ϥ��뤿��
317
// for(int i = 0; i< objects.length; i++)
318
// objects[i].writeX3D(w,this);
321
Enumeration e = base.routes.elements();
322
while(e.hasMoreElements()) {
323
Route r = (Route)(e.nextElement());
329
w.write(" </Scene>\n</X3D>\n");