NASA World Wind

gov.nasa.worldwind.util
Class VecBuffer

java.lang.Object
  extended by gov.nasa.worldwind.util.VecBuffer

public class VecBuffer
extends java.lang.Object

VecBuffer provides an logical interface on BufferWrapper to interpret its contents as a series of vector tuples (rather than individual primitive types). The number of coordinates in each logical vector is specified by the property coordsPerElem. For example, if a VecBuffer is composed of (x, y, z) tuples then coordsPerElem would be 3.


Nested Class Summary
protected  class VecBuffer.BasicIterator<T>
           
protected  class VecBuffer.CoordAccessor
           
protected static interface VecBuffer.ElementAccessor<T>
           
protected  class VecBuffer.LocationAccessor
           
protected  class VecBuffer.PositionAccessor
           
protected  class VecBuffer.ReverseIterator<T>
           
protected  class VecBuffer.VectorAccessor
           
 
Field Summary
protected  BufferWrapper buffer
           
protected  int coordsPerVec
           
 
Constructor Summary
VecBuffer(int coordsPerVec, BufferWrapper buffer)
          Constructs a new VecBuffer with the specified vector size, and backing BufferWrapper.
VecBuffer(int coordsPerVec, int size, BufferFactory factory)
          Constructs a new VecBuffer with the specified vector size, number of vectors, and buffer factory.
 
Method Summary
 void bindAsColorBuffer(DrawContext dc)
          Binds this buffer as the source of color values to use when rendering OpenGL primitives.
 void bindAsNormalBuffer(DrawContext dc)
          Binds this buffer as the source of normal coordinates to use when rendering OpenGL primitives.
 void bindAsTexCoordBuffer(DrawContext dc)
          Binds this buffer as the source of texture coordinates to use when rendering OpenGL primitives.
 void bindAsVertexBuffer(DrawContext dc)
          Binds this buffer as the source of vertex coordinates to use when rendering OpenGL primitives.
 void drawArrays(DrawContext dc, int drawMode)
          Renders getSize() elements from the currently bounds OpenGL coordinate buffers, beginning with element 0.
 double[] get(int position, double[] array)
          Returns the vector element at the specified position.
 BufferWrapper getBufferWrapper()
          Returns the backing BufferWrapper.
 java.lang.Iterable<double[]> getCoords()
          Returns an iterator over this buffer's logical vectors, as double[] coordinate arrays.
 java.lang.Iterable<double[]> getCoords(int minCoordsPerVec)
          Returns an iterator over this buffer's logical vectors, as double[] coordinate arrays.
 int getCoordsPerVec()
          Returns the number of coordinates per logical vector element.
 float[] getFloat(int position, float[] array)
          Returns the vector element at the specified position.
 LatLon getLocation(int position)
          Returns the vector element at the specified position, as a geographic LatLon.
 java.lang.Iterable<LatLon> getLocations()
          Returns an iterator over this buffer's logical vectors, as LatLon locations.
 Position getPosition(int position)
          Returns the vector element at the specified position, as a geographic Position.
 java.lang.Iterable<Position> getPositions()
          Returns an iterator over this buffer's logical vectors, as geographic Positions.
 java.lang.Iterable<double[]> getReverseCoords(int minCoordsPerVec)
          Returns a reverse iterator over this buffer's logical vectors, as double[] coordinate arrays.
 java.lang.Iterable<LatLon> getReverseLocations()
          Returns a reverse iterator over this buffer's logical vectors, as LatLon locations.
 java.lang.Iterable<Position> getReversePositions()
          Returns a reverse iterator over this buffer's logical vectors, as geographic Positions.
 java.lang.Iterable<Vec4> getReverseVectors()
          Returns a reverse iterator over this buffer's logical vectors, as Vec4 references.
 int getSize()
          Returns the number of logical vector elements contained in the VecBuffer.
 VecBuffer getSubBuffer(int position, int size)
          Returns a new VecBuffer which is a subsequence of this buffer.
 Vec4 getVector(int position)
          Returns the vector element at the specified position, as a Vec4.
 java.lang.Iterable<Vec4> getVectors()
          Returns an iterator over this buffer's logical vectors, as Vec4 references.
