library of assembled shared sources

http://lass.cocamware.com

vector_4d.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
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 /** Wrap index around range.
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 /** Wrap index around range.
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 /** A weird way to get back the same object
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 /** componentwise addition
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 /** componentwise subtraction
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 /** Componentwise multiplication.
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 /** Componentwise division.
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 /** add other to each component of this.
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 /** subtract other of each component of this.
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 /** multiply each component of this with other.
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 /** divide each component of this by other.
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 /** Return true if all the components are (exactly!) zero
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 /** Return true if at least one of the components is NaN
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 /** Return squared norm of vector.
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 /** Return norm of vector.
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 /** return a unit vector with same direction/sense as this vector.
00317  *
00318  * <i>The normalized vector of <b>X</b> is a vector in the same direction but with norm (length) 1.
00319  * It is denoted <b>X^</b> and given by <b>X^</b> = <b>X</b> / |<b>X</b>|</i>,
00320  * http://mathworld.wolfram.com/NormalizedVector.html.
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 /** Project vector on this one
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 /** Project vector on this one
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 /** apply a function to every component
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 /** Normalize vector.
00374  */
00375 template<typename T> inline
00376 void Vector4D<T>::normalize()
00377 {
00378     *this /= norm();
00379 }
00380 
00381 
00382 
00383 /** dot product.
00384  *  @relates lass::prim::Vector4D
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 /** @relates lass::prim::Vector4D
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 /** @relates lass::prim::Vector4D
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 /** componentwise addition
00415  *  @relates lass::prim::Vector4D
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 /** componentwise subtraction
00428  *  @relates lass::prim::Vector4D
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 /** Componentwise multiplication
00441  *  @relates lass::prim::Vector4D
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 /** Componentwise division
00454  *  @relates lass::prim::Vector4D
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 /** add b to all components of a.
00467  *  @relates lass::prim::Vector4D
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 /** subtract b of all components of a.
00480  *  @relates lass::prim::Vector4D
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 /** muliply all components of a by b
00493  *  @relates lass::prim::Vector4D
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 /** divide all components of a by b
00506  *  @relates lass::prim::Vector4D
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 /** add a to all components of b
00519  *  @relates lass::prim::Vector4D
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 /** subtract a of all components of b
00532  *  @relates lass::prim::Vector4D
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 /** multiply all components of b with a
00545  *  @relates lass::prim::Vector4D
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 /** return a vector with, for each coordinate, the minimum value of @a a and @a b
00558  *  @relates lass::prim::Vector4D
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 /** return a vector with, for each coordinate, the maximum value of @a a and @a b
00570  *  @relates lass::prim::Vector4D
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 /** interpolate linearly between two vectors: a + t * (b - a)
00582  *  @relates lass::prim::Vector4D
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 /** @relates lass::prim::Vector4D
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 /** @relates lass::prim::Vector4D
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 /** @relates lass::prim::Vector4D
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

Generated on Mon Nov 10 14:22:02 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo