package org.jzy3d.demos.drawing.vbo.barmodel.builder;

import java.util.List;

import org.jzy3d.colors.Color;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.demos.drawing.vbo.barmodel.color.KeyRankColorMapper;
import org.jzy3d.io.KeyVal;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.primitives.vbo.buffers.FloatVBO;
import org.jzy3d.plot3d.primitives.vbo.builders.VBOBuilder;
import org.jzy3d.plot3d.primitives.vbo.drawable.DrawableVBO;

import com.jogamp.opengl.GL;

/**
 * Building a VBO that represent a list of rows made of lists of
 * {@link KeyVal}ue pairs.
 * 
 * Column are sorted in increasing alphabetical order along Y axis. Value (
 * {@link KeyVal.val}) is ploted as a Z value for the current column.
 * 
 * @author Martin Pernollet
 */
public class VBOBuilderTableColumnsScatter3d extends VBOBuilder {
    List>> rows;

    public VBOBuilderTableColumnsScatter3d(List>> rows) {
        super();
        this.rows = rows;
    }

    public void load(GL gl, DrawableVBO drawable) throws Exception {
        preConfigureDrawable(drawable);
        FloatVBO vbo = initFloatVBO(drawable, true, countKv(rows));
        fill(rows, vbo);
        drawable.setData(gl, vbo);
    }

    private void preConfigureDrawable(DrawableVBO drawable) {
        drawable.setGeometry(GL.GL_POINTS);
        drawable.setHasColorBuffer(true);
        drawable.setWidth(1f);
    }

    protected int countKv(List>> rows) {
        int n = 0;
        for (int i = 0; i < rows.size(); i++) {
            List> row = rows.get(i);
            for (int j = 0; j < row.size(); j++) {
                n++;
            }
        }
        return n;
    }

    /**
     * Create one point per column, assigning unique column color based on
     * column ({@link KeyVal.key}) name. Column are sorted in increasing
     * alphabetical order along Y axis. Value ({@link KeyVal.value}) is ploted
     * as a Z value for the current column
     * 
     * @param rows
     * @param vbo
     */
    protected void fill(List>> rows, FloatVBO vbo) {
        int size = 0;
        KeyRankColorMapper coloring = new KeyRankColorMapper(rows, new ColorMapRainbow());
        for (int i = 0; i < rows.size(); i++) {
            List> row = rows.get(i);

            for (int j = 0; j < row.size(); j++) {
                KeyVal k = row.get(j);
                Color color = coloring.getColor(k);
                Coord3d c1 = new Coord3d(i, j, k.val);
                putPoint(vbo, size++, color, c1);
            }
        }
        vbo.getVertices().rewind();
        vbo.getIndices().rewind();
    }
}

 

Ads help maintain this website.