Vec3r.h

Go to the documentation of this file.
00001 /*************************************************************************
00002 *                                                                       *
00003 * Open Physics Abstraction Layer                                        *
00004 * Copyright (C) 2004-2005                                               *
00005 * Alan Fischer  alan.fischer@gmail.com                                  *
00006 * Andres Reinot  andres@reinot.com                                      *
00007 * Tyler Streeter  tylerstreeter@gmail.com                               *
00008 * All rights reserved.                                                  *
00009 * Web: opal.sourceforge.net                                             *
00010 *                                                                       *
00011 * This library is free software; you can redistribute it and/or         *
00012 * modify it under the terms of EITHER:                                  *
00013 *   (1) The GNU Lesser General Public License as published by the Free  *
00014 *       Software Foundation; either version 2.1 of the License, or (at  *
00015 *       your option) any later version. The text of the GNU Lesser      *
00016 *       General Public License is included with this library in the     *
00017 *       file license-LGPL.txt.                                          *
00018 *   (2) The BSD-style license that is included with this library in     *
00019 *       the file license-BSD.txt.                                       *
00020 *                                                                       *
00021 * This library is distributed in the hope that it will be useful,       *
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00024 * license-LGPL.txt and license-BSD.txt for more details.                *
00025 *                                                                       *
00026 *************************************************************************/
00027 
00028 #ifndef OPAL_VEC3R_H
00029 #define OPAL_VEC3R_H
00030 
00031 // project headers
00032 #include "OpalMath.h"
00033 #include "Portability.h"
00034 
00035 // system headers
00036 #include <iostream>
00037 
00038 namespace opal
00039 {
00040     class Vec3r;
00041     inline Vec3r operator+( const Vec3r &u, const Vec3r &v );
00042     inline Vec3r operator-( const Vec3r &u, const Vec3r &v );
00043     inline Vec3r operator*( const Vec3r &v, real scalar );
00044     inline Vec3r operator*( real scalar, const Vec3r &v );
00045     inline Vec3r operator/( const Vec3r &v, real scalar );
00046     inline Vec3r operator%( const Vec3r & a, const Vec3r & b );
00047     inline Vec3r operator-( const Vec3r &v );
00048     inline real dot( const Vec3r & u, const Vec3r & v );
00049     inline Vec3r cross( const Vec3r & u, const Vec3r& v );
00050 
00052     inline Vec3r project( const Vec3r& u, const Vec3r& v );
00053 
00056     inline Vec3r projectPreNorm( const Vec3r& u, const Vec3r& v );
00057 
00059     inline real angleBetween( const Vec3r& u, const Vec3r& v );
00060 
00063     inline real angleBetweenPreNorm( const Vec3r& u, const Vec3r& v );
00064 
00066     inline bool areCollinear( const Vec3r& u, const Vec3r& v );
00067 
00069     inline std::ostream& operator<<( std::ostream& o, const Vec3r& v );
00070 
00071     class Vec3r
00072     {
00073         public:
00075             real x;
00076 
00078             real y;
00079 
00081             real z;
00082 
00084             OPAL_DECL Vec3r();
00085 
00086             OPAL_DECL Vec3r( const Vec3r & src );
00087 
00088             OPAL_DECL Vec3r( real xx, real yy, real zz );
00089 
00090             OPAL_DECL Vec3r( const real * data );
00091 
00092             OPAL_DECL void OPAL_CALL set( real xx, real yy, real zz );
00093 
00094             OPAL_DECL void OPAL_CALL set( real * data );
00095 
00096             OPAL_DECL real & OPAL_CALL operator[] ( unsigned int i );
00097 
00098             OPAL_DECL const real & OPAL_CALL operator[] ( unsigned int i ) const;
00099 
00100             OPAL_DECL real OPAL_CALL lengthSquared() const;
00101 
00102             OPAL_DECL real OPAL_CALL length() const;
00103 
00104             OPAL_DECL void OPAL_CALL normalize();
00105 
00106             OPAL_DECL Vec3r OPAL_CALL unit() const;
00107 
00108             OPAL_DECL void OPAL_CALL operator+=( const Vec3r & v );
00109 
00110             OPAL_DECL void OPAL_CALL operator-=( const Vec3r & v );
00111 
00112             OPAL_DECL void OPAL_CALL operator*=( const Vec3r & v );
00113 
00114             OPAL_DECL void OPAL_CALL operator*=( real scalar );
00115 
00116             OPAL_DECL void OPAL_CALL operator/=( real scalar );
00117 
00118             OPAL_DECL void OPAL_CALL operator/=( const Vec3r & v );
00119 
00120             OPAL_DECL bool OPAL_CALL operator==( const Vec3r & v ) const;
00121 
00122             OPAL_DECL bool OPAL_CALL operator!=( const Vec3r & v ) const;
00123 
00124             OPAL_DECL void OPAL_CALL operator=( const Vec3r & v );
00125     };
00126 
00127     inline Vec3r operator+( const Vec3r &u, const Vec3r &v )
00128     {
00129         return Vec3r( u.x + v.x, u.y + v.y, u.z + v.z );
00130     }
00131 
00132     inline Vec3r operator-( const Vec3r &u, const Vec3r &v )
00133     {
00134         return Vec3r( u.x - v.x, u.y - v.y, u.z - v.z );
00135     }
00136 
00137     inline Vec3r operator*( const Vec3r &v, real scalar )
00138     {
00139         return Vec3r( scalar * v.x, scalar * v.y, scalar * v.z );
00140     }
00141 
00142     inline Vec3r operator*( real scalar, const Vec3r &v )
00143     {
00144         return Vec3r( scalar * v.x, scalar * v.y, scalar * v.z );
00145     }
00146 
00147     inline Vec3r operator/( const Vec3r &v, real scalar )
00148     {
00149         return Vec3r( v.x / scalar, v.y / scalar, v.z / scalar );
00150     }
00151 
00152     inline Vec3r operator%( const Vec3r & a, const Vec3r & b )
00153     {
00154         return Vec3r( ( a.y * b.z ) - ( a.z * b.y ), ( a.z * b.x ) - ( a.x * b.z ),
00155                       ( a.x * b.y ) - ( a.y * b.x ) );
00156     }
00157 
00158     inline real dot( const Vec3r & u, const Vec3r & v )
00159     {
00160         return u.x * v.x + u.y * v.y + u.z * v.z;
00161     }
00162 
00163     inline Vec3r cross( const Vec3r & u, const Vec3r & v )
00164     {
00165         return u % v;
00166     }
00167 
00168     inline Vec3r operator-( const Vec3r &v )
00169     {
00170         return v * -1;
00171     }
00172 
00173     inline Vec3r project( const Vec3r& u, const Vec3r& v )
00174     {
00175         Vec3r u2 = u;
00176         u2.normalize();
00177         return projectPreNorm( u2, v );
00178     }
00179 
00180     inline Vec3r projectPreNorm( const Vec3r& u, const Vec3r& v )
00181     {
00182         return dot( u, v ) * u;
00183     }
00184 
00185     inline real angleBetween( const Vec3r& u, const Vec3r& v )
00186     {
00187         Vec3r u2 = u;
00188         u2.normalize();
00189         Vec3r v2 = v;
00190         v2.normalize();
00191         return angleBetweenPreNorm( u2, v2 );
00192     }
00193 
00194     inline real angleBetweenPreNorm( const Vec3r& u, const Vec3r& v )
00195     {
00196         real val = dot( u, v );
00197 
00198         // Clamp to avoid rounding errors; acos will puke with values outside
00199         // this range.
00200         if ( val < -1 )
00201         {
00202             val = -1;
00203         }
00204         else if ( val > 1 )
00205         {
00206             val = 1;
00207         }
00208 
00209         return radToDeg( acos( val ) );
00210     }
00211 
00212     inline std::ostream& operator<<( std::ostream& o, const Vec3r& v )
00213     {
00214         return o << "[" << v.x << " " << v.y << " " << v.z << "]";
00215     }   
00216 
00217     bool areCollinear( const Vec3r & u, const Vec3r & v )
00218     {
00219         Vec3r a = u, b = v;
00220         a.normalize();
00221         b.normalize();
00222 
00223         if ( areEqual( a.x, b.x ) && areEqual( a.y, b.y ) && areEqual( a.z, b.z ) )
00224             return true;
00225         if ( areEqual( a.x, -b.x ) && areEqual( a.y, -b.y ) && areEqual( a.z, -b.z ) )
00226             return true;
00227 
00228         return false;
00229     }
00230 }
00231 
00232 #endif

Generated on Tue May 16 17:49:53 2006 for OPAL by  doxygen 1.4.6-NO