Vec3r.cpp

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 * Oleksandr Lozitskiy mr.olexander@gmail.com                            *
00009 * All rights reserved.                                                  *
00010 * Web: opal.sourceforge.net                                             *
00011 *                                                                       *
00012 * This library is free software; you can redistribute it and/or         *
00013 * modify it under the terms of EITHER:                                  *
00014 *   (1) The GNU Lesser General Public License as published by the Free  *
00015 *       Software Foundation; either version 2.1 of the License, or (at  *
00016 *       your option) any later version. The text of the GNU Lesser      *
00017 *       General Public License is included with this library in the     *
00018 *       file license-LGPL.txt.                                          *
00019 *   (2) The BSD-style license that is included with this library in     *
00020 *       the file license-BSD.txt.                                       *
00021 *                                                                       *
00022 * This library is distributed in the hope that it will be useful,       *
00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00025 * license-LGPL.txt and license-BSD.txt for more details.                *
00026 *                                                                       *
00027 *************************************************************************/
00028 
00029 // class header
00030 #include "Vec3r.h" 
00031 //#pragma implementation
00032 
00033 // system headers
00034 #include <cassert>
00035 
00036 namespace opal
00037 {
00038     Vec3r::Vec3r()
00039     {
00040         x = 0;
00041         y = 0;
00042         z = 0;
00043     }
00044 
00045     Vec3r::Vec3r( const Vec3r & src )
00046     {
00047         x = src.x;
00048         y = src.y;
00049         z = src.z;
00050     }
00051 
00052     Vec3r::Vec3r( real xx, real yy, real zz )
00053     {
00054         x = xx;
00055         y = yy;
00056         z = zz;
00057     }
00058 
00059     Vec3r::Vec3r( const real * data )
00060     {
00061         x = data[ 0 ];
00062         y = data[ 1 ];
00063         z = data[ 2 ];
00064     }
00065 
00066     void Vec3r::set( real xx, real yy, real zz )
00067     {
00068         x = xx;
00069         y = yy;
00070         z = zz;
00071     }
00072 
00073     void Vec3r::set( real * data )
00074     {
00075         x = data[ 0 ];
00076         y = data[ 1 ];
00077         z = data[ 2 ];
00078     }
00079 
00080     real & Vec3r::operator[] ( unsigned int i )
00081     {
00082         switch ( i )
00083         {
00084             case 0:
00085             return x;
00086             case 1:
00087             return y;
00088             case 2:
00089             return z;
00090             default:
00091             assert( i < 3 );
00092             return z;
00093         }
00094     }
00095 
00096     const real & Vec3r::operator[] ( unsigned int i ) const
00097     {
00098         switch ( i )
00099         {
00100             case 0:
00101             return x;
00102             case 1:
00103             return y;
00104             case 2:
00105             return z;
00106             default:
00107             assert( i < 3 );
00108             return z;
00109         }
00110     }
00111 
00112     real Vec3r::lengthSquared() const
00113     {
00114         return dot( *this, *this );
00115     }
00116 
00117     real Vec3r::length() const
00118     {
00119         return sqrt( dot( *this, *this ) );
00120     }
00121 
00122     void Vec3r::normalize()
00123     {
00124         real len = length();
00125         assert( 0 != len );
00126         real factor = ( real ) 1.0 / len;
00127         ( *this ) *= factor;
00128     }
00129 
00130     Vec3r Vec3r::unit() const
00131     {
00132         return ( *this ) / length();
00133     }
00134 
00135     void Vec3r::operator+=( const Vec3r & v )
00136     {
00137         x += v.x;
00138         y += v.y;
00139         z += v.z;
00140     }
00141 
00142     void Vec3r::operator-=( const Vec3r & v )
00143     {
00144         x -= v.x;
00145         y -= v.y;
00146         z -= v.z;
00147     }
00148 
00149     void Vec3r::operator*=( const Vec3r & v )
00150     {
00151         x *= v.x;
00152         y *= v.y;
00153         z *= v.z;
00154     }
00155 
00156     void Vec3r::operator*=( real scalar )
00157     {
00158         x *= scalar;
00159         y *= scalar;
00160         z *= scalar;
00161     }
00162 
00163     void Vec3r::operator/=( real scalar )
00164     {
00165         x /= scalar;
00166         y /= scalar;
00167         z /= scalar;
00168     }
00169 
00170     void Vec3r::operator/=( const Vec3r & v )
00171     {
00172         x /= v.x;
00173         y /= v.y;
00174         z /= v.z;
00175     }
00176 
00177     bool Vec3r::operator==( const Vec3r & v ) const
00178     {
00179         return ( areEqual( x, v.x ) && areEqual( y , v.y ) && areEqual( z , v.z ) );
00180     }
00181 
00182     void Vec3r::operator=( const Vec3r & v )
00183     {
00184         x = v.x;
00185         y = v.y;
00186         z = v.z;
00187     }
00188 
00189     bool Vec3r::operator!=( const Vec3r & v ) const
00190     {
00191         return ( !areEqual( x, v.x ) || !areEqual( y , v.y ) || !areEqual( z , v.z ) );
00192     }
00193 }

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