HVector.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*****************************************************************************
  2. * HVector.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: Vector class for engine and HDevelop export
  7. *
  8. * (c) 2014-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_VECTOR_H
  16. #define HCPP_VECTOR_H
  17. // clang-format off
  18. #include "halconcpp/HObjectBase.h"
  19. #include "halconcpp/HObject.h"
  20. #include "halconcpp/HTuple.h"
  21. // clang-format on
  22. namespace HalconCpp
  23. {
  24. class Hvector;
  25. class LIntExport HVector
  26. {
  27. protected:
  28. // No public constructors, create HTupleVector or HObjectVector instead
  29. // Create an empty vector
  30. HVector(Hlong dimension);
  31. // Copy constructor
  32. HVector(const HVector& vector);
  33. public:
  34. // Destroy vector
  35. virtual ~HVector();
  36. /* Operator overloads */
  37. // Assignment operator
  38. HVector& operator=(const HVector& vector);
  39. // Comparison operators
  40. bool operator==(const HVector& vector) const;
  41. bool operator!=(const HVector& vector) const;
  42. // Non-const indexing operator. If necessary, the vector
  43. // will be enlarged to accomodate the index
  44. HVector& operator[](Hlong index);
  45. // Const indexing operator. An exception will be raised if the
  46. // index is out of range.
  47. const HVector& operator[](Hlong index) const;
  48. /* Methods */
  49. // Dimension of vector;
  50. Hlong Dimension() const
  51. {
  52. return mDimension;
  53. }
  54. // Length of vector
  55. Hlong Length() const;
  56. // Clear contents of vector
  57. HVector& Clear();
  58. // Remove element from vector
  59. HVector& Remove(Hlong index);
  60. // Insert element into vector
  61. HVector& Insert(Hlong index, const HVector& vector);
  62. // Return a simple string representation of the vector contents,
  63. // mainly for logging and debugging purposes. The result should not
  64. // be parsed and text format may change in the future.
  65. virtual HString ToString() const;
  66. // Assistive functionality for engine and code export
  67. // Raise exception on dimension mismatch
  68. void AssertDimension(Hlong dimension) const;
  69. // Assistive functionality for internal vector use
  70. protected:
  71. // FOR INTERNAL IMPLEMENTATION ONLY. NOT INTENDED
  72. // TO BE USED IN USER-DERIVED CLASSES
  73. // Increase length of vector (with dimension > 0) to ensure index is valid
  74. void AssertSize(Hlong index);
  75. // Test for equality
  76. virtual bool EqualImpl(const HVector& vector) const;
  77. // Create empty element to initialize gaps in vector
  78. virtual void ClearImpl();
  79. // Create concatenation of two vectors on the heap.
  80. HVector* ConcatImpl(const HVector& vector) const;
  81. // Create deep copy of all subvectors and elements
  82. virtual HVector* CloneImpl() const = 0;
  83. // Create empty element to initialize gaps in vector
  84. virtual HVector* GetDefaultElement() const = 0;
  85. // Returns new copy of this instance on the heap
  86. HVector* Clone() const;
  87. // Member variables
  88. protected:
  89. Hlong mDimension;
  90. // Data container
  91. Hvector* mVector;
  92. };
  93. class LIntExport HTupleVector : public HVector
  94. {
  95. public:
  96. // Create empty vector of specified dimension. In case of dimension
  97. // 0 a leaf vector for an empty tuple is created
  98. explicit HTupleVector(Hlong dimension) : HVector(dimension) {}
  99. // Create leaf vector of dimension 0 for the specified tuple
  100. explicit HTupleVector(const HTuple& tuple) : HVector(0), mTuple(tuple) {}
  101. // Create 1-dimensional vector by splitting input tuple into
  102. // blocks of fixed size (except possibly for the last block)
  103. // This corresponds to convert_tuple_to_vector_1d in HDevelop.
  104. explicit HTupleVector(const HTuple& tuple, Hlong block_size);
  105. // Copy constructor
  106. HTupleVector(const HTupleVector& vector);
  107. /* Operator overloads */
  108. // Assignment operator
  109. HTupleVector& operator=(const HTupleVector& vector);
  110. // Comparison operator
  111. bool operator==(const HTupleVector& vector) const;
  112. bool operator!=(const HTupleVector& vector) const;
  113. // Non-const indexing operator. If necessary, the vector
  114. // will be enlarged to accomodate the index
  115. HTupleVector& operator[](Hlong index);
  116. // Const indexing operator. An exception will be raised if the
  117. // index is out of range.
  118. const HTupleVector& operator[](Hlong index) const;
  119. /* Methods */
  120. // Clear contents of vector
  121. HTupleVector& Clear();
  122. // Remove element from vector
  123. HTupleVector& Remove(Hlong index);
  124. // Insert element into vector
  125. HTupleVector& Insert(Hlong index, const HTupleVector& vector);
  126. // Concatenate two vectors (creating new vector)
  127. HTupleVector Concat(const HTupleVector& vector);
  128. /* Value access (for leaf vectors of dimension 0) */
  129. const HTuple& T() const;
  130. HTuple& T();
  131. // Concatenates all tuples stored in the vector
  132. HTuple ConvertVectorToTuple() const;
  133. // Return a simple string representation of the vector contents,
  134. // mainly for logging and debugging purposes. The result should not
  135. // be parsed and text format may change in the future.
  136. virtual HString ToString() const;
  137. // Assistive functionality for internal vector use
  138. protected:
  139. // Test for equality
  140. virtual bool EqualImpl(const HVector& vector) const;
  141. // Create deep copy of all subvectors and elements
  142. virtual HVector* CloneImpl() const;
  143. // Create empty element to initialize gaps in vector
  144. virtual HVector* GetDefaultElement() const;
  145. protected:
  146. HTuple mTuple;
  147. };
  148. class LIntExport HObjectVector : public HVector
  149. {
  150. public:
  151. // Create empty vector of specified dimension. In case of dimension
  152. // 0 a leaf vector for an empty object is created
  153. explicit HObjectVector(Hlong dimension);
  154. // Create leaf vector of dimension 0 for the specified object
  155. explicit HObjectVector(const HObject& obj) : HVector(0), mObject(obj) {}
  156. // Copy constructor
  157. HObjectVector(const HObjectVector& vector);
  158. /* Operator overloads */
  159. // Assignment operator
  160. HObjectVector& operator=(const HObjectVector& vector);
  161. // Comparison operator
  162. bool operator==(const HObjectVector& vector) const;
  163. bool operator!=(const HObjectVector& vector) const;
  164. // Non-const indexing operator. If necessary, the vector
  165. // will be enlarged to accomodate the index
  166. HObjectVector& operator[](Hlong index);
  167. // Const indexing operator. An exception will be raised if the
  168. // index is out of range.
  169. const HObjectVector& operator[](Hlong index) const;
  170. /* Methods */
  171. // Clear contents of vector
  172. HObjectVector& Clear();
  173. // Remove element from vector
  174. HObjectVector& Remove(Hlong index);
  175. // Insert element into vector
  176. HObjectVector& Insert(Hlong index, const HObjectVector& vector);
  177. // Concatenate two vectors (creating new vector)
  178. HObjectVector Concat(const HObjectVector& vector);
  179. /* Value access (for leaf vectors of dimension 0) */
  180. const HObject& O() const;
  181. HObject& O();
  182. // Return a simple string representation of the vector contents,
  183. // mainly for logging and debugging purposes. The result should not
  184. // be parsed and text format may change in the future.
  185. virtual HString ToString() const;
  186. // Assistive functionality for internal vector use
  187. protected:
  188. // Test for equality
  189. virtual bool EqualImpl(const HVector& vector) const;
  190. // Create deep copy of all subvectors and elements
  191. virtual HVector* CloneImpl() const;
  192. // Create empty element to initialize gaps in vector
  193. virtual HVector* GetDefaultElement() const;
  194. protected:
  195. HObject mObject;
  196. };
  197. }
  198. #endif