Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Vector3.h

Go to the documentation of this file.
00001 /* SimData: Data Infrastructure for Simulations
00002  * Copyright (C) 2002, 2003 Mark Rose <tm2@stm.lbl.gov>
00003  * 
00004  * This file is part of SimData.
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  */
00020 
00021 
00036 #ifndef __SIMDATA_VECTOR3_H__
00037 #define __SIMDATA_VECTOR3_H__
00038 
00039 #include <SimData/BaseType.h>
00040 #include <SimData/Math.h>
00041 
00042 #include <cmath>
00043 #include <vector>
00044 #include <iostream>
00045 
00046 
00047 NAMESPACE_SIMDATA
00048 
00049 
00050 class Matrix3;
00051 
00056 class SIMDATA_EXPORT Vector3: public BaseType
00057 {
00058 protected:
00060         double _x;
00062         double _y;
00064         double _z;
00065 
00066 public:
00067 
00069         static const Vector3 ZERO;
00071         static const Vector3 XAXIS;
00073         static const Vector3 YAXIS;
00075         static const Vector3 ZAXIS;
00076 
00078         Vector3(): _x(0.0), _y(0.0), _z(0.0) {}
00080         Vector3(double x_, double y_, double z_): _x(x_), _y(y_), _z(z_) {}
00082         Vector3(const Vector3& v): BaseType(v), _x(v._x), _y(v._y), _z(v._z) {}
00083 
00084 #ifndef SWIG
00085 
00086         inline const Vector3& operator = (const Vector3& v) { 
00087                 _x=v._x; _y=v._y; _z=v._z; 
00088                 return *this;
00089         }
00090 #endif // SWIG
00091 
00093         inline bool operator == (const Vector3& v) const { return _x==v._x && _y==v._y && _z==v._z; }
00094         
00096         inline bool operator != (const Vector3& v) const { return _x!=v._x || _y!=v._y || _z!=v._z; }
00097 
00099         inline void set(double x_, double y_, double z_) { _x=x_; _y=y_; _z=z_; }
00100 
00101 #ifndef SWIG
00102 
00109         inline double& operator [] (int i) { 
00110                 switch (i) {
00111                         case 0: return _x;
00112                         case 1: return _y;
00113                         case 2: return _z;
00114                         default:
00115                                 throw ""; // FIXME
00116                 }
00117         }
00118 
00126         inline double operator [] (int i) const { 
00127                 switch (i) {
00128                         case 0: return _x;
00129                         case 1: return _y;
00130                         case 2: return _z;
00131                         default:
00132                                 throw ""; // FIXME
00133                 }
00134         }
00135 #endif // SWIG
00136 
00137 
00138 #ifndef SWIG
00139 
00140         inline double& x() { return _x; }
00142         inline double& y() { return _y; }
00144         inline double& z() { return _z; }
00145 #endif // SWIG
00146 
00148         inline double x() const { return _x; }
00150         inline double y() const { return _y; }
00152         inline double z() const { return _z; }
00153 
00155         inline bool valid() const { return !isNaN(); }
00156         
00158         inline bool isNaN() const { return simdata::isNaN(_x) || simdata::isNaN(_y) || simdata::isNaN(_z); }
00159         inline bool isZero() const { return *this == ZERO; }
00160 
00162         inline double operator * (const Vector3& rhs) const {
00163                 return _x*rhs._x+_y*rhs._y+_z*rhs._z;
00164         }
00165 
00167         inline const Vector3 operator ^ (const Vector3& rhs) const {
00168                 return Vector3(_y*rhs._z-_z*rhs._y, _z*rhs._x-_x*rhs._z, _x*rhs._y-_y*rhs._x);
00169         }
00170 
00172         inline const Vector3 operator * (double rhs) const {
00173                 return Vector3(_x*rhs, _y*rhs, _z*rhs);
00174         }
00175 
00176 #ifndef SWIG
00177 
00178         inline Vector3& operator *= (double rhs) {
00179                 _x*=rhs;
00180                 _y*=rhs;
00181                 _z*=rhs;
00182                 return *this;
00183         }
00184 #endif // SWIG
00185 
00187         inline const Vector3 operator / (double rhs) const {
00188                 return (*this)*(1.0/rhs);
00189         }
00190 
00191 #ifndef SWIG
00192 
00193         inline Vector3& operator /= (double rhs) {
00194                 return *this *= (1.0/rhs);
00195         }
00196 #endif // SWIG
00197 
00199         inline const Vector3 operator + (const Vector3& rhs) const {
00200                 return Vector3(_x+rhs._x, _y+rhs._y, _z+rhs._z);
00201         }
00202 
00203 #ifndef SWIG
00204 
00205         inline Vector3& operator += (const Vector3& rhs) {
00206                 _x += rhs._x;
00207                 _y += rhs._y;
00208                 _z += rhs._z;
00209                 return *this;
00210         }
00211 #endif // SWIG
00212 
00214         inline const Vector3 operator - (const Vector3& rhs) const {
00215                 return Vector3(_x-rhs._x, _y-rhs._y, _z-rhs._z);
00216         }
00217 
00218 #ifndef SWIG
00219 
00220         inline Vector3& operator -= (const Vector3& rhs) {
00221                 _x-=rhs._x;
00222                 _y-=rhs._y;
00223                 _z-=rhs._z;
00224                 return *this;
00225         }
00226 #endif // SWIG
00227 
00229         inline const Vector3 operator - () const {
00230                 return Vector3(-_x, -_y, -_z);
00231         }
00232 
00234         inline double length() const {
00235                 return ::sqrt( _x*_x + _y*_y + _z*_z );
00236         }
00237 
00239         inline double length2() const {
00240                 return _x*_x + _y*_y + _z*_z;
00241         }
00242 
00247         inline double normalize() {
00248                 double norm = length();
00249                 if (norm > 0.0) {
00250                         _x /= norm;
00251                         _y /= norm;
00252                         _z /= norm;
00253                 }                
00254                 return norm;
00255         }
00256 
00258         inline const Vector3 normalized() const {
00259                 double norm = length();
00260                 if (norm > 0.0) {
00261                         return (*this / norm);
00262                 }                
00263                 return *this;
00264         }
00265 
00267         Matrix3 starMatrix() const;
00268 
00274         inline void bound(Vector3& min, Vector3& max) {
00275                 if (_x < min._x) min._x = _x;
00276                 if (_y < min._y) min._y = _y;
00277                 if (_z < min._z) min._z = _z;
00278                 if (_x > max._x) max._x = _x;
00279                 if (_y > max._y) max._y = _y;
00280                 if (_z > max._z) max._z = _z;
00281         }
00282                 
00283 #ifndef SWIG
00284 
00285         friend double dot(const Vector3& a, const Vector3& b); // inline
00286 
00288         friend const Vector3 cross(const Vector3& a, const Vector3& b); // inline
00289 
00291         friend SIMDATA_EXPORT std::ostream& operator << (std::ostream& output, const Vector3& vec);
00292 
00294         friend Vector3 operator * (double lhs, const Vector3 &rhs); // inline
00295 #endif // SWIG
00296 
00298         std::vector<double> getElements() const {
00299                 std::vector<double> v(3);
00300                 v[0] = _x;
00301                 v[1] = _y;
00302                 v[2] = _z;
00303                 return v;
00304         }
00305 
00307         void setElements(std::vector<double> const &v) {
00308                 if (v.size() == 3) {
00309                         _x = v[0];
00310                         _y = v[1];
00311                         _z = v[2];
00312                 } else {
00313                         // FIXME (throw)
00314                 }
00315         }
00316 
00318         virtual std::string asString() const;
00319 
00321         virtual std::string typeString() const { return "Vector3"; }
00322 
00324         virtual void serialize(Archive&);
00325 
00330         virtual void parseXML(const char*);
00331 
00332 #ifdef SWIG
00333         // setup accessors for x, y, and z (ugly hack)
00334 %extend {
00335         void set_x(double x) { self->x()=x; }
00336         void set_y(double y) { self->y()=y; }
00337         void set_z(double z) { self->z()=z; }
00338         double get_x() { return self->x(); }
00339         double get_y() { return self->y(); }
00340         double get_z() { return self->z(); }
00341 }
00342 %insert("shadow") %{
00343         if _newclass:
00344                 x = property(_cSimData.Vector3_get_x, _cSimData.Vector3_set_x)
00345                 y = property(_cSimData.Vector3_get_y, _cSimData.Vector3_set_y)
00346                 z = property(_cSimData.Vector3_get_z, _cSimData.Vector3_set_z)
00347         __swig_setmethods__["x"] = _cSimData.Vector3_set_x
00348         __swig_getmethods__["x"] = _cSimData.Vector3_get_x
00349         __swig_setmethods__["y"] = _cSimData.Vector3_set_y
00350         __swig_getmethods__["y"] = _cSimData.Vector3_get_y
00351         __swig_setmethods__["z"] = _cSimData.Vector3_set_z
00352         __swig_getmethods__["z"] = _cSimData.Vector3_get_z
00353 %}
00354 #endif // SWIG
00355 
00356 };      // end of class Vector3
00357 
00358 inline double dot(const Vector3& a, const Vector3& b) { return a*b; }
00359 
00360 inline const Vector3 cross(const Vector3& a, const Vector3& b) { return a^b; }
00361 
00362 inline Vector3 operator * (double lhs, const Vector3 &rhs) { return rhs*lhs; }
00363 
00364 NAMESPACE_SIMDATA_END // simdata
00365 
00366 
00367 #endif // __SIMDATA_VECTOR3_H__
00368 

SimData version pre-0.4.0. For more information on SimData, visit the SimData Homepage.

Generated on Tue Oct 14 12:06:39 2003, using Doxygen 1.2.18.

[SF.net]