Search in sources :

Example 1 with Vector3d

use of com.jme3.scene.plugins.blender.math.Vector3d in project jmonkeyengine by jMonkeyEngine.

the class Edge method getCrossPoint.

/**
	 * The method computes the crossing pint of this edge and another edge. If
	 * there is no crossing then null is returned. Also null is returned if the edges are parallel.
	 * This method also allows to get the crossing point of the straight lines that contain these edges if
	 * you set the 'extend' parameter to true.
	 * 
	 * @param edge
	 *            the edge to compute corss point with
	 * @param extendThisEdge
	 *            set to <b>true</b> to find a crossing point along the whole
	 *            straight that contains the current edge
	 * @param extendSecondEdge
	 *            set to <b>true</b> to find a crossing point along the whole
	 *            straight that contains the given edge
	 * @return cross point on null if none exist or the edges are parallel
	 */
public Vector3f getCrossPoint(Edge edge, boolean extendThisEdge, boolean extendSecondEdge) {
    Vector3d P1 = new Vector3d(this.getFirstVertex());
    Vector3d P2 = new Vector3d(edge.getFirstVertex());
    Vector3d u = new Vector3d(this.getSecondVertex()).subtract(P1).normalizeLocal();
    Vector3d v = new Vector3d(edge.getSecondVertex()).subtract(P2).normalizeLocal();
    if (Math.abs(u.dot(v)) >= 1 - FastMath.DBL_EPSILON) {
        // the edges are parallel; do not care about the crossing point
        return null;
    }
    double t1 = 0, t2 = 0;
    if (u.x == 0 && v.x == 0) {
        t2 = (u.z * (P2.y - P1.y) - u.y * (P2.z - P1.z)) / (u.y * v.z - u.z * v.y);
        t1 = (P2.z - P1.z + v.z * t2) / u.z;
    } else if (u.y == 0 && v.y == 0) {
        t2 = (u.x * (P2.z - P1.z) - u.z * (P2.x - P1.x)) / (u.z * v.x - u.x * v.z);
        t1 = (P2.x - P1.x + v.x * t2) / u.x;
    } else if (u.z == 0 && v.z == 0) {
        t2 = (u.x * (P2.y - P1.y) - u.y * (P2.x - P1.x)) / (u.y * v.x - u.x * v.y);
        t1 = (P2.x - P1.x + v.x * t2) / u.x;
    } else {
        t2 = (P1.y * u.x - P1.x * u.y + P2.x * u.y - P2.y * u.x) / (v.y * u.x - u.y * v.x);
        t1 = (P2.x - P1.x + v.x * t2) / u.x;
        if (Math.abs(P1.z - P2.z + u.z * t1 - v.z * t2) > FastMath.FLT_EPSILON) {
            return null;
        }
    }
    Vector3d p1 = P1.add(u.mult(t1));
    Vector3d p2 = P2.add(v.mult(t2));
    if (p1.distance(p2) <= FastMath.FLT_EPSILON) {
        if (extendThisEdge && extendSecondEdge) {
            return p1.toVector3f();
        }
        // the lines cross, check if p1 and p2 are within the edges
        Vector3d p = p1.subtract(P1);
        double cos = p.dot(u) / p.length();
        if (extendThisEdge || p.length() <= FastMath.FLT_EPSILON || cos >= 1 - FastMath.FLT_EPSILON && p.length() - this.getLength() <= FastMath.FLT_EPSILON) {
            // p1 is inside the first edge, lets check the other edge now
            p = p2.subtract(P2);
            cos = p.dot(v) / p.length();
            if (extendSecondEdge || p.length() <= FastMath.FLT_EPSILON || cos >= 1 - FastMath.FLT_EPSILON && p.length() - edge.getLength() <= FastMath.FLT_EPSILON) {
                return p1.toVector3f();
            }
        }
    }
    return null;
}
Also used : Vector3d(com.jme3.scene.plugins.blender.math.Vector3d)

Example 2 with Vector3d

use of com.jme3.scene.plugins.blender.math.Vector3d in project jmonkeyengine by jMonkeyEngine.

the class DTransform method read.

public void read(JmeImporter e) throws IOException {
    InputCapsule capsule = e.getCapsule(this);
    rotation = (DQuaternion) capsule.readSavable("rot", new DQuaternion());
    translation = (Vector3d) capsule.readSavable("translation", Vector3d.ZERO);
    scale = (Vector3d) capsule.readSavable("scale", Vector3d.UNIT_XYZ);
}
Also used : InputCapsule(com.jme3.export.InputCapsule)

Example 3 with Vector3d

use of com.jme3.scene.plugins.blender.math.Vector3d in project jmonkeyengine by jMonkeyEngine.

