Esempio n. 1
0
  @Override
  public DoubleBuffer asNioDouble() {
    if (wrappedBuffer == null) {
      if (offset() == 0) {
        return DoubleBuffer.wrap(doubleData);
      } else return (DoubleBuffer) DoubleBuffer.wrap(doubleData).position(offset());
    }

    if (offset() == 0) {
      return wrappedBuffer.asDoubleBuffer();
    } else return (DoubleBuffer) wrappedBuffer.asDoubleBuffer().position(offset());
  }
Esempio n. 2
0
  @Override
  public DoubleBuffer asNioDouble() {
    if (offset() >= Integer.MAX_VALUE)
      throw new IllegalStateException("Index out of bounds " + offset());

    if (wrappedBuffer == null) {
      return pointer.asByteBuffer().asDoubleBuffer();
    } else if (offset() == 0) {
      return wrappedBuffer.asDoubleBuffer();
    } else {
      return (DoubleBuffer) wrappedBuffer.asDoubleBuffer().position((int) (offset()));
    }
  }
Esempio n. 3
0
  @Override
  public double getDouble(int i) {
    if (doubleData != null) {
      if (i >= doubleData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return doubleData[i];
    } else if (floatData != null) {
      if (i >= floatData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return (double) floatData[i];
    } else if (intData != null) {
      dirty.set(false);
      return (double) intData[i];
    }

    if (dataType() == Type.FLOAT) {
      dirty.set(false);
      return wrappedBuffer.asFloatBuffer().get(i);
    } else if (dataType() == Type.INT) {
      dirty.set(false);
      return wrappedBuffer.asIntBuffer().get(i);
    } else {
      dirty.set(false);
      return wrappedBuffer.asDoubleBuffer().get(i);
    }
  }
 public static void main(String[] args) {
   ByteBuffer bb = ByteBuffer.allocate(BSIZE);
   // Allocation automatically zeroes the ByteBuffer:
   int i = 0;
   while (i++ < bb.limit()) if (bb.get() != 0) print("nonzero");
   print("i = " + i);
   bb.rewind();
   // Store and read a char array:
   bb.asCharBuffer().put("Howdy!");
   char c;
   while ((c = bb.getChar()) != 0) printnb(c + " ");
   print();
   bb.rewind();
   // Store and read a short:
   bb.asShortBuffer().put((short) 471142);
   print(bb.getShort());
   bb.rewind();
   // Store and read an int:
   bb.asIntBuffer().put(99471142);
   print(bb.getInt());
   bb.rewind();
   // Store and read a long:
   bb.asLongBuffer().put(99471142);
   print(bb.getLong());
   bb.rewind();
   // Store and read a float:
   bb.asFloatBuffer().put(99471142);
   print(bb.getFloat());
   bb.rewind();
   // Store and read a double:
   bb.asDoubleBuffer().put(99471142);
   print(bb.getDouble());
   bb.rewind();
 }
Esempio n. 5
0
  /**
   * Create a data buffer from the given length
   *
   * @param buffer
   * @param length
   */
  public BaseDataBuffer(ByteBuffer buffer, int length) {
    allocationMode = Nd4j.alloc;
    this.length = length;
    buffer.order(ByteOrder.nativeOrder());
    if (allocationMode() == AllocationMode.DIRECT) {
      this.wrappedBuffer = buffer;
    } else if (dataType() == Type.INT) {
      intData = new int[length];
      IntBuffer intBuffer = buffer.asIntBuffer();
      for (int i = 0; i < length; i++) {
        intData[i] = intBuffer.get(i);
      }
    } else if (dataType() == Type.DOUBLE) {
      doubleData = new double[length];
      DoubleBuffer doubleBuffer = buffer.asDoubleBuffer();
      for (int i = 0; i < length; i++) {
        doubleData[i] = doubleBuffer.get(i);
      }

    } else if (dataType() == Type.FLOAT) {
      floatData = new float[length];
      FloatBuffer floatBuffer = buffer.asFloatBuffer();
      for (int i = 0; i < length; i++) {
        floatData[i] = floatBuffer.get(i);
      }
    }
  }
Esempio n. 6
0
 @Override
 public DoubleBuffer asNioDouble() {
   if (wrappedBuffer == null) {
     return DoubleBuffer.wrap(doubleData);
   }
   return wrappedBuffer.asDoubleBuffer();
 }
Esempio n. 7
0
 public MemDeserializer(ByteChannel channel, ByteBuffer byteBuffer) {
   if (byteBuffer.capacity() % MemHolder.DOUBLESIZE != 0)
     throw new IllegalArgumentException("buffer capacity must be dividable by 8");
   this.channel = channel;
   this.byteBuffer = byteBuffer;
   this.intBuffer = byteBuffer.asIntBuffer();
   this.doubleBuffer = byteBuffer.asDoubleBuffer();
 }
 public WindowInputHandler setWindow(Window window) {
   if (this.window != null) {
     this.window.setKeyCallback(null);
   }
   this.window = window;
   if (window != null) {
     ByteBuffer buf = BufferUtils.createByteBuffer(16);
     buf.limit(8);
     ByteBuffer bufX = buf.slice();
     buf.limit(16);
     buf.position(8);
     ByteBuffer bufY = buf.slice();
     window.getCursorPos(bufX.asDoubleBuffer(), bufY.asDoubleBuffer());
     mouseX = bufX.getDouble();
     mouseY = window.getViewport().getHeight() - bufY.getDouble();
     setImmediateMode(immediateMode);
   }
   return this;
 }
Esempio n. 9
0
  public static double getMedian(ByteBuffer simulationResults) {
    double[] b = new double[simulationResults.capacity() / 8];
    simulationResults.rewind();
    simulationResults.asDoubleBuffer().get(b);
    Arrays.sort(b);

    if (b.length % 2 == 0) {
      return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2.0;
    } else {
      return b[b.length / 2];
    }
  }
  public NioWrapLittleConversion() {

    // big/little endian difference one liner
    order = ByteOrder.LITTLE_ENDIAN;

    byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(order);

    // views on the bytebuffer to fill/drain it efficiently
    charBuffer = byteBuffer.asCharBuffer();
    shortBuffer = byteBuffer.asShortBuffer();
    intBuffer = byteBuffer.asIntBuffer();
    longBuffer = byteBuffer.asLongBuffer();
    floatBuffer = byteBuffer.asFloatBuffer();
    doubleBuffer = byteBuffer.asDoubleBuffer();
  }
  @NotNull
  public static IndexedCollection<Double> ofDouble(@NotNull ByteBuffer byteBuffer) {
    int magic = byteBuffer.getInt();
    if (magic != MAGIC) {
      throw new IllegalArgumentException("bad magic number");
    }
    int version = byteBuffer.getInt();
    if (version != VERSION) {
      throw new IllegalArgumentException("bad version number");
    }

    int size = byteBuffer.getInt();
    DoubleBuffer values = byteBuffer.asDoubleBuffer();
    values.limit(size);
    byteBuffer.position(byteBuffer.position() + size * Integer.BYTES);
    return new DoubleBufferCollection(values.slice());
  }
Esempio n. 12
0
  @Override
  public void put(int i, double element) {
    if (doubleData != null) doubleData[offset() + i] = element;
    else if (floatData != null) floatData[offset() + i] = (float) element;
    else if (intData != null) intData[offset() + i] = (int) element;
    else {
      if (dataType() == Type.DOUBLE) {
        wrappedBuffer.asDoubleBuffer().put(offset() + i, element);
      } else if (dataType() == Type.INT) {
        wrappedBuffer.asIntBuffer().put(offset() + i, (int) element);
      } else {
        wrappedBuffer.asFloatBuffer().put(offset() + i, (float) element);
      }
    }

    dirty.set(true);
  }
Esempio n. 13
0
  /**
   * Create a data buffer from the given length
   *
   * @param buffer
   * @param length
   */
  public BaseDataBuffer(ByteBuffer buffer, long length) {
    if (length < 1) throw new IllegalArgumentException("Length must be >= 1");
    initTypeAndSize();

    this.length = length;
    allocationMode = AllocUtil.getAllocationModeFromContext();

    if (dataType() == Type.DOUBLE) {
      pointer = new DoublePointer(buffer.asDoubleBuffer());
      indexer = DoubleIndexer.create((DoublePointer) pointer);
    } else if (dataType() == Type.FLOAT) {
      pointer = new FloatPointer(buffer.asFloatBuffer());
      indexer = FloatIndexer.create((FloatPointer) pointer);
    } else if (dataType() == Type.INT) {
      pointer = new IntPointer(buffer.asIntBuffer());
      indexer = IntIndexer.create((IntPointer) pointer);
    }
  }
Esempio n. 14
0
 /**
  * @param data
  * @param copy
  */
 public BaseDataBuffer(double[] data, boolean copy) {
   allocationMode = Nd4j.alloc;
   if (allocationMode == AllocationMode.HEAP) {
     if (copy) {
       doubleData = ArrayUtil.copy(data);
     } else {
       this.doubleData = data;
     }
   } else {
     wrappedBuffer = ByteBuffer.allocateDirect(8 * data.length);
     wrappedBuffer.order(ByteOrder.nativeOrder());
     DoubleBuffer buffer = wrappedBuffer.asDoubleBuffer();
     for (int i = 0; i < data.length; i++) {
       buffer.put(i, data[i]);
     }
   }
   length = data.length;
 }
Esempio n. 15
0
  @Override
  public void put(int i, double element) {
    if (i < 0 || i >= length()) throw new IllegalArgumentException("Illegal index " + i);

    if (doubleData != null) doubleData[i] = element;
    else if (floatData != null) floatData[i] = (float) element;
    else if (intData != null) intData[i] = (int) element;
    else {
      if (dataType() == Type.DOUBLE) {
        wrappedBuffer.asDoubleBuffer().put(i, element);

      } else if (dataType() == Type.INT) {
        wrappedBuffer.asIntBuffer().put(i, (int) element);
      } else {
        wrappedBuffer.asFloatBuffer().put(i, (float) element);
      }
    }

    dirty.set(true);
  }
Esempio n. 16
0
 public void writeFile(File file) {
   try {
     int ints = 3;
     int doubles = _a.length;
     int size = 4 * ints + 8 * doubles;
     ByteBuffer bb = ByteBuffer.allocateDirect(size);
     IntBuffer ib = bb.asIntBuffer();
     ib.put(_nx);
     ib.put(_ny);
     ib.put(_nz);
     bb.position(4 * ib.position());
     DoubleBuffer db = bb.asDoubleBuffer();
     db.put(_a);
     bb.rewind();
     FileChannel channel = new FileOutputStream(file).getChannel();
     channel.write(bb);
     channel.close();
   } catch (IOException e) {
     System.out.println(e.getMessage());
   }
 }
Esempio n. 17
0
  /** Allocates a new packed image frame in native memory where rows are 8-byte aligned. */
  public Frame(int width, int height, int depth, int channels) {
    int pixelSize = Math.abs(depth) / 8;
    this.imageWidth = width;
    this.imageHeight = height;
    this.imageDepth = depth;
    this.imageChannels = channels;
    this.imageStride =
        ((imageWidth * imageChannels * pixelSize + 7) & ~7) / pixelSize; // 8-byte aligned
    this.image = new Buffer[1];

    ByteBuffer buffer =
        ByteBuffer.allocateDirect(imageHeight * imageStride * pixelSize)
            .order(ByteOrder.nativeOrder());
    switch (imageDepth) {
      case DEPTH_BYTE:
      case DEPTH_UBYTE:
        image[0] = buffer;
        break;
      case DEPTH_SHORT:
      case DEPTH_USHORT:
        image[0] = buffer.asShortBuffer();
        break;
      case DEPTH_INT:
        image[0] = buffer.asIntBuffer();
        break;
      case DEPTH_LONG:
        image[0] = buffer.asLongBuffer();
        break;
      case DEPTH_FLOAT:
        image[0] = buffer.asFloatBuffer();
        break;
      case DEPTH_DOUBLE:
        image[0] = buffer.asDoubleBuffer();
        break;
      default:
        throw new UnsupportedOperationException("Unsupported depth value: " + imageDepth);
    }
  }
Esempio n. 18
0
  @Override
  public float getFloat(int i) {
    if (doubleData != null) {
      if (i >= doubleData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return (float) doubleData[i];
    } else if (floatData != null) {
      if (i >= floatData.length) throw new IllegalStateException("Index out of bounds " + i);
      dirty.set(false);
      return floatData[i];
    } else if (intData != null) {
      dirty.set(false);
      return (float) intData[i];
    }

    if (dataType() == Type.DOUBLE) {
      dirty.set(false);
      return (float) wrappedBuffer.asDoubleBuffer().get(i);
    }

    dirty.getAndSet(true);
    return wrappedBuffer.asFloatBuffer().get(i);
  }
Esempio n. 19
0
 /**
  * Creates a buffer for the given number of elements and native byte ordering
  *
  * @param elements The number of elements in the buffer
  * @return The buffer
  */
 public static DoubleBuffer createDoubleBuffer(int elements) {
   ByteBuffer byteBuffer = ByteBuffer.allocateDirect(elements * 8);
   byteBuffer.order(ByteOrder.nativeOrder());
   return byteBuffer.asDoubleBuffer();
 }
Esempio n. 20
0
 public static DoubleBuffer newDoubleBuffer(int numDoubles) {
   ByteBuffer buffer = ByteBuffer.allocateDirect(numDoubles * 8);
   buffer.order(ByteOrder.nativeOrder());
   return buffer.asDoubleBuffer();
 }
Esempio n. 21
0
  private void setGeometry(MultiSurface ms, IfcProduct ifcProduct) {
    GeometryInfo geometryInfo = ifcProduct.getGeometry();
    if (geometryInfo != null) {
      GeometryData geometryData = geometryInfo.getData();
      if (geometryData != null && geometryData.getVertices() != null) {
        ByteBuffer indicesBuffer = ByteBuffer.wrap(geometryData.getIndices());
        indicesBuffer.order(ByteOrder.LITTLE_ENDIAN);
        IntBuffer indices = indicesBuffer.asIntBuffer();

        ByteBuffer verticesBuffer = ByteBuffer.wrap(geometryData.getVertices());
        verticesBuffer.order(ByteOrder.LITTLE_ENDIAN);
        FloatBuffer vertices = verticesBuffer.asFloatBuffer();

        double[] matrix = new double[16];
        ByteBuffer transformationBuffer = ByteBuffer.wrap(geometryInfo.getTransformation());
        transformationBuffer.order(ByteOrder.LITTLE_ENDIAN);
        DoubleBuffer transformation = transformationBuffer.asDoubleBuffer();
        transformation.get(matrix);

        for (int i = 0; i < indices.capacity(); i += 3) {
          int index1 = indices.get(i);
          int index2 = indices.get(i + 1);
          int index3 = indices.get(i + 2);

          double[] v1 = new double[4];
          double[] v2 = new double[4];
          double[] v3 = new double[4];

          Matrix.multiplyMV(
              v1,
              0,
              matrix,
              0,
              new double[] {
                (double) vertices.get(index1 * 3),
                (double) vertices.get(index1 * 3 + 1),
                (double) vertices.get(index1 * 3 + 2),
                1
              },
              0);
          Matrix.multiplyMV(
              v2,
              0,
              matrix,
              0,
              new double[] {
                (double) vertices.get(index2 * 3),
                (double) vertices.get(index2 * 3 + 1),
                (double) vertices.get(index2 * 3 + 2),
                1
              },
              0);
          Matrix.multiplyMV(
              v3,
              0,
              matrix,
              0,
              new double[] {
                (double) vertices.get(index3 * 3),
                (double) vertices.get(index3 * 3 + 1),
                (double) vertices.get(index3 * 3 + 2),
                1
              },
              0);

          ms.addSurfaceMember(
              createSurfaceProperty(
                  Arrays.asList(
                      new Double[] {
                        v1[0], v1[1], v1[2],
                        v2[0], v2[1], v2[2],
                        v3[0], v3[1], v3[2],
                        v1[0], v1[1], v1[2]
                      })));
        }
      }
    }
  }