All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scijava.java3d.ConeSoundRetained Maven / Gradle / Ivy

/*
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package org.scijava.java3d;

import org.scijava.vecmath.Point2f;
import org.scijava.vecmath.Point3f;
import org.scijava.vecmath.Vector3f;



/**
 * A ConeSoundRetained node defines a point sound source located at some
 * location
 * in space whose amplitude is constrained not only by maximum and minimum
 * amplitude
 * spheres but by two concentric cone volumes directed down an vector radiating
 * from the sound's location.
 */

class ConeSoundRetained extends PointSoundRetained {
    /**
     * The Cone Sound's direction vector.  This is the cone axis.
     */
    Vector3f	direction = new Vector3f(0.0f, 0.0f, 1.0f);

    // The transformed direction of this sound
    Vector3f xformDirection = new Vector3f(0.0f, 0.0f, 1.0f);

    // Sound's gain is attenuated for listener locations off-angle from
    // the source source direction.
    // This can be set of three numbers:
    //     angular distance in radians
    //     gain scale factor
    //     filtering (currently the only filtering supported is lowpass)

    // For now the only supported filterType will be LOW_PASS frequency cutoff.
    // At some time full FIR filtering will be supported.
    static final int  NO_FILTERING  = -1;
    static final int  LOW_PASS      =  1;

    // Pairs of distances and gain scale factors that define piecewise linear
    // gain BACK attenuation between each pair.
    // These are used for defining elliptical attenuation regions.
    float[]     backAttenuationDistance = null;
    float[]     backAttenuationGain = null;

    float[]	angularDistance = {0.0f, ((float)(Math.PI) * 0.5f)};
    float[]	angularGain     = {1.0f, 0.0f};
    int         filterType      = NO_FILTERING;
    float[]     frequencyCutoff = {Sound.NO_FILTER, Sound.NO_FILTER};

    ConeSoundRetained() {
        this.nodeType = NodeRetained.CONESOUND;
    }

    // *********************
    //
    // Distance Gain methods
    //
    // *********************

    /**
     * Sets this sound's distance gain elliptical attenuation -
     * where gain scale factor is applied to sound based on distance listener
     * is from sound source.
     * @param frontAttenuation defined by pairs of (distance,gain-scale-factor)
     * @param backAttenuation defined by pairs of (distance,gain-scale-factor)
     * @exception CapabilityNotSetException if appropriate capability is
     * not set and this object is part of live or compiled scene graph
     */
    void setDistanceGain(Point2f[] frontAttenuation,
                                      Point2f[] backAttenuation ) {

        this.setDistanceGain(frontAttenuation);
        this.setBackDistanceGain(backAttenuation);
    }

    /**
     * Sets this sound's distance gain attenuation as an array of Point2fs.
     * @param frontDistance array of monotonically-increasing floats
     * @param frontGain array of non-negative scale factors
     * @param backDistance array of monotonically-increasing floats
     * @param backGain array of non-negative scale factors
     * @exception CapabilityNotSetException if appropriate capability is
     * not set and this object is part of live or compiled scene graph
     */
    void setDistanceGain(float[] frontDistance, float[] frontGain,
                                      float[] backDistance, float[] backGain) {
        this.setDistanceGain(frontDistance, frontGain);
        this.setBackDistanceGain(backDistance, backGain);
    }

    /**
     * Sets this sound's back distance gain attenuation - where gain scale
     * factor is applied to sound based on distance listener along the negative
     * sound direction axis from sound source.
     * @param attenuation defined by pairs of (distance,gain-scale-factor)
     * @exception CapabilityNotSetException if appropriate capability is
     * not set and this object is part of live or compiled scene graph
     */
    void setBackDistanceGain(Point2f[] attenuation)
    {
        // if attenuation array null set both attenuation components to null
        if (attenuation == null) {
            this.backAttenuationDistance = null;
            this.backAttenuationGain = null;
        }
        else {
            int attenuationLength = attenuation.length;
            if (attenuationLength == 0) {
                this.backAttenuationDistance = null;
                this.backAttenuationGain = null;
            }
            else {
                this.backAttenuationDistance = new float[attenuationLength];
                this.backAttenuationGain = new float[attenuationLength];
                for (int i = 0; i < attenuationLength; i++) {
                    this.backAttenuationDistance[i] = attenuation[i].x;
                    this.backAttenuationGain[i] = attenuation[i].y;
                }
            }
        }
        dispatchAttribChange(BACK_DISTANCE_GAIN_DIRTY_BIT, attenuation);
	if (source != null && source.isLive()) {
	    notifySceneGraphChanged(false);
	}
    }

