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 #include "CeylanVector3.h"
00028
00029 #include "CeylanMatrix3.h"
00030 #include "CeylanTripoint.h"
00031
00032 #include "CeylanLogPlug.h"
00033 #include "CeylanOperators.h"
00034
00035
00036 #ifdef CEYLAN_USES_CONFIG_H
00037 #include "CeylanConfig.h"
00038 #endif // CEYLAN_USES_CONFIG_H
00039
00040
00041 #include <iostream>
00042 using std::endl ;
00043
00044 #include <sstream>
00045 using std::ostringstream ;
00046
00047 #include <iomanip>
00048
00049
00050 using std::string ;
00051
00052
00053 using namespace Ceylan ;
00054 using namespace Ceylan::Maths ;
00055 using namespace Ceylan::Maths::Linear ;
00056
00057 using Ceylan::Maths::Real ;
00058
00059
00060
00061
00062 Vector3::Vector3( Real x0, Real x1, Real x2 )
00063 {
00064
00065 _vec[0] = x0 ;
00066 _vec[1] = x1 ;
00067 _vec[2] = x2 ;
00068
00069 }
00070
00071
00072
00073 Vector3::~Vector3() throw()
00074 {
00075
00076 }
00077
00078
00079
00080 void Vector3::setTo( Real x0, Real x1, Real x2 )
00081 {
00082
00083 _vec[0] = x0 ;
00084 _vec[1] = x1 ;
00085 _vec[2] = x2 ;
00086
00087 }
00088
00089
00090
00091 void Vector3::nullify()
00092 {
00093
00094 setAllElementsTo( 0 ) ;
00095
00096 }
00097
00098
00099
00100 void Vector3::setAllElementsTo( Real commonValue )
00101 {
00102
00103
00104 for ( MatrixIndex i = 0 ; i < Dimensions ; i++ )
00105 {
00106 _vec[i] = 0 ;
00107 }
00108
00109
00110 }
00111
00112
00113
00114 Real Vector3::getElementAt( MatrixIndex index ) const
00115 {
00116
00117 #if CEYLAN_DEBUG
00118
00119 if ( index >= Dimensions )
00120 throw MathsException(
00121 "Vector3::getElementAt: index out of bounds." ) ;
00122
00123 #endif // CEYLAN_DEBUG
00124
00125 return _vec[ index ] ;
00126
00127 }
00128
00129
00130
00131 void Vector3::setElementAt( MatrixIndex index, Real newValue )
00132 {
00133
00134 #if CEYLAN_DEBUG
00135
00136 if ( index >= Dimensions )
00137 throw MathsException(
00138 "Vector3::setElementAt: index out of bounds." ) ;
00139
00140 #endif // CEYLAN_DEBUG
00141
00142 _vec[ index ] = newValue ;
00143
00144 }
00145
00146
00147
00148 void Vector3::normalize()
00149 {
00150
00151 Real mag = magnitude() ;
00152
00153 if ( IsNull( mag ) )
00154 throw LinearException(
00155 "Vector3::normalize: null vector cannot be normalized." ) ;
00156
00157 Real factor = static_cast<Real>( 1.0 ) / mag ;
00158
00159 for ( MatrixIndex i = 0 ; i < Dimensions ; i++ )
00160 {
00161 _vec[i] *= factor ;
00162 }
00163
00164 }
00165
00166
00167
00168 Real Vector3::magnitude() const
00169 {
00170
00171
00172
00173 Real sum = 0 ;
00174
00175 for ( MatrixIndex i = 0 ; i < Dimensions ; i++ )
00176 {
00177 sum += _vec[i] * _vec[i] ;
00178 }
00179
00180 return Sqrt( sum ) ;
00181
00182 }
00183
00184
00185
00186 const string Vector3::toString( VerbosityLevels level ) const
00187 {
00188
00189 string res ;
00190
00191 if ( TextDisplayable::GetOutputFormat() == TextDisplayable::html )
00192 {
00193
00194 res = "<table border=1>" ;
00195
00196 res += " <tr>\n" ;
00197
00198 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00199 {
00200 res += " <tr>\n" ;
00201 res += " <td>" + Ceylan::toString( _vec[i] ) + "</td>" ;
00202 res += " </tr>\n" ;
00203 }
00204
00205 res += "</table>" ;
00206
00207
00208 return res ;
00209 }
00210
00211
00212
00213 if ( level == high )
00214 {
00215 ostringstream oss ;
00216
00217 oss.precision( Ceylan::DigitOutputPrecision ) ;
00218
00219 oss << endl ;
00220
00221 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00222 {
00223 oss << "[ " << std::setw(5) << _vec[i] <<" ]" << endl ;
00224
00225 }
00226
00227 oss << endl ;
00228
00229 res = oss.str() ;
00230
00231 #if CEYLAN_DEBUG
00232
00233 if ( oss.fail() )
00234 {
00235 string message = "Vector3::toString: conversion error." ;
00236 Log::LogPlug::error( message ) ;
00237 return message ;
00238 }
00239
00240 #endif // CEYLAN_DEBUG
00241
00242 return res ;
00243 }
00244 else
00245 {
00246
00247 res = "Vector3: [ " ;
00248
00249 for ( MatrixIndex i = 0; i < Dimensions; i++ )
00250 {
00251 res += Ceylan::toString( _vec[i] )
00252 + ( ( i == Dimensions-1 ) ? " ]" : " ; " ) ;
00253 }
00254
00255 return res ;
00256
00257 }
00258
00259
00260 return res ;
00261
00262 }
00263
00264
00265
00266 bool Ceylan::Maths::Linear::operator == ( const Vector3 & v1,
00267 const Vector3 & v2 )
00268 {
00269
00270 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00271 {
00272
00273
00274
00275
00276
00277
00278 if ( ! AreRelativelyEqual<Real>( v1._vec[i],
00279 v2._vec[i] ) )
00280 return false ;
00281 }
00282
00283 return true ;
00284
00285 }
00286
00287
00288
00289 bool Ceylan::Maths::Linear::operator != ( const Vector3 & v1,
00290 const Vector3 & v2 )
00291 {
00292
00293 return ( ! ( v1 == v2 ) ) ;
00294
00295 }
00296
00297
00298
00299 Vector3 Ceylan::Maths::Linear::operator + ( const Vector3 & v1 ,
00300 const Vector3 & v2 )
00301 {
00302
00303 Vector3 result ;
00304
00305 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00306 {
00307 result._vec[i] = v1._vec[i] + v2._vec[i] ;
00308 }
00309
00310 return result ;
00311
00312 }
00313
00314
00315
00316 Vector3 Ceylan::Maths::Linear::operator - ( const Vector3 & v1 ,
00317 const Vector3 & v2 )
00318 {
00319
00320 Vector3 result ;
00321
00322 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00323 {
00324 result._vec[i] = v1._vec[i] - v2._vec[i] ;
00325 }
00326
00327 return result ;
00328
00329 }
00330
00331
00332
00333 Vector3 Ceylan::Maths::Linear::operator * ( Real lambda,
00334 const Vector3 & v )
00335 {
00336
00337 Vector3 result ;
00338
00339 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00340 {
00341 result._vec[i] = lambda * v._vec[i] ;
00342 }
00343
00344 return result ;
00345
00346 }
00347
00348
00349
00350 Vector3 Ceylan::Maths::Linear::operator * ( const Matrix3 & m,
00351 const Vector3 & v )
00352 {
00353
00354 Vector3 result ;
00355
00356 for ( MatrixIndex j = 0; j < Vector3::Dimensions; j++ )
00357 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00358 result._vec[j] += m._mat[i][j] * v._vec[i] ;
00359
00360 return result ;
00361
00362 }
00363
00364
00365
00366 Vector3 Ceylan::Maths::Linear::operator ^ ( const Vector3 & v1 ,
00367 const Vector3 & v2 )
00368 {
00369
00370
00371
00372 Vector3 result ;
00373
00374 result._vec[0] = v1._vec[1] * v2._vec[2] - v1._vec[2] * v2._vec[1] ;
00375 result._vec[1] = v1._vec[2] * v2._vec[0] - v1._vec[0] * v2._vec[2] ;
00376 result._vec[2] = v1._vec[0] * v2._vec[1] - v1._vec[1] * v2._vec[0] ;
00377
00378 return result ;
00379
00380 }
00381
00382
00383
00384 Real Ceylan::Maths::Linear::operator ~ ( const Vector3 & v )
00385 {
00386
00387 return v.magnitude() ;
00388
00389 }
00390
00391
00392
00393 Real Ceylan::Maths::Linear::operator | ( const Vector3 & v1 ,
00394 const Vector3 & v2 )
00395 {
00396
00397 Real result = 0 ;
00398
00399 for ( MatrixIndex i = 0; i < Vector3::Dimensions; i++ )
00400 {
00401 result += v1._vec[i] * v2._vec[i] ;
00402 }
00403
00404 return result ;
00405
00406 }
00407