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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_4D_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_4D_INL
00047
00048
00049
00050
00051 #include "vector_4d.h"
00052
00053
00054
00055 namespace lass
00056 {
00057
00058 namespace prim
00059 {
00060
00061 template<typename T> inline
00062 Vector4D<T>::Vector4D() :
00063 x(T()),
00064 y(T()),
00065 z(T()),
00066 w(T())
00067 {
00068 }
00069
00070
00071
00072 template<typename T> inline
00073 Vector4D<T>::Vector4D(TParam x, TParam y, TParam z, TParam w) :
00074 x(x),
00075 y(y),
00076 z(z),
00077 w(w)
00078 {
00079 }
00080
00081
00082
00083 template <typename T>
00084 template <typename U>
00085 Vector4D<T>::Vector4D(const Vector4D<U>& other):
00086 x(static_cast<TValue>(other.x)),
00087 y(static_cast<TValue>(other.y)),
00088 z(static_cast<TValue>(other.z)),
00089 w(static_cast<TValue>(other.w))
00090 {
00091 }
00092
00093
00094
00095 template <typename T>
00096 template <typename U>
00097 Vector4D<T>::Vector4D(const U& x, const U& y, const U& z, const U& w):
00098 x(static_cast<TValue>(x)),
00099 y(static_cast<TValue>(y)),
00100 z(static_cast<TValue>(z)),
00101 w(static_cast<TValue>(w))
00102 {
00103 }
00104
00105
00106
00107 template<typename T> inline
00108 typename Vector4D<T>::TConstReference Vector4D<T>::operator[](size_t index) const
00109 {
00110 LASS_ASSERT(index < dimension);
00111 return *(&x + index);
00112 }
00113
00114
00115
00116 template<typename T> inline
00117 typename Vector4D<T>::TReference Vector4D<T>::operator[](size_t index)
00118 {
00119 LASS_ASSERT(index < dimension);
00120 return *(&x + index);
00121 }
00122
00123
00124
00125
00126
00127 template<typename T> inline
00128 typename Vector4D<T>::TConstReference Vector4D<T>::at(signed index) const
00129 {
00130 return *(&x + num::mod(index, dimension));
00131 }
00132
00133
00134
00135
00136
00137 template<typename T> inline
00138 typename Vector4D<T>::TReference Vector4D<T>::at(signed index)
00139 {
00140 return *(&x + num::mod(index, dimension));
00141 }
00142
00143
00144
00145
00146
00147 template<typename T> inline
00148 const Vector4D<T>& Vector4D<T>::operator+() const
00149 {
00150 return *this;
00151 }
00152
00153
00154
00155 template<typename T> inline
00156 const Vector4D<T> Vector4D<T>::operator-() const
00157 {
00158 return Vector4D(-x, -y, -z, -w);
00159 }
00160
00161
00162
00163
00164
00165 template<typename T> inline
00166 Vector4D<T>& Vector4D<T>::operator+=(const Vector4D<T>& other)
00167 {
00168 x += other.x;
00169 y += other.y;
00170 z += other.z;
00171 w += other.w;
00172 return *this;
00173 }
00174
00175
00176
00177
00178
00179 template<typename T> inline
00180 Vector4D<T>& Vector4D<T>::operator-=(const Vector4D<T>& other)
00181 {
00182 x -= other.x;
00183 y -= other.y;
00184 z -= other.z;
00185 w -= other.w;
00186 return *this;
00187 }
00188
00189
00190
00191
00192
00193 template<typename T> inline
00194 Vector4D<T>& Vector4D<T>::operator*=(const Vector4D<T>& other)
00195 {
00196 x *= other.x;
00197 y *= other.y;
00198 z *= other.z;
00199 w *= other.w;
00200 return *this;
00201 }
00202
00203
00204
00205
00206
00207 template<typename T> inline
00208 Vector4D<T>& Vector4D<T>::operator/=(const Vector4D<T>& other)
00209 {
00210 x /= other.x;
00211 y /= other.y;
00212 z /= other.z;
00213 w /= other.w;
00214 return *this;
00215 }
00216
00217
00218
00219
00220
00221 template<typename T> inline
00222 Vector4D<T>& Vector4D<T>::operator+=(TParam other)
00223 {
00224 x += other;
00225 y += other;
00226 z += other;
00227 w += other;
00228 return *this;
00229 }
00230
00231
00232
00233
00234
00235 template<typename T> inline
00236 Vector4D<T>& Vector4D<T>::operator-=(TParam other)
00237 {
00238 x -= other;
00239 y -= other;
00240 z -= other;
00241 w -= other;
00242 return *this;
00243 }
00244
00245
00246
00247
00248
00249 template<typename T> inline
00250 Vector4D<T>& Vector4D<T>::operator*=(TParam other)
00251 {
00252 x *= other;
00253 y *= other;
00254 z *= other;
00255 w *= other;
00256 return *this;
00257 }
00258
00259
00260
00261
00262
00263 template<typename T> inline
00264 Vector4D<T>& Vector4D<T>::operator/=(TParam other)
00265 {
00266 x /= other;
00267 y /= other;
00268 z /= other;
00269 w /= other;
00270 return *this;
00271 }
00272
00273
00274
00275
00276
00277 template<typename T> inline
00278 const bool Vector4D<T>::isZero() const
00279 {
00280 return x == TNumTraits::zero && y == TNumTraits::zero &&
00281 z == TNumTraits::zero && w == TNumTraits::zero;
00282 }
00283
00284
00285
00286
00287
00288 template<typename T> inline
00289 const bool Vector4D<T>::isNaN() const
00290 {
00291 return num::isNaN(x) || num::isNaN(y) || num::isNaN(z) || num::isNaN(w);
00292 }
00293
00294
00295
00296
00297
00298 template<typename T> inline
00299 const typename Vector4D<T>::TValue Vector4D<T>::squaredNorm() const
00300 {
00301 return dot(*this, *this);
00302 }
00303
00304
00305
00306
00307
00308 template<typename T> inline
00309 const typename Vector4D<T>::TValue Vector4D<T>::norm() const
00310 {
00311 return num::sqrt(squaredNorm());
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 template<typename T>
00323 const Vector4D<T> Vector4D<T>::normal() const
00324 {
00325 Vector4D<T> result(*this);
00326 result.normalize();
00327 return result;
00328 }
00329
00330
00331
00332
00333
00334 template <typename T>
00335 const Vector4D<T> Vector4D<T>::project(const Vector4D<T>& other) const
00336 {
00337 Vector4D<T> result(*this);
00338 result *= dot(other, *this);
00339 result /= squaredNorm();
00340 return result;
00341 }
00342
00343
00344
00345
00346
00347 template<typename T> inline
00348 const Vector4D<T> Vector4D<T>::reject(const Vector4D<T>& other) const
00349 {
00350 return other - project(other);
00351 }
00352
00353
00354
00355 template<typename T> inline
00356 const Vector4D<T> Vector4D<T>::reflect(const Vector4D<T>& other) const
00357 {
00358 return 2 * project(other) - other;
00359 }
00360
00361
00362
00363
00364
00365 template <typename T>
00366 const Vector4D<T> Vector4D<T>::transform(T (*iOperator)(T)) const
00367 {
00368 return Vector4D<T>(iOperator(x), iOperator(y), iOperator(z), iOperator(w));
00369 }
00370
00371
00372
00373
00374
00375 template<typename T> inline
00376 void Vector4D<T>::normalize()
00377 {
00378 *this /= norm();
00379 }
00380
00381
00382
00383
00384
00385
00386 template<typename T> inline
00387 typename Vector4D<T>::TValue dot(const Vector4D<T>& a, const Vector4D<T>& b)
00388 {
00389 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
00390 }
00391
00392
00393
00394
00395
00396 template<typename T> inline
00397 bool operator==(const Vector4D<T>& a, const Vector4D<T>& b)
00398 {
00399 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
00400 }
00401
00402
00403
00404
00405
00406 template<typename T> inline
00407 bool operator!=(const Vector4D<T>& a, const Vector4D<T>& b)
00408 {
00409 return !(a == b);
00410 }
00411
00412
00413
00414
00415
00416
00417 template<typename T> inline
00418 Vector4D<T> operator+(const Vector4D<T>& a, const Vector4D<T>& b)
00419 {
00420 Vector4D<T> result(a);
00421 result += b;
00422 return result;
00423 }
00424
00425
00426
00427
00428
00429
00430 template<typename T> inline
00431 Vector4D<T> operator-(const Vector4D<T>& a, const Vector4D<T>& b)
00432 {
00433 Vector4D<T> result(a);
00434 result -= b;
00435 return result;
00436 }
00437
00438
00439
00440
00441
00442
00443 template<typename T> inline
00444 Vector4D<T> operator*(const Vector4D<T>& a, const Vector4D<T>& b)
00445 {
00446 Vector4D<T> result(a);
00447 result *= b;
00448 return result;
00449 }
00450
00451
00452
00453
00454
00455
00456 template<typename T> inline
00457 Vector4D<T> operator/(const Vector4D<T>& a, const Vector4D<T>& b)
00458 {
00459 Vector4D<T> result(a);
00460 result /= b;
00461 return result;
00462 }
00463
00464
00465
00466
00467
00468
00469 template<typename T> inline
00470 Vector4D<T> operator+(const Vector4D<T>& a, typename Vector4D<T>::TParam b)
00471 {
00472 Vector4D<T> result(a);
00473 result += b;
00474 return result;
00475 }
00476
00477
00478
00479
00480
00481
00482 template<typename T> inline
00483 Vector4D<T> operator-(const Vector4D<T>& a, typename Vector4D<T>::TParam b)
00484 {
00485 Vector4D<T> result(a);
00486 result -= b;
00487 return result;
00488 }
00489
00490
00491
00492
00493
00494
00495 template<typename T> inline
00496 Vector4D<T> operator*(const Vector4D<T>& a, typename Vector4D<T>::TParam b)
00497 {
00498 Vector4D<T> result(a);
00499 result *= b;
00500 return result;
00501 }
00502
00503
00504
00505
00506
00507
00508 template<typename T> inline
00509 Vector4D<T> operator/(const Vector4D<T>& a, typename Vector4D<T>::TParam b)
00510 {
00511 Vector4D<T> result(a);
00512 result /= b;
00513 return result;
00514 }
00515
00516
00517
00518
00519
00520
00521 template<typename T> inline
00522 Vector4D<T> operator+(typename Vector4D<T>::TParam a, const Vector4D<T>& b)
00523 {
00524 Vector4D<T> result(b);
00525 result += a;
00526 return result;
00527 }
00528
00529
00530
00531
00532
00533
00534 template<typename T> inline
00535 Vector4D<T> operator-(typename Vector4D<T>::TParam a, const Vector4D<T>& b)
00536 {
00537 Vector4D<T> result(-b);
00538 result += a;
00539 return result;
00540 }
00541
00542
00543
00544
00545
00546
00547 template<typename T> inline
00548 Vector4D<T> operator*(typename Vector4D<T>::TParam a, const Vector4D<T>& b)
00549 {
00550 Vector4D<T> result(b);
00551 result *= a;
00552 return result;
00553 }
00554
00555
00556
00557
00558
00559
00560 template<typename T> inline
00561 Vector4D<T> pointwiseMin(const Vector4D<T>& a, const Vector4D<T>& b)
00562 {
00563 return Vector4D<T>(std::min(a.x, b.x), std::min(a.y, b.y),
00564 std::min(a.z, b.z), std::min(a.w, b.w));
00565 }
00566
00567
00568
00569
00570
00571
00572 template<typename T> inline
00573 Vector4D<T> pointwiseMax(const Vector4D<T>& a, const Vector4D<T>& b)
00574 {
00575 return Vector4D<T>(std::max(a.x, b.x), std::max(a.y, b.y),
00576 std::max(a.z, b.z), std::max(a.w, b.w));
00577 }
00578
00579
00580
00581
00582
00583
00584 template<typename T>
00585 inline Vector4D<T> lerp(const Vector4D<T>& a, const Vector4D<T>& b, typename Vector4D<T>::TParam t)
00586 {
00587 Vector4D<T> result = b;
00588 result -= a;
00589 result *= t;
00590 result += a;
00591 return result;
00592 }
00593
00594
00595
00596
00597
00598 template<typename T, typename Char, typename Traits>
00599 std::basic_ostream<Char, Traits>& operator<<(
00600 std::basic_ostream<Char, Traits>& stream, const Vector4D<T>& b)
00601 {
00602 LASS_ENFORCE_STREAM(stream) << "(" << b.x << ", " << b.y << ", " << b.z << ", " << b.w << ")";
00603 return stream;
00604 }
00605
00606
00607
00608
00609
00610 template<typename T, typename Char, typename Traits>
00611 std::basic_istream<Char, Traits>& operator>>(
00612 std::basic_istream<Char, Traits>& stream, Vector4D<T>& b)
00613 {
00614 Vector4D<T> result;
00615
00616 char c = 0;
00617 stream >> c;
00618 if (c != '(')
00619 {
00620 stream.clear(std::ios::failbit);
00621 return stream;
00622 }
00623
00624 c = 0;
00625 stream >> result.x >> c;
00626 if (c != ',')
00627 {
00628 stream.clear(std::ios::failbit);
00629 return stream;
00630 }
00631
00632 c = 0;
00633 stream >> result.y >> c;
00634 if (c != ',')
00635 {
00636 stream.clear(std::ios::failbit);
00637 return stream;
00638 }
00639
00640 c = 0;
00641 stream >> result.z >> c;
00642 if (c != ',')
00643 {
00644 stream.clear(std::ios::failbit);
00645 return stream;
00646 }
00647
00648 c = 0;
00649 stream >> result.w >> c;
00650 if (c != ')')
00651 {
00652 stream.clear(std::ios::failbit);
00653 return stream;
00654 }
00655
00656 b = result;
00657 return stream;
00658 }
00659
00660
00661
00662
00663
00664 template<typename T>
00665 io::XmlOStream& operator<<(io::XmlOStream& stream, const Vector4D<T>& b)
00666 {
00667 LASS_ENFORCE_STREAM(stream)
00668 << "<Vector4D>" << b.x << " " << b.y << " " << b.z << " " << b.w << "</Vector4D>\n";
00669 return stream;
00670 }
00671
00672
00673
00674 }
00675
00676 }
00677
00678 #endif