~hrg/hrg-packaging/openhrp3

« back to all changes in this revision

Viewing changes to server/ModelLoader.old/ModelLoader/VrmlSceneEx.java

  • Committer: nakaoka
  • Date: 2008-07-06 15:26:51 UTC
  • Revision ID: git-v1:fbb3ce756bbcedb021fed5d474b58ef092ea8b42
コンパイルに cmake を使用するようにし、MakefileをCMakeLists.txt で置き換えた。

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package jp.go.aist.hrp.simulator;
2
 
 
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.*;
10
 
import vrml.node.*;
11
 
import vrml.*;
12
 
import java.util.StringTokenizer;
13
 
 
14
 
// ���
15
 
// ���Υ��饹�� VrmlScene �򤽤Τޤޥ��ԡ��������
16
 
// ����� getField �� getNodeTypeString ���ɲä�����ΤǤ���
17
 
// ��äư����Υ����ȤϤ�Ȥ� VrmlScene �Τ�ΤǤ���
18
 
// �Ѹ�ΤޤޤǤ���
19
 
 
20
 
public class VrmlSceneEx implements com.sun.j3d.loaders.Scene {
21
 
    com.sun.j3d.loaders.vrml97.impl.Scene base;
22
 
    String              description;
23
 
    int                 numTris;
24
 
    BaseNode[]          objects;
25
 
    Viewpoint[]         viewpoints;
26
 
    Node[]              navInfos;
27
 
    Background[]        backgrounds;
28
 
    Fog[]               fogs;
29
 
    Light[]             lights;
30
 
    Hashtable<Object, Object>           defTable;
31
 
    BranchGroup         scene = null;
32
 
    // proto �����Ƥ���¸���Ƥ���
33
 
    
34
 
 
35
 
    // ���β�����Ĥ���ʬ���ɲä����᥽�åɤǤ��롣
36
 
    // ���Υ᥽�åɤ� def ��̾���� field ��̾������ꤷ�� Field ���֤����Ȥ������ΤޤޤΥ᥽�åɤǤ���
37
 
    // �֤��ʤ����ϤȤꤢ���� null ���֤���
38
 
    public com.sun.j3d.loaders.vrml97.impl.Field getField(String defName,String fieldName)
39
 
    {
40
 
        // �ޤ� getNamedObjects �� def ̾�ΰ������������¸���Ƥ���
41
 
        
42
 
        try{
43
 
        
44
 
            com.sun.j3d.loaders.vrml97.impl.BaseNode node = base.use(defName);
45
 
            return node.getField(fieldName);
46
 
        }catch(Exception e)
47
 
        {
48
 
            return null;
49
 
        }
50
 
    }
51
 
 
52
 
    // def ��̾������ꤷ�Ƥ���̾���򥹥ȥ�󥰤��֤��Ȥ������ΤޤޤΥ᥽�åɤǤ���
53
 
    public String getNodeTypeString(String defName)
54
 
    {
55
 
        try{
56
 
        
57
 
            com.sun.j3d.loaders.vrml97.impl.BaseNode node = base.use(defName);
58
 
            return node.wrap().getType();
59
 
        }catch(Exception e)
60
 
        {
61
 
            return null;
62
 
        }
63
 
    }
64
 
    
65
 
    VrmlSceneEx(com.sun.j3d.loaders.vrml97.impl.Scene base) {
66
 
        this.base = base;
67
 
        description = base.description;
68
 
        numTris = base.numTris;
69
 
        
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();
76
 
        }
77
 
 
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();
84
 
        }
85
 
 
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();
92
 
        }
93
 
 
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();
100
 
        }
101
 
 
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();
108
 
        }
109
 
 
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();
116
 
        }
117
 
 
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);
124
 
        }
125
 
    }
126
 
 
127
 
    /**
128
 
     * Returns the root nodes of the VRML scene, gathered into a BranchGroup
129
 
     */
