<commit_before>package org.ine.telluride.jaer.eyeinthesky;

import net.sf.jaer.eventprocessing.EventFilter2D;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.event.BasicEvent;
import net.sf.jaer.event.PolarityEvent;
import net.sf.jaer.chip.AEChip;
import net.sf.jaer.graphics.FrameAnnotater;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GL;
import java.awt.*;

/**
 * User: jauerbac
 * Date: Jul 7, 2009
 * Time: 2:06:46 PM
 */
public class JoshSingleLineTracker extends EventFilter2D implements FrameAnnotater {

    private double A=1,B=1,C=1,D=1,E =1, F=1;
    private double m=1,b=1;


    private boolean xHorizontal = true;

    private double maxDist;

    private final Object lock = new Object();

    public JoshSingleLineTracker(AEChip chip) {
        super(chip);
        maxDist = Math.sqrt(chip.getSizeX() * chip.getSizeX() + chip.getSizeY() * chip.getSizeY());
    }

    public Object getFilterState() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void resetFilter() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void initFilter() {
        //To change body of implemented methods use File | Settings | File Templates.
    }


    public EventPacket<?> filterPacket(EventPacket<?> in) {
        if(!isFilterEnabled()) return in;
        if(maxDist == 0)
            maxDist = Math.sqrt(chip.getSizeX() * chip.getSizeX() + chip.getSizeY() * chip.getSizeY());
        for(BasicEvent event : in) {
//            if(((PolarityEvent) event).polarity == PolarityEvent.Polarity.On) {
                double dist = (xHorizontal ? Math.abs(m * event.x - event.y + b) : Math.abs(m * event.y - event.x + b)) / Math.sqrt(m*m + 1);
                //System.out.println("m: " + m + " md: " + maxDist + " d: " + dist);

                double weight = (maxDist - dist)/maxDist;

                updateCoefficients(event.x, event.y, 0.01 * Math.pow(weight,10));
//            }


        }
        return in;

    }

    public void annotate(float[][][] frame) {}
    public void annotate(Graphics2D g) {}

    public void annotate(GLAutoDrawable drawable) {
         if (!isAnnotationEnabled()) {
            return;
        }
        GL gl = drawable.getGL();
        gl.glBegin((GL.GL_LINES));
        gl.glColor3d(1, 0, 0);


        double xBottomIntercept,xTopIntercept,yLeftIntercept,yRightIntercept;

        synchronized(lock) {
            if(xHorizontal) {
                xBottomIntercept = -b/m;
                xTopIntercept = (chip.getSizeY() - b)/m;
                yLeftIntercept = b;
                yRightIntercept = m*chip.getSizeX() + b;
            } else {
                xBottomIntercept = b;
                xTopIntercept = m*chip.getSizeY() + b;
                yLeftIntercept = -b/m;
                yRightIntercept = (chip.getSizeX() - b)/m;
            }
        }




        if(yLeftIntercept >= 0 && yLeftIntercept<=chip.getSizeY()) {
            gl.glVertex2d(0, yLeftIntercept);
        }
        if(yRightIntercept >= 0 && yRightIntercept<=chip.getSizeY()) {
            gl.glVertex2d(chip.getSizeX(), yRightIntercept);
        }
        if(xBottomIntercept > 0 && xBottomIntercept<chip.getSizeY()) {
            gl.glVertex2d(xBottomIntercept, 0);
        }
        if(xTopIntercept > 0 && xTopIntercept < chip.getSizeY()) {
            gl.glVertex2d(xTopIntercept, chip.getSizeY());
        }


        gl.glEnd();

    }

