/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWEAR_POINT_H
#define __IWEAR_POINT_H

#ifndef __IWEAR_MVECTOR_H
#include <iwear/mvector.h>
#endif

#ifndef __IWEAR_IWM_H
#include <iwear/iwm.h>
#endif

namespace iwear
{
    namespace math
    {

template<class T>
struct Point2D
{
    T x;
    T y;
    inline Point2D<T>( ) : x(T()), y(T()) { }
    inline Point2D<T>(const T& _x, const T& _y) : x(_x), y(_y) { }
    inline bool operator==(const Point2D<T>& p ) const { return (x == p.x && y == p.y ); }
};

template<class T>
class Point3D : public MathVector<T>
{
private:
protected:
public:
    Point3D( ) : MathVector<T>(4) 
    { 
	this->operator[](3) = Help<T>::neutral_multiplication(this->operator[](0));
    }

    Point3D( const T& _x, const T& _y, const T& _z ) : MathVector<T>(4) 
    {
	this->operator[](0) = _x;
	this->operator[](1) = _y;
	this->operator[](2) = _z;
	this->operator[](3) = Help<T>::neutral_multiplication(this->operator[](0));
    }

    const T& get_x( void ) const { return this->operator[](0); }
    const T& get_y( void ) const { return this->operator[](1); }
    const T& get_z( void ) const { return this->operator[](2); }
    const T& get_scale( void ) const { return this->operator[](3); }
    void normalize( void ) { 
	this->operator[](0) /= this->operator[](3);
	this->operator[](1) /= this->operator[](3);
	this->operator[](2) /= this->operator[](3);
	this->operator[](3) = Help<T>::neutral_multiplication(this->operator[](0)); }
};

template<class T> 
std::istream& operator>>( std::istream& i, Point2D<T>& p )
{
    if( i.peek() == EOF )
    {
	i.clear();
	return i;
    }

    skipws(i);
    T x, y;
    if( i.get() != '(' )
    {
	i.setstate(i.failbit);
	return i;
    }
    i >> x;
//    std::cout << "x=" << x << std::endl;
    if( i.get() != ',' )
    {
	i.setstate(i.failbit);
	return i;
    }
    i >> y;
//    std::cout << "y=" << y << std::endl;
    if( i.get() != ')' )
    {
	i.setstate(i.failbit);
	return i;
    }
    p.x =x;
    p.y =y;
    return i;
}

template<class T>
std::ostream& operator<<( std::ostream& o, const Point2D<T>& p )
{
    o << "(";
    o << p.x;
    o << ",";
    o << p.y;
    o << ")";
    return o;
}

}// namespace math
}// namespace iwear

#endif
