Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members

glvector.h

00001 /***************************************************************************
00002                           glvector.h  -  description
00003                              -------------------
00004     begin                : Mon Apr 28 2003
00005     copyright            : (C) 2003 by Jacques Gasselin De Richebourg
00006     email                : jgasseli@hotmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU Lesser General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This library is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this library; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
00024  *                                                                         *
00025  ***************************************************************************/
00026 
00027 #ifndef GLVECTOR_H
00028 #define GLVECTOR_H
00029 
00030 #include <GL/gl.h>
00031 #include <iostream>
00032 #include "glassert.h"
00033 
00040 class GLVector
00041 {
00042    GLfloat val[3];
00043    //unit values
00044    
00045 public:
00046    static char tag[20];
00047    
00048    GLVector() { val[0] = 0; val[1] = 0; val[2] = 0; }
00049    GLVector(GLfloat i, GLfloat j, GLfloat k)
00050    { val[0] = i; val[1] = j; val[2] = k; }
00051    GLVector(const GLVector& gv)
00052    { val[0] = gv.val[0]; val[1] = gv.val[1]; val[2] = gv.val[2]; }
00053    GLVector(const GLfloat*);
00054    ~GLVector() {};
00055 
00056    TEST_FUNC( void unitTest() );
00057 
00058    
00059    inline void set(const GLVector& v)
00060    { val[0] = v.val[0]; val[1] = v.val[1]; val[2] = v.val[2]; }
00061 
00062    inline void set(const GLfloat* f)
00063    { val[0] = f[0]; val[1] = f[1]; val[2] = f[2]; }
00064    inline void set(GLfloat _i, GLfloat _j, GLfloat _k)
00065    { val[0] = _i; val[1] = _j; val[2] = _k; }
00066 
00067    inline const GLfloat* get() const { return val; }
00068    inline const GLfloat getX() const { return val[0]; }
00069    inline const GLfloat getY() const { return val[1]; }
00070    inline const GLfloat getZ() const { return val[2]; }
00071 
00072    inline void glScale() const { ::glScalef(val[0], val[1], val[2]); }
00073    inline void glRotate(GLfloat ang) const { ::glRotatef(ang, val[0], val[1], val[2]); }
00074    inline void glTranslate() const { ::glTranslatef(val[0],val[1],val[2]); }
00075    inline void glUnTranslate() const { ::glTranslatef(-val[0],-val[1],-val[2]); }
00076    inline void glVertex() const { ::glVertex3fv(val); }
00077    inline void glNormal() const { ::glNormal3fv(val); }
00078    
00079    inline const GLVector operator + (const GLVector gv) const
00080    { return GLVector(val[0] + gv.val[0], val[1] + gv.val[1], val[2] + gv.val[2]);}
00081    inline const GLVector operator - (const GLVector gv) const
00082    { return GLVector(val[0] - gv.val[0], val[1] - gv.val[1], val[2] - gv.val[2]);}
00083    inline const GLVector operator * (const GLfloat f) const
00084    { return GLVector(val[0] * f, val[1] * f, val[2] * f);}
00085    inline const GLVector operator / (const GLfloat f) const
00086    {customAssert( f != 0, "Division by zero in GLVector::operator /");
00087       return GLVector(val[0] / f, val[1] / f, val[2] / f);
00088    }
00089 
00090    GLVector& operator += (const GLVector gv);
00091    GLVector& operator -= (const GLVector gv);
00092    GLVector& operator *= (const GLfloat f);
00093    GLVector& operator /= (const GLfloat f);
00094 
00095    GLVector& operator = ( const GLVector& v )
00096    { val[0] = v.val[0]; val[1] = v.val[1]; val[2] = v.val[2]; return *this; }
00097    
00098    inline const GLVector operator - () const { return GLVector(-val[0], - val[1], -val[2]); }
00099    inline const GLfloat dot(const GLVector& gv) const
00100    { return ( val[0]*gv. val[0] + val[1]*gv.val[1] + val[2]*gv.val[2]); }
00101    GLfloat length() const;
00102    inline GLfloat lengthSqr() const
00103    { return val[0]*val[0] + val[1]*val[1] + val[2]*val[2]; }
00104    
00105    const GLVector getCross(const GLVector& gv) const;
00106    GLVector& cross(const GLVector& gv);
00107    const GLVector unit() const;
00108    GLVector& normalize();
00109 
00110    inline const GLfloat projection(GLVector in) const
00111    { return dot(in); }
00112 
00113    inline GLVector orthogonalProjection(const GLVector& in) const
00114    { return in - vectorProjection(in); }
00115    
00116    GLVector vectorProjection(const GLVector& in) const;
00117    void saveToStream(std::ostream& out);
00118 
00119    friend std::basic_istream<char, std::char_traits<char> >& operator >>
00120       ( std::basic_istream<char, std::char_traits<char> >& in, GLVector& vec)
00121    { in>>vec.val[0]>>vec.val[1]>>vec.val[2]; return in;};
00122 
00123    friend std::basic_ostream<char, std::char_traits<char> >& operator <<
00124       ( std::basic_ostream<char, std::char_traits<char> >& out, GLVector& vec)
00125    { out<<vec.val[0]<<" "<<vec.val[1]<<" "<<vec.val[2]; return out;};
00126 
00127 };
00128 
00129 
00130 #endif

Generated on Wed Feb 4 23:11:34 2004 by doxygen 1.3.3