git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7089 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
b641b5670c
commit
a9a7d2d4de
@ -0,0 +1,127 @@ |
||||
package com.jme3.math; |
||||
|
||||
import java.util.List; |
||||
|
||||
import com.jme3.math.Spline.SplineType; |
||||
|
||||
/** |
||||
* This class offers methods to help with curves and surfaces calculations. |
||||
* @author Marcin Roguski (Kealthas) |
||||
*/ |
||||
public class CurveAndSurfaceMath { |
||||
private static final float KNOTS_MINIMUM_DELTA = 0.0001f; |
||||
|
||||
/** |
||||
* A private constructor is defined to avoid instatiation of this class. |
||||
*/ |
||||
private CurveAndSurfaceMath() {} |
||||
|
||||
/** |
||||
* This method interpolates tha data for the nurbs curve. |
||||
* @param u |
||||
* the u value |
||||
* @param nurbSpline |
||||
* the nurbs spline definition |
||||
* @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!"); |
||||
} |
||||
List<Vector3f> controlPoints = nurbSpline.getControlPoints(); |
||||
float[] weights = nurbSpline.getWeights(); |
||||
List<Float> knots = nurbSpline.getKnots(); |
||||
int controlPointAmount = controlPoints.size(); |
||||
|
||||
store.set(Vector3f.ZERO); |
||||
float delimeter = 0; |
||||
for (int i = 0; i < controlPointAmount; ++i) { |
||||
float val = weights[i] * CurveAndSurfaceMath.computeBaseFunctionValue(i, nurbSpline.getBasisFunctionDegree(), u, knots); |
||||
store.addLocal(nurbSpline.getControlPoints().get(i) |
||||
.mult(val)); |
||||
delimeter += val; |
||||
} |
||||
store.divideLocal(delimeter); |
||||
} |
||||
|
||||
/** |
||||
* This method interpolates tha data for the nurbs surface. |
||||
* @param u |
||||
* the u value |
||||
* @param v |
||||
* the v value |
||||
* @param controlPoints |
||||
* the nurbs' control points |
||||
* @param knots |
||||
* the nurbs' knots |
||||
* @param store |
||||
* the resulting point in 3D space |
||||
*/ |
||||
public static void interpolate(float u, float v, List<List<Vector4f>> controlPoints, List<Float>[] knots, Vector3f store) { |
||||
store.set(Vector3f.ZERO); |
||||
float delimeter = 0; |
||||
int vControlPointsAmount = controlPoints.size(); |
||||
int uControlPointsAmount = controlPoints.get(0).size(); |
||||
int basisUFunctionDegree = knots[0].size() - controlPoints.get(0).size(); |
||||
int basisVFunctionDegree = knots[1]==null ? 0 : knots[1].size() - controlPoints.size(); |
||||
for (int i = 0; i < vControlPointsAmount; ++i) { |
||||
for (int j = 0; j < uControlPointsAmount; ++j) { |
||||
Vector4f controlPoint = controlPoints.get(j).get(i); |
||||
float val = controlPoint.w |
||||
* CurveAndSurfaceMath.computeBaseFunctionValue(i, basisVFunctionDegree, v, knots[1]) |
||||
* CurveAndSurfaceMath.computeBaseFunctionValue(j, basisUFunctionDegree, u, knots[0]); |
||||
store.addLocal(controlPoint.x * val, controlPoint.y * val, controlPoint.z * val); |
||||
delimeter += val; |
||||
} |
||||
} |
||||
store.divideLocal(delimeter); |
||||
} |
||||
|
||||
/** |
||||
* This method prepares the knots to be used. If the knots represent non-uniform B-splines (first and last knot values are being |
||||
* repeated) it leads to NaN results during calculations. This method adds a small number to each of such knots to avoid NaN's. |
||||
* @param knots |
||||
* the knots to be prepared to use |
||||
* @param basisFunctionDegree |
||||
* the degree of basis function |
||||
*/ |
||||
// TODO: improve this; constant delta may lead to errors if the difference between tha last repeated
|
||||
// point and the following one is lower than it
|
||||
public static void prepareNurbsKnots(List<Float> knots, int basisFunctionDegree) { |
||||
float delta = KNOTS_MINIMUM_DELTA; |
||||
for (int i = 1; i < basisFunctionDegree && knots.get(i).equals(knots.get(0)); ++i) { |
||||
knots.set(i, Float.valueOf(knots.get(i).floatValue() + delta)); |
||||
delta += KNOTS_MINIMUM_DELTA; |
||||
} |
||||
float lastKnot = knots.get(knots.size() - 1); |
||||
delta = KNOTS_MINIMUM_DELTA; |
||||
for (int i = knots.size() - basisFunctionDegree + 1; i < knots.size() && knots.get(i).equals(lastKnot); ++i) { |
||||
knots.set(i, Float.valueOf(knots.get(i).floatValue() + delta)); |
||||
delta += KNOTS_MINIMUM_DELTA; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* This method computes the base function value for the NURB curve. |
||||
* @param i |
||||
* the knot index |
||||
* @param k |
||||
* the base function degree |
||||
* @param t |
||||
* the knot value |
||||
* @param knots |
||||
* the knots' values |
||||
* @return the base function value |
||||
*/ |
||||
private static float computeBaseFunctionValue(int i, int k, float t, List<Float> knots) { |
||||
if (k == 1) { |
||||
return knots.get(i) <= t && t < knots.get(i + 1) ? 1.0f : 0.0f; |
||||
} else { |
||||
return (t - knots.get(i)) / (knots.get(i + k - 1) - knots.get(i)) * |
||||
CurveAndSurfaceMath.computeBaseFunctionValue(i, k - 1, t, knots) |
||||
+ (knots.get(i + k) - t) / (knots.get(i + k) - knots.get(i + 1)) * |
||||
CurveAndSurfaceMath.computeBaseFunctionValue(i + 1, k - 1, t, knots); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,277 @@ |
||||
package com.jme3.scene.shape; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import com.jme3.math.FastMath; |
||||
import com.jme3.math.CurveAndSurfaceMath; |
||||
import com.jme3.math.Spline.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; |
||||
|
||||
/** |
||||
* This class represents a surface described by knots, weights and control points. |
||||
* Currently the following types are supported: |
||||
* a) NURBS |
||||
* @author Marcin Roguski (Kealthas) |
||||
*/ |
||||
public class Surface extends Mesh { |
||||
private SplineType type; //the type of the surface
|
||||
private List<List<Vector4f>> controlPoints; //space control points and their weights
|
||||
private List<Float>[] knots; //knots of the surface
|
||||
private int basisUFunctionDegree; //the degree of basis U function (computed automatically)
|
||||
private int basisVFunctionDegree; //the degree of basis V function (computed automatically)
|
||||
private int uSegments; //the amount of U segments
|
||||
private int vSegments; //the amount of V segments
|
||||
|
||||
/** |
||||
* Constructor. Constructs required surface. |
||||
* @param controlPoints space control points |
||||
* @param nurbKnots knots of the surface |
||||
* @param uSegments the amount of U segments |
||||
* @param vSegments the amount of V segments |
||||
*/ |
||||
private Surface(List<List<Vector4f>> controlPoints, List<Float>[] nurbKnots, int uSegments, int vSegments) { |
||||
this.validateInputData(controlPoints, nurbKnots, uSegments, vSegments); |
||||
this.type = SplineType.Nurb; |
||||
this.uSegments = uSegments; |
||||
this.vSegments = vSegments; |
||||
this.controlPoints = controlPoints; |
||||
this.knots = nurbKnots; |
||||
this.basisUFunctionDegree = nurbKnots[0].size() - controlPoints.get(0).size(); |
||||
CurveAndSurfaceMath.prepareNurbsKnots(nurbKnots[0], basisUFunctionDegree); |
||||
if(nurbKnots[1]!=null) { |
||||
this.basisVFunctionDegree = nurbKnots[1].size() - controlPoints.size(); |
||||
CurveAndSurfaceMath.prepareNurbsKnots(nurbKnots[1], basisVFunctionDegree); |
||||
} |
||||
|
||||
this.buildSurface(); |
||||
} |
||||
|
||||
/** |
||||
* This method creates a NURBS surface. |
||||
* @param controlPoints space control points |
||||
* @param nurbKnots knots of the surface |
||||
* @param uSegments the amount of U segments |
||||
* @param vSegments the amount of V segments |
||||
* @return an instance of NURBS surface |
||||
*/ |
||||
public static final Surface createNurbsSurface(List<List<Vector4f>> controlPoints, List<Float>[] nurbKnots, int uSegments, int vSegments) { |
||||
Surface result = new Surface(controlPoints, nurbKnots, uSegments, vSegments); |
||||
result.type = SplineType.Nurb; |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* This method creates the surface. |
||||
*/ |
||||
private void buildSurface() { |
||||
boolean smooth = true;//TODO: take smoothing into consideration
|
||||
float minUKnot = this.getMinUNurbKnot(); |
||||
float maxUKnot = this.getMaxUNurbKnot(); |
||||
float deltaU = (maxUKnot - minUKnot)/uSegments; |
||||
|
||||
float minVKnot = this.getMinVNurbKnot(); |
||||
float maxVKnot = this.getMaxVNurbKnot(); |
||||
float deltaV = (maxVKnot - minVKnot)/vSegments; |
||||
|
||||
Vector3f[] vertices = new Vector3f[(uSegments + 1) * (vSegments + 1)]; |
||||
|
||||
float u = minUKnot, v = minVKnot; |
||||
int arrayIndex = 0; |
||||
|
||||
for(int i=0;i<=vSegments; ++i) { |
||||
for(int j=0;j<=uSegments; ++j) { |
||||
Vector3f interpolationResult = new Vector3f(); |
||||
CurveAndSurfaceMath.interpolate(u, v, controlPoints, knots, interpolationResult); |
||||
vertices[arrayIndex++] = interpolationResult; |
||||
u += deltaU; |
||||
} |
||||
u = minUKnot; |
||||
v += deltaV; |
||||
} |
||||
|
||||
//adding indexes
|
||||
int uVerticesAmount = uSegments + 1; |
||||
int[] indices = new int[uSegments * vSegments * 6]; |
||||
arrayIndex = 0; |
||||
for(int i=0;i<vSegments; ++i) { |
||||
for(int j=0;j<uSegments; ++j) { |
||||
indices[arrayIndex++] = j + i*uVerticesAmount; |
||||
indices[arrayIndex++] = j + i*uVerticesAmount + 1; |
||||
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount; |
||||
indices[arrayIndex++] = j + i*uVerticesAmount + 1; |
||||
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount + 1; |
||||
indices[arrayIndex++] = j + i*uVerticesAmount + uVerticesAmount; |
||||
} |
||||
} |
||||
|
||||
//normalMap merges normals of faces that will be rendered smooth
|
||||
Map<Vector3f, Vector3f> normalMap = new HashMap<Vector3f, Vector3f>(vertices.length); |
||||
for(int i=0;i<indices.length;i+=3) { |
||||
Vector3f n = FastMath.computeNormal(vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]]); |
||||
this.addNormal(n, normalMap, smooth, vertices[indices[i]], vertices[indices[i + 1]], vertices[indices[i + 2]]); |
||||
} |
||||
//preparing normal list (the order of normals must match the order of vertices)
|
||||
float[] normals = new float[vertices.length * 3]; |
||||
arrayIndex = 0; |
||||
for(int i=0;i<vertices.length;++i) { |
||||
Vector3f n = normalMap.get(vertices[i]); |
||||
normals[arrayIndex++] = n.x; |
||||
normals[arrayIndex++] = n.y; |
||||
normals[arrayIndex++] = n.z; |
||||
} |
||||
|
||||
this.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); |
||||
this.setBuffer(VertexBuffer.Type.Index, 3, indices); |
||||
this.setBuffer(VertexBuffer.Type.Normal, 3, normals); |
||||
this.updateBound(); |
||||
this.updateCounts(); |
||||
} |
||||
|
||||
public List<List<Vector4f>> getControlPoints() { |
||||
return controlPoints; |
||||
} |
||||
|
||||
/** |
||||
* This method returns the amount of U control points. |
||||
* @return the amount of U control points |
||||
*/ |
||||
public int getUControlPointsAmount() { |
||||
return controlPoints.size(); |
||||
} |
||||
|
||||
/** |
||||
* This method returns the amount of V control points. |
||||
* @return the amount of V control points |
||||
*/ |
||||
public int getVControlPointsAmount() { |
||||
return controlPoints.get(0)==null ? 0 : controlPoints.get(0).size(); |
||||
} |
||||
|
||||
/** |
||||
* This method returns the degree of basis U function. |
||||
* @return the degree of basis U function |
||||
*/ |
||||
public int getBasisUFunctionDegree() { |
||||
return basisUFunctionDegree; |
||||
} |
||||
|
||||
/** |
||||
* This method returns the degree of basis V function. |
||||
* @return the degree of basis V function |
||||
*/ |
||||
public int getBasisVFunctionDegree() { |
||||
return basisVFunctionDegree; |
||||
} |
||||
|
||||
/** |
||||
* This method returns the knots for specified dimension (U knots - value: '0', |
||||
* V knots - value: '1'). |
||||
* @param dim an integer specifying if the U or V knots are required |
||||
* @return an array of knots |
||||
*/ |
||||
public List<Float> getKnots(int dim) { |
||||
return knots[dim]; |
||||
} |
||||
|
||||
/** |
||||
* This method returns the type of the surface. |
||||
* @return the type of the surface |
||||
*/ |
||||
public SplineType getType() { |
||||
return type; |
||||
} |
||||
|
||||
/** |
||||
* This method returns the minimum nurb curve U knot value. |
||||
* @return the minimum nurb curve knot value |
||||
*/ |
||||
private float getMinUNurbKnot() { |
||||
return knots[0].get(basisUFunctionDegree - 1); |
||||
} |
||||
|
||||
/** |
||||
* This method returns the maximum nurb curve U knot value. |
||||
* @return the maximum nurb curve knot value |
||||
*/ |
||||
private float getMaxUNurbKnot() { |
||||
return knots[0].get(controlPoints.get(0).size()); |
||||
} |
||||
|
||||
/** |
||||
* This method returns the minimum nurb curve U knot value. |
||||
* @return the minimum nurb curve knot value |
||||
*/ |
||||
private float getMinVNurbKnot() { |
||||
return knots[1].get(basisVFunctionDegree - 1); |
||||
} |
||||
|
||||
/** |
||||
* This method returns the maximum nurb curve U knot value. |
||||
* @return the maximum nurb curve knot value |
||||
*/ |
||||
private float getMaxVNurbKnot() { |
||||
return knots[1].get(controlPoints.size()); |
||||
} |
||||
|
||||
/** |
||||
* This method adds a normal to a normals' map. This map is used to merge normals of a vertor that should be rendered smooth. |
||||
* @param normalToAdd |
||||
* a normal to be added |
||||
* @param normalMap |
||||
* merges normals of faces that will be rendered smooth; the key is the vertex and the value - its normal vector |
||||
* @param smooth |
||||
* the variable that indicates wheather to merge normals (creating the smooth mesh) or not |
||||
* @param vertices |
||||
* a list of vertices read from the blender file |
||||
*/ |
||||
private void addNormal(Vector3f normalToAdd, Map<Vector3f, Vector3f> normalMap, boolean smooth, Vector3f... vertices) { |
||||
for(Vector3f v : vertices) { |
||||
Vector3f n = normalMap.get(v); |
||||
if(!smooth || n == null) { |
||||
normalMap.put(v, normalToAdd.clone()); |
||||
} else { |
||||
n.addLocal(normalToAdd).normalizeLocal(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* This method validates the input data. It throws {@link IllegalArgumentException} if |
||||
* the data is invalid. |
||||
* @param controlPoints space control points |
||||
* @param nurbKnots knots of the surface |
||||
* @param uSegments the amount of U segments |
||||
* @param vSegments the amount of V segments |
||||
*/ |
||||
private void validateInputData(List<List<Vector4f>> controlPoints, List<Float>[] nurbKnots, |
||||
int uSegments, int vSegments) { |
||||
int uPointsAmount = controlPoints.get(0).size(); |
||||
for(int i=1;i<controlPoints.size();++i) { |
||||
if(controlPoints.get(i).size()!=uPointsAmount) { |
||||
throw new IllegalArgumentException("The amount of 'U' control points is invalid!"); |
||||
} |
||||
} |
||||
if(uSegments<=0) { |
||||
throw new IllegalArgumentException("U segments amount should be positive!"); |
||||
} |
||||
if(vSegments<0) { |
||||
throw new IllegalArgumentException("V segments amount cannot be negative!"); |
||||
} |
||||
if (nurbKnots.length != 2) { |
||||
throw new IllegalArgumentException("Nurb surface should have two rows of knots!"); |
||||
} |
||||
for (int i = 0; i < nurbKnots.length; ++i) { |
||||
for (int j = 0; j < nurbKnots[i].size() - 1; ++j) { |
||||
if (nurbKnots[i].get(j) > nurbKnots[i].get(j+1)) { |
||||
throw new IllegalArgumentException("The knots' values cannot decrease!"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue