IBSimu 1.0.4
|
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 00055 00066 class Vec4D { 00067 00068 double p[4]; 00069 00070 public: 00071 00072 Vec4D() { p[0] = 0.0; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; } 00073 Vec4D( double x ) { p[0] = x; p[1] = 0.0; p[2] = 0.0; p[3] = 0.0; } 00074 Vec4D( double x, double y ) { p[0] = x; p[1] = y; p[2] = 0.0; p[3] = 0.0; } 00075 Vec4D( double x, double y, double z ) { p[0] = x; p[1] = y; p[2] = z; p[3] = 0.0; } 00076 Vec4D( double x, double y, double z, double w ) { p[0] = x; p[1] = y; p[2] = z; p[3] = w; } 00077 00078 Vec4D( const class Vec3D &vec ); 00079 00080 Vec4D( std::istream &s ) { 00081 p[0] = read_double( s ); 00082 p[1] = read_double( s ); 00083 p[2] = read_double( s ); 00084 p[3] = read_double( s ); 00085 } 00086 ~Vec4D() {} 00087 00088 double &operator[]( int i ) { return( p[i] ); } 00089 const double &operator[]( int i ) const { return( p[i] ); } 00090 double &operator()( int i ) { return( p[i] ); } 00091 const double &operator()( int i ) const { return( p[i] ); } 00092 00098 Vec4D operator+( const Vec4D &vec ) const { 00099 return( Vec4D( p[0] + vec[0], 00100 p[1] + vec[1], 00101 p[2] + vec[2], 00102 (p[2] == vec[2] ? 0.0 : 1.0) ) ); 00103 } 00104 00110 Vec4D operator-( const Vec4D &vec ) const { 00111 return( Vec4D( p[0] - vec[0], 00112 p[1] - vec[1], 00113 p[2] - vec[2], 00114 (p[2] == vec[2] ? 0.0 : 1.0) ) ); 00115 } 00116 00122 Vec4D &operator+=( const Vec4D &vec ) { 00123 p[0] += vec[0]; 00124 p[1] += vec[1]; 00125 p[2] += vec[2]; 00126 return( *this ); 00127 } 00128 00133 double operator*( const Vec4D &vec ) const { 00134 return( p[0] * vec[0] + 00135 p[1] * vec[1] + 00136 p[2] * vec[2] ); 00137 } 00138 00143 Vec4D operator*( double x ) const { 00144 return( Vec4D( x*p[0], x*p[1], x*p[2], p[3] ) ); 00145 } 00146 00151 Vec4D &operator*=( double x ) { 00152 p[0] *= x; 00153 p[1] *= x; 00154 p[2] *= x; 00155 return( *this ); 00156 } 00157 00162 Vec4D &operator/=( double x ) { 00163 double div = 1.0/x; 00164 p[0] *= div; 00165 p[1] *= div; 00166 p[2] *= div; 00167 return( *this ); 00168 } 00169 00174 bool operator!=( const Vec4D &x ) { 00175 if( p[0] != x.p[0] || p[1] != x.p[1] || p[2] != x.p[2] || p[3] != x.p[3] ) 00176 return( true ); 00177 return( false ); 00178 } 00179 00184 bool operator==( const Vec4D &x ) { 00185 if( p[0] == x.p[0] && p[1] == x.p[1] && p[2] == x.p[2] && p[3] == x.p[3] ) 00186 return( true ); 00187 return( false ); 00188 } 00189 00192 Vec4D &operator=( const Vec4D &x ) { 00193 p[0] = x[0]; 00194 p[1] = x[1]; 00195 p[2] = x[2]; 00196 p[3] = x[3]; 00197 return( *this ); 00198 } 00199 00205 void homogenize() { 00206 double inv_w = 1.0/p[3]; 00207 p[0] *= inv_w; 00208 p[1] *= inv_w; 00209 p[2] *= inv_w; 00210 } 00211 00216 void normalize() { 00217 double inv_norm = 1.0/sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00218 p[0] *= inv_norm; 00219 p[1] *= inv_norm; 00220 p[2] *= inv_norm; 00221 p[3] = 0.0; 00222 } 00223 00228 double norm2() const { 00229 return( sqrt( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ) ); 00230 } 00231 00236 double ssqr() const { 00237 return( p[0]*p[0] + p[1]*p[1] + p[2]*p[2] ); 00238 } 00239 00240 void save( std::ostream &s ) const { 00241 write_double( s, p[0] ); 00242 write_double( s, p[1] ); 00243 write_double( s, p[2] ); 00244 write_double( s, p[3] ); 00245 } 00246 00251 friend Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ); 00252 00255 friend double norm2( const Vec4D &vec ); 00256 00261 friend Vec4D operator*( double x, const Vec4D &vec ); 00262 00265 friend std::ostream &operator<<( std::ostream &os, const Vec4D &vec ); 00266 }; 00267 00268 00269 inline double norm2( const Vec4D &vec ) { 00270 return( vec.norm2() ); 00271 } 00272 00273 inline Vec4D cross( const Vec4D &vec1, const Vec4D &vec2 ) { 00274 return( Vec4D( vec1[1] * vec2[2] - vec1[2] * vec2[1], 00275 vec1[2] * vec2[0] - vec1[0] * vec2[2], 00276 vec1[0] * vec2[1] - vec1[1] * vec2[0], 00277 0.0 ) ); 00278 } 00279 00280 00281 inline Vec4D operator*( double x, const Vec4D &vec ) 00282 { 00283 return( Vec4D( x*vec[0], x*vec[1], x*vec[2], vec[3] ) ); 00284 } 00285 00286 00287 inline std::ostream &operator<<( std::ostream &os, const Vec4D &vec ) 00288 { 00289 os << std::setw(12) << to_string(vec[0]).substr(0,12) << " "; 00290 os << std::setw(12) << to_string(vec[1]).substr(0,12) << " "; 00291 os << std::setw(12) << to_string(vec[2]).substr(0,12) << " "; 00292 os << std::setw(12) << to_string(vec[3]).substr(0,12); 00293 return( os ); 00294 } 00295 00296 00297 #endif 00298 00299 00300 00301 00302 00303 00304 00305 00306 00307 00308 00309 00310 00311 00312 00313 00314 00315