Package gov.nasa.worldwind.util
Class VecBuffer
- java.lang.Object
-
- gov.nasa.worldwind.util.VecBuffer
-
public class VecBuffer extends java.lang.Object
VecBuffer provides an logical interface onBufferWrapper
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 propertycoordsPerElem
. For example, if a VecBuffer is composed of (x, y, z) tuples then coordsPerElem would be 3.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description 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
Fields Modifier and Type Field Description protected BufferWrapper
buffer
protected int
coordsPerVec
-
Constructor Summary
Constructors Constructor Description VecBuffer(int coordsPerVec, BufferWrapper buffer)
Constructs a new VecBuffer with the specified vector size, and backing BufferWrapper.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.VecBuffer
copyOf(int newSize)
Returns a copy of this VecBuffer with the specified new size.void
drawArrays(DrawContext dc, int drawMode)
RendersgetSize()
elements from the currently bounds OpenGL coordinate buffers, beginning with element 0.static VecBuffer
emptyVecBuffer(int coordsPerVec)
Returns the empty VecBuffer.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 geographicLatLon
.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 geographicPosition
.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 aVec4
.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
putSubBuffer(int position, VecBuffer buffer, int offset, int size)
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.
-
-
-
Field Detail
-
coordsPerVec
protected int coordsPerVec
-
buffer
protected BufferWrapper buffer
-
-
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.
-
-
Method Detail
-
emptyVecBuffer
public static VecBuffer emptyVecBuffer(int coordsPerVec)
Returns the empty VecBuffer. The returned VecBuffer has no backing buffer, and is immutable.- Parameters:
coordsPerVec
- the number of coordinates per logical vector.- Returns:
- the empty VecBuffer.
-
getCoordsPerVec
public int getCoordsPerVec()
Returns the number of coordinates per logical vector element.- Returns:
- the cardinality of a logical vector element.
-
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.
-
getBufferWrapper
public BufferWrapper getBufferWrapper()
Returns the backing BufferWrapper.- Returns:
- the backing buffer.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
putSubBuffer
public void putSubBuffer(int position, VecBuffer buffer, int offset, int size)
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 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.offset
- the vector position to start copying values from the specified buffer.size
- the number of vectors to read copy form the specified buffer.- Throws:
java.lang.IllegalArgumentException
- if the position is out of range, if the buffer is null or incompatible, if this buffer has insufficient length to store the sub-buffer at the specified position, or if the specified offset and size define a range outside of the specified buffer.
-
getVector
public Vec4 getVector(int position)
Returns the vector element at the specified position, as aVec4
. 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.
-
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.
-
getLocation
public LatLon getLocation(int position)
Returns the vector element at the specified position, as a geographicLatLon
. 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.
-
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.
-
getPosition
public Position getPosition(int position)
Returns the vector element at the specified position, as a geographicPosition
. 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.
-
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.
-
copyOf
public VecBuffer copyOf(int newSize)
Returns a copy of this VecBuffer with the specified new size. The new size must be greater than or equal to this VecBuffer's size. If the new size is greater than this buffer's size, this returns a new buffer which is partially filled with the contents of this buffer. The returned VecBuffer has the same number of coordinates per tuple and the same backing buffer type, but its contents are independent from this VecBuffer.- Parameters:
newSize
- the new buffer's size.- Returns:
- the new buffer, with the specified size.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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 NIOBuffer
. This buffer's vector size must be 3, or 4.- Parameters:
dc
- the currentDrawContext
.- 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 NIOBuffer
. This buffer's vector size must be 3.- Parameters:
dc
- the currentDrawContext
.- 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.
-
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.
-
drawArrays
public void drawArrays(DrawContext dc, int drawMode)
RendersgetSize()
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.
-
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.
-
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.
-
-