<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.unizh.ini.jaer.projects.einsteintunnel.visualeffects;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.media.opengl.*;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics2D;


/**
 *
 * @author braendch
 */
public class EinsteinTunnelDisplay {

    public static boolean makeHistogram = true;
    
    public static boolean dropPerls = false; //doesnt work yet

    public static int commandPort = 20021;
    public static int packetCount = 0;
    public static int csx = 128;
    public static int csy = 128;
    public static int dsx = 78;
    public static int dsy = 102;
    public static int maxClusters = 200;
    public static int packetLength;
    public static int maxHistogramX;
    public static int nrClusters;
    public static short[] xHistogram;
    public static float[] xPos;
    public static float[] yPos;

    protected static Random random = new Random();

    public static void main(String[] args) throws IOException{
        char[] msg = new char[4];
        packetLength = maxClusters*8+4+4;
        xPos = new float[maxClusters];
        yPos = new float[maxClusters];
        xHistogram = new short[dsx];
        maxHistogramX = 1;
        byte[] buf = new byte[packetLength];

        DatagramSocket socket = new DatagramSocket(commandPort);

        System.out.println("Einstein Tunnel Display Method");

        while(true){
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            //seq #
            int packetNumber = ((buf[0] & 0xFF) << 24)
            | ((buf[1] & 0xFF) << 16)
            | ((buf[2] & 0xFF) << 8)
            | (buf[3] & 0xFF);
            //msg
            msg[0]=(char)(buf[4]);
            msg[1]=(char)(buf[5]);
            msg[2]=(char)(buf[6]);
            msg[3]=(char)(buf[7]);
            //nrc
            nrClusters = (((buf[8] & 0xFF) << 8)
                        | (buf[9] & 0xFF));
            //data
            if(msg[0]=='h' && msg[1]=='i' && msg[2]=='s'){
                readHistogramPacket(buf);
                packetCount++;
            }
            if(msg[0]=='p' && msg[1]=='o' && msg[2]=='s'){
                readPositionPacket(buf);
                //System.out.println("Read position... first position: "+xPos[0]);
                packetCount++;
            }
            checkInput();
            displayCanvas.repaint();
            if (packetNumber>packetCount){
                System.out.println("Lost Packet! "+packetNumber);
                packetCount = packetNumber;
            }
        }
    }

    static void readHistogramPacket(byte[] buf){
        for(int i=0; i<dsx; i++){
            xHistogram[i] = (short)(((buf[10+2*i] & 0xFF) << 8)
                           | (buf[11+2*i] & 0xFF));
        }
    }

    static void readPositionPacket(byte[] buf){
        for(int i=0; i<nrClusters; i++){
            int offset = i*8;
            int xInt =    ((buf[10+offset] & 0xFF) << 24)
                        | ((buf[11+offset] & 0xFF) << 16)
                        | ((buf[12+offset] & 0xFF) << 8)
                        |  (buf[13+offset] & 0xFF);
            xPos[i] =  Float.intBitsToFloat(xInt);
            int yInt =    ((buf[14+offset] & 0xFF) << 24)
                        | ((buf[15+offset] & 0xFF) << 16)
                        | ((buf[16+offset] & 0xFF) << 8)
                        |  (buf[17+offset] & 0xFF);
            yPos[i] = Float.intBitsToFloat(yInt);
        }
    }

    static GLU glu=null;
    static JFrame displayFrame=null;
    static GLCanvas displayCanvas=null;

    static void checkInput(){
        if(displayFrame==null || (displayFrame!=null && !displayFrame.isVisible())){
            createDisplay();
        }
    }