protected  int indexFromVectorPosition(int position)
          Maps the logical vector position to a physical buffer index.
 void put(int position, double[] array)
          Sets the vector element at the specified position.
 void putAll(int position, double[] array, int count)
          Sets the vector elements starting at the specified position, and ending at the specified position + count.
 void putCoords(int position, java.lang.Iterable<double[]> iterable)
          Sets a subsequence of this buffer with the contents of the specified Iterable.
 void putFloat(int position, float[] array)
          Sets the vector element at the specified position.
 void putLocation(int position, LatLon ll)
          Sets the vector element at the specified position, as a geographic LatLon.
 void putLocations(int position, java.lang.Iterable<? extends LatLon> iterable)
          Sets a subsequence of this buffer with the contents of the specified Iterable.
 void putPosition(int position, Position p)
          Sets the vector element at the specified position, as a geographic Position.
 void putPositions(int position, java.lang.Iterable<? extends Position> iterable)
          Sets a subsequence of this buffer with the contents of the specified Iterable.
 void putSubBuffer(int position, VecBuffer buffer)
          Sets a subsequence of this buffer with the contents of the specified buffer.
 void putVector(int position, Vec4 vec)
          Sets the vector element at the specified position, as a Vec4.
 void putVectors(int position, java.lang.Iterable<? extends Vec4> iterable)
          Sets a subsequence of this buffer with the contents of the specified Iterable.
protected  int vectorPositionFromIndex(int index)
          Maps the physical buffer index to a logical vector position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected BufferWrapper buffer

coordsPerVec

protected int coordsPerVec
Constructor Detail

VecBuffer

public VecBuffer(int coordsPerVec,
                 BufferWrapper buffer)
Constructs a new VecBuffer with the specified vector size, and backing BufferWrapper.

Parameters:
coordsPerVec - the number of coordinates per logical vector.
buffer - the backing BufferWrapper.
Throws:
java.lang.IllegalArgumentException - if coordsPerElem is 0 or negative, or if the buffer is null.

VecBuffer

public VecBuffer(int coordsPerVec,
                 int size,
                 BufferFactory factory)
Constructs a new VecBuffer with the specified vector size, number of vectors, and buffer factory.

Parameters:
coordsPerVec - the number of coordinates per logical vector.
size - the number of vectors.
factory - the factory used to a backing BufferWrapper of the appropriate size.
Throws:
java.lang.IllegalArgumentException - if coordsPerElem is 0 or negative, if size is negative, or if the factory is null.
Method Detail

bindAsColorBuffer

public void bindAsColorBuffer(DrawContext dc)
Binds this buffer as the source of color values to use when rendering OpenGL primitives. The color type is equal to buffer's underlying BufferWrapper GL type, the stride is 0, and the vertex data itself is this buffer's backing NIO Buffer. This buffer's vector size must be 3, or 4.

Parameters:
dc - the current DrawContext.
Throws:
java.lang.IllegalArgumentException - if the DrawContext is null, or if this buffer is not compatible as a color buffer.

bindAsNormalBuffer

public void bindAsNormalBuffer(DrawContext dc)
Binds this buffer as the source of normal coordinates to use when rendering OpenGL primitives. The normal type is equal to buffer's underlying BufferWrapper GL type, the stride is 0, and the vertex data itself is this buffer's backing NIO Buffer. This buffer's vector size must be 3.

Parameters:
dc - the current DrawContext.
Throws:
java.lang.IllegalArgumentException - if the DrawContext is null, or if this buffer is not compatible as a normal buffer.

bindAsTexCoordBuffer

