001: /*
002: * $RCSfile: ViewUtils.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.1 $
041: * $Date: 2007/08/15 16:25:56 $
042: * $State: Exp $
043: */
044: package org.jdesktop.j3d.utils.view;
045:
046: import javax.media.j3d.TransformGroup;
047: import javax.media.j3d.Transform3D;
048: import javax.media.j3d.Bounds;
049: import javax.media.j3d.BoundingSphere;
050:
051: import javax.vecmath.Point3d;
052: import javax.vecmath.Vector3d;
053: import javax.vecmath.Matrix3d;
054:
055: /**
056: * Utility functions for manipulating the View
057: *
058: * @author Paul Byrne
059: * @version $Id: ViewUtils.java,v 1.1 2007/08/15 16:25:56 paulby Exp $
060: */
061: public class ViewUtils extends Object {
062:
063: public enum Axis {
064: POSITIVE_X_AXIS, POSITIVE_Y_AXIS, POSITIVE_Z_AXIS, NEGATIVE_X_AXIS, NEGATIVE_Y_AXIS, NEGATIVE_Z_AXIS
065: };
066:
067: /**
068: * Set the transform so that everything inside the Bounds is
069: * visible within the view frustum. Return the views distance from
070: * center of the scene after transformation
071: *
072: * @param tg The transform to change
073: * @param sceneBounds The bounds of the scene to fit inside the view frustum
074: * @param fieldOfView The fieldOfView
075: * @param axis The axis along which to view the scene, this must be one of
076: * POSITIVE_X_AXIS, POSITIVE_Y_AXIS, POSITIVE_Z_AXIS, NEGATIVE_X_AXIS, NEGATIVE_Y_AXIS or NEGATIVE_Z_AXIS
077: * @return The distance the eye is from the center of the scene bounds.
078: */
079: public static double setViewpoint(TransformGroup tg,
080: Bounds sceneBounds, double fieldOfView, Axis axis) {
081:
082: if (sceneBounds instanceof BoundingSphere)
083: return setViewpoint(tg, (BoundingSphere) sceneBounds,
084: fieldOfView, axis);
085:
086: return setViewpoint(tg, new BoundingSphere(sceneBounds),
087: fieldOfView, axis);
088: }
089:
090: private static double setViewpoint(TransformGroup tg,
091: BoundingSphere sceneBounds, double fieldOfView, Axis axis) {
092: Transform3D viewTrans = new Transform3D();
093: Transform3D eyeTrans = new Transform3D();
094:
095: // point the view at the center of the object
096: Point3d center = new Point3d();
097: sceneBounds.getCenter(center);
098: double radius = sceneBounds.getRadius();
099: Point3d eyePos = new Point3d(center);
100: Vector3d up = new Vector3d();
101:
102: // pull the eye back far enough to see the whole object
103: double eyeDist = radius / Math.tan(fieldOfView / 2.0);
104: switch (axis) {
105: case POSITIVE_X_AXIS:
106: eyePos.x += eyeDist;
107: up.y = 1;
108: break;
109: case POSITIVE_Y_AXIS:
110: eyePos.y += eyeDist;
111: up.z = -1;
112: break;
113: case POSITIVE_Z_AXIS:
114: eyePos.z += eyeDist;
115: up.y = 1;
116: break;
117: case NEGATIVE_X_AXIS:
118: eyePos.x -= eyeDist;
119: up.y = 1;
120: break;
121: case NEGATIVE_Y_AXIS:
122: eyePos.y -= eyeDist;
123: up.z = -1;
124: break;
125: case NEGATIVE_Z_AXIS:
126: eyePos.z -= eyeDist;
127: up.y = 1;
128: break;
129: }
130:
131: viewTrans.setIdentity();
132: viewTrans.lookAt(eyePos, center, up);
133: viewTrans.invert();
134: // set the view transform
135: tg.setTransform(viewTrans);
136:
137: return eyeDist;
138: }
139:
140: }
|