下面列出了java.nio.DoubleBuffer#position ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void copy(DoubleBuffer srcBuf, int srcStep, DoubleBuffer dstBuf, int dstStep) {
int w = Math.min(srcStep, dstStep);
int srcLine = srcBuf.position();
int dstLine = dstBuf.position();
while (srcLine < srcBuf.capacity() && dstLine < dstBuf.capacity()) {
srcBuf.position(srcLine);
dstBuf.position(dstLine);
w = Math.min(Math.min(w, srcBuf.remaining()), dstBuf.remaining());
for (int x = 0; x < w; x++) {
double in = srcBuf.get();
double out = in;
dstBuf.put(out);
}
srcLine += srcStep;
dstLine += dstStep;
}
}
@Override
public DoubleDataBuffer slice(long index, long size) {
Validator.sliceArgs(this, index, size);
DoubleBuffer sliceBuf = buf.duplicate();
sliceBuf.position((int)index);
sliceBuf.limit((int)index + (int)size);
return new DoubleNioDataBuffer(sliceBuf.slice());
}
private double[] getVectorOrNull(final String word) {
final Integer index = word2vectorOffset.get(word);
if(index == null)
return null;
final DoubleBuffer vectors = model.vectors.duplicate();
double[] result = new double[model.layerSize];
vectors.position(index);
vectors.get(result);
return result;
}
public static void copy(double[] src, int srcOffset, DoubleBuffer dest, int destOffset) {
dest.position(destOffset);
dest.put(src, srcOffset, 3);
}
public static void copy(DoubleBuffer src, int srcOffset, double[] dest, int destOffset) {
src.position(srcOffset);
dest[destOffset + 0] = src.get();
dest[destOffset + 1] = src.get();
dest[destOffset + 2] = src.get();
}
public static DoubleBuffer getDoubleBufferPosition(DoubleBuffer buffer, int position) {
return buffer.position(position);
}
private BufferSet appendInvertedGeometry(IntBuffer indicesAsInt, DoubleBuffer verticesAsDouble, FloatBuffer normalsAsFloat, IntBuffer colorIndices) {
indicesAsInt.position(0);
normalsAsFloat.position(0);
ByteBuffer newVerticesByteBuffer = ByteBuffer.allocate(verticesAsDouble.capacity() * 16);
DoubleBuffer newVerticesBuffer = newVerticesByteBuffer.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer();
verticesAsDouble.position(0);
newVerticesBuffer.put(verticesAsDouble);
verticesAsDouble.position(0);
newVerticesBuffer.put(verticesAsDouble);
int nrVertices = verticesAsDouble.capacity() / 3;
ByteBuffer newIntByteBuffer = ByteBuffer.allocate(indicesAsInt.capacity() * 8);
IntBuffer newIntBuffer = newIntByteBuffer.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
for (int i=0; i<indicesAsInt.capacity(); i+=3) {
int index1 = indicesAsInt.get();
int index2 = indicesAsInt.get();
int index3 = indicesAsInt.get();
newIntBuffer.put(i, index1);
newIntBuffer.put(i + 1, index2);
newIntBuffer.put(i + 2, index3);
// And draw the same triangle again in a different order
newIntBuffer.put(i + indicesAsInt.capacity(), index1 + nrVertices);
newIntBuffer.put(i + indicesAsInt.capacity() + 1, index3 + nrVertices);
newIntBuffer.put(i + indicesAsInt.capacity() + 2, index2 + nrVertices);
}
ByteBuffer newNormalsByteBuffer = ByteBuffer.allocate(normalsAsFloat.capacity() * 8);
FloatBuffer newNormalsBuffer = newNormalsByteBuffer.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
normalsAsFloat.position(0);
newNormalsBuffer.put(normalsAsFloat);
for (int i=0; i<normalsAsFloat.capacity(); i+=3) {
float[] normal = new float[] {normalsAsFloat.get(i + 0), normalsAsFloat.get(i + 1), normalsAsFloat.get(i + 2)};
normal = Vector.invert(normal);
newNormalsBuffer.put(i + normalsAsFloat.capacity(), normal[0]);
newNormalsBuffer.put(i + 1 + normalsAsFloat.capacity(), normal[1]);
newNormalsBuffer.put(i + 2 + normalsAsFloat.capacity(), normal[2]);
}
ByteBuffer newColorsByteBuffer = ByteBuffer.allocate(colorIndices.capacity() * 8);
IntBuffer newColorsBuffer = newColorsByteBuffer.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
colorIndices.position(0);
newColorsBuffer.put(colorIndices);
colorIndices.position(0);
newColorsBuffer.put(colorIndices);
return new BufferSet(newIntByteBuffer, newVerticesByteBuffer, newNormalsByteBuffer, newColorsByteBuffer);
}
/**
* Set the values of this matrix by reading 6 double values from the given {@link DoubleBuffer} in column-major order,
* starting at its current position.
* <p>
* The DoubleBuffer is expected to contain the values in column-major order.
* <p>
* The position of the DoubleBuffer will not be changed by this method.
*
* @param buffer
* the DoubleBuffer to read the matrix values from in column-major order
* @return this
*/
public Matrix3x2d set(DoubleBuffer buffer) {
int pos = buffer.position();
MemUtil.INSTANCE.get(this, pos, buffer);
return this;
}