vec4d.hpp
Go to the documentation of this file.
00001 00005 /* Copyright (c) 2005-2010 Taneli Kalvas. All rights reserved. 00006 * 00007 * You can redistribute this software and/or modify it under the terms 00008 * of the GNU General Public License as published by the Free Software 00009 * Foundation; either version 2 of the License, or (at your option) 00010 * any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this library (file "COPYING" included in the package); 00019 * if not, write to the Free Software Foundation, Inc., 51 Franklin 00020 * Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 * 00022 * If you have questions about your rights to use or distribute this 00023 * software, please contact Berkeley Lab's Technology Transfer 00024 * Department at TTD@lbl.gov. Other questions, comments and bug 00025 * reports should be sent directly to the author via email at 00026 * taneli.kalvas@jyu.fi. 00027 * 00028 * NOTICE. This software was developed under partial funding from the 00029 * U.S. Department of Energy. As such, the U.S. Government has been 00030 * granted for itself and others acting on its behalf a paid-up, 00031 * nonexclusive, irrevocable, worldwide license in the Software to 00032 * reproduce, prepare derivative works, and perform publicly and 00033 * display publicly. Beginning five (5) years after the date 00034 * permission to assert copyright is obtained from the U.S. Department 00035 * of Energy, and subject to any subsequent five (5) year renewals, 00036 * the U.S. Government is granted for itself and others acting on its 00037 * behalf a paid-up, nonexclusive, irrevocable, worldwide license in 00038 * the Software to reproduce, prepare derivative works, distribute 00039 * copies to the public, perform publicly and display publicly, and to 00040 * permit others to do so. 00041 */ 00042 00043 #ifndef VEC4D_HPP 00044 #define VEC4D_HPP 1 00045 00046 00047 #include <math.h> 00048 #include <stdint.h> 00049 #include <iostream> 00050 #include <iostream> 00051 #include <iomanip> 00052 #include "vec3d.hpp" 00053 #include "file.hpp" 00054 #include "error.hpp" 00055 00056 00067 class Vec4D { 00068 00069 double p[4]; 00070 00071 public: 00072 00073 Vec4D() { p[0] = 0.0; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; } 00074 Vec4D( double x ) { p[0] = x; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; } 00075 Vec4D( double x, double y ) { p[0] = x; p[1] = y; p[2] = 0.0; p[3] = 0.0; } 00076 Vec4D( double x, double y, double z ) { p[0] = x; p[1] = y; p[2] = z; p[3] = 0.0; } 00077 Vec4D( double x, double y, double z, double w ) { p[0] = x; p[1] = y; p[2] = z; p[3] = w; } 00078 00079 Vec4D( const class Vec3D &vec ); 00080 00081 Vec4D( std::istream &s ) { 00082 p[0] = read_double( s ); 00083 p[1] = read_double( s ); 00084 p[2] = read_double( s ); 00085 p[3] = read_double( s ); 00086 } 00087 ~Vec4D() {} 00088 00089 double &operator[]( int i ) { return( p[i] ); } 00090 const double &operator[]( int i ) const { return( p[i] ); } 00091 double &operator()( int i ) { return( p[i] ); } 00092 const double &operator()( int i ) const { return( p[i] ); } 00093 00099 Vec4D operator+( const Vec4D &vec ) const { 00100 return( Vec4D( p[0] + vec[0], 00101 p[1] + vec[1], 00102 p[2] + vec[2], 00103 (p[2] == vec[2] ? 0.0 : 1.0) ) ); 00104 } 00105 00111 Vec4D operator-( const Vec4D &vec ) const { 00112 return( Vec4D( p[0] - vec[0], 00113 p[1] - vec[1], 00114 p[2] - vec[2], 00115 (p[2] == vec[2] ? 0.0 : 1.0) ) ); 00116 } 00117 00123 Vec4D &operator+=( const Vec4D &vec ) { 00124 p[0] += vec[0]; 00125 p[1] += vec[1]; 00126 p[2] += vec[2]; 00127 return( *this ); 00128 } 00129 00134 double operator*( const Vec4D &vec ) const { 00135 return( p[0] * vec[0] + 00136 p[1] * vec[1] + 00137 p[2] * vec[2] ); 00138 } 00139 00144 Vec4D operator*( double x ) const { 00145 return( Vec4D( x*p[0], x*p[1], x*p[2], p[3] ) ); 00146 } 00147 00152 Vec4D &operator*=( double x ) { 00153 p[0] *= x; 00154 p[1] *= x; 00155 p[2] *= x; 00156 return( *this ); 00157 } 00158 00163 Vec4D &operator/=( double x ) { 00164 double div = 1.0/x; 00165 p[0] *= div; 00166 p[1] *= div; 00167 p[2] *= div; 00168 return( *this ); 00169 } 00170 00175 bool operator!=( const Vec4D &x ) { 00176 if( p[0] != x.p[0] || p[1] != x.p[1] || p[2] != x.p[2] || p[3] != x.p[3] ) 00177 return( true ); 00178 return( false ); 00179 } 00180 00185 bool operator==( const Vec4D &x ) { 00186 if( p[0] == x.p[0] && p[1] == x.p[1] && p[2] == x.p[2] && p[3] == x.p[3] ) 00187 return( true ); 00188 return( false ); 00189 } 00190 00193 Vec4D &operator=( const Vec4D &x ) { 00194 p[0] = x[0]; 00195 p[1] = x[1]; 00196 p[2] = x[2]; 00197 p[3] = x[3]; 00198 return( *this ); 00199 } 00200 00206 void homogenize() { 00207 double inv_w = 1.0/p[3]; 00208 p[0] *= inv_w; 00209 p[1] *= inv_w; 00210 p[2] *= inv_w; 00211 p[3] = 1.0; 00212 } 00213 00218 void normalize() { 00219 double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00220 p[0] *= inv_norm; 00221 p[1] *= inv_norm; 00222 p[2] *= inv_norm; 00223 p[3] = 0.0; 00224 } 00225 00230 double norm2() const { 00231 return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) ); 00232 } 00233 00238 double ssqr() const { 00239 return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00240 } 00241 00242 void save( std::ostream &s ) const { 00243 write_double( s, p[0] ); 00244 write_double( s, p[1] ); 00245 write_double( s, p[2] ); 00246 write_double( s, p[3] ); 00247 } 00248 00253 friend Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ); 00254 00257 friend double norm2( const Vec4D &vec ); 00258 00263 friend Vec4D operator*( double x, const Vec4D &vec ); 00264 00267 friend std::ostream &operator<<( std::ostream &os, const Vec4D &vec ); 00268 }; 00269 00270 00271 inline double norm2( const Vec4D &vec ) { 00272 return( vec.norm2() ); 00273 } 00274 00275 inline Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ) { 00276 return( Vec4D( vec1[1] * vec2[2] - vec1[2] * vec2[1], 00277 vec1[2] * vec2[0] - vec1[0] * vec2[2], 00278 vec1[0] * vec2[1] - vec1[1] * vec2[0], 00279 0.0 ) ); 00280 } 00281 00282 00283 inline Vec4D operator*( double x, const Vec4D &vec ) 00284 { 00285 return( Vec4D( x*vec[0], x*vec[1], x*vec[2], vec[3] ) ); 00286 } 00287 00288 00289 inline std::ostream &operator<<( std::ostream &os, const Vec4D &vec ) 00290 { 00291 os << std::setw(12) << to_string(vec[0]).substr(0,12) << " "; 00292 os << std::setw(12) << to_string(vec[1]).substr(0,12) << " "; 00293 os << std::setw(12) << to_string(vec[2]).substr(0,12) << " "; 00294 os << std::setw(12) << to_string(vec[3]).substr(0,12); 00295 return( os ); 00296 } 00297 00298 00299 #endif 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315 00316 00317