GLCubicHermiteCurve.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Jacques Gasselin                                *
00003  *   jacquesgasselin@hotmail.com                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef GLCUBICHERMITECURVE_H
00021 #define GLCUBICHERMITECURVE_H
00022 
00023 #include "GLMatrix.h"
00030 namespace mathglpp
00031 {
00032 
00033 template <typename T>
00034 class GLCubicHermiteCurve4
00035 {
00036 public:
00037     enum { D = 4 };
00038     typedef T value_type;
00039     typedef GLVector4<value_type> vector_type;
00040     typedef GLMatrix<value_type> matrix_type;
00041 
00043     GLCubicHermiteCurve4(const value_type* cpoints)
00044     :basis(cpoints)
00045     {
00047         static value_type coeffs[16] =
00048             {  1,  0,  0,  0,
00049                0,  1,  0,  0,
00050               -3, -2,  3, -1,
00051                2,  1, -2,  1 };
00052 
00054         basis *= matrix_type(coeffs);
00055     }
00056 
00058     GLCubicHermiteCurve4(const vector_type* cpoints)
00059     {
00061         static value_type coeffs[16] =
00062             {  1,  0,  0,  0,
00063                0,  1,  0,  0,
00064               -3, -2,  3, -1,
00065                2,  1, -2,  1 };
00066 
00067         cpoints[0].copyTo(&basis[0]);
00068         cpoints[1].copyTo(&basis[4]);
00069         cpoints[2].copyTo(&basis[8]);
00070         cpoints[3].copyTo(&basis[12]);
00071 
00073         basis *= matrix_type(coeffs);
00074     }
00075 
00077     GLCubicHermiteCurve4(const vector_type& c0, const vector_type& c1, const vector_type& c2, const vector_type& c3)
00078     {
00080         static value_type coeffs[16] =
00081             {  1,  0,  0,  0,
00082                0,  1,  0,  0,
00083               -3, -2,  3, -1,
00084                2,  1, -2,  1 };
00085 
00086         c0.copyTo(&basis[0]);
00087         c1.copyTo(&basis[4]);
00088         c2.copyTo(&basis[8]);
00089         c3.copyTo(&basis[12]);
00090 
00092         basis *= matrix_type(coeffs);
00093     }
00094 
00095     GLCubicHermiteCurve4(const GLCubicHermiteCurve4& hc)
00096     :basis(hc.basis) {}
00097 
00099     ~GLCubicHermiteCurve4(){}
00100 
00102     inline void glVertex(GLclampf interp) const
00103     {
00104         value_type interpVec[D] = { 1, value_type(interp), value_type(interp * interp), value_type(interp * interp * interp) };
00105         basis.dot4(interpVec).glVertex();
00106     }
00107 
00109     inline vector_type interpolate(GLclampf interp) const
00110     {
00111         value_type interpVec[D] = { 1, value_type(interp), value_type(interp * interp), value_type(interp * interp * interp) };
00112         return basis.dot4(interpVec);
00113     }
00114 
00116     void interpolate(GLclampf interp, value_type* vertex) const
00117     {
00118         vertex[0] = 1;
00119         vertex[1] = value_type(interp);
00120         vertex[2] = value_type(interp * interp);
00121         vertex[3] = value_type(interp * interp * interp);
00122         basis.vdot4(vertex);
00123     }
00124 
00125     inline vector_type operator()(GLclampf interp) const
00126     {
00127         value_type interpVec[D] = { 1, value_type(interp), value_type(interp * interp), value_type(interp * interp * interp) };
00128         return basis.dot4(interpVec);
00129     }
00130 
00131     inline void operator()(GLclampf interp, value_type* v) const
00132     {
00133         v[0] = 1;
00134         v[1] = value_type(interp);
00135         v[2] = value_type(interp * interp);
00136         v[3] = value_type(interp * interp * interp);
00137         basis.vdot4(v);
00138     }
00139 
00140 private:
00141     matrix_type basis;
00142 };
00143 
00144 
00146 typedef GLCubicHermiteCurve4<GLbyte> GLCubicHermiteCurve4b;
00148 typedef GLCubicHermiteCurve4<GLshort> GLCubicHermiteCurve4s;
00150 typedef GLCubicHermiteCurve4<GLint> GLCubicHermiteCurve4i;
00152 typedef GLCubicHermiteCurve4<GLfloat> GLCubicHermiteCurve4f;
00154 typedef GLCubicHermiteCurve4<GLdouble> GLCubicHermiteCurve4d;
00155 
00156 };
00157 #endif

Generated on Wed Oct 3 12:50:48 2007 for MathGL++ by  doxygen 1.5.2