View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class ViewingPlatform
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:06 $
11   * 
12   * $Id: ViewingPlatform.java,v 1.3 2004/03/03 11:53:06 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.universe;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import java.io.*;
27  import java.util.*;
28  import javax.media.j3d.*;
29  import javax.vecmath.*;
30  
31  import net.jbeans.util.debug.*;
32  
33  /*====================================================================
34                 Implementation of class ViewingPlatform                
35  ====================================================================*/
36  /***
37   * generally describe ViewingPlatform in here
38   * 
39   * @version $Revision: 1.3 $
40   * @author Masahiro Takatsuka (masa@jbeans.net)
41   * @see com.sun.j3d.utils.universe.ViewingPlatform
42   */
43  
44  /*
45   * this class needs to be public. However, the constructors are package.
46   */
47  public final class ViewingPlatform extends com.sun.j3d.utils.universe.ViewingPlatform {
48  	private static final boolean DEBUG = Debug.getDebugFlag(ViewingPlatform.class);
49  
50  	private com.sun.j3d.utils.universe.Viewer viewer;
51  
52  	/***
53  	 * the original eye location.
54  	 */
55      private Vector4d orgEye;
56  
57  	/***
58  	 * the original up direction.
59  	 */
60  	private Vector3d orgUp;
61  
62  	/***
63  	 * the original viewing direction.
64  	 */
65  	private Vector3d orgViewDir;
66  
67  	/***
68  	 * the original object center.
69  	 */
70      private Vector4d orgObjCenter;
71  
72  	/*
73  	  working object.
74  	 */
75  	transient private Point3d eye;
76  	transient private Vector3d up;
77  	transient private Vector3d viewDir;
78  	transient private Point3d objCenter;
79  	transient private Vector3d tmpVec3d;
80  	transient private Vector4d tmpVec4d;
81  	transient private Transform3D t3d;
82  
83  	/***
84  	 * constructs a new ViewingPlatform object.
85  	 */
86      ViewingPlatform() {
87  		this(1);
88      }
89  
90  	/***
91  	 * constructs a new ViewingPlatform object.
92  	 */
93      ViewingPlatform(int i) {
94  		// notice I'm not doing "super(i)".
95          platformGeometry = null;
96          viewerList = new Hashtable();
97          setCapability(Group.ALLOW_CHILDREN_WRITE);
98          setCapability(Group.ALLOW_CHILDREN_EXTEND);
99          if (i < 1) {
100             i = 1;
101 		}
102         mtg = new MultiTransformGroup(i);
103         TransformGroup transformgroup = mtg.getTransformGroup(0);
104         addChild(transformgroup);
105         transformgroup = mtg.getTransformGroup(i - 1);
106         viewPlatform = new ViewPlatform();
107         viewPlatform.setCapability(Group.ALLOW_CHILDREN_READ);
108         viewPlatform.setCapability(Group.ALLOW_CHILDREN_WRITE);
109         transformgroup.addChild(viewPlatform);
110         transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
111         transformgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
112         avatarRoot = new BranchGroup();
113         avatarRoot.setCapability(Group.ALLOW_CHILDREN_READ);
114         avatarRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
115         avatarRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
116         transformgroup.addChild(avatarRoot);
117         platformGeometryRoot = new BranchGroup();
118         platformGeometryRoot.setCapability(Group.ALLOW_CHILDREN_READ);
119         platformGeometryRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
120         platformGeometryRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
121         transformgroup.addChild(platformGeometryRoot);
122 
123 		initialize();
124     }
125 
126 	/*
127 	 * special initialization.
128 	 */
129 	private final void initialize() {
130 		this.orgEye = new Vector4d(0.0, 0.0, 0.0, 1.0);
131 		this.orgUp = new Vector3d(0.0, 1.0, 0.0);
132 		this.orgViewDir = new Vector3d(0.0, 0.0, -1.0);
133 		this.orgObjCenter = new Vector4d(0.0, 0.0, 0.0, 1.0);
134 		this.eye = new Point3d();
135 		this.up = new Vector3d();
136 		this.viewDir = new Vector3d();
137 		this.objCenter = new Point3d();
138 		this.tmpVec3d = new Vector3d();
139 		this.t3d = new Transform3D();
140   		this.tmpVec4d = new Vector4d();
141 	}
142 
143 	/***
144 	 * Returns the location of the eye.
145 	 */
146 	public final Point3d getEyePosition() {
147 		TransformGroup tg = getViewPlatformTransform();
148 		tg.getTransform(this.t3d);
149 		this.t3d.transform(this.orgEye, this.tmpVec4d);
150 		this.eye.x = this.tmpVec4d.x;
151 		this.eye.y = this.tmpVec4d.y;
152 		this.eye.z = this.tmpVec4d.z;
153 
154 		return this.eye;
155 	}
156 
157 	/***
158 	 * Returns the up direction.
159 	 */
160 	public final Vector3d getUpPosition() {
161 		TransformGroup tg = getViewPlatformTransform();
162 		tg.getTransform(this.t3d);
163 		this.t3d.transform(this.orgUp, this.up);
164 
165 		return this.up;
166 	}
167 
168 	/***
169 	 * Returns the viewing direction.
170 	 */
171 	public final Vector3d getViewingDirection() {
172 		TransformGroup tg = getViewPlatformTransform();
173 		tg.getTransform(this.t3d);
174 		this.t3d.transform(this.orgViewDir, this.viewDir);
175 
176 		return this.viewDir;
177 	}
178 
179 	/***
180 	 * Sets a nominal viewing transformation.
181 	 */
182     public final void setNominalViewingTransform() {
183 		super.setNominalViewingTransform();
184 		// then save the current transform.
185         TransformGroup tg = getViewPlatformTransform();
186 		if (tg instanceof net.jbeans.j3d.transform.ResettableTransformGroup) {
187 			((net.jbeans.j3d.transform.ResettableTransformGroup) tg).saveTransform();
188 		}
189 	}
190 
191 	/***
192 	 *
193 	 */
194 	public final void removeViewer(com.sun.j3d.utils.universe.Viewer viewer) {
195 		this.viewerList.remove(viewer);
196 	}
197 
198 	/***
199 	 * Sets a new Viewer object.
200 	 */
201 	public final void setViewer(com.sun.j3d.utils.universe.Viewer viewer) {
202 		this.viewer = viewer;
203 	}
204 
205 	/***
206 	 * Moves a viewer to see all objects in the scene described by
207 	 * the specified Node object.
208 	 */
209 	public final void viewAll(Node node) {
210 		Bounds bounds = node.getBounds();
211 		BoundingSphere sbox = (bounds instanceof BoundingSphere) ?
212 			(BoundingSphere) bounds :
213 			new BoundingSphere(bounds);
214 
215 		// compute the center of object
216 		sbox.getCenter(this.objCenter);
217 		// If node is TransformGroup, apply current transformation to the center.
218 		if (node instanceof TransformGroup) {
219 			this.orgObjCenter.x = this.objCenter.x;
220 			this.orgObjCenter.y = this.objCenter.y;
221 			this.orgObjCenter.z = this.objCenter.z;
222 			((TransformGroup)node).getTransform(this.t3d);
223 			this.t3d.transform(this.orgObjCenter, this.tmpVec4d);
224 			this.objCenter.x = this.tmpVec4d.x;
225 			this.objCenter.y = this.tmpVec4d.y;
226 			this.objCenter.z = this.tmpVec4d.z;
227 		}
228 
229 		// find the ideal distance from the object.
230 		double fov_2 = this.viewer.getView().getFieldOfView() / 2.0;
231 		double r = sbox.getRadius();
232 		double d_ideal = r / Math.tan(fov_2);
233 
234 		// find the ideal eye position. (= objCenter + -d_ideal * norm(viewDir))
235 		getViewingDirection();
236 		this.tmpVec3d.set(this.viewDir);
237 		this.tmpVec3d.normalize();
238 		this.tmpVec3d.scale(- d_ideal);
239 		this.tmpVec3d.add(this.objCenter);
240 		this.t3d.set(this.tmpVec3d);
241 		if (DEBUG) {
242 			System.out.println("ViewingPlatform.viewAll() : debuging information...");
243 			System.out.println("\t center = " + this.objCenter);
244 			System.out.println("\t eye = " + this.eye);
245 			System.out.println("\t dir = " + this.viewDir);
246 			System.out.println("\t translate = " + this.tmpVec3d);
247 			System.out.println("\t d_ideal = " + d_ideal);
248 		}
249 
250 		TransformGroup tg = getViewPlatformTransform();
251   		tg.setTransform(this.t3d);
252 
253 		// set backClipDistance to (d_ideal + r) * 1.25
254 		this.viewer.getView().setBackClipDistance((d_ideal + r) * 1.25);
255 		this.viewer.getView().setFrontClipDistance(0.001);
256 	}
257 }