HALCON/.NET provides the class HVector for the use of variables of type 'vector' of the HDevelop language, i.e., containers 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 one-dimensional vector 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 vector of iconic objects, and so on. Instances of vectors can be created from the following derived classes:
In the following some basic information about the use of vectors in HALCON/.NET is given when using C#.
As already mentioned, an instance of a vector can only be created from HObjectVector or HTupleVector.
// Create a vector of iconic objects HObjectVector vectorObj; // Create a vector of tuples HTupleVector vectorTup;
The vector type cannot be changed after its construction. Thus, a tuple cannot be assigned to a vector of iconic objects and vice versa.
You may also create a multi-dimensional vector, i.e., a vector of vectors and so on, by specifying the number of dimensions in brackets. However, the dimension of a vector has to remain constant within the program and cannot be changed. The following code line describes how to create a vector of two dimensions, i.e., a vector of vectors of tuples.
HObjectVector vectorObjMulti = new HObjectVector(2);
Note that the vectors created by these calls are still empty. How to access and set vector elements is described below.
Accessing and Setting Vector Elements
When setting or accessing an element of a vector you have to differ between vectors of type HObjectVector and HTupleVector. Elements of vectors of iconic objects can be accessed with .O whereas elements of vectors of tuples can be accessed with .T.
// Access an element of a one-dimensional HObjectVector vectorObj[0].O; // Access an element of a one-dimensional HTupleVector vectorTup[0].T;
The specified index in square brackets defines the element to be accessed. 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[0][1].O;
When setting a vector element the expression for accessing a vector element is needed as reference to the HObject or HTuple, respectively. The value to be set must be specified on the right side of the assignment.
// Set an element of a one-dimensional HObjectVector HImage image = new HImage("Image1"); vectorObj[0].O = image; // Set an element of a HTupleVector HTuple tuple = new HTuple(1,2); vectorTup[1].T = tuple;
In the example code the image is copied and set as the first element of vectorObj. The tuple is also copied but set as the second vector element of vectorTup.
Setting a subelement of a multi-dimensional vector can be done analogously to setting an element in a one-dimensional vector when using the corresponding indices of the subvector(s) and its subelement.
If a non-existing vector element is set or accessed, the vector is automatically enlarged and filled with empty elements if necessary.
If a HObjectVector or HTupleVector is not needed anymore for further processing its contents should be cleared with .Dispose().
vectorObj.Dispose();
In addition to accessing and setting the elements of vectors, HObjectVector and HTupleVector provide further functionalities for the use of HALCON vectors such as inserting and removing vector elements or concatenation of vectors. For more details on the provided vector functionality you may use the automatic context help in Visual Studio, IntelliSense.