Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emil Kleszcz & Piotr ŒSwierczynski Refactoring #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
93 changes: 52 additions & 41 deletions jme3-core/src/main/java/com/jme3/cinematic/MotionPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,29 @@
*/
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;
import com.jme3.scene.Node;
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.
Expand All @@ -59,7 +64,7 @@ public class MotionPath implements Savable {
private Node debugNode;
private AssetManager assetManager;
private List<MotionPathListener> listeners;
private Spline spline = new Spline();
private Spline spline = new CatmullRomSpline();
int prevWayPoint = 0;

/**
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -253,17 +263,21 @@ public Iterator<Vector3f> 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();
Expand Down Expand Up @@ -336,22 +350,22 @@ 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;
}
}

/**
* sets the tension of the curve (only for catmull rom) 0.0 will give a linear curve, 1.0 a round curve
* @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() {
Expand All @@ -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();
}

/**
Expand All @@ -386,4 +392,9 @@ public boolean isCycle() {
public Spline getSpline() {
return spline;
}

public void setSpline(Spline spline) {
this.spline = spline;
changeDebugNodeAfterSplinePropertiesChange();
}
}
115 changes: 115 additions & 0 deletions jme3-core/src/main/java/com/jme3/math/BezierSpline.java
Original file line number Diff line number Diff line change
@@ -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<Vector3f> 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<Vector3f> 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);
}

}
Loading