⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spurgear.java

📁 JAVA网络三维技术3D的设计与实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *	@(#)SpurGear.java 1.12 98/02/20 14:29:58
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * 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.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */
package Java3DApplet;
import java.lang.Math.*;
import javax.media.j3d.*;
import javax.vecmath.*;

public class SpurGear extends Gear {

    float toothTopAngleIncrement;
    float toothDeclineAngleIncrement;

    float rootRadius;
    float outsideRadius;

    //The angle subtended by the ascending or descending portion of a tooth
    float circularToothEdgeAngle;
    // The angle subtended by a flat (either a tooth top or a valley
    // between teeth
    float circularToothFlatAngle;

    /**
     * internal constructor for SpurGear, used by subclasses to establish
     * SpurGear's required state
     * @return a new spur gear that contains sufficient information to
     * continue building
     * @param toothCount number of teeth
     * @param pitchCircleRadius radius at center of teeth
     * @param addendum distance from pitch circle to top of teeth
     * @param dedendum distance from pitch circle to root of teeth
     * @param toothToValleyAngleRatio the ratio of the angle subtended by the
     * tooth to the angle subtended by the valley (must be <= .25)
     */
    SpurGear(int toothCount, float pitchCircleRadius,
	     float addendum, float dedendum, float toothToValleyAngleRatio) {

	super(toothCount);

	// The angle about Z subtended by one tooth and its associated valley
	circularPitchAngle = (float)(2.0 * Math.PI / (double)toothCount);

	// The angle subtended by a flat (either a tooth top or a valley
	// between teeth
	circularToothFlatAngle = circularPitchAngle * toothToValleyAngleRatio;

	//The angle subtended by the ascending or descending portion of a tooth
	circularToothEdgeAngle = circularPitchAngle/2.0f -
	    circularToothFlatAngle;

	// Increment angles
	toothTopAngleIncrement = circularToothEdgeAngle;
	toothDeclineAngleIncrement
	    = toothTopAngleIncrement + circularToothFlatAngle;
	toothValleyAngleIncrement
	    = toothDeclineAngleIncrement + circularToothEdgeAngle;

	// Differential angles for offsetting to the center of tooth's top
	// and valley
	toothTopCenterAngle
	    = toothTopAngleIncrement + circularToothFlatAngle/2.0f;
	valleyCenterAngle
	    = toothValleyAngleIncrement +  circularToothFlatAngle/2.0f;

	// Gear start differential angle. All gears are constructed with the
	// center of a tooth at Z-axis angle = 0.
	gearStartAngle = -1.0 * toothTopCenterAngle;

	// The radial distance to the root and top of the teeth, respectively
	rootRadius = pitchCircleRadius - dedendum;
	outsideRadius = pitchCircleRadius + addendum;

	// Allow this object to spin. etc.
	this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    }

    /**
     * Construct a SpurGear;
     * @return a new spur gear that conforms to the input paramters
     * @param toothCount number of teeth
     * @param pitchCircleRadius radius at center of teeth
     * @param shaftRadius radius of hole at center
     * @param addendum distance from pitch circle to top of teeth
     * @param dedendum distance from pitch circle to root of teeth
     * @param gearThickness  thickness of the gear
     */
    public SpurGear(int toothCount, float pitchCircleRadius, float shaftRadius,
		    float addendum, float dedendum, float gearThickness) {
	this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
	     gearThickness, gearThickness, 0.25f, null);
    }

    /**
     * Construct a SpurGear;
     * @return a new spur gear that conforms to the input paramters
     * @param toothCount number of teeth
     * @param pitchCircleRadius radius at center of teeth
     * @param shaftRadius radius of hole at center
     * @param addendum distance from pitch circle to top of teeth
     * @param dedendum distance from pitch circle to root of teeth
     * @param gearThickness  thickness of the gear
     * @param look the gear's appearance
     */
    public SpurGear(int toothCount, float pitchCircleRadius, float shaftRadius,
		    float addendum, float dedendum, float gearThickness,
		    Appearance look) {
	this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
	     gearThickness, gearThickness, 0.25f, look);
    }

    /**
     * Construct a SpurGear;
     * @return a new spur gear that conforms to the input paramters
     * @param toothCount number of teeth
     * @param pitchCircleRadius radius at center of teeth
     * @param shaftRadius radius of hole at center
     * @param addendum distance from pitch circle to top of teeth
     * @param dedendum distance from pitch circle to root of teeth
     * @param gearThickness thickness of the gear
     * @param toothTipThickness thickness of the tip of the tooth
     * @param look the gear's appearance
     */
    public SpurGear(int toothCount, float pitchCircleRadius, float shaftRadius,
		    float addendum, float dedendum, float gearThickness,
		    float toothTipThickness, Appearance look) {
	this(toothCount, pitchCircleRadius, shaftRadius, addendum, dedendum,
	     gearThickness, toothTipThickness, 0.25f, look);
	}

    /**
     * Construct a SpurGear;
     * @return a new spur gear that conforms to the input paramters
     * @param toothCount number of teeth
     * @param pitchCircleRadius radius at center of teeth
     * @param shaftRadius radius of hole at center
     * @param addendum distance from pitch circle to top of teeth
     * @param dedendum distance from pitch circle to root of teeth
     * @param gearThickness thickness of the gear
     * @param toothTipThickness thickness of the tip of the tooth
     * @param toothToValleyAngleRatio the ratio of the angle subtended by the
     * tooth to the angle subtended by the valley (must be <= .25)
     * @param look the gear's appearance object
     */
    public SpurGear(int toothCount, float pitchCircleRadius, float shaftRadius,
		    float addendum, float dedendum, float gearThickness,
		    float toothTipThickness, float toothToValleyAngleRatio,
		    Appearance look) {

    this(toothCount, pitchCircleRadius, addendum, dedendum,
	 toothToValleyAngleRatio);

	// Generate the gear's body disks
	addBodyDisks(shaftRadius, rootRadius, gearThickness, look);

	// Generate the gear's interior shaft
	addCylinderSkins(shaftRadius, gearThickness, InwardNormals, look);

	// Generate the gear's teeth
	addTeeth(pitchCircleRadius, rootRadius,
		 outsideRadius, gearThickness, toothTipThickness,
		 toothToValleyAngleRatio, look);
    }

    /**
     * Construct a SpurGear's teeth by adding the teeth shape nodes
     * @param pitchCircleRadius radius at center of teeth
     * @param rootRadius distance from pitch circle to top of teeth
     * @param outsideRadius distance from pitch circle to root of teeth
     * @param gearThickness thickness of the gear
     * @param toothTipThickness thickness of the tip of the tooth
     * @param toothToValleyAngleRatio the ratio of the angle subtended by the
     * tooth to the angle subtended by the valley (must be <= .25)
     * @param look the gear's appearance object
     */
    void addTeeth(float pitchCircleRadius, float rootRadius,
		  float outsideRadius, float gearThickness,
		  float toothTipThickness, float toothToValleyAngleRatio,
		  Appearance look) {
	int index;
	Shape3D newShape;

	// Temporaries that store start angle for each portion of tooth facet
	double toothStartAngle, toothTopStartAngle,
	    toothDeclineStartAngle, toothValleyStartAngle,
	    nextToothStartAngle;

	// 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;
	float xOuter1, yOuter1;
	float xOuter2, yOuter2;
	float xRoot3, yRoot3;
	float xRoot4, yRoot4;

	// The z coordinates for the gear
	final float frontZ = -0.5f * gearThickness;
	final float rearZ = 0.5f * gearThickness;

	// The z coordinates for the tooth tip of the gear
	final float toothTipFrontZ = -0.5f * toothTipThickness;
	final float toothTipRearZ = 0.5f * toothTipThickness;

	int toothFacetVertexCount;		// #(vertices) per tooth facet
	int toothFacetCount;			// #(facets) per tooth
	int toothFaceTotalVertexCount;          // #(vertices) in all teeth
	int toothFaceStripCount[] = new int[toothCount];
						// per tooth vertex count
	int topVertexCount;			// #(vertices) for teeth tops
	int topStripCount[] = new int[1];	// #(vertices) in strip/strip

	// Front and rear facing normals for the teeth faces
	Vector3f frontToothNormal = new Vector3f(0.0f, 0.0f, -1.0f);
	Vector3f rearToothNormal = new Vector3f(0.0f, 0.0f, 1.0f);

	// Normals for teeth tops up incline, tooth top, and down incline
	Vector3f leftNormal = new Vector3f(-1.0f, 0.0f, 0.0f);
	Vector3f rightNormal = new Vector3f(1.0f, 0.0f, 0.0f);
	Vector3f outNormal = new Vector3f(1.0f, 0.0f, 0.0f);
	Vector3f inNormal = new Vector3f(-1.0f, 0.0f, 0.0f);

	// Temporary variables for storing coordinates and vectors
	Point3f coordinate = new Point3f(0.0f, 0.0f, 0.0f);
	Point3f tempCoordinate1 = new Point3f(0.0f, 0.0f, 0.0f);
	Point3f tempCoordinate2 = new Point3f(0.0f, 0.0f, 0.0f);
	Point3f tempCoordinate3 = new Point3f(0.0f, 0.0f, 0.0f);
	Vector3f tempVector1 = new Vector3f(0.0f, 0.0f, 0.0f);
	Vector3f tempVector2 = new Vector3f(0.0f, 0.0f, 0.0f);

	/* Construct the gear's front facing teeth facets
	 *	   0______2
	 *         /     /\
	 *        /   /    \
	 *       /  /       \
	 *      //___________\
	 *     1              3
	 */
	toothFacetVertexCount = 4;
	toothFaceTotalVertexCount = toothFacetVertexCount * toothCount;
	for(int i = 0; i < toothCount; i++)
	    toothFaceStripCount[i] = toothFacetVertexCount;

	TriangleStripArray frontGearTeeth
	    = new TriangleStripArray(toothFaceTotalVertexCount,
				     GeometryArray.COORDINATES
				     | GeometryArray.NORMALS,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -