EMAN2
Public Types | Public Member Functions | Private Attributes
EMAN::Vec2< Type > Class Template Reference

The Vec2 is precisely the same as Vec3 except it works exclusively in 2D Note there are convenient typedef so one needn't bother about using template terminology typedef Vec2<float> Vec2f; typedef Vec2<int> Vec2i; typedef Vec2double> Vec2d; // Not recommended for use unless precision is addressed in this class. More...

#include <vec3.h>

List of all members.

Public Types

typedef Type type
 One can always cast to the type of a Vec2 by accessing Vec2<Type>::type.

Public Member Functions

 Vec2 ()
 contruct a Vec2 object with all elements equal to 0.
template<typename Type2 , typename Type3 >
 Vec2 (const Type2 &x, const Type3 &y)
 contruct a Vec2 object given (x,y) or (x,y,z) values.
template<typename Type2 >
 Vec2 (const vector< Type2 > &v)
 Construct a Vec2 object given a std::vector object.
template<typename Type2 >
 Vec2 (const Vec2< Type2 > &v)
 Copy constructor copies vector elements.
 ~Vec2 ()
 Destructor.
float normalize ()
 Normalize the vector and return its length before the normalization.
float length () const
 Calculate its length.
Type squared_length () const
 Calculate its squared length.
template<typename Type2 >
Type dot (const Vec2< Type2 > &v) const
 Calculate the dot product of 'this' vector with a second vector.
vector< Type > as_list () const
 Return the values of this vector as a std::vector.
template<typename Type2 >
void set_value (const vector< Type2 > &v)
 Set new values using a std::vector object.
template<typename Type2 >
void set_value_at (int index, const Type2 &value)
 Set values at a particular index.
void set_value (const Type &x, const Type &y)
 Set new values to this vector object.
Type operator[] (int i) const
 Get the ith item of the vector.
Type & operator[] (int i)
 Get the ith item of the vector.
Type at (int i)
 Get the ith item of the vector.
int number_of_element ()
 For python __len__.
Type * begin ()
 Add this function to make it iterable in Python, so we can call list() or tuple() to convert Vec3f in python to a list or tuple.
Type * end ()
 Add this function to make it iterable in Python, so we can call list() or tuple() to convert Vec3f in python to a list or tuple.
template<typename Type2 >
Vec2< Type > & operator+= (const Vec2< Type2 > &v)
 'this' += v; Add the 2 vectors by adding item by item.
template<typename Type2 >
Vec2< Type > & operator+= (const Type2 &d)
 'this' += d.
template<typename Type2 >
Vec2< Type > & operator-= (const Vec2< Type2 > &v)
 'this' -= v; Minus the 2 vectors item by item.
template<typename Type2 >
Vec2< Type > & operator-= (const Type2 &d)
 'this' -= d; Minus a number from each item of 'this' vector.
template<typename Type2 >
Vec2< Type > & operator*= (const Type2 &d)
 'this' *= d; Multiply a number on each item of 'this' vector.
template<typename Type2 >
Vec2< Type > & operator/= (const Type2 &d)
 'this' /= d; Divide a number on each item of 'this' vector.

Private Attributes

Type vec [2]

Detailed Description

template<typename Type>
class EMAN::Vec2< Type >

The Vec2 is precisely the same as Vec3 except it works exclusively in 2D Note there are convenient typedef so one needn't bother about using template terminology typedef Vec2<float> Vec2f; typedef Vec2<int> Vec2i; typedef Vec2double> Vec2d; // Not recommended for use unless precision is addressed in this class.

Author:
David Woolford (based on the work of who ever wrote the original Vec3f and Vec3i classes - extracted into a template)
Date:
August 2008 ?

Definition at line 713 of file vec3.h.


Member Typedef Documentation

template<typename Type>
typedef Type EMAN::Vec2< Type >::type

One can always cast to the type of a Vec2 by accessing Vec2<Type>::type.

Definition at line 718 of file vec3.h.


Constructor & Destructor Documentation

template<typename Type>
EMAN::Vec2< Type >::Vec2 ( ) [inline]

contruct a Vec2 object with all elements equal to 0.

Definition at line 722 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                         {
                        vec[0] = static_cast<Type>(0);
                        vec[1] = static_cast<Type>(0);
                }
template<typename Type>
template<typename Type2 , typename Type3 >
EMAN::Vec2< Type >::Vec2 ( const Type2 &  x,
const Type3 &  y 
) [inline]

contruct a Vec2 object given (x,y) or (x,y,z) values.

Parameters:
xValue of the first item.
yValue of the second item. default to 0.

Definition at line 733 of file vec3.h.

