HMatrix.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /***********************************************************
  2. * File generated by the HALCON-Compiler hcomp version 20.11
  3. * Usage: Interface to C++
  4. *
  5. * Software by: MVTec Software GmbH, www.mvtec.com
  6. ***********************************************************/
  7. #ifndef HCPP_HMATRIX
  8. #define HCPP_HMATRIX
  9. namespace HalconCpp
  10. {
  11. // Represents an instance of a matrix.
  12. class LIntExport HMatrix : public HHandle
  13. {
  14. public:
  15. // Create an uninitialized instance
  16. HMatrix():HHandle() {}
  17. // Copy constructor
  18. HMatrix(const HMatrix& source) : HHandle(source) {}
  19. // Copy constructor
  20. HMatrix(const HHandle& handle);
  21. // Create HMatrix from handle, taking ownership
  22. explicit HMatrix(Hlong handle);
  23. bool operator==(const HHandle& obj) const
  24. {
  25. return HHandleBase::operator==(obj);
  26. }
  27. bool operator!=(const HHandle& obj) const
  28. {
  29. return HHandleBase::operator!=(obj);
  30. }
  31. protected:
  32. // Verify matching semantic type ('matrix')!
  33. virtual void AssertType(Hphandle handle) const;
  34. public:
  35. // Deep copy of all data represented by this object instance
  36. HMatrix Clone() const;
  37. /*****************************************************************************
  38. * Operator-based class constructors
  39. *****************************************************************************/
  40. // read_matrix: Read a matrix from a file.
  41. explicit HMatrix(const HString& FileName);
  42. // read_matrix: Read a matrix from a file.
  43. explicit HMatrix(const char* FileName);
  44. #ifdef _WIN32
  45. // read_matrix: Read a matrix from a file.
  46. explicit HMatrix(const wchar_t* FileName);
  47. #endif
  48. // create_matrix: Create a matrix.
  49. explicit HMatrix(Hlong Rows, Hlong Columns, const HTuple& Value);
  50. // create_matrix: Create a matrix.
  51. explicit HMatrix(Hlong Rows, Hlong Columns, double Value);
  52. /*****************************************************************************
  53. * Operator overloads (non-member overloads reside in HOperatorOverloads.h)
  54. *****************************************************************************/
  55. // Negate matrix
  56. HMatrix operator - () const;
  57. // Cast to HTuple
  58. #if defined(HCPP_LEGACY_HANDLE_API)
  59. // Up to HALCON 13.0 it used to return all matrix elements,
  60. // keep this behavior in place for legacy code
  61. operator HTuple() const
  62. {
  63. return GetFullMatrix();
  64. }
  65. #else
  66. // However for consistency with new handle mechanism cast HMatrix
  67. // should keep the case class behavior and wrap the handle instead.
  68. #endif
  69. // Access matrix element
  70. double operator () (Hlong row, Hlong column) const;
  71. /*****************************************************************************
  72. * Non-generic convenience members
  73. *****************************************************************************/
  74. Hlong NumRows() const;
  75. Hlong NumColumns() const;
  76. /***************************************************************************
  77. * Operators *
  78. ***************************************************************************/
  79. // Deserialize a serialized matrix.
  80. void DeserializeMatrix(const HSerializedItem& SerializedItemHandle);
  81. // Serialize a matrix.
  82. HSerializedItem SerializeMatrix() const;
  83. // Read a matrix from a file.
  84. void ReadMatrix(const HString& FileName);
  85. // Read a matrix from a file.
  86. void ReadMatrix(const char* FileName);
  87. #ifdef _WIN32
  88. // Read a matrix from a file.
  89. void ReadMatrix(const wchar_t* FileName);
  90. #endif
  91. // Write a matrix to a file.
  92. void WriteMatrix(const HString& FileFormat, const HString& FileName) const;
  93. // Write a matrix to a file.
  94. void WriteMatrix(const char* FileFormat, const char* FileName) const;
  95. #ifdef _WIN32
  96. // Write a matrix to a file.
  97. void WriteMatrix(const wchar_t* FileFormat, const wchar_t* FileName) const;
  98. #endif
  99. // Perform an orthogonal decomposition of a matrix.
  100. HMatrix OrthogonalDecomposeMatrix(const HString& DecompositionType, const HString& OutputMatricesType, const HString& ComputeOrthogonal, HMatrix* MatrixTriangularID) const;
  101. // Perform an orthogonal decomposition of a matrix.
  102. HMatrix OrthogonalDecomposeMatrix(const char* DecompositionType, const char* OutputMatricesType, const char* ComputeOrthogonal, HMatrix* MatrixTriangularID) const;
  103. #ifdef _WIN32
  104. // Perform an orthogonal decomposition of a matrix.
  105. HMatrix OrthogonalDecomposeMatrix(const wchar_t* DecompositionType, const wchar_t* OutputMatricesType, const wchar_t* ComputeOrthogonal, HMatrix* MatrixTriangularID) const;
  106. #endif
  107. // Decompose a matrix.
  108. HMatrix DecomposeMatrix(const HString& MatrixType, HMatrix* Matrix2ID) const;
  109. // Decompose a matrix.
  110. HMatrix DecomposeMatrix(const char* MatrixType, HMatrix* Matrix2ID) const;
  111. #ifdef _WIN32
  112. // Decompose a matrix.
  113. HMatrix DecomposeMatrix(const wchar_t* MatrixType, HMatrix* Matrix2ID) const;
  114. #endif
  115. // Compute the singular value decomposition of a matrix.
  116. HMatrix SvdMatrix(const HString& SVDType, const HString& ComputeSingularVectors, HMatrix* MatrixSID, HMatrix* MatrixVID) const;
  117. // Compute the singular value decomposition of a matrix.
  118. HMatrix SvdMatrix(const char* SVDType, const char* ComputeSingularVectors, HMatrix* MatrixSID, HMatrix* MatrixVID) const;
  119. #ifdef _WIN32
  120. // Compute the singular value decomposition of a matrix.
  121. HMatrix SvdMatrix(const wchar_t* SVDType, const wchar_t* ComputeSingularVectors, HMatrix* MatrixSID, HMatrix* MatrixVID) const;
  122. #endif
  123. // Compute the generalized eigenvalues and optionally the generalized eigenvectors of general matrices.
  124. void GeneralizedEigenvaluesGeneralMatrix(const HMatrix& MatrixBID, const HString& ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  125. // Compute the generalized eigenvalues and optionally the generalized eigenvectors of general matrices.
  126. void GeneralizedEigenvaluesGeneralMatrix(const HMatrix& MatrixBID, const char* ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  127. #ifdef _WIN32
  128. // Compute the generalized eigenvalues and optionally the generalized eigenvectors of general matrices.
  129. void GeneralizedEigenvaluesGeneralMatrix(const HMatrix& MatrixBID, const wchar_t* ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  130. #endif
  131. // Compute the generalized eigenvalues and optionally generalized eigenvectors of symmetric input matrices.
  132. HMatrix GeneralizedEigenvaluesSymmetricMatrix(const HMatrix& MatrixBID, const HString& ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  133. // Compute the generalized eigenvalues and optionally generalized eigenvectors of symmetric input matrices.
  134. HMatrix GeneralizedEigenvaluesSymmetricMatrix(const HMatrix& MatrixBID, const char* ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  135. #ifdef _WIN32
  136. // Compute the generalized eigenvalues and optionally generalized eigenvectors of symmetric input matrices.
  137. HMatrix GeneralizedEigenvaluesSymmetricMatrix(const HMatrix& MatrixBID, const wchar_t* ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  138. #endif
  139. // Compute the eigenvalues and optionally the eigenvectors of a general matrix.
  140. void EigenvaluesGeneralMatrix(const HString& ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  141. // Compute the eigenvalues and optionally the eigenvectors of a general matrix.
  142. void EigenvaluesGeneralMatrix(const char* ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  143. #ifdef _WIN32
  144. // Compute the eigenvalues and optionally the eigenvectors of a general matrix.
  145. void EigenvaluesGeneralMatrix(const wchar_t* ComputeEigenvectors, HMatrix* EigenvaluesRealID, HMatrix* EigenvaluesImagID, HMatrix* EigenvectorsRealID, HMatrix* EigenvectorsImagID) const;
  146. #endif
  147. // Compute the eigenvalues and optionally eigenvectors of a symmetric matrix.
  148. HMatrix EigenvaluesSymmetricMatrix(const HString& ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  149. // Compute the eigenvalues and optionally eigenvectors of a symmetric matrix.
  150. HMatrix EigenvaluesSymmetricMatrix(const char* ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  151. #ifdef _WIN32
  152. // Compute the eigenvalues and optionally eigenvectors of a symmetric matrix.
  153. HMatrix EigenvaluesSymmetricMatrix(const wchar_t* ComputeEigenvectors, HMatrix* EigenvectorsID) const;
  154. #endif
  155. // Compute the solution of a system of equations.
  156. HMatrix SolveMatrix(const HString& MatrixLHSType, double Epsilon, const HMatrix& MatrixRHSID) const;
  157. // Compute the solution of a system of equations.
  158. HMatrix SolveMatrix(const char* MatrixLHSType, double Epsilon, const HMatrix& MatrixRHSID) const;
  159. #ifdef _WIN32
  160. // Compute the solution of a system of equations.
  161. HMatrix SolveMatrix(const wchar_t* MatrixLHSType, double Epsilon, const HMatrix& MatrixRHSID) const;
  162. #endif
  163. // Compute the determinant of a matrix.
  164. double DeterminantMatrix(const HString& MatrixType) const;
  165. // Compute the determinant of a matrix.
  166. double DeterminantMatrix(const char* MatrixType) const;
  167. #ifdef _WIN32
  168. // Compute the determinant of a matrix.
  169. double DeterminantMatrix(const wchar_t* MatrixType) const;
  170. #endif
  171. // Invert a matrix.
  172. void InvertMatrixMod(const HString& MatrixType, double Epsilon) const;
  173. // Invert a matrix.
  174. void InvertMatrixMod(const char* MatrixType, double Epsilon) const;
  175. #ifdef _WIN32
  176. // Invert a matrix.
  177. void InvertMatrixMod(const wchar_t* MatrixType, double Epsilon) const;
  178. #endif
  179. // Invert a matrix.
  180. HMatrix InvertMatrix(const HString& MatrixType, double Epsilon) const;
  181. // Invert a matrix.
  182. HMatrix InvertMatrix(const char* MatrixType, double Epsilon) const;
  183. #ifdef _WIN32
  184. // Invert a matrix.
  185. HMatrix InvertMatrix(const wchar_t* MatrixType, double Epsilon) const;
  186. #endif
  187. // Transpose a matrix.
  188. void TransposeMatrixMod() const;
  189. // Transpose a matrix.
  190. HMatrix TransposeMatrix() const;
  191. // Returns the elementwise maximum of a matrix.
  192. HMatrix MaxMatrix(const HString& MaxType) const;
  193. // Returns the elementwise maximum of a matrix.
  194. HMatrix MaxMatrix(const char* MaxType) const;
  195. #ifdef _WIN32
  196. // Returns the elementwise maximum of a matrix.
  197. HMatrix MaxMatrix(const wchar_t* MaxType) const;
  198. #endif
  199. // Returns the elementwise minimum of a matrix.
  200. HMatrix MinMatrix(const HString& MinType) const;
  201. // Returns the elementwise minimum of a matrix.
  202. HMatrix MinMatrix(const char* MinType) const;
  203. #ifdef _WIN32
  204. // Returns the elementwise minimum of a matrix.
  205. HMatrix MinMatrix(const wchar_t* MinType) const;
  206. #endif
  207. // Compute the power functions of a matrix.
  208. void PowMatrixMod(const HString& MatrixType, const HTuple& Power) const;
  209. // Compute the power functions of a matrix.
  210. void PowMatrixMod(const HString& MatrixType, double Power) const;
  211. // Compute the power functions of a matrix.
  212. void PowMatrixMod(const char* MatrixType, double Power) const;
  213. #ifdef _WIN32
  214. // Compute the power functions of a matrix.
  215. void PowMatrixMod(const wchar_t* MatrixType, double Power) const;
  216. #endif
  217. // Compute the power functions of a matrix.
  218. HMatrix PowMatrix(const HString& MatrixType, const HTuple& Power) const;
  219. // Compute the power functions of a matrix.
  220. HMatrix PowMatrix(const HString& MatrixType, double Power) const;
  221. // Compute the power functions of a matrix.
  222. HMatrix PowMatrix(const char* MatrixType, double Power) const;
  223. #ifdef _WIN32
  224. // Compute the power functions of a matrix.
  225. HMatrix PowMatrix(const wchar_t* MatrixType, double Power) const;
  226. #endif
  227. // Compute the power functions of the elements of a matrix.
  228. void PowElementMatrixMod(const HMatrix& MatrixExpID) const;
  229. // Compute the power functions of the elements of a matrix.
  230. HMatrix PowElementMatrix(const HMatrix& MatrixExpID) const;
  231. // Compute the power functions of the elements of a matrix.
  232. void PowScalarElementMatrixMod(const HTuple& Power) const;
  233. // Compute the power functions of the elements of a matrix.
  234. void PowScalarElementMatrixMod(double Power) const;
  235. // Compute the power functions of the elements of a matrix.
  236. HMatrix PowScalarElementMatrix(const HTuple& Power) const;
  237. // Compute the power functions of the elements of a matrix.
  238. HMatrix PowScalarElementMatrix(double Power) const;
  239. // Compute the square root values of the elements of a matrix.
  240. void SqrtMatrixMod() const;
  241. // Compute the square root values of the elements of a matrix.
  242. HMatrix SqrtMatrix() const;
  243. // Compute the absolute values of the elements of a matrix.
  244. void AbsMatrixMod() const;
  245. // Compute the absolute values of the elements of a matrix.
  246. HMatrix AbsMatrix() const;
  247. // Norm of a matrix.
  248. double NormMatrix(const HString& NormType) const;
  249. // Norm of a matrix.
  250. double NormMatrix(const char* NormType) const;
  251. #ifdef _WIN32
  252. // Norm of a matrix.
  253. double NormMatrix(const wchar_t* NormType) const;
  254. #endif
  255. // Returns the elementwise mean of a matrix.
  256. HMatrix MeanMatrix(const HString& MeanType) const;
  257. // Returns the elementwise mean of a matrix.
  258. HMatrix MeanMatrix(const char* MeanType) const;
  259. #ifdef _WIN32
  260. // Returns the elementwise mean of a matrix.
  261. HMatrix MeanMatrix(const wchar_t* MeanType) const;
  262. #endif
  263. // Returns the elementwise sum of a matrix.
  264. HMatrix SumMatrix(const HString& SumType) const;
  265. // Returns the elementwise sum of a matrix.
  266. HMatrix SumMatrix(const char* SumType) const;
  267. #ifdef _WIN32
  268. // Returns the elementwise sum of a matrix.
  269. HMatrix SumMatrix(const wchar_t* SumType) const;
  270. #endif
  271. // Divide matrices element-by-element.
  272. void DivElementMatrixMod(const HMatrix& MatrixBID) const;
  273. // Divide matrices element-by-element.
  274. HMatrix DivElementMatrix(const HMatrix& MatrixBID) const;
  275. // Multiply matrices element-by-element.
  276. void MultElementMatrixMod(const HMatrix& MatrixBID) const;
  277. // Multiply matrices element-by-element.
  278. HMatrix MultElementMatrix(const HMatrix& MatrixBID) const;
  279. // Scale a matrix.
  280. void ScaleMatrixMod(const HTuple& Factor) const;
  281. // Scale a matrix.
  282. void ScaleMatrixMod(double Factor) const;
  283. // Scale a matrix.
  284. HMatrix ScaleMatrix(const HTuple& Factor) const;
  285. // Scale a matrix.
  286. HMatrix ScaleMatrix(double Factor) const;
  287. // Subtract two matrices.
  288. void SubMatrixMod(const HMatrix& MatrixBID) const;
  289. // Subtract two matrices.
  290. HMatrix SubMatrix(const HMatrix& MatrixBID) const;
  291. // Add two matrices.
  292. void AddMatrixMod(const HMatrix& MatrixBID) const;
  293. // Add two matrices.
  294. HMatrix AddMatrix(const HMatrix& MatrixBID) const;
  295. // Multiply two matrices.
  296. void MultMatrixMod(const HMatrix& MatrixBID, const HString& MultType) const;
  297. // Multiply two matrices.
  298. void MultMatrixMod(const HMatrix& MatrixBID, const char* MultType) const;
  299. #ifdef _WIN32
  300. // Multiply two matrices.
  301. void MultMatrixMod(const HMatrix& MatrixBID, const wchar_t* MultType) const;
  302. #endif
  303. // Multiply two matrices.
  304. HMatrix MultMatrix(const HMatrix& MatrixBID, const HString& MultType) const;
  305. // Multiply two matrices.
  306. HMatrix MultMatrix(const HMatrix& MatrixBID, const char* MultType) const;
  307. #ifdef _WIN32
  308. // Multiply two matrices.
  309. HMatrix MultMatrix(const HMatrix& MatrixBID, const wchar_t* MultType) const;
  310. #endif
  311. // Get the size of a matrix.
  312. void GetSizeMatrix(Hlong* Rows, Hlong* Columns) const;
  313. // Repeat a matrix.
  314. HMatrix RepeatMatrix(Hlong Rows, Hlong Columns) const;
  315. // Copy a matrix.
  316. HMatrix CopyMatrix() const;
  317. // Set the diagonal elements of a matrix.
  318. void SetDiagonalMatrix(const HMatrix& VectorID, Hlong Diagonal) const;
  319. // Get the diagonal elements of a matrix.
  320. HMatrix GetDiagonalMatrix(Hlong Diagonal) const;
  321. // Set a sub-matrix of a matrix.
  322. void SetSubMatrix(const HMatrix& MatrixSubID, Hlong Row, Hlong Column) const;
  323. // Get a sub-matrix of a matrix.
  324. HMatrix GetSubMatrix(Hlong Row, Hlong Column, Hlong RowsSub, Hlong ColumnsSub) const;
  325. // Set all values of a matrix.
  326. void SetFullMatrix(const HTuple& Values) const;
  327. // Set all values of a matrix.
  328. void SetFullMatrix(double Values) const;
  329. // Return all values of a matrix.
  330. HTuple GetFullMatrix() const;
  331. // Set one or more elements of a matrix.
  332. void SetValueMatrix(const HTuple& Row, const HTuple& Column, const HTuple& Value) const;
  333. // Set one or more elements of a matrix.
  334. void SetValueMatrix(Hlong Row, Hlong Column, double Value) const;
  335. // Return one ore more elements of a matrix.
  336. HTuple GetValueMatrix(const HTuple& Row, const HTuple& Column) const;
  337. // Return one ore more elements of a matrix.
  338. double GetValueMatrix(Hlong Row, Hlong Column) const;
  339. // Free the memory of a matrix.
  340. static void ClearMatrix(const HMatrixArray& MatrixID);
  341. // Free the memory of a matrix.
  342. void ClearMatrix() const;
  343. // Create a matrix.
  344. void CreateMatrix(Hlong Rows, Hlong Columns, const HTuple& Value);
  345. // Create a matrix.
  346. void CreateMatrix(Hlong Rows, Hlong Columns, double Value);
  347. };
  348. // forward declarations and types for internal array implementation
  349. template<class T> class HSmartPtr;
  350. template<class T> class HHandleBaseArrayRef;
  351. typedef HHandleBaseArrayRef<HMatrix> HMatrixArrayRef;
  352. typedef HSmartPtr< HMatrixArrayRef > HMatrixArrayPtr;
  353. // Represents multiple tool instances
  354. class LIntExport HMatrixArray : public HHandleBaseArray
  355. {
  356. public:
  357. // Create empty array
  358. HMatrixArray();
  359. // Create array from native array of tool instances
  360. HMatrixArray(HMatrix* classes, Hlong length);
  361. // Copy constructor
  362. HMatrixArray(const HMatrixArray &tool_array);
  363. // Destructor
  364. virtual ~HMatrixArray();
  365. // Assignment operator
  366. HMatrixArray &operator=(const HMatrixArray &tool_array);
  367. // Clears array and all tool instances
  368. virtual void Clear();
  369. // Get array of native tool instances
  370. const HMatrix* Tools() const;
  371. // Get number of tools
  372. virtual Hlong Length() const;
  373. // Create tool array from tuple of handles
  374. virtual void SetFromTuple(const HTuple& handles);
  375. // Get tuple of handles for tool array
  376. virtual HTuple ConvertToTuple() const;
  377. protected:
  378. // Smart pointer to internal data container
  379. HMatrixArrayPtr *mArrayPtr;
  380. };
  381. }
  382. #endif