Package java.nio

Examples of java.nio.FloatBuffer


  }

  protected void initGL() {
    try {
      // setup ogl
      FloatBuffer pos = BufferUtils.createFloatBuffer(4).put(new float[] { 5.0f, 5.0f, 10.0f, 0.0f});
      FloatBuffer red = BufferUtils.createFloatBuffer(4).put(new float[] { 0.8f, 0.1f, 0.0f, 1.0f});
      FloatBuffer green = BufferUtils.createFloatBuffer(4).put(new float[] { 0.0f, 0.8f, 0.2f, 1.0f});
      FloatBuffer blue = BufferUtils.createFloatBuffer(4).put(new float[] { 0.2f, 0.2f, 1.0f, 1.0f});

      pos.flip();
      red.flip();
      green.flip();
      blue.flip();

      glLight(GL_LIGHT0, GL_POSITION, pos);
      glEnable(GL_CULL_FACE);
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glEnable(GL_DEPTH_TEST);

      /* make the gears */
      gear1 = glGenLists(1);
      glNewList(gear1, GL_COMPILE);
      glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
      gear(1.0f, 4.0f, 1.0f, 20, 0.7f);
      glEndList();

      gear2 = glGenLists(1);
      glNewList(gear2, GL_COMPILE);
      glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
      gear(0.5f, 2.0f, 2.0f, 10, 0.7f);
      glEndList();

      gear3 = glGenLists(1);
      glNewList(gear3, GL_COMPILE);
      glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
      gear(1.3f, 2.0f, 0.5f, 10, 0.7f);
      glEndList();
      glEnable(GL_NORMALIZE);
      glMatrixMode(GL_PROJECTION);

      System.err.println("LWJGL: " + Sys.getVersion() + " / " + LWJGLUtil.getPlatformName());
      System.err.println("GL_VENDOR: " + glGetString(GL_VENDOR));
      System.err.println("GL_RENDERER: " + glGetString(GL_RENDERER));
      System.err.println("GL_VERSION: " + glGetString(GL_VERSION));
      System.err.println();
      System.err.println("glLoadTransposeMatrixfARB() supported: " + GLContext.getCapabilities().GL_ARB_transpose_matrix);

      if (!GLContext.getCapabilities().GL_ARB_transpose_matrix) {
        // --- not using extensions
        glLoadIdentity();
      } else {
        // --- using extensions
        final FloatBuffer identityTranspose = BufferUtils.createFloatBuffer(16).put(
            new float[] { 1, 0, 0, 0, 0, 1, 0, 0,
              0, 0, 1, 0, 0, 0, 0, 1});
        identityTranspose.flip();
        glLoadTransposeMatrixARB(identityTranspose);
      }
      float h = (float) display_parent.getHeight() / (float) display_parent.getWidth();
      glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
      glMatrixMode(GL_MODELVIEW);
View Full Code Here


    // lag effect of loading a scene larger than the video RAM. Indeed, when using JVM
    // memory, the sanction is immediate : the scene becomes quite impossible to manipulate.
    // With NIO buffers, there is a progressive degradation of performances when the scene
    // doesn't fit entirely in the 3D card RAM.
    DoubleBuffer nioCoords = ByteBuffer.allocateDirect(triangles.length*3 * 8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
    FloatBuffer nioColors = ByteBuffer.allocateDirect(triangles.length*3 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
    FloatBuffer nioNormals = ByteBuffer.allocateDirect(triangles.length * 3 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();

    // Set up the coordinates loaded by the parent method into the
    // NIO buffers.
    // This also expand the geometry. The file is described in indexed mode : triangles
    // are defined by indexing points.
    // Java3D also has an indexed mode, and using it would be immediate.
    // Unfortunately, when using an indexed mode, the triangle vertices are shared by definition.
    // It is thus not possible to change the values for the vertices for only one triangle
    // For example, when highlighting a triangle, it is necessary to modify the colors of
    // this triangle only, not its neighbors.
    // On the other hand, if picking/highlighting was done on vertices only, then modifying
    // all triangles using this vertex could be a good idea.
    // In this example, a triangle is picked, and the closest vertex is also shown, but
    // highlighted only in this triangle. This is not that complicated, but shows potential
    // problems. One should thus be able to easily adapt this example to simpler cases.
    for (int i=0; i<triangles.length; i+=3) {
      int pt = triangles[i];
      nioCoords.put(data[0][pt]);
      nioCoords.put(data[1][pt]);
      nioCoords.put(data[2][pt]);
      pt = triangles[i+1];
      nioCoords.put(data[0][pt]);
      nioCoords.put(data[1][pt]);
      nioCoords.put(data[2][pt]);
      pt = triangles[i+2];
      nioCoords.put(data[0][pt]);
      nioCoords.put(data[1][pt]);
      nioCoords.put(data[2][pt]);
    }

    // Create a triangle array using NIO.
    // Unfortunately, Sun picking utilities don't handle NIO very well, thus
    // we need to overload a method
    TriangleArray ta = new TriangleArray(triangles.length, GeometryArray.BY_REFERENCE|GeometryArray.USE_NIO_BUFFER|GeometryArray.COORDINATES|GeometryArray.NORMALS|GeometryArray.COLOR_3) {
      public double[] getCoordRefDouble() {
        // When picking, tests have shown that NIO buffer copy has no noticeable impact
        // on performance. So, it is actually better to copy the array each time the picking is
        // done than to hog memory with a permanent copy.
        // It is also still better to copy the buffer only when picking rather than at each rendering
        double[] array = new double[getVertexCount()*3];
        DoubleBuffer db = (DoubleBuffer)super.getCoordRefBuffer().getBuffer();
        db.rewind();
        db.get(array); // optimized get
        return array;
      }
    };
      ta.setCoordRefBuffer(new J3DBuffer(nioCoords));

      // Use Java3D utilities to compute normals
    GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
    double[] coords = new double[data[0].length*3];
    nioCoords.position(0);
    nioCoords.get(coords);
    gi.setCoordinates(coords);
        // generate normals
        NormalGenerator ng = new NormalGenerator();
        ng.generateNormals(gi);
        Vector3f[] n3f = gi.getNormals();
        int[] ni = gi.getNormalIndices();
        float[] tmp = new float[3];
    for (int i=0; i<ni.length; ++i) {
      n3f[ni[i]].get(tmp);
      nioNormals.put(tmp);
    }
    ta.setNormalRefBuffer(new J3DBuffer(nioNormals));
       
    // Setup color buffer
    ta.setColorRefBuffer(new J3DBuffer(nioColors));
View Full Code Here

    // Get the shape back from the children list. Ignore case when there is no file loaded
    if (children.size()<1) return;
    Shape3D shape3d = (Shape3D)((ShapeNodeJava3D)children.get(0)).get3DObject();
   
    // Find the color buffer
    FloatBuffer colors = (FloatBuffer)(((TriangleArray)shape3d.getGeometry()).getColorRefBuffer().getBuffer());
   
    // helper variables
    int d = num+3;
    if (d>=names.length) return; // ignore keypressed events for unknown variables
    currentVariable = d;
   
    float[] color = new float[3];
   
    // Update the buffer
    for (int i=0; i<triangles.length; i+=3) {
      getColorForValue(data[d][triangles[i]]).getRGBColorComponents(color);
      colors.position(i*3);
      colors.put(color);
      getColorForValue(data[d][triangles[i+1]]).getRGBColorComponents(color);
      colors.position((i+1)*3);
      colors.put(color);
      getColorForValue(data[d][triangles[i+2]]).getRGBColorComponents(color);
      colors.position((i+2)*3);
      colors.put(color);
    }

    // Update the name in the tree to show the selected variable
    ((ActiveNode)children.get(0)).setName(names[d]);
   
View Full Code Here

    // indices of the picked triangle
    // should always be triangle at this point
    // Indices are set to vertex indices, as this is not an Index Geometry object
    int[] idx = pi.getPrimitiveColorIndices();
    FloatBuffer colors = (FloatBuffer)(pi.getGeometryArray()).getColorRefBuffer().getBuffer();
    float[] color = new float[3];
   
    // The index returned by this method is relative the the array returned above,
    // not the shape geometry array!
    int closest = pi.getClosestVertexIndex();

    // This method is called both when highlighting the shape and when de-selecting it
    // => handles both, so as to restore the original colors
    // Tip : try to hold CTRL while picking elements, for multiple selection
    // => this method is called exactly once for each element state change
    if (on) {
      // Choose a color for highlighting that is never used usually
      //Color.getHSBColor(5f/6f,1,1).getRGBColorComponents(color);
     
      // Let's do something more useful as an example : also highlight the
      // closest point in the picked triangle using the color saturation.
      // Tip: when using indexed geometry, one could change only the color of the
      // closest point and this would affect all triangles sharing it. On the other
      // hand, many applications need the selection of a triangle (or quad), and
      // don't want side effects on neighboring triangles.
      // This method gives a way to do both point and tiangle selection as an example.
      // It should be quite straightforward to adapt according to the needs of a
      // real application.
      getColorForValue(data[currentVariable][triangles[idx[0]]],(closest==0) ? 0.1f : 0.5f).getRGBColorComponents(color);
      colors.position(idx[0]*3);
      colors.put(color);
      getColorForValue(data[currentVariable][triangles[idx[1]]],(closest==1) ? 0.1f : 0.5f).getRGBColorComponents(color);
      colors.position(idx[1]*3);
      colors.put(color);
      getColorForValue(data[currentVariable][triangles[idx[2]]],(closest==2) ? 0.1f : 0.5f).getRGBColorComponents(color);
      colors.position(idx[2]*3);
      colors.put(color);
     
      // Also display the selected values to standard output
      System.out.print("Values for the picked triangle ("+names[currentVariable]+"): ");
      System.out.print(data[currentVariable][triangles[idx[0]]]);
      System.out.print(closest==0 ? " *  " : "    " );
      System.out.print(data[currentVariable][triangles[idx[1]]]);
      System.out.print(closest==1 ? " *  " : "    " );
      System.out.print(data[currentVariable][triangles[idx[2]]]);
      System.out.print(closest==2 ? " *  " : "    " );
      System.out.println(" with * = closest point");
    }
    else {
      // restore original colors
      getColorForValue(data[currentVariable][triangles[idx[0]]]).getRGBColorComponents(color);
      colors.position(idx[0]*3);
      colors.put(color);
      getColorForValue(data[currentVariable][triangles[idx[1]]]).getRGBColorComponents(color);
      colors.position(idx[1]*3);
      colors.put(color);
      getColorForValue(data[currentVariable][triangles[idx[2]]]).getRGBColorComponents(color);
      colors.position(idx[2]*3);
      colors.put(color);
    }

    // Syn3D event propagation, refresh the scene, etc...
    super.highlight(on,parameter);
  }
View Full Code Here

      ja[i] = iter.getFloatNext();
  }

  public ByteBuffer getDataAsByteBuffer() {
    ByteBuffer bb = ByteBuffer.allocate((int)(4*getSize()));
    FloatBuffer ib = bb.asFloatBuffer();
    ib.put( (float[]) get1DJavaArray(float.class)); // make sure its in canonical order
    return bb;
  }
View Full Code Here

  }

  static public void testNIO(String filenameIn, String filenameOut, long kbchunks) throws IOException {

    ByteBuffer bb = ByteBuffer.allocate(8*1000);
    FloatBuffer fb = bb.asFloatBuffer();
  }
View Full Code Here

        }
        i1 = allObjectMarkers.iterator();
        i2 = allImageMarkers.iterator();
        CvMat objectPoints = CvMat.create(1, totalPointCount, CV_32F, 3);
        CvMat imagePoints  = CvMat.create(1, totalPointCount, CV_32F, 2);
        FloatBuffer objectPointsBuf = objectPoints.getFloatBuffer();
        FloatBuffer imagePointsBuf  = imagePoints.getFloatBuffer();
        while (i1.hasNext() && i2.hasNext()) {
            Marker[] m1 = i1.next(),
                     m2 = i2.next();
            for (int j = 0; j < m1.length; j++) {
                if (useCenters) {
                    double[] c1 = m1[j].getCenter();
                    objectPointsBuf.put((float)c1[0]);
                    objectPointsBuf.put((float)c1[1]);
                    objectPointsBuf.put(0);

                    double[] c2 = m2[j].getCenter();
                    imagePointsBuf.put((float)c2[0]);
                    imagePointsBuf.put((float)c2[1]);
                } else { // use corners...
                    for (int k = 0; k < 4; k++) {
                        objectPointsBuf.put((float)m1[j].corners[2*k    ]);
                        objectPointsBuf.put((float)m1[j].corners[2*k + 1]);
                        objectPointsBuf.put(0);

                        imagePointsBuf.put((float)m2[j].corners[2*k    ]);
                        imagePointsBuf.put((float)m2[j].corners[2*k + 1]);
                    }
                }
            }
        }

View Full Code Here

        CvMat[] points1 = getPoints(useCenters);
        CvMat[] points2 = peer.getPoints(useCenters);

        // find points in common from points1 and points2
        // since points1[0] and points2[0] might not be equal...
        FloatBuffer objPts1 = points1[0].getFloatBuffer();
        FloatBuffer imgPts1 = points1[1].getFloatBuffer();
        IntBuffer imgCount1 = points1[2].getIntBuffer();
        FloatBuffer objPts2 = points2[0].getFloatBuffer();
        FloatBuffer imgPts2 = points2[1].getFloatBuffer();
        IntBuffer imgCount2 = points2[2].getIntBuffer();
        assert(imgCount1.capacity() == imgCount2.capacity());

        CvMat objectPointsMat = CvMat.create(1, Math.min(objPts1.capacity(), objPts2.capacity()), CV_32F, 3);
        CvMat imagePoints1Mat = CvMat.create(1, Math.min(imgPts1.capacity(), imgPts2.capacity()), CV_32F, 2);
        CvMat imagePoints2Mat = CvMat.create(1, Math.min(imgPts1.capacity(), imgPts2.capacity()), CV_32F, 2);
        CvMat pointCountsMat  = CvMat.create(1, imgCount1.capacity(), CV_32S, 1);
        FloatBuffer objectPoints = objectPointsMat.getFloatBuffer();
        FloatBuffer imagePoints1 = imagePoints1Mat.getFloatBuffer();
        FloatBuffer imagePoints2 = imagePoints2Mat.getFloatBuffer();
        IntBuffer   pointCounts  = pointCountsMat .getIntBuffer();

        int end1 = 0, end2 = 0;
        for (int i = 0; i < imgCount1.capacity(); i++) {
            int start1 = end1;
            int start2 = end2;
            end1 = start1 + imgCount1.get(i);
            end2 = start2 + imgCount2.get(i);

            int count = 0;
            for (int j = start1; j < end1; j++) {
                float x1 = objPts1.get(j*);
                float y1 = objPts1.get(j*3+1);
                float z1 = objPts1.get(j*3+2);
                for (int k = start2; k < end2; k++) {
                    float x2 = objPts2.get(k*);
                    float y2 = objPts2.get(k*3+1);
                    float z2 = objPts2.get(k*3+2);
                    if (x1 == x2 && y1 == y2 && z1 == z2) {
                        objectPoints.put(x1);
                        objectPoints.put(y1);
                        objectPoints.put(z1);

                        imagePoints1.put(imgPts1.get(j*2));
                        imagePoints1.put(imgPts1.get(j*2+1));

                        imagePoints2.put(imgPts2.get(k*2));
                        imagePoints2.put(imgPts2.get(k*2+1));

                        count++;
                        break;
                    }
                }
            }
            if (count > 0) {
                pointCounts.put(count);
            }
        }
        objectPointsMat.cols(objectPoints.position()/3);
        imagePoints1Mat.cols(imagePoints1.position()/2);
        imagePoints2Mat.cols(imagePoints2.position()/2);
        pointCountsMat .cols(pointCounts .position());


        // place our ProjectiveDevice at the origin...
        d.R = CvMat.create(3, 3); d.R.put(1.0, 0.0, 0.00.0, 1.0, 0.00.0, 0.0, 1.0);
View Full Code Here

    private IplImage distortMap1 = null, distortMap2 = null;
    private void initDistortMaps(boolean useFixedPointMaps) {
        if (distortMap1 == null || distortMap2 == null) {
            IplImage mapx = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_32F, 1);
            IplImage mapy = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_32F, 1);
            FloatBuffer bufx = mapx.getFloatBuffer();
            FloatBuffer bufy = mapy.getFloatBuffer();
            int width  = mapx.width();
            int height = mapx.height();
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    double[] distxy = undistort(x, y);
                    bufx.put((float)distxy[0]);
                    bufy.put((float)distxy[1]);
                }
            }
            if (useFixedPointMaps) {
                distortMap1 = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_16S, 2);
                distortMap2 = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_16U /* IPL_DEPTH_16S */, 1);
View Full Code Here

                    for (int i = 0; i < length; i++) {
                        vv[i+offset] = ib.get();
                    }
                    break;
                case CV_32F:
                    FloatBuffer fb = getFloatBuffer();
                    fb.position(index);
                    for (int i = 0; i < length; i++) {
                        vv[i+offset] = fb.get();
                    }
                    break;
                case CV_64F:
                    getDoubleBuffer().position(index);
                    getDoubleBuffer().get(vv, offset, length);
View Full Code Here

TOP

Related Classes of java.nio.FloatBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.