EMAN::Vec2< Type > Class Template Reference
[unit test in Python]

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>

Collaboration diagram for EMAN::Vec2< Type >:

Collaboration graph
[legend]

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.
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 476 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 481 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 485 of file vec3.h.

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

00485                          : vec[0](0),vec[1](0),vec[2](0)*/ {
00486                         vec[0] = static_cast<Type>(0);
00487                         vec[1] = static_cast<Type>(0);
00488                 }

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:
x Value of the first item.
y Value of the second item. default to 0.

Definition at line 496 of file vec3.h.

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

00497                 {
00498                         vec[0] = static_cast<Type>(x);
00499                         vec[1] = static_cast<Type>(y);
00500                 }

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:
v The std::vector object. It should have at least 3 items.

Definition at line 507 of file vec3.h.

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

00508                 {
00509                         vec[0] = static_cast<Type>(v[0]);
00510                         vec[1] = static_cast<Type>(v[1]);
00511                 }

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

Copy constructor copies vector elements.

Definition at line 516 of file vec3.h.

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

00517                 {
00518                         vec[0] = static_cast<Type>(v[0]);
00519                         vec[1] = static_cast<Type>(v[1]);
00520                 }

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

Destructor.

Definition at line 524 of file vec3.h.

00524 {}


Member Function Documentation

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

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

00532                 {
00533                 // Warning - float precision
00534                         float len = length();
00535                         if (len != 0) {
00536                                 vec[0] = static_cast<Type> (vec[0] / len);
00537                                 vec[1] = static_cast<Type> (vec[1] / len);
00538                         }
00539                         else {
00540                                 set_value(0, 0);
00541                         }
00542                         return len;
00543                 }

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

Calculate its length.

Returns:
The vector's length Warning - float precision

Definition at line 550 of file vec3.h.

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

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

00551                 {
00552                         float t = (float)(vec[0] * vec[0] + vec[1] * vec[1]);
00553                         return (float)sqrt(t);
00554                 }

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

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

00561                 {
00562                         return  vec[0] * vec[0] + vec[1] * vec[1] ;
00563                 }

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:
v The second vector to do the dot product.
Returns:
The dot product.

Definition at line 571 of file vec3.h.

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

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

00572                 {
00573                         return static_cast<Type>((vec[0] * v[0] + vec[1] * v[1]));
00574                 }

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

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

00580                 {
00581                         vector < Type > v(2);
00582                         v[0] = vec[0];
00583                         v[1] = vec[1];
00584                         return v;
00585                 }

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:
v A std::vector object used to set 'this' vector's value. It should have at least 3 items.

Definition at line 592 of file vec3.h.

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

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

00593                 {
00594                         vec[0] =  static_cast<Type>(v[0]);
00595                         vec[1] =  static_cast<Type>(v[1]);;
00596                 }

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:
index The index to be set
value The value to be set

Definition at line 603 of file vec3.h.

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

00604                 {
00605                         vec[index] = static_cast<Type>(value);
00606                 }

template<typename Type>
void EMAN::Vec2< Type >::set_value ( const Type &  x,
const Type &  y 
) [inline]

Set new values to this vector object.

Parameters:
x Value of the first item.
y Value of the second item.

Definition at line 612 of file vec3.h.

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

00613                 {
00614                         vec[0] =  x;
00615                         vec[1] =  y;
00616                 }

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:
i The index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 625 of file vec3.h.

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

00625 { 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:
i The index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 634 of file vec3.h.

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

00634 { return vec[i]; }

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:
i The index of the item to get. Its validality is not checked.
Returns:
The ith item of the vector.

Definition at line 643 of file vec3.h.

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

00643 { return vec[i]; }

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:
v The vector used to be added to 'this' vector.
Returns:
The new 'this' as a result of add.

Definition at line 651 of file vec3.h.

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

00651                                                               {
00652                         vec[0] = static_cast<Type>(vec[0]+v[0]);
00653                         vec[1] = static_cast<Type>(vec[1]+v[1]);
00654                         return *this;
00655                 }

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:
d The number used to be added to this vector.
Returns:
The new 'this' as a result of add.

Definition at line 662 of file vec3.h.

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

00662                                                         {
00663                         vec[0] = static_cast<Type>(vec[0]+d);
00664                         vec[1] = static_cast<Type>(vec[1]+d);
00665                         return *this;
00666                 }

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:
v The vector used to be substracted from 'this' vector.
Returns:
The new 'this' as a result of substraction.

Definition at line 673 of file vec3.h.

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

00673                                                               {
00674                         vec[0] = static_cast<Type>(vec[0]-v[0]);
00675                         vec[1] = static_cast<Type>(vec[1]-v[1]);
00676                         return *this;
00677                 }

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:
d The number used to be substracted from 'this' vector.
Returns:
The new 'this' as a result of substraction.

Definition at line 684 of file vec3.h.

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

00684                                                         {
00685                         vec[0] = static_cast<Type>(vec[0]-d);
00686                         vec[1] = static_cast<Type>(vec[1]-d);
00687                         return *this;
00688                 }

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:
d The number to multiply.
Returns:
The new 'this' as a result of multiplication.

Definition at line 695 of file vec3.h.

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

00695                                                         {
00696                         vec[0] = static_cast<Type>(vec[0]*d);
00697                         vec[1] = static_cast<Type>(vec[1]*d);
00698                         return *this;
00699                 }

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:
d The number to divide.
Returns:
The new 'this' as a result of division.

Definition at line 706 of file vec3.h.

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

00706                                                         {
00707                         vec[0] = static_cast<Type>(vec[0]/d);
00708                         vec[1] = static_cast<Type>(vec[1]/d);
00709                         return *this;
00710                 }


Member Data Documentation

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


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

Generated on Sat Nov 21 02:20:50 2009 for EMAN2 by  doxygen 1.5.6