#ifndef _OFX_VEC3f
#define _OFX_VEC3f

#include "ofConstants.h"




class ofxVec3f : public ofPoint {


  public:

    ofxVec3f( float _x=0.0f,
             float _y=0.0f,
             float _z=0.0f )
    {
        x = _x;
        y = _y;
        z = _z;
    }




    // Getters and Setters.
    //
    //
    void set( float _x, float _y, float _z ) {
        x = _x;
        y = _y;
        z = _z;
    }

    void set( const ofxVec3f &vec ) {
        x = vec.x;
        y = vec.y;
        z = vec.z;
    }

    float &operator[]( const int &i ) {
        switch(i) {
            case 0:  return x;
            case 1:  return y;
            case 2:  return z;
            default: return x;
        }
    }



    // Check similarity/equality.
    //
    //
    bool operator==( const ofxVec3f vec ) {
        return (x == vec.x) && (y == vec.y) && (z == vec.z);
    }

    bool operator!=( const ofxVec3f vec ) {
        return (x != vec.x) || (y != vec.y) || (z != vec.z);
    }

    bool match( const ofxVec3f vec, float tollerance=0.0001 ) {
        return (fabs(x - vec.x) < tollerance)
            && (fabs(y - vec.y) < tollerance)
            && (fabs(z - vec.z) < tollerance);
    }

    /**
    * Checks if vectors look in the same direction.
    */
    bool align( const ofxVec3f& vec, float tollerance=0.0001 ) const {
        float angle = this->angle( vec );
        return  angle < tollerance;
    }



    // Additions and Subtractions.
    //
    //
    ofxVec3f operator+( const ofxVec3f& vec ) const {
        return ofxVec3f( x+vec.x, y+vec.y, z+vec.z);
    }

    ofxVec3f& operator+=( const ofxVec3f& vec ) {
        x += vec.x;
        y += vec.y;
        z += vec.z;
        return *this;
    }

    ofxVec3f operator+( const float f ) const {
        return ofxVec3f( x+f, y+f, z+f );
    }

    ofxVec3f& operator+=( const float f ) {
        x += f;
        y += f;
        z += f;
        return *this;
    }

    ofxVec3f operator-( const ofxVec3f& vec ) const {
        return ofxVec3f( x-vec.x, y-vec.y, z-vec.z );
    }

    ofxVec3f& operator-=( const ofxVec3f& vec ) {
        x -= vec.x;
        y -= vec.y;
        z -= vec.z;
        return *this;
    }

    ofxVec3f operator-( const float f ) const {
        return ofxVec3f( x-f, y-f, z-f );
    }

    ofxVec3f& operator-=( const float f ) {
        x -= f;
        y -= f;
        z -= f;
        return *this;
    }

    ofxVec3f operator-() const {
        return ofxVec3f( -x, -y, -z );
    }


    // Scalings
    //
    //
    ofxVec3f operator*( const ofxVec3f& vec ) const {
        return ofxVec3f( x*vec.x, y*vec.y, z*vec.z );
    }

    ofxVec3f& operator*=( const ofxVec3f& vec ) {
        x *= vec.x;
        y *= vec.y;
        z *= vec.z;
        return *this;
    }

    ofxVec3f operator*( const float f ) const {
        return ofxVec3f( x*f, y*f, z*f );
    }

    ofxVec3f& operator*=( const float f ) {
        x *= f;
        y *= f;
        z *= f;
        return *this;
    }

    ofxVec3f operator/( const ofxVec3f& vec ) const {
        return ofxVec3f( x/vec.x, y/vec.y, z/vec.z );
    }

    ofxVec3f& operator/=( const ofxVec3f& vec ) {
        x /= vec.x;
        y /= vec.y;
        z /= vec.z;
        return *this;
    }

    ofxVec3f operator/( const float f ) const {
        return ofxVec3f( x/f, y/f, z/f );
    }

    ofxVec3f& operator/=( const float f ) {
        x /= f;
        y /= f;
        z /= f;
        return *this;
    }

    ofxVec3f rescaled( const float length ) const {
        float l = (float)sqrt(x*x + y*y + z*z);
        if( l > 0 )
            return ofxVec3f( (x/l)*length, (y/l)*length, (z/l)*length );
        else
            return ofxVec3f();
    }

    ofxVec3f& rescale( const float length ) {
        float l = (float)sqrt(x*x + y*y + z*z);
        if (l > 0) {
            x = (x/l)*length;
            y = (y/l)*length;
            z = (z/l)*length;
        }
        return *this;
    }



    // Rotation
    //
    //
    ofxVec3f rotated( float angle, const ofxVec3f& axis ) const {
        ofxVec3f ax = axis.normalized();
        float a = angle*DEG_TO_RAD;
        float sina = sin( a );
        float cosa = cos( a );
        float cosb = 1.0 - cosa;

        return ofxVec3f( x*(ax.x*ax.x*cosb + cosa)
                      + y*(ax.x*ax.y*cosb - ax.z*sina)
                      + z*(ax.x*ax.z*cosb + ax.y*sina),
                        x*(ax.y*ax.x*cosb + ax.z*sina)
                      + y*(ax.y*ax.y*cosb + cosa)
                      + z*(ax.y*ax.z*cosb - ax.x*sina),
                        x*(ax.z*ax.x*cosb - ax.y*sina)
                      + y*(ax.z*ax.y*cosb + ax.x*sina)
                      + z*(ax.z*ax.z*cosb + cosa) );
    }

