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