the class ConstraintDefinitionIK method bake.

@Override
public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) {
    if (influence == 0 || !trackToBeChanged || targetTransform == null || bonesCount == 0) {
        // no need to do anything
        return;
    }
    if (bones == null) {
        bones = new BonesChain((Bone) this.getOwner(), useTail, bonesAffected, alteredOmas, blenderContext);
    }
    if (bones.size() == 0) {
        bonesCount = 0;
        // no need to do anything
        return;
    }
    double distanceFromTarget = Double.MAX_VALUE;
    target.set(targetTransform.getTranslation().x, targetTransform.getTranslation().y, targetTransform.getTranslation().z);
    if (bonesCount < 0) {
        bonesCount = bones.size();
        rotationVectors = new Vector3d[bonesCount];
        for (int i = 0; i < bonesCount; ++i) {
            rotationVectors[i] = new Vector3d();
        }
        J = new Matrix(3, bonesCount);
    }
    BoneContext topBone = bones.get(0);
    for (int i = 0; i < iterations; ++i) {
        DTransform topBoneTransform = bones.getWorldTransform(topBone);
        // effector
        Vector3d e = topBoneTransform.getTranslation().add(topBoneTransform.getRotation().mult(Vector3d.UNIT_Y).multLocal(topBone.getLength()));
        distanceFromTarget = e.distance(target);
        if (distanceFromTarget <= MIN_DISTANCE) {
            break;
        }
        deltaP.setColumn(0, 0, target.x - e.x, target.y - e.y, target.z - e.z);
        int column = 0;
        for (BoneContext boneContext : bones) {
            DTransform boneWorldTransform = bones.getWorldTransform(boneContext);
            // current join position
            Vector3d j = boneWorldTransform.getTranslation();
            Vector3d vectorFromJointToEffector = e.subtract(j);
            vectorFromJointToEffector.cross(target.subtract(j), rotationVectors[column]).normalizeLocal();
            rotationVectors[column].cross(vectorFromJointToEffector, col);
            J.setColumn(col, column++);
        }
        Matrix J_1 = J.pseudoinverse();
        SimpleMatrix deltaThetas = J_1.mult(deltaP);
        if (deltaThetas.elementMaxAbs() < MIN_ANGLE_CHANGE) {
            break;
        }
        for (int j = 0; j < deltaThetas.numRows(); ++j) {
            double angle = deltaThetas.get(j, 0);
            Vector3d rotationVector = rotationVectors[j];
            tempDQuaternion.fromAngleAxis(angle, rotationVector);
            BoneContext boneContext = bones.get(j);
            Bone bone = boneContext.getBone();
            if (bone.equals(this.getOwner())) {
                if (boneContext.isLockX()) {
                    tempDQuaternion.set(0, tempDQuaternion.getY(), tempDQuaternion.getZ(), tempDQuaternion.getW());
                }
                if (boneContext.isLockY()) {
                    tempDQuaternion.set(tempDQuaternion.getX(), 0, tempDQuaternion.getZ(), tempDQuaternion.getW());
                }
                if (boneContext.isLockZ()) {
                    tempDQuaternion.set(tempDQuaternion.getX(), tempDQuaternion.getY(), 0, tempDQuaternion.getW());
                }
            }
            DTransform boneTransform = bones.getWorldTransform(boneContext);
            boneTransform.getRotation().set(tempDQuaternion.mult(boneTransform.getRotation()));
            bones.setWorldTransform(boneContext, boneTransform);
        }
    }
    // applying the results
    for (int i = bonesCount - 1; i >= 0; --i) {
        BoneContext boneContext = bones.get(i);
        DTransform transform = bones.getWorldTransform(boneContext);
        constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), Space.CONSTRAINT_SPACE_WORLD, transform.toTransform());
    }
    // need to reload them again
    bones = null;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Matrix(com.jme3.scene.plugins.blender.math.Matrix) SimpleMatrix(org.ejml.simple.SimpleMatrix) Vector3d(com.jme3.scene.plugins.blender.math.Vector3d) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) DTransform(com.jme3.scene.plugins.blender.math.DTransform) Bone(com.jme3.animation.Bone)

Aggregations

Vector3d (com.jme3.scene.plugins.blender.math.Vector3d)2 Bone (com.jme3.animation.Bone)1 InputCapsule (com.jme3.export.InputCapsule)1 BoneContext (com.jme3.scene.plugins.blender.animations.BoneContext)1 DTransform (com.jme3.scene.plugins.blender.math.DTransform)1 Matrix (com.jme3.scene.plugins.blender.math.Matrix)1 SimpleMatrix (org.ejml.simple.SimpleMatrix)1