<commit_before>package com.veil.ai;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class FrameHistoryBuffer {

	//Change threshold that will cause frame[x-2] obsolete when calculating change at frame[x]
	public static float distanceChangeThreshold = 15; //unit in pixel
	public static float directionChangeThreshold = 20; //unit in degree
	
	public boolean verbose = false;
	
	private List<Rectangle> buf;
	private int bufSize;
	private boolean updated;
	
	public FrameHistoryBuffer(int bufferSize){
		buf = new LinkedList<Rectangle>();
		bufSize = bufferSize;
	}
	
	public List<Rectangle> getBuffer(){
		return buf;
	}
	
	public void clearUpdateFlag(){
		updated = false;
	}
	
	public boolean isUpdated(){
		return updated;
	}
	
	public void addFrame(Rectangle rect){
		if(buf.size() == bufSize){
			buf.remove(0);
		}
		buf.add(rect);
		updated = true;
	}
	
	//NOTE:: Prediction algorithm may need to consult research on "path extrapolation in human"
	public Rectangle[] predictNextFrame(int futureFrame){
		if(buf.size() == 0){
			if(verbose) System.out.println("Empty buf");
			return null;
		}else if(buf.size() == 1){
			//Predict as static object since there is not enough data
			Rectangle startingFrame = buf.iterator().next(); 
			Rectangle[] frames = new Rectangle[futureFrame];
			for(int i=0; i<frames.length; i++){
				frames[i] = new Rectangle(startingFrame);
			}
			if(verbose) System.out.println("Single frame buf");
			return frames;
		}else if(buf.size() == 2){
			//Use the same displacement, we have no enough data for direction change
			Iterator<Rectangle> it = buf.iterator();
			Rectangle first = it.next();
			Rectangle second = it.next();
			Vector2 displacement = new Vector2(second.x - first.x, second.y - first.y);
			Rectangle[] frames = new Rectangle[futureFrame];
			frames[0] = new Rectangle(second.x+displacement.x, second.y+displacement.y, second.width, second.height);
			for(int i=1; i<frames.length; i++){
				frames[i] = new Rectangle(frames[i-1].x+displacement.x, frames[i-1].y+displacement.y, 
						frames[i-1].width, frames[i-1].height);<commit_msg>Try out new prediction<commit_after>package com.veil.ai;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class FrameHistoryBuffer {

	//Change threshold that will cause frame[x-2] obsolete when calculating change at frame[x]
	public static float distanceChangeThreshold = 15; //unit in pixel
	public static float directionChangeThreshold = 20; //unit in degree
	
	public boolean verbose = false;
	
	private List<Rectangle> buf;
	private int bufSize;
	private boolean updated;
	
	public FrameHistoryBuffer(int bufferSize){
		buf = new LinkedList<Rectangle>();
		bufSize = bufferSize;
	}
	
	public List<Rectangle> getBuffer(){
		return buf;
	}
	
	public void clearUpdateFlag(){
		updated = false;
	}
	
	public boolean isUpdated(){
		return updated;
	}
	
	public void addFrame(Rectangle rect){
		if(buf.size() == bufSize){
			buf.remove(0);
		}
		buf.add(rect);
		updated = true;
	}
	
	public Rectangle[] predictNextFrame(int futureFrame){
		if(buf.size() == 0){
			if(verbose) System.out.println("Empty buf");
			return null;
		}
		double[] xPredictor = new double[buf.size()];
		double[] xVar = new double[buf.size()];
		double[] yPredictor = new double[buf.size()];
		double[] yVar = new double[buf.size()];
		int i=0;
		float w=0,h=0;
		for(Rectangle rect : buf){
			xPredictor[i] = i;
			xVar[i] = rect.x;
			yPredictor[i] = i;
			yVar[i] = rect.y;
			w = rect.width;
			h = rect.height;
			i++;
		}
		regression.PolynomialRegression xRegression = 
				new regression.PolynomialRegression(xPredictor, xVar, 1);
		regression.PolynomialRegression yRegression = 
				new regression.PolynomialRegression(yPredictor, yVar, 2);
		Rectangle[] rects = new Rectangle[futureFrame];
		for(i=0; i<futureFrame; i++){
			float predictedX = (float)xRegression.predict(buf.size()+i);
			float predictedY = (float)yRegression.predict(buf.size()+i);
			rects[i] = new Rectangle(predictedX, predictedY, w, h);
		}
		return rects;
	}
	
	//NOTE:: Prediction algorithm may need to consult research on "path extrapolation in human"
	public Rectangle[] __predictNextFrame(int futureFrame){
		if(buf.size() == 0){
			if(verbose) System.out.println("Empty buf");
			return null;
		}else if(buf.size() == 1){
			//Predict as static object since there is not enough data
			Rectangle startingFrame = buf.iterator().next(); 
			Rectangle[] frames = new Rectangle[futureFrame];
			for(int i=0; i<frames.length; i++){
				frames[i] = new Rectangle(startingFrame);
			}
			if(verbose) System.out.println("Single frame buf");
			return frames;
		}else if(buf.size() == 2){
			//Use the same displacement, we have no enough data for direction change
			Iterator<Rectangle> it = buf.iterator();
			Rectangle first = it.next();
			Rectangle second = it.next();
			Vector2 displacement = new Vector2(second.x - first.x, second.y - first.y);
			Rectangle[] frames = new Rectangle[futureFrame];
			frames[0] = new Rectangle(second.x+displacement.x, second.y+displacement.y, second.width, second.height);
			for(int i=1; i<frames.length; i++){
				frames[i] = new Rectangle(frames[i-1].x+displacement.x, frames[i-1].y+displacement.y, 
						frames[i-1].width, frames[i-1].height);
{'fraction_non_alphanumeric': 0.13351598173515983, 'fraction_numerical': 0.00821917808219178, 'mean_word_length': 4.151458137347131, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 11, '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': '4430917', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1766, 'n_words': 476}