comparison tools/java3d-test/src/graff3d.java @ 13:0652ac3c8b5f

add two_obj
author axmo
date Mon, 11 Aug 2008 22:20:03 +0900
parents
children
comparison
equal deleted inserted replaced
12:34821c03b206 13:0652ac3c8b5f
1 // Draw the graph of a function
2 // version 1.0 (c) K.Sasano
3
4 import java.applet.Applet;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.awt.BorderLayout;
8 import java.awt.Frame;
9 import java.awt.Font;
10 import java.awt.GraphicsConfiguration;
11 import java.util.Enumeration;
12 import javax.media.j3d.*;
13 import javax.vecmath.*;
14 import com.sun.j3d.utils.universe.*;
15 import com.sun.j3d.utils.applet.MainFrame;
16 import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
17 import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
18 import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
19 import com.sun.j3d.utils.geometry.Text2D;
20
21 public class graff3d extends Applet {
22
23 // global variables
24 float xwidth;
25 float x0;
26 float ywidth;
27 float y0;
28 float xdelta;
29 float ydelta;
30 float z0;
31 float zwidth;
32 float zvalue[][];
33 float zcenter[][];
34 Shape3D surfaceGraph;
35 Shape3D meshGraph;
36 Shape3D xyPlane;
37 Shape3D xyzAxes;
38 Shape3D xyzArrowHead;
39 TransformGroup xyzAxesLabels;
40
41 //key behavior
42 public class KeyBehavior extends Behavior{
43
44 private BranchGroup bg;
45 private WakeupOnAWTEvent wue;
46
47 KeyBehavior(BranchGroup bg){
48 this.bg=bg;
49 }
50
51 public void initialize(){
52 this.wakeupOn(wue = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
53 }
54
55 public void processStimulus(Enumeration criteria){
56 AWTEvent ae = (wue.getAWTEvent())[0];
57 char c=((KeyEvent)ae).getKeyChar();
58 TransparencyAttributes
59 surftr = (surfaceGraph.getAppearance()).getTransparencyAttributes();
60 TransparencyAttributes
61 meshtr = (meshGraph.getAppearance()).getTransparencyAttributes();
62 TransparencyAttributes
63 xytr = (xyPlane.getAppearance()).getTransparencyAttributes();
64
65 if(c=='s'){ //Show/Erase surfacegraph
66 addSurface=!addSurface;
67 if(addSurface){
68 surftr = new TransparencyAttributes(TransparencyAttributes.NONE,0.0f);
69 }else{
70 surftr = new TransparencyAttributes(TransparencyAttributes.NICEST,1.0f);
71 }
72 }
73 if(c=='m'){ //Show/Erase meshgraph
74 float tr;
75 addMesh=!addMesh;
76 if(addMesh){ tr= 0.0f; } else { tr=1.0f; }
77 meshtr = new TransparencyAttributes(TransparencyAttributes.NICEST,tr);
78 }
79 if(c=='z'){ //Show/Erase xyplane
80 float tr;
81 addXYPlane=!addXYPlane;
82 if(addXYPlane){ tr= 0.6f; } else { tr=1.0f; }
83 xytr = new TransparencyAttributes(TransparencyAttributes.NICEST,tr);
84 }
85 (xyPlane.getAppearance()).setTransparencyAttributes(xytr);
86 (meshGraph.getAppearance()).setTransparencyAttributes(meshtr);
87 (surfaceGraph.getAppearance()).setTransparencyAttributes(surftr);
88
89 this.wakeupOn(wue = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
90 }
91 }
92
93 //create scene
94 public BranchGroup createSceneGraph() {
95 BranchGroup objRoot = new BranchGroup();
96 BoundingSphere bounds = new BoundingSphere();
97 bounds.setRadius( 1000.0 );
98
99 //prepare for movement
100 TransformGroup objTrans = new TransformGroup();
101 objRoot.addChild(objTrans);
102
103 objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
104 objTrans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
105
106 MouseRotate rotat = new MouseRotate( objTrans );
107 MouseTranslate trans = new MouseTranslate( objTrans );
108 MouseZoom zoom = new MouseZoom( objTrans );
109
110 rotat.setSchedulingBounds( bounds );
111 trans.setSchedulingBounds( bounds );
112 zoom.setSchedulingBounds( bounds );
113
114 objTrans.addChild(rotat);
115 objTrans.addChild(trans);
116 objTrans.addChild(zoom);
117
118 //add xy-plane
119 objTrans.addChild(xyPlane);
120
121 //add graphs
122 objTrans.addChild(surfaceGraph);
123 objTrans.addChild(meshGraph);
124
125 //add axes
126 objTrans.addChild(xyzAxes); //axis
127 objTrans.addChild(xyzArrowHead); //arrowhead
128 objTrans.addChild(xyzAxesLabels); //label
129
130 //add key behavior
131 KeyBehavior myKeyBehav=new KeyBehavior(objRoot);
132 myKeyBehav.setSchedulingBounds( bounds );
133 objRoot.addChild(myKeyBehav);
134
135 //compile the object
136 objRoot.compile();
137
138 return objRoot;
139 }
140
141 //surface graph
142 private Shape3D graph(){
143 int totalN=6*n*n;
144
145 Point3f coord[] = new Point3f[totalN];
146 Color3f color[] = new Color3f[totalN];
147
148 for(int i=0; i<n; i++){
149 float xi=xmin+i*xdelta;
150 float xii=xi+xdelta;
151 float xcenter=xi+0.5f*xdelta;
152 for(int j=0; j<n; j++){
153 int tmp=6*(i*n+j);
154 float yj=ymin+j*ydelta;
155 float yjj=yj+ydelta;
156 float ycenter=yj+0.5f*ydelta;
157
158 coord[tmp]=new Point3f(xn(xcenter),yn(ycenter),zn(zcenter[i][j]));
159 coord[tmp+1]=new Point3f(xn(xi),yn(yj),zn(zvalue[i][j]));
160 coord[tmp+2]=new Point3f(xn(xii),yn(yj),zn(zvalue[i+1][j]));
161 coord[tmp+3]=new Point3f(xn(xii),yn(yjj),zn(zvalue[i+1][j+1]));
162 coord[tmp+4]=new Point3f(xn(xi),yn(yjj),zn(zvalue[i][j+1]));
163 coord[tmp+5]=coord[tmp+1];
164
165 color[tmp]=hcolor(zn(zcenter[i][j]));
166 color[tmp+1]=hcolor(zn(zvalue[i][j]));
167 color[tmp+2]=hcolor(zn(zvalue[i+1][j]));
168 color[tmp+3]=hcolor(zn(zvalue[i+1][j+1]));
169 color[tmp+4]=hcolor(zn(zvalue[i][j+1]));
170 color[tmp+5]=color[tmp+1];
171 }
172 }
173
174 int stripCounts[] = new int[n*n];
175 for(int i=0; i<n*n; i++){stripCounts[i]=6;}
176
177 TriangleFanArray tfa =
178 new TriangleFanArray(
179 totalN,
180 TriangleFanArray.COORDINATES|TriangleFanArray.COLOR_3,
181 stripCounts
182 );
183 tfa.setCoordinates(0,coord);
184 tfa.setColors(0,color);
185
186 //construct appearence
187 TransparencyAttributes surftr;
188 if(addSurface){
189 surftr = new TransparencyAttributes(TransparencyAttributes.NONE,0.0f);
190 }else{
191 surftr = new TransparencyAttributes(TransparencyAttributes.NICEST,1.0f);
192 }
193 PolygonAttributes attr = new PolygonAttributes();
194 attr.setCullFace(PolygonAttributes.CULL_NONE);
195 attr.setPolygonMode(PolygonAttributes.POLYGON_FILL);
196
197 Appearance appear = new Appearance();
198 appear.setPolygonAttributes(attr);
199 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
200 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
201 appear.setTransparencyAttributes(surftr);
202
203 //construct Shape3D for surface graph
204 Shape3D graphshape=new Shape3D();
205 graphshape.removeGeometry(0);
206 graphshape.addGeometry(tfa);
207 graphshape.setAppearance(appear);
208 graphshape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
209 graphshape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
210
211 return graphshape;
212 }
213
214 //color data corresponding to the height
215 private Color3f hcolor(float h){
216 float r,g,b;
217 h=h*2;
218 if(h>1){ r=1.0f; g=0.5f; b=0.0f; }
219 else if(h>0){ r=h*1.0f; g=0.75f-h*0.25f; b=0.0f; }
220 else if(h>-1){ r=0.0f; g=(1+h)*0.75f; b= -h*0.75f; }
221 else{ r=0.0f; g=0.0f; b=0.75f;}
222 return new Color3f(r,g,b);
223 }
224
225 //mesh graph
226 private Shape3D meshgraph(){
227 //construct coordinat data
228 int totalN=2*(nmesh+1)*(nmesh+1);
229
230 Point3f coord[] = new Point3f[totalN];
231 Color3f color[] = new Color3f[totalN];
232 Color3f lcolor = new Color3f(1.0f, 1.0f, 1.0f);
233
234 for(int i=0; i<=nmesh; i++){
235 float xi=xmin+i*(xmax-xmin)/nmesh;
236
237 for(int j=0; j<=nmesh; j++){
238 int tmp=i*(nmesh+1)+j;
239 int tmp1=j*(nmesh+1)+i+(nmesh+1)*(nmesh+1);
240 float yj=ymin+j*(ymax-ymin)/nmesh;
241
242 coord[tmp]=new Point3f(xn(xi),yn(yj),zn(f(xi,yj)));
243 color[tmp]=lcolor;
244
245 coord[tmp1]=coord[tmp];
246 color[tmp1]=color[tmp];
247 }
248 }
249
250 int stripCounts[];
251 stripCounts= new int[2*(nmesh+1)];
252 for(int i=0; i<=2*nmesh+1; i++){stripCounts[i]=nmesh+1;}
253
254 LineStripArray lsa =
255 new LineStripArray(
256 totalN,
257 LineStripArray.COORDINATES|LineStripArray.COLOR_3,
258 stripCounts
259 );
260 lsa.setCoordinates(0,coord);
261 lsa.setColors(0,color);
262
263 //construct appearence
264 LineAttributes attr = new LineAttributes();
265 attr.setLineWidth(meshLineWidth);
266
267 float tr;
268 if(addMesh){ tr= 0.0f; } else { tr=1.0f; }
269 TransparencyAttributes transAttrib;
270 transAttrib = new TransparencyAttributes(TransparencyAttributes.NICEST,tr);
271
272 Appearance appear = new Appearance();
273 appear.setLineAttributes(attr);
274 appear.setTransparencyAttributes(transAttrib);
275 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
276 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
277
278 //construct Shape3D for meshgraph
279 Shape3D graphshape=new Shape3D();
280 graphshape.removeGeometry(0);
281 graphshape.addGeometry(lsa);
282 graphshape.setAppearance(appear);
283 graphshape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
284 graphshape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
285
286 return graphshape;
287 }
288
289 //xy-plane
290 private Shape3D xyplane(){
291 QuadArray xyp= new QuadArray(4,QuadArray.COORDINATES|QuadArray.COLOR_3);
292 xyp.setCoordinate(0,new Point3f(0.5f,0.5f,zn(0.0f)));
293 xyp.setCoordinate(1,new Point3f(-0.5f,0.5f,zn(0.0f)));
294 xyp.setCoordinate(2,new Point3f(-0.5f,-0.5f,zn(0.0f)));
295 xyp.setCoordinate(3,new Point3f(0.5f,-0.5f,zn(0.0f)));
296 for(int i=0; i<4; i++){xyp.setColor(i, new Color3f(1.0f,1.0f,1.0f));}
297
298 PolygonAttributes attrib = new PolygonAttributes();
299 attrib.setCullFace(PolygonAttributes.CULL_NONE);
300 attrib.setPolygonMode(PolygonAttributes.POLYGON_FILL);
301
302 TransparencyAttributes transAttrib;
303 float tr;
304 if(addXYPlane){ tr= 0.6f; } else { tr=1.0f; }
305 transAttrib = new TransparencyAttributes(TransparencyAttributes.NICEST,tr);
306
307 Appearance appear = new Appearance();
308 appear.setPolygonAttributes(attrib);
309 appear.setTransparencyAttributes(transAttrib);
310 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
311 appear.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
312
313 Shape3D xyplaneshape=new Shape3D();
314 xyplaneshape.removeGeometry(0);
315 xyplaneshape.addGeometry(xyp);
316 xyplaneshape.setAppearance(appear);
317 xyplaneshape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
318 xyplaneshape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);
319
320 return xyplaneshape;
321 }
322
323 //axes
324 private Shape3D axes(){
325 LineArray a=new LineArray(6,LineArray.COORDINATES|LineArray.COLOR_3);
326 a.setCoordinate(0,new Point3f(0.6f,yn(0.0f),zn(0.0f)));
327 a.setCoordinate(1,new Point3f(-0.6f,yn(0.0f),zn(0.0f)));
328 a.setCoordinate(2,new Point3f(xn(0.0f),0.6f,zn(0.0f)));
329 a.setCoordinate(3,new Point3f(xn(0.0f),-0.6f,zn(0.0f)));
330 a.setCoordinate(4,new Point3f(xn(0.0f),yn(0.0f),0.6f));
331 a.setCoordinate(5,new Point3f(xn(0.0f),yn(0.0f),-0.6f));
332 for(int i=0; i<6; i++){ a.setColor(i,new Color3f(1.0f,1.0f,0.0f));}
333
334 LineAttributes att=new LineAttributes();
335 att.setLineWidth(2.0f);
336 Appearance appear = new Appearance();
337 appear.setLineAttributes(att);
338
339 Shape3D ax=new Shape3D();
340 ax.removeGeometry(0);
341 ax.addGeometry(a);
342 ax.setAppearance(appear);
343
344 return ax;
345 }
346
347 //arrowhead
348 private Shape3D arrowhead(){
349 int scounts[]= new int[3];
350 for(int i=0; i<3; i++){scounts[i]=10;}
351
352 TriangleFanArray ah=new TriangleFanArray(30,
353 TriangleFanArray.COORDINATES|TriangleFanArray.COLOR_3,
354 scounts);
355 ah.setCoordinate(0,new Point3f(0.6f, yn(0.0f), zn(0.0f)));
356 ah.setCoordinate(10,new Point3f(xn(0.0f),0.6f, zn(0.0f)));
357 ah.setCoordinate(20,new Point3f(xn(0.0f), yn(0.0f), 0.6f));
358 float d=(float)Math.PI/4;
359 for(int i=0 ; i<=8; i++){
360 ah.setCoordinate(i+1,
361 new Point3f(0.55f, yn(0.0f)+0.02f * (float)Math.cos(d*i), zn(0.0f)+0.02f *(float)Math.sin(d*i)));
362 ah.setCoordinate(i+11,
363 new Point3f(xn(0.0f)+0.02f * (float)Math.cos(d*i), 0.55f, zn(0.0f)+0.02f *(float)Math.sin(d*i)));
364 ah.setCoordinate(i+21,
365 new Point3f(xn(0.0f)+0.02f * (float)Math.cos(d*i), yn(0.0f)+0.02f *(float)Math.sin(d*i), 0.55f));
366 }
367 Color3f color[]=new Color3f[30];
368 for(int i=0; i<30; i++){ color[i]=new Color3f(1.0f, 1.0f, 0.0f);}
369 ah.setColors(0,color);
370
371 return new Shape3D(ah);
372 }
373
374 //axes labels
375 TransformGroup axeslabels(){
376 TransformGroup tmp = new TransformGroup();
377
378 PolygonAttributes polyAttrib = new PolygonAttributes();
379 polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
380 polyAttrib.setBackFaceNormalFlip(true);
381
382 Transform3D xmov= (new Transform3D());
383 xmov.set(new Vector3f(0.63f,yn(0.0f)-0.05f,zn(0.0f)));
384 Text2D xlbl=new Text2D("x",new Color3f(1.0f,1.0f,0.0f),"Helvetica",24,Font.BOLD);
385 (xlbl.getAppearance()).setPolygonAttributes(polyAttrib);
386 TransformGroup xmovg=new TransformGroup(xmov);
387 xmovg.addChild(xlbl);
388
389 Transform3D ymov= new Transform3D();
390 Transform3D tmpymov=new Transform3D();
391 tmpymov.rotZ(Math.PI/2.0d);
392 ymov.set(new Vector3f(xn(0.0f)+0.05f,0.63f,zn(0.0f)));
393 ymov.mul(tmpymov);
394 Text2D ylbl=new Text2D("y",new Color3f(1.0f,1.0f,0.0f),"Helvetica",24,Font.BOLD);
395 (ylbl.getAppearance()).setPolygonAttributes(polyAttrib);
396 TransformGroup ymovg=new TransformGroup(ymov);
397 ymovg.addChild(ylbl);
398
399 Transform3D zmov= new Transform3D();
400 Transform3D tmpzmov=new Transform3D();
401 tmpzmov.rotX(Math.PI/2.0d);
402 zmov.set(new Vector3f(xn(0.0f)-0.02f,yn(0.0f),0.63f));
403 zmov.mul(tmpzmov);
404 Text2D zlbl=new Text2D("z",new Color3f(1.0f,1.0f,0.0f),"Helvetica",24,Font.BOLD);
405 (zlbl.getAppearance()).setPolygonAttributes(polyAttrib);
406 TransformGroup zmovg=new TransformGroup(zmov);
407 zmovg.addChild(zlbl);
408
409 tmp.addChild(xmovg);
410 tmp.addChild(ymovg);
411 tmp.addChild(zmovg);
412
413 return tmp;
414 }
415
416 //get normalized coordinate
417 float xn(float x){ return (x-x0)/xwidth;}
418 float yn(float y){ return (y-y0)/ywidth;}
419 float zn(float z){ return (z-z0)/zwidth;}
420
421 //////////// main class ///////////////
422 public graph3d() {
423 // preparation for numeric data
424 xwidth=xmax-xmin;
425 x0=(xmax+xmin)/2;
426 ywidth=ymax-ymin;
427 y0=(ymax+ymin)/2;
428 xdelta=xwidth/n;
429 ydelta=ywidth/n;
430
431 zvalue = new float[n+1][n+1];
432 zcenter = new float[n][n];
433
434 int i; int j;
435
436 for(i=0; i<=n; i++){
437 for(j=0; j<=n; j++){
438 zvalue[i][j]=f(xmin+i*xdelta, ymin+j*ydelta);
439 }
440 }
441 for(i=0; i<n; i++){
442 for(j=0; j<n; j++){
443 zcenter[i][j]=f(xmin+(i+0.5f)*xdelta, ymin+(j+0.5f)*ydelta);
444 }
445 }
446
447 // if necessary, determine zmin and zmax automatically
448 if(zmin==0.0f && zmax==0.0f){
449 zmin=Float.POSITIVE_INFINITY; zmax=Float.NEGATIVE_INFINITY;
450 for(i=0; i<=n; i++){
451 for(j=0; j<=n; j++){
452 zmin=Math.min(zmin,zvalue[i][j]);
453 zmax=Math.max(zmax,zvalue[i][j]);
454 }
455 }
456 for(i=0; i<n; i++){
457 for(j=0; j<n; j++){
458 zmin=Math.min(zmin,zcenter[i][j]);
459 zmax=Math.max(zmax,zcenter[i][j]);
460 }
461 }
462 }
463
464 zwidth=zmax-zmin;
465 z0=(zmax+zmin)/2;
466
467 /////////// main part ///////////
468 setLayout(new BorderLayout());
469
470 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
471
472 Canvas3D canvas = new Canvas3D(config);
473 add("Center", canvas);
474
475 SimpleUniverse universe = new SimpleUniverse(canvas);
476 universe.getViewingPlatform().setNominalViewingTransform();
477
478 //construct graphic objects
479 surfaceGraph = graph();
480 meshGraph = meshgraph();
481 xyPlane = xyplane();
482 xyzAxes = axes();
483 xyzArrowHead = arrowhead();
484 xyzAxesLabels = axeslabels();
485
486 BranchGroup scene = createSceneGraph();
487 universe.addBranchGraph(scene);
488 }
489
490 //main method to make both of Application and Applet
491 public static void main(String[] args) {
492 Frame frame = new MainFrame(new graff3d(), 480, 480);
493 }
494
495 /////////////////////////////////////////////////
496 // define our function and give necessary data //
497 /////////////////////////////////////////////////
498
499 // our function
500 float f(float x, float y){
501 return x*x-y*y;
502 //return x*x*x + y*y*y - 3*x*y;
503 //return (y-x*x)*(y-2*x*x);
504 //return (float)Math.sin(x*x+y*y);
505 //if(x==0.0f){ return 0.0f; } else { return y*y/x; }
506 //if((x==0.0f) && (y==0.0f)){ return 0.5f; } else {return x*y/(x*x+y*y);}
507 }
508
509 // domain of definition
510 float xmin=-1.0f; // min of x
511 float xmax=1.0f; // max of x
512 float ymin=-1.0f; // min of y
513 float ymax=1.0f; // max of y
514
515 // range of the value
516 // If both of zmin and zmax are set to 0.0f, then zmin and zmax
517 // are calculated automatically.
518 float zmin=0.0f; // min of z
519 float zmax=0.0f; // max of z
520
521 int n=20; // number of divisions for surface graph
522 int nmesh=20; // number of divisions for mesh graph
523
524 float meshLineWidth=2.0f; // width of lines in mesh graph
525
526 boolean addSurface=true; // FLAG : show/erase surface graph
527 boolean addMesh=true; // FLAG : show/erase mesh graph
528 boolean addXYPlane=false; // FLAG : show/erase xy-plane
529 }