102 lines
3.0 KiB
Plaintext
102 lines
3.0 KiB
Plaintext
|
||
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() { // Fisher–Yates 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);
|
||
}
|
||
} |