144 lines
4.9 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_VEC_HPP
#define REXY_VEC_HPP
#include "mat.hpp"
namespace math{
//class representing vectors
//inherit from matrix base because it also shared matrix attributes
template<class T, size_t R>
class vector : public matrix_base<T,R,1>
{
private:
using base = matrix_base<T,R,1>;
public:
using value_type = typename base::value_type;
using size_type = typename base::size_type;
using pointer = typename base::pointer;
using const_pointer = typename base::const_pointer;
using reference = typename base::reference;
using const_reference = typename base::const_reference;
public:
using base::base;
template<size_t TR,class... Args,std::enable_if_t<TR <= R && (std::is_convertible_v<Args,T> && ...),int> = 0>
constexpr vector(const vector<T,TR>& other, Args&&... args);
template<class U>
constexpr vector(const vector<U,R>& other);
constexpr vector(const vector&) = default;
constexpr vector(vector&&) = default;
~vector(void) = default;
//Assignement
constexpr vector& operator=(const vector&) = default;
constexpr vector& operator=(vector&&) = default;
template<class U, size_t TR>
constexpr vector& operator=(const vector<U,TR>& m);
constexpr reference operator[](size_type i);
constexpr const_reference operator[](size_type i)const;
constexpr reference x(void);
constexpr const_reference x(void)const;
template<class U = T>
constexpr reference y(void);
template<class U = T>
constexpr const_reference y(void)const;
template<class U = T>
constexpr reference z(void);
template<class U = T>
constexpr const_reference z(void)const;
template<class U = T>
constexpr reference w(void);
template<class U = T>
constexpr const_reference w(void)const;
value_type magnitude(void)const;
vector normalize(void);
protected:
template<class U, class... Args>
constexpr void assign_(size_type offset, U&& u, Args&&... args);
};
namespace detail{
template<class T>
struct is_vector_helper {
template<class U, size_t R>
static std::true_type test(vector<U,R>*);
static std::false_type test(void*);
static constexpr bool value = std::is_same<std::true_type,decltype(test(static_cast<std::decay_t<T>*>(nullptr)))>::value;
};
}
template<class... Ms>
struct is_vector{
static constexpr bool value = (detail::is_vector_helper<Ms>::value && ...);
};
template<class T>
concept Vector = is_vector<T>::value;
template<class T>
constexpr auto perp(const vector<T,2>& v);
template<class T, class U, size_t R1, size_t R2>
constexpr auto perp(const vector<T,R1>& left, const vector<U,R2>& right);
template<class T, class U>
constexpr auto cross(const vector<T,3>& left, const vector<U,3>& right);
template<class T, size_t R>
constexpr auto magnitude(const vector<T,R>& v);
template<class T, class U, size_t C, size_t R>
constexpr auto operator*(const matrix<U,R,C>& left, const vector<T,C>& right);
template<class T, class U, size_t R>
constexpr auto operator*(const vector<T,R>& left, const vector<U,R>& right);
template<class T, class U, size_t R> requires Compatible_Scalar<U,T>
constexpr auto operator*(const vector<T,R>& left, U&& right);
template<class T, class U, size_t R> requires Compatible_Scalar<U,T>
constexpr auto operator*(U&& left, const vector<T,R>& right);
template<class T, class U, size_t R> requires Compatible_Scalar<U,T>
constexpr auto operator/(const vector<T,R>& left, U&& right);
template<class T, class U, size_t R>
constexpr auto operator+(const vector<T,R>& left, const vector<U,R>& right);
template<class T, class U, size_t R>
constexpr auto operator-(const vector<T,R>& left, const vector<U,R>& right);
template<class T, class U, size_t R>
constexpr auto operator-(const vector<T,R>& left);
template<class T, class U, size_t R> requires Compatible_Scalar<U,T>
constexpr decltype(auto) operator*=(vector<T,R>& left, U&& right);
template<class T, class U, size_t R> requires Compatible_Scalar<U,T>
constexpr decltype(auto) operator/=(vector<T,R>& left, U&& right);
template<class T, class U, size_t R>
constexpr decltype(auto) operator+=(vector<T,R>& left, const vector<U,R>& right);
template<class T, class U, size_t R>
constexpr decltype(auto) operator-=(vector<T,R>& left, const vector<U,R>& right);
}
#include "vec.tpp"
#endif