Java Code Examples for java.nio.FloatBuffer#remaining()

The following examples show how to use java.nio.FloatBuffer#remaining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferUtils.java    From Ultraino with MIT License 6 votes vote down vote up
/**
 * Ensures there is at least the <code>required</code> number of entries
 * left after the current position of the buffer. If the buffer is too small
 * a larger one is created and the old one copied to the new buffer.
 *
 * @param buffer
 *            buffer that should be checked/copied (may be null)
 * @param required
 *            minimum number of elements that should be remaining in the
 *            returned buffer
 * @return a buffer large enough to receive at least the
 *         <code>required</code> number of entries, same position as the
 *         input buffer, not null
 */
public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, int required) {
    if (buffer != null) {
        buffer.limit(buffer.capacity());
    }
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        FloatBuffer newVerts = createFloatBuffer(position + required);
        if (buffer != null) {
            buffer.flip();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 2
Source File: ArrayUtils.java    From pixymeta-android with Eclipse Public License 1.0 6 votes vote down vote up
public static float[] toFloatArray(byte[] data, int offset, int len, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, len);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
	
	FloatBuffer floatBuf = byteBuffer.asFloatBuffer();
	float[] array = new float[floatBuf.remaining()];
	floatBuf.get(array);		
	
	return array;
}
 
Example 3
Source File: ArrayUtils.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
public static float[] toFloatArray(byte[] data, int offset, int len, boolean bigEndian) {
	
	ByteBuffer byteBuffer = ByteBuffer.wrap(data, offset, len);
	
	if (bigEndian) {
		byteBuffer.order(ByteOrder.BIG_ENDIAN);
	} else {
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
	}
	
	FloatBuffer floatBuf = byteBuffer.asFloatBuffer();
	float[] array = new float[floatBuf.remaining()];
	floatBuf.get(array);		
	
	return array;
}
 
Example 4
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Ensures there is at least the <code>required</code> number of entries left after the current position of the buffer. If the buffer is too small a larger one is created and the old one copied to
 * the new buffer.
 *
 * @param buffer
 *            buffer that should be checked/copied (may be null)
 * @param required
 *            minimum number of elements that should be remaining in the returned buffer
 * @return a buffer large enough to receive at least * * the <code>required</code> number of entries, same position as the input buffer, not null
 */
public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, int required) {
	if (buffer == null || (buffer.remaining() < required)) {
		int position = (buffer != null ? buffer.position() : 0);
		FloatBuffer newVerts = createFloatBuffer(position + required);
		if (buffer != null) {
			buffer.rewind();
			newVerts.put(buffer);
			newVerts.position(position);
		}
		buffer = newVerts;
	}
	return buffer;
}
 
Example 5
Source File: NDArray.java    From djl with Apache License 2.0 5 votes vote down vote up
/**
 * Converts this {@code NDArray} to a float array.
 *
 * @return a float array
 * @throws IllegalStateException when {@link DataType} of this {@code NDArray} mismatches
 */
default float[] toFloatArray() {
    if (getDataType() != DataType.FLOAT32) {
        throw new IllegalStateException(
                "DataType mismatch, Required float, Actual " + getDataType());
    }
    FloatBuffer fb = toByteBuffer().asFloatBuffer();
    float[] ret = new float[fb.remaining()];
    fb.get(ret);
    return ret;
}
 
Example 6
Source File: Convolution.java    From JOML with MIT License 5 votes vote down vote up
/**
 * Generate a Gaussian convolution kernel with the given number of rows and columns, and store
 * the factors in row-major order in <code>dest</code>.
 *
 * @param rows
 *          the number of rows (must be an odd number)
 * @param cols
 *          the number of columns (must be an odd number)
 * @param sigma
 *          the standard deviation of the filter kernel values
 * @param dest
 *          will hold the kernel factors in row-major order
 */
