migrated code to public repository

This commit is contained in:
mohsen-mansouryar 2016-03-09 19:52:35 +01:00
parent a7df82d7a4
commit f34dc653e5
233 changed files with 16279 additions and 186 deletions

View file

@ -0,0 +1,102 @@
float marker_image_width;
float screen_border_width;
float mark_pause; // seconds
// Represents a 9-point grid calibration board
class CalibrationBoard {
PVector[] marks;
float xgap, ygap, xOffset, yOffset;
// standard order for 9point grid, starting from center and then clockwise
int[] inds = new int[] {1, 2, 3, 8, 0, 4, 7, 6, 5};
public void shuffleCalibrationOrder() { // FisherYates shuffle
for (int i = inds.length - 1; i > 0; i--)
{
int index = int(random(0, i + 1));
int tmp = inds[index];
inds[index] = inds[i];
inds[i] = tmp;
}
}
public void generateInds(int grid_width) {
inds = new int[grid_width*grid_width];
for (int i=0; i<inds.length; i++) {
inds[i] = i;
}
}
public CalibrationBoard(int mode, int grid_width, float xOffset, float yOffset, float xgap, float ygap) {
if (mode == 1) {
this.xOffset = xOffset;
this.yOffset = yOffset;
generateInds(grid_width);
shuffleCalibrationOrder();
if (xgap == -1 || ygap == -1) {
computeGridMarks(grid_width, false);
} else {
this.xgap = xgap;
this.ygap = ygap;
computeGridMarks(grid_width, true);
}
} else {
int num = grid_width; // num of calibration points
inds = new int[num];
for (int i=0; i<inds.length; i++)
inds[i] = i;
generateRandomMarks();
}
}
public void generateRandomMarks() {
float w = width - 2*screen_border_width - marker_image_width;
float xOffset = screen_border_width + marker_image_width/2.;
float h = height - 2*screen_border_width - marker_image_width;
float yOffset = screen_border_width + marker_image_width/2.;
float x, y;
marks = new PVector[inds.length];
for (int i=0; i<inds.length; i++) {
x = random(0, w);
y = random(0, h);
marks[inds[i]] = new PVector(x + xOffset, y + yOffset);
}
}
public void computeGridMarks(int grid_width, boolean custom_gap_size) {
float w = width - 2*screen_border_width;
float h = height - 2*screen_border_width;
if (!custom_gap_size) {
xgap = (w - grid_width*marker_image_width)/float(grid_width-1);
ygap = (h - grid_width*marker_image_width)/float(grid_width-1);
}
marks = new PVector[grid_width*grid_width];
float x, y;
int c = 0;
for (int i=0; i<grid_width; i++) {
y = i * (ygap + marker_image_width) + screen_border_width + yOffset;
y += marker_image_width/2.;
for (int j=0; j<grid_width; j++) {
x = j * (xgap + marker_image_width) + screen_border_width + xOffset;
x += marker_image_width/2.;
marks[inds[c++]] = new PVector(x, y);
}
}
}
public void drawMarks() {
for (int i=0; i<marks.length; i++) {
drawMark(marks[i]);
fill(0);
//text(i + "", marks[i].x + 3, marks[i].y - 5);
}
}
private void drawMark(PVector pos) {
int w = 13;
stroke(0);
line(pos.x - w, pos.y, pos.x + w, pos.y);
line(pos.x, pos.y - w, pos.x, pos.y + w);
}
}

View file

@ -0,0 +1,69 @@
int marker_stroke_weight;
int marker_stroke_width;
float marker_move_step;
class Marker {
PVector p, c;
float w, h;
PImage image;
PVector dest;
boolean onDest;
public Marker(PImage img) {
this.image = img;
this.w = img.width;
this.h = img.height;
this.c = new PVector(width/2., height/2.);
this.p = new PVector(c.x - w/2., c.y - h/2.);
this.dest = this.p;
this.onDest = true;
}
public void draw() {
image(image, p.x, p.y);
stroke(255, 0, 0);
strokeWeight(marker_stroke_weight);
line(c.x - marker_stroke_width, c.y, c.x + marker_stroke_width, c.y);
line(c.x, c.y - marker_stroke_width, c.x, c.y + marker_stroke_width);
}
public void update() {
if (p.x != dest.x || p.y != dest.y) {
onDest = false;
PVector diff = dest.get();
diff.sub(p);
if (diff.mag() > marker_move_step) {
diff.normalize();
moveX(diff.x * marker_move_step);
moveY(diff.y * marker_move_step);
} else {
setX(dest.x);
setY(dest.y);
}
} else {
onDest = true;
}
}
public void moveX(float step) {
this.p.x += step;
this.c.x += step;
}
public void moveY(float step) {
this.p.y += step;
this.c.y += step;
}
public void setX(float x) {
this.p.x = x;
this.c.x = x + w/2.;
}
public void setY(float y) {
this.p.y = y;
this.c.y = y + h/2.;
}
}

