190 lines
4.5 KiB
C++
190 lines
4.5 KiB
C++
////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Paella
|
|
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
|
|
// Authors :
|
|
// Thomas FORGIONE
|
|
// Emilie JALRAS
|
|
// Marion LENFANT
|
|
// Thierry MALON
|
|
// Amandine PAILLOUX
|
|
//
|
|
// This file is part of the project Paella
|
|
// This software is provided 'as-is', without any express or implied warranty.
|
|
// In no event will the authors be held liable for any damages arising from the use of this software.
|
|
//
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
// including commercial applications, and to alter it and redistribute it freely,
|
|
// subject to the following restrictions:
|
|
//
|
|
// 1. The origin of this software must not be misrepresented;
|
|
// you must not claim that you wrote the original software.
|
|
// If you use this software in a product, an acknowledgment
|
|
// in the product documentation would be appreciated but is not required.
|
|
//
|
|
// 2. Altered source versions must be plainly marked as such,
|
|
// and must not be misrepresented as being the original software.
|
|
//
|
|
// 3. This notice may not be removed or altered from any source distribution.
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
#include <iostream>
|
|
#include <cmath>
|
|
#include "Geometry/Vector.hpp"
|
|
|
|
namespace geo
|
|
{
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N>::Vector() : data{}
|
|
{
|
|
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
template<typename...Types>
|
|
constexpr Vector<T,N>::Vector(Types... l) : data{{static_cast<T>(l)...}}
|
|
{
|
|
|
|
}
|
|
template<typename T, std::size_t N>
|
|
template<typename U>
|
|
Vector<T,N>::Vector(Vector<U,N> const& v) : data(v.data)
|
|
{
|
|
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
T Vector<T,N>::norm2() const
|
|
{
|
|
return (*this) * (*this);
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
T Vector<T,N>::norm() const
|
|
{
|
|
return std::sqrt(norm2());
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
std::istream& operator>>(std::istream& stream, Vector<T,N>& v)
|
|
{
|
|
for (auto& d : v.data)
|
|
stream >> d;
|
|
|
|
return stream;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
std::ostream& operator<<(std::ostream& stream, Vector<T,N> const& v)
|
|
{
|
|
stream << "[" << v.data[0];
|
|
for (std::size_t i = 1; i < N; i++)
|
|
stream << ", " << v.data[i];
|
|
|
|
return stream << "]";
|
|
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
bool operator==(Vector<T,N> const& l, Vector<T,N> const& r)
|
|
{
|
|
bool ret = true;
|
|
for (std::size_t i = 0; i < N; i++)
|
|
ret = ret && (l.data[i] == r.data[i]);
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
bool operator!=(Vector<T,N> const& l, Vector<T,N> const& r)
|
|
{
|
|
return !(l == r);
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> Vector<T,N>::operator-()
|
|
{
|
|
Vector<T,N> ret;
|
|
for (std::size_t i = 0; i < N; i++)
|
|
{
|
|
ret.data[i] = -data[i];
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N>& Vector<T,N>::operator+=(Vector<T,N> const& v)
|
|
{
|
|
for (std::size_t i = 0; i < N; i++)
|
|
data[i] += v.data[i];
|
|
|
|
return *this;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N>& Vector<T,N>::operator-=(Vector<T,N> const& v)
|
|
{
|
|
for (std::size_t i = 0; i < N; i++)
|
|
data[i] -= v.data[i];
|
|
|
|
return *this;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N>& Vector<T,N>::operator*=(float f)
|
|
{
|
|
for (std::size_t i = 0; i < N; i++)
|
|
data[i] *= f;
|
|
|
|
return *this;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N>& Vector<T,N>::operator/=(float f)
|
|
{
|
|
for (std::size_t i = 0; i < N; i++)
|
|
data[i] /= f;
|
|
|
|
return *this;
|
|
}
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> operator+(Vector<T,N> t, Vector<T,N> const& r) { return t+=r; }
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> operator-(Vector<T,N> t, Vector<T,N> const& r) { return t-=r; }
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> operator*(Vector<T,N> l, float r) { return l*=r; }
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> operator*(float r, Vector<T,N> l) { return l*r; }
|
|
|
|
template<typename T, std::size_t N>
|
|
Vector<T,N> operator/(Vector<T,N> l, float r) { return l/=r; }
|
|
|
|
template<typename T, std::size_t N>
|
|
T operator*(Vector<T,N> const& l, Vector<T,N> const& r)
|
|
{
|
|
T ret = 0;
|
|
|
|
for (std::size_t i = 0; i < N; i++)
|
|
ret += (l.data[i] * r.data[i]);
|
|
|
|
return ret;
|
|
}
|
|
|
|
template <typename T>
|
|
Vector<T,3> crossProduct(Vector<T,3> const& v1, Vector<T,3> const& v2)
|
|
{
|
|
return Vector<T,3>{
|
|
v1.y() * v2.z() - v1.z() * v2.y(),
|
|
v1.z() * v2.x() - v1.x() * v2.z(),
|
|
v1.x() * v2.y() - v1.y() * v2.x()
|
|
};
|
|
}
|
|
|
|
} // namespace geo
|
|
|
|
|