public static void gaussianKernel(int rows, int cols, float sigma, FloatBuffer dest) {
    if ((rows & 1) == 0) {
        throw new IllegalArgumentException("rows must be an odd number");
    }
    if ((cols & 1) == 0) {
        throw new IllegalArgumentException("cols must be an odd number");
    }
    if (dest == null) {
        throw new IllegalArgumentException("dest must not be null");
    }
    if (dest.remaining() < rows * cols) {
        throw new IllegalArgumentException("dest must have at least " + (rows * cols) + " remaining values");
    }
    float sum = 0.0f;
    int pos = dest.position();
    for (int i = 0, y = -(rows - 1) / 2; y <= (rows - 1) / 2; y++) {
        for (int x = -(cols - 1) / 2; x <= (cols - 1) / 2; x++, i++) {
            float k = (float) Math.exp(-(y * y + x * x) / (2.0 * sigma * sigma));
            dest.put(pos + i, k);
            sum += k;
        }
    }
    for (int i = 0; i < rows * cols; i++) {
        dest.put(pos + i, dest.get(pos + i) / sum);
    }
}
 
Example 7
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void gatherVertexData(Mesh mesh, List<Vertex> vertexLookup) {

        //in case the model is currently animating with software animation
        //attempting to retrieve the bind position instead of the position.
        VertexBuffer position = mesh.getBuffer(VertexBuffer.Type.BindPosePosition);
        if (position == null) {
            position = mesh.getBuffer(VertexBuffer.Type.Position);
        }
        FloatBuffer pos = (FloatBuffer) position.getDataReadOnly();
        pos.rewind();
        
        while (pos.remaining() != 0) {
            Vertex v = new Vertex();
            v.position.setX(pos.get());
            v.position.setY(pos.get());
            v.position.setZ(pos.get());
            v.isSeam = false;
            Vertex existingV = findSimilar(v);
            if (existingV != null) {
                //vertex position already exists
                existingV.isSeam = true;
                v.isSeam = true;
            } else {
                vertexList.add(v);
            }
            vertexLookup.add(v);
        }
        pos.rewind();
    }
 
Example 8
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Ensures there is at least the <code>required</code> number of entries left after the current position of the
 * buffer. If the buffer is too small a larger one is created and the old one copied to the new buffer.
 * @param buffer buffer that should be checked/copied (may be null)
 * @param required minimum number of elements that should be remaining in the returned buffer
 * @return a buffer large enough to receive at least the <code>required</code> number of entries, same position as
 * the input buffer, not null
 */