130
 
    public BranchGroup getSceneGroup() {
131
 
        if (scene == null) {
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);
137
 
                }
138
 
            }
139
 
        }
140
 
        return scene;
141
 
    }
142
 
 
143
 
    /**
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 
147
 
     */
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();
152
 
        }
153
 
        return views;
154
 
    }
155
 
 
156
 
    /**
157
 
     * Returns the horizontal field of view values for the Viewpoints in 
158
 
     * the scene.
159
 
     */
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();
164
 
        }
165
 
        return fovs;
166
 
    }
167
 
 
168
 
    /**
169
 
     * Returns Java3D Light nodes in the scene.
170
 
     * The Light nodes returned will be parented within the SceneGroup
171
 
     */
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();
178
 
        }
179
 
        return j3dLights;
180
 
    }
181
 
 
182
 
    /** 
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.
186
 
     */ 
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();
195
 
            if (value != null) {
196
 
                j3dDefTable.put(key, value);
197
 
            }
198
 
        }
199
 
        return j3dDefTable;
200
 
    }
201
 
 
202
 
    /**
203
 
     * Returns the J3D Background nodes in the scene.
204
 
     * The Background nodes returned will be parented within the SceneGroup
205
 
     */
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();
211
 
        }
212
 
        return j3dBackgrounds;
213
 
    }
214
 
 
215
 
    /**
216
 
     * Returns the J3D Fog nodes in the scene.
217
 
     * The Fog nodes returned will be parented within the SceneGroup
218
 
     */
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();
223
 
        }
224
 
        return j3dFogs;
225
 
    }
226
 
 
227
 
    /**
228
 
     * The VRML loader does not support loading behaviors, this method
229
 
     * returns null.
230
 
     */
231
 
    public javax.media.j3d.Behavior[] getBehaviorNodes() {
232
 
        return null;
233
 
    }
234
 
 
235
 
 
236
 
    /**
237
 
     * The VRML loader does not support loading sounds, this method
238
 
     * returns null.
239
 
     */
240
 
    public javax.media.j3d.Sound[] getSoundNodes() {
241
 
        return null;
242
 
    }
243
 
 
244
 
    /**
245
 
     * Returns the description (if any) from the first WorldInfo node
246
 
     * read.  If there is no description specified, null will be returned
247
 
     */
248
 
    public String getDescription() {
249
 
         return description;
250
 
    }
251
 
 
252
 
 
253
 
    // the VRML specific methods start here
254
 
 
255
 
    /**
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.
261
 
     */
262
 
    public void cleanForCompile(javax.media.j3d.Node root) {
263
 
        com.sun.j3d.loaders.vrml97.impl.TreeCleaner.cleanSubgraph(root);
264
 
    }
265
 
 
266
 
    /**
267
 
     * Returns the base level VRML nodes
268
 
     */
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];
273
 
        }
274
 
        return nodes;
275
 
    }
276
 
 
277
 
    /**
278
 
     * Returns the Viewpoint nodes in the scene
279
 
     */
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];
284
 
        }
285
 
        return vps;
286
 
    }
287
 
 
288
 
    /**
289
 
     * Returns the a Hashtable which associated DEF names with Nodes
290
 
     */
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);
298
 
        }
299
 
        return userDefTable;
300
 
    }
301
 
 
302
 
    /**
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
305
 
     * counted.
306
 
     */
307
 
    public int getNumTris() {
308
 
        return numTris;
309
 
    }
310
 
 
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);
319
 
 
320
 
        // routes
321
 
        Enumeration e = base.routes.elements();
322
 
        while(e.hasMoreElements()) {
323
 
            Route r = (Route)(e.nextElement());
324
 
            r.writeX3D(w);
325
 
            w.write("\n");
326
 
        }
327
 
 
328
 
            
329
 
        w.write(" </Scene>\n</X3D>\n");
330
 
    }
331
 
        
332
 
}