Skip to content

Commit

Permalink
Jeddic#6 Prevent illegal reuse of the curve builder (which would caus…
Browse files Browse the repository at this point in the history
…e very weird behaviour)
  • Loading branch information
richardTingle committed Dec 18, 2021
1 parent e08906d commit 5f96488
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtAnchor{
public class CurveBuilderAtAnchor extends CurveBuilderPiece{

Curve curveBeingBuilt;
Vector2f controlPointIn;
Vector2f currentAnchor;
private final Curve curveBeingBuilt;
private final Vector2f controlPointIn;
private final Vector2f currentAnchor;

public CurveBuilderAtAnchor(Curve curveBeingBuilt, Vector2f controlPointIn, Vector2f currentAnchor){
this.curveBeingBuilt = curveBeingBuilt;
Expand All @@ -16,9 +16,9 @@ public CurveBuilderAtAnchor(Curve curveBeingBuilt, Vector2f controlPointIn, Vect
}

/**
* Adds a point that the curve will attempt to move towards but may not actually touch.
* Adds a point that the curve will attempt to move towards (but may not actually touch).
*
* The 2 control points are used to define a cubic Bézier curve between 2 anchors
* The 2 control points are used to define a cubic Bézier-like curve between 2 anchors
* @param x the next control point's x
* @param y the next control point's y
* @return a CurveBuilderAtControlPoint1 a part of the curve builder system
Expand All @@ -28,13 +28,14 @@ public CurveBuilderAtControlPoint1 controlPoint1( float x, float y ){
}

/**
* Adds a point that the curve will attempt to move towards but may not actually touch.
* Adds a point that the curve will attempt to move towards (but may not actually touch)
*
* The 2 control points are used to define a cubic Bézier curve between 2 anchors
* The 2 control points are used to define a cubic Bézier-like curve between 2 anchors
* @param nextControlPoint the control point
* @return a CurveBuilderAtControlPoint1 a part of the curve builder system
*/
public CurveBuilderAtControlPoint1 controlPoint1( Vector2f nextControlPoint ){
checkReuse();
return new CurveBuilderAtControlPoint1(curveBeingBuilt, controlPointIn, currentAnchor, nextControlPoint);
}

Expand All @@ -54,14 +55,17 @@ public CurveBuilderAtAnchor anchorPoint(float x, float y){
* @return a CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f nextAnchor ){
//no checkReuse() as the call to controlPoint1 will do that
//simulate a straight line using a Bézier curve
Vector2f midOne = currentAnchor.mult(2f/3).add(nextAnchor.mult(1f/3));
Vector2f midTwo = currentAnchor.mult(1f/3).add(nextAnchor.mult(2f/3));
return controlPoint1(midOne).controlPoint2(midTwo).anchorPoint(nextAnchor);
}

public Curve end(){
public Curve build(){
checkReuse();
curveBeingBuilt.addControlPoint(controlPointIn, currentAnchor, null);
return curveBeingBuilt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtControlPoint1{
public class CurveBuilderAtControlPoint1 extends CurveBuilderPiece{

Curve curveBeingBuilt;

Expand Down Expand Up @@ -32,6 +32,7 @@ public CurveBuilderAtControlPoint2 controlPoint2( float x, float y ){
* @return a CurveBuilderAtControlPoint1 a part of the curve builder system
*/
public CurveBuilderAtControlPoint2 controlPoint2( Vector2f nextControlPoint ){
checkReuse();
return new CurveBuilderAtControlPoint2(curveBeingBuilt, nextControlPoint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtControlPoint2{
public class CurveBuilderAtControlPoint2 extends CurveBuilderPiece{

Curve curveBeingBuilt;
Vector2f inControlPoint;
Expand Down Expand Up @@ -33,6 +33,7 @@ public CurveBuilderAtAnchor anchorPoint(float x, float y){
* @return a CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f nextAnchor ){
checkReuse();
return new CurveBuilderAtAnchor(curveBeingBuilt, inControlPoint, nextAnchor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.epaga.particles.valuetypes.curvebuilder;

public class CurveBuilderPiece{

boolean used = false;

protected void checkReuse(){
if (used){
throw new IllegalStateException("Curve builders must not be reused (As they actually build a single curve as they go along)");
}
used = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;

public class CurveBuilderStart{
public class CurveBuilderStart extends CurveBuilderPiece{

Curve curveBeingBuilt = new Curve();

Expand All @@ -17,6 +17,7 @@ public CurveBuilderAtAnchor anchorPoint(float x, float y){
* @return CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f start){
checkReuse();
return new CurveBuilderAtAnchor(curveBeingBuilt, null, start);
}

Expand Down
19 changes: 15 additions & 4 deletions src/test/java/com/epaga/particles/valuetypes/CurveTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.epaga.particles.valuetypes;

import com.epaga.particles.valuetypes.curvebuilder.CurveBuilderAtAnchor;
import org.junit.Test;

import static org.junit.Assert.*;
Expand All @@ -11,7 +12,7 @@ public void builder_straightLine(){
Curve curve = Curve.builder()
.anchorPoint(0,0)
.anchorPoint(1,10)
.end();
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(4, curve.getValue(0.4f), 0.001);
Expand All @@ -27,7 +28,7 @@ public void builder_doubleStraightLine(){
.anchorPoint(0,0)
.anchorPoint(0.4f,10)
.anchorPoint(1f, 10)
.end();
.build();

assertEquals(0, curve.getValue(0f), 0.001);
assertEquals(5, curve.getValue(0.2f), 0.001);
Expand All @@ -48,7 +49,7 @@ public void builder_curve(){
.controlPoint1(0.2f, 1)
.controlPoint2(0.8f, 0)
.anchorPoint(1,1)
.end();
.build();

//expected values obtained using https://www.desmos.com/calculator/ebdtbxgbq0

Expand All @@ -71,4 +72,14 @@ public void builder_curve(){
assertEquals(0.5, curve.getValue(0.5f), 0.001);
assertEquals(1, curve.getValue(1), 0.001);
}
}

@Test(expected = IllegalStateException.class)
public void builder_reuseLeadsToException(){
CurveBuilderAtAnchor builder = Curve.builder()
.anchorPoint(0,0);

Curve legalUse = builder.build();
Curve illegalReuse = builder.build();
}

}

0 comments on commit 5f96488

Please sign in to comment.