View file

@ -0,0 +1,177 @@
Marker marker;
CalibrationBoard gc, gc_test;
String status = "not started";
int mark_pause_start = -1;
int curr_mark = 0;
boolean pause = true;
boolean finished = false;
boolean display_marks = true;
boolean calibration_mode = true;
boolean display_stats = true;
float start_delay = 3;
int count_down_begin;
void setup() {
fullScreen();
//size(640, 480);
frameRate(60);
screen_border_width = 0;
mark_pause = 2.0; // in seconds
marker_stroke_weight = 4;
marker_stroke_width = 13;
marker_move_step = 7;
PImage img = loadImage("1023.png");
marker_image_width = img.width;
marker = new Marker(img);
gc = new CalibrationBoard(1, 5, 0, 0, -1, -1); // means 5-point grid
gc_test = new CalibrationBoard(1, 4, (gc.xgap + marker_image_width)/2., (gc.ygap + marker_image_width)/2., gc.xgap, gc.ygap);
resetMarker();
}
void reset() {
mark_pause_start = -1;
curr_mark = 0;
pause = true;
finished = false;
display_marks = true;
calibration_mode = true;
gc = new CalibrationBoard(1, 5, 0, 0, -1, -1); // means 5-point grid
gc_test = new CalibrationBoard(1, 4, (gc.xgap + marker_image_width)/2., (gc.ygap + marker_image_width)/2., gc.xgap, gc.ygap);
resetMarker();
}
void resetMarker() {
float x, y;
if (calibration_mode) {
x = gc.marks[curr_mark].x - marker_image_width/2.;
y = gc.marks[curr_mark].y - marker_image_width/2.;
} else {
x = gc_test.marks[curr_mark].x - marker_image_width/2.;
y = gc_test.marks[curr_mark].y - marker_image_width/2.;
}
marker.dest = new PVector(x, y);
marker.setX(x);
marker.setY(y);
}
void draw() {
background(255);
if (!finished) {
marker.draw();
marker.update();
}
if (!pause) {
if (frameCount - count_down_begin > start_delay * frameRate) {
status = "started";
if (marker.onDest) {
if (mark_pause_start < 0) {
mark_pause_start = frameCount;
} else {
status = ((frameCount - mark_pause_start) / frameRate) + "";
if (frameCount - mark_pause_start > mark_pause * frameRate) {
if ((calibration_mode && curr_mark < gc.inds.length-1) || (!calibration_mode && curr_mark < gc_test.inds.length-1)) {
PVector destC;
if (calibration_mode) {
destC = gc.marks[++curr_mark];
} else {
destC = gc_test.marks[++curr_mark];
}
marker.dest = new PVector(destC.x - marker_image_width/2., destC.y - marker_image_width/2.);
} else {
status = "finished";
finished = true;
}
}
}
} else {
status = "moving";
mark_pause_start = -1;
}
} else {
text(((frameCount - count_down_begin)/frameRate) + "/" + start_delay, width/2, height-7);
}
} else {
status = "paused";
}
if (display_marks) {
gc.drawMarks();
gc_test.drawMarks();
}
//fill(255, 0, 0);
//noStroke();
//ellipse(marker.dest.x, marker.dest.y, 5, 5);
//fill(0, 0, 255);
//for (PVector m: gc.marks) {
// ellipse(m.x - marker_image_width/2., m.y - marker_image_width/2., 5, 5);
//}
if (finished) {
fill(255, 0, 0);
} else if (pause) {
fill(0);
} else {
fill(0, 0, 255);
}
if (display_stats) {
text("status: " + status, 3, 13);
String progress;
if (calibration_mode) {
progress = (curr_mark+1) + "/" + gc.inds.length;
} else {
progress = (curr_mark+1) + "/" + gc_test.inds.length;
}
text(progress, 3, height-7);
}
}
void keyPressed() {
println(keyCode);
switch (keyCode) {
case 32: // Space Button
pause = !pause;
display_marks = false;
if (!pause)
count_down_begin = frameCount;
break;
case 77: // M key
display_marks = !display_marks;
break;
case 67: // C key
curr_mark = 0;
calibration_mode = true;
println("calibration mode");
resetMarker();
break;
case 84: // T key
curr_mark = 0;
calibration_mode = false;
println("test mode");
resetMarker();
break;
case 82: // R key
reset();
break;
case 83: // S key
saveFrame("screenshot_###.png");
break;
case 72: // H key
display_stats = !display_stats;
break;
default:
break;
}
}

View file

@ -0,0 +1,2 @@
mode.id=processing.mode.java.JavaMode
mode=Java