public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, int required) {
    if (buffer == null || (buffer.remaining() < required)) {
        int position = (buffer != null ? buffer.position() : 0);
        FloatBuffer newVerts = createFloatBuffer(position + required);
        if (buffer != null) {
            buffer.rewind();
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}
 
Example 9
Source File: LODGeomap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
        if (!isLoaded()) {
            throw new NullPointerException();
        }

        if (tangentStore != null) {
            if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        tangentStore.rewind();

        if (binormalStore != null) {
            if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        binormalStore.rewind();

        Vector3f normal = new Vector3f();
        Vector3f tangent = new Vector3f();
        Vector3f binormal = new Vector3f();
        /*Vector3f v1 = new Vector3f();
        Vector3f v2 = new Vector3f();
        Vector3f v3 = new Vector3f();
        Vector2f t1 = new Vector2f();
        Vector2f t2 = new Vector2f();
        Vector2f t3 = new Vector2f();*/

        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {
                
                int idx = (r * getWidth() + c) * 3;
                normal.set(normalBuffer.get(idx), normalBuffer.get(idx+1), normalBuffer.get(idx+2));
                tangent.set(normal.cross(new Vector3f(0,0,1)));
                binormal.set(new Vector3f(1,0,0).cross(normal));
                
                BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }

/*        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {

                int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
                int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
                int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end

                v1.set(c, getValue(c, r), r);
                t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));

                // below
                if (r == getHeight()-1) { // last row
                    v3.set(c, getValue(c, r), r + 1);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
                    v += textureBuffer.get(texIdx + 1);
                    t3.set(u, v);
                } else {
                    v3.set(c, getValue(c, r + 1), r + 1);
                    t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
                }
                
                //right
                if (c == getWidth()-1) { // last column
                    v2.set(c + 1, getValue(c, r), r);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
                    v += textureBuffer.get(texIdx - 1);
                    t2.set(u, v);
                } else {
                    v2.set(c + 1, getValue(c + 1, r), r); // one to the right
                    t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
                }

                calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
                BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }
        */
        return new FloatBuffer[]{tangentStore, binormalStore};
    }
 
Example 10
Source File: NADCON.java    From sis with Apache License 2.0 4 votes vote down vote up
/**
 * Loads latitude or longitude shifts data. This method should be invoked twice:
 *
 * <ol>
 *   <li>On an instance created for the latitude shifts file with a {@code latitude} argument set to null.</li>
 *   <li>On an instance created for the longitude shifts file with a {@code latitude} argument set to the
 *       instance created in the previous step.</li>
 * </ol>
 *
 * The result is stored in the {@link #grid} field.
 *
 * @param fb               a {@code FloatBuffer} view over the full {@link #buffer} range.
 * @param latitudeShifts   the previously loaded latitude shifts, or {@code null} if not yet loaded.
 * @param longitudeShifts  the file for the longitude grid.
 */
final void readGrid(final FloatBuffer fb, final Loader latitudeShifts, final Path longitudeShifts)
        throws IOException, FactoryException, NoninvertibleTransformException
{
    final int dim;
    final double scale;
    if (latitudeShifts == null) {
        dim   = 1;                                              // Dimension of latitudes.
        scale = DEGREES_TO_SECONDS * Δy;                        // NADCON shifts are positive north.
        grid  = new DatumShiftGridFile.Float<>(2, Units.DEGREE, Units.DEGREE,
                true, x0, y0, Δx, Δy, nx, ny, PARAMETERS, file, longitudeShifts);
        grid.accuracy = SECOND_PRECISION / DEGREES_TO_SECONDS;
    } else {
        if (x0 != latitudeShifts.x0 || Δx != latitudeShifts.Δx || nx != latitudeShifts.nx ||
            y0 != latitudeShifts.y0 || Δy != latitudeShifts.Δy || ny != latitudeShifts.ny || nz != latitudeShifts.nz)
        {
            throw new FactoryException(Errors.format(Errors.Keys.MismatchedGridGeometry_2,
                    latitudeShifts.file.getFileName(), file.getFileName()));
        }
        dim   = 0;                                              // Dimension of longitudes
        scale = -DEGREES_TO_SECONDS * Δx;                       // NADCON shifts are positive west.
        grid  = latitudeShifts.grid;                            // Continue writing in existing grid.
    }
    final float[] array = grid.offsets[dim];
    if (ascii != null) {
        for (int i=0; i<array.length; i++) {
            array[i] = (float) (Double.parseDouble(nextWord()) / scale);
        }
    } else {
        /*
         * Transfer all data from the FloatBuffer to the float[] array, except one float at the beginning
         * of every row which must be skipped. That skipped float value is not a translation value and is
         * expected to be always zero.
         */
        syncView(fb);
        int forCurrentRow = 0;
        for (int i=0; i<array.length;) {
            if (forCurrentRow == 0) {
                if (!fb.hasRemaining()) {
                    fillBuffer(fb);
                }
                if (fb.get() != 0) {
                    throw unexpectedFormat();
                }
                forCurrentRow = nx;
            }
            int remaining = fb.remaining();
            if (remaining == 0) {
                fillBuffer(fb);
                remaining = fb.remaining();
            }
            final int n = Math.min(forCurrentRow, remaining);
            fb.get(array, i, n);
            forCurrentRow -= n;
            i += n;
        }
        /*
         * Convert seconds to degrees for consistency with the unit declared at the beginning of this method,
         * then divide by cell size for consistency with the 'isCellRatio = true' configuration.
         */
        for (int i=0; i<array.length; i++) {
            array[i] /= scale;
        }
    }
}
 
Example 11
Source File: Line.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void orthogonalLineFit(FloatBuffer points) {
    if (points == null) {
        return;
    }

    TempVars vars = TempVars.get();

    Vector3f compVec1 = vars.vect1;
    Vector3f compVec2 = vars.vect2;
    Matrix3f compMat1 = vars.tempMat3;
    Eigen3f compEigen1 = vars.eigen;

    points.rewind();

    // compute average of points
    int length = points.remaining() / 3;

    BufferUtils.populateFromBuffer(origin, points, 0);
    for (int i = 1; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        origin.addLocal(compVec1);
    }

    origin.multLocal(1f / (float) length);

    // compute sums of products
    float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
    float sumYY = 0.0f, sumYZ = 0.0f, sumZZ = 0.0f;

    points.rewind();
    for (int i = 0; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        compVec1.subtract(origin, compVec2);
        sumXX += compVec2.x * compVec2.x;
        sumXY += compVec2.x * compVec2.y;
        sumXZ += compVec2.x * compVec2.z;
        sumYY += compVec2.y * compVec2.y;
        sumYZ += compVec2.y * compVec2.z;
        sumZZ += compVec2.z * compVec2.z;
    }

    //find the smallest eigen vector for the direction vector
    compMat1.m00 = sumYY + sumZZ;
    compMat1.m01 = -sumXY;
    compMat1.m02 = -sumXZ;
    compMat1.m10 = -sumXY;
    compMat1.m11 = sumXX + sumZZ;
    compMat1.m12 = -sumYZ;
    compMat1.m20 = -sumXZ;
    compMat1.m21 = -sumYZ;
    compMat1.m22 = sumXX + sumYY;

    compEigen1.calculateEigen(compMat1);
    direction = compEigen1.getEigenVector(0);

    vars.release();
}
 
Example 12
Source File: GeoMap.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
     * Creates a normal array from the normal data in this Geomap
     *
     * @param store A preallocated FloatBuffer where to store the data (optional), size must be >= getWidth()*getHeight()*3
     * @returns store, or a new FloatBuffer if store is null
     *
     * @throws NullPointerException If isLoaded() or hasNormalmap() is false
     */
    public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
        
        if (store!=null){
            if (store.remaining() < getWidth()*getHeight()*3)
                throw new BufferUnderflowException();
        }else{
            store = BufferUtils.createFloatBuffer(getWidth()*getHeight()*3);
        }
        store.rewind();

        Vector3f oppositePoint = new Vector3f();
        Vector3f adjacentPoint = new Vector3f();
        Vector3f rootPoint = new Vector3f();
        Vector3f tempNorm = new Vector3f();
        int normalIndex = 0;

        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                rootPoint.set(x, getValue(x,y), y);
                if (y == getHeight() - 1) {
                    if (x == getWidth() - 1) {  // case #4 : last row, last col
                        // left cross up
//                            adj = normalIndex - getWidth();
//                            opp = normalIndex - 1;
                        adjacentPoint.set(x, getValue(x,y-1), y-1);
                        oppositePoint.set(x-1, getValue(x-1, y), y);
                    } else {                    // case #3 : last row, except for last col
                        // right cross up
//                            adj = normalIndex + 1;
//                            opp = normalIndex - getWidth();
                        adjacentPoint.set(x+1, getValue(x+1,y), y);
                        oppositePoint.set(x, getValue(x,y-1), y-1);
                    }
                } else {
                    if (x == getWidth() - 1) {  // case #2 : last column except for last row
                        // left cross down
                        adjacentPoint.set(x-1, getValue(x-1,y), y);
                        oppositePoint.set(x, getValue(x,y+1), y+1);
//                            adj = normalIndex - 1;
//                            opp = normalIndex + getWidth();
                    } else {                    // case #1 : most cases
                        // right cross down
                        adjacentPoint.set(x, getValue(x,y+1), y+1);
                        oppositePoint.set(x+1, getValue(x+1,y), y);
//                            adj = normalIndex + getWidth();
//                            opp = normalIndex + 1;
                    }
                }



                tempNorm.set(adjacentPoint).subtractLocal(rootPoint)
                        .crossLocal(oppositePoint.subtractLocal(rootPoint));
                tempNorm.multLocal(scale).normalizeLocal();
//                    store.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
                BufferUtils.setInBuffer(tempNorm, store,
                        normalIndex);
                normalIndex++;
            }
        }

        return store;
    }
 
