paella/Code/include/Geometry/Spline.hpp

150 lines
6.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 SPLINE_HPP
#define SPLINE_HPP
#include <vector>
#include <tuple>
#include <cmath>
#include <Geometry/Circle.hpp>
#include <Utility/TupleFunctions.hpp>
namespace geo
{
///////////////////////////////////////////////////////////////
/// \ingroup geometry
/// \brief Utility template class for manipulating B-splines
//////////////////////////////////////////////////////////////
template<typename... Types>
class Spline
{
public:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
/// Creates an empty spline (with no control points)
///////////////////////////////////////////////////////////
Spline();
///////////////////////////////////////////////////////////
/// \brief Evaluates the spline at a certain time
///
/// \param t Time at which you want to evaluate the spline. Must be between 0 and 1
///
/// \return Tuple that corresponds to the points of the spline at time t
///////////////////////////////////////////////////////////
std::tuple<Types...> operator()(float t) const;
///////////////////////////////////////////////////////////
/// \brief Evaluates the derivative of the spline at a certain time
///
/// \param t Time at which you want to evaluate the derivative of the
/// spline. Must be between 0 and 1
///
/// \return Tuple that corresponds to the derivative of each
/// coordinates of the spline at time t
///////////////////////////////////////////////////////////
std::tuple<Types...> prime(float t) const;
///////////////////////////////////////////////////////////
/// \brief Prints the spline to the stream
///
/// \param out stream to print the spline
/// \param spline spline to be printed on the stream
/// \return a reference to out
///
/// Prints first the degree of the spline, than the nodes and finally
/// the control points. For example
/// \code
/// d 3
///
/// n 0 0 0 0 0.333333 0.666667 1 1 1 1
///
/// p 0.60 0.43 0.51 0.142
/// p 0.60 0.89 0.36 0.135
/// p 0.72 1.28 0.24 0.129
/// p 0.96 1.72 0.14 0.125
/// p 1.27 2.10 0.06 0.122
/// p 1.69 2.29 0 0.12
/// \endcode
///////////////////////////////////////////////////////////
template<typename... T>
friend std::ostream& operator<<( std::ostream& out, Spline<T...> const& spline);
///////////////////////////////////////////////////////////
/// \brief Computes a circle around a spline
///
/// \param t instant of the spline where the circle will be computed
/// \param nbPoints number of the points on the circle
/// \param v up vector (the first point of the circle will be center + v)
/// \param offset offset for the indices in the mesh
/// \return a circle containing the center and the points of the circle
///////////////////////////////////////////////////////////
geo::Circle<float> computeCircle(float t, unsigned int const nbPoints, geo::Vector3<float>& v, unsigned int const offset = 0) const;
///////////////////////////////////////////////////////////
/// \brief Computes circles around the spline
/// \param nbCircles number of circles on the spline
/// \param nbPoints number of points on the circles
/// \param globalOffset offset for the indices in the mesh
/// \return a vector of the circles
///////////////////////////////////////////////////////////
std::vector<geo::Circle<float>> computeCircles(unsigned int nbCircles, unsigned int const nbPoints, unsigned int const globalOffset = 0) const;
// private:
///////////////////////////////////////////////////////////
/// \brief Add a control point to the spline
///
/// \param node String representation of the node to be added
///////////////////////////////////////////////////////////
void addControlPoint(std::string const& node);
std::vector<std::tuple<Types...>> controlPoints; ///< Vector of the control points of the spline
std::vector<float> nodes; ///< Vector of nodes of the spline
int degree; ///< Degree of the spline
};
namespace detail
{
template<typename... Types>
std::tuple<Types...> evalSpline(std::vector<std::tuple<Types...>> const& controlPoints , std::vector<float> const& nodes, int degree, float f);
template<typename... Types>
std::tuple<Types...> evalDerivativeSpline(std::vector<std::tuple<Types...>> const& controlPoints , std::vector<float> const& nodes, int degree, float f);
} // namespace detail
} // namespace geo
#include "Spline.inl"
#endif // SPLINE_HPP