HALCON/C provides the data structure Hvector for the use of the vector functionality of the HDevelop language. A HALCON vector is a container that can hold an arbitrary number of elements of the identical data type (i.e., tuples, iconic objects, or vectors) 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.
When creating such a vector in HALCON/C you have to differ between vectors of iconic objects and vectors of tuples.
Hvector vectorObj, vectorTup; // Create a one-dimensional vector of iconic objects V_create_object_vector(1,&vectorObj); // Create a one-dimensional vector of tuples V_create_tuple_vector(1,&vectorTup);
These calls create empty vectors of one dimension, e.g., a vector of tuples. It is also possible to create multi-dimensional vectors, i.e., a vector of vectors of tuples or a vector of vectors of iconic objects and so on, by specifying the number of dimensions in the call.
V_create_object_vector(2,&vectorObjMulti);
Note that the vector type and its dimension cannot be changed after the creation of the vector.
Accessing and Setting Vector Elements
To access an element in a vector of tuples you may use V_get_vector_tuple. The following code line queries a tuple contained in vectorTup at position index and returns a copy of the result as a HTuple in tuple.
V_get_vector_tuple(vectorTup,index,tuple);
For accessing elements in vectors of iconic objects V_get_vector_obj can be used instead.
V_get_vector_obj(image,vectorTup,index);
The element to be accessed is specified by its index in form of a tuple. It may either contain a single index of an element in one-dimensional vectors or several indices of the corresponding subvector(s) and its subelement(s) in multi-dimensional vectors.
You may also query a whole subvector of a multi-dimensional vector using V_get_vector_elem.
Before accessing the contents of a vector you may set some vector elements first. Again a distinction is made between vectors of tuples, vectors of iconic objects, and multi-dimensional vectors. The following lines show how to set vector elements of one-dimensional vectors.
// Set an element of a vector of iconic objects V_set_vector_obj(image,vectorObj,index); // Set an element of a vector of tuples V_set_vector_tuple(vectorTup,index,tuple);
The element to be set is again addressed by the specified index, which is represented as a Htuple. Beside the index, the vector itself and the respective Hobject or Htuple to be set (e.g., image or tuple) must be added to the call.
For multi-dimensional vectors the specified index tuple must contain the indices of the subvector(s) and its subelement. The following code lines show the whole process from creating a two-dimensional vector, the image to be set, and the index tuple up to specifying the indices and finally setting the element of the vector.
// Initialize and set the image and indices Hobject img; Htuple indices; Hvector vectorObjMulti; V_create_object_vector(2,&vectorObjMulti); read_image(&img,"Image"); set_i(indices,0,0); set_i(indices,1,1); // Set a subvector of a multi-dimensional vector V_set_vector_obj(img,vectorObjMulti,indices);
It is also possible to set a subvector of multi-dimensional vectors using V_set_vector_elem.
Note that it is also possible to set non-existing vector elements. If necessary the vector is automatically enlarged and initialized with empty elements.
If a Hvector is not needed for further processing its contents and allocated memory must be freed with V_destroy_vector.
V_destroy_vector(vectorTup);
In addition to the previously mentioned basic information the data structure of Hvector provides some more functionality, e.g., inserting or removing vector elements, or concatenation of vectors. Please refer to the corresponding header file Hvector.h in %HALCONROOT%\include\halconc for further information.