About vector class

In order to use the vector class, you include the header file

depending on the dimension:N ( N = 2, 3, 4 ).

Header: #include <kvs/VectorN>

 

And, you declare a variable depending on the type:T ( T = i, ui, f, d).

Variable x: kvs::VectorNT  x;

 

Here, the abbreviation of type name defines as follows:

  i int
  ui unsigned int
  f float
  d double

Moreover, you can use following abbreviation :

  VecN float
  VecNi int
  VecNui unsigned int

 

The abbreviation type:

kvs::Vector3f a;

kvs::Vec3 a;

is same as follows:

kvs::Vector3<float> a;

Member functions

Constructor

There are four-type of constructor.

Following example explains the constructor case of "kvs::Vector3i"
( Notie that the initial value sets to zero in case of the default constructor )

// Default constructor

kvs::Vector3i a;

 

// assign all component

kvs::Vector3i b( 1, 2, 0 );

 

// 2-D vector (kvs::Vector2i) and 3rd component

lvs::Vector2i t( 1, 1);        // 2-D vector

kvs::Vector3i c( t, 3 );

 

// 3-D array

int element[3] = {1, 2, 3 };

kvs::Vector3i d( element );

Value substitution: Operator [ ],  zero( ), set( )

Operator [] : same as arrays

zero(): all components of vector sets to ZERO

set( kvs::Vector2T, T x), set( T x[3] ):

   components of vector set to the arguments of a function

// Operator []

a[0] = 1;        a[1] = 10;        a[2] =5;

 

// Zero clear

a.zero( );

 

// method set(): all components

a.set( 1, 2, 2 );

 

// method set(): 2-D vector + 3rd component

lvs::Vector2i t( 1, 1);    

a.set( t, 5 );

 

// method set(): 3-D array

int element[3] = { 1, 2, 3 };

a.set( element );

Static member function to clear all component: Zero( ), All( )

Zero(): all components of a vector sets to ZERO

All( Type x ): all components of a vector sets to x

//Zero Clear

a = kvs::Vector3f::Zero( );

 

//All components set to 3.0

a = kvs::Vector3f::All( 3.0 );

Getting value: Operator [ ],  x( ),  y( ),  z( )

Operator []: same as arrays

x(), y(), z() : getting the components x, y, z

// Operator []

int x0, y0, z0;

x0 = a[0];        y0 = a[1];        z0 = a[2];

 

// Component x, y, z

std::cout << a.x( ) << ",  " << a.y( ) << ",  " << a.z( ) << std::endl;

Here, a[0] is equal to a.x().

 

Standard output: print( ), output operator<<

a.print(  );

std::cout << a;

Above example is same output:

1 2 3

 

Normalization: normalize( ), normalized( ) 

normalize( ): superscription (normalize own)

normalized( ): normalized vector returns (the own value does not change)

// Superscription

kvs::Vector3d x( 1.0, 2.0, 3.0 );

x.normalize(  );

 

// Return the normalized vector

kvs::Vector3d y( 1.0, 2.0, 3.0 );

kvs::Vector3d z = y.normalized( );

 

Length: length( ), length2( )

length(): length of vector

length2(): square of the length

Here, length() and length2() function have a return type of double.

// Length of vector

double v0 = y.length(  );

 

// Square of the length

double w0 = y.length2(  );

Four arithmetic operations: +,  -,  *,  /

Vector Addition and Subtraction are defined as

Operator + (+=) and Operator - ( -= ), respectively.

 

a.set( 1, 1, 1 );

b.set( 3, 4, 5 );

c = a + b;

a+=c;

a -= b;

std::cout << -a << std::endl;

Operator * ( *= ) multiply by scalar value or vector. In the case of multipling by vector, the product of each component is calculated.

// Multiply by vector

a*=b;

// Multiply by scalar value

int i0 = 10;

c = i0*b;

c = b*i0;  

c *= i0;  

Operator / (/= ) divide by scalar value or vector,

d = c/i0;

c /= i0;

Inner product: dot( ),   Cross product: cross( ),   Swap: swap( )

For example:

// Inner product

int dot = a.dot( b );

// Cross product

c = a.cross( b );

// Swap

x.swap( y );

Relational operator: ==, !=

Relational operator compares each component.

For exampe:

// Relational operator

if( a == b ) {

    std::cout << "a == b" << std::endl;

}

else if( a != b ) {

    std::cout << "a != b" << std::endl; }

 

Sample program: VectorTest.tgz

 

Compile and execute

Let's try to compile and execute above program.

When compiling a KVS program, it is easy to use the automatic Makefile-generation tool kvsmake supplied by KVS.

Here, the option “-G” defines the name of executable file same as the name of current directory.

# kvsmake -G       <-- Generate Makefile

# kvsmake           <-- Compile

And then,

# ./VectorTest

 

 

 

Last Updated at Nov. 1, 2015 

 

TOP