References EMAN::Vec2< Type >::vec, x, and y.

                {
                        vec[0] = static_cast<Type>(x);
                        vec[1] = static_cast<Type>(y);
                }
template<typename Type>
template<typename Type2 >
EMAN::Vec2< Type >::Vec2 ( const vector< Type2 > &  v) [inline]

Construct a Vec2 object given a std::vector object.

The std::vector object should have at least 3 items.

Parameters:
vThe std::vector object. It should have at least 3 items.

Definition at line 744 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        vec[0] = static_cast<Type>(v[0]);
                        vec[1] = static_cast<Type>(v[1]);
                }
template<typename Type>
template<typename Type2 >
EMAN::Vec2< Type >::Vec2 ( const Vec2< Type2 > &  v) [inline]

Copy constructor copies vector elements.

Definition at line 753 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        vec[0] = static_cast<Type>(v[0]);
                        vec[1] = static_cast<Type>(v[1]);
                }
template<typename Type>
EMAN::Vec2< Type >::~Vec2 ( ) [inline]

Destructor.

Definition at line 761 of file vec3.h.

{}

Member Function Documentation

template<typename Type>
vector<Type> EMAN::Vec2< Type >::as_list ( ) const [inline]

Return the values of this vector as a std::vector.

Returns:
The std::vector version of this vector.

Definition at line 816 of file vec3.h.

References v, and EMAN::Vec2< Type >::vec.

                {
                        vector < Type > v(2);
                        v[0] = vec[0];
                        v[1] = vec[1];
                        return v;
                }
template<typename Type>
Type EMAN::Vec2< Type >::at ( int  i) [inline]

Get the ith item of the vector.

Used in the left side of the assignment.

Parameters:
iThe index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 880 of file vec3.h.

References EMAN::Vec2< Type >::vec.

{ return vec[i]; }
template<typename Type>
Type* EMAN::Vec2< Type >::begin ( void  ) [inline]

Add this function to make it iterable in Python, so we can call list() or tuple() to convert Vec3f in python to a list or tuple.

Returns:
the iterator (here is the pointer) of the first element

Definition at line 897 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        return &vec[0];
                }
template<typename Type>
template<typename Type2 >
Type EMAN::Vec2< Type >::dot ( const Vec2< Type2 > &  v) const [inline]

Calculate the dot product of 'this' vector with a second vector.

Parameters:
vThe second vector to do the dot product.
Returns:
The dot product.

Definition at line 808 of file vec3.h.

References EMAN::Vec2< Type >::vec.

Referenced by EMAN::operator*(), and EMAN::Util::point_is_in_triangle_2d().

                {
                        return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1]));
                }
template<typename Type>
Type* EMAN::Vec2< Type >::end ( void  ) [inline]

Add this function to make it iterable in Python, so we can call list() or tuple() to convert Vec3f in python to a list or tuple.

Returns:
the iterator (here is the pointer) of the one beyond the last element.

Definition at line 907 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        return &vec[2];
                }
template<typename Type>
float EMAN::Vec2< Type >::length ( ) const [inline]

Calculate its length.

Returns:
The vector's length Warning - float precision

Definition at line 787 of file vec3.h.

References sqrt(), t, and EMAN::Vec2< Type >::vec.

Referenced by EMAN::Vec2< Type >::normalize().

                {
                        float t = (float)(vec[0] * vec[0] + vec[1] * vec[1]);
                        return (float)sqrt(t);
                }
template<typename Type>
float EMAN::Vec2< Type >::normalize ( ) [inline]

Normalize the vector and return its length before the normalization.

Returns:
The length of the Vec before normalization.

Definition at line 768 of file vec3.h.

References EMAN::Vec2< Type >::length(), EMAN::Vec2< Type >::set_value(), and EMAN::Vec2< Type >::vec.

                {
                // Warning - float precision
                        float len = length();
                        if (len != 0) {
                                vec[0] = static_cast<Type> (vec[0] / len);
                                vec[1] = static_cast<Type> (vec[1] / len);
                        }
                        else {
                                set_value(0, 0);
                        }
                        return len;
                }
template<typename Type>
int EMAN::Vec2< Type >::number_of_element ( ) [inline]

For python __len__.

Returns:
the number of elements in this container. it's always 2.

