diff --git a/jme3-blender/src/main/java/com/jme3/scene/plugins/blender/curves/CurvesTemporalMesh.java b/jme3-blender/src/main/java/com/jme3/scene/plugins/blender/curves/CurvesTemporalMesh.java index 51a5aeb638..a838ab8c25 100644 --- a/jme3-blender/src/main/java/com/jme3/scene/plugins/blender/curves/CurvesTemporalMesh.java +++ b/jme3-blender/src/main/java/com/jme3/scene/plugins/blender/curves/CurvesTemporalMesh.java @@ -12,9 +12,11 @@ import java.util.logging.Logger; import com.jme3.material.RenderState.FaceCullMode; +import com.jme3.math.BezierSpline; import com.jme3.math.FastMath; +import com.jme3.math.LinearSpline; +import com.jme3.math.NurbSpline; import com.jme3.math.Spline; -import com.jme3.math.Spline.SplineType; import com.jme3.math.Vector3f; import com.jme3.math.Vector4f; import com.jme3.scene.VertexBuffer.Type; @@ -273,7 +275,7 @@ private void loadBezierCurve(Structure nurbStructure, int materialIndex) throws controlPoints.remove(controlPoints.size() - 1); // creating curve - Curve curve = new Curve(new Spline(SplineType.Bezier, controlPoints, 0, false), resolution); + Curve curve = new Curve(new BezierSpline(controlPoints, false), resolution); FloatBuffer vertsBuffer = (FloatBuffer) curve.getBuffer(Type.Position).getData(); beziers.add(new BezierLine(BufferUtils.getVector3Array(vertsBuffer), materialIndex, smooth, cyclic)); @@ -344,7 +346,7 @@ private void loadNurbSurface(Structure nurb, int materialIndex) throws BlenderFi int originalVerticesAmount = vertices.size(); int resolu = ((Number) nurb.getFieldValue("resolu")).intValue(); if (knots[1] == null) {// creating the NURB curve - Curve curve = new Curve(new Spline(controlPoints.get(0), knots[0]), resolu); + Curve curve = new Curve(new NurbSpline(controlPoints.get(0), knots[0]), resolu); FloatBuffer vertsBuffer = (FloatBuffer) curve.getBuffer(Type.Position).getData(); beziers.add(new BezierLine(BufferUtils.getVector3Array(vertsBuffer), materialIndex, smooth, false)); } else {// creating the NURB surface @@ -506,9 +508,9 @@ private CurvesTemporalMesh loadBevelObject(Structure curveStructure) throws Blen } } - bevelCurve = new Curve(new Spline(SplineType.Bezier, conrtolPoints, 0, false), bevResol); + bevelCurve = new Curve(new BezierSpline(conrtolPoints, false), bevResol); } else if (extrude > 0.0f) { - Spline bevelSpline = new Spline(SplineType.Linear, new Vector3f[] { new Vector3f(0, extrude, 0), new Vector3f(0, -extrude, 0) }, 1, false); + Spline bevelSpline = new LinearSpline(new Vector3f[] { new Vector3f(0, extrude, 0), new Vector3f(0, -extrude, 0) }, false); bevelCurve = new Curve(bevelSpline, bevResol); } if (bevelCurve != null) { diff --git a/jme3-core/src/main/java/com/jme3/cinematic/MotionPath.java b/jme3-core/src/main/java/com/jme3/cinematic/MotionPath.java index 9eef4b8d3d..af9105d1e0 100644 --- a/jme3-core/src/main/java/com/jme3/cinematic/MotionPath.java +++ b/jme3-core/src/main/java/com/jme3/cinematic/MotionPath.java @@ -31,13 +31,22 @@ */ package com.jme3.cinematic; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + import com.jme3.asset.AssetManager; import com.jme3.cinematic.events.MotionEvent; -import com.jme3.export.*; +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; +import com.jme3.export.Savable; import com.jme3.material.Material; +import com.jme3.math.CatmullRomSpline; import com.jme3.math.ColorRGBA; import com.jme3.math.Spline; -import com.jme3.math.Spline.SplineType; import com.jme3.math.Vector2f; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; @@ -45,10 +54,6 @@ import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Curve; import com.jme3.util.TempVars; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; /** * Motion path is used to create a path between way points. @@ -59,7 +64,7 @@ public class MotionPath implements Savable { private Node debugNode; private AssetManager assetManager; private List listeners; - private Spline spline = new Spline(); + private Spline spline = new CatmullRomSpline(); int prevWayPoint = 0; /** @@ -126,17 +131,22 @@ private void attachDebugNode(Node root) { debugNode.attachChild(geo); } - switch (spline.getType()) { - case CatmullRom: - debugNode.attachChild(CreateCatmullRomPath()); - break; - case Linear: - debugNode.attachChild(CreateLinearPath()); - break; - default: - debugNode.attachChild(CreateLinearPath()); - break; + if(spline instanceof CatmullRomSpline) { + debugNode.attachChild(CreateCatmullRomPath()); + } else { + debugNode.attachChild(CreateLinearPath()); } +// switch (spline.getType()) { +// case CatmullRom: +// debugNode.attachChild(CreateCatmullRomPath()); +// break; +// case Linear: +// debugNode.attachChild(CreateLinearPath()); +// break; +// default: +// debugNode.attachChild(CreateLinearPath()); +// break; +// } root.attachChild(debugNode); } @@ -253,17 +263,21 @@ public Iterator iterator() { * return the type of spline used for the path interpolation for this path * @return the path interpolation spline type */ - public SplineType getPathSplineType() { - return spline.getType(); - } +// public SplineType getPathSplineType() { +// return spline.getType(); +// } /** * sets the type of spline used for the path interpolation for this path * @param pathSplineType */ - public void setPathSplineType(SplineType pathSplineType) { - spline.setType(pathSplineType); - if (debugNode != null) { +// public void setPathSplineType(SplineType pathSplineType) { +// spline.setType(pathSplineType); +// changeDebugNodeAfterSplinePropertiesChange(); +// } + + private void changeDebugNodeAfterSplinePropertiesChange() { + if (debugNode != null) { Node parent = debugNode.getParent(); debugNode.removeFromParent(); debugNode.detachAllChildren(); @@ -336,7 +350,11 @@ public void triggerWayPointReach(int wayPointIndex, MotionEvent control) { * @return */ public float getCurveTension() { - return spline.getCurveTension(); + if(spline instanceof CatmullRomSpline) { + return ((CatmullRomSpline)spline).getCurveTension(); + } else { + return 0; + } } /** @@ -344,14 +362,10 @@ public float getCurveTension() { * @param curveTension */ public void setCurveTension(float curveTension) { - spline.setCurveTension(curveTension); - if (debugNode != null) { - Node parent = debugNode.getParent(); - debugNode.removeFromParent(); - debugNode.detachAllChildren(); - debugNode = null; - attachDebugNode(parent); - } + if(spline instanceof CatmullRomSpline) { + ((CatmullRomSpline)spline).setCurveTension(curveTension); + changeDebugNodeAfterSplinePropertiesChange(); + } } public void clearWayPoints() { @@ -363,16 +377,8 @@ public void clearWayPoints() { * @param cycle */ public void setCycle(boolean cycle) { - spline.setCycle(cycle); - if (debugNode != null) { - Node parent = debugNode.getParent(); - debugNode.removeFromParent(); - debugNode.detachAllChildren(); - debugNode = null; - attachDebugNode(parent); - } - + changeDebugNodeAfterSplinePropertiesChange(); } /** @@ -386,4 +392,9 @@ public boolean isCycle() { public Spline getSpline() { return spline; } + + public void setSpline(Spline spline) { + this.spline = spline; + changeDebugNodeAfterSplinePropertiesChange(); + } } diff --git a/jme3-core/src/main/java/com/jme3/math/BezierSpline.java b/jme3-core/src/main/java/com/jme3/math/BezierSpline.java new file mode 100644 index 0000000000..d226351ab4 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/BezierSpline.java @@ -0,0 +1,115 @@ +package com.jme3.math; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; + +public class BezierSpline extends Spline{ + + public BezierSpline(List controlPoints, boolean cycle) { + super(controlPoints, cycle); + } + + public BezierSpline(Vector3f[] controlPoints, boolean cycle) { + super(Arrays.asList(controlPoints), cycle); + } + + @Override + public Vector3f interpolate(float value, int currentControlPoint, Vector3f store) { + if (store == null) { + store = new Vector3f(); + } + FastMath.interpolateBezier(value, controlPoints.get(currentControlPoint), controlPoints.get(currentControlPoint + 1), controlPoints.get(currentControlPoint + 2), controlPoints.get(currentControlPoint + 3), store); + return store; + } + + @Override + public void computeTotalLentgh() { + prepareTotalLengthComputation(); + computeBezierLength(); + } + + /** + * This method calculates the Bezier curve length. + */ + private void computeBezierLength() { + float l = 0; + if (controlPoints.size() > 1) { + for (int i = 0; i < controlPoints.size() - 1; i+=3) { + l = FastMath.getBezierP1toP2Length(controlPoints.get(i), + controlPoints.get(i + 1), controlPoints.get(i + 2), controlPoints.get(i + 3)); + segmentsLength.add(l); + totalLength += l; + } + } + } + + @Override + public float[] getPositionArrayForMesh(int nbSubSegments) { + Vector3f temp = new Vector3f(); + if (nbSubSegments == 0) { + nbSubSegments = 1; + } + int centerPointsAmount = (this.getControlPoints().size() + 2) / 3; + + //calculating vertices + float[] array = new float[((centerPointsAmount - 1) * nbSubSegments + 1) * 3]; + int currentControlPoint = 0; + List controlPoints = this.getControlPoints(); + int lineIndex = 0; + for (int i = 0; i < centerPointsAmount - 1; ++i) { + Vector3f vector3f = controlPoints.get(currentControlPoint); + array[lineIndex++] = vector3f.x; + array[lineIndex++] = vector3f.y; + array[lineIndex++] = vector3f.z; + for (int j = 1; j < nbSubSegments; ++j) { + this.interpolate((float) j / nbSubSegments, currentControlPoint, temp); + array[lineIndex++] = temp.getX(); + array[lineIndex++] = temp.getY(); + array[lineIndex++] = temp.getZ(); + } + currentControlPoint += 3; + } + Vector3f vector3f = controlPoints.get(currentControlPoint); + array[lineIndex++] = vector3f.x; + array[lineIndex++] = vector3f.y; + array[lineIndex++] = vector3f.z; + + return array; + } + + @Override + public short[] getIndicesArrayForMesh(int nbSubSegments) { + if (nbSubSegments == 0) { + nbSubSegments = 1; + } + int centerPointsAmount = (this.getControlPoints().size() + 2) / 3; + int i = 0, k; + short[] indices = new short[(centerPointsAmount - 1) * nbSubSegments << 1]; + for (int j = 0; j < (centerPointsAmount - 1) * nbSubSegments; ++j) { + k = j; + indices[i++] = (short) k; + ++k; + indices[i++] = (short) k; + } + return indices; + } + + @Override + public void write(JmeExporter ex) throws IOException { + OutputCapsule oc = ex.getCapsule(this); + SplineSavable.write(oc, this); + } + + @Override + public void read(JmeImporter im) throws IOException { + InputCapsule in = im.getCapsule(this); + SplineSavable.read(in, this); + } + +} diff --git a/jme3-core/src/main/java/com/jme3/math/CatmullRomSpline.java b/jme3-core/src/main/java/com/jme3/math/CatmullRomSpline.java new file mode 100644 index 0000000000..f878c6a29e --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/CatmullRomSpline.java @@ -0,0 +1,185 @@ +package com.jme3.math; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; + +public class CatmullRomSpline extends Spline{ + + private List CRcontrolPoints; + private float curveTension = 0.5f; + + public CatmullRomSpline() {} + + public CatmullRomSpline(Spline spline) { + super(spline); + } + + public CatmullRomSpline(List controlPoints, float curveTension, boolean cycle) { + super(controlPoints, cycle); + this.curveTension = curveTension; + } + + public CatmullRomSpline(Vector3f[] controlPoints, float curveTension, boolean cycle) { + super(Arrays.asList(controlPoints), cycle); + this.curveTension = curveTension; + } + + public CatmullRomSpline(List controlPoints, boolean cycle) { + super(controlPoints, cycle); + } + + public CatmullRomSpline(Vector3f[] controlPoints, boolean cycle) { + super(Arrays.asList(controlPoints), cycle); + } + + @Override + public Vector3f interpolate(float value, int currentControlPoint, Vector3f store) { + if (store == null) { + store = new Vector3f(); + } + FastMath.interpolateCatmullRom(value, curveTension, CRcontrolPoints.get(currentControlPoint), CRcontrolPoints.get(currentControlPoint + 1), CRcontrolPoints.get(currentControlPoint + 2), CRcontrolPoints.get(currentControlPoint + 3), store); + return store; + } + + @Override + public void computeTotalLentgh() { + prepareTotalLengthComputation(); + initCatmullRomWayPoints(controlPoints); + computeCatmullRomLength(); + } + + public float getCurveTension() { + return curveTension; + } + + /** + * sets the curve tension + * + * @param curveTension the tension + */ + public void setCurveTension(float curveTension) { + this.curveTension = curveTension; + if(!controlPoints.isEmpty()) { + computeTotalLentgh(); + } + } + + private void initCatmullRomWayPoints(List list) { + if (CRcontrolPoints == null) { + CRcontrolPoints = new ArrayList(); + } else { + CRcontrolPoints.clear(); + } + int nb = list.size() - 1; + + if (cycle) { + CRcontrolPoints.add(list.get(list.size() - 2)); + } else { + CRcontrolPoints.add(list.get(0).subtract(list.get(1).subtract(list.get(0)))); + } + + for (Iterator it = list.iterator(); it.hasNext();) { + Vector3f vector3f = it.next(); + CRcontrolPoints.add(vector3f); + } + if (cycle) { + CRcontrolPoints.add(list.get(1)); + } else { + CRcontrolPoints.add(list.get(nb).add(list.get(nb).subtract(list.get(nb - 1)))); + } + + } + + private void computeCatmullRomLength() { + float l = 0; + if (controlPoints.size() > 1) { + for (int i = 0; i < controlPoints.size() - 1; i++) { + l = FastMath.getCatmullRomP1toP2Length(CRcontrolPoints.get(i), + CRcontrolPoints.get(i + 1), CRcontrolPoints.get(i + 2), CRcontrolPoints.get(i + 3), 0, 1, curveTension); + segmentsLength.add(l); + totalLength += l; + } + } + } + + public List getCRcontrolPoints() { + return CRcontrolPoints; + } + + public void setCRcontrolPoints(List cRcontrolPoints) { + CRcontrolPoints = cRcontrolPoints; + } + + @Override + public float[] getPositionArrayForMesh(int nbSubSegments) { + Vector3f temp = new Vector3f(); + float[] array = new float[((this.getControlPoints().size() - 1) * nbSubSegments + 1) * 3]; + int i = 0; + int cptCP = 0; + for (Iterator it = this.getControlPoints().iterator(); it.hasNext();) { + Vector3f vector3f = it.next(); + array[i] = vector3f.x; + i++; + array[i] = vector3f.y; + i++; + array[i] = vector3f.z; + i++; + if (it.hasNext()) { + for (int j = 1; j < nbSubSegments; j++) { + this.interpolate((float) j / nbSubSegments, cptCP, temp); + array[i] = temp.getX(); + i++; + array[i] = temp.getY(); + i++; + array[i] = temp.getZ(); + i++; + } + } + cptCP++; + } + return array; + } + + @Override + public short[] getIndicesArrayForMesh(int nbSubSegments) { + short[] indices = new short[(this.getControlPoints().size() - 1) * nbSubSegments * 2]; + int i = 0; + int k; + for (int j = 0; j < (this.getControlPoints().size() - 1) * nbSubSegments; j++) { + k = j; + indices[i] = (short) k; + i++; + k++; + indices[i] = (short) k; + i++; + } + return indices; + } + + @SuppressWarnings("rawtypes") + @Override + public void write(JmeExporter ex) throws IOException { + OutputCapsule oc = ex.getCapsule(this); + SplineSavable.write(oc, this); + oc.writeSavableArrayList((ArrayList) CRcontrolPoints, "CRControlPoints", null); + oc.write(curveTension, "curveTension", 0.5f); + } + + @SuppressWarnings("unchecked") + @Override + public void read(JmeImporter im) throws IOException { + InputCapsule in = im.getCapsule(this); + SplineSavable.read(in, this); + CRcontrolPoints = (ArrayList) in.readSavableArrayList("CRControlPoints", null); + curveTension = in.readFloat("curveTension", 0.5f); + } + +} diff --git a/jme3-core/src/main/java/com/jme3/math/CurveAndSurfaceMath.java b/jme3-core/src/main/java/com/jme3/math/CurveAndSurfaceMath.java index 522821e035..608cceba42 100644 --- a/jme3-core/src/main/java/com/jme3/math/CurveAndSurfaceMath.java +++ b/jme3-core/src/main/java/com/jme3/math/CurveAndSurfaceMath.java @@ -31,7 +31,6 @@ */ package com.jme3.math; -import com.jme3.math.Spline.SplineType; import java.util.List; /** @@ -46,7 +45,7 @@ public class CurveAndSurfaceMath { * class. */ private CurveAndSurfaceMath() {} - + /** * This method interpolates the data for the nurbs curve. * @param u @@ -56,10 +55,7 @@ private CurveAndSurfaceMath() {} * @param store * the resulting point in 3D space */ - public static void interpolateNurbs(float u, Spline nurbSpline, Vector3f store) { - if (nurbSpline.getType() != SplineType.Nurb) { - throw new IllegalArgumentException("Given spline is not of a NURB type!"); - } + public static void interpolateNurbs(float u, NurbSpline nurbSpline, Vector3f store) { List controlPoints = nurbSpline.getControlPoints(); float[] weights = nurbSpline.getWeights(); List knots = nurbSpline.getKnots(); diff --git a/jme3-core/src/main/java/com/jme3/math/LinearSpline.java b/jme3-core/src/main/java/com/jme3/math/LinearSpline.java new file mode 100644 index 0000000000..029da9b255 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/LinearSpline.java @@ -0,0 +1,98 @@ +package com.jme3.math; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; + +public class LinearSpline extends Spline{ + + public LinearSpline(Spline spline) { + super(spline); + } + + public LinearSpline(List controlPoints, boolean cycle) { + super(controlPoints, cycle); + } + + public LinearSpline(Vector3f[] controlPoints, boolean cycle) { + super(Arrays.asList(controlPoints), cycle); + } + + @Override + public Vector3f interpolate(float value, int currentControlPoint, Vector3f store) { + if (store == null) { + store = new Vector3f(); + } + FastMath.interpolateLinear(value, controlPoints.get(currentControlPoint), controlPoints.get(currentControlPoint + 1), store); + return store; + } + + @Override + public void computeTotalLentgh() { + prepareTotalLengthComputation(); + float l = 0; + if (controlPoints.size() > 1) { + for (int i = 0; i < controlPoints.size() - 1; i++) { + l = controlPoints.get(i + 1).subtract(controlPoints.get(i)).length(); + segmentsLength.add(l); + totalLength += l; + } + } + } + + @Override + public float[] getPositionArrayForMesh(int nbSubSegments) { + float[] array = new float[this.getControlPoints().size() * 3]; + int i = 0; + for (Iterator it = this.getControlPoints().iterator(); it.hasNext();) { + Vector3f vector3f = it.next(); + array[i] = vector3f.getX(); + i++; + array[i] = vector3f.getY(); + i++; + array[i] = vector3f.getZ(); + i++; + } + return array; + } + + @Override + public short[] getIndicesArrayForMesh(int nbSubSegments) { + short[] indices = new short[(this.getControlPoints().size() - 1) * 2]; + int cpt = 0; + int k; + int j = 0; + for (Iterator it = this.getControlPoints().iterator(); it.hasNext();) { + it.next(); + if (it.hasNext()) { + k = j; + indices[cpt] = (short) k; + cpt++; + k++; + indices[cpt] = (short) k; + cpt++; + j++; + } + } + return indices; + } + + @Override + public void write(JmeExporter ex) throws IOException { + OutputCapsule oc = ex.getCapsule(this); + SplineSavable.write(oc, this); + } + + @Override + public void read(JmeImporter im) throws IOException { + InputCapsule in = im.getCapsule(this); + SplineSavable.read(in, this); + } + +} diff --git a/jme3-core/src/main/java/com/jme3/math/NurbSpline.java b/jme3-core/src/main/java/com/jme3/math/NurbSpline.java new file mode 100644 index 0000000000..4e1c0f6c16 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/NurbSpline.java @@ -0,0 +1,154 @@ +package com.jme3.math; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; + +public class NurbSpline extends Spline{ + + private List knots; //knots of NURBS spline + private float[] weights; //weights of NURBS spline + private int basisFunctionDegree; //degree of NURBS spline basis function (computed automatically) + + public NurbSpline(List controlPoints, List nurbKnots) { + //input data control + for(int i=0;inurbKnots.get(i+1)) { + throw new IllegalArgumentException("The knots values cannot decrease!"); + } + } + //storing the data + this.weights = new float[controlPoints.size()]; + this.knots = nurbKnots; + this.basisFunctionDegree = nurbKnots.size() - weights.length; + for(int i=0;i getKnots() { + return knots; + } + + public void setKnots(List knots) { + this.knots = knots; + } + + public float[] getWeights() { + return weights; + } + + public void setWeights(float[] weights) { + this.weights = weights; + } + + public int getBasisFunctionDegree() { + return basisFunctionDegree; + } + + public void setBasisFunctionDegree(int basisFunctionDegree) { + this.basisFunctionDegree = basisFunctionDegree; + } + + @Override + public float[] getPositionArrayForMesh(int nbSubSegments) { + if(nbSubSegments == 0) { + nbSubSegments = this.getControlPoints().size() + 1; + } else { + nbSubSegments = this.getControlPoints().size() * nbSubSegments + 1; + } + float minKnot = this.getMinNurbKnot(); + float maxKnot = this.getMaxNurbKnot(); + float deltaU = (maxKnot - minKnot) / nbSubSegments; + + float[] array = new float[(nbSubSegments + 1) * 3]; + + float u = minKnot; + Vector3f interpolationResult = new Vector3f(); + for (int i = 0; i < array.length; i += 3) { + this.interpolate(u, 0, interpolationResult); + array[i] = interpolationResult.x; + array[i + 1] = interpolationResult.y; + array[i + 2] = interpolationResult.z; + u += deltaU; + } + return array; + } + + @Override + public short[] getIndicesArrayForMesh(int nbSubSegments) { + int i = 0; + short[] indices = new short[nbSubSegments << 1]; + for (int j = 0; j < nbSubSegments; ++j) { + indices[i++] = (short) j; + indices[i++] = (short) (j + 1); + } + return indices; + } + + @Override + public void write(JmeExporter ex) throws IOException { + OutputCapsule oc = ex.getCapsule(this); + SplineSavable.write(oc, this); + oc.writeSavableArrayList((ArrayList)knots, "knots", null); + oc.write(weights, "weights", null); + oc.write(basisFunctionDegree, "basisFunctionDegree", 0); + } + + @SuppressWarnings("unchecked") + @Override + public void read(JmeImporter im) throws IOException { + InputCapsule in = im.getCapsule(this); + SplineSavable.read(in, this); + knots = in.readSavableArrayList("knots", null); + weights = in.readFloatArray("weights", null); + basisFunctionDegree = in.readInt("basisFunctionDegree", 0); + + } + +} diff --git a/jme3-core/src/main/java/com/jme3/math/Spline.java b/jme3-core/src/main/java/com/jme3/math/Spline.java index 041baa0bad..b79025b4fc 100644 --- a/jme3-core/src/main/java/com/jme3/math/Spline.java +++ b/jme3-core/src/main/java/com/jme3/math/Spline.java @@ -1,178 +1,40 @@ -/* - * Copyright (c) 2009-2012 jMonkeyEngine - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * * Neither the name of 'jMonkeyEngine' nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ package com.jme3.math; -import com.jme3.export.*; -import java.io.IOException; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -/** - * - * @author Nehon - */ -public class Spline implements Savable { - - public enum SplineType { - Linear, - CatmullRom, - Bezier, - Nurb - } - - private List controlPoints = new ArrayList(); - private List knots; //knots of NURBS spline - private float[] weights; //weights of NURBS spline - private int basisFunctionDegree; //degree of NURBS spline basis function (computed automatically) - private boolean cycle; - private List segmentsLength; - private float totalLength; - private List CRcontrolPoints; - private float curveTension = 0.5f; - private SplineType type = SplineType.CatmullRom; - - public Spline() { - } - - /** - * Create a spline - * @param splineType the type of the spline @see {SplineType} - * @param controlPoints an array of vector to use as control points of the spline - * If the type of the curve is Bezier curve the control points should be provided - * in the appropriate way. Each point 'p' describing control position in the scene - * should be surrounded by two handler points. This applies to every point except - * for the border points of the curve, who should have only one handle point. - * The pattern should be as follows: - * P0 - H0 : H1 - P1 - H1 : ... : Hn - Pn - * - * n is the amount of 'P' - points. - * @param curveTension the tension of the spline - * @param cycle true if the spline cycle. - */ - public Spline(SplineType splineType, Vector3f[] controlPoints, float curveTension, boolean cycle) { - if(splineType==SplineType.Nurb) { - throw new IllegalArgumentException("To create NURBS spline use: 'public Spline(Vector3f[] controlPoints, float[] weights, float[] nurbKnots)' constructor!"); - } - for (int i = 0; i < controlPoints.length; i++) { - Vector3f vector3f = controlPoints[i]; - this.controlPoints.add(vector3f); - } - type = splineType; - this.curveTension = curveTension; +import com.jme3.export.Savable; + +public abstract class Spline implements SplineInterface, Savable{ + + protected List controlPoints = new ArrayList(); + protected List segmentsLength; + protected float totalLength; + protected boolean cycle = false; + + protected Spline() { } + + protected Spline(List controlPoints, boolean cycle) { + this.controlPoints.addAll(controlPoints); this.cycle = cycle; this.computeTotalLentgh(); - } - - /** - * Create a spline - * @param splineType the type of the spline @see {SplineType} - * @param controlPoints a list of vector to use as control points of the spline - * If the type of the curve is Bezier curve the control points should be provided - * in the appropriate way. Each point 'p' describing control position in the scene - * should be surrounded by two handler points. This applies to every point except - * for the border points of the curve, who should have only one handle point. - * The pattern should be as follows: - * P0 - H0 : H1 - P1 - H1 : ... : Hn - Pn - * - * n is the amount of 'P' - points. - * @param curveTension the tension of the spline - * @param cycle true if the spline cycle. - */ - public Spline(SplineType splineType, List controlPoints, float curveTension, boolean cycle) { - if(splineType==SplineType.Nurb) { - throw new IllegalArgumentException("To create NURBS spline use: 'public Spline(Vector3f[] controlPoints, float[] weights, float[] nurbKnots)' constructor!"); - } - type = splineType; - this.controlPoints.addAll(controlPoints); - this.curveTension = curveTension; - this.cycle = cycle; - this.computeTotalLentgh(); - } - - /** - * Create a NURBS spline. A spline type is automatically set to SplineType.Nurb. - * The cycle is set to false by default. - * @param controlPoints a list of vector to use as control points of the spline - * @param nurbKnots the nurb's spline knots - */ - public Spline(List controlPoints, List nurbKnots) { - //input data control - for(int i=0;inurbKnots.get(i+1)) { - throw new IllegalArgumentException("The knots values cannot decrease!"); - } - } - - //storing the data - type = SplineType.Nurb; - this.weights = new float[controlPoints.size()]; - this.knots = nurbKnots; - this.basisFunctionDegree = nurbKnots.size() - weights.length; - for(int i=0;i list) { - if (CRcontrolPoints == null) { - CRcontrolPoints = new ArrayList(); - } else { - CRcontrolPoints.clear(); - } - int nb = list.size() - 1; - - if (cycle) { - CRcontrolPoints.add(list.get(list.size() - 2)); - } else { - CRcontrolPoints.add(list.get(0).subtract(list.get(1).subtract(list.get(0)))); - } - - for (Iterator it = list.iterator(); it.hasNext();) { - Vector3f vector3f = it.next(); - CRcontrolPoints.add(vector3f); - } - if (cycle) { - CRcontrolPoints.add(list.get(1)); + } + + protected void prepareTotalLengthComputation() { + totalLength = 0; + if (segmentsLength == null) { + segmentsLength = new ArrayList(); } else { - CRcontrolPoints.add(list.get(nb).add(list.get(nb).subtract(list.get(nb - 1)))); + segmentsLength.clear(); } - - } - + } + /** * Adds a controlPoint to the spline * @param controlPoint a position in world space @@ -205,270 +67,47 @@ public void clearControlPoints(){ controlPoints.clear(); totalLength = 0; } - - /** - * This method computes the total length of the curve. - */ - private void computeTotalLentgh() { - totalLength = 0; - float l = 0; - if (segmentsLength == null) { - segmentsLength = new ArrayList(); - } else { - segmentsLength.clear(); - } - if (type == SplineType.Linear) { - if (controlPoints.size() > 1) { - for (int i = 0; i < controlPoints.size() - 1; i++) { - l = controlPoints.get(i + 1).subtract(controlPoints.get(i)).length(); - segmentsLength.add(l); - totalLength += l; - } - } - } else if(type == SplineType.Bezier) { - this.computeBezierLength(); - } else if(type == SplineType.Nurb) { - this.computeNurbLength(); - } else { - this.initCatmullRomWayPoints(controlPoints); - this.computeCatmulLength(); - } - } - - /** - * This method computes the Catmull Rom curve length. - */ - private void computeCatmulLength() { - float l = 0; - if (controlPoints.size() > 1) { - for (int i = 0; i < controlPoints.size() - 1; i++) { - l = FastMath.getCatmullRomP1toP2Length(CRcontrolPoints.get(i), - CRcontrolPoints.get(i + 1), CRcontrolPoints.get(i + 2), CRcontrolPoints.get(i + 3), 0, 1, curveTension); - segmentsLength.add(l); - totalLength += l; - } - } - } - /** - * This method calculates the Bezier curve length. - */ - private void computeBezierLength() { - float l = 0; - if (controlPoints.size() > 1) { - for (int i = 0; i < controlPoints.size() - 1; i+=3) { - l = FastMath.getBezierP1toP2Length(controlPoints.get(i), - controlPoints.get(i + 1), controlPoints.get(i + 2), controlPoints.get(i + 3)); - segmentsLength.add(l); - totalLength += l; - } - } - } - - /** - * This method calculates the NURB curve length. - */ - private void computeNurbLength() { - //TODO: implement - } - - /** - * Iterpolate a position on the spline - * @param value a value from 0 to 1 that represent the postion between the curent control point and the next one - * @param currentControlPoint the current control point - * @param store a vector to store the result (use null to create a new one that will be returned by the method) - * @return the position - */ - public Vector3f interpolate(float value, int currentControlPoint, Vector3f store) { - if (store == null) { - store = new Vector3f(); - } - switch (type) { - case CatmullRom: - FastMath.interpolateCatmullRom(value, curveTension, CRcontrolPoints.get(currentControlPoint), CRcontrolPoints.get(currentControlPoint + 1), CRcontrolPoints.get(currentControlPoint + 2), CRcontrolPoints.get(currentControlPoint + 3), store); - break; - case Linear: - FastMath.interpolateLinear(value, controlPoints.get(currentControlPoint), controlPoints.get(currentControlPoint + 1), store); - break; - case Bezier: - FastMath.interpolateBezier(value, controlPoints.get(currentControlPoint), controlPoints.get(currentControlPoint + 1), controlPoints.get(currentControlPoint + 2), controlPoints.get(currentControlPoint + 3), store); - break; - case Nurb: - CurveAndSurfaceMath.interpolateNurbs(value, this, store); - break; - default: - break; - } - return store; - } - - /** - * returns the curve tension - */ - public float getCurveTension() { - return curveTension; - } - - /** - * sets the curve tension - * - * @param curveTension the tension - */ - public void setCurveTension(float curveTension) { - this.curveTension = curveTension; - if(type==SplineType.CatmullRom && !getControlPoints().isEmpty()) { - this.computeTotalLentgh(); - } - } - - /** - * returns true if the spline cycle - */ - public boolean isCycle() { - return cycle; - } - - /** - * set to true to make the spline cycle - * @param cycle - */ - public void setCycle(boolean cycle) { - if(type!=SplineType.Nurb) { - if (controlPoints.size() >= 2) { - if (this.cycle && !cycle) { - controlPoints.remove(controlPoints.size() - 1); - } - if (!this.cycle && cycle) { - controlPoints.add(controlPoints.get(0)); - } - this.cycle = cycle; - this.computeTotalLentgh(); - } else { - this.cycle = cycle; - } - } - } - - /** - * return the total lenght of the spline - */ - public float getTotalLength() { - return totalLength; - } - - /** - * return the type of the spline - */ - public SplineType getType() { - return type; - } - - /** - * Sets the type of the spline - * @param type - */ - public void setType(SplineType type) { - this.type = type; - this.computeTotalLentgh(); - } - - /** - * returns this spline control points - */ public List getControlPoints() { - return controlPoints; - } + return controlPoints; + } - /** - * returns a list of float representing the segments lenght - */ - public List getSegmentsLength() { - return segmentsLength; - } - - //////////// NURBS getters ///////////////////// - - /** - * This method returns the minimum nurb curve knot value. Check the nurb type before calling this method. It the curve is not of a Nurb - * type - NPE will be thrown. - * @return the minimum nurb curve knot value - */ - public float getMinNurbKnot() { - return knots.get(basisFunctionDegree - 1); - } - - /** - * This method returns the maximum nurb curve knot value. Check the nurb type before calling this method. It the curve is not of a Nurb - * type - NPE will be thrown. - * @return the maximum nurb curve knot value - */ - public float getMaxNurbKnot() { - return knots.get(weights.length); - } - - /** - * This method returns NURBS' spline knots. - * @return NURBS' spline knots - */ - public List getKnots() { - return knots; + public void setControlPoints(List controlPoints) { + this.controlPoints = controlPoints; } - - /** - * This method returns NURBS' spline weights. - * @return NURBS' spline weights - */ - public float[] getWeights() { - return weights; + + public List getSegmentsLength() { + return segmentsLength; } - - /** - * This method returns NURBS' spline basis function degree. - * @return NURBS' spline basis function degree - */ - public int getBasisFunctionDegree() { - return basisFunctionDegree; + + public void setSegmentsLength(List segmentsLength) { + this.segmentsLength = segmentsLength; } - @Override - public void write(JmeExporter ex) throws IOException { - OutputCapsule oc = ex.getCapsule(this); - oc.writeSavableArrayList((ArrayList) controlPoints, "controlPoints", null); - oc.write(type, "type", SplineType.CatmullRom); - float list[] = new float[segmentsLength.size()]; - for (int i = 0; i < segmentsLength.size(); i++) { - list[i] = segmentsLength.get(i); - } - oc.write(list, "segmentsLength", null); + public float getTotalLength() { + return totalLength; + } - oc.write(totalLength, "totalLength", 0); - oc.writeSavableArrayList((ArrayList) CRcontrolPoints, "CRControlPoints", null); - oc.write(curveTension, "curveTension", 0.5f); - oc.write(cycle, "cycle", false); - oc.writeSavableArrayList((ArrayList)knots, "knots", null); - oc.write(weights, "weights", null); - oc.write(basisFunctionDegree, "basisFunctionDegree", 0); - } + public void setTotalLength(float totalLength) { + this.totalLength = totalLength; + } - @Override - public void read(JmeImporter im) throws IOException { - InputCapsule in = im.getCapsule(this); + public boolean isCycle() { + return cycle; + } - controlPoints = (ArrayList) in.readSavableArrayList("wayPoints", null); - float list[] = in.readFloatArray("segmentsLength", null); - if (list != null) { - segmentsLength = new ArrayList(); - for (int i = 0; i < list.length; i++) { - segmentsLength.add(new Float(list[i])); - } - } - type = in.readEnum("pathSplineType", SplineType.class, SplineType.CatmullRom); - totalLength = in.readFloat("totalLength", 0); - CRcontrolPoints = (ArrayList) in.readSavableArrayList("CRControlPoints", null); - curveTension = in.readFloat("curveTension", 0.5f); - cycle = in.readBoolean("cycle", false); - knots = in.readSavableArrayList("knots", null); - weights = in.readFloatArray("weights", null); - basisFunctionDegree = in.readInt("basisFunctionDegree", 0); - } + public void setCycle(boolean cycle) { + if (controlPoints.size() >= 2) { + if (this.cycle && !cycle) { + controlPoints.remove(controlPoints.size() - 1); + } + if (!this.cycle && cycle) { + controlPoints.add(controlPoints.get(0)); + } + this.cycle = cycle; + this.computeTotalLentgh(); + } else { + this.cycle = cycle; + } + } } diff --git a/jme3-core/src/main/java/com/jme3/math/SplineInterface.java b/jme3-core/src/main/java/com/jme3/math/SplineInterface.java new file mode 100644 index 0000000000..05764cb279 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/SplineInterface.java @@ -0,0 +1,11 @@ +package com.jme3.math; + +public interface SplineInterface { + public void addControlPoint(Vector3f controlPoint); + public void removeControlPoint(Vector3f controlPoint); + public void clearControlPoints(); + public Vector3f interpolate(float value, int currentControlPoint, Vector3f store); + public void computeTotalLentgh(); + public float[] getPositionArrayForMesh(int nbSubSegments); + public short[] getIndicesArrayForMesh(int nbSubSegments); +} diff --git a/jme3-core/src/main/java/com/jme3/math/SplineSavable.java b/jme3-core/src/main/java/com/jme3/math/SplineSavable.java new file mode 100644 index 0000000000..c0034cbc59 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/SplineSavable.java @@ -0,0 +1,36 @@ +package com.jme3.math; + +import java.io.IOException; +import java.util.ArrayList; + +import com.jme3.export.InputCapsule; +import com.jme3.export.OutputCapsule; + +public final class SplineSavable { + + @SuppressWarnings("rawtypes") + public static void write(OutputCapsule oc, Spline spline) throws IOException { + oc.writeSavableArrayList((ArrayList) spline.controlPoints, "controlPoints", null); + float list[] = new float[spline.segmentsLength.size()]; + for (int i = 0; i < spline.segmentsLength.size(); i++) { + list[i] = spline.segmentsLength.get(i); + } + oc.write(list, "segmentsLength", null); + oc.write(spline.totalLength, "totalLength", 0); + oc.write(spline.cycle, "cycle", false); + } + + @SuppressWarnings("unchecked") + public static void read(InputCapsule in, Spline spline) throws IOException { + spline.controlPoints = (ArrayList) in.readSavableArrayList("wayPoints", null); + float list[] = in.readFloatArray("segmentsLength", null); + if (list != null) { + spline.segmentsLength = new ArrayList(); + for (int i = 0; i < list.length; i++) { + spline.segmentsLength.add(new Float(list[i])); + } + } + spline.totalLength = in.readFloat("totalLength", 0); + spline.cycle = in.readBoolean("cycle", false); + } +} diff --git a/jme3-core/src/main/java/com/jme3/math/SplineType.java b/jme3-core/src/main/java/com/jme3/math/SplineType.java new file mode 100644 index 0000000000..3d0b1fc4a9 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/math/SplineType.java @@ -0,0 +1,8 @@ +package com.jme3.math; + +public enum SplineType { + Linear, + CatmullRom, + Bezier, + Nurb +} diff --git a/jme3-core/src/main/java/com/jme3/renderer/IDList.java b/jme3-core/src/main/java/com/jme3/renderer/IDList.java index 70fea4218d..7d2b7627fb 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/IDList.java +++ b/jme3-core/src/main/java/com/jme3/renderer/IDList.java @@ -39,10 +39,10 @@ */ public class IDList { - public int[] newList = new int[16]; - public int[] oldList = new int[16]; - public int newLen = 0; - public int oldLen = 0; + private int[] newList = new int[16]; + private int[] oldList = new int[16]; + private int newLen = 0; + private int oldLen = 0; /** * Reset all states to zero @@ -92,29 +92,68 @@ public void copyNewToOld(){ oldLen = newLen; newLen = 0; } + + /** + * @return the newList + */ + public int[] getNewList() { + return newList; + } - /** - * Prints the contents of the lists - */ - public void print(){ - if (newLen > 0){ - System.out.print("New List: "); - for (int i = 0; i < newLen; i++){ - if (i == newLen -1) - System.out.println(newList[i]); - else - System.out.print(newList[i]+", "); - } - } - if (oldLen > 0){ - System.out.print("Old List: "); - for (int i = 0; i < oldLen; i++){ - if (i == oldLen -1) - System.out.println(oldList[i]); - else - System.out.print(oldList[i]+", "); - } - } - } + /** + * @param newList the newList to set + */ + public void setNewList(int[] newList) { + this.newList = newList; + } + + /** + * @return the oldList + */ + public int[] getOldList() { + return oldList; + } + + /** + * @param oldList the oldList to set + */ + public void setOldList(int[] oldList) { + this.oldList = oldList; + } + + /** + * @return the newLen + */ + public int getNewLen() { + return newLen; + } + + /** + * @param newLen the newLen to set + */ + public void setNewLen(int newLen) { + this.newLen = newLen; + } + + /** + * @return the oldLen + */ + public int getOldLen() { + return oldLen; + } + /** + * @param oldLen the oldLen to set + */ + public void setOldLen(int oldLen) { + this.oldLen = oldLen; + } + + public int getOldListElem(int i){ + return oldList[i]; + } + + public int getNewListElem(int i){ + return newList[i]; + } } diff --git a/jme3-core/src/main/java/com/jme3/renderer/IDListPlainTextPrinter.java b/jme3-core/src/main/java/com/jme3/renderer/IDListPlainTextPrinter.java new file mode 100644 index 0000000000..01a1230e2f --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/renderer/IDListPlainTextPrinter.java @@ -0,0 +1,44 @@ +/** + * + */ +package com.jme3.renderer; + +/** + * Printer for IDList. + * @author Tr0k + * + */ +public class IDListPlainTextPrinter implements IListPrinter { + + private IDList idList; + + public IDListPlainTextPrinter(IDList idList) { + this.idList = idList; + } + + /** + * Prints the contents of the lists + */ + @Override + public void printList() { + if (idList.getNewLen() > 0){ + System.out.print("New List: "); + for (int i = 0; i < idList.getNewLen(); i++){ + if (i == idList.getNewLen() -1) + System.out.println(idList.getNewListElem(i)); + else + System.out.print(idList.getNewListElem(i)+", "); + } + } + if (idList.getOldLen() > 0){ + System.out.print("Old List: "); + for (int i = 0; i < idList.getOldLen(); i++){ + if (i == idList.getOldLen() -1) + System.out.println(idList.getOldListElem(i)); + else + System.out.print(idList.getOldListElem(i)+", "); + } + } + } + +} diff --git a/jme3-core/src/main/java/com/jme3/renderer/IListPrinter.java b/jme3-core/src/main/java/com/jme3/renderer/IListPrinter.java new file mode 100644 index 0000000000..1bdc8e5038 --- /dev/null +++ b/jme3-core/src/main/java/com/jme3/renderer/IListPrinter.java @@ -0,0 +1,10 @@ +package com.jme3.renderer; + +/** + * Deliver methods for printing list. + * @author Tr0k + * + */ +public interface IListPrinter { + void printList(); +} diff --git a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java index 50e603d2f1..6c9103ac87 100644 --- a/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java +++ b/jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java @@ -2312,8 +2312,8 @@ public void deleteBuffer(VertexBuffer vb) { public void clearVertexAttribs() { IDList attribList = context.attribIndexList; - for (int i = 0; i < attribList.oldLen; i++) { - int idx = attribList.oldList[i]; + for (int i = 0; i < attribList.getOldLen(); i++) { + int idx = attribList.getOldListElem(i); gl.glDisableVertexAttribArray(idx); if (context.boundAttribs[idx].isInstanced()) { glext.glVertexAttribDivisorARB(idx, 0); diff --git a/jme3-core/src/main/java/com/jme3/scene/shape/Curve.java b/jme3-core/src/main/java/com/jme3/scene/shape/Curve.java index d6eb8dc209..d3d16beb9b 100644 --- a/jme3-core/src/main/java/com/jme3/scene/shape/Curve.java +++ b/jme3-core/src/main/java/com/jme3/scene/shape/Curve.java @@ -31,12 +31,12 @@ */ package com.jme3.scene.shape; +import com.jme3.math.CatmullRomSpline; +import com.jme3.math.NurbSpline; import com.jme3.math.Spline; import com.jme3.math.Vector3f; import com.jme3.scene.Mesh; import com.jme3.scene.VertexBuffer; -import java.util.Iterator; -import java.util.List; /** * A @@ -50,7 +50,6 @@ public class Curve extends Mesh { private Spline spline; - private Vector3f temp = new Vector3f(); /** * Serialization only. Do not use. @@ -65,7 +64,7 @@ public Curve() { * @param nbSubSegments the number of subsegments between the control points */ public Curve(Vector3f[] controlPoints, int nbSubSegments) { - this(new Spline(Spline.SplineType.CatmullRom, controlPoints, 10, false), nbSubSegments); + this(new CatmullRomSpline(controlPoints, 10, false), nbSubSegments); } /** @@ -77,196 +76,13 @@ public Curve(Vector3f[] controlPoints, int nbSubSegments) { public Curve(Spline spline, int nbSubSegments) { super(); this.spline = spline; - switch (spline.getType()) { - case CatmullRom: - this.createCatmullRomMesh(nbSubSegments); - break; - case Bezier: - this.createBezierMesh(nbSubSegments); - break; - case Nurb: - this.createNurbMesh(nbSubSegments); - break; - case Linear: - default: - this.createLinearMesh(); - break; - } - } - - private void createCatmullRomMesh(int nbSubSegments) { - float[] array = new float[((spline.getControlPoints().size() - 1) * nbSubSegments + 1) * 3]; - short[] indices = new short[(spline.getControlPoints().size() - 1) * nbSubSegments * 2]; - int i = 0; - int cptCP = 0; - for (Iterator it = spline.getControlPoints().iterator(); it.hasNext();) { - Vector3f vector3f = it.next(); - array[i] = vector3f.x; - i++; - array[i] = vector3f.y; - i++; - array[i] = vector3f.z; - i++; - if (it.hasNext()) { - for (int j = 1; j < nbSubSegments; j++) { - spline.interpolate((float) j / nbSubSegments, cptCP, temp); - array[i] = temp.getX(); - i++; - array[i] = temp.getY(); - i++; - array[i] = temp.getZ(); - i++; - } - } - cptCP++; - } - - i = 0; - int k; - for (int j = 0; j < (spline.getControlPoints().size() - 1) * nbSubSegments; j++) { - k = j; - indices[i] = (short) k; - i++; - k++; - indices[i] = (short) k; - i++; - } - - this.setMode(Mesh.Mode.Lines); - this.setBuffer(VertexBuffer.Type.Position, 3, array); - this.setBuffer(VertexBuffer.Type.Index, 2, indices);//(spline.getControlPoints().size() - 1) * nbSubSegments * 2 - this.updateBound(); - this.updateCounts(); - } - - /** - * This method creates the Bezier path for this curve. - * - * @param nbSubSegments amount of subsegments between position control - * points - */ - private void createBezierMesh(int nbSubSegments) { - if (nbSubSegments == 0) { - nbSubSegments = 1; - } - int centerPointsAmount = (spline.getControlPoints().size() + 2) / 3; - - //calculating vertices - float[] array = new float[((centerPointsAmount - 1) * nbSubSegments + 1) * 3]; - int currentControlPoint = 0; - List controlPoints = spline.getControlPoints(); - int lineIndex = 0; - for (int i = 0; i < centerPointsAmount - 1; ++i) { - Vector3f vector3f = controlPoints.get(currentControlPoint); - array[lineIndex++] = vector3f.x; - array[lineIndex++] = vector3f.y; - array[lineIndex++] = vector3f.z; - for (int j = 1; j < nbSubSegments; ++j) { - spline.interpolate((float) j / nbSubSegments, currentControlPoint, temp); - array[lineIndex++] = temp.getX(); - array[lineIndex++] = temp.getY(); - array[lineIndex++] = temp.getZ(); - } - currentControlPoint += 3; - } - Vector3f vector3f = controlPoints.get(currentControlPoint); - array[lineIndex++] = vector3f.x; - array[lineIndex++] = vector3f.y; - array[lineIndex++] = vector3f.z; - - //calculating indexes - int i = 0, k; - short[] indices = new short[(centerPointsAmount - 1) * nbSubSegments << 1]; - for (int j = 0; j < (centerPointsAmount - 1) * nbSubSegments; ++j) { - k = j; - indices[i++] = (short) k; - ++k; - indices[i++] = (short) k; - } - - this.setMode(Mesh.Mode.Lines); - this.setBuffer(VertexBuffer.Type.Position, 3, array); - this.setBuffer(VertexBuffer.Type.Index, 2, indices); - this.updateBound(); - this.updateCounts(); - } - - /** - * This method creates the Nurb path for this curve. - * - * @param nbSubSegments amount of subsegments between position control - * points - */ - private void createNurbMesh(int nbSubSegments) { - if(spline.getControlPoints() != null && spline.getControlPoints().size() > 0) { - if(nbSubSegments == 0) { - nbSubSegments = spline.getControlPoints().size() + 1; - } else { - nbSubSegments = spline.getControlPoints().size() * nbSubSegments + 1; - } - float minKnot = spline.getMinNurbKnot(); - float maxKnot = spline.getMaxNurbKnot(); - float deltaU = (maxKnot - minKnot) / nbSubSegments; - - float[] array = new float[(nbSubSegments + 1) * 3]; - - float u = minKnot; - Vector3f interpolationResult = new Vector3f(); - for (int i = 0; i < array.length; i += 3) { - spline.interpolate(u, 0, interpolationResult); - array[i] = interpolationResult.x; - array[i + 1] = interpolationResult.y; - array[i + 2] = interpolationResult.z; - u += deltaU; - } - - //calculating indexes - int i = 0; - short[] indices = new short[nbSubSegments << 1]; - for (int j = 0; j < nbSubSegments; ++j) { - indices[i++] = (short) j; - indices[i++] = (short) (j + 1); - } - - this.setMode(Mesh.Mode.Lines); - this.setBuffer(VertexBuffer.Type.Position, 3, array); - this.setBuffer(VertexBuffer.Type.Index, 2, indices); + if(!(spline instanceof NurbSpline) || (spline.getControlPoints()!= null && spline.getControlPoints().size() > 0)) { + this.setMode(Mesh.Mode.Lines); + this.setBuffer(VertexBuffer.Type.Position, 3, spline.getPositionArrayForMesh(nbSubSegments)); + this.setBuffer(VertexBuffer.Type.Index, 2, spline.getIndicesArrayForMesh(nbSubSegments));//(spline.getControlPoints().size() - 1) * nbSubSegments * 2 this.updateBound(); this.updateCounts(); - } - } - - private void createLinearMesh() { - float[] array = new float[spline.getControlPoints().size() * 3]; - short[] indices = new short[(spline.getControlPoints().size() - 1) * 2]; - int i = 0; - int cpt = 0; - int k; - int j = 0; - for (Iterator it = spline.getControlPoints().iterator(); it.hasNext();) { - Vector3f vector3f = it.next(); - array[i] = vector3f.getX(); - i++; - array[i] = vector3f.getY(); - i++; - array[i] = vector3f.getZ(); - i++; - if (it.hasNext()) { - k = j; - indices[cpt] = (short) k; - cpt++; - k++; - indices[cpt] = (short) k; - cpt++; - j++; - } } - - this.setMode(Mesh.Mode.Lines); - this.setBuffer(VertexBuffer.Type.Position, 3, array); - this.setBuffer(VertexBuffer.Type.Index, 2, indices); - this.updateBound(); - this.updateCounts(); } /** @@ -277,4 +93,5 @@ private void createLinearMesh() { public float getLength() { return spline.getTotalLength(); } + } diff --git a/jme3-core/src/main/java/com/jme3/scene/shape/Surface.java b/jme3-core/src/main/java/com/jme3/scene/shape/Surface.java index a359c6d4fd..4129620e7f 100644 --- a/jme3-core/src/main/java/com/jme3/scene/shape/Surface.java +++ b/jme3-core/src/main/java/com/jme3/scene/shape/Surface.java @@ -31,20 +31,20 @@ */ package com.jme3.scene.shape; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.jme3.math.CurveAndSurfaceMath; import com.jme3.math.FastMath; -import com.jme3.math.Spline.SplineType; +import com.jme3.math.SplineType; import com.jme3.math.Vector3f; import com.jme3.math.Vector4f; import com.jme3.scene.Mesh; import com.jme3.scene.VertexBuffer; import com.jme3.util.BufferUtils; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * This class represents a surface described by knots, weights and control points. * Currently the following types are supported: diff --git a/jme3-core/src/main/resources/com/jme3/system/version.properties b/jme3-core/src/main/resources/com/jme3/system/version.properties deleted file mode 100644 index 98168a14e1..0000000000 --- a/jme3-core/src/main/resources/com/jme3/system/version.properties +++ /dev/null @@ -1,11 +0,0 @@ -# THIS IS AN AUTO-GENERATED FILE.. -# DO NOT MODIFY! -build.date=1900-01-01 -git.revision=0 -git.branch=unknown -git.hash= -git.hash.short= -git.tag= -name.full=jMonkeyEngine 3.1.0-UNKNOWN -version.number=3.1.0 -version.tag=SNAPSHOT \ No newline at end of file diff --git a/jme3-core/src/test/java/com/jme3/cinematic/MotionPathTest.java b/jme3-core/src/test/java/com/jme3/cinematic/MotionPathTest.java new file mode 100644 index 0000000000..9caf2270e9 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/cinematic/MotionPathTest.java @@ -0,0 +1,57 @@ +package com.jme3.cinematic; + +import static com.jme3.math.DataGeneratorForSplineTest.initSampleVectorList; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.mockito.Mockito; + +import com.jme3.asset.AssetKey; +import com.jme3.asset.AssetManager; +import com.jme3.material.MatParam; +import com.jme3.material.MaterialDef; +import com.jme3.math.BezierSpline; +import com.jme3.math.LinearSpline; +import com.jme3.math.Spline; +import com.jme3.math.Vector3f; +import com.jme3.scene.Node; + +public class MotionPathTest { + +// @Test +// public void test() { +// MotionPath motionPath = getPreparedMotionPathForTests(); +// motionPath.setPathSplineType(SplineType.Linear); +// assertTrue(motionPath.getSpline().getType().equals(SplineType.Linear)); +// motionPath.setPathSplineType(SplineType.Bezier); +// assertTrue(motionPath.getSpline().getType().equals(SplineType.Bezier)); +// motionPath.setPathSplineType(SplineType.CatmullRom); +// assertTrue(motionPath.getSpline().getType().equals(SplineType.CatmullRom)); +// } + + @Test + public void testAfterRefactoring() { + MotionPath motionPath = getPreparedMotionPathForTests(); + Spline testSpline = new LinearSpline(initSampleVectorList(), false); + motionPath.setSpline(testSpline); + assertTrue(motionPath.getSpline() instanceof LinearSpline); + testSpline = new BezierSpline(initSampleVectorList(), false); + motionPath.setSpline(testSpline); + assertTrue(motionPath.getSpline() instanceof BezierSpline); + } + + private MotionPath getPreparedMotionPathForTests() { + MotionPath motionPath = new MotionPath(); + motionPath.getSpline().addControlPoint(new Vector3f(0,0,0)); + motionPath.getSpline().addControlPoint(new Vector3f(1,1,1)); + motionPath.getSpline().addControlPoint(new Vector3f(2,2,2)); + motionPath.getSpline().addControlPoint(new Vector3f(3,3,3)); + AssetManager assetManager = Mockito.mock(AssetManager.class); + MaterialDef materialDef = Mockito.mock(MaterialDef.class); + Mockito.when(materialDef.getMaterialParam("Color")).thenReturn(new MatParam()); + Mockito.when(assetManager.loadAsset(new AssetKey<>("Common/MatDefs/Misc/Unshaded.j3md"))).thenReturn(materialDef); + motionPath.enableDebugShape(assetManager, new Node()); + return motionPath; + } + +} diff --git a/jme3-core/src/test/java/com/jme3/math/CurveAndSurfaceMathTest.java b/jme3-core/src/test/java/com/jme3/math/CurveAndSurfaceMathTest.java new file mode 100644 index 0000000000..35c41ef4b0 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/CurveAndSurfaceMathTest.java @@ -0,0 +1,27 @@ +package com.jme3.math; + +import static com.jme3.math.DataGeneratorForSplineTest.initControlPoints; +import static com.jme3.math.DataGeneratorForSplineTest.initNurbKnots; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class CurveAndSurfaceMathTest { + +// @Test +// public void testInterpolateNurbsFloatNurbSplineVector3f() { +// Spline nurbSpline = new Spline(initControlPoints(), initNurbKnots()); +// Vector3f store = new Vector3f(); +// CurveAndSurfaceMath.interpolateNurbs(0.3f, nurbSpline, store); +// assertFalse(new Vector3f().equals(store)); +// } + + @Test + public void testInterpolateNurbsFloatNurbSplineVector3f() { + NurbSpline nurbSpline = new NurbSpline(initControlPoints(), initNurbKnots()); + Vector3f store = new Vector3f(); + CurveAndSurfaceMath.interpolateNurbs(0.3f, nurbSpline, store); + assertFalse(new Vector3f().equals(store)); + } + +} diff --git a/jme3-core/src/test/java/com/jme3/math/DataGeneratorForSplineTest.java b/jme3-core/src/test/java/com/jme3/math/DataGeneratorForSplineTest.java new file mode 100644 index 0000000000..4c9edddd15 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/DataGeneratorForSplineTest.java @@ -0,0 +1,38 @@ +package com.jme3.math; + +import java.util.ArrayList; +import java.util.List; + +public class DataGeneratorForSplineTest { + + public static List initSampleVectorList() { + List sampleList = new ArrayList<>(); + sampleList.add(new Vector3f(1,1,1)); + sampleList.add(new Vector3f(2,2,2)); + sampleList.add(new Vector3f(3,3,3)); + sampleList.add(new Vector3f(4,4,4)); + return sampleList; + } + + public static Vector3f[] initSampleVectorArray() { + return new Vector3f[] {new Vector3f(1,1,1), new Vector3f(2,2,2), new Vector3f(3,3,3), new Vector3f(4,4,4)}; + } + + public static List initControlPoints() { + List controlPoints = new ArrayList<>(); + controlPoints.add(new Vector4f(1,1,0,0.1f)); + controlPoints.add(new Vector4f(2,6,0,0.8f)); + controlPoints.add(new Vector4f(3,1,0,0.1f)); + return controlPoints; + } + + public static List initNurbKnots() { + List nurbKnots = new ArrayList<>(); + nurbKnots.add(0.0f); + nurbKnots.add(1.0f); + nurbKnots.add(2.0f); + nurbKnots.add(3.0f); + return nurbKnots; + } + +} diff --git a/jme3-core/src/test/java/com/jme3/math/SplineTest.java b/jme3-core/src/test/java/com/jme3/math/SplineTest.java new file mode 100644 index 0000000000..ad98b93038 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/math/SplineTest.java @@ -0,0 +1,664 @@ +package com.jme3.math; + +import static com.jme3.math.DataGeneratorForSplineTest.initControlPoints; +import static com.jme3.math.DataGeneratorForSplineTest.initNurbKnots; +import static com.jme3.math.DataGeneratorForSplineTest.initSampleVectorArray; +import static com.jme3.math.DataGeneratorForSplineTest.initSampleVectorList; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; + +import com.jme3.export.InputCapsule; +import com.jme3.export.JmeExporter; +import com.jme3.export.JmeImporter; +import com.jme3.export.OutputCapsule; + +public class SplineTest { + + Spline spline; + float tension = 0.3f; + boolean cycle = false; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { +// spline = new Spline(); + spline = new LinearSpline(initSampleVectorList(), cycle); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testConstructors() { + List sampleList = initSampleVectorList(); + spline = new LinearSpline(sampleList, cycle); + splineProperties(tension, cycle); + spline = null; + spline = new CatmullRomSpline(sampleList, tension, cycle); + splineProperties(tension, cycle); + assertNotNull(((CatmullRomSpline)spline).getCRcontrolPoints()); + spline = null; + spline = new BezierSpline(sampleList, cycle); + splineProperties(tension, cycle); + spline = null; + spline = new BezierSpline(initSampleVectorArray(), cycle); + splineProperties(tension, cycle); + Spline linearSpline = new LinearSpline(spline); + assertTrue(linearSpline.isCycle() == spline.isCycle()); + assertTrue(linearSpline.getControlPoints().equals(spline.getControlPoints())); + Spline catmullRomSpline = new CatmullRomSpline(linearSpline); + assertTrue(catmullRomSpline.isCycle() == linearSpline.isCycle()); + assertTrue(catmullRomSpline.getControlPoints().equals(linearSpline.getControlPoints())); + catmullRomSpline = null; + catmullRomSpline = new CatmullRomSpline(initSampleVectorArray(), cycle); + assertNotNull(catmullRomSpline); + } + + @Test(expected=AssertionError.class) + public void testNurbConstructor() { + spline = new NurbSpline(initControlPoints(), initNurbKnots()); + splineProperties(0.0f, spline.isCycle()); + assertNotNull(((NurbSpline)spline).getKnots()); + assertNotNull(((NurbSpline)spline).getWeights()); + assertTrue(((NurbSpline)spline).getKnots().size()-((NurbSpline)spline).getWeights().length == ((NurbSpline)spline).getBasisFunctionDegree()); + } + + private void splineProperties(float tension, boolean cycle) { + assertNotNull(spline); + assertTrue(spline.getControlPoints().size() > 0); + assertNotNull(spline.getSegmentsLength()); + assertTrue(spline.getSegmentsLength().size() > 0); + assertTrue(spline.getTotalLength() > 0); + assertTrue(spline.isCycle() == cycle); + } + + @Test + public void testSetters() { + spline = new LinearSpline(initSampleVectorArray(), false); + spline.setCycle(true); + spline.setControlPoints(null); + spline.setSegmentsLength(null); + spline.setTotalLength(0); + assertNull(spline.getControlPoints()); + assertNull(spline.getSegmentsLength()); + assertTrue(spline.isCycle()); + assertTrue(spline.getTotalLength()==0); + NurbSpline spline = new NurbSpline(initControlPoints(), initNurbKnots()); + spline.setBasisFunctionDegree(0); + spline.setKnots(null); + spline.setWeights(null); + assertNull(spline.getKnots()); + assertNull(spline.getWeights()); + assertTrue(spline.getBasisFunctionDegree()==0); + CatmullRomSpline catmullRomSpline = new CatmullRomSpline(initSampleVectorList(), tension, cycle); + catmullRomSpline.setCRcontrolPoints(null); + assertNull(catmullRomSpline.getCRcontrolPoints()); + } + + /** + * Test method for {@link com.jme3.math.Spline#Spline(com.jme3.math.Spline.SplineType, com.jme3.math.Vector3f[], float, boolean)}. + */ + @Test + public void splineSplineTypeVector3fArrayFloatBoolean() { + spline = new LinearSpline(initSampleVectorArray(), false); + assertNotNull(spline); + assertTrue(spline instanceof LinearSpline); + assertTrue(spline.isCycle() == false); + } + + /** + * Test method for {@link com.jme3.math.Spline#Spline(com.jme3.math.Spline.SplineType, java.util.List, float, boolean)}. + */ + @Test + public void splineSplineTypeListOfVector3fFloatBoolean() { + List sampleList = initSampleVectorList(); + spline = new LinearSpline(sampleList, false); + assertNotNull(spline); + assertTrue(spline instanceof LinearSpline); + assertTrue(spline.isCycle() == false); + } + + /** + * Test method for {@link com.jme3.math.Spline#Spline(java.util.List, java.util.List)}. + */ + @Test(expected = IllegalArgumentException.class) + public void splineListOfVector4fListOfFloat() { + List controlPoints = initControlPoints(); + List nurbKnots = initNurbKnots(); + spline = new NurbSpline(controlPoints, nurbKnots); + assertNotNull(spline); + assertTrue(spline instanceof NurbSpline); + nurbKnots.add(2.0f); + spline = new NurbSpline(controlPoints, nurbKnots); + } + + /** + * Test method for {@link com.jme3.math.Spline#addControlPoint(com.jme3.math.Vector3f)}. + */ + @Test + public void testAddControlPoint() { + List sampleList = initSampleVectorList(); + spline = new LinearSpline(sampleList, true); + spline.addControlPoint(new Vector3f()); + assertTrue(spline.getControlPoints().contains(new Vector3f())); + spline.clearControlPoints(); + spline.addControlPoint(new Vector3f()); + assertTrue(spline.getControlPoints().size()==1); + spline.addControlPoint(new Vector3f(1,1,1)); + assertTrue(spline.getTotalLength() > 0); + } + + /** + * Test method for {@link com.jme3.math.Spline#removeControlPoint(com.jme3.math.Vector3f)}. + */ + @Test + public void testOperationsOverControlPoint() { + spline = new NurbSpline(initControlPoints(), initNurbKnots()); + spline.addControlPoint(new Vector3f()); + assertTrue(spline.getControlPoints().contains(new Vector3f())); + spline.removeControlPoint(new Vector3f()); + assertFalse(spline.getControlPoints().contains(new Vector3f())); + spline.clearControlPoints(); + assertTrue(spline.getControlPoints().isEmpty()); + } + + /** + * Test method for {@link com.jme3.math.Spline#interpolate(float, int, com.jme3.math.Vector3f)}. + */ + @Test + public void testLinearInterpolate() { + spline = new LinearSpline(initSampleVectorList(), false); + Vector3f result = spline.interpolate(0.3f, 0, null); + assertTrue(result.equals(new Vector3f(1.3f, 1.3f, 1.3f))); + } + + @Test + public void testCatmullRomInterpolate() { + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, false); + Vector3f result = spline.interpolate(0.3f, 0, null); + assertTrue(result.equals(new Vector3f(1.3840001f, 1.3840001f, 1.3840001f))); + } + + @Test + public void testBezierInterpolate() { + spline = new BezierSpline(initSampleVectorList(), false); + Vector3f result = spline.interpolate(0.3f, 0, null); + assertTrue(result.equals(new Vector3f(1.9f, 1.9f, 1.9f))); + } + + @Test + public void testNurbInterpolate() { + spline = new NurbSpline(initControlPoints(), initNurbKnots()); + Vector3f result = spline.interpolate(0.3f, 0, null); + assertTrue(result.equals(new Vector3f(1.0f, 1.0f, 0.0f))); + } + + /** + * Test method for {@link com.jme3.math.Spline#setCurveTension(float)}. + */ + @Test + public void testSetCurveTension() { + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, false); + ((CatmullRomSpline)spline).setCurveTension(3.5f); + assertTrue(((CatmullRomSpline)spline).getCurveTension() == 3.5f); + } + + /** + * Test method for {@link com.jme3.math.Spline#setCycle(boolean)}. + */ + @Test + public void testSetCycle() { + spline = new NurbSpline(initControlPoints(), initNurbKnots()); + spline.setCycle(true); + assertFalse(spline.isCycle()); + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, false); + spline.setCycle(true); + assertTrue(spline.isCycle()); + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, true); + spline.setCycle(false); + assertFalse(spline.isCycle()); + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, true); + spline.setCycle(true); + assertTrue(spline.isCycle()); + spline = new CatmullRomSpline(initSampleVectorList(), 1.0f, false); + spline.setCycle(false); + assertFalse(spline.isCycle()); + spline.clearControlPoints(); + spline.setCycle(true); + assertTrue(spline.isCycle()); + } + + /** + * Test method for {@link com.jme3.math.Spline#getSegmentsLength()}. + */ + @Test + public void testGetSegmentsLength() { + spline = new LinearSpline(initSampleVectorList(), false); + assertTrue(spline.getSegmentsLength().size() != 0); + } + + /** + * Test method for {@link com.jme3.math.Spline#getMinNurbKnot()}. + */ + @Test + public void testGetMinNurbKnot() { + NurbSpline spline = new NurbSpline(initControlPoints(), initNurbKnots()); + assertTrue(spline.getMinNurbKnot() == spline.getKnots().get(spline.getBasisFunctionDegree() - 1)); + } + + /** + * Test method for {@link com.jme3.math.Spline#getMaxNurbKnot()}. + */ + @Test + public void testGetMaxNurbKnot() { + NurbSpline spline = new NurbSpline(initControlPoints(), initNurbKnots()); + assertTrue(spline.getMaxNurbKnot() == spline.getKnots().get(spline.getWeights().length)); + } + + /** + * Test method for {@link com.jme3.math.Spline#write(com.jme3.export.JmeExporter)}. + */ + @Test + public void testWriteLinearSpline() throws IOException{ + JmeExporter jmeExporter = Mockito.mock(JmeExporter.class); + OutputCapsule outputCapsule = Mockito.mock(OutputCapsule.class); + Mockito.when(jmeExporter.getCapsule(Mockito.any())).thenReturn(outputCapsule); + spline = new LinearSpline(initSampleVectorList(), false); + spline.write(jmeExporter); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyFloat(), Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.any(float[].class), Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(outputCapsule, Mockito.times(1)).writeSavableArrayList(Mockito.any(ArrayList.class), Mockito.anyString(), Mockito.any(ArrayList.class)); + } + + @Test + public void testWriteBezierSpline() throws IOException{ + JmeExporter jmeExporter = Mockito.mock(JmeExporter.class); + OutputCapsule outputCapsule = Mockito.mock(OutputCapsule.class); + Mockito.when(jmeExporter.getCapsule(Mockito.any())).thenReturn(outputCapsule); + spline = new BezierSpline(initSampleVectorList(), false); + spline.write(jmeExporter); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyFloat(), Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.any(float[].class), Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(outputCapsule, Mockito.times(1)).writeSavableArrayList(Mockito.any(ArrayList.class), Mockito.anyString(), Mockito.any(ArrayList.class)); + } + + @Test + public void testWriteCatmullRomSpline() throws IOException{ + JmeExporter jmeExporter = Mockito.mock(JmeExporter.class); + OutputCapsule outputCapsule = Mockito.mock(OutputCapsule.class); + Mockito.when(jmeExporter.getCapsule(Mockito.any())).thenReturn(outputCapsule); + spline = new CatmullRomSpline(initSampleVectorList(), false); + spline.write(jmeExporter); + Mockito.verify(outputCapsule, Mockito.times(2)).write(Mockito.anyFloat(), Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.any(float[].class), Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(outputCapsule, Mockito.times(2)).writeSavableArrayList(Mockito.any(ArrayList.class), Mockito.anyString(), Mockito.any(ArrayList.class)); + } + + @Test + public void testWriteNurbSpline() throws IOException{ + JmeExporter jmeExporter = Mockito.mock(JmeExporter.class); + OutputCapsule outputCapsule = Mockito.mock(OutputCapsule.class); + Mockito.when(jmeExporter.getCapsule(Mockito.any())).thenReturn(outputCapsule); + spline = new NurbSpline(initControlPoints(),initNurbKnots()); + spline.write(jmeExporter); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyFloat(), Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt()); + Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(outputCapsule, Mockito.times(2)).write(Mockito.any(float[].class), Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(outputCapsule, Mockito.times(2)).writeSavableArrayList(Mockito.any(ArrayList.class), Mockito.anyString(), Mockito.any(ArrayList.class)); + } + + /** + * Test method for {@link com.jme3.math.Spline#read(com.jme3.export.JmeImporter)}. + * @throws IOException + */ + @Test + public void testReadLinearSpline() throws IOException { + JmeImporter jmeImporter = Mockito.mock(JmeImporter.class); + InputCapsule inputCapsule = Mockito.mock(InputCapsule.class); + Mockito.when(jmeImporter.getCapsule(Mockito.any())).thenReturn(inputCapsule); + spline = new LinearSpline(initSampleVectorList(), false); + Mockito.when(inputCapsule.readFloatArray("segmentsLength", null)).thenReturn(new float[]{1.2f,2.3f}); + Mockito.when(inputCapsule.readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class))).thenReturn(null); + spline.read(jmeImporter); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloat(Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(inputCapsule, Mockito.times(1)).readBoolean(Mockito.anyString(), Mockito.anyBoolean()); +// Mockito.verify(inputCapsule, Mockito.times(1)).readInt(Mockito.anyString(), Mockito.anyInt()); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloatArray(Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(inputCapsule, Mockito.times(1)).readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class)); + assertNull(spline.getControlPoints()); + assertTrue(spline.getTotalLength()==0); + assertTrue(spline.getSegmentsLength().size()==2); + assertFalse(spline.isCycle()); + } + + @Test + public void testReadBezierSpline() throws IOException { + JmeImporter jmeImporter = Mockito.mock(JmeImporter.class); + InputCapsule inputCapsule = Mockito.mock(InputCapsule.class); + Mockito.when(jmeImporter.getCapsule(Mockito.any())).thenReturn(inputCapsule); + spline = new BezierSpline(initSampleVectorList(), false); + Mockito.when(inputCapsule.readFloatArray("segmentsLength", null)).thenReturn(new float[]{1.2f,2.3f}); + Mockito.when(inputCapsule.readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class))).thenReturn(null); + spline.read(jmeImporter); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloat(Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(inputCapsule, Mockito.times(1)).readBoolean(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloatArray(Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(inputCapsule, Mockito.times(1)).readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class)); + assertNull(spline.getControlPoints()); + assertTrue(spline.getTotalLength()==0); + assertTrue(spline.getSegmentsLength().size()==2); + assertFalse(spline.isCycle()); + } + + @Test + public void testReadCatmullRomSpline() throws IOException { + JmeImporter jmeImporter = Mockito.mock(JmeImporter.class); + InputCapsule inputCapsule = Mockito.mock(InputCapsule.class); + Mockito.when(jmeImporter.getCapsule(Mockito.any())).thenReturn(inputCapsule); + CatmullRomSpline spline = new CatmullRomSpline(initSampleVectorList(), false); + Mockito.when(inputCapsule.readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class))).thenReturn(null); + Mockito.when(inputCapsule.readFloatArray("segmentsLength", null)).thenReturn(new float[]{1.2f,2.3f}); + Mockito.when(inputCapsule.readFloat("curveTension", 0.5f)).thenReturn(0.5f); + spline.read(jmeImporter); + Mockito.verify(inputCapsule, Mockito.times(2)).readFloat(Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(inputCapsule, Mockito.times(1)).readBoolean(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloatArray(Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(inputCapsule, Mockito.times(2)).readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class)); + assertNull(spline.getControlPoints()); + assertTrue(spline.getTotalLength()==0); + assertTrue(spline.getSegmentsLength().size()==2); + assertFalse(spline.isCycle()); + assertTrue(spline.getCurveTension() == 0.5f); + assertNull(spline.getCRcontrolPoints()); + } + + @Test + public void testReadNurbSpline() throws IOException { + JmeImporter jmeImporter = Mockito.mock(JmeImporter.class); + InputCapsule inputCapsule = Mockito.mock(InputCapsule.class); + Mockito.when(jmeImporter.getCapsule(Mockito.any())).thenReturn(inputCapsule); + NurbSpline spline = new NurbSpline(initControlPoints(), initNurbKnots()); + Mockito.when(inputCapsule.readFloatArray("segmentsLength", null)).thenReturn(new float[]{1.2f,2.3f}); + Mockito.when(inputCapsule.readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class))).thenReturn(null); + spline.read(jmeImporter); + Mockito.verify(inputCapsule, Mockito.times(1)).readFloat(Mockito.anyString(), Mockito.anyFloat()); + Mockito.verify(inputCapsule, Mockito.times(1)).readBoolean(Mockito.anyString(), Mockito.anyBoolean()); + Mockito.verify(inputCapsule, Mockito.times(2)).readFloatArray(Mockito.anyString(), Mockito.any(float[].class)); + Mockito.verify(inputCapsule, Mockito.times(2)).readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class)); + Mockito.verify(inputCapsule, Mockito.times(1)).readInt(Mockito.anyString(), Mockito.anyInt()); + assertNull(spline.getControlPoints()); + assertTrue(spline.getTotalLength()==0); + assertTrue(spline.getSegmentsLength().size()==2); + assertFalse(spline.isCycle()); + assertNull(spline.getKnots()); + assertTrue(spline.getBasisFunctionDegree() == 0); + } + + /* + * TESTS BEFORE REFACTORING + */ + +// +// /** +// * Test method for {@link com.jme3.math.Spline#Spline()}. +// */ +// @Test +// public void testSpline() { +// assertNotNull(spline); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#Spline(com.jme3.math.Spline.SplineType, com.jme3.math.Vector3f[], float, boolean)}. +// */ +// @Test(expected = IllegalArgumentException.class) +// public void testSplineSplineTypeVector3fArrayFloatBoolean() { +// Vector3f[] sampleList = new Vector3f[]{new Vector3f(1,1,1), new Vector3f(2,2,2)}; +// spline = new Spline(SplineType.Linear, sampleList, 1.0f, false); +// assertNotNull(spline); +// assertTrue(spline.getCurveTension() == 1.0f); +// assertTrue(spline.getType().equals(SplineType.Linear)); +// assertTrue(spline.isCycle() == false); +// spline = null; +// spline = new Spline(SplineType.Nurb, sampleList, 1.0f, false); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#Spline(com.jme3.math.Spline.SplineType, java.util.List, float, boolean)}. +// */ +// @Test(expected = IllegalArgumentException.class) +// public void testSplineSplineTypeListOfVector3fFloatBoolean() { +// List sampleList = initSampleVectorList(); +// spline = new Spline(SplineType.Linear, sampleList, 1.0f, false); +// assertNotNull(spline); +// assertTrue(spline.getCurveTension() == 1.0f); +// assertTrue(spline.getType().equals(SplineType.Linear)); +// assertTrue(spline.isCycle() == false); +// spline = new Spline(SplineType.Nurb, sampleList, 1.0f, false); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#Spline(java.util.List, java.util.List)}. +// */ +// @Test(expected = IllegalArgumentException.class) +// public void testSplineListOfVector4fListOfFloat() { +// List controlPoints = initControlPoints(); +// List nurbKnots = initNurbKnots(); +// spline = new Spline(controlPoints, nurbKnots); +// assertNotNull(spline); +// assertTrue(spline.getType().equals(SplineType.Nurb)); +// nurbKnots.add(2.0f); +// spline = new Spline(controlPoints, nurbKnots); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#addControlPoint(com.jme3.math.Vector3f)}. +// */ +// @Test +// public void testAddControlPoint() { +// List sampleList = initSampleVectorList(); +// spline = new Spline(SplineType.Linear, sampleList, 1.0f, true); +// spline.addControlPoint(new Vector3f()); +// assertTrue(spline.getControlPoints().contains(new Vector3f())); +// spline.clearControlPoints(); +// spline.addControlPoint(new Vector3f()); +// assertTrue(spline.getControlPoints().size()==1); +// spline.addControlPoint(new Vector3f(1,1,1)); +// assertTrue(spline.getTotalLength() > 0); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#removeControlPoint(com.jme3.math.Vector3f)}. +// */ +// @Test +// public void testOperationsOverControlPoint() { +// spline = new Spline(initControlPoints(), initNurbKnots()); +// spline.addControlPoint(new Vector3f()); +// assertTrue(spline.getControlPoints().contains(new Vector3f())); +// spline.removeControlPoint(new Vector3f()); +// assertFalse(spline.getControlPoints().contains(new Vector3f())); +// spline.clearControlPoints(); +// assertTrue(spline.getControlPoints().isEmpty()); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#interpolate(float, int, com.jme3.math.Vector3f)}. +// */ +// @Test +// public void testLinearInterpolate() { +// spline = new Spline(SplineType.Linear, initSampleVectorList(), 1.0f, false); +// Vector3f result = spline.interpolate(0.3f, 0, null); +// assertTrue(result.equals(new Vector3f(1.3f, 1.3f, 1.3f))); +// } +// +// @Test +// public void testCatmullRomInterpolate() { +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, false); +// Vector3f result = spline.interpolate(0.3f, 0, null); +// assertTrue(result.equals(new Vector3f(1.3840001f, 1.3840001f, 1.3840001f))); +// } +// +// @Test +// public void testBezierInterpolate() { +// spline = new Spline(SplineType.Bezier, initSampleVectorList(), 1.0f, false); +// Vector3f result = spline.interpolate(0.3f, 0, null); +// assertTrue(result.equals(new Vector3f(1.9f, 1.9f, 1.9f))); +// } +// +// @Test +// public void testNurbInterpolate() { +// spline = new Spline(initControlPoints(), initNurbKnots()); +// Vector3f result = spline.interpolate(0.3f, 0, null); +// assertTrue(result.equals(new Vector3f(1.0f, 1.0f, 0.0f))); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#setCurveTension(float)}. +// */ +// @Test +// public void testSetCurveTension() { +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, false); +// spline.setCurveTension(3.5f); +// assertTrue(spline.getCurveTension() == 3.5f); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#setCycle(boolean)}. +// */ +// @Test +// public void testSetCycle() { +// spline = new Spline(initControlPoints(), initNurbKnots()); +// spline.setCycle(true); +// assertFalse(spline.isCycle()); +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, false); +// spline.setCycle(true); +// assertTrue(spline.isCycle()); +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, true); +// spline.setCycle(false); +// assertFalse(spline.isCycle()); +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, true); +// spline.setCycle(true); +// assertTrue(spline.isCycle()); +// spline = new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, false); +// spline.setCycle(false); +// assertFalse(spline.isCycle()); +// spline.clearControlPoints(); +// spline.setCycle(true); +// assertTrue(spline.isCycle()); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#setType(com.jme3.math.Spline.SplineType)}. +// */ +// @Test +// public void testSetType() { +// spline = new Spline(SplineType.Bezier, initSampleVectorList(), 1.0f, false); +// float prevoiusLength = spline.getTotalLength(); +// spline.setType(SplineType.Linear); +// assertFalse(spline.getTotalLength()==prevoiusLength); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#getSegmentsLength()}. +// */ +// @Test +// public void testGetSegmentsLength() { +// assertNull(spline.getSegmentsLength()); +// spline = new Spline(SplineType.Linear, initSampleVectorList(), 1.0f, false); +// assertTrue(spline.getSegmentsLength().size() != 0); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#getMinNurbKnot()}. +// */ +// @Test +// public void testGetMinNurbKnot() { +// spline = new Spline(initControlPoints(), initNurbKnots()); +// assertTrue(spline.getMinNurbKnot() == spline.getKnots().get(spline.getBasisFunctionDegree() - 1)); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#getMaxNurbKnot()}. +// */ +// @Test +// public void testGetMaxNurbKnot() { +// spline = new Spline(initControlPoints(), initNurbKnots()); +// assertTrue(spline.getMaxNurbKnot() == spline.getKnots().get(spline.getWeights().length)); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#write(com.jme3.export.JmeExporter)}. +// */ +// @Test +// public void testWrite() throws IOException{ +// JmeExporter jmeExporter = Mockito.mock(JmeExporter.class); +// OutputCapsule outputCapsule = Mockito.mock(OutputCapsule.class); +// Mockito.when(jmeExporter.getCapsule(Mockito.any())).thenReturn(outputCapsule); +// spline = new Spline(SplineType.Linear, initSampleVectorList(), 1.0f, false); +// spline.write(jmeExporter); +// Mockito.verify(outputCapsule, Mockito.times(2)).write(Mockito.anyFloat(), Mockito.anyString(), Mockito.anyFloat()); +// Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyBoolean()); +// Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.any(SplineType.class), Mockito.anyString(), Mockito.any(SplineType.class)); +// Mockito.verify(outputCapsule, Mockito.times(1)).write(Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt()); +// Mockito.verify(outputCapsule, Mockito.times(2)).write(Mockito.any(float[].class), Mockito.anyString(), Mockito.any(float[].class)); +// Mockito.verify(outputCapsule, Mockito.times(3)).writeSavableArrayList(Mockito.any(ArrayList.class), Mockito.anyString(), Mockito.any(ArrayList.class)); +// } +// +// /** +// * Test method for {@link com.jme3.math.Spline#read(com.jme3.export.JmeImporter)}. +// * @throws IOException +// */ +// @SuppressWarnings("unchecked") +// @Test +// public void testRead() throws IOException { +// JmeImporter jmeImporter = Mockito.mock(JmeImporter.class); +// InputCapsule inputCapsule = Mockito.mock(InputCapsule.class); +// Mockito.when(jmeImporter.getCapsule(Mockito.any())).thenReturn(inputCapsule); +// spline = new Spline(SplineType.Linear, initSampleVectorList(), 1.0f, false); +// Mockito.when(inputCapsule.readFloatArray("segmentsLength", null)).thenReturn(new float[]{1.2f,2.3f}); +// Mockito.when(inputCapsule.readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class))).thenReturn(null); +// Mockito.when(inputCapsule.readEnum(Mockito.anyString(), Mockito.any(Class.class), Mockito.any(SplineType.class))).thenReturn(SplineType.CatmullRom); +// Mockito.when(inputCapsule.readFloat("curveTension", 0.5f)).thenReturn(0.5f); +// spline.read(jmeImporter); +// Mockito.verify(inputCapsule, Mockito.times(2)).readFloat(Mockito.anyString(), Mockito.anyFloat()); +// Mockito.verify(inputCapsule, Mockito.times(1)).readBoolean(Mockito.anyString(), Mockito.anyBoolean()); +// Mockito.verify(inputCapsule, Mockito.times(1)).readEnum(Mockito.anyString(), Mockito.any(Class.class), Mockito.any(SplineType.class)); +// Mockito.verify(inputCapsule, Mockito.times(1)).readInt(Mockito.anyString(), Mockito.anyInt()); +// Mockito.verify(inputCapsule, Mockito.times(2)).readFloatArray(Mockito.anyString(), Mockito.any(float[].class)); +// Mockito.verify(inputCapsule, Mockito.times(3)).readSavableArrayList(Mockito.anyString(), Mockito.any(ArrayList.class)); +// assertNull(spline.getControlPoints()); +// assertNull(spline.getKnots()); +// assertTrue(spline.getTotalLength()==0); +// assertTrue(spline.getSegmentsLength().size()==2); +// assertTrue(spline.getType().equals(SplineType.CatmullRom)); +// assertTrue(spline.getCurveTension() == 0.5f); +// assertNull(spline.getWeights()); +// assertTrue(spline.getBasisFunctionDegree() == 0); +// assertFalse(spline.isCycle()); +// } + +} diff --git a/jme3-core/src/test/java/com/jme3/renderer/IDListAfterRefactorTest.java b/jme3-core/src/test/java/com/jme3/renderer/IDListAfterRefactorTest.java new file mode 100644 index 0000000000..2824219758 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/renderer/IDListAfterRefactorTest.java @@ -0,0 +1,132 @@ +/** + * + */ +package com.jme3.renderer; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author tr0k + * + */ +public class IDListAfterRefactorTest { + + private IDList idList; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + idList = new IDList(); + } + + /** + * Should reset all states to zero when all states are zero. + * Test method for {@link com.jme3.renderer.IDList#reset()}. + */ + @Test + public void testResetWhenZeroStates() { + idList.reset(); + assertEquals(idList.getNewLen(), 0); + assertEquals(idList.getOldLen(), 0); + assertArrayEquals(idList.getNewList(), new int[16]); + assertArrayEquals(idList.getOldList(), new int[16]); + } + + /** + * Should reset all states to zero when all states are non zero. + * Test method for {@link com.jme3.renderer.IDList#reset()}. + */ + @Test + public void testResetWhenNonZeroStates() { + //setup state + idList.setNewLen(5); + idList.setOldLen(11); + int[] newListArr = new int[16]; + Arrays.fill(newListArr, 1); + idList.setNewList(newListArr); + int[] oldListArr = new int[16]; + Arrays.fill(oldListArr, 3); + idList.setNewList(oldListArr); + + //reset state + idList.reset(); + assertEquals(idList.getNewLen(), 0); + assertEquals(idList.getOldLen(), 0); + assertArrayEquals(idList.getNewList(), new int[16]); + assertArrayEquals(idList.getOldList(), new int[16]); + } + + /** + * Adds an index to the new list when index was not in old list. + * Test method for {@link com.jme3.renderer.IDList#moveToNew(int)}. + */ + @Test + public void testMoveToNewWhenIndexNotInOldList() { + idList.setNewLen(0); + idList.setOldLen(0); + + //the index was not in the old list, false is returned + boolean state = idList.moveToNew(5); + assertFalse(state); + } + + /** + * Adds an index to the new list when index is in old list. + * Test method for {@link com.jme3.renderer.IDList#moveToNew(int)}. + */ + @Test + public void testMoveToNewWhenIndexInOldList() { + idList.setNewLen(1); + idList.setNewList(new int[16]); + idList.setOldLen(5); + idList.setOldList(new int[]{1,3,5,6,7}); + + //the index was in the old list, true is returned + boolean state = idList.moveToNew(5); + assertTrue(state); + assertEquals(idList.getOldLen(), 4); + assertEquals(idList.getNewListElem(1), 5); + } + + /** + * Copies the new list to the old list, and clears the new list. + * Test method for {@link com.jme3.renderer.IDList#copyNewToOld()}. + */ + @Test + public void testCopyNewToOld() { + idList.setNewLen(4); + idList.setNewList(new int[]{1,2,3,4}); + idList.setOldLen(4); + idList.setOldList(new int[]{5,6,7,8}); + + idList.copyNewToOld(); + + assertArrayEquals(idList.getNewList(), new int[]{1,2,3,4}); + assertEquals(idList.getNewLen(), 0); + assertArrayEquals(idList.getOldList(), new int[]{1,2,3,4}); + assertEquals(idList.getOldLen(), 4); + + } + + /** + * Copies the new list to the old list, and clears the new list when old has less length. + * In this case is uncaptured exception and func do not copy list. + * Test method for {@link com.jme3.renderer.IDList#copyNewToOld()}. + */ + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testCopyNewToOldWhenOldHasLessLen() { + idList.setNewLen(4); + idList.setNewList(new int[]{1,2,3,4}); + idList.setOldLen(4); + idList.setOldList(new int[]{5,6,7}); + + idList.copyNewToOld(); + } +} diff --git a/jme3-core/src/test/java/com/jme3/renderer/IDListPlainTextPrinterJMockitoTest.java b/jme3-core/src/test/java/com/jme3/renderer/IDListPlainTextPrinterJMockitoTest.java new file mode 100644 index 0000000000..7850617936 --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/renderer/IDListPlainTextPrinterJMockitoTest.java @@ -0,0 +1,89 @@ +package com.jme3.renderer; + +import static org.mockito.Mockito.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import static org.junit.Assert.*; + + +/** + * Mocked IDListPlainTextPrinter class. + * @author Tr0k + */ +public class IDListPlainTextPrinterJMockitoTest { + + @Test + public void printExpectedList(){ + //All System.out.println() statements will come to outContent stream + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + //Filling list + IDList list = new IDList(); + list.setNewLen(5); + list.setNewList(new int[] {1,2,3,4,5}); + list.setOldLen(5); + list.setOldList(new int[] {6,7,8,9,10}); + + IListPrinter printer = new IDListPlainTextPrinter(list); + printer.printList(); + + String separator = System.getProperty( "line.separator" ); + String expectedOutput = "New List: "+"1, 2, 3, 4, 5"+separator+ + "Old List: "+"6, 7, 8, 9, 10"+separator; + assertEquals(outContent.toString(), expectedOutput); + } + + @Test + public void printMockedList(){ + //All System.out.println() statements will come to outContent stream + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + + //Filling list + IDList mockList = Mockito.mock(IDList.class); + when(mockList.getNewLen()).thenReturn(4); + when(mockList.getOldLen()).thenReturn(4); + when(mockList.getNewList()).thenReturn(new int[] {1,2,3,4}); + when(mockList.getOldList()).thenReturn(new int[] {6,7,8,9}); + //Method returns arg + 1 + when(mockList.getNewListElem(anyInt())).thenAnswer(new Answer() { + public Integer answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + return (int) args[0] + 1; + } + }); + //Method returns arg + 5 + when(mockList.getOldListElem(anyInt())).thenAnswer(new Answer() { + public Integer answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + return (int) args[0] + 6; + } + }); + + IListPrinter printer = new IDListPlainTextPrinter(mockList); + printer.printList(); + + //Verify method execution + verify(mockList, atLeast(5)).getNewLen(); + verify(mockList, atLeast(5)).getOldLen(); + verify(mockList, times(4)).getNewListElem(anyInt()); + verify(mockList, times(4)).getOldListElem(anyInt()); + verify(mockList, never()).reset(); + verify(mockList, never()).moveToNew(anyInt()); + verify(mockList, never()).copyNewToOld(); + + //Check result + String separator = System.getProperty( "line.separator" ); + String expectedOutput = "New List: "+"1, 2, 3, 4"+separator+ + "Old List: "+"6, 7, 8, 9"+separator; + assertEquals(outContent.toString(), expectedOutput); + } +} diff --git a/jme3-core/src/test/java/com/jme3/renderer/IDListTest.java b/jme3-core/src/test/java/com/jme3/renderer/IDListTest.java new file mode 100644 index 0000000000..2634d1a0fc --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/renderer/IDListTest.java @@ -0,0 +1,126 @@ +/** + * + */ +package com.jme3.renderer; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author tr0k + * + */ +public class IDListTest { + + private IDList idList; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + idList = new IDList(); + } + + /** + * Should reset all states to zero when all states are zero. + * Test method for {@link com.jme3.renderer.IDList#reset()}. + */ +// @Test +// public void testResetWhenZeroStates() { +// idList.reset(); +// assertEquals(idList.newLen, 0); +// assertEquals(idList.oldLen, 0); +// assertArrayEquals(idList.newList, new int[16]); +// assertArrayEquals(idList.oldList, new int[16]); +// } +// +// /** +// * Should reset all states to zero when all states are non zero. +// * Test method for {@link com.jme3.renderer.IDList#reset()}. +// */ +// @Test +// public void testResetWhenNonZeroStates() { +// //setup state +// idList.newLen = 5; +// idList.oldLen = 11; +// Arrays.fill(idList.newList, 1); +// Arrays.fill(idList.oldList, 3); +// +// //reset state +// idList.reset(); +// assertEquals(idList.newLen, 0); +// assertEquals(idList.oldLen, 0); +// assertArrayEquals(idList.newList, new int[16]); +// assertArrayEquals(idList.oldList, new int[16]); +// } +// +// /** +// * Adds an index to the new list when index was not in old list. +// * Test method for {@link com.jme3.renderer.IDList#moveToNew(int)}. +// */ +// @Test +// public void testMoveToNewWhenIndexNotInOldList() { +// idList.newLen = 0; +// idList.oldLen = 0; +// +// //the index was not in the old list, false is returned +// boolean state = idList.moveToNew(5); +// assertFalse(state); +// } +// +// /** +// * Adds an index to the new list when index is in old list. +// * Test method for {@link com.jme3.renderer.IDList#moveToNew(int)}. +// */ +// @Test +// public void testMoveToNewWhenIndexInOldList() { +// idList.newLen = 1; +// idList.newList = new int[16]; +// idList.oldLen = 5; +// idList.oldList = new int[]{1,3,5,6,7}; +// +// //the index was in the old list, true is returned +// boolean state = idList.moveToNew(5); +// assertTrue(state); +// assertEquals(idList.oldLen, 4); +// assertEquals(idList.newList[1], 5); +// } +// +// /** +// * Copies the new list to the old list, and clears the new list. +// * Test method for {@link com.jme3.renderer.IDList#copyNewToOld()}. +// */ +// @Test +// public void testCopyNewToOld() { +// idList.newList = new int[]{1,2,3,4}; +// idList.newLen = 4; +// idList.oldList = new int[]{5,6,7,8}; +// idList.oldLen = 4; +// idList.copyNewToOld(); +// +// assertArrayEquals(idList.newList, new int[]{1,2,3,4}); +// assertEquals(idList.newLen, 0); +// assertArrayEquals(idList.oldList, new int[]{1,2,3,4}); +// assertEquals(idList.oldLen, 4); +// +// } +// +// /** +// * Copies the new list to the old list, and clears the new list when old has less length. +// * In this case is uncaptured exception and func do not copy list. +// * Test method for {@link com.jme3.renderer.IDList#copyNewToOld()}. +// */ +// @Test(expected = ArrayIndexOutOfBoundsException.class) +// public void testCopyNewToOldWhenOldHasLessLen() { +// idList.newList = new int[]{1,2,3,4}; +// idList.newLen = 4; +// idList.oldList = new int[]{5,6,7}; +// idList.oldLen = 4; +// idList.copyNewToOld(); +// } +} diff --git a/jme3-core/src/test/java/com/jme3/scene/shape/CurveTest.java b/jme3-core/src/test/java/com/jme3/scene/shape/CurveTest.java new file mode 100644 index 0000000000..6879113eec --- /dev/null +++ b/jme3-core/src/test/java/com/jme3/scene/shape/CurveTest.java @@ -0,0 +1,141 @@ +/** + * + */ +package com.jme3.scene.shape; + +import static com.jme3.math.DataGeneratorForSplineTest.initControlPoints; +import static com.jme3.math.DataGeneratorForSplineTest.initNurbKnots; +import static com.jme3.math.DataGeneratorForSplineTest.initSampleVectorList; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jme3.math.BezierSpline; +import com.jme3.math.CatmullRomSpline; +import com.jme3.math.LinearSpline; +import com.jme3.math.NurbSpline; +import com.jme3.math.Vector3f; +import com.jme3.scene.VertexBuffer; +/** + * @author Piotr + * + */ +public class CurveTest { + + Curve testCurve; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + testCurve = new Curve(); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link com.jme3.scene.shape.Curve#Curve()}. + */ + @Test + public void testCurve() { + assertNotNull(testCurve); + } + + /** + * Test method for {@link com.jme3.scene.shape.Curve#Curve(com.jme3.math.Vector3f[], int)}. + */ + @Test + public void testCurveVector3fArrayInt() { + testCurve = new Curve(new Vector3f[] {new Vector3f(1,1,1), new Vector3f(2,2,2)}, 5); + assertNotNull(testCurve); + } + + private LinearSpline createLinearSpline() { +// return new Spline(SplineType.Linear, initSampleVectorList(), 1.0f, false); + return new LinearSpline(initSampleVectorList(), false); + } + + private CatmullRomSpline createCatmullRomSpline() { +// return new Spline(SplineType.CatmullRom, initSampleVectorList(), 1.0f, false); + return new CatmullRomSpline(initSampleVectorList(), 1.0f, false); + } + + private BezierSpline createBezierSpline() { +// return new Spline(SplineType.Bezier, initSampleVectorList(), 1.0f, false); + return new BezierSpline(initSampleVectorList(), false); + } + + private NurbSpline createNurbSpline() { +// return new Spline(initControlPoints(), initNurbKnots()); + return new NurbSpline(initControlPoints(), initNurbKnots()); + } + + /** + * Test method for {@link com.jme3.scene.shape.Curve#Curve(com.jme3.math.Spline, int)}. + */ + @Test + public void testCurveSplineInt() { + assertNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createLinearSpline(), 5); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createBezierSpline(), 0); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createBezierSpline(), 5); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createCatmullRomSpline(), 5); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createNurbSpline(), 5); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + testCurve = new Curve(createNurbSpline(), 0); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Position)); + assertNotNull(testCurve.getBuffer(VertexBuffer.Type.Index)); + } + + /** + * Test method for {@link com.jme3.scene.shape.Curve#getLength()}. + */ + @Test + public void testGetLength() { + testCurve = new Curve(createLinearSpline(), 5); + assertTrue(testCurve.getLength()!=0.0f); + testCurve = new Curve(createBezierSpline(), 5); + assertTrue(testCurve.getLength()!=0.0f); + testCurve = new Curve(createCatmullRomSpline(), 5); + assertTrue(testCurve.getLength()!=0.0f); + testCurve = new Curve(createNurbSpline(), 5); + assertTrue(testCurve.getLength()==0.0f);// no function calculating total length of nurb spline + } + +} diff --git a/jme3-examples/src/main/java/jme3test/animation/TestCameraMotionPath.java b/jme3-examples/src/main/java/jme3test/animation/TestCameraMotionPath.java index ffe65a8888..35b76df19f 100644 --- a/jme3-examples/src/main/java/jme3test/animation/TestCameraMotionPath.java +++ b/jme3-examples/src/main/java/jme3test/animation/TestCameraMotionPath.java @@ -36,7 +36,6 @@ import com.jme3.cinematic.MotionPath; import com.jme3.cinematic.MotionPathListener; import com.jme3.cinematic.events.MotionEvent; -import com.jme3.cinematic.events.MotionEvent; import com.jme3.font.BitmapText; import com.jme3.input.ChaseCamera; import com.jme3.input.KeyInput; @@ -44,8 +43,9 @@ import com.jme3.input.controls.KeyTrigger; import com.jme3.light.DirectionalLight; import com.jme3.material.Material; +import com.jme3.math.CatmullRomSpline; import com.jme3.math.ColorRGBA; -import com.jme3.math.Spline.SplineType; +import com.jme3.math.LinearSpline; import com.jme3.math.Vector3f; import com.jme3.scene.CameraNode; import com.jme3.scene.Geometry; @@ -181,11 +181,16 @@ public void onAction(String name, boolean keyPressed, float tpf) { } if (name.equals("SwitchPathInterpolation") && keyPressed) { - if (path.getPathSplineType() == SplineType.CatmullRom) { - path.setPathSplineType(SplineType.Linear); - } else { - path.setPathSplineType(SplineType.CatmullRom); - } +// if (path.getPathSplineType() == SplineType.CatmullRom) { +// path.setPathSplineType(SplineType.Linear); +// } else { +// path.setPathSplineType(SplineType.CatmullRom); +// } + if(path.getSpline() instanceof CatmullRomSpline) { + path.setSpline(new LinearSpline(path.getSpline())); + } else { + path.setSpline(new CatmullRomSpline(path.getSpline())); + } } if (name.equals("tensionUp") && keyPressed) { diff --git a/jme3-examples/src/main/java/jme3test/animation/TestMotionPath.java b/jme3-examples/src/main/java/jme3test/animation/TestMotionPath.java index 57584f7c50..5b5f54e31f 100644 --- a/jme3-examples/src/main/java/jme3test/animation/TestMotionPath.java +++ b/jme3-examples/src/main/java/jme3test/animation/TestMotionPath.java @@ -31,12 +31,10 @@ */ package jme3test.animation; -import com.jme3.animation.LoopMode; import com.jme3.app.SimpleApplication; import com.jme3.cinematic.MotionPath; import com.jme3.cinematic.MotionPathListener; import com.jme3.cinematic.events.MotionEvent; -import com.jme3.cinematic.events.MotionEvent; import com.jme3.font.BitmapText; import com.jme3.input.ChaseCamera; import com.jme3.input.KeyInput; @@ -44,10 +42,11 @@ import com.jme3.input.controls.KeyTrigger; import com.jme3.light.DirectionalLight; import com.jme3.material.Material; +import com.jme3.math.CatmullRomSpline; import com.jme3.math.ColorRGBA; import com.jme3.math.FastMath; +import com.jme3.math.LinearSpline; import com.jme3.math.Quaternion; -import com.jme3.math.Spline.SplineType; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.Spatial; @@ -175,11 +174,16 @@ public void onAction(String name, boolean keyPressed, float tpf) { } if (name.equals("SwitchPathInterpolation") && keyPressed) { - if (path.getPathSplineType() == SplineType.CatmullRom){ - path.setPathSplineType(SplineType.Linear); - } else { - path.setPathSplineType(SplineType.CatmullRom); - } +// if (path.getPathSplineType() == SplineType.CatmullRom){ +// path.setPathSplineType(SplineType.Linear); +// } else { +// path.setPathSplineType(SplineType.CatmullRom); +// } + if(path.getSpline() instanceof CatmullRomSpline) { + path.setSpline(new LinearSpline(path.getSpline())); + } else { + path.setSpline(new CatmullRomSpline(path.getSpline())); + } } if (name.equals("tensionUp") && keyPressed) { diff --git a/jme3-examples/src/main/java/jme3test/app/TestIDList.java b/jme3-examples/src/main/java/jme3test/app/TestIDList.java index 37f3f56eaa..b602ce93a9 100644 --- a/jme3-examples/src/main/java/jme3test/app/TestIDList.java +++ b/jme3-examples/src/main/java/jme3test/app/TestIDList.java @@ -131,8 +131,8 @@ static void checkSlots(StateCol state){ } static void clearSlots(){ - for (int i = 0; i < list.oldLen; i++){ - int slot = list.oldList[i]; + for (int i = 0; i < list.getOldLen(); i++){ + int slot = list.getOldListElem(i); disable(slot); slots[slot] = null; }