fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <chrono>
  6.  
  7.  
  8. class Transform {
  9. public:
  10. Transform() :
  11. transform ({{1, 0, 0, 0},
  12. {0, 1, 0, 0},
  13. {0, 0, 1, 0},
  14. {0, 0, 0, 1}})
  15. {}
  16.  
  17. void setTransform(double x, double y, double z, double roll, double pitch, double yaw) {
  18. double r11, r12, r13;
  19. double r21, r22, r23;
  20. double r31, r32, r33;
  21.  
  22. r11 = cos(pitch)*cos(yaw)-cos(roll)*sin(yaw)*sin(pitch);
  23. r12 = cos(pitch)*sin(yaw)+cos(roll)*cos(yaw)*sin(pitch);
  24. r13 = sin(pitch)*sin(roll);
  25. r21 = -sin(pitch)*cos(yaw)-cos(roll)*sin(yaw)*cos(pitch);
  26. r22 = -sin(pitch)*sin(yaw)+cos(roll)*cos(yaw)*cos(pitch);
  27. r23 = cos(pitch)*sin(roll);
  28. r31 = sin(roll)*sin(yaw);
  29. r32 = -sin(roll)*cos(yaw);
  30. r33 = cos(roll);
  31.  
  32. transform[0][0] = r11;
  33. transform[0][1] = r12;
  34. transform[0][2] = r13;
  35. transform[1][0] = r21;
  36. transform[1][1] = r22;
  37. transform[1][2] = r23;
  38. transform[2][0] = r31;
  39. transform[2][1] = r32;
  40. transform[2][2] = r33;
  41.  
  42. transform[0][3] = x;
  43. transform[1][3] = y;
  44. transform[2][3] = z;
  45. }
  46.  
  47. Transform& operator=(const Transform &rhs) {
  48. for (int x = 0; x < 4; x++) { // row number of output
  49. for (int y = 0; y < 4; y++) { // column number of output
  50. this->transform[x][y] = rhs.transform[x][y];
  51. }
  52. }
  53. return *this;
  54. }
  55.  
  56.  
  57. Transform operator*(const Transform &rhs) {
  58.  
  59. Transform out;
  60.  
  61. for (int x = 0; x < 4; x++) { // row number of output
  62. for (int y = 0; y < 4; y++) { // column number of output
  63. out.transform[x][y] = 0;
  64. for (int z = 0; z < 4; z++) { // four elements are added for this output
  65. out.transform[x][y] += this->transform[x][z] * rhs.transform[z][y];
  66. }
  67. }
  68. }
  69. return out;
  70.  
  71. }
  72.  
  73. private:
  74. std::vector<std::vector<double>> transform;
  75. };
  76.  
  77.  
  78. inline double genRand(double low, double high) {
  79. return rand()%(int)high+low+high-(int)high;
  80. }
  81.  
  82.  
  83. int main() {
  84.  
  85. // generate 4 random transforms
  86. std::cout << "Creating the transforms" << std::endl;
  87. std::vector<Transform> transforms;
  88. transforms.resize(4);
  89.  
  90.  
  91. unsigned int seed = std::chrono::system_clock::now().time_since_epoch() / std::chrono::seconds(1);
  92. std::cout << "seeding rand: " << seed << std::endl;
  93.  
  94. srand(seed);
  95.  
  96.  
  97. std::cout << "Setting Transform" << std::endl;
  98. for (int i = 0; i < 4; ++i) {
  99. transforms[i].setTransform(genRand(0, 10), genRand(0, 10), genRand(0, 10), genRand(0, 4.14), genRand(0, 3.14), genRand(0, 3.14));
  100. }
  101.  
  102. std::cout << "Starting Clock" << std::endl;
  103. const auto start = std::chrono::system_clock::now();
  104.  
  105.  
  106. for (unsigned long i = 0; i < 1000000; ++i) {
  107.  
  108. //combine the transforms
  109. Transform c;
  110. c = transforms[0] * transforms[1] * transforms[2] * transforms[3];
  111. }
  112.  
  113. auto end = std::chrono::system_clock::now();
  114. auto elapsed = end - start;
  115. std::cout << "elapsed Time: " << elapsed / std::chrono::milliseconds(1) << "ms\n";
  116.  
  117. }
  118.  
  119.  
Success #stdin #stdout 1.41s 5300KB
stdin
Standard input is empty
stdout
Creating the transforms
seeding rand: 1715067140
Setting Transform
Starting Clock
elapsed Time: 1415ms