Loading...
Searching...
No Matches
vector2.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_VECTOR2_H
#define BALL_MATHS_VECTOR2_H
#ifndef BALL_CONCEPT_PERSISTENCEMANAGER_H
# include <BALL/CONCEPT/persistenceManager.h>
#endif
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_MATHS_COMMON_H
# include <BALL/MATHS/common.h>
#endif
namespace BALL
{
template <typename T>
class TVector2;
template <typename T>
BALL_INLINE
TVector2<T> operator * (const T& scalar, const TVector2<T>& vector);
template <typename T>
std::istream& operator >> (std::istream& s, TVector2<T>& vector);
/* Output stream.
*/
template <typename T>
std::ostream& operator << (std::ostream& s, const TVector2<T>& vector);
template <typename T>
class TVector2
: public PersistentObject
{
public:
BALL_CREATE(TVector2<T>)
TVector2();
explicit TVector2(const T& value);
TVector2(const T& vx, const T& vy);
TVector2(const TVector2& vector);
TVector2(const T* ptr);
virtual ~TVector2();
virtual void clear();
virtual void persistentWrite(PersistenceManager& pm,
const char* name = 0) const;
virtual void persistentRead(PersistenceManager& pm);
void set(const T& value);
void set(const T& vx, const T& vy);
void set(const TVector2& vector);
TVector2& operator = (const TVector2& v);
TVector2& operator = (const T& value);
TVector2& operator = (const T* ptr);
T getLength() const;
T getSquareLength() const;
TVector2& normalize();
TVector2& negate();
static const TVector2& getZero();
static const TVector2& getUnit();
T& operator [] (Position position);
const T& operator [] (Position position) const;
const TVector2& operator + () const;
TVector2 operator - () const;
TVector2 operator + (const TVector2& b) const;
TVector2 operator - (const TVector2& b) const;
TVector2& operator += (const TVector2& vector);
TVector2& operator -= (const TVector2& vector);
TVector2 operator * (const T& scalar) const;
TVector2& operator *= (const T& scalar);
TVector2 operator / (const T& lambda) const;
TVector2& operator /= (const T& lambda);
T operator * (const TVector2& vector) const;
T getDistance(const TVector2& vector) const;
T getSquareDistance(const TVector2& vector) const;
bool operator == (const TVector2& vector) const;
bool operator != (const TVector2& vector) const;
bool isZero() const;
bool isOrthogonalTo(TVector2& vector) const;
void dump(std::ostream& s = std::cout, Size depth = 0) const;
bool isValid() const;
T x;
T y;
private:
};
template <typename T>
TVector2<T>::TVector2()
: PersistentObject(),
x(0),
y(0)
{
}
template <typename T>
TVector2<T>::TVector2(const T& value)
: PersistentObject(),
x(value),
y(value)
{
}
template <typename T>
TVector2<T>::TVector2(const T& vx, const T& vy)
: PersistentObject(),
x(vx),
y(vy)
{
}
template <typename T>
TVector2<T>::TVector2(const TVector2& vector)
: PersistentObject(),
x(vector.x),
y(vector.y)
{
}
template <typename T>
TVector2<T>::~TVector2()
{
}
template <typename T>
BALL_INLINE
TVector2<T>::TVector2(const T* ptr)
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
x = *ptr++;
y = *ptr;
}
template <typename T>
void TVector2<T>::clear()
{
x = y = (T)0;
}
template <typename T>
void TVector2<T>::persistentWrite(PersistenceManager& pm, const char* name) const
{
pm.writeObjectHeader(this, name);
pm.writePrimitive(x, "x");
pm.writePrimitive(y, "y");
pm.writeObjectTrailer(name);
}
template <typename T>
void TVector2<T>::persistentRead(PersistenceManager& pm)
{
pm.readPrimitive(x, "x");
pm.readPrimitive(y, "y");
}
template <typename T>
BALL_INLINE
void TVector2<T>::set(const T& value)
{
x = value;
y = value;
}
template <typename T>
BALL_INLINE
void TVector2<T>::set(const T& vx, const T& vy)
{
x = vx;
y = vy;
}
template <typename T>
BALL_INLINE
void TVector2<T>::set(const TVector2<T>& vector)
{
x = vector.x;
y = vector.y;
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator = (const TVector2<T>& vector)
{
x = vector.x;
y = vector.y;
return *this;
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator = (const T* ptr)
{
if (ptr == 0)
{
throw Exception::NullPointer(__FILE__, __LINE__);
}
x = *ptr++;;
y = *ptr;;
return *this;
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator = (const T& value)
{
x = value;
y = value;
return *this;
}
template <typename T>
BALL_INLINE
T TVector2<T>::getLength() const
{
return (T)sqrt(x * x + y * y);
}
template <typename T>
BALL_INLINE
T TVector2<T>::getSquareLength() const
{
return (T)(x * x + y * y);
}
template <typename T>
TVector2<T>& TVector2<T>::normalize()
{
T len = (T)sqrt(x * x + y * y);
if (Maths::isZero(len))
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
x /= len;
y /= len;
return *this;
}
template <typename T>
TVector2<T>& TVector2<T>::negate()
{
x *= -1;
y *= -1;
return *this;
}
template <typename T>
BALL_INLINE
const TVector2<T>& TVector2<T>::getZero()
{
static TVector2<T> null_vector(0, 0);
return null_vector;
}
template <typename T>
BALL_INLINE
const TVector2<T>& TVector2<T>::getUnit()
{
static TVector2<T> unit_vector(1, 1);
return unit_vector;
}
template <typename T>
BALL_INLINE
T& TVector2<T>::operator [] (Position position)
{
if (position > 1)
{
throw Exception::IndexOverflow(__FILE__, __LINE__);
}
switch (position)
{
case 0: return x;
case 1:
default:
return y;
}
}
template <typename T>
BALL_INLINE
const T& TVector2<T>::operator [] (Position position) const
{
if (position > 1)
{
throw Exception::IndexOverflow(__FILE__, __LINE__);
}
switch (position)
{
case 0: return x;
case 1:
default:
return y;
}
}
template <typename T>
BALL_INLINE
const TVector2<T>& TVector2<T>::operator + () const
{
return *this;
}
template <typename T>
BALL_INLINE
TVector2<T> TVector2<T>::operator - () const
{
return TVector2<T>(-x, -y);
}
template <typename T>
BALL_INLINE
TVector2<T> TVector2<T>::operator + (const TVector2<T>& b) const
{
return TVector2<T>(x + b.x, y + b.y);
}
template <typename T>
BALL_INLINE
TVector2<T> TVector2<T>::operator - (const TVector2<T>& b) const
{
return TVector2<T>(x - b.x, y - b.y);
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator += (const TVector2<T>& vector)
{
x += vector.x;
y += vector.y;
return *this;
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator -= (const TVector2<T>& vector)
{
x -= vector.x;
y -= vector.y;
return *this;
}
template <typename T>
BALL_INLINE
TVector2<T> TVector2<T>::operator * (const T& scalar) const
{
return TVector2<T>(x * scalar, y * scalar);
}
template <typename T>
BALL_INLINE
TVector2<T>& TVector2<T>::operator *= (const T &scalar)
{
x *= scalar;
y *= scalar;
return *this;
}
template <typename T>
TVector2<T> TVector2<T>::operator / (const T& lambda) const
{
if (lambda == (T)0)
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
return TVector2<T>(x / lambda, y / lambda);
}
template <typename T>
TVector2<T>& TVector2<T>::operator /= (const T& lambda)
{
if (lambda == (T)0)
{
throw Exception::DivisionByZero(__FILE__, __LINE__);
}
x /= lambda;
y /= lambda;
return *this;
}
template <typename T>
BALL_INLINE
T TVector2<T>::operator * (const TVector2<T>& vector) const
{
return (x * vector.x + y * vector.y);
}
template <typename T>
BALL_INLINE
T TVector2<T>::getDistance(const TVector2<T>& v) const
{
T dx = x - v.x;
T dy = y - v.y;
return (T)sqrt(dx * dx + dy * dy);
}
template <typename T>
BALL_INLINE
T TVector2<T>::getSquareDistance(const TVector2<T>& v) const
{
T dx = x - v.x;
T dy = y - v.y;
return (dx * dx + dy * dy);
}
template <typename T>
BALL_INLINE
bool TVector2<T>::operator == (const TVector2<T>& v) const
{
return (Maths::isEqual(x, v.x) && Maths::isEqual(y, v.y));
}
template <typename T>
BALL_INLINE
bool TVector2<T>::operator != (const TVector2<T>& v) const
{
return (Maths::isNotEqual(x, v.x) || Maths::isNotEqual(y, v.y));
}
template <typename T>
BALL_INLINE
bool TVector2<T>::isOrthogonalTo(TVector2<T>& v) const
{
return Maths::isZero((*this) * v);
}
template <typename T>
BALL_INLINE
bool TVector2<T>::isValid() const
{
return true;
}
template <typename T>
BALL_INLINE
bool TVector2<T>::isZero() const
{
return (Maths::isZero(x) && Maths::isZero(y));
}
template <typename T>
void TVector2<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 << ")" << std::endl;
BALL_DUMP_STREAM_SUFFIX(s);
}
typedef TVector2<float> Vector2;
template <typename T>
BALL_INLINE
TVector2<T> operator * (const T& scalar, const TVector2<T>& vector)
{
return TVector2<T>(scalar * vector.x, scalar * vector.y);
}
template <typename T>
std::istream& operator >> (std::istream& s, TVector2<T>& v)
{
char c;
s >> c >> v.x >> v.y >> c;
return s;
}
template <typename T>
std::ostream& operator << (std::ostream& s, const TVector2<T>& v)
{
s << "(" << v.x << ' ' << v.y << ')';
return s;
}
}// namespace BALL
#endif // BALL_MATHS_VECTOR2_H
exception.h
common.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::PersistenceManager
Definition persistenceManager.h:73
BALL::PersistenceManager::writeObjectHeader
void writeObjectHeader(const T *object, const char *name=0)
Definition persistenceManager.h:809
BALL::PersistenceManager::writeObjectTrailer
void writeObjectTrailer(const char *name=0)
BALL::PersistenceManager::readPrimitive
bool readPrimitive(T &t, const char *name)
Definition persistenceManager.h:827
BALL::PersistenceManager::writePrimitive
void writePrimitive(const T &t, const char *name)
Definition persistenceManager.h:818
BALL::PersistentObject::PersistentObject
PersistentObject()
Definition persistentObject.h:39
BALL::TVector2
Definition vector2.h:63
BALL::TVector2< float >::dump
void dump(std::ostream &s=std::cout, Size depth=0) const
BALL::TVector2< float >::persistentRead
virtual void persistentRead(PersistenceManager &pm)
BALL::TVector2< float >::set
void set(const float &value)
BALL::TVector2< float >::y
float y
Definition vector2.h:376
BALL::TVector2::~TVector2
virtual ~TVector2()
Definition vector2.h:418
BALL::TVector2< float >::x
float x
Definition vector2.h:372
BALL::TVector2::operator==
bool operator==(const TVector2 &vector) const
Definition vector2.h:720
BALL::TVector2< float >::getDistance
float getDistance(const TVector2 &vector) const
BALL::TVector2< float >::getLength
float getLength() const
BALL::TVector2::operator=
TVector2 & operator=(const TVector2 &v)
Definition vector2.h:483
BALL::TVector2< float >::getUnit
static const TVector2 & getUnit()
BALL::TVector2::operator!=
bool operator!=(const TVector2 &vector) const
Definition vector2.h:727
BALL::TVector2::operator[]
T & operator[](Position position)
Definition vector2.h:571
BALL::TVector2::operator/
TVector2 operator/(const T &lambda) const
Definition vector2.h:669
BALL::TVector2::TVector2
TVector2()
Definition vector2.h:386
BALL::TVector2< float >::isValid
bool isValid() const
BALL::TVector2::operator*=
TVector2 & operator*=(const T &scalar)
Definition vector2.h:660
BALL::TVector2< float >::isZero
bool isZero() const
BALL::TVector2::operator+
const TVector2 & operator+() const
Definition vector2.h:605
BALL::TVector2< float >::persistentWrite
virtual void persistentWrite(PersistenceManager &pm, const char *name=0) const
BALL::TVector2::operator+=
TVector2 & operator+=(const TVector2 &vector)
Definition vector2.h:633
BALL::TVector2< float >::isOrthogonalTo
bool isOrthogonalTo(TVector2 &vector) const
BALL::TVector2< float >::normalize
TVector2 & normalize()
BALL::TVector2< float >::negate
TVector2 & negate()
BALL::TVector2::operator*
TVector2 operator*(const T &scalar) const
Definition vector2.h:653
BALL::TVector2::operator-=
TVector2 & operator-=(const TVector2 &vector)
Definition vector2.h:643
BALL::TVector2< float >::getSquareLength
float getSquareLength() const
BALL::TVector2< float >::getSquareDistance
float getSquareDistance(const TVector2 &vector) const
BALL::TVector2::operator/=
TVector2 & operator/=(const T &lambda)
Definition vector2.h:679
BALL::TVector2< float >::getZero
static const TVector2 & getZero()
BALL::TVector2::operator-
TVector2 operator-() const
Definition vector2.h:612
BALL::TVector2< float >::clear
virtual void clear()
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::Vector2
TVector2< float > Vector2
Definition vector2.h:771
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::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>>
std::istream & operator>>(std::istream &is, TRegularData1D< ValueType > &grid)
Input operator.
Definition regularData1D.h:802
std
STL namespace.
persistenceManager.h