EMAN2
Public Types | Public Member Functions | Private Attributes | List of all members
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>

Public Types

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

Public Member Functions

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

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 709 of file vec3.h.

Member Typedef Documentation

◆ type

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 714 of file vec3.h.

Constructor & Destructor Documentation

◆ Vec2() [1/4]

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

contruct a Vec2 object with all elements equal to 0.

Definition at line 718 of file vec3.h.

718 {
719 vec[0] = static_cast<Type>(0);
720 vec[1] = static_cast<Type>(0);
721 }
Type vec[2]
Definition: vec3.h:977

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

◆ Vec2() [2/4]

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 729 of file vec3.h.

730 {
731 vec[0] = static_cast<Type>(x);
732 vec[1] = static_cast<Type>(y);
733 }
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517

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

◆ Vec2() [3/4]

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 740 of file vec3.h.

741 {
742 vec[0] = static_cast<Type>(v[0]);
743 vec[1] = static_cast<Type>(v[1]);
744 }

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

◆ Vec2() [4/4]

template<typename Type >
template<typename Type2 >
EMAN::Vec2< Type >::Vec2 ( const Vec2< Type2 > &  v)
inline

Copy constructor copies vector elements.

Definition at line 749 of file vec3.h.

750 {
751 vec[0] = static_cast<Type>(v[0]);
752 vec[1] = static_cast<Type>(v[1]);
753 }

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

◆ ~Vec2()

template<typename Type >
EMAN::Vec2< Type >::~Vec2 ( )
inline

Destructor.

Definition at line 757 of file vec3.h.

757{}

Member Function Documentation

◆ as_list()

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 812 of file vec3.h.

813 {
814 vector < Type > v(2);
815 v[0] = vec[0];
816 v[1] = vec[1];
817 return v;
818 }

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

◆ at()

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 876 of file vec3.h.

876{ return vec[i]; }

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

◆ begin()

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 893 of file vec3.h.

894 {
895 return &vec[0];
896 }

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

◆ dot()

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 804 of file vec3.h.

805 {
806 return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1]));
807 }

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

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

◆ end()

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 903 of file vec3.h.

904 {
905 return &vec[2];
906 }

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

◆ length()

template<typename Type >
float EMAN::Vec2< Type >::length ( ) const
inline

Calculate its length.

Returns
The vector's length Warning - float precision

Definition at line 783 of file vec3.h.

784 {
785 float t = (float)(vec[0] * vec[0] + vec[1] * vec[1]);
786 return (float)sqrt(t);
787 }
EMData * sqrt() const
return square root of current image

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

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

◆ normalize()

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 764 of file vec3.h.

765 {
766 // Warning - float precision
767 float len = length();
768 if (len != 0) {
769 vec[0] = static_cast<Type> (vec[0] / len);
770 vec[1] = static_cast<Type> (vec[1] / len);
771 }
772 else {
773 set_value(0, 0);
774 }
775 return len;
776 }
float length() const
Calculate its length.
Definition: vec3.h:783
void set_value(const vector< Type2 > &v)
Set new values using a std::vector object.
Definition: vec3.h:825

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

◆ number_of_element()

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 883 of file vec3.h.

884 {
885 return 2;
886 }

◆ operator*=()

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 958 of file vec3.h.

958 {
959 vec[0] = static_cast<Type>(vec[0]*d);
960 vec[1] = static_cast<Type>(vec[1]*d);
961 return *this;
962 }

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

◆ operator+=() [1/2]

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 925 of file vec3.h.

925 {
926 vec[0] = static_cast<Type>(vec[0]+d);
927 vec[1] = static_cast<Type>(vec[1]+d);
928 return *this;
929 }

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

◆ operator+=() [2/2]

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 914 of file vec3.h.

914 {
915 vec[0] = static_cast<Type>(vec[0]+v[0]);
916 vec[1] = static_cast<Type>(vec[1]+v[1]);
917 return *this;
918 }

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

◆ operator-=() [1/2]

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 947 of file vec3.h.

947 {
948 vec[0] = static_cast<Type>(vec[0]-d);
949 vec[1] = static_cast<Type>(vec[1]-d);
950 return *this;
951 }

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

◆ operator-=() [2/2]

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 936 of file vec3.h.

936 {
937 vec[0] = static_cast<Type>(vec[0]-v[0]);
938 vec[1] = static_cast<Type>(vec[1]-v[1]);
939 return *this;
940 }

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

◆ operator/=()

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 969 of file vec3.h.

969 {
970 vec[0] = static_cast<Type>(vec[0]/d);
971 vec[1] = static_cast<Type>(vec[1]/d);
972 return *this;
973 }

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

◆ operator[]() [1/2]

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 867 of file vec3.h.

867{ return vec[i]; }

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

◆ operator[]() [2/2]

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 858 of file vec3.h.

858{ return vec[i]; }

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

◆ set_value() [1/2]

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 845 of file vec3.h.

846 {
847 vec[0] = x;
848 vec[1] = y;
849 }

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

◆ set_value() [2/2]

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 825 of file vec3.h.

826 {
827 vec[0] = static_cast<Type>(v[0]);
828 vec[1] = static_cast<Type>(v[1]);;
829 }

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

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

◆ set_value_at()

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 836 of file vec3.h.

837 {
838 vec[index] = static_cast<Type>(value);
839 }

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

◆ squared_length()

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 793 of file vec3.h.

794 {
795 return vec[0] * vec[0] + vec[1] * vec[1] ;
796 }

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

Member Data Documentation

◆ vec

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

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