6.3   Vectors

HALCON/C++ provides the class HVector for the use of HALCON vectors in C++ programs. A HALCON vector is a container that can hold an arbitrary number of elements of the identical data type (i.e., tuple, iconic object, or vector) and dimension. The type of a vector, i.e., its dimension and the type of its elements is defined when initializing the vector instance and cannot be changed during its lifetime. A vector with one dimension may be a vector of tuples or a vector of iconic objects. A two-dimensional vector may be a vector of vectors of tuples or a vector of vectors of iconic objects, and so on.

Two classes are derived from the root class HVector:

In the following some basic information on how to use vectors in HALCON/C++ is given, e.g., how to construct vectors and how to access and set vector elements. For a complete list of the available functionality please refer to the corresponding header file HVector.h in %HALCONROOT%\include\halconcpp.

Construction of Vectors

As already mentioned above, a distinction is made between vectors of iconic objects (HObjectVector) and vectors of tuples (HTupleVector). The type of a vector must be defined at its construction as in the following lines:

   // Create a one-dimensional vector of iconic objects
   HObjectVector    vectorObj(1);  

   // Create a one-dimensional vector of tuples
   HTupleVector     vectorTup(1); 

Note that the type of the vector cannot be changed within a program after its construction. Thus, a tuple cannot be assigned to a vector of iconic objects and vice versa. To create a two-dimensional vector, i.e., a vector of vectors of iconic objects or a vector of vector of tuples you may use the following line:

   // Create a two-dimensional vector of iconic objects
   HObjectVector    vectorObjMulti(2);   

   // Create a two-dimensional vector of tuples
   HTupleVector     vectorTupMulti(2);

You can also create a multi-dimensional vector with more than two dimensions by specifying the desired dimension in brackets. However, the dimension of a vector is part of its type and has to remain constant within the program and cannot be changed.

Note that the vectors created by these calls are still empty. How to set the elements of vectors and how to access them is described below.

Accessing and Setting Vector Elements

Like with the construction of vectors, the call for accessing and setting vector elements differs depending on the vector type. A single element of a vector of iconic objects may be accessed by using the method O() whereas the elements of a vector of tuples may be accessed with T().

   // Access a vector element of a one-dimensional HObjectVector
   vectorObj[elem_index].O();  

   // Access a vector element of a one-dimensional HTupleVector
   vectorTup[elem_index].T(); 

The vector element to be accessed is addressed by the specified index in square brackets. If a subelement of a multi-dimensional vector is to be accessed, you have to use the indices of the corresponding subvector and its subelement instead.

   // Access a subelement of a two-dimensional HObjectVector 
   vectorObjMulti[vec_index][elem_index].O();

The left index vec_index defines the index of the subvector and elem_index defines the desired element of the specified subvector.

If a vector element is to be set, the expression for accessing a vector element is used as reference to the HObject or HTuple element to be set. The right side of the assignment specifies the value which is assigned to the vector element.

   // Set a vector element of a one-dimensional HObjectVector
   vectorObj[0].O() = HImage("Image");

   // Set a vector element of a one-dimensional HTupleVector
   HTuple tuple; 
   tuple[0] = 1.0;
   tuple[1] = 2.5;
   vectorTup[0].T() = tuple;

In the example code above the Image is copied and set as the first vector element of vectorObj. The tuple is also copied and set as first vector element of vectorTup.

Setting a subelement of a multi-dimensional vector can be done with the same call. However, instead of a single index, multiple indices must be specified for the corresponding vector element and its subelement, which is to be set.

   // Set a subelement in a two-dimensional HObjectVector
   vectorObjMulti[0][1].O() = HImage("Image");   

It is also allowed to write to a non-existing vector element. Then, the vector is automatically filled with empty elements if necessary.

Destruction of a Vector

If a vector is not needed anymore for further processing, its contents can be cleared with the following call, explicitly:

   vectorTup.Clear();

Additional Information

In addition to the described functionalities HObjectVector and HTupleVector provide some more functionality for the use of HALCON vectors in HALCON/CPP, e.g., inserting or removing vector elements, or concatenation of vectors. Please refer to the corresponding header file HVector.h in %HALCONROOT%\include\halconcpp for more information.