public void bindAsTexCoordBuffer(DrawContext dc)
Binds this buffer as the source of texture coordinates to use when rendering OpenGL primitives. The texture coordinate size is equal to coordsPerVertex, the texture coordinate type is equal to buffer's underlying BufferWrapper GL type, the stride is 0, and the texture coordinate data itself is this buffer's backing NIO Buffer. This buffer's vector size must be 1, 2, 3, or 4.

Parameters:
dc - the current DrawContext.
Throws:
java.lang.IllegalArgumentException - if the DrawContext is null, or if this buffer is not compatible as a normal buffer.

bindAsVertexBuffer

public void bindAsVertexBuffer(DrawContext dc)
Binds this buffer as the source of vertex coordinates to use when rendering OpenGL primitives. The vertex size is equal to coordsPerVertex, the vertex type is equal to buffer's underlying BufferWrapper GL type, the stride is 0, and the normal data itself is this buffer's backing NIO Buffer. This buffer's vector size must be 2, 3, or 4.

Parameters:
dc - the current DrawContext.
Throws:
java.lang.IllegalArgumentException - if the DrawContext is null, or if this buffer is not compatible as a vertex buffer.

drawArrays

public void drawArrays(DrawContext dc,
                       int drawMode)
Renders getSize() elements from the currently bounds OpenGL coordinate buffers, beginning with element 0. The specified drawMode indicates which type of OpenGL primitives to render.

Parameters:
dc - the current DrawContext.
drawMode - the type of OpenGL primtives to render.
Throws:
java.lang.IllegalArgumentException - if the DrawContext is null.

get

public double[] get(int position,
                    double[] array)
Returns the vector element at the specified position. The position is a logical vector position, position n corresponds to the buffer's nth vector. If the specified array length is smaller than the logical vector size, only the specified portion of the vector element is returned.

Parameters:
position - the logical vector position.
array - the destination array.
Returns:
an array of vector elements.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if the array is null.

getBufferWrapper

public BufferWrapper getBufferWrapper()
Returns the backing BufferWrapper.

Returns:
the backing buffer.

getCoords

public java.lang.Iterable<double[]> getCoords()
Returns an iterator over this buffer's logical vectors, as double[] coordinate arrays. The array returned from each call to Iterator.next() will be newly allocated, and will have length equal to coordsPerVec.

Returns:
iterator over this buffer's vectors, as double[] arrays.

getCoords

public java.lang.Iterable<double[]> getCoords(int minCoordsPerVec)
Returns an iterator over this buffer's logical vectors, as double[] coordinate arrays. The array returned from a call to Iterator.next() will be newly allocated, and will have length equal to coordsPerVec or minCoordsPerVec, whichever is larger. If minCoordsPerVec is larger than coordsPerVec, then the elements in the returned array will after index "coordsPerVec - 1" will be undefined.

Parameters:
minCoordsPerVec - the minimum number of coordinates returned in each double[] array.
Returns:
iterator over this buffer's vectors, as double[] arrays.

getCoordsPerVec

public int getCoordsPerVec()
Returns the number of coordinates per logical vector element.

Returns:
the cardinality of a logical vector element.

getFloat

public float[] getFloat(int position,
                        float[] array)
Returns the vector element at the specified position. The position is a logical vector position, position n corresponds to the buffer's nth vector. If the specified array length is smaller than the logical vector size, only the specified portion of the vector element is returned.

Parameters:
position - the logical vector position.
array - the destination array.
Returns:
an array of vector elements.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if the array is null.

getLocation

public LatLon getLocation(int position)
Returns the vector element at the specified position, as a geographic LatLon. This buffer's logical vector size must be at least 2.

Parameters:
position - the logical vector position.
Returns:
the geographic location at the specified vector position.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if this buffer cannot store a LatLon.

getLocations

public java.lang.Iterable<LatLon> getLocations()
Returns an iterator over this buffer's logical vectors, as LatLon locations.