    private void updateCoefficients(double x, double y, double weight) {
//        if(weight < 0.01)
//            weight = 0.01;
//        if(weight > 0.5)
//            weight = 0.5;
        double iWeight = 1.0 - weight;

        double var1 = xHorizontal ? x : y;
        double var2 = xHorizontal ? y : x;


        A = iWeight * A + weight * var1 * var1;
        B = iWeight * B + weight * 2 * var1;
        C = iWeight * C + weight;
        D = iWeight * D - weight * 2 * var1 * var2;
        E = iWeight * E - weight * 2 * var2;
        F = iWeight * F + weight * var2 * var2;

        double denominator = 4*A*C - (B * B);

        b = (B*D - 2*A*E)/denominator;
        m = (B*E - 2*C*D)/denominator;

        synchronized(lock) {
            if(Math.abs(m) > 1.1) {
                System.out.print("FLIP! -- xHorizontal=" +xHorizontal+" m=" + m);

                double temp = A;
                A = F;
                F = temp;
                temp = B;<commit_msg>pseudo center tracking<commit_after>package org.ine.telluride.jaer.eyeinthesky;

import net.sf.jaer.eventprocessing.EventFilter2D;
import net.sf.jaer.event.EventPacket;
import net.sf.jaer.event.BasicEvent;
import net.sf.jaer.event.PolarityEvent;
import net.sf.jaer.chip.AEChip;
import net.sf.jaer.graphics.FrameAnnotater;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GL;
import java.awt.*;

/**
 * User: jauerbac
 * Date: Jul 7, 2009
 * Time: 2:06:46 PM
 */
public class JoshSingleLineTracker extends EventFilter2D implements FrameAnnotater {

    private double A=1,B=1,C=1,D=1,E =1, F=1;
    private double m=1,b=1;

    private double center = 50;


    private boolean xHorizontal = true;

    private double maxDist;

    private final Object lock = new Object();

    public JoshSingleLineTracker(AEChip chip) {
        super(chip);
        maxDist = Math.sqrt(chip.getSizeX() * chip.getSizeX() + chip.getSizeY() * chip.getSizeY());
    }

    public Object getFilterState() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void resetFilter() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public void initFilter() {
        //To change body of implemented methods use File | Settings | File Templates.
    }


    public EventPacket<?> filterPacket(EventPacket<?> in) {
        if(!isFilterEnabled()) return in;
        if(maxDist == 0)
            maxDist = Math.sqrt(chip.getSizeX() * chip.getSizeX() + chip.getSizeY() * chip.getSizeY());
        for(BasicEvent event : in) {
//            if(((PolarityEvent) event).polarity == PolarityEvent.Polarity.On) {
                double dist = (xHorizontal ? Math.abs(m * event.x - event.y + b) : Math.abs(m * event.y - event.x + b)) / Math.sqrt(m*m + 1);
                //System.out.println("m: " + m + " md: " + maxDist + " d: " + dist);

                double weight = (maxDist - dist)/maxDist;

                updateCoefficients(event.x, event.y, 0.01 * Math.pow(weight,10));
//            }


        }
        return in;

    }

    public void annotate(float[][][] frame) {}
    public void annotate(Graphics2D g) {}

    public void annotate(GLAutoDrawable drawable) {
         if (!isAnnotationEnabled()) {
            return;
        }
        GL gl = drawable.getGL();
        gl.glBegin((GL.GL_LINES));
        gl.glColor3d(1, 0, 0);


        double xBottomIntercept,xTopIntercept,yLeftIntercept,yRightIntercept;

        double xCenter,yCenter;

        synchronized(lock) {
            if(xHorizontal) {
                xBottomIntercept = -b/m;
                xTopIntercept = (chip.getSizeY() - b)/m;
                yLeftIntercept = b;
                yRightIntercept = m*chip.getSizeX() + b;

                xCenter = center;
                yCenter = m*center + b;

            } else {
                xBottomIntercept = b;
                xTopIntercept = m*chip.getSizeY() + b;
                yLeftIntercept = -b/m;
                yRightIntercept = (chip.getSizeX() - b)/m;

                xCenter = m*center + b;
                yCenter = center;

            }
        }




        if(yLeftIntercept >= 0 && yLeftIntercept<=chip.getSizeY()) {
            gl.glVertex2d(0, yLeftIntercept);
        }
        if(yRightIntercept >= 0 && yRightIntercept<=chip.getSizeY()) {
            gl.glVertex2d(chip.getSizeX(), yRightIntercept);
        }
        if(xBottomIntercept > 0 && xBottomIntercept<chip.getSizeY()) {
            gl.glVertex2d(xBottomIntercept, 0);
        }
        if(xTopIntercept > 0 && xTopIntercept < chip.getSizeY()) {
            gl.glVertex2d(xTopIntercept, chip.getSizeY());
        }


        gl.glEnd();

        gl.glBegin((GL.GL_LINES));
        gl.glColor3d(1, 0, 0);
        gl.glVertex2d(Math.max(xCenter - 5, 0), yCenter);
        gl.glVertex2d(Math.min(xCenter + 5, chip.getSizeX()), yCenter);
        gl.glEnd();
        gl.glBegin((GL.GL_LINES));
        gl.glColor3d(1, 0, 0);
        gl.glVertex2d(xCenter,Math.max(yCenter - 5, 0));
        gl.glVertex2d(xCenter,Math.min(yCenter + 5, chip.getSizeY()));
        gl.glEnd();

    }

    private void updateCoefficients(double x, double y, double weight) {
//        if(weight < 0.01)
//            weight = 0.01;
//        if(weight > 0.5)
//            weight = 0.5;
        double iWeight = 1.0 - weight;

        double var1 = xHorizontal ? x : y;
        double var2 = xHorizontal ? y : x;


        A = iWeight * A + weight * var1 * var1;
        B = iWeight * B + weight * 2 * var1;
        C = iWeight * C + weight;
        D = iWeight * D - weight * 2 * var1 * var2;
        E = iWeight * E - weight * 2 * var2;
        F = iWeight * F + weight * var2 * var2;

        center = iWeight * center + weight * var1;

        double denominator = 4*A*C - (B * B);

        b = (B*D - 2*A*E)/denominator;
        m = (B*E - 2*C*D)/denominator;

        synchronized(lock) {
            if(Math.abs(m) > 1.1) {
                System.out.print("FLIP! -- xHorizontal=" +xHorizontal+" m=" + m);

                double temp = A;
                A = F;
                F = temp;
                temp = B;
{'fraction_non_alphanumeric': 0.13173591114767585, 'fraction_numerical': 0.016968325791855202, 'mean_word_length': 1.94786298878448, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, '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': '1693789', 'n_tokens_mistral': 3313, 'n_tokens_neox': 3123, 'n_words': 688}