Example 13
Source File: BoundingBox.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * <code>containAABB</code> creates a minimum-volume axis-aligned bounding
 * box of the points, then selects the smallest enclosing sphere of the box
 * with the sphere centered at the boxes center.
 * 
 * @param points
 *            the list of points.
 */
public void containAABB(FloatBuffer points) {
    if (points == null) {
        return;
    }

    points.rewind();
    if (points.remaining() <= 2) // we need at least a 3 float vector
    {
        return;
    }

    TempVars vars = TempVars.get();
    
    float[] tmpArray = vars.skinPositions;

    float minX = Float.POSITIVE_INFINITY, minY = Float.POSITIVE_INFINITY, minZ = Float.POSITIVE_INFINITY;
    float maxX = Float.NEGATIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY, maxZ = Float.NEGATIVE_INFINITY;
    
    int iterations = (int) FastMath.ceil(points.limit() / ((float) tmpArray.length));
    for (int i = iterations - 1; i >= 0; i--) {
        int bufLength = Math.min(tmpArray.length, points.remaining());
        points.get(tmpArray, 0, bufLength);

        for (int j = 0; j < bufLength; j += 3) {
            vars.vect1.x = tmpArray[j];
            vars.vect1.y = tmpArray[j+1];
            vars.vect1.z = tmpArray[j+2];
            
            if (vars.vect1.x < minX) {
                minX = vars.vect1.x;
            }
            if (vars.vect1.x > maxX) {
                maxX = vars.vect1.x;
            }

            if (vars.vect1.y < minY) {
                minY = vars.vect1.y;
            }
            if (vars.vect1.y > maxY) {
                maxY = vars.vect1.y;
            }

            if (vars.vect1.z < minZ) {
                minZ = vars.vect1.z;
            }
            if (vars.vect1.z > maxZ) {
                maxZ = vars.vect1.z;
            }
        }
    }

    vars.release();

    center.set(minX + maxX, minY + maxY, minZ + maxZ);
    center.multLocal(0.5f);

    xExtent = maxX - center.x;
    yExtent = maxY - center.y;
    zExtent = maxZ - center.z;
}
 
