paella/Code/include/Skeleton/VectorFunctions.hpp

78 lines
2.2 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.
////////////////////////////////////////////////////////////////////////////////
#ifndef VECTOR_FUNCTIONS_HPP
#define VECTOR_FUNCTIONS_HPP
#include <cmath>
#include <SFML/System/Vector3.hpp>
using sf::Vector3;
template <class T>
Vector3<T> makeVector(Vector3<T> const& from, Vector3<T> const& to)
{
return to - from;
}
template <class T>
double dotProduct(Vector3<T> const& v1, Vector3<T> const& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
template <class T>
double norm2(Vector3<T> const& vecteur)
{
return dotProduct(vecteur, vecteur);
}
template <class T>
double norm(Vector3<T> const& vecteur)
{
return sqrt(norm2(vecteur));
}
template <class T>
void normalize(Vector3<T>& vecteur)
{
vecteur /= norm(vecteur);
}
template <class T>
Vector3<T> crossProduct(Vector3<T> const& v1, Vector3<T> const& v2)
{
return Vector3<T>{v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x
};
}
#endif // VECTOR_FUNCTIONS_HPP