#ifndef _OF_VEC4f
#define _OF_VEC4f


#ifndef DEG_TO_RAD
	#define DEG_TO_RAD 0.0174532925199433
#endif

#ifndef RAD_TO_DEG
	#define RAD_TO_DEG 57.2957795130823
#endif



class ofVec4f {

  public:
	
	
    //----------------  Constructors
    
    ofVec4f( const float _x = 0.0f, 
             const float _y = 0.0f,  
             const float _z = 0.0f,
             const float _w = 0.0f  )
    {
        x = _x;
        y = _y;
        z = _z;
        w = _w;
    }
	
    ofVec4f( const ofVec4f& vec )  
    {
        x = vec.x;
        y = vec.y;
        z = vec.z;
        w = vec.w;
    }
    
    
    
    
    //---------------- Data Members
    float x;
    float y;
    float z;
    float w;
    
   
   
   
    //---------------- Methods


    // Getters and Setters.
    //
    //     
    void set( float _x, float _y, float _z, float _w )
    {
        x = _x;
        y = _y;
        z = _z;
        w = _w;
    }
    
    void set( const ofVec4f &vec )
    {
        x = vec.x;
        y = vec.y;
        z = vec.z;
        w = vec.w;
    }

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

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

    bool operator!=( const ofVec4f vec )           
    { 
        return (x != vec.x) || (y != vec.y) || (z != vec.z) || (w != vec.w);
    }
    
    bool match( const ofVec4f vec, float tollerance=0.0001) {
        return (fabs(x - vec.x) < tollerance) 
            && (fabs(y - vec.y) < tollerance)
            && (fabs(z - vec.z) < tollerance)
            && (fabs(w - vec.w) < tollerance);
    }    
    
    /**
    * Checks if vectors look in the same direction.
    */      
    bool align( const ofVec4f& vec ) const
    {
        return (x*vec.x + y*vec.y + z*vec.z + w*vec.w) > 0.0;
    }    
    
    

    // Additions and Subtractions.
    //
    //
    ofVec4f operator+( const ofVec4f& vec ) const          
    { 
        return ofVec4f( x+vec.x, y+vec.y, z+vec.z, w+vec.w); 
    } 
    
    ofVec4f& operator+=( const ofVec4f& vec )              
    { 
        x += vec.x;
        y += vec.y;
        z += vec.z;
        w += vec.w;
        return *this; 
    } 
    
    ofVec4f operator-( const ofVec4f& vec ) const          
    { 
        return ofVec4f( x-vec.x, y-vec.y, z-vec.z, w-vec.w ); 
    } 
    
    ofVec4f& operator-=( const ofVec4f& vec )              
    { 
        x -= vec.x;
        y -= vec.y;
        z -= vec.z;
        w -= vec.w;
        return *this; 
    } 

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


    // Scalings
    //
    //
    ofVec4f operator*( const ofVec4f& vec ) const
    { 
        return ofVec4f( x*vec.x, y*vec.y, z*vec.z, w*vec.w ); 
    } 
    
    ofVec4f& operator*=( const ofVec4f& vec )      
    { 
        x *= vec.x;
        y *= vec.y;
        z *= vec.z;
        w *= vec.w;
        return *this; 
    } 

    ofVec4f operator*( const float f ) const                
    { 
        return ofVec4f( x*f, y*f, z*f, w*f );
    } 
    
    ofVec4f& operator*=( const float f )                    
    { 
        x *= f;
        y *= f;
        z *= f;
        w *= f;
        return *this; 
    } 
    
    ofVec4f operator/( const ofVec4f& vec ) const  
    { 
        return ofVec4f( x/vec.x, y/vec.y, z/vec.z, w/vec.w );
    } 
    
    ofVec4f& operator/=( const ofVec4f& vec )      
    { 
        x /= vec.x;
        y /= vec.y;
        z /= vec.z;
        w /= vec.w;
        return *this; 
    } 

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

    ofVec4f& operator/=( const float f )          
    { 
        x /= f;
        y /= f;
        z /= f;
        w /= f;
        return *this; 
    }
    
    ofVec4f rescaled( const float length ) const
    { 
        float l = (float)sqrt(x*x + y*y + z*z + w*w);
        if( l > 0 )
            return ofVec4f( (x/l)*length, (y/l)*length, 
                            (z/l)*length, (w/l)*length );
        else
            return ofVec4f();
    }
        
    ofVec4f& rescale( const float length )
    { 
        float l = (float)sqrt(x*x + y*y + z*z + w*w);
        if (l > 0) {
            x = (x/l)*length;
            y = (y/l)*length;
            z = (z/l)*length;
            w = (w/l)*length;
        }
        return *this;
    }

    

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

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

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


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

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

#endif