Example 14
Source File: Line.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Fit this line to the specified points.
 *
 * @param points a buffer containing location vectors, or null
 */
public void orthogonalLineFit(FloatBuffer points) {
    if (points == null) {
        return;
    }

    TempVars vars = TempVars.get();

    Vector3f compVec1 = vars.vect1;
    Vector3f compVec2 = vars.vect2;
    Matrix3f compMat1 = vars.tempMat3;
    Eigen3f compEigen1 = vars.eigen;

    points.rewind();

    // compute average of points
    int length = points.remaining() / 3;

    BufferUtils.populateFromBuffer(origin, points, 0);
    for (int i = 1; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        origin.addLocal(compVec1);
    }

    origin.multLocal(1f / length);

    // compute sums of products
    float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
    float sumYY = 0.0f, sumYZ = 0.0f, sumZZ = 0.0f;

    points.rewind();
    for (int i = 0; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        compVec1.subtract(origin, compVec2);
        sumXX += compVec2.x * compVec2.x;
        sumXY += compVec2.x * compVec2.y;
        sumXZ += compVec2.x * compVec2.z;
        sumYY += compVec2.y * compVec2.y;
        sumYZ += compVec2.y * compVec2.z;
        sumZZ += compVec2.z * compVec2.z;
    }

    //find the smallest eigen vector for the direction vector
    compMat1.m00 = sumYY + sumZZ;
    compMat1.m01 = -sumXY;
    compMat1.m02 = -sumXZ;
    compMat1.m10 = -sumXY;
    compMat1.m11 = sumXX + sumZZ;
    compMat1.m12 = -sumYZ;
    compMat1.m20 = -sumXZ;
    compMat1.m21 = -sumYZ;
    compMat1.m22 = sumXX + sumYY;

    compEigen1.calculateEigen(compMat1);
    direction = compEigen1.getEigenVector(0);

    vars.release();
}
 
