|
/*
* @(#)GearTest.java 1.17 02/10/21 13:40:16
*
* Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: -
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. - Redistribution in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGES.
*
* You acknowledge that Software is not designed,licensed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Material;
import javax.media.j3d.QuadArray;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleFanArray;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
public class GearTest extends Applet {
static final int defaultToothCount = 24;
private int toothCount;
private SimpleUniverse u = null;
public BranchGroup createSceneGraph(int toothCount) {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create a Transformgroup to scale all objects so they
// appear in the scene.
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
// Create a bounds for the background and lights
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// Set up the background
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
objScale.addChild(bgNode);
// Set up the global lights
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
Vector3f light2Direction = new Vector3f(-6.0f, -2.0f, -1.0f);
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objScale.addChild(ambientLightNode);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objScale.addChild(light1);
DirectionalLight light2 = new DirectionalLight(light2Color,
light2Direction);
light2.setInfluencingBounds(bounds);
objScale.addChild(light2);
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objScale.addChild(objTrans);
// Create an Appearance.
Appearance look = new Appearance();
Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
look
.setMaterial(new Material(objColor, black, objColor, white,
100.0f));
// Create a gear, add it to the scene graph.
// SpurGear gear = new SpurGear(toothCount, 1.0f, 0.2f,
SpurGear gear = new SpurGearThinBody(toothCount, 1.0f, 0.2f, 0.05f,
0.05f, 0.3f, 0.28f, look);
objTrans.addChild(gear);
// Create a new Behavior object that will rotate the object and
// add it into the scene graph.
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
8000, 0, 0, 0, 0, 0);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
// Have Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
public GearTest() {
this(defaultToothCount);
}
public GearTest(int toothCount) {
this.toothCount = toothCount;
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph(toothCount);
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void destroy() {
u.cleanup();
}
//
// The following allows GearTest to be run as an application
// as well as an applet
//
public static void main(String[] args) {
int value;
if (args.length > 1) {
System.out.println("Usage: java GearTest [#teeth]");
System.exit(0);
} else if (args.length == 0) {
new MainFrame(new GearTest(), 700, 700);
} else {
try {
value = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Illegal integer specified");
System.out.println("Usage: java GearTest [#teeth]");
value = 0;
System.exit(0);
}
if (value <= 0) {
System.out.println("Integer must be positive (> 0)");
System.out.println("Usage: java GearBox [#teeth]");
System.exit(0);
}
new MainFrame(new GearTest(value), 700, 700);
}
}
}
class Gear extends javax.media.j3d.TransformGroup {
// Specifiers determining whether to generate outward facing normals or
// inward facing normals.
static final int OutwardNormals = 1;
static final int InwardNormals = -1;
// The number of teeth in the gear
int toothCount;
// Gear start differential angle. All gears are constructed with the
// center of a tooth at Z-axis angle = 0.
double gearStartAngle;
// The Z-rotation angle to place the tooth center at theta = 0
float toothTopCenterAngle;
// The Z-rotation angle to place the valley center at theta = 0
float valleyCenterAngle;
// The angle about Z subtended by one tooth and its associated valley
float circularPitchAngle;
// Increment angles
float toothValleyAngleIncrement;
// Front and rear facing normals for the gear's body
final Vector3f frontNormal = new Vector3f(0.0f, 0.0f, -1.0f);
final Vector3f rearNormal = new Vector3f(0.0f, 0.0f, 1.0f);
Gear(int toothCount) {
this.toothCount = toothCount;
}
void addBodyDisks(float shaftRadius, float bodyOuterRadius,
float thickness, Appearance look) {
int gearBodySegmentVertexCount; // #(segments) per tooth-unit
int gearBodyTotalVertexCount; // #(vertices) in a gear face
int gearBodyStripCount[] = new int[1]; // per strip (1) vertex count
// A ray from the gear center, used in normal calculations
float xDirection, yDirection;
// The x and y coordinates at each point of a facet and at each
// point on the gear: at the shaft, the root of the teeth, and
// the outer point of the teeth
float xRoot0, yRoot0, xShaft0, yShaft0;
float xRoot3, yRoot3, xShaft3, yShaft3;
float xRoot4, yRoot4, xShaft4, yShaft4;
// Temporary variables for storing coordinates and vectors
Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
// Gear start differential angle. All gears are constructed with the
// center of a tooth at Z-axis angle = 0.
double gearStartAngle = -1.0 * toothTopCenterAngle;
// Temporaries that store start angle for each portion of tooth facet
double toothStartAngle, toothTopStartAngle, toothDeclineStartAngle, toothValleyStartAngle, nextToothStartAngle;
Shape3D newShape;
int index;
// The z coordinates for the body disks
final float frontZ = -0.5f * thickness;
final float rearZ = 0.5f * thickness;
/*
* Construct the gear's front body (front facing torus disk) __2__ - | -
* 4 - /| /- / / | /| \ 0\ / | / / > \ / | / | > \ / | / / | \ / ____|/ | >
* \-- --__/ | 1 3 5
*
*/
gearBodySegmentVertexCount = 4;
gearBodyTotalVertexCount = 2 + gearBodySegmentVertexCount * toothCount;
gearBodyStripCount[0] = gearBodyTotalVertexCount;
TriangleStripArray frontGearBody = new TriangleStripArray(
gearBodyTotalVertexCount, GeometryArray.COORDINATES
| GeometryArray.NORMALS, gearBodyStripCount);
xDirection = (float) Math.cos(gearStartAngle);
yDirection = (float) Math.sin(gearStartAngle);
xShaft0 = shaftRadius * xDirection;
yShaft0 = shaftRadius * yDirection;
xRoot0 = bodyOuterRadius * xDirection;
yRoot0 = bodyOuterRadius * yDirection;
coordinate.set(xRoot0, yRoot0, frontZ);
frontGearBody.setCoordinate(0, coordinate);
frontGearBody.setNormal(0, frontNormal);
coordinate.set(xShaft0, yShaft0, frontZ);
frontGearBody.setCoordinate(1, coordinate);
frontGearBody.setNormal(1, frontNormal);
for (int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle = gearStartAngle + circularPitchAngle
* (double) count;
toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float) Math.cos(toothValleyStartAngle);
yDirection = (float) Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
xRoot3 = bodyOuterRadius * xDirection;
yRoot3 = bodyOuterRadius * yDirection;
xDirection = (float) Math.cos(nextToothStartAngle);
yDirection = (float) Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
xRoot4 = bodyOuterRadius * xDirection;
yRoot4 = bodyOuterRadius * yDirection;
coordinate.set(xRoot3, yRoot3, frontZ);
frontGearBody.setCoordinate(index, coordinate);
frontGearBody.setNormal(index, frontNormal);
coordinate.set(xShaft3, yShaft3, frontZ);
frontGearBody.setCoordinate(index + 1, coordinate);
frontGearBody.setNormal(index + 1, frontNormal);
coordinate.set(xRoot4, yRoot4, frontZ);
frontGearBody.setCoordinate(index + 2, coordinate);
frontGearBody.setNormal(index + 2, frontNormal);
coordinate.set(xShaft4, yShaft4, frontZ);
frontGearBody.setCoordinate(index + 3, coordinate);
frontGearBody.setNormal(index + 3, frontNormal);
}
newShape = new Shape3D(frontGearBody, look);
this.addChild(newShape);
// Construct the gear's rear body (rear facing torus disc)
TriangleStripArray rearGearBody = new TriangleStripArray(
gearBodyTotalVertexCount, GeometryArray.COORDINATES
| GeometryArray.NORMALS, gearBodyStripCount);
xDirection = (float) Math.cos(gearStartAngle);
yDirection = (float) Math.sin(gearStartAngle);
xShaft0 = shaftRadius * xDirection;
yShaft0 = shaftRadius * yDirection;
xRoot0 = bodyOuterRadius * xDirection;
yRoot0 = bodyOuterRadius * yDirection;
coordinate.set(xShaft0, yShaft0, rearZ);
rearGearBody.setCoordinate(0, coordinate);
rearGearBody.setNormal(0, rearNormal);
coordinate.set(xRoot0, yRoot0, rearZ);
rearGearBody.setCoordinate(1, coordinate);
rearGearBody.setNormal(1, rearNormal);
for (int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle = gearStartAngle + circularPitchAngle
* (double) count;
toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float) Math.cos(toothValleyStartAngle);
yDirection = (float) Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
xRoot3 = bodyOuterRadius * xDirection;
yRoot3 = bodyOuterRadius * yDirection;
xDirection = (float) Math.cos(nextToothStartAngle);
yDirection = (float) Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
xRoot4 = bodyOuterRadius * xDirection;
yRoot4 = bodyOuterRadius * yDirection;
coordinate.set(xShaft3, yShaft3, rearZ);
rearGearBody.setCoordinate(index, coordinate);
rearGearBody.setNormal(index, rearNormal);
coordinate.set(xRoot3, yRoot3, rearZ);
rearGearBody.setCoordinate(index + 1, coordinate);
rearGearBody.setNormal(index + 1, rearNormal);
coordinate.set(xShaft4, yShaft4, rearZ);
rearGearBody.setCoordinate(index + 2, coordinate);
rearGearBody.setNormal(index + 2, rearNormal);
coordinate.set(xRoot4, yRoot4, rearZ);
rearGearBody.setCoordinate(index + 3, coordinate);
rearGearBody.setNormal(index + 3, rearNormal);
}
newShape = new Shape3D(rearGearBody, look);
this.addChild(newShape);
}
void addCylinderSkins(float shaftRadius, float length, int normalDirection,
Appearance look) {
int insideShaftVertexCount; // #(vertices) for shaft
int insideShaftStripCount[] = new int[1]; // #(vertices) in strip/strip
double toothStartAngle, nextToothStartAngle, toothValleyStartAngle;
// A ray from the gear center, used in normal calculations
float xDirection, yDirection;
// The z coordinates for the body disks
final float frontZ = -0.5f * length;
final float rearZ = 0.5f * length;
// Temporary variables for storing coordinates, points, and vectors
float xShaft3, yShaft3, xShaft4, yShaft4;
Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
Vector3f surfaceNormal = new Vector3f();
Shape3D newShape;
int index;
int firstIndex;
int secondIndex;
/*
* Construct gear's inside shaft cylinder First the tooth's up, flat
* outer, and down distances Second the tooth's flat inner distance
*
* Outward facing vertex order: 0_______2____4 | /| /| | / | / | | / | / |
* |/______|/___| 1 3 5
*
* Inward facing vertex order: 1_______3____5 |\ |\ | | \ | \ | | \ | \ |
* |______\|___\| 0 2 4
*/
insideShaftVertexCount = 4 * toothCount + 2;
insideShaftStripCount[0] = insideShaftVertexCount;
TriangleStripArray insideShaft = new TriangleStripArray(
insideShaftVertexCount, GeometryArray.COORDINATES
| GeometryArray.NORMALS, insideShaftStripCount);
xShaft3 = shaftRadius * (float) Math.cos(gearStartAngle);
yShaft3 = shaftRadius * (float) Math.sin(gearStartAngle);
if (normalDirection == OutwardNormals) {
surfaceNormal.set(1.0f, 0.0f, 0.0f);
firstIndex = 1;
secondIndex = 0;
} else {
surfaceNormal.set(-1.0f, 0.0f, 0.0f);
firstIndex = 0;
secondIndex = 1;
}
// Coordinate labeled 0 in the strip
coordinate.set(shaftRadius, 0.0f, frontZ);
insideShaft.setCoordinate(firstIndex, coordinate);
insideShaft.setNormal(firstIndex, surfaceNormal);
// Coordinate labeled 1 in the strip
coordinate.set(shaftRadius, 0.0f, rearZ);
insideShaft.setCoordinate(secondIndex, coordinate);
insideShaft.setNormal(secondIndex, surfaceNormal);
for (int count = 0; count < toothCount; count++) {
index = 2 + count * 4;
toothStartAngle = circularPitchAngle * (double) count;
toothValleyStartAngle = toothStartAngle + toothValleyAngleIncrement;
nextToothStartAngle = toothStartAngle + circularPitchAngle;
xDirection = (float) Math.cos(toothValleyStartAngle);
yDirection = (float) Math.sin(toothValleyStartAngle);
xShaft3 = shaftRadius * xDirection;
yShaft3 = shaftRadius * yDirection;
if (normalDirection == OutwardNormals)
surfaceNormal.set(xDirection, yDirection, 0.0f);
else
surfaceNormal.set(-xDirection, -yDirection, 0.0f);
// Coordinate labeled 2 in the strip
coordinate.set(xShaft3, yShaft3, frontZ);
insideShaft.setCoordinate(index + firstIndex, coordinate);
insideShaft.setNormal(index + firstIndex, surfaceNormal);
// Coordinate labeled 3 in the strip
coordinate.set(xShaft3, yShaft3, rearZ);
insideShaft.setCoordinate(index + secondIndex, coordinate);
insideShaft.setNormal(index + secondIndex, surfaceNormal);
xDirection = (float) Math.cos(nextToothStartAngle);
yDirection = (float) Math.sin(nextToothStartAngle);
xShaft4 = shaftRadius * xDirection;
yShaft4 = shaftRadius * yDirection;
if (normalDirection == OutwardNormals)
surfaceNormal.set(xDirection, yDirection, 0.0f);
else
surfaceNormal.set(-xDirection, -yDirection, 0.0f);
// Coordinate labeled 4 in the strip
coordinate.set(xShaft4, yShaft4, frontZ);
insideShaft.setCoordinate(index + 2 + firstIndex, coordinate);
insideShaft.setNormal(index + 2 + firstIndex, surfaceNormal);
// Coordinate labeled 5 in the strip
coordinate.set(xShaft4, yShaft4, rearZ);
insideShaft.setCoordinate(index + 2 + secondIndex, coordinate);
insideShaft.setNormal(index + 2 + secondIndex, surfaceNormal);
}
newShape = new Shape3D(insideShaft, look);
this.addChild(newShape);
}
public float getToothTopCenterAngle() {
return toothTopCenterAngle;
}
public float getValleyCenterAngle() {
return valleyCenterAngle;
}
public float getCircularPitchAngle() {
return circularPitchAngle;
}
}
class GearBox extends Applet {
static final int defaultToothCount = 48;
private int toothCount;
private SimpleUniverse u = null;
public BranchGroup createGearBox(int toothCount) {
Transform3D tempTransform = new Transform3D();
// Create the root of the branch graph
BranchGroup branchRoot = createBranchEnvironment();
// Create a Transformgroup to scale all objects so they
// appear in the scene.
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objScale.setTransform(t3d);
branchRoot.addChild(objScale);
// Create an Appearance.
Appearance look = new Appearance();
Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
look
.setMaterial(new Material(objColor, black, objColor, white,
100.0f));
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
TransformGroup gearboxTrans = new TransformGroup();
gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(gearboxTrans);
// Create a bounds for the mouse behavior methods
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
100.0);
// Define the shaft base information
int shaftCount = 4;
int secondsPerRevolution = 8000;
// Create the Shaft(s)
Shaft shafts[] = new Shaft[shaftCount];
TransformGroup shaftTGs[] = new TransformGroup[shaftCount];
Alpha shaftAlphas[] = new Alpha[shaftCount];
RotationInterpolator shaftRotors[] = new RotationInterpolator[shaftCount];
Transform3D shaftAxis[] = new Transform3D[shaftCount];
// Note: the following arrays we're incorporated to make changing
// the gearbox easier.
float shaftRatios[] = new float[shaftCount];
shaftRatios[0] = 1.0f;
shaftRatios[1] = 0.5f<
|