Loading...
Searching...
No Matches
vector4.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_VECTOR4_H
#define BALL_MATHS_VECTOR4_H
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_MATHS_ANGLE_H
# include <BALL/MATHS/angle.h>
#endif
#ifdef BALL_HAS_IEEEFP_H
# include <ieeefp.h>
#endif
namespace BALL
{
template <typename T>
class TVector4;
template <typename T>
BALL_INLINE
TVector4<T> operator + (const TVector4<T>& a, const TVector4<T>& b);
template <typename T>
BALL_INLINE
TVector4<T> operator - (const TVector4<T>& a, const TVector4<T>& b);
template <typename T>
std::istream& operator >> (std::istream& s, TVector4<T>& vector);
template <typename T>
std::ostream& operator << (std::ostream& s, const TVector4<T>& vector);
template <typename T>
class TVector4
{
public:
BALL_CREATE(TVector4<T>)
TVector4();
TVector4(const T* ptr);
explicit TVector4(const T& value);
TVector4(const T& x, const T& y, const T& z, const T& h = (T)1);
TVector4(const TVector4& vector);
virtual ~TVector4();
virtual void clear()
{
x = y = z = h = (T)0;
}
void set(const T* ptr);
void set(const T& rx, const T& ry, const T& rz, const T& rh = (T)1);
void set(const TVector4& vector);
TVector4& operator = (const T* ptr);
TVector4& operator = (const TVector4& vector);
TVector4& operator = (T value);
void get(T* ptr) const;
void get(T& rx, T& ry, T& rz, T& rh) const;
void get(TVector4& vector) const;
void swap(TVector4& vector);
T getLength() const;
T getSquareLength() const;
TVector4& normalize();
static const TVector4& getZero();
static const TVector4& getUnit();
void set(const T& value = (T)1);
T& operator [] (Position position);
const T& operator [] (Position position) const;
TVector4 operator + () const;
TVector4 operator - () const;
TVector4& operator += (const TVector4& vector);
TVector4& operator -= (const TVector4& vector);
TVector4 operator * (const T& scalar);
TVector4& operator *= (const T& scalar);
TVector4 operator / (const T& scalar);
TVector4& operator /= (const T& scalar);
T operator * (const TVector4& vector) const;
T getDistance(const TVector4& vector) const;
T getSquareDistance(const TVector4& vector) const;
bool operator == (const TVector4& vector) const;
bool operator != (const TVector4& vector) const;
bool isOrthogonalTo(const TVector4& vector) const;
bool isValid() const;
void dump(std::ostream& s = std::cout, Size depth = 0) const;
T x;
T y;
T z;
T h;
};
template <typename T>
TVector4<T>::TVector4()
: x(0),
y(0),
z(0),
h(0)
{
}
template <typename T>
TVector4<T>::TVector4(const T* ptr)
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
x = *ptr++;
y = *ptr++;
z = *ptr++;
h = *ptr;
}
template <typename T>
TVector4<T>::TVector4(const T& value)
: x(value),
y(value),
z(value),
h(value)
{
}
template <typename T>
TVector4<T>::TVector4(const T& x, const T& y, const T& z, const T& h)
: x(x),
y(y),
z(z),
h(h)
{
}
template <typename T>
TVector4<T>::TVector4(const TVector4<T>& v)
: x(v.x),
y(v.y),
z(v.z),
h(v.h)
{
}
template <typename T>
TVector4<T>::~TVector4()
{
}
template <typename T>
BALL_INLINE
void TVector4<T>::set(const T* ptr)
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
x = *ptr++;
y = *ptr++;
z = *ptr++;
h = *ptr;
}
template <typename T>
BALL_INLINE
void TVector4<T>::set(const T& rx, const T& ry, const T& rz, const T& rh)
{
x = rx;
y = ry;
z = rz;
h = rh;
}
template <typename T>
BALL_INLINE
void TVector4<T>::set(const TVector4<T>& v)
{
x = v.x;
y = v.y;
z = v.z;
h = v.h;
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator = (const T* ptr)
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
x = *ptr++;
y = *ptr++;
z = *ptr++;
h = *ptr;
return *this;
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator = (const TVector4<T>& v)
{
x = v.x;
y = v.y;
z = v.z;
h = v.h;
return *this;
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator = (T value)
{
x = value;
y = value;
z = value;
h = value;
return *this;
}
template <typename T>
BALL_INLINE
void TVector4<T>::get(T* ptr) const
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
*ptr++ = x;
*ptr++ = y;
*ptr++ = z;
*ptr = h;
}
template <typename T>
BALL_INLINE
void TVector4<T>::get(T& rx, T& ry, T& rz, T& rh) const
{
rx = x;
ry = y;
rz = z;
rh = h;
}
template <typename T>
BALL_INLINE
void TVector4<T>::get(TVector4<T>& v) const
{
v.x = x;
v.y = y;
v.z = z;
v.h = h;
}
template <typename T>
void TVector4<T>::swap(TVector4<T>& v)
{
T temp = x;
x = v.x;
v.x = temp;
temp = y;
y = v.y;
v.y = temp;
temp = z;
z = v.z;
v.z = temp;
temp = h;
h = v.h;
v.h = temp;
}
template <typename T>
BALL_INLINE
T TVector4<T>::getLength() const
{
return (T)sqrt(x * x + y * y + z * z + h * h);
}
template <typename T>
BALL_INLINE
T TVector4<T>::getSquareLength() const
{
return (T)(x * x + y * y + z * z + h * h);
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::normalize()
{
T len = (T)sqrt(x * x + y * y + z * z + h * h);
if (Maths::isZero(len))
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
x /= len;
y /= len;
z /= len;
h /= len;
return *this;
}
template <typename T>
BALL_INLINE
const TVector4<T>& TVector4<T>::getZero()
{
static const TVector4<T> null4(0, 0, 0, 0);
return null4;
}
template <typename T>
BALL_INLINE
const TVector4<T>& TVector4<T>::getUnit()
{
static const TVector4<T> unit_vector(1, 1, 1, 1);
return unit_vector;
}
template <typename T>
BALL_INLINE
void TVector4<T>::set(const T& value)
{
x = y = z = h = value;
}
template <typename T>
BALL_INLINE
T& TVector4<T>::operator [] (Position pos)
{
if (pos > 3)
{
throw Exception::IndexOverflow(__FILE__, __LINE__);
}
switch (pos)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3:
default:
return h;
}
}
template <typename T>
BALL_INLINE
const T& TVector4<T>::operator [] (Position pos) const
{
if (pos > 3)
{
throw Exception::IndexOverflow(__FILE__, __LINE__);
}
switch (pos)
{
case 0: return x;
case 1: return y;
case 2: return z;
case 3:
default:
return h;
}
}
template <typename T>
BALL_INLINE
TVector4<T> TVector4<T>::operator + () const
{
return *this;
}
template <typename T>
BALL_INLINE
TVector4<T> TVector4<T>::operator - () const
{
return TVector4<T>(-x, -y, -z, -h);
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator += (const TVector4<T>& v)
{
x += v.x;
y += v.y;
z += v.z;
h += v.h;
return *this;
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator -= (const TVector4<T> &v)
{
x -= v.x;
y -= v.y;
z -= v.z;
h -= v.h;
return *this;
}
template <typename T>
BALL_INLINE
TVector4<T> TVector4<T>::operator * (const T& scalar)
{
return TVector4<T>(x * scalar, y * scalar, z * scalar, h * scalar);
}
template <typename T>
BALL_INLINE
TVector4<T>& TVector4<T>::operator *= (const T &scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
h *= scalar;
return *this;
}
template <typename T>
TVector4<T>TVector4<T>::operator / (const T &scalar)
{
if (Maths::isZero(scalar))
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
return TVector4<T>(x / scalar, y / scalar, z / scalar, h / scalar);
}
template <typename T>
TVector4<T>& TVector4<T>::operator /= (const T& scalar)
{
if (Maths::isZero(scalar))
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
x /= scalar;
y /= scalar;
z /= scalar;
h /= scalar;
return *this;
}
template <typename T>
BALL_INLINE
T TVector4<T>::operator * (const TVector4<T>& v) const
{
return (x * v.x + y * v.y + z * v.z + h * v.h);
}
template <typename T>
BALL_INLINE
T TVector4<T>::getDistance(const TVector4<T> &v) const
{
T da = x - v.x;
T db = y - v.y;
T dc = z - v.z;
T dd = h - v.h;
return (T)sqrt(da * da + db * db + dc * dc + dd * dd);
}
template <typename T>
BALL_INLINE
T TVector4<T>::getSquareDistance(const TVector4<T> &v) const
{
T da = x - v.x;
T db = y - v.y;
T dc = z - v.z;
T dd = h - v.h;
return (da * da + db * db + dc * dc + dd * dd);
}
template <typename T>
BALL_INLINE
bool TVector4<T>::operator == (const TVector4<T>& v) const
{
return (Maths::isEqual(x, v.x) && Maths::isEqual(y, v.y)
&& Maths::isEqual(z, v.z) && Maths::isEqual(h, v.h));
}
template <typename T>
BALL_INLINE
bool TVector4<T>::operator != (const TVector4<T>& v) const
{
return (Maths::isNotEqual(x, v.x) || Maths::isNotEqual(y, v.y)
|| Maths::isNotEqual(z, v.z) || Maths::isNotEqual(h, v.h));
}
template <typename T>
BALL_INLINE
bool TVector4<T>::isOrthogonalTo(const TVector4<T>& v) const
{
return Maths::isZero(*this * v);
}
template <typename T>
BALL_INLINE
bool TVector4<T>::isValid() const
{
return true;
}
template <typename T>
void TVector4<T>::dump(std::ostream& s, Size depth) const
{
BALL_DUMP_STREAM_PREFIX(s);
BALL_DUMP_HEADER(s, this, this);
BALL_DUMP_DEPTH(s, depth);
s << "x= " << x
<< ", y = " << y
<< ", z = " << z
<< ", h = " << h << std::endl;
BALL_DUMP_STREAM_SUFFIX(s);
}
typedef TVector4<float> Vector4;
template <typename T>
BALL_INLINE
TVector4<T> operator + (const TVector4<T>& a, const TVector4<T>& b)
{
return TVector4<T>(a.x + b.x, a.y + b.y, a.z + b.z, a.h + b.h);
}
template <typename T>
BALL_INLINE
TVector4<T> operator - (const TVector4<T>& a, const TVector4<T>& b)
{
return TVector4<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.h - b.h);
}
template <typename T>
BALL_INLINE
TVector4<T> operator * (const T& scalar, const TVector4<T>& v)
{
return TVector4<T>(scalar * v.x, scalar * v.y, scalar * v.z, scalar * v.h);
}
template <typename T>
BALL_INLINE
TVector4<T> operator * (const TVector4<T>& v, const T& scalar)
{
return TVector4<T>(scalar * v.x, scalar * v.y, scalar * v.z, scalar * v.h);
}
template <typename T>
std::istream& operator >> (std::istream& s, TVector4<T>& v)
{
char c;
s >> c >> v.x >> v.y >> v.z >> v.h >>c;
return s;
}
template <typename T>
std::ostream& operator << (std::ostream& s, const TVector4<T>& v)
{
s << '(' <<v.x << ' ' << v.y << ' ' << v.z << ' ' << v.h << ')';
return s;
}
} // namespace BALL
#endif // BALL_MATHS_VECTOR4_H
exception.h
db
db
Definition GAFFTypeProcessor.h:74
angle.h
BALL::Exception::DivisionByZero
Definition COMMON/exception.h:342
BALL::Exception::IndexOverflow
Definition COMMON/exception.h:164
BALL::Exception::NullPointer
Definition COMMON/exception.h:260
BALL::TVector4
Definition vector4.h:53
BALL::TVector4::getLength
T getLength() const
Definition vector4.h:562
BALL::TVector4::TVector4
TVector4()
Definition vector4.h:377
BALL::TVector4< float >::z
float z
Definition vector4.h:366
BALL::TVector4< float >::clear
virtual void clear()
Definition vector4.h:107
BALL::TVector4::get
void get(TVector4 &vector) const
Definition vector4.h:532
BALL::TVector4::operator/=
TVector4 & operator/=(const T &scalar)
Definition vector4.h:722
BALL::TVector4::getSquareDistance
T getSquareDistance(const TVector4 &vector) const
Definition vector4.h:757
BALL::TVector4< float >::h
float h
Definition vector4.h:370
BALL::TVector4::normalize
TVector4 & normalize()
Definition vector4.h:576
BALL::TVector4::get
void get(T *ptr) const
Definition vector4.h:508
BALL::TVector4::set
void set(const T *ptr)
Definition vector4.h:434
BALL::TVector4::operator/
TVector4 operator/(const T &scalar)
Definition vector4.h:712
BALL::TVector4::getUnit
static const TVector4 & getUnit()
Definition vector4.h:603
BALL::TVector4::operator-=
TVector4 & operator-=(const TVector4 &vector)
Definition vector4.h:682
BALL::TVector4< float >::x
float x
Definition vector4.h:358
BALL::TVector4::operator[]
T & operator[](Position position)
Definition vector4.h:618
BALL::TVector4::operator=
TVector4 & operator=(const T *ptr)
Definition vector4.h:468
BALL::TVector4::getSquareLength
T getSquareLength() const
Definition vector4.h:569
BALL::TVector4::getDistance
T getDistance(const TVector4 &vector) const
Definition vector4.h:745
BALL::TVector4::operator!=
bool operator!=(const TVector4 &vector) const
Definition vector4.h:777
BALL::TVector4::set
void set(const T &value=(T) 1)
Definition vector4.h:611
BALL::TVector4::set
void set(const T &rx, const T &ry, const T &rz, const T &rh=(T) 1)
Definition vector4.h:448
BALL::TVector4::isOrthogonalTo
bool isOrthogonalTo(const TVector4 &vector) const
Definition vector4.h:785
BALL::TVector4::operator*
TVector4 operator*(const T &scalar)
Definition vector4.h:694
BALL::TVector4::operator+
TVector4 operator+() const
Definition vector4.h:656
BALL::TVector4::~TVector4
virtual ~TVector4()
Definition vector4.h:428
BALL::TVector4::get
void get(T &rx, T &ry, T &rz, T &rh) const
Definition vector4.h:522
BALL::TVector4::getZero
static const TVector4 & getZero()
Definition vector4.h:595
BALL::TVector4::operator==
bool operator==(const TVector4 &vector) const
Definition vector4.h:769
BALL::TVector4::set
void set(const TVector4 &vector)
Definition vector4.h:458
BALL::TVector4::operator+=
TVector4 & operator+=(const TVector4 &vector)
Definition vector4.h:670
BALL::TVector4< float >::y
float y
Definition vector4.h:362
BALL::TVector4::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
Definition vector4.h:798
BALL::TVector4::operator-
TVector4 operator-() const
Definition vector4.h:663
BALL::TVector4::swap
void swap(TVector4 &vector)
Definition vector4.h:541
BALL::TVector4::operator*=
TVector4 & operator*=(const T &scalar)
Definition vector4.h:701
BALL::TVector4::isValid
bool isValid() const
Definition vector4.h:792
BALL_CREATE
#define BALL_CREATE(name)
Definition create.h:62
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
BALL::operator<<
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
BALL_DUMP_STREAM_PREFIX
#define BALL_DUMP_STREAM_PREFIX(os)
Definition macros.h:391
BALL_DUMP_STREAM_SUFFIX
#define BALL_DUMP_STREAM_SUFFIX(os)
Definition macros.h:395
BALL_DUMP_DEPTH
#define BALL_DUMP_DEPTH(os, depth)
Definition macros.h:390
BALL_DUMP_HEADER
#define BALL_DUMP_HEADER(os, cl, ob)
Definition macros.h:393
BALL::Maths::isNotEqual
bool isNotEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:224
BALL::Maths::isZero
bool isZero(const T &t)
Definition MATHS/common.h:189
BALL::Maths::isEqual
bool isEqual(const T1 &a, const T2 &b)
Definition MATHS/common.h:212
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
BALL::Vector4
TVector4< float > Vector4
Definition vector4.h:815
BALL::operator*
BALL_INLINE TAngle< T > operator*(const T &val, const TAngle< T > &angle)
Definition angle.h:704
BALL::Position
BALL_SIZE_TYPE Position
Definition COMMON/global.h:140
BALL::operator+
ConstRandomAccessIterator< Container, DataType, Position, Traits > operator+(Distance distance, const ConstRandomAccessIterator< Container, DataType, Position, Traits > &iterator)
Definition randomAccessIterator.h:192
BALL::operator>>
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
Definition regularData1D.h:802
BALL::operator-
BALL_INLINE TAngle< T > operator-(const T &val, const TAngle< T > &angle)
Definition angle.h:724