    static void createDisplay(){

        displayFrame=new JFrame("Tunnel Display");
        Insets displayInsets = displayFrame.getInsets();
        displayFrame.setSize(dsx+displayInsets.left+displayInsets.right, dsy+displayInsets.bottom+displayInsets.top+40);
        //histogramFrame.setSize(new Dimension(dsx,dsy));
        //displayFrame.setResizable(false);
        displayFrame.setAlwaysOnTop(true);
        displayFrame.setLocation(0, 0);
        displayCanvas=new GLCanvas();
        displayCanvas.setSize(dsx,dsy);
        displayCanvas.addGLEventListener(new GLEventListener(){
            public void init(GLAutoDrawable drawable) {
            }

            synchronized public void display(GLAutoDrawable drawable) {
                GL gl=drawable.getGL();
                gl.glLoadIdentity();
                gl.glClearColor(0,0,0,0);
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                //iteration through the xHistogram
                if(makeHistogram){
                    //background
                    //drawBackgroundSnakes(gl);
                    drawPerls(gl);
                    //histogram
                    drawHistogramFire(gl);
                    //dot
                    drawDot(gl);
                }
                //if(dropPerls)drawPerls(gl);
                int error=gl.glGetError();
                if(error!=GL.GL_NO_ERROR){
                    if(glu==null) glu=new GLU();
                    //log.warning("GL error number "+error+" "+glu.gluErrorString(error));
                }
            }

            synchronized public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
                GL gl=drawable.getGL();
                final int B=10;
                gl.glMatrixMode(GL.GL_PROJECTION);
                gl.glLoadIdentity(); // very important to load identity matrix here so this works after first resize!!!
                gl.glOrtho(-B,drawable.getWidth()+B,-B,drawable.getHeight()+B,10000,-10000);
                gl.glMatrixMode(GL.GL_MODELVIEW);<commit_msg>small setup changes<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.unizh.ini.jaer.projects.einsteintunnel.visualeffects;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.media.opengl.*;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics2D;


/**
 *
 * @author braendch
 */
public class EinsteinTunnelDisplay {

    public static boolean makeHistogram = true;
    
    public static boolean dropPerls = false; //doesnt work yet

    public static int commandPort = 20021;
    public static int packetCount = 0;
    public static int csx = 128;
    public static int csy = 128;
    public static int dsx = 78;
    public static int dsy = 102;
    public static int maxClusters = 200;
    public static int packetLength;
    public static int maxHistogramX;
    public static int nrClusters;
    public static short[] xHistogram;
    public static float[] xPos;
    public static float[] yPos;

    protected static Random random = new Random();

    public static void main(String[] args) throws IOException{
        char[] msg = new char[4];
        packetLength = maxClusters*8+4+4;
        xPos = new float[maxClusters];
        yPos = new float[maxClusters];
        xHistogram = new short[dsx];
        maxHistogramX = 1;
        byte[] buf = new byte[packetLength];

        DatagramSocket socket = new DatagramSocket(commandPort);

        System.out.println("Einstein Tunnel Display Method");

        while(true){
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            //seq #
            int packetNumber = ((buf[0] & 0xFF) << 24)
            | ((buf[1] & 0xFF) << 16)
            | ((buf[2] & 0xFF) << 8)
            | (buf[3] & 0xFF);
            //msg
            msg[0]=(char)(buf[4]);
            msg[1]=(char)(buf[5]);
            msg[2]=(char)(buf[6]);
            msg[3]=(char)(buf[7]);
            //nrc
            nrClusters = (((buf[8] & 0xFF) << 8)
                        | (buf[9] & 0xFF));
            //data
            if(msg[0]=='h' && msg[1]=='i' && msg[2]=='s'){
                readHistogramPacket(buf);
                packetCount++;
            }
            if(msg[0]=='p' && msg[1]=='o' && msg[2]=='s'){
                readPositionPacket(buf);
                //System.out.println("Read position... first position: "+xPos[0]);
                packetCount++;
            }
            checkInput();
            displayCanvas.repaint();
            if (packetNumber>packetCount){
                System.out.println("Lost Packet! "+packetNumber);
                packetCount = packetNumber;
            }
        }
    }

    static void readHistogramPacket(byte[] buf){
        for(int i=0; i<dsx; i++){
            xHistogram[i] = (short)(((buf[10+2*i] & 0xFF) << 8)
                           | (buf[11+2*i] & 0xFF));
        }
    }

    static void readPositionPacket(byte[] buf){
        for(int i=0; i<nrClusters; i++){
            int offset = i*8;
            int xInt =    ((buf[10+offset] & 0xFF) << 24)
                        | ((buf[11+offset] & 0xFF) << 16)
                        | ((buf[12+offset] & 0xFF) << 8)
                        |  (buf[13+offset] & 0xFF);
            xPos[i] =  Float.intBitsToFloat(xInt);
            int yInt =    ((buf[14+offset] & 0xFF) << 24)
                        | ((buf[15+offset] & 0xFF) << 16)
                        | ((buf[16+offset] & 0xFF) << 8)
                        |  (buf[17+offset] & 0xFF);
            yPos[i] = Float.intBitsToFloat(yInt);
        }
    }

    static GLU glu=null;
    static JFrame displayFrame=null;
    static GLCanvas displayCanvas=null;

    static void checkInput(){
        if(displayFrame==null || (displayFrame!=null && !displayFrame.isVisible())){
            createDisplay();
        }
    }

    static void createDisplay(){

        displayFrame=new JFrame("Tunnel Display");
        Insets displayInsets = displayFrame.getInsets();
        displayFrame.setSize(dsx+displayInsets.left+displayInsets.right, dsy+displayInsets.bottom+displayInsets.top+40);
        //histogramFrame.setSize(new Dimension(dsx,dsy));
        //displayFrame.setResizable(false);
        displayFrame.setAlwaysOnTop(true);
        displayFrame.setLocation(0, 0);
        displayCanvas=new GLCanvas();
        displayCanvas.setSize(dsx,dsy);
        displayCanvas.addGLEventListener(new GLEventListener(){
            public void init(GLAutoDrawable drawable) {
            }

            synchronized public void display(GLAutoDrawable drawable) {
                GL gl=drawable.getGL();
                gl.glLoadIdentity();
                gl.glClearColor(0,0,0,0);
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                //iteration through the xHistogram
                if(makeHistogram){
                    //background
                    //drawBackgroundSnakes(gl);
                    drawPerls(gl);
                    //histogram
                    drawHistogramFire(gl);
                    //dot
                    //drawDot(gl);
                }
                //if(dropPerls)drawPerls(gl);
                int error=gl.glGetError();
                if(error!=GL.GL_NO_ERROR){
                    if(glu==null) glu=new GLU();
                    //log.warning("GL error number "+error+" "+glu.gluErrorString(error));
                }
            }

            synchronized public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
                GL gl=drawable.getGL();
                final int B=10;
                gl.glMatrixMode(GL.GL_PROJECTION);
                gl.glLoadIdentity(); // very important to load identity matrix here so this works after first resize!!!
                gl.glOrtho(-B,drawable.getWidth()+B,-B,drawable.getHeight()+B,10000,-10000);
                gl.glMatrixMode(GL.GL_MODELVIEW);
{'fraction_non_alphanumeric': 0.1291445623342175, 'fraction_numerical': 0.020722811671087533, 'mean_word_length': 1.8182667601027798, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7254015', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3457, 'n_words': 757}