/test/mafResourcesTest/mafMatrixTest.cpp

https://github.com/chrisidefix/MAF · C++ · 185 lines · 113 code · 34 blank · 38 comment · 8 complexity · eb3f337a07902d6f81884c31780cc3de MD5 · raw file

  1. /*
  2. * mafMatrixTest.cpp
  3. * mafMatrixsTest
  4. *
  5. * Created by Daniele Giunchi on 15/06/11.
  6. * Copyright 2011 SCS-B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #include <mafTestSuite.h>
  12. #include <mafResourcesDefinitions.h>
  13. #include <mafMatrix.h>
  14. using namespace mafCore;
  15. using namespace mafEventBus;
  16. using namespace mafResources;
  17. /**
  18. Class name: mafMatrixTest
  19. This class implements the test suite for mafMatrix.
  20. */
  21. //! <title>
  22. //mafMatrix
  23. //! </title>
  24. //! <description>
  25. //mafMatrix is the class that wrap third party library matrix.
  26. //! </description>
  27. class mafMatrixTest: public QObject {
  28. Q_OBJECT
  29. private Q_SLOTS:
  30. /// Initialize test variables
  31. void initTestCase() {
  32. mafMessageHandler::instance()->installMessageHandler();
  33. mafEventBusManager::instance();
  34. m_Matrix = new mafMatrix();
  35. m_Matrix->setIdentity();
  36. }
  37. /// Cleanup test variables memory allocation.
  38. void cleanupTestCase() {
  39. delete m_Matrix;
  40. mafEventBusManager::instance()->shutdown();
  41. mafMessageHandler::instance()->shutdown();
  42. }
  43. /// mafMatrix allocation test case.
  44. void mafMatrixAllocationTest();
  45. /// mafMatrix clone test
  46. void mafMatrixCloneTest();
  47. /// mafMatrix clone test
  48. void mafMatrixElementAccessors();
  49. /// identity test
  50. void mafMatrixIdentityTest();
  51. /// test matrix multiplication and assignment
  52. void mafMatrixMultiplicationAndAssignmentTest();
  53. /// test rawData matrix extraction
  54. void mafMatrixRawDataExtractionTest();
  55. /// Test the row and column extraction.
  56. void mafMatrixRowColumnExtractionTest();
  57. private:
  58. mafMatrix *m_Matrix; ///< Test var.
  59. };
  60. void mafMatrixTest::mafMatrixAllocationTest() {
  61. QVERIFY(m_Matrix != NULL);
  62. }
  63. void mafMatrixTest::mafMatrixCloneTest() {
  64. mafMatrix *clonedMat = m_Matrix->clone();
  65. clonedMat->description();
  66. QVERIFY(clonedMat != NULL);
  67. QVERIFY(m_Matrix->isEqual(*clonedMat));
  68. m_Matrix->description();
  69. delete clonedMat;
  70. }
  71. void mafMatrixTest::mafMatrixElementAccessors() {
  72. double valueToCheck = 5.36;
  73. m_Matrix->setElement(2, 3, valueToCheck);
  74. QVERIFY(valueToCheck == m_Matrix->element(2,3));
  75. }
  76. void mafMatrixTest::mafMatrixIdentityTest() {
  77. m_Matrix->setIdentity();
  78. mafMatrix identity;
  79. identity.setElement(0, 0, 1.0); identity.setElement(0, 1, 0.0); identity.setElement(0, 2, 0.0); identity.setElement(0, 3, 0.0);
  80. identity.setElement(1, 0, 0.0); identity.setElement(1, 1, 1.0); identity.setElement(1, 2, 0.0); identity.setElement(1, 3, 0.0);
  81. identity.setElement(2, 0, 0.0); identity.setElement(2, 1, 0.0); identity.setElement(2, 2, 1.0); identity.setElement(2, 3, 0.0);
  82. identity.setElement(3, 0, 0.0); identity.setElement(3, 1, 0.0); identity.setElement(3, 2, 0.0); identity.setElement(3, 3, 1.0);
  83. QVERIFY(m_Matrix->isEqual(identity));
  84. }
  85. void mafMatrixTest::mafMatrixMultiplicationAndAssignmentTest() {
  86. mafMatrix identity;
  87. identity.setIdentity();
  88. mafMatrix first;
  89. first.setElement(0, 0, 2.0); first.setElement(0, 1, 2.0); first.setElement(0, 2, 0.0); first.setElement(0, 3, 2.0);
  90. first.setElement(1, 0, 2.0); first.setElement(1, 1, 2.0); first.setElement(1, 2, 2.0); first.setElement(1, 3, 2.0);
  91. first.setElement(2, 0, 0.0); first.setElement(2, 1, 2.0); first.setElement(2, 2, 2.0); first.setElement(2, 3, 2.0);
  92. first.setElement(3, 0, 2.0); first.setElement(3, 1, 0.0); first.setElement(3, 2, 2.0); first.setElement(3, 3, 2.0);
  93. mafMatrix second;
  94. second.setElement(0, 0, 0.0); second.setElement(0, 1, 0.5); second.setElement(0, 2, -0.5);second.setElement(0, 3, 0.0);
  95. second.setElement(1, 0, 0.0); second.setElement(1, 1, 0.5); second.setElement(1, 2, 0.0); second.setElement(1, 3, -0.5);
  96. second.setElement(2, 0, -0.5);second.setElement(2, 1, 0.5); second.setElement(2, 2, 0.0); second.setElement(2, 3, 0.0);
  97. second.setElement(3, 0, 0.5); second.setElement(3, 1, -1.0);second.setElement(3, 2, 0.5); second.setElement(3, 3, 0.5);
  98. mafMatrix result;
  99. result = first * second;
  100. QVERIFY(result.isEqual(identity));
  101. }
  102. void mafMatrixTest::mafMatrixRawDataExtractionTest() {
  103. // | 2.0 2.0 0.0 2.0 |
  104. // | 3.0 2.0 2.0 2.0 |
  105. // | 0.0 2.0 2.0 5.0 |
  106. // | 1.0 0.0 2.0 2.0 |
  107. mafMatrix matrtix;
  108. matrtix.setElement(0, 0, 2.0); matrtix.setElement(0, 1, 2.0); matrtix.setElement(0, 2, 0.0); matrtix.setElement(0, 3, 2.0);
  109. matrtix.setElement(1, 0, 3.0); matrtix.setElement(1, 1, 2.0); matrtix.setElement(1, 2, 2.0); matrtix.setElement(1, 3, 2.0);
  110. matrtix.setElement(2, 0, 0.0); matrtix.setElement(2, 1, 2.0); matrtix.setElement(2, 2, 2.0); matrtix.setElement(2, 3, 5.0);
  111. matrtix.setElement(3, 0, 1.0); matrtix.setElement(3, 1, 0.0); matrtix.setElement(3, 2, 2.0); matrtix.setElement(3, 3, 2.0);
  112. double *val = matrtix.rawData();
  113. bool ok(false);
  114. ok = val[0] == 2.0;
  115. QVERIFY(ok);
  116. ok = val[4] == 3.0;
  117. QVERIFY(ok);
  118. ok = val[11] == 5.0;
  119. QVERIFY(ok);
  120. ok = val[12] == 1.0;
  121. QVERIFY(ok);
  122. ok = val[14] == 2.0;
  123. QVERIFY(ok);
  124. }
  125. void mafMatrixTest::mafMatrixRowColumnExtractionTest() {
  126. // | 2.0 2.0 0.0 2.0 |
  127. // | 3.0 2.0 2.0 2.0 |
  128. // | 0.0 2.0 2.0 5.0 |
  129. // | 1.0 0.0 2.0 2.0 |
  130. mafMatrix matrtix;
  131. matrtix.setElement(0, 0, 2.0); matrtix.setElement(0, 1, 2.0); matrtix.setElement(0, 2, 0.0); matrtix.setElement(0, 3, 2.0);
  132. matrtix.setElement(1, 0, 3.0); matrtix.setElement(1, 1, 2.0); matrtix.setElement(1, 2, 2.0); matrtix.setElement(1, 3, 2.0);
  133. matrtix.setElement(2, 0, 0.0); matrtix.setElement(2, 1, 2.0); matrtix.setElement(2, 2, 2.0); matrtix.setElement(2, 3, 5.0);
  134. matrtix.setElement(3, 0, 1.0); matrtix.setElement(3, 1, 0.0); matrtix.setElement(3, 2, 2.0); matrtix.setElement(3, 3, 2.0);
  135. // Extract a row in the range of matrix.
  136. mafMatrix resultRowMatrix(1,4);
  137. resultRowMatrix.setElement(0, 0, 2.0); resultRowMatrix.setElement(0, 1, 2.0); resultRowMatrix.setElement(0, 2, 0.0); resultRowMatrix.setElement(0, 3, 2.0);
  138. mafMatrix mRow = matrtix.extractRow(0);
  139. bool ok = mRow.isEqual(resultRowMatrix);
  140. QVERIFY(ok);
  141. mafMatrix resultColMatrix(4,1);
  142. resultColMatrix.setElement(0, 0, 2.0);
  143. resultColMatrix.setElement(1, 0, 3.0);
  144. resultColMatrix.setElement(2, 0, 0.0);
  145. resultColMatrix.setElement(3, 0, 1.0);
  146. mafMatrix mCol = matrtix.extractColumn(0);
  147. ok = mCol.isEqual(resultColMatrix);
  148. QVERIFY(ok);
  149. }
  150. MAF_REGISTER_TEST(mafMatrixTest);
  151. #include "mafMatrixTest.moc"