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