VVector.hh

Go to the documentation of this file.
00001 /* -*- Mode: CC; indent-tabs-mode: nil; c-basic-offset: 5 -*- */
00002 
00011 /* 
00012 -----BEGIN PGP SIGNED MESSAGE-----
00013 Hash: SHA1
00014 
00015 Created by Daniel Aarno on Tue Nov 25 2003.
00016 Copyright (c) 2004 by Daniel Aarno - <macbishop@users.sf.net>
00017 M. Sc. in Electrical Engineering - http://www.e.kth.se/~e98_aar
00018 
00019 All Rights Reserved
00020 ATTRIBUTION ASSURANCE LICENSE (adapted from the original BSD license)
00021 Redistribution and use in source and binary forms, with or without
00022 modification, are permitted provided that the conditions below are met.
00023 These conditions require a modest attribution to  (the
00024 "Author"), who hopes that its promotional value may help justify the
00025 thousands of dollars in otherwise billable time invested in writing
00026 this and other freely available, open-source software.
00027 
00028 1. Redistributions of source code, in whole or part and with or without
00029 modification (the "Code"), must prominently display this GPG-signed
00030 text in verifiable form.
00031 2. Redistributions of the Code in binary form must be accompanied by
00032 this GPG-signed text in any documentation and, each time the resulting
00033 executable program or a program dependent thereon is launched, a
00034 prominent display (e.g., splash screen or banner text) of the Author's
00035 attribution information, which includes:
00036 (a) Name ("Daniel Aarno"),
00037 (b) Professional identification ("M. Sc. in Electrical Engineering"), and
00038 (c) URL ("http://www.e.kth.se/~e98_aar").
00039 3. Neither the name nor any trademark of the Author may be used to
00040 endorse or promote products derived from this software without specific
00041 prior written permission.
00042 4. Users are entirely responsible, to the exclusion of the Author and
00043 any other persons, for compliance with (1) regulations set by owners or
00044 administrators of employed equipment, (2) licensing terms of any other
00045 software, and (3) local regulations regarding use, including those
00046 regarding import, export, and use of encryption software.
00047 
00048 THIS FREE SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND
00049 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00050 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00051 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00052 EVENT SHALL THE AUTHOR OR ANY CONTRIBUTOR BE LIABLE FOR
00053 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00054 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00055 EFFECTS OF UNAUTHORIZED OR MALICIOUS NETWORK ACCESS;
00056 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00057 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00058 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00059 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00060 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00061 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00062 -----BEGIN PGP SIGNATURE-----
00063 Version: GnuPG v1.2.4 (GNU/Linux)
00064 
00065 iD8DBQFB+7jgi6ECThHTSIkRAuJFAKCXjyB/7HxUEKqvIXry38bquCySZQCfUNbk
00066 XEaqordUUTUdgKj87lhewM8=
00067 =tkIO
00068 -----END PGP SIGNATURE-----
00069 */
00070 
00071 #ifndef _VVECTOR_
00072 #define _VVECTOR_
00073 
00074 #ifndef VVECTOR_ENABLE_INLINE
00075 #define VVECTOR_SHOULD_UNDEF_INLINE
00076 #endif
00077 #define VVECTOR_ENABLE_INLINE
00078 #include "vvector.h"
00079 #ifdef VVECTOR_SHOULD_UNDEF_INLINE
00080 #undef VVECTOR_ENABLE_INLINE
00081 #undef VVECTOR_SHOULD_UNDEF_INLINE
00082 #endif
00083 
00084 #include <iostream>
00085 
00087 class VVector {
00088 private:
00089      d3 m_vval;
00090 
00091 public:
00092      //======================================================================
00093      //                          Constructors
00094      //======================================================================
00095 
00097      VVector() {}
00098 
00101      explicit VVector(bool zero_) { 
00102           if(zero_)
00103                zero();
00104      }
00105      
00111      explicit VVector(double val) { 
00112           set(val, val, val);
00113      }
00114 
00117      explicit VVector(const f3 fv) { 
00118           m_vval[0] = fv[0]; 
00119           m_vval[1] = fv[1]; 
00120           m_vval[2] = fv[2]; 
00121      }
00122 
00125      explicit VVector(const d3 dv) { 
00126           m_vval[0] = dv[0]; 
00127           m_vval[1] = dv[1]; 
00128           m_vval[2] = dv[2]; 
00129      }
00130 
00135      VVector(double x, double y, double z) {
00136           set(x,y,z);
00137      }
00138 
00139      //======================================================================
00140      //                     Access functions
00141      //======================================================================
00142 
00147      const double operator[](int i) const {
00148           return (i >= 0 && i < 3 ? m_vval[i] : getDummy());
00149      }
00150 
00155      double& operator[](int i) {
00156           return (i >= 0 && i < 3 ? m_vval[i] : getDummy());
00157      }
00158   
00160      double& x() { return this->operator[](0); }
00162      double& y() { return this->operator[](1); }
00164      double& z() { return this->operator[](2); }
00165 
00167      const double x() const { return this->operator[](0); }
00169      const double y() const { return this->operator[](1); }
00171      const double z() const { return this->operator[](2); }
00172 
00173      //======================================================================
00174      //             Arithmetic and conversion operators
00175      //======================================================================
00176 
00178      operator const double*() const { return m_vval; }
00179 
00181      const VVector operator+(const VVector &rhs) const { 
00182           VVector ret;
00183           VPlusd(ret.m_vval, m_vval, rhs.m_vval); 
00184           return ret;
00185      }
00186 
00190      const VVector operator-(const VVector &rhs) const { 
00191           VVector ret;
00192           VMinusd(ret.m_vval, m_vval, rhs.m_vval); 
00193           return ret;
00194      }
00195 
00199      double operator*(const VVector &rhs) const { 
00200           return VDotd(m_vval, rhs.m_vval); 
00201      }
00202 
00204      const VVector operator*(double scalar) const { 
00205           VVector ret;
00206           VScalarMultiplyd(ret.m_vval, scalar, m_vval); 
00207           return ret;
00208      }
00209 
00211      const VVector operator/(double scalar) const { 
00212           VVector ret;
00213           VScalarMultiplyd(ret.m_vval, 1.0/scalar, m_vval); 
00214           return ret;
00215      }
00216 
00218      VVector& operator+=(const VVector &rhs) {
00219           VPlusd(m_vval, m_vval, rhs.m_vval);
00220           return *this;
00221      }
00222 
00224      VVector& operator-=(const VVector &rhs) {
00225           VMinusd(m_vval, m_vval, rhs.m_vval);
00226           return *this;
00227      }
00228 
00230      VVector& operator*=(double scalar) {
00231           VScalarMultiplyd(m_vval, scalar, m_vval);
00232           return *this;
00233      }
00234 
00236      VVector& operator/=(double scalar) {
00237           VScalarMultiplyd(m_vval, 1.0/scalar, m_vval);
00238           return *this;
00239      }
00240 
00241      //======================================================================
00242      //                       Vector arithmetic
00243      //======================================================================
00244 
00246      const VVector crossproduct(const VVector &with) const {
00247           d3 result;
00248           VCrossd(result, m_vval, with.m_vval);
00249           VVector ret(result);
00250           return ret;
00251      }
00252   
00255      const VVector projection(const VVector &onto) const {
00256           d3 result;
00257           VProjd(result, m_vval, onto.m_vval);
00258           VVector ret(result);
00259           return ret;
00260      }
00261   
00264      const VVector perpendicular(const VVector &to) const {
00265           d3 result;
00266           VPerpd(result, m_vval, to.m_vval);
00267           VVector ret(result);
00268           return ret;
00269      }
00270   
00274      const VVector rotX(double angleRad) const {
00275           d3 result;
00276           VRotXd(result, m_vval, angleRad);
00277           VVector ret(result);
00278           return ret;
00279      }
00280   
00284      const VVector rotY(double angleRad) const {
00285           d3 result;
00286           VRotYd(result, m_vval, angleRad);
00287           VVector ret(result);
00288           return ret;
00289      }
00290   
00294      const VVector rotZ(double angleRad) const {
00295           d3 result;
00296           VRotZd(result, m_vval, angleRad);
00297           VVector ret(result);
00298           return ret;
00299      }
00300   
00305      const VVector rotN(const VVector &n, double angleRad) const {
00306           d3 result;
00307           VRotNd(result, m_vval, n, angleRad);
00308           VVector ret(result);
00309           return ret;
00310      }
00311 
00313      const double length() const {
00314           return VLengthd(m_vval);
00315      }
00316   
00318      void normalize() {
00319           VNormalized(m_vval);
00320      }
00321 
00323      void set(double x, double y, double z) {        
00324           m_vval[0] = x; 
00325           m_vval[1] = y; 
00326           m_vval[2] = z;
00327      }
00328 
00329      //Set value of the vector to (0,0,0)
00330      void zero() { set(0,0,0); }
00331   
00332      //======================================================================
00333      //                           Helper functions
00334      //======================================================================
00335 private:
00336      static double& getDummy() 
00337      { 
00338           static double dummy = 0; 
00339 #ifndef NDEBUG
00340           std::cerr << "WARNING VVector.hh:" << __LINE__ 
00341                     << " Dummy value accessed" << std::endl;
00342 #endif
00343           return dummy;
00344      }
00345   
00346      //======================================================================
00347      //                              Friends
00348      //======================================================================
00350      friend std::ostream& operator<<(std::ostream &o, const VVector &v);
00351 }; //< class VVector
00352 
00353 
00354 //======================================================================
00355 //                          Gloabl functions
00356 //======================================================================
00357 
00358 //Inline helps prevent multiple definitions and speeds things up
00360 inline std::ostream& operator<<(std::ostream &o, const VVector &v)
00361 {
00362      o << '[' << v.m_vval[0] << ", " << v.m_vval[1] 
00363        << ", " << v.m_vval[2] << ']';
00364      return o;
00365 }
00366 
00368 inline const VVector operator*(double scalar, const VVector &rhs) { 
00369      return rhs * scalar;
00370 }
00371 
00372 #endif

Generated on Sun Mar 11 15:33:24 2007 for MetalWarriors by  doxygen 1.4.7