/*
 * Copyright (c) 1999-2019 Hartmut Seichter
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#ifndef PW_CORE_VECTOR_HPP
#define PW_CORE_VECTOR_HPP

#include <pw/core/matrix.hpp>

namespace pw {

template <typename T>
struct vector2_ : matrix_<2,1,T> {

    typedef matrix_<2,1,T> base_type;

    using base_type::base_type;
    using base_type::operator = ;

    vector2_(const base_type& m) : base_type(m) {}
    vector2_(T x_,T y_) : base_type({x_,y_}) {}

	inline const T& x() const { return (*this)[0]; }
	inline T& x() { return (*this)[0]; }

	inline const T& y() const { return (*this)[1]; }
	inline T& y() { return (*this)[1]; }

    inline auto homogenous(T w = 1) const { return matrix_<3,1,T>( { x(),y(),w } ); }

    static T angle_between(const vector2_ &a,const vector2_ &b) {
        return std::acos( dot( a.normalized(), b.normalized() ) );
    }

};

template <typename T>
struct vector3_ : matrix_<3,1,T>  {

    typedef matrix_<3,1,T> base_type;

    using base_type::base_type;
    using base_type::operator = ;

    vector3_() : base_type() {}
    vector3_(const base_type& m) : base_type(m) {}
    vector3_(T x_,T y_,T z_) : base_type({x_,y_,z_}) {}
	vector3_(const vector2_<T> &m, T w) : base_type({m(0),m(1),w}) {}

    inline const T& x() const { return (*this)[0]; }
    inline T& x() { return (*this)[0]; }
    inline vector3_& set_x(T val) { (*this)[0] = val; return *this;}

    inline const T& y() const { return (*this)[1]; }
    inline T& y() { return (*this)[1]; }
    inline vector3_& set_y(T val) { (*this)[1] = val; return *this;}

    inline const T& z() const { return (*this)[2]; }
    inline T& z() { return (*this)[2]; }
    inline vector3_& set_z(T val) { (*this)[2] = val; return *this;}

    inline auto xy() const { return vector2_( { x(),y() } ); }
    inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); }

    inline const vector3_ cross(const vector3_& rhs) const {
        return vector3_( {
                             (*this)[1] * rhs[2] - rhs[1] * (*this)[2],
                             (*this)[2] * rhs[0] - rhs[2] * (*this)[0],
                             (*this)[0] * rhs[1] - rhs[0] * (*this)[1]
                         }
                         );
    }


    inline static vector3_<T> forward() { return vector3_<T> ( { T(0), T(0),-T(1) } ); }
    inline static vector3_<T> backward() { return vector3_<T>( { T(0), T(0), T(1) } ); }
    inline static vector3_<T> right() { return vector3_<T>   ( { T(1), T(0), T(0) } ); }
    inline static vector3_<T> left() { return vector3_<T>    ( {-T(1), T(0), T(0) } ); }
    inline static vector3_<T> up() { return vector3_<T>      ( { T(0), T(1), T(0) } ); }
    inline static vector3_<T> down() { return vector3_<T>    ( { T(0),-T(1), T(0) } ); }

    inline static vector3_<T> x_axis() { return vector3_<T> ( { T(1), T(0), T(0) } ); }
    inline static vector3_<T> y_axis() { return vector3_<T> ( { T(0), T(1), T(0) } ); }
    inline static vector3_<T> z_axis() { return vector3_<T> ( { T(0), T(0), T(1) } ); }
};

template <typename T>
struct vector4_ : matrix_<4,1,T>  {

    typedef matrix_<4,1,T> base_type;

    using base_type::base_type;
    using base_type::operator = ;

    vector4_(T x_,T y_,T z_,T w_) : base_type({x_,y_,z_,w_}) {}
    vector4_(const base_type& m) : base_type(m) {}
	vector4_(const vector3_<T> &m, T w) : base_type({m(0),m(1),m(2),w}) {}

	inline const T& x() const { return (*this)[0]; }
	inline T& x() { return (*this)[0]; }

	inline const T& y() const { return (*this)[1]; }
	inline T& y() { return (*this)[1]; }

	inline const T& z() const { return (*this)[2]; }
	inline T& z() { return (*this)[2]; }

	inline const T& w() const { return (*this)[3]; }
	inline T& w() { return (*this)[3]; }

	inline auto xyz() const { return vector3_<T>({ x(),y(),z() } ); }

	inline auto project() const { return vector3_<T>({ x()/w(),y()/w(),z()/w() } ); }
};

//
//
//

using vector2f = vector2_<float>;
using vector2d = vector2_<double>;
using vector2  = vector2_<real_t>;

using vector3f = vector3_<float>;
using vector3d = vector3_<double>;
using vector3  = vector3_<real_t>;

using vector4f = vector4_<float>;
using vector4d = vector4_<double>;
using vector4  = vector4_<real_t>;

}

#endif