    ofxVec3f& rotate( float angle, const ofxVec3f& axis ) {
        ofxVec3f ax = axis.normalized();
        float a = angle*DEG_TO_RAD;
        float sina = sin( a );
        float cosa = cos( a );
        float cosb = 1.0 - cosa;

        float nx = x*(ax.x*ax.x*cosb + cosa)
          + y*(ax.x*ax.y*cosb - ax.z*sina)
          + z*(ax.x*ax.z*cosb + ax.y*sina);
        float ny = x*(ax.y*ax.x*cosb + ax.z*sina)
          + y*(ax.y*ax.y*cosb + cosa)
          + z*(ax.y*ax.z*cosb - ax.x*sina);
        float nz = x*(ax.z*ax.x*cosb - ax.y*sina)
          + y*(ax.z*ax.y*cosb + ax.x*sina)
          + z*(ax.z*ax.z*cosb + cosa);
        x = nx; y = ny; z = nz;
        return *this;
    }

    ofxVec3f rotated(float ax, float ay, float az) {
        float a = cos(DEG_TO_RAD*(ax));
        float b = sin(DEG_TO_RAD*(ax));
        float c = cos(DEG_TO_RAD*(ay));
        float d = sin(DEG_TO_RAD*(ay));
        float e = cos(DEG_TO_RAD*(az));
        float f = sin(DEG_TO_RAD*(az));

        float nx = c * e * x - c * f * y + d * z;
        float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
        float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;

        return ofxVec3f( nx, ny, nz );
    }

    ofxVec3f& rotate(float ax, float ay, float az) {
        float a = cos(DEG_TO_RAD*(ax));
        float b = sin(DEG_TO_RAD*(ax));
        float c = cos(DEG_TO_RAD*(ay));
        float d = sin(DEG_TO_RAD*(ay));
        float e = cos(DEG_TO_RAD*(az));
        float f = sin(DEG_TO_RAD*(az));

        float nx = c * e * x - c * f * y + d * z;
        float ny = (a * f + b * d * e) * x + (a * e - b * d * f) * y - b * c * z;
        float nz = (b * f - a * d * e) * x + (a * d * f + b * e) * y + a * c * z;

        x = nx; y = ny; z = nz;
        return *this;
    }



    // Normalization
    //
    //
    ofxVec3f normalized() const {
        float length = (float)sqrt(x*x + y*y + z*z);
        if( length > 0 ) {
            return ofxVec3f( x/length, y/length, z/length );
        } else {
            return ofxVec3f();
        }
    }

    ofxVec3f& normalize() {
        float lenght = (float)sqrt(x*x + y*y + z*z);
        if( lenght > 0 ) {
            x /= lenght;
            y /= lenght;
            z /= lenght;
        }
        return *this;
	}



    // Limit length.
    //
    //
	 ofxVec3f limited(float max) const {
        float length = (float)sqrt(x*x + y*y + z*z);
		if( length > max && length > 0 ) {
            return ofxVec3f( (x/length)*max, (y/length)*max, (z/length)*max );
		} else {
            return ofxVec3f( x, y, z );
        }
	}

    ofxVec3f& limit(float max) {
        float length = (float)sqrt(x*x + y*y + z*z);
		if( length > max && length > 0 ) {
            x = (x/length)*max;
            y = (y/length)*max;
            z = (z/length)*max;
        }
        return *this;
    }



    // Perpendicular vector.
    //
    //
    ofxVec3f cross( const ofxVec3f& vec ) const {
        return ofxVec3f( y*vec.z - z*vec.y,
                        z*vec.x - x*vec.z,
                        x*vec.y - y*vec.x );
    }

    /**
    * Normalized perpendicular.
    */
    ofxVec3f perpendicular( const ofxVec3f& vec ) const {
        float crossX = y*vec.z - z*vec.y;
        float crossY = z*vec.x - x*vec.z;
        float crossZ = x*vec.y - y*vec.x;

        float length = (float)sqrt(crossX*crossX +
                                   crossY*crossY +
                                   crossZ*crossZ);

        if( length > 0 )
            return ofxVec3f( crossX/length, crossY/length, crossZ/length );
        else
            return ofxVec3f();
    }



    // Length
    //
    //
    float length() const {
        return (float)sqrt( x*x + y*y + z*z );
    }

    float lengthSquared() const {
        return (float)(x*x + y*y + z*z);
    }



    /**
    * Angle (deg) between two vectors.
    * This is an unsigned relative angle from 0 to 180.
    * http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
    */
    float angle( const ofxVec3f& vec ) const {
        ofxVec3f n1 = this->normalized();
        ofxVec3f n2 = vec.normalized();
        return acos( n1.dot(n2) )*RAD_TO_DEG;
    }



    /**
    * Dot Product.
    */
    float dot( const ofxVec3f& vec ) const {
        return x*vec.x + y*vec.y + z*vec.z;
    }

};

#endif