Definition at line 887 of file vec3.h.

                {
                        return 2;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator*= ( const Type2 &  d) [inline]

'this' *= d; Multiply a number on each item of 'this' vector.

Parameters:
dThe number to multiply.
Returns:
The new 'this' as a result of multiplication.

Definition at line 962 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                        {
                        vec[0] = static_cast<Type>(vec[0]*d);
                        vec[1] = static_cast<Type>(vec[1]*d);
                        return *this;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator+= ( const Type2 &  d) [inline]

'this' += d.

Add d to each item of this vector.

Parameters:
dThe number used to be added to this vector.
Returns:
The new 'this' as a result of add.

Definition at line 929 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                        {
                        vec[0] = static_cast<Type>(vec[0]+d);
                        vec[1] = static_cast<Type>(vec[1]+d);
                        return *this;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator+= ( const Vec2< Type2 > &  v) [inline]

'this' += v; Add the 2 vectors by adding item by item.

Parameters:
vThe vector used to be added to 'this' vector.
Returns:
The new 'this' as a result of add.

Definition at line 918 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                              {
                        vec[0] = static_cast<Type>(vec[0]+v[0]);
                        vec[1] = static_cast<Type>(vec[1]+v[1]);
                        return *this;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator-= ( const Type2 &  d) [inline]

'this' -= d; Minus a number from each item of 'this' vector.

Parameters:
dThe number used to be substracted from 'this' vector.
Returns:
The new 'this' as a result of substraction.

Definition at line 951 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                        {
                        vec[0] = static_cast<Type>(vec[0]-d);
                        vec[1] = static_cast<Type>(vec[1]-d);
                        return *this;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator-= ( const Vec2< Type2 > &  v) [inline]

'this' -= v; Minus the 2 vectors item by item.

Parameters:
vThe vector used to be substracted from 'this' vector.
Returns:
The new 'this' as a result of substraction.

Definition at line 940 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                              {
                        vec[0] = static_cast<Type>(vec[0]-v[0]);
                        vec[1] = static_cast<Type>(vec[1]-v[1]);
                        return *this;
                }
template<typename Type>
template<typename Type2 >
Vec2<Type>& EMAN::Vec2< Type >::operator/= ( const Type2 &  d) [inline]

'this' /= d; Divide a number on each item of 'this' vector.

Parameters:
dThe number to divide.
Returns:
The new 'this' as a result of division.

Definition at line 973 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                                                        {
                        vec[0] = static_cast<Type>(vec[0]/d);
                        vec[1] = static_cast<Type>(vec[1]/d);
                        return *this;
                }
template<typename Type>
Type EMAN::Vec2< Type >::operator[] ( int  i) const [inline]

Get the ith item of the vector.

Used in the right side of the assignment.

Parameters:
iThe index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 862 of file vec3.h.

References EMAN::Vec2< Type >::vec.

{ return vec[i]; }
template<typename Type>
Type& EMAN::Vec2< Type >::operator[] ( int  i) [inline]

Get the ith item of the vector.

Used in the left side of the assignment.

Parameters:
iThe index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 871 of file vec3.h.

References EMAN::Vec2< Type >::vec.

{ return vec[i]; }
template<typename Type>
template<typename Type2 >
void EMAN::Vec2< Type >::set_value ( const vector< Type2 > &  v) [inline]

Set new values using a std::vector object.

Parameters:
vA std::vector object used to set 'this' vector's value. It should have at least 3 items.

Definition at line 829 of file vec3.h.

References EMAN::Vec2< Type >::vec.

Referenced by EMAN::Vec2< Type >::normalize().

                {
                        vec[0] =  static_cast<Type>(v[0]);
                        vec[1] =  static_cast<Type>(v[1]);;
                }
template<typename Type>
void EMAN::Vec2< Type >::set_value ( const Type &  x,
const Type &  y 
) [inline]

Set new values to this vector object.

Parameters:
xValue of the first item.
yValue of the second item.

Definition at line 849 of file vec3.h.

References EMAN::Vec2< Type >::vec, x, and y.

                {
                        vec[0] =  x;
                        vec[1] =  y;
                }
template<typename Type>
template<typename Type2 >
void EMAN::Vec2< Type >::set_value_at ( int  index,
const Type2 &  value 
) [inline]

Set values at a particular index.

Parameters:
indexThe index to be set
valueThe value to be set

Definition at line 840 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        vec[index] = static_cast<Type>(value);
                }
template<typename Type>
Type EMAN::Vec2< Type >::squared_length ( ) const [inline]

Calculate its squared length.

no sqrt called

Returns:
The vector's length squared.

Definition at line 797 of file vec3.h.

References EMAN::Vec2< Type >::vec.

                {
                        return  vec[0] * vec[0] + vec[1] * vec[1] ;
                }

Member Data Documentation

template<typename Type>
Type EMAN::Vec2< Type >::vec[2] [private]

The documentation for this class was generated from the following file: