Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Before changing to tetrahedron / triangle-ray test.
  • Loading branch information
Andrew Lawson authored and Andrew Lawson committed Dec 8, 2014
1 parent 40fa6d4 commit 6d60e6d
Showing 1 changed file with 59 additions and 52 deletions.
111 changes: 59 additions & 52 deletions geometric_separators.pde
Expand Up @@ -3,6 +3,8 @@ import java.util.Map.*;
import java.lang.Boolean; import java.lang.Boolean;
import java.util.Random; import java.util.Random;
import org.jblas.*; import org.jblas.*;
import org.apache.commons.math3.linear.*;
import org.apache.commons.math3.linear.SingularValueDecomposition;


int reset_x = 140; int reset_x = 140;
int reset_y = 460; int reset_y = 460;
Expand Down Expand Up @@ -31,67 +33,72 @@ private class CenterAndSphere {


// Setup // Setup
void setup() { void setup() {
background(255); background(255);
size(500,500,P2D); size(500,500,P2D);
reset = createShape(RECT, reset_x, reset_y, reset_w, reset_h); reset = createShape(RECT, reset_x, reset_y, reset_w, reset_h);
calculate = createShape(RECT, calc_x, calc_y, calc_w, calc_h); calculate = createShape(RECT, calc_x, calc_y, calc_w, calc_h);
noLoop(); noLoop();
} }


// On mouse press // On mouse press
void mousePressed() { void mousePressed() {
// If mouse presses reset button // If mouse presses reset button
if ((mouseX >= reset_x && mouseX <= (reset_x + reset_w)) && if ((mouseX >= reset_x && mouseX <= (reset_x + reset_w)) &&
(mouseY >= reset_y && mouseY <= (reset_y + reset_h))) { (mouseY >= reset_y && mouseY <= (reset_y + reset_h))) {
// Reset input and background // Reset input and background
rawInput.clear(); rawInput.clear();
centerPoint = null; centerPoint = null;
sphere = null; sphere = null;
} }
// If mouse presses calculate button // If mouse presses calculate button
else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) && else if ((mouseX >= calc_x && mouseX <= (calc_x + calc_w)) &&
(mouseY >= calc_y && mouseY <= (calc_y + calc_h))) { (mouseY >= calc_y && mouseY <= (calc_y + calc_h))) {
CenterAndSphere returnVals = getSeparator(rawInput); CenterAndSphere returnVals = getSeparator(rawInput);
sphere = returnVals.sphere; sphere = returnVals.sphere;
centerPoint = returnVals.center; centerPoint = returnVals.center;
} }
// Otherwise add input to list // Otherwise add input to list
else { else {
// Create new 2D point from mouse coordinates // Create new 2D point from mouse coordinates
PVector point = new PVector(mouseX, mouseY, 0); PVector point = new PVector(mouseX, mouseY, 0);
PVector pointLifted = new PVector(point.x, point.y, (float)Math.pow(point.mag(), 2)); PVector pointLifted = new PVector(point.x, point.y, (float)Math.pow(point.mag(), 2));
rawInput.add(pointLifted); rawInput.add(pointLifted);
} }
redraw(); redraw();
} }


// Get Radon point // Get Radon point
PVector getRadonPoint(ArrayList<PVector> input) { PVector getRadonPoint(ArrayList<PVector> input) {
// Set a to equal our points // Set a to equal our points
float[][] aData = new float[3][input.size()]; double[][] aData = new double[4][input.size()];
for (int i = 0; i < input.size(); i++) { for (int i = 0; i < input.size(); i++) {
for (int j = 0; j < 3; j++) { for (int j = 0; j < 4; j++) {
switch(j) { switch(j) {
case 0: aData[j][i] = input.get(i).x; case 0: aData[j][i] = (double)input.get(i).x;
break;
case 1: aData[j][i] = (double)input.get(i).y;
break; break;
case 1: aData[j][i] = input.get(i).y; case 2: aData[j][i] = (double)input.get(i).z;
break; break;
case 2: aData[j][i] = input.get(i).z; case 3: aData[j][i] = 1;
break; break;
} }
} }
} }
FloatMatrix coefficients = new FloatMatrix(aData); RealMatrix coefficients = new Array2DRowRealMatrix(aData, false);
FloatMatrix constants = FloatMatrix.zeros(5, 2); RealVector constants = new ArrayRealVector(new double[] {0, 0, 0, 0}, false);
FloatMatrix solution = Solve.solveLeastSquares(coefficients, constants); DecompositionSolver solver = new SingularValueDecomposition(coefficients).getSolver();
RealVector solution = solver.solve(constants);
for (double x : solution.toArray()) {
System.out.println(x);
}
return new PVector(0,0); return new PVector(0,0);
} }


// Get geometric separator // Get geometric separator
CenterAndSphere getSeparator(ArrayList<PVector> input) { CenterAndSphere getSeparator(ArrayList<PVector> input) {
PVector centerPoint = approxCenterpoint(input); PVector centerPoint = approxCenterpoint(input);
PVector unitVector = PVector.random3D(); PVector unitVector = PVector.random3D();
unitVector.mult(500);
unitVector.x = Math.abs(unitVector.x); unitVector.x = Math.abs(unitVector.x);
unitVector.y = Math.abs(unitVector.y); unitVector.y = Math.abs(unitVector.y);
unitVector.z = Math.abs(unitVector.z); unitVector.z = Math.abs(unitVector.z);
Expand Down Expand Up @@ -164,31 +171,31 @@ PVector approxCenterpoint(ArrayList<PVector> input) {


// Draw // Draw
void draw() { void draw() {
background(255); background(255);
// Draw center point shape(reset);
if (centerPoint != null) { shape(calculate);
fill(50);
textSize(20);
text("Geometric Separators", 140, 20);
textSize(12);
text("Reset.", reset_x + 30, reset_y + 15);
text("Calculate.", calc_x + 25, calc_y + 15);
// Draw input
for (PVector point : rawInput) {
strokeWeight(8);
point(point.x, point.y);
strokeWeight(2);
}
// Draw center point
if (centerPoint != null) {
strokeWeight(6); strokeWeight(6);
point(centerPoint.x, centerPoint.y); point(centerPoint.x, centerPoint.y);
System.out.println("x-coordinate: " + Float.toString(centerPoint.x)); System.out.println("x-coordinate: " + Float.toString(centerPoint.x));
System.out.println("y-coordinate: " + Float.toString(centerPoint.y)); System.out.println("y-coordinate: " + Float.toString(centerPoint.y));
} }
// Draw separator sphere projected to 2D // Draw separator sphere projected to 2D
if (sphere != null) { if (sphere != null) {
strokeWeight(6); strokeWeight(6);
shape(sphere); shape(sphere);
} }
shape(reset);
shape(calculate);
fill(50);
textSize(20);
text("Geometric Separators", 140, 20);
textSize(12);
text("Reset.", reset_x + 30, reset_y + 15);
text("Calculate.", calc_x + 25, calc_y + 15);
// Draw input
for (PVector point : rawInput) {
strokeWeight(8);
point(point.x, point.y);
strokeWeight(2);
}
} }

0 comments on commit 6d60e6d

Please sign in to comment.