Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package com.jme3.util;
     33 
     34 import com.jme3.collision.bih.BIHNode.BIHStackData;
     35 import com.jme3.math.*;
     36 import com.jme3.scene.Spatial;
     37 import java.nio.FloatBuffer;
     38 import java.nio.IntBuffer;
     39 import java.util.ArrayList;
     40 
     41 /**
     42  * Temporary variables assigned to each thread. Engine classes may access
     43  * these temp variables with TempVars.get(), all retrieved TempVars
     44  * instances must be returned via TempVars.release().
     45  * This returns an available instance of the TempVar class ensuring this
     46  * particular instance is never used elsewhere in the mean time.
     47  */
     48 public class TempVars {
     49 
     50     /**
     51      * Allow X instances of TempVars in a single thread.
     52      */
     53     private static final int STACK_SIZE = 5;
     54 
     55     /**
     56      * <code>TempVarsStack</code> contains a stack of TempVars.
     57      * Every time TempVars.get() is called, a new entry is added to the stack,
     58      * and the index incremented.
     59      * When TempVars.release() is called, the entry is checked against
     60      * the current instance and  then the index is decremented.
     61      */
     62     private static class TempVarsStack {
     63 
     64         int index = 0;
     65         TempVars[] tempVars = new TempVars[STACK_SIZE];
     66     }
     67     /**
     68      * ThreadLocal to store a TempVarsStack for each thread.
     69      * This ensures each thread has a single TempVarsStack that is
     70      * used only in method calls in that thread.
     71      */
     72     private static final ThreadLocal<TempVarsStack> varsLocal = new ThreadLocal<TempVarsStack>() {
     73 
     74         @Override
     75         public TempVarsStack initialValue() {
     76             return new TempVarsStack();
     77         }
     78     };
     79     /**
     80      * This instance of TempVars has been retrieved but not released yet.
     81      */
     82     private boolean isUsed = false;
     83 
     84     private TempVars() {
     85     }
     86 
     87     /**
     88      * Acquire an instance of the TempVar class.
     89      * You have to release the instance after use by calling the
     90      * release() method.
     91      * If more than STACK_SIZE (currently 5) instances are requested
     92      * in a single thread then an ArrayIndexOutOfBoundsException will be thrown.
     93      *
     94      * @return A TempVar instance
     95      */
     96     public static TempVars get() {
     97         TempVarsStack stack = varsLocal.get();
     98 
     99         TempVars instance = stack.tempVars[stack.index];
    100 
    101         if (instance == null) {
    102             // Create new
    103             instance = new TempVars();
    104 
    105             // Put it in there
    106             stack.tempVars[stack.index] = instance;
    107         }
    108 
    109         stack.index++;
    110 
    111         instance.isUsed = true;
    112 
    113         return instance;
    114     }
    115 
    116     /**
    117      * Releases this instance of TempVars.
    118      * Once released, the contents of the TempVars are undefined.
    119      * The TempVars must be released in the opposite order that they are retrieved,
    120      * e.g. Acquiring vars1, then acquiring vars2, vars2 MUST be released
    121      * first otherwise an exception will be thrown.
    122      */
    123     public void release() {
    124         if (!isUsed) {
    125             throw new IllegalStateException("This instance of TempVars was already released!");
    126         }
    127 
    128         isUsed = false;
    129 
    130         TempVarsStack stack = varsLocal.get();
    131 
    132         // Return it to the stack
    133         stack.index--;
    134 
    135         // Check if it is actually there
    136         if (stack.tempVars[stack.index] != this) {
    137             throw new IllegalStateException("An instance of TempVars has not been released in a called method!");
    138         }
    139     }
    140     /**
    141      * For interfacing with OpenGL in Renderer.
    142      */
    143     public final IntBuffer intBuffer1 = BufferUtils.createIntBuffer(1);
    144     public final IntBuffer intBuffer16 = BufferUtils.createIntBuffer(16);
    145     public final FloatBuffer floatBuffer16 = BufferUtils.createFloatBuffer(16);
    146     /**
    147      * Skinning buffers
    148      */
    149     public final float[] skinPositions = new float[512 * 3];
    150     public final float[] skinNormals = new float[512 * 3];
    151      //tangent buffer as 4 components by elements
    152     public final float[] skinTangents = new float[512 * 4];
    153     /**
    154      * Fetching triangle from mesh
    155      */
    156     public final Triangle triangle = new Triangle();
    157     /**
    158      * Color
    159      */
    160     public final ColorRGBA color = new ColorRGBA();
    161     /**
    162      * General vectors.
    163      */
    164     public final Vector3f vect1 = new Vector3f();
    165     public final Vector3f vect2 = new Vector3f();
    166     public final Vector3f vect3 = new Vector3f();
    167     public final Vector3f vect4 = new Vector3f();
    168     public final Vector3f vect5 = new Vector3f();
    169     public final Vector3f vect6 = new Vector3f();
    170     public final Vector3f vect7 = new Vector3f();
    171     //seems the maximum number of vector used is 7 in com.jme3.bounding.java
    172     public final Vector3f vect8 = new Vector3f();
    173     public final Vector3f vect9 = new Vector3f();
    174     public final Vector3f vect10 = new Vector3f();
    175     public final Vector4f vect4f = new Vector4f();
    176     public final Vector3f[] tri = {new Vector3f(),
    177         new Vector3f(),
    178         new Vector3f()};
    179     /**
    180      * 2D vector
    181      */
    182     public final Vector2f vect2d = new Vector2f();
    183     public final Vector2f vect2d2 = new Vector2f();
    184     /**
    185      * General matrices.
    186      */
    187     public final Matrix3f tempMat3 = new Matrix3f();
    188     public final Matrix4f tempMat4 = new Matrix4f();
    189     public final Matrix4f tempMat42 = new Matrix4f();
    190     /**
    191      * General quaternions.
    192      */
    193     public final Quaternion quat1 = new Quaternion();
    194     public final Quaternion quat2 = new Quaternion();
    195     /**
    196      * Eigen
    197      */
    198     public final Eigen3f eigen = new Eigen3f();
    199     /**
    200      * Plane
    201      */
    202     public final Plane plane = new Plane();
    203     /**
    204      * BoundingBox ray collision
    205      */
    206     public final float[] fWdU = new float[3];
    207     public final float[] fAWdU = new float[3];
    208     public final float[] fDdU = new float[3];
    209     public final float[] fADdU = new float[3];
    210     public final float[] fAWxDdU = new float[3];
    211     /**
    212      * Maximum tree depth .. 32 levels??
    213      */
    214     public final Spatial[] spatialStack = new Spatial[32];
    215     public final float[] matrixWrite = new float[16];
    216     /**
    217      * BIHTree
    218      */
    219     public final float[] bihSwapTmp = new float[9];
    220     public final ArrayList<BIHStackData> bihStack = new ArrayList<BIHStackData>();
    221 }
    222