Example 15
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public FloatBuffer writeNormalArray(FloatBuffer store, Vector3f scale) {
    if (!isLoaded()) {
        throw new NullPointerException();
    }

    if (store != null) {
        if (store.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    store.rewind();

    TempVars vars = TempVars.get();
    
    Vector3f rootPoint = vars.vect1;
    Vector3f rightPoint = vars.vect2;
    Vector3f leftPoint = vars.vect3;
    Vector3f topPoint = vars.vect4;
    Vector3f bottomPoint = vars.vect5;
    
    Vector3f tmp1 = vars.vect6;

    // calculate normals for each polygon
    for (int r = 0; r < getHeight(); r++) {
        for (int c = 0; c < getWidth(); c++) {

            rootPoint.set(0, getValue(c, r), 0);
            Vector3f normal = vars.vect8;

            if (r == 0) { // first row
                if (c == 0) { // first column
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(bottomPoint, rootPoint, rightPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    getNormal(leftPoint, rootPoint, bottomPoint, scale, normal);
                } else { // all middle columns
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                }
            } else if (r == getHeight() - 1) { // last row
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    getNormal(rightPoint, rootPoint, topPoint, scale, normal);
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    getNormal(topPoint, rootPoint, leftPoint, scale, normal);
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    
                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            } else { // all middle rows
                if (c == 0) { // first column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                } else if (c == getWidth() - 1) { // last column
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);

                    normal.set( getNormal(topPoint, rootPoint, leftPoint, scale, tmp1) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                } else { // all middle columns
                    topPoint.set(0, getValue(c, r - 1), -1);
                    leftPoint.set(-1, getValue(c - 1, r), 0);
                    rightPoint.set(1, getValue(c + 1, r), 0);
                    bottomPoint.set(0, getValue(c, r + 1), 1);
                    
                    normal.set( getNormal(topPoint,  rootPoint, leftPoint, scale, tmp1 ) );
                    normal.addLocal( getNormal(leftPoint, rootPoint, bottomPoint, scale, tmp1) );
                    normal.addLocal( getNormal(bottomPoint, rootPoint, rightPoint, scale, tmp1) );
                    normal.addLocal( getNormal(rightPoint, rootPoint, topPoint, scale, tmp1) );
                }
            }
            normal.normalizeLocal();
            BufferUtils.setInBuffer(normal, store, (r * getWidth() + c)); // save the normal
        }
    }
    vars.release();
    
    return store;
}
 
Example 16
Source File: LODGeomap.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
        if (!isLoaded()) {
            throw new NullPointerException();
        }

        if (tangentStore != null) {
            if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        tangentStore.rewind();

        if (binormalStore != null) {
            if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
                throw new BufferUnderflowException();
            }
        } else {
            binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
        }
        binormalStore.rewind();

        Vector3f normal = new Vector3f();
        Vector3f tangent = new Vector3f();
        Vector3f binormal = new Vector3f();
        /*Vector3f v1 = new Vector3f();
        Vector3f v2 = new Vector3f();
        Vector3f v3 = new Vector3f();
        Vector2f t1 = new Vector2f();
        Vector2f t2 = new Vector2f();
        Vector2f t3 = new Vector2f();*/

        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {
                
                int idx = (r * getWidth() + c) * 3;
                normal.set(normalBuffer.get(idx), normalBuffer.get(idx+1), normalBuffer.get(idx+2));
                tangent.set(normal.cross(new Vector3f(0,0,1)));
                binormal.set(new Vector3f(1,0,0).cross(normal));
                
                BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }

/*        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {

                int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
                int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
                int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end

                v1.set(c, getValue(c, r), r);
                t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));

                // below
                if (r == getHeight()-1) { // last row
                    v3.set(c, getValue(c, r), r + 1);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
                    v += textureBuffer.get(texIdx + 1);
                    t3.set(u, v);
                } else {
                    v3.set(c, getValue(c, r + 1), r + 1);
                    t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
                }
                
                //right
                if (c == getWidth()-1) { // last column
                    v2.set(c + 1, getValue(c, r), r);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
                    v += textureBuffer.get(texIdx - 1);
                    t2.set(u, v);
                } else {
                    v2.set(c + 1, getValue(c + 1, r), r); // one to the right
                    t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
                }

                calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
                BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }
        */
        return new FloatBuffer[]{tangentStore, binormalStore};
    }
 
Example 17
Source File: FloatVBO.java    From tribaltrouble with GNU General Public License v2.0 4 votes vote down vote up
public FloatVBO(int usage, FloatBuffer initial_data) {
	this(usage, initial_data.remaining());
	put(initial_data);
}
 
Example 18
Source File: Utils.java    From RobotCA with GNU General Public License v3.0 4 votes vote down vote up
private static int countVertices(FloatBuffer vertices, int size) {
    Preconditions.checkArgument(vertices.remaining() % size == 0, "Number of vertices: " + vertices.remaining());
    return vertices.remaining() / size;
}
 
Example 19
Source File: BoundingBox.java    From Ultraino with MIT License 4 votes vote down vote up
/**
 * <code>containAABB</code> creates a minimum-volume axis-aligned bounding
 * box of the points, then selects the smallest enclosing sphere of the box
 * with the sphere centered at the boxes center.
 * 
 * @param points
 *            the list of points.
 */
public void containAABB(FloatBuffer points) {
    if (points == null) {
        return;
    }

    points.rewind();
    if (points.remaining() <= 2) // we need at least a 3 float vector
    {
        return;
    }

    TempVars vars = TempVars.get();
    
    float[] tmpArray = vars.skinPositions;

    float minX = Float.POSITIVE_INFINITY, minY = Float.POSITIVE_INFINITY, minZ = Float.POSITIVE_INFINITY;
    float maxX = Float.NEGATIVE_INFINITY, maxY = Float.NEGATIVE_INFINITY, maxZ = Float.NEGATIVE_INFINITY;
    
    int iterations = (int) M.ceil(points.limit() / ((float) tmpArray.length));
    for (int i = iterations - 1; i >= 0; i--) {
        int bufLength = Math.min(tmpArray.length, points.remaining());
        points.get(tmpArray, 0, bufLength);

        for (int j = 0; j < bufLength; j += 3) {
            vars.vect1.x = tmpArray[j];
            vars.vect1.y = tmpArray[j+1];
            vars.vect1.z = tmpArray[j+2];
            
            if (vars.vect1.x < minX) {
                minX = vars.vect1.x;
            }
            if (vars.vect1.x > maxX) {
                maxX = vars.vect1.x;
            }

            if (vars.vect1.y < minY) {
                minY = vars.vect1.y;
            }
            if (vars.vect1.y > maxY) {
                maxY = vars.vect1.y;
            }

            if (vars.vect1.z < minZ) {
                minZ = vars.vect1.z;
            }
            if (vars.vect1.z > maxZ) {
                maxZ = vars.vect1.z;
            }
        }
    }

    vars.release();

    center.set(minX + maxX, minY + maxY, minZ + maxZ);
    center.multLocal(0.5f);

    xExtent = maxX - center.x;
    yExtent = maxY - center.y;
    zExtent = maxZ - center.z;
}
 
Example 20
Source File: BoundingBox.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
/**
 * <code>containAABB</code> creates a minimum-volume axis-aligned bounding box of the points, then selects the smallest enclosing sphere of the box with the sphere centered at the boxes center.
 *
 * @param points
 *            the list of points.
 */
public void containAABB(FloatBuffer points) {
	if (points == null) {
		return;
	}

	points.rewind();
	if (points.remaining() <= 2) // we need at least a 3 float vector
	{
		return;
	}

	Vector3f vect1 = Vector3f.newInstance();
	BufferUtils.populateFromBuffer(vect1, points, 0);
	float minX = vect1.x, minY = vect1.y, minZ = vect1.z;
	float maxX = vect1.x, maxY = vect1.y, maxZ = vect1.z;

	for (int i = 1, len = points.remaining() / 3; i < len; i++) {
		BufferUtils.populateFromBuffer(vect1, points, i);
		if (vect1.x < minX) {
			minX = vect1.x;
		}
		else if (vect1.x > maxX) {
			maxX = vect1.x;
		}

		if (vect1.y < minY) {
			minY = vect1.y;
		}
		else if (vect1.y > maxY) {
			maxY = vect1.y;
		}

		if (vect1.z < minZ) {
			minZ = vect1.z;
		}
		else if (vect1.z > maxZ) {
			maxZ = vect1.z;
		}
	}
	Vector3f.recycle(vect1);

	center.set(minX + maxX, minY + maxY, minZ + maxZ);
	center.multLocal(0.5f);

	xExtent = maxX - center.x;
	yExtent = maxY - center.y;
	zExtent = maxZ - center.z;
}