Returns:
iterator over this buffer's vectors, as LatLon locations.

getPosition

public Position getPosition(int position)
Returns the vector element at the specified position, as a geographic Position. This buffer's logical vector size must be at least 2.

Parameters:
position - the logical vector position.
Returns:
the geographic Position at the specified vector position.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if this buffer cannot store a Position.

getPositions

public java.lang.Iterable<Position> getPositions()
Returns an iterator over this buffer's logical vectors, as geographic Positions.

Returns:
iterator over this buffer's vectors, as geographic Positions.

getReverseCoords

public java.lang.Iterable<double[]> getReverseCoords(int minCoordsPerVec)
Returns a reverse iterator over this buffer's logical vectors, as double[] coordinate arrays. The array returned from a call to Iterator.next() will be newly allocated, and will have length equal to coordsPerVec or minCoordsPerVec, whichever is larger. If minCoordsPerVec is larger than coordsPerVec, then the elements in the returned array will after index "coordsPerVec - 1" will be undefined.

Parameters:
minCoordsPerVec - the minimum number of coordinates returned in each double[] array.
Returns:
reverse iterator over this buffer's vectors, as double[] arrays.

getReverseLocations

public java.lang.Iterable<LatLon> getReverseLocations()
Returns a reverse iterator over this buffer's logical vectors, as LatLon locations.

Returns:
reverse iterator over this buffer's vectors, as LatLon locations.

getReversePositions

public java.lang.Iterable<Position> getReversePositions()
Returns a reverse iterator over this buffer's logical vectors, as geographic Positions.

Returns:
reverse iterator over this buffer's vectors, as geographic Positions.

getReverseVectors

public java.lang.Iterable<Vec4> getReverseVectors()
Returns a reverse iterator over this buffer's logical vectors, as Vec4 references.

Returns:
reverse iterator over this buffer's vectors, as Vec4 references.

getSize

public int getSize()
Returns the number of logical vector elements contained in the VecBuffer.

Returns:
the size of this VecBuffer, in units of logical vectors.

getSubBuffer

public VecBuffer getSubBuffer(int position,
                              int size)
Returns a new VecBuffer which is a subsequence of this buffer. The new buffer starts with the vector at the specified position, and has the specified length. The two buffers share the same backing store, so changes to this buffer are reflected in the new buffer, and visa versa.

Parameters:
position - the new buffer's staring position, in logical vectors.
size - the new buffer's size, in logical vectors.
Returns:
a subsequence of this buffer.

getVector

public Vec4 getVector(int position)
Returns the vector element at the specified position, as a Vec4. This buffer's logical vector size must be either 2, 3 or 4.

Parameters:
position - the logical vector position.
Returns:
the vector at the specified vector position.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if this buffer cannot store a Vec4.

getVectors

public java.lang.Iterable<Vec4> getVectors()
Returns an iterator over this buffer's logical vectors, as Vec4 references.

Returns:
iterator over this buffer's vectors, as Vec4 references.

indexFromVectorPosition

protected int indexFromVectorPosition(int position)
Maps the logical vector position to a physical buffer index.

Parameters:
position - the vector position.
Returns:
the physical buffer index.

put

public void put(int position,
                double[] array)
Sets the vector element at the specified position. The position is a logical vector position, position n corresponds to the buffer's nth vector. If the specified array length is smaller than the logical vector size, only the specified portion of the vector element is set.

Parameters:
position - the logical vector position.
array - the source array.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if the array is null.

putAll

public void putAll(int position,
                   double[] array,
                   int count)
Sets the vector elements starting at the specified position, and ending at the specified position + count. The position is a logical vector position, position n corresponds to the buffer's nth vector. The array must have sufficient length to represent count separate logical vectors (each with size equal to coordsPerVec) tightly packed into the array, starting at index 0.

Parameters:
position - the starting logical vector position.
array - the source array.
count - the number of logical arrays to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the array is null, or if the array has insufficient length.

