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

vector.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: vector.h,v 1.7 2004/12/17 20:18:46 rollmark Exp $
00008 
00009    This program 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; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 
00022    Here we define/complete the vector definitions of TVector into specific
00023    vector types (2d float, 3d float, etc...).
00024 
00025    HISTORY:
00026                 12.08.2002 MK
00027                         - initial version
00028 
00029 */
00030 #ifndef SALT_VECTOR_H
00031 #define SALT_VECTOR_H
00032 
00033 // #ifdef HAVE_CONFIG_H
00034 // #include <config.h>
00035 // #endif
00036 
00037 #include "tvector.h"
00038 #include <cstdlib>
00039 #include <climits>
00040 
00041 namespace salt
00042 {
00043 
00044 class Vector2f : public TVector2<float, Vector2f>
00045 {
00046     //
00047     // Methods
00048     //
00049 public:
00051     f_inline        Vector2f()                                      : TVector2<float, Vector2f>() { }
00052     f_inline        Vector2f(float x, float y)      : TVector2<float, Vector2f>(x, y) { }
00053     explicit        Vector2f(const float *f)        : TVector2<float, Vector2f>() { SetData(f);  }
00054     f_inline        Vector2f(const Vector2f &v)     : TVector2<float, Vector2f>(v) { }
00055     explicit        Vector2f(float f)                       : TVector2<float, Vector2f>() { Fill(f); }
00056 
00057     float           GetAngleRad() const
00058     {
00059         const double length = Length();
00060         if (length == 0)
00061             {
00062                 return 0.0;
00063             }
00064 
00065         double rad = gArcCos(Get(0) / length);
00066 
00067         if (Get(1) < 0)
00068             {
00069                 rad = 2*M_PI - rad;
00070             }
00071 
00072         return rad;
00073     }
00074 
00075     float           GetAngleDeg() const
00076     {
00077         return gRadToDeg(GetAngleRad());
00078     }
00079 };
00080 
00081 class Vector3f : public TVector3<float, Vector3f>
00082 {
00083     //
00084     // Methods
00085     //
00086 public:
00088     f_inline        Vector3f()                                                      : TVector3<float, Vector3f>() { }
00089     f_inline        Vector3f(float x, float y, float z)     : TVector3<float, Vector3f>(x, y, z) { }
00090     explicit        Vector3f(const float *f)                        : TVector3<float, Vector3f>() { SetData(f);  }
00091     f_inline        Vector3f(const Vector3f &v)                     : TVector3<float, Vector3f>(v) { }
00092     explicit        Vector3f(float f)                                       : TVector3<float, Vector3f>() { Fill(f); }
00093 
00094     f_inline        Vector3f Reflect(const Vector3f &normal)
00095     {
00096         float fac = 2.0f * (normal.x() * x() + normal.y() * y() + normal.z() * z());
00097         return Vector3f(fac * normal.x()-x(),
00098                         fac * normal.y()-y(),
00099                         fac * normal.z()-z());
00100     }
00101 
00102     f_inline void RandomUnitVector()
00103     {
00104         x() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00105         y() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00106         z() = 1.0f - 2.0f*rand()/(float)RAND_MAX;
00107 
00108         Normalize();
00109     }
00110 };
00111 
00112 } //namespace salt
00113 
00114 #endif //SALT_VECTOR_H

Generated on Thu Apr 6 15:25:40 2006 for rcssserver3d by  doxygen 1.4.4