    /**
     * Sets this sound's back distance gain attenuation as an array of Point2fs.
     * @param distance array of monotonically-increasing floats
     * @param gain array of non-negative scale factors
     * @exception CapabilityNotSetException if appropriate capability is
     * not set and this object is part of live or compiled scene graph
     */
    void setBackDistanceGain(float[] distance, float[] gain)
    {
        int distanceLength = 0;
        // if distance or gain arrays are null then treat both as null
        if (distance == null || gain == null) {
            this.backAttenuationDistance = null;
            this.backAttenuationGain = null;
        }
        else {
            // now process the back attenuation values
            int gainLength = gain.length;
            distanceLength = distance.length;
            if (distanceLength == 0 || gainLength == 0) {
                this.backAttenuationDistance = null;
                this.backAttenuationGain = null;
            }
            else {
                this.backAttenuationDistance = new float[distanceLength];
                this.backAttenuationGain = new float[distanceLength];
                // Copy the distance array into nodes field
                System.arraycopy(distance, 0, this.backAttenuationDistance,
                                           0, distanceLength);
                // Copy the gain array an array of same length as the distance array
                if (distanceLength <= gainLength) {
                    System.arraycopy(gain, 0, this.backAttenuationGain,
                                           0, distanceLength);
                }
                else {
                    System.arraycopy(gain, 0, this.backAttenuationGain, 0, gainLength);
		    // Extend gain array to length of distance array
		    // replicate last gain values.
                    for (int i=gainLength; i< distanceLength; i++) {
                        this.backAttenuationGain[i] = gain[gainLength - 1];
                    }
                }
            }
        }

        Point2f [] attenuation = new Point2f[distanceLength];
        for (int i=0; i distanceLength)
            attenuationLength = distanceLength;
        System.arraycopy(this.backAttenuationDistance, 0, distance, 0, attenuationLength);
        attenuationLength = this.backAttenuationGain.length;
        int gainLength = gain.length;
        if (attenuationLength > gainLength)
            attenuationLength = gainLength;
        System.arraycopy(this.backAttenuationGain, 0, gain, 0, attenuationLength);
    }


    // *********************
    //
    // Direction Methods
    //
    // *********************

    /**
     * Sets this sound's direction from the vector provided.
     * @param direction the new direction
     */
    void setDirection(Vector3f direction) {
	if (staticTransform != null) {
	    staticTransform.transform.transform(direction, this.direction);
	} else {
	    this.direction.set(direction);
	}
        dispatchAttribChange(DIRECTION_DIRTY_BIT,
				(new Vector3f(this.direction)));

	if (source != null && source.isLive()) {
	    notifySceneGraphChanged(false);
	}
    }

    /**
     * Sets this sound's direction from the three values provided.
     * @param x the new x direction
     * @param y the new y direction
     * @param z the new z direction
     */
    void setDirection(float x, float y, float z) {
	direction.x = x;
	direction.y = y;
	direction.z = z;
	if (staticTransform != null) {
	    staticTransform.transform.transform(direction);
	}
        dispatchAttribChange(DIRECTION_DIRTY_BIT, (new Vector3f(direction)));

	if (source != null && source.isLive()) {
	    notifySceneGraphChanged(false);
	}
    }


    /**
     * Retrieves this sound's direction and places it in the
     * vector provided.
     * @return direction vector (axis of cones)
     */
    void getDirection(Vector3f direction)
    {
        if (staticTransform != null) {
            Transform3D invTransform = staticTransform.getInvTransform();
            invTransform.transform(this.direction, direction);
        } else {
            direction.set(this.direction);
        }
    }

    void getXformDirection(Vector3f direction)
    {
        direction.set(this.xformDirection);
    }


    // ***************************
    //
    // Angular Attenuation
    //
    // ***************************

    /**
     * Sets this sound's angular gain attenuation (not including filter)
     * @param attenuation array containing angular distance and gain
     */
    void setAngularAttenuation(Point2f[] attenuation) {
        int attenuationLength = 0;
        this.filterType = NO_FILTERING;
        if (attenuation == null) {
            this.angularDistance = null;
            this.angularGain = null;
        }
        else {
            attenuationLength = attenuation.length;
            if (attenuationLength == 0) {
                this.angularDistance = null;
                this.angularGain = null;
            }
            else {
                this.angularDistance = new float[attenuationLength];
                this.angularGain = new float[attenuationLength];
                for (int i = 0; i < attenuationLength; i++) {
                   this.angularDistance[i] = attenuation[i].x;
                   this.angularGain[i] = attenuation[i].y;
                }
            } // lengths not zero
        } // arrays not null
        Point3f [] attenuation3f = new Point3f[attenuationLength];
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy