Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Servlet generates the points for the curve and adds them as JSON to t…
…he response. Need to implement error handling for when processing the file upload fails.
  • Loading branch information
pdz10001 committed Oct 30, 2016
1 parent 719859e commit 585f4ed
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 42 deletions.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -29,6 +29,7 @@
<classpathentry kind="lib" path="libs/org.hamcrest.core_1.3.0.v201303031735.jar"/>
<classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-fileupload-1.3.2.jar"/>
<classpathentry kind="lib" path="WebContent/WEB-INF/lib/commons-io-2.5.jar"/>
<classpathentry kind="lib" path="WebContent/WEB-INF/lib/core.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
Binary file added WebContent/WEB-INF/lib/core.jar
Binary file not shown.
105 changes: 105 additions & 0 deletions junit_tests/com/knotrenderer/utiltest/RandomTest.java
@@ -0,0 +1,105 @@
package com.knotrenderer.utiltest;

import java.util.ArrayList;

import org.junit.Test;

import com.knotrenderer.model.BezierCurve;
import com.knotrenderer.model.Curve;
import com.knotrenderer.model.LineSegment;
import com.knotrenderer.util.CurveLoader;

import processing.core.PVector;

public class RandomTest {
private static final String CURVE_PATH = "D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-C1.curve";
private static final String CURVE_PATH2 = "D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-C2.curve";

private static final Curve CURVE = CurveLoader.loadCurveFromFile(CURVE_PATH);
private static final BezierCurve BEZIER_CURVE = new BezierCurve(CURVE.getKnotPoints());

private static final Curve CURVE2 = CurveLoader.loadCurveFromFile(CURVE_PATH2);
private static final BezierCurve BEZIER_CURVE2 = new BezierCurve(CURVE2.getKnotPoints());


private static PVector vector1 = new PVector(0f, 0f, 0f);
private static PVector vector2 = new PVector(0.31f, 0.81f, 1.00f);
private static PVector vector3 = new PVector(-2.1f, -2.7f, 1.90f);
private static PVector vector4 = new PVector(-1.9f, -3.4f, 0.91f);
private static PVector vector5 = new PVector(0.19f, 0.78f, 0.19f);
private static PVector vector6 = new PVector(1.56f, 2.32f, -0.96f);
private static PVector vector7 = new PVector(0f, 0f, 0f);

private static PVector vector8 = new PVector(0f, 0f, 0f);
private static PVector vector9 = new PVector(0.0305f, 0.0810f, 0.9962f);
private static PVector vector10 = new PVector(-0.2074f, -0.2671f, 1.9030f);
private static PVector vector11 = new PVector(-0.1792f, -0.3402f, 0.9063f);
private static PVector vector12 = new PVector(0.0189f, 0.0782f, 0.0185f);
private static PVector vector13 = new PVector(0.1557f, 0.2329f, -0.9600f);
private static PVector vector14 = new PVector(0f, 0f, 0f);


private static ArrayList<PVector> knotPoints = new ArrayList<PVector>();
static{
knotPoints.add(vector1);
knotPoints.add(vector2);
knotPoints.add(vector3);
knotPoints.add(vector4);
knotPoints.add(vector5);
knotPoints.add(vector6);
knotPoints.add(vector7);
}

private static ArrayList<PVector> knotPoints_2 = new ArrayList<PVector>();
static{
knotPoints_2.add(vector8);
knotPoints_2.add(vector9);
knotPoints_2.add(vector10);
knotPoints_2.add(vector11);
knotPoints_2.add(vector12);
knotPoints_2.add(vector13);
knotPoints_2.add(vector14);
}

@Test
public void testAll()
{
System.out.println("Curve 1:");
testOne_A();
testOne_B();
System.out.println("Curve 2:");
testTwo_A();
testTwo_B();
}
@Test
public void testOne_A()
{
testCurve(1, BEZIER_CURVE, 0.7031f);
}

@Test
public void testOne_B()
{
testCurve(2, BEZIER_CURVE2, 0.7031f);
}

@Test
public void testTwo_A()
{
testCurve(1, BEZIER_CURVE, 0.0633f);
}

@Test
public void testTwo_B()
{
testCurve(2, BEZIER_CURVE2, 0.0633f);
}

private void testCurve(int curvePos, BezierCurve curve, double curvePointToEvaluate)
{
System.out.println("Curve "+ curvePos + " Recursive Value at " + curvePointToEvaluate +": " + curve.getCurveValueRecursively(curvePointToEvaluate));
// System.out.println("Curve "+ curvePos + " Summation Value at " + curvePointToEvaluate +": " + curve.getCurveValueFromSummationExpansion(curvePointToEvaluate));

}

}
24 changes: 24 additions & 0 deletions src/com/knotrenderer/model/BezierCurve.java
Expand Up @@ -34,6 +34,11 @@ public class BezierCurve
}
}

/**
* PDZ- DOESN'T WORK AT THE MOMENT. NEEDS TO BE TWEAKED
* @param t
* @return
*/
public PVector getCurveValueFromSummationExpansion(double t)
{
double n = coefficients.length;
Expand Down Expand Up @@ -101,4 +106,23 @@ public class BezierCurve
PVector rightRecurseBranch = getCurveValueRecursively(parametrizedPoint, leftControlPointIndex + 1, rightControlPointIndex).mult((float)parametrizedPoint);
return leftRecurseBranch.add(rightRecurseBranch);
}

/**
* Evaluates and return an array of all the points on the curve at the given spacing. The smaller
* the spacing the more points and the more accurate the curve will be. The space cannot be bigger
* than 1 since it is evaluated using the parametrized curve.
* @param spacing- double: the spacing of the points to be evaluated at (must be less than 1)
* @return {@link ArrayList<{@link PVector}>} of all points at the spacings
*/
public ArrayList<PVector> generateAllDrawPoints(double spacing)
{
ArrayList<PVector> bezierPoints = new ArrayList<>();

for(double i = 0; i <= 1; i+= spacing)
{
PVector bezierPoint = this.getCurveValueRecursively(i);
bezierPoints.add(bezierPoint);
}
return bezierPoints;
}
}
52 changes: 51 additions & 1 deletion src/com/knotrenderer/util/CurveLoader.java
Expand Up @@ -3,6 +3,9 @@ package com.knotrenderer.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import com.knotrenderer.model.Curve;
Expand Down Expand Up @@ -32,7 +35,7 @@ public class CurveLoader
//TODO: handle when a line has more or less than 3 coordinates on it (meaning display to the user when it isn't correct)
if(splitString.length != 3)
{
throw new Exception("File line wasn't the correct lenght. It should have exactly 3 numbers on it.");
throw new Exception("File line wasn't the correct length. It should have exactly 3 numbers on it.");
}
else
{
Expand All @@ -56,4 +59,51 @@ public class CurveLoader

return new Curve(knotPoints, path);
}

public static Curve loadCurveFromInputStream(InputStream inputStream)
{
ArrayList<PVector> knotPoints = new ArrayList<PVector>();

try{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line;
while ((line = bufferedReader.readLine()) != null)
{
if(line.equals("") || line.replaceAll("\\s+", "").startsWith("%"))
continue;
else
{
//TODO: handle the case where you have a comment at the end of the line
line = line.replaceAll("\\s+", "");
String[] splitString = line.split(",");

//TODO: handle when a line has more or less than 3 coordinates on it (meaning display to the user when it isn't correct)
if(splitString.length != 3)
{
throw new Exception("File line wasn't the correct length. It should have exactly 3 numbers on it.");
}
else
{
//TODO: Figure out why there is some precision loss here and how to fix it (compare file to values read in)
float x = Float.parseFloat(splitString[0]);
float y = Float.parseFloat(splitString[1]);
float z = Float.parseFloat(splitString[2]);

System.out.println("X is: " + x + " Y is: " + y + " Z is: " + z);
knotPoints.add(new PVector(x, y, z));
}

}
}
bufferedReader.close();
}
catch(Exception e)
{
e.printStackTrace();
}

//TODO: PDZ- We are passing an empty string as a path for now b/c it doens't exist on the server. Need to change how the next curve iteration is generated in the Curve class so that we don't have to
//Instantiate a curve with a path
return new Curve(knotPoints, "");
}
}
10 changes: 8 additions & 2 deletions src/com/knotrenderer/view/KnotRenderer.java
@@ -1,5 +1,8 @@
package com.knotrenderer.view;

import java.sql.Date;
import java.text.SimpleDateFormat;

import com.knotrenderer.model.Curve;
import com.knotrenderer.util.CurveLoader;

Expand Down Expand Up @@ -52,13 +55,16 @@ public class KnotRenderer extends PApplet

// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4th-C1-high-precision.curve");
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-3.curve");
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/test.curve");
Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-C2.curve");
Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot.curve");
// Curve curve = CurveLoader.loadCurveFromFile("D:/Peter/College_Grad_School/Independent_Study_Spring_2016_Cont/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-2.curve");


// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot.curve");
// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/test.curve");
// Curve curve = CurveLoader.loadCurveFromFile("/Users/peterzaffetti/UC/Knot-Renderer-Processing/Testing_Files/TJP-4_1_stick-unknot-halved-3.curve");
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
System.out.println("Program Started at " + formatter.format(date));
renderCurve = new RenderCurve(this, curve);
}

Expand Down
27 changes: 0 additions & 27 deletions src/com/knotrenderer/view/KnotRendererApplet.java

This file was deleted.

12 changes: 8 additions & 4 deletions src/com/knotrenderer/view/RenderCurve.java
@@ -1,5 +1,8 @@
package com.knotrenderer.view;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -129,11 +132,12 @@ public class RenderCurve
return;
}

recursive = bezierCurve.getCurveValueRecursively(0.25);
summation = bezierCurve.getCurveValueFromSummationExpansion(0.25);
// recursive = bezierCurve.getCurveValueRecursively(0.25);
// summation = bezierCurve.getCurveValueFromSummationExpansion(0.25);

System.out.println("Recurisve = " + recursive);
System.out.println("Summation = " + summation);
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
System.out.println("Thread joined in at " + formatter.format(date));

applet.stroke(255, 0, 0);
applet.scale(scale);
Expand Down

0 comments on commit 585f4ed

Please sign in to comment.