Add equals with delta to Vector3f

Here's a method for comparing to Vector3fs with a given max difference value.

it's like the assertEquals(double actual, double expected, double delta) in jUnit.



Would be gread if you will adding this ;]

Here's the code:


public boolean equals(Object o, float maxDiff) {
        if (!(o instanceof Vector3f)) { return false; }
        if (this == o) { return true; }

        Vector3f comp = (Vector3f) o;
        if (comp.x == x && comp.y == y && comp.z == z)
            return true;
        if (Math.abs(x - comp.x) <= maxDiff)
            if (Math.abs(y - comp.y) <= maxDiff)
                if (Math.abs(z - comp.z) <= maxDiff)
                    return true;
        return false;
    }



Nice coding!
Tim

Sun’s javax.vecmath package has also such method. I have also found it usefull. I think vecmath’s Float.isNaN is not needed jMe.





http://java.sun.com/javase/technologies/desktop/java3d/forDevelopers/j3dapi/javax/vecmath/package-summary.html




https://vecmath.dev.java.net/source/browse/vecmath/src/javax/vecmath/Tuple3f.java?rev=1.8&view=markup



   /**
     * Returns true if the L-infinite distance between this tuple
     * and tuple t1 is less than or equal to the epsilon parameter,
     * otherwise returns false.  The L-infinite
     * distance is equal to MAX[abs(x1-x2), abs(y1-y2), abs(z1-z2)].
     * @param t1  the tuple to be compared to this tuple
     * @param epsilon  the threshold value 
     * @return  true or false
     */
    public boolean epsilonEquals(Tuple3f t1, float epsilon)
    {
       float diff;

       diff = x - t1.x;
       if(Float.isNaN(diff)) return false;
       if((diff<0?-diff:diff) > epsilon) return false;

       diff = y - t1.y;
       if(Float.isNaN(diff)) return false;
       if((diff<0?-diff:diff) > epsilon) return false;

       diff = z - t1.z;
       if(Float.isNaN(diff)) return false;
       if((diff<0?-diff:diff) > epsilon) return false;

       return true;

    }

sounds useful  :slight_smile:

tim8dev said:

Here's a method for comparing to Vector3fs with a given max difference value.
it's like the assertEquals(double actual, double expected, double delta) in jUnit.

Would be gread if you will adding this ;]
Here's the code:

public boolean equals(Object o, float maxDiff) {
        if (!(o instanceof Vector3f)) { return false; }
        if (this == o) { return true; }

        Vector3f comp = (Vector3f) o;
        if (comp.x == x && comp.y == y && comp.z == z)
            return true;
        if (Math.abs(x - comp.x) <= maxDiff)
            if (Math.abs(y - comp.y) <= maxDiff)
                if (Math.abs(z - comp.z) <= maxDiff)
                    return true;
        return false;
    }



Nice coding!
Tim


good idea Tim8div :), but the equals method you placed is an equal by coordinates or by object i can say and not equal by properties plus  you can directly insert Vector3f in  the parameter instead of inserting an object and checking if it was an instance of Vector3f, mainly because Vector3f have no subclasses, anyway there are also plenty of other methods  that could be useful to add, there ,  i wrote some usefull methods that can be added to the Vector3f class, but for now i made a new class which  extends Vector3f =]



import com.jme.math.Vector3f;

/**
 *
 * @author Nader
 */
public class ProVector3f extends Vector3f {

    public ProVector3f(float x, float y, float z) {

        this.setX(x);
        this.setY(y);
        this.setZ(z);
    }
    /*
     * Additional methdos to be added to the Vector3f Class
     *
     */
    // By tim8dev

    public boolean equalsByCoordinates(Vector3f v, int maxDiff) {
        if (v.x == x && v.y == y && v.z == z) {
            return true;
        }
        if (Math.abs(x - v.x) <= maxDiff) {
            if (Math.abs(y - v.y) <= maxDiff) {
                if (Math.abs(z - v.z) <= maxDiff) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean equalsByProperties(Vector3f v) {
        // 2 vectors are officially equal, when they are equal by properties, and when they are equal by properties
        //they should be parallel and have same magnitude.
        return (this.parallelTo(v) && this.equalMagnitude(v));
    }

    public boolean orthogonalTo(Vector3f v) {
        // two vectors are orthogonal if thier dot product result is equal to zero.
        return (this.dot(v) == 0f);
    }

    public boolean parallelTo(Vector3f v) {
        // two vectors are equal when their coordinates ratio are equal.
        return (this.getX() / v.x == this.getY() / v.y && this.getY() / v.y == this.getZ() / v.z);
    }

    public boolean equalMagnitude(Vector3f v) {
        return (v.length() == this.length());
    }
}



Enjoy!

So, I'm glad to here, that my initial topic lead to even more features!



Well, I used the coordinates, because they're used in the equals() method and it follows the java standard :smiley:

tim8dev said:

So, I'm glad to here, that my initial topic lead to even more features!

Well, I used the coordinates, because they're used in the equals() method and it follows the java standard :D


yes i understand :)