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