Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

vec.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the QGLViewer library
00004  Copyright (C) 2002-2004  Gilles Debunne (Gilles.Debunne@imag.fr)
00005  Version 1.3.5 Release 8. Packaged on Monday December 22, 2003.
00006 
00007  http://www-imagis.imag.fr/Membres/Gilles.Debunne/CODE/QGLViewer
00008 
00009  libQGLViewer is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  libQGLViewer is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with libQGLViewer; if not, write to the Free Software
00021  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023 *****************************************************************************/
00024 
00025 #ifndef QGLVIEWER_VEC_H
00026 #define QGLVIEWER_VEC_H
00027 
00028 #include <math.h>
00029 #include <iostream>
00030 #include <qdom.h>
00031 #include "qapplication.h"
00032 
00033 // Included by all files as vec.h is at the end of the include hierarchy
00034 #include "config.h" // Specific (win32) configuration.
00035 
00036 namespace qglviewer {
00037 
00071   class QGLVIEWER_EXPORT Vec
00072   {
00073   public:
00075     float x, y, z;
00076 
00079 
00080     explicit Vec() : x(0.0), y(0.0), z(0.0) {}
00081 
00083     explicit Vec(const float X, const float Y, const float Z) : x(X), y(Y), z(Z) {}
00084 
00087 
00096     template <class C>
00097     explicit Vec(const C& c) : x(c[0]), y(c[1]), z(c[2]) {}
00098 
00100     // explicit Vec(const Vec& v) : x(v.x), y(v.y), z(v.z) {}
00101 
00103     Vec& operator=(const Vec& v)
00104     {
00105       x = v.x;   y = v.y;   z = v.z;
00106       return *this;
00107     }
00108 
00110     void setValue(const float X, const float Y, const float Z)
00111     { x=X; y=Y; z=Z; }
00112     
00113     // Universal equal operator which allows the use of any type in place of Vec,
00114     // as long as the [] operator is implemented (v[0]=v.x, v[1]=v.y, v[2]=v.z).
00115     // template <class C>
00116     // Vec& operator=(const C& c)
00117     // {
00118     // x=c[0]; y=c[1]; z=c[2];
00119     // return *this;
00120     // }
00122     
00125 
00126     float operator[](int i) const { return (&x)[i]; }
00127 
00129     float& operator[](int i) { return (&x)[i]; }
00130 
00132     const float* address() const { return &x; };
00133 
00135     operator const float*() const { return &x; };
00137 
00140 
00141     friend Vec operator+(const Vec &a, const Vec &b)
00142     {
00143       return Vec(a.x+b.x, a.y+b.y, a.z+b.z);
00144     }
00145 
00147     friend Vec operator-(const Vec &a, const Vec &b)
00148     {
00149       return Vec(a.x-b.x, a.y-b.y, a.z-b.z);
00150     }
00151 
00153     friend Vec operator-(const Vec &a)
00154     {
00155       return Vec(-a.x, -a.y, -a.z);
00156     }
00157 
00159     friend Vec operator*(const Vec &a, const float k)
00160     {
00161       return Vec(a.x*k, a.y*k, a.z*k);
00162     }
00163 
00165     friend Vec operator*(float k, const Vec &a)
00166     {
00167       return Vec(a.x*k, a.y*k, a.z*k);
00168     }
00169 
00171     friend Vec operator/(const Vec &a, const float k)
00172     {
00173 #ifndef QT_NO_DEBUG
00174       if (fabs(k) < 1.0E-10)
00175     qWarning("Vec::operator / : dividing by a null value");
00176 #endif
00177       return Vec(a.x/k, a.y/k, a.z/k);
00178     }
00179 
00181     friend bool operator!=(const Vec &a, const Vec &b)
00182     {
00183       return !(a==b);
00184     }
00185 
00187     friend bool operator==(const Vec &a, const Vec &b)
00188     {
00189       const float epsilon = 1.0E-10f;
00190       return (a-b).sqNorm() < epsilon;
00191     }
00192 
00194     Vec& operator+=(const Vec &a)
00195     {
00196       x += a.x; y += a.y; z += a.z;
00197       return *this;
00198     }
00199 
00201     Vec& operator-=(const Vec &a)
00202     {
00203       x -= a.x; y -= a.y; z -= a.z;
00204       return *this;
00205     }
00206 
00208     Vec& operator*=(float k)
00209     {
00210       x *= k; y *= k; z *= k;
00211       return *this;
00212     }
00213 
00215     Vec& operator/=(float k)
00216     {
00217 #ifndef QT_NO_DEBUG
00218       if (fabs(k)<1.0E-10)
00219     qWarning("Vec::operator /= : dividing by a null value");
00220 #endif
00221       x /= k; y /= k; z /= k;
00222       return *this;
00223     }
00224 
00226     friend float operator*(const Vec &a, const Vec &b)
00227     {
00228       return a.x*b.x + a.y*b.y + a.z*b.z;
00229     }
00230 
00232     friend Vec cross(const Vec &a, const Vec &b)
00233     {
00234       return Vec(a.y*b.z - a.z*b.y,
00235          a.z*b.x - a.x*b.z,
00236          a.x*b.y - a.y*b.x);
00237     }
00238 
00240     friend Vec operator^(const Vec &a, const Vec &b)
00241     {
00242       return Vec(a.y*b.z - a.z*b.y,
00243          a.z*b.x - a.x*b.z,
00244          a.x*b.y - a.y*b.x);
00245     }
00247 
00250 
00251     float sqNorm() const { return x*x + y*y + z*z; }
00252 
00254     float norm() const { return sqrt(x*x + y*y + z*z); }
00255 
00257     Vec& normalize()
00258     {
00259       const float n = norm();
00260 #ifndef QT_NO_DEBUG
00261       if (n < 1.0E-10)
00262     qWarning("Vec::normalize : normalizing a null vector");
00263 #endif
00264       *this /= n;
00265       return *this;
00266     }
00268   
00271     void projectOnAxis(const Vec& dir);
00272     void projectOnPlane(const Vec& n);
00274 
00279     QDomElement domElement(const QString& name, QDomDocument& doc) const;
00280     void initFromDOMElement(const QDomElement& de);
00282   };
00283   
00284 } // namespace
00285 
00286 std::ostream& operator<<(std::ostream& o, const qglviewer::Vec&);
00287   
00288 #endif // QGLVIEWER_VEC_H

Generated on Fri Feb 27 12:01:39 2004 for Glitch by doxygen 1.3.2