00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef OPAL_POINT3R_H
00029 #define OPAL_POINT3R_H
00030
00031
00032 #include "OpalMath.h"
00033 #include "Vec3r.h"
00034
00035
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