1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file maths/Spline.h
22 * @brief Cubic spline matrix.
25 #ifndef _FILE_maths_spline_h
26 #define _FILE_maths_spline_h
33 /// Definition of a cubic spline type.
35 * @param T The type to use for components.
38 class CubicSpline
: public Matrix
<4, T
>
41 /// The value to divide all others by.
45 /// Copy the matrix from an array of vectors.
47 * @param columns Pointer to array of column vectors.
48 * @param newDivisor The value to divide all others by.
49 * @pre @p newDivisor != 0
51 inline CubicSpline(const Vector
<4, T
> * columns
, T newDivisor
= (T
)1)
52 : Matrix
<4, T
>(columns
), divisor(newDivisor
)
56 /// Get the columns of the matrix as vectors.
58 * @param c1 First column.
59 * @param c2 Second column.
60 * @param c3 Third column.
61 * @param c4 Forth column.
62 * @param newDivisor The value to divide all others by.
63 * @pre @p newDivisor != 0
65 inline CubicSpline(const Vector
<4, T
> & c1
, const Vector
<4, T
> & c2
,
66 const Vector
<4, T
> & c3
, const Vector
<4, T
> & c4
,
68 : Matrix
<4, T
>(c1
, c2
, c3
, c4
), divisor(newDivisor
)
72 /// Perform spline interpolation using the spline definition.
74 * @param U Data type to interpolate, should have arithmetic operators defined.
75 * @param F Floating point data type to use for interpolation values.
76 * @param u Interpolation value.
77 * @param u2 Interpolation value squared.
78 * @param u3 Interpolation value cubed.
79 * @param cp0 First control point.
80 * @param cp1 Second control point.
81 * @param cp2 Third control point.
82 * @param cp3 Forth control point.
84 template <typename U
, typename F
>
85 inline U
operator () (F u
, F u2
, F u3
,
86 U cp0
, U cp1
, U cp2
, U cp3
) const
88 return (cp0
* (u3
*Matrix
<4,T
>::col
[0][0] + u2
*Matrix
<4,T
>::col
[0][1] + u
*Matrix
<4,T
>::col
[0][2] + Matrix
<4,T
>::col
[0][3]) +
89 cp1
* (u3
*Matrix
<4,T
>::col
[1][0] + u2
*Matrix
<4,T
>::col
[1][1] + u
*Matrix
<4,T
>::col
[1][2] + Matrix
<4,T
>::col
[1][3]) +
90 cp2
* (u3
*Matrix
<4,T
>::col
[2][0] + u2
*Matrix
<4,T
>::col
[2][1] + u
*Matrix
<4,T
>::col
[2][2] + Matrix
<4,T
>::col
[2][3]) +
91 cp3
* (u3
*Matrix
<4,T
>::col
[3][0] + u2
*Matrix
<4,T
>::col
[3][1] + u
*Matrix
<4,T
>::col
[3][2] + Matrix
<4,T
>::col
[3][3])) / divisor
;
95 /// Defines a method of cube spline.
97 * This consists of the main spline, used for interpolating positions, and a
98 * tangent spline used for finding the tangent to the curve at a particular
100 * @param T component type of the cubic splines.
102 template <typename T
>
103 class CubicSplineDefinition
106 /// Cubic spline for main curve position vectors.
108 * @note This can be used as a function, see CubicSpline<T>::operator ()
110 CubicSpline
<T
> Spline
;
112 /// Cubic spline for the tangent to the curve.
114 * @note This can be used as a function, see CubicSpline<T>::operator ()
116 CubicSpline
<T
> Tangent
;
121 * @param newSpline Main curve position spline.
122 * @param newTangent Tangent to the curve spline.
124 inline CubicSplineDefinition(const CubicSpline
<T
> & spline
, const CubicSpline
<T
> & tangent
)
125 : Spline(spline
), Tangent(tangent
)
131 #endif // _FILE_maths_spline_h