Point3r.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_POINT3R_H
00029 #define OPAL_POINT3R_H
00030 
00031 // project headers
00032 #include "OpalMath.h"
00033 #include "Vec3r.h"
00034 
00035 // systeam headers
00036 #include <cassert>
00037 
00038 namespace opal
00039 {
00040     class Point3r;
00041     inline Point3r operator+( const Point3r &u, const Vec3r &v );
00042     inline Point3r operator-( const Point3r &u, const Vec3r &v );
00043     inline Vec3r operator-( const Point3r &u, const Point3r &v );
00044     inline Point3r operator*( const Point3r &v, real scalar );
00045     inline Point3r operator*( real scalar, const Point3r &v );
00046     inline Point3r operator/( const Point3r &v, real scalar );
00047     inline Point3r operator-( const Point3r &p );
00048 
00050     inline real distance( const Point3r& p1, const Point3r& p2 );
00051 
00053     inline std::ostream& operator<<( std::ostream& o, const Point3r& p );
00054 
00055     class Point3r
00056     {
00057         public:
00059             real x;
00060 
00062             real y;
00063 
00065             real z;
00066 
00067             Point3r()
00068             {
00069                 x = 0;
00070                 y = 0;
00071                 z = 0;
00072             }
00073 
00074             Point3r( const Point3r & src )
00075             {
00076                 x = src.x;
00077                 y = src.y;
00078                 z = src.z;
00079             }
00080 
00081             Point3r( real xx, real yy, real zz )
00082             {
00083                 x = xx;
00084                 y = yy;
00085                 z = zz;
00086             }
00087 
00088             Point3r( const real * data )
00089             {
00090                 x = data[ 0 ];
00091                 y = data[ 1 ];
00092                 z = data[ 2 ];
00093             }
00094 
00095             inline void set( real xx, real yy, real zz )
00096             {
00097                 x = xx;
00098                 y = yy;
00099                 z = zz;
00100             }
00101 
00102             inline void set( real * data )
00103             {
00104                 x = data[ 0 ];
00105                 y = data[ 1 ];
00106                 z = data[ 2 ];
00107             }
00108 
00109             inline real & operator[] ( unsigned int i )
00110             {
00111                 switch ( i )
00112                 {
00113                     case 0:
00114                         return x;
00115                     case 1:
00116                         return y;
00117                     case 2:
00118                         return z;
00119                     default:
00120                         assert( i < 3 );
00121                         return z;
00122                 }
00123             }
00124 
00125             inline const real & operator[] ( unsigned int i ) const
00126             {
00127                 switch ( i )
00128                 {
00129                     case 0:
00130                         return x;
00131                     case 1:
00132                         return y;
00133                     case 2:
00134                         return z;
00135                     default:
00136                         assert( i < 3 );
00137                         return z;
00138                 }
00139             }
00140 
00141             inline void operator+=( const Vec3r & v )
00142             {
00143                 x += v.x;
00144                 y += v.y;
00145                 z += v.z;
00146             }
00147 
00148             inline void operator-=( const Vec3r & v )
00149             {
00150                 x -= v.x;
00151                 y -= v.y;
00152                 z -= v.z;
00153             }
00154 
00155             inline void operator*=( real scalar )
00156             {
00157                 x *= scalar;
00158                 y *= scalar;
00159                 z *= scalar;
00160             }
00161 
00162             inline void operator/=( real scalar )
00163             {
00164                 x /= scalar;
00165                 y /= scalar;
00166                 z /= scalar;
00167             }
00168 
00169             inline bool operator==( const Point3r & v ) const
00170             {
00171                 return ( x == v.x && y == v.y && z == v.z );
00172             }
00173 
00174             inline bool operator!=( const Point3r & v ) const
00175             {
00176                 return ( x != v.x || y != v.y || z != v.z );
00177             }
00178     };
00179 
00180     inline Point3r operator+( const Point3r &u, const Vec3r &v )
00181     {
00182         return Point3r( u.x + v.x, u.y + v.y, u.z + v.z );
00183     }
00184 
00185     inline Point3r operator-( const Point3r &u, const Vec3r &v )
00186     {
00187         return Point3r( u.x - v.x, u.y - v.y, u.z - v.z );
00188     }
00189 
00190     inline Vec3r operator-( const Point3r &u, const Point3r &v )
00191     {
00192         return Vec3r( u.x - v.x, u.y - v.y, u.z - v.z );
00193     }
00194 
00195     inline Point3r operator*( const Point3r &v, real scalar )
00196     {
00197         return Point3r( scalar * v.x, scalar * v.y, scalar * v.z );
00198     }
00199 
00200     inline Point3r operator*( real scalar, const Point3r &v )
00201     {
00202         return Point3r( scalar * v.x, scalar * v.y, scalar * v.z );
00203     }
00204 
00205     inline Point3r operator/( const Point3r &v, real scalar )
00206     {
00207         return Point3r( v.x / scalar, v.y / scalar, v.z / scalar );
00208     }
00209 
00210     inline real distance( const Point3r& p1, const Point3r& p2 )
00211     {
00212         return sqrt( pow( ( p2.x - p1.x ), 2 ) + pow( ( p2.y - p1.y ), 2 ) +
00213                      pow( ( p2.z - p1.z ), 2 ) );
00214     }
00215 
00216     inline Point3r operator-( const Point3r &p )
00217     {
00218         return p * -1;
00219     }
00220 
00221     inline std::ostream& operator<<( std::ostream& o, const Point3r& p )
00222     {
00223         return o << "[" << p.x << " " << p.y << " " << p.z << "]";
00224     }
00225 }
00226 
00227 #endif
00228 

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