📄 jsdirectionalsample.java
字号:
/* * $RCSfile: JSDirectionalSample.java,v $ * * Copyright (c) 2007 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: * * - Redistribution 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 MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS 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 THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.4 $ * $Date: 2007/02/09 17:20:03 $ * $State: Exp $ *//* * DirectionalSample object * * IMPLEMENTATION NOTE: The JavaSoundMixer is incomplete and really needs * to be rewritten. */package com.sun.j3d.audioengines.javasound;import javax.media.j3d.*;import com.sun.j3d.audioengines.*;import javax.vecmath.*;/** * The PostionalSample Class defines the data and methods associated with a * PointSound sample played through the AudioDevice. */class JSDirectionalSample extends JSPositionalSample{ // The transformed direction of this sound Vector3f xformDirection = new Vector3f(0.0f, 0.0f, 1.0f); public JSDirectionalSample() { super(); if (debugFlag) debugPrintln("JSDirectionalSample constructor"); } void setXformedDirection() { if (debugFlag) debugPrint("*** setXformedDirection"); if (!getVWrldXfrmFlag()) { if (debugFlag) debugPrint(" Transform NOT set yet, so dir => xformDir"); xformDirection.set(direction); } else { if (debugFlag) debugPrint(" Transform dir => xformDir"); vworldXfrm.transform(direction, xformDirection); } if (debugFlag) debugPrint(" xform(sound)Direction <= "+xformDirection.x+ ", " + xformDirection.y + ", " + xformDirection.z); } /* *********************************** * * Intersect ray to head with Ellipse * * ***********************************/ /* * An ellipse is defined using: * (1) the ConeSound's direction vector as the major axis of the ellipse; * (2) the max parameter (a front distance attenuation value) along the * cone's position axis; and * (3) the min parameter (a back distance attenuation value) along the * cone's negative axis * This method calculates the distance from the sound source to the * Intersection of the Ellipse with the ray from the sound source to the * listener's head. * This method returns the resulting distance. * If an error occurs, -1.0 is returned. * * A calculation are done in 'Cone' space: * The origin is defined as being the sound source position. * The ConeSound source axis is the X-axis of this Cone's space. * Since this ConeSound source defines a prolate spheroid (obtained * by revolving an ellipsoid about the major axis) we can define the * Y-axis of this Cone space as being in the same plane as the X-axis * and the vector from the origin to the head. * All calculations in Cone space can be generalized in this two- * dimensional space without loss of precision. * Location of the head, H, in Cone space can then be defined as: * H'(x,y) = (cos @, sin @) * | H | * where @ is the angle between the X-axis and the ray to H. * Using the equation of the line thru the origin and H', and the * equation of ellipse defined with min and max, find the * intersection by solving for x and then y. * * (I) The equation of the line thru the origin and H', and the * | H'(y) - S(y) | * y - S(y) = | ----------- | * [x - S(x)] * | H'(x) - S(x) | * and since S(x,y) is the origin of ConeSpace: * | H'(y) | * y = | ----- | x * | H'(x) | * * (II) The equation of ellipse: * x**2 y**2 * ---- + ---- = 1 * a**2 b**2 * given a is length from origin to ellipse along major, X-axis, and * b is length from origin to ellipse along minor, Y-axis; * where a**2 = [(max+min)/2]**2 , since 2a = min+max; * where b**2 = min*max , since the triangle abc is made is defined by the * the points: S(x,y), origin, and (0,b), * thus b**2 = a**2 - S(x,y) = a**2 - ((a-min)**2) = 2a*min - min**2 * b**2 = ((min+max)*min) - min**2 = min*max. * so the equation of the ellipse becomes: * x**2 y**2 * ---------------- + ------- = 1 * [(max+min)/2]**2 min*max * * Substuting for y from Eq.(I) into Eq.(II) gives * x**2 [(H'(y)/H'(x))*x]**2 * ---------------- + -------------------- = 1 * [(max+min)/2]**2 min*max * * issolating x**2 gives * | 1 [H'(y)/H'(x)]**2 | * x**2 | ---------------- + ---------------- | = 1 * | [(max+min)/2]**2 min*max | * * * | 4 [(sin @ * |H|)/(cos @ * |H|)]**2 | * x**2 | -------------- + -------------------------------- | = 1 * | [(max+min)]**2 min*max | * * | | * | 1 | * | | * x**2 = | --------------------------------------- | * | | 4 [sin @/cos @]**2 | | * | | -------------- + ---------------- | | * | | [(max+min)]**2 min*max | | * * substitute tan @ for [sin @/cos @], and take the square root and you have * the equation for x as calculated below. * * Then solve for y by plugging x into Eq.(I). * * Return the distance from the origin in Cone space to this intersection * point: square_root(x**2 + y**2). * */ double intersectEllipse(double max, double min ) { if (debugFlag) debugPrint(" intersectEllipse entered with min/max = " + min + "/" + max); /* * First find angle '@' between the X-axis ('A') and the ray to Head ('H'). * In local coordinates, use Dot Product of those two vectors to get cos @: * A(u)*H(u) + A(v)*H(v) + A(w)*H(v) * cos @ = -------------------------------- * |A|*|H| * then since domain of @ is { 0 <= @ <= PI }, arccos can be used to get @. */ Vector3f xAxis = this.direction; // axis is sound direction vector // Get the already calculated vector from sound source position to head Vector3f sourceToHead = this.sourceToCenterEar; // error check vectors not empty if (xAxis == null || sourceToHead == null) { if (debugFlag) debugPrint( " one or both of the vectors are null" ); return (-1.0f); // denotes an error occurred } // Dot Product double dotProduct = (double)( (sourceToHead.dot(xAxis)) / (sourceToHead.length() * xAxis.length())); if (debugFlag) debugPrint( " dot product = " + dotProduct ); // since theta angle is in the range between 0 and PI, arccos can be used double theta = (float)(Math.acos(dotProduct)); if (debugFlag) debugPrint( " theta = " + theta ); /* * Solve for X using Eq.s (I) and (II) from above. */ double minPlusMax = (double)(min + max); double tangent = Math.tan(theta); double xSquared = 1.0 / ( ( 4.0 / (minPlusMax * minPlusMax) ) + ( (tangent * tangent) / (min * max) ) ); double x = Math.sqrt(xSquared); if (debugFlag) debugPrint( " X = " + x ); /* * Solve for y, given the result for x: * | H'(y) | | sin @ | * y = | ----- | x = | ----- | x * | H'(x) | | cos @ | */ double y = tangent * x; if (debugFlag) debugPrint( " Y = " + y ); double ySquared = y * y; /* * Now return distance from origin to intersection point (x,y) */ float distance = (float)(Math.sqrt(xSquared + ySquared)); if (debugFlag) debugPrint( " distance to intersection = " + distance ); return (distance); } /* *****************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -