1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.gui.animation3D;
11
12 import javax.media.j3d.BoundingSphere;
13 import javax.media.j3d.BranchGroup;
14 import javax.media.j3d.Canvas3D;
15 import javax.media.j3d.PhysicalBody;
16 import javax.media.j3d.PhysicalEnvironment;
17 import javax.media.j3d.Transform3D;
18 import javax.media.j3d.TransformGroup;
19 import javax.media.j3d.View;
20 import javax.media.j3d.ViewPlatform;
21 import javax.vecmath.Point3d;
22 import javax.vecmath.Vector3f;
23
24 import nl.tudelft.simulation.dsol.gui.animation3D.mouse.RotateXYMouseBehavior;
25 import nl.tudelft.simulation.dsol.gui.animation3D.mouse.TranslateMouseBehavior;
26 import nl.tudelft.simulation.dsol.gui.animation3D.mouse.ZoomMouseBehavior;
27
28 import com.sun.j3d.utils.behaviors.mouse.MouseBehavior;
29
30 /***
31 * ViewBranch, the view platform of the scene graph <br>
32 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
33 * University of Technology </a>, the Netherlands. <br>
34 * See for project information <a
35 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
36 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
37 * General Public License (LGPL) </a>, no warranty.
38 *
39 * @version $Revision$ $Date$
40 * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
41 * </a>
42 */
43 public class ViewBranch extends BranchGroup
44 {
45 /*** Rotate around X-axis */
46 protected Transform3D rotateX = new Transform3D();
47
48 /*** Rotate around Y-axis */
49 protected Transform3D rotateY = new Transform3D();
50
51 /*** Zoom */
52 protected Transform3D zoom = new Transform3D();
53
54 /*** Translation */
55 protected Transform3D translation = new Transform3D();
56
57 /*** Rotation around the y-axis of view platform */
58 protected TransformGroup rotateYGroup;
59
60 /*** Rotation around the x-axis of view platform */
61 protected TransformGroup rotateXGroup;
62
63 /*** Zoom of view platform */
64 protected TransformGroup zoomGroup;
65
66 /*** Translation of view platform */
67 protected TransformGroup translateGroup;
68
69 /***
70 * Constructs the view branch
71 *
72 * @param canvas3D A canvas3D
73 */
74 public ViewBranch(final Canvas3D canvas3D)
75 {
76 super();
77 createBranch(canvas3D);
78 }
79
80 /***
81 * Create the view branch
82 *
83 * @param canvas3D A canvas3D
84 */
85 protected void createBranch(final Canvas3D canvas3D)
86 {
87
88 this.rotateX.set(new Vector3f(0.0f, 0.0f, 0.0f));
89 this.rotateY.set(new Vector3f(0.0f, 0.0f, 0.0f));
90 this.zoom.set(new Vector3f(0.0f, 0.0f, 10.0f));
91 this.translation.set(new Vector3f(0.0f, 1.0f, 0.0f));
92
93
94 this.rotateYGroup = new TransformGroup(this.rotateY);
95 this.rotateXGroup = new TransformGroup(this.rotateX);
96 this.zoomGroup = new TransformGroup(this.zoom);
97 this.translateGroup = new TransformGroup(this.translation);
98
99
100 this.rotateYGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
101 this.rotateYGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
102 this.rotateXGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
103 this.rotateXGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
104 this.zoomGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
105 this.zoomGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
106 this.translateGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
107 this.translateGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
108
109
110 ViewPlatform viewPlatform = new ViewPlatform();
111
112
113 RotateXYMouseBehavior mouseRotate = new RotateXYMouseBehavior(
114 MouseBehavior.INVERT_INPUT);
115 mouseRotate.setTransformGroup(this.rotateYGroup);
116 mouseRotate.setTransformGroupX(this.rotateXGroup);
117 mouseRotate.setFactor(0.1);
118 this.rotateYGroup.addChild(mouseRotate);
119 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
120 1000.0);
121 mouseRotate.setSchedulingBounds(bounds);
122
123 ZoomMouseBehavior mouseZoom = new ZoomMouseBehavior(canvas3D,
124 MouseBehavior.INVERT_INPUT);
125
126 mouseZoom.setTransformGroup(this.translateGroup);
127 mouseZoom.setSchedulingBounds(bounds);
128 mouseZoom.setRotateXGroup(this.rotateXGroup);
129 mouseZoom.setRotateYGroup(this.rotateYGroup);
130 this.zoomGroup.addChild(mouseZoom);
131
132 TranslateMouseBehavior mouseTranslate = new TranslateMouseBehavior(
133 canvas3D, MouseBehavior.INVERT_INPUT);
134 mouseTranslate.setTransformGroup(this.translateGroup);
135 mouseTranslate.setSchedulingBounds(bounds);
136 mouseTranslate.setRotateXGroup(this.rotateXGroup);
137 mouseTranslate.setRotateYGroup(this.rotateYGroup);
138 this.translateGroup.addChild(mouseTranslate);
139
140
141 PhysicalBody body = new PhysicalBody();
142 PhysicalEnvironment environment = new PhysicalEnvironment();
143
144
145
146
147
148
149
150
151
152
153 this.translateGroup.addChild(this.rotateYGroup);
154 this.rotateYGroup.addChild(this.rotateXGroup);
155 this.rotateXGroup.addChild(this.zoomGroup);
156 this.zoomGroup.addChild(viewPlatform);
157 this.addChild(this.translateGroup);
158
159 View view = new View();
160
161
162 view.setBackClipDistance(5000);
163 view.addCanvas3D(canvas3D);
164 view.attachViewPlatform(viewPlatform);
165 view.setPhysicalBody(body);
166 view.setPhysicalEnvironment(environment);
167 }
168
169 /***
170 * Reset the view to the original setting
171 */
172 public void resetView()
173 {
174 this.rotateYGroup.setTransform(this.rotateY);
175 this.rotateXGroup.setTransform(this.rotateX);
176 this.zoomGroup.setTransform(this.zoom);
177 this.translateGroup.setTransform(this.translation);
178 }
179
180 /***
181 * @return RotateX
182 */
183 public Transform3D getRotateX()
184 {
185 return this.rotateX;
186 }
187
188 /***
189 * @return RotateY
190 */
191 public Transform3D getRotateY()
192 {
193 return this.rotateY;
194 }
195
196 /***
197 * @return Translation
198 */
199 public Transform3D getTranslation()
200 {
201 return this.translation;
202 }
203
204 /***
205 * @return Zoom
206 */
207 public Transform3D getZoom()
208 {
209 return this.zoom;
210 }
211
212 /***
213 * @param rotateX Rotation around X-axis
214 */
215 public void setRotateX(final Transform3D rotateX)
216 {
217 this.rotateX = rotateX;
218 this.rotateXGroup.setTransform(rotateX);
219 }
220
221 /***
222 * @param rotateY Rotation around Y-axis
223 */
224 public void setRotateY(final Transform3D rotateY)
225 {
226 this.rotateY = rotateY;
227 this.rotateYGroup.setTransform(rotateY);
228 }
229
230 /***
231 * @param translation Translation
232 */
233 public void setTranslation(final Transform3D translation)
234 {
235 this.translation = translation;
236 this.translateGroup.setTransform(translation);
237 }
238
239 /***
240 * @param zoom Zoom factor
241 */
242 public void setZoom(final Transform3D zoom)
243 {
244 this.zoom = zoom;
245 this.zoomGroup.setTransform(zoom);
246 }
247
248 }