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

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