putCoords

public void putCoords(int position,
                      java.lang.Iterable<double[]> iterable)
Sets a subsequence of this buffer with the contents of the specified Iterable. The subsequence to set starts with the vector at the specified position, and has size equal to the number of elements in the Iterable or the number of remaining vectors in the buffer, whichever is less.

Parameters:
position - the starting vector position to set.
iterable - iterator over the elements to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if the iterable is null.

putFloat

public void putFloat(int position,
                     float[] array)
Sets the vector element at the specified position. The position is a logical vector position, position n corresponds to the buffer's nth vector. If the specified array length is smaller than the logical vector size, only the specified portion of the vector element is set.

Parameters:
position - the logical vector position.
array - the source array.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, or if the array is null.

putLocation

public void putLocation(int position,
                        LatLon ll)
Sets the vector element at the specified position, as a geographic LatLon. This buffer's logical vector size must be at least 2.

Parameters:
position - the logical vector position.
ll - the geographic location to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the LatLon is null, or if this buffer cannot store a LatLon.

putLocations

public void putLocations(int position,
                         java.lang.Iterable<? extends LatLon> iterable)
Sets a subsequence of this buffer with the contents of the specified Iterable. The subsequence to set starts with the vector at the specified position, and has size equal to the number of elements in the Iterable or the number of remaining vectors in the buffer, whichever is less. This buffer's logical vector size must be at least 2.

Parameters:
position - the starting vector position to set.
iterable - iterator over the elements to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the iterable is null, or if this buffer cannot store a LatLon.

putPosition

public void putPosition(int position,
                        Position p)
Sets the vector element at the specified position, as a geographic Position. This buffer's logical vector size must be at least 2.

Parameters:
position - the logical vector position.
p - the geographic Position to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the Position is null, or if this buffer cannot store a Position.

putPositions

public void putPositions(int position,
                         java.lang.Iterable<? extends Position> iterable)
Sets a subsequence of this buffer with the contents of the specified Iterable. The subsequence to set starts with the vector at the specified position, and has size equal to the number of elements in the Iterable or the number of remaining vectors in the buffer, whichever is less. This buffer's logical vector size must be at least 2.

Parameters:
position - the starting vector position to set.
iterable - iterator over the elements to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the iterable is null, or if this buffer cannot store a LatLon.

putSubBuffer

public void putSubBuffer(int position,
                         VecBuffer buffer)
Sets a subsequence of this buffer with the contents of the specified buffer. The subsequence to set starts with the vector at the specified position, and has size equal to the specified buffer's size. The specified buffer must have the same logical vector size as this buffer (coordsPerVec must be equivalent).

Parameters:
position - the starting vector position to set.
buffer - the input buffer.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the buffer is null or incompatible, or if this buffer has insufficient length to store the sub-buffer at the specified position.

putVector

public void putVector(int position,
                      Vec4 vec)
Sets the vector element at the specified position, as a Vec4. This buffer's logical vector size must be either 2, 3 or 4.

Parameters:
position - the logical vector position.
vec - the vector to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the vector is null, or if this buffer cannot store a Vec4.

putVectors

public void putVectors(int position,
                       java.lang.Iterable<? extends Vec4> iterable)
Sets a subsequence of this buffer with the contents of the specified Iterable. The subsequence to set starts with the vector at the specified position, and has size equal to the number of elements in the Iterable or the number of remaining vectors in the buffer, whichever is less. This buffer's logical vector size must be either 2, 3 or 4.

Parameters:
position - the starting vector position to set.
iterable - iterator over the elements to set.
Throws:
java.lang.IllegalArgumentException - if the position is out of range, if the iterable is null, or if this buffer cannot store a Vec4.

vectorPositionFromIndex

protected int vectorPositionFromIndex(int index)
Maps the physical buffer index to a logical vector position.

Parameters:
index - the physical buffer index.
Returns:
the vector position.

NASA World Wind