vector_3.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. vector_3.cpp - Vector library for bed leveling
  3. Copyright (c) 2012 Lars Brubaker. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include <math.h>
  17. #include "Marlin.h"
  18. #ifdef ENABLE_AUTO_BED_LEVELING
  19. #include "vector_3.h"
  20. vector_3::vector_3() : x(0), y(0), z(0) { }
  21. vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
  22. vector_3 vector_3::cross(vector_3 left, vector_3 right)
  23. {
  24. return vector_3(left.y * right.z - left.z * right.y,
  25. left.z * right.x - left.x * right.z,
  26. left.x * right.y - left.y * right.x);
  27. }
  28. vector_3 vector_3::operator+(vector_3 v)
  29. {
  30. return vector_3((x + v.x), (y + v.y), (z + v.z));
  31. }
  32. vector_3 vector_3::operator-(vector_3 v)
  33. {
  34. return vector_3((x - v.x), (y - v.y), (z - v.z));
  35. }
  36. vector_3 vector_3::get_normal()
  37. {
  38. vector_3 normalized = vector_3(x, y, z);
  39. normalized.normalize();
  40. return normalized;
  41. }
  42. float vector_3::get_length()
  43. {
  44. float length = sqrt((x * x) + (y * y) + (z * z));
  45. return length;
  46. }
  47. void vector_3::normalize()
  48. {
  49. float length = get_length();
  50. x /= length;
  51. y /= length;
  52. z /= length;
  53. }
  54. void vector_3::apply_rotation(matrix_3x3 matrix)
  55. {
  56. float resultX = x * matrix.matrix[3*0+0] + y * matrix.matrix[3*1+0] + z * matrix.matrix[3*2+0];
  57. float resultY = x * matrix.matrix[3*0+1] + y * matrix.matrix[3*1+1] + z * matrix.matrix[3*2+1];
  58. float resultZ = x * matrix.matrix[3*0+2] + y * matrix.matrix[3*1+2] + z * matrix.matrix[3*2+2];
  59. x = resultX;
  60. y = resultY;
  61. z = resultZ;
  62. }
  63. void vector_3::debug(char* title)
  64. {
  65. SERIAL_PROTOCOL(title);
  66. SERIAL_PROTOCOLPGM(" x: ");
  67. SERIAL_PROTOCOL(x);
  68. SERIAL_PROTOCOLPGM(" y: ");
  69. SERIAL_PROTOCOL(y);
  70. SERIAL_PROTOCOLPGM(" z: ");
  71. SERIAL_PROTOCOL(z);
  72. SERIAL_PROTOCOLPGM("\n");
  73. }
  74. void apply_rotation_xyz(matrix_3x3 matrix, float &x, float& y, float& z)
  75. {
  76. vector_3 vector = vector_3(x, y, z);
  77. vector.apply_rotation(matrix);
  78. x = vector.x;
  79. y = vector.y;
  80. z = vector.z;
  81. }
  82. matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2)
  83. {
  84. //row_0.debug("row_0");
  85. //row_1.debug("row_1");
  86. //row_2.debug("row_2");
  87. matrix_3x3 new_matrix;
  88. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  89. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  90. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  91. //new_matrix.debug("new_matrix");
  92. return new_matrix;
  93. }
  94. void matrix_3x3::set_to_identity()
  95. {
  96. matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
  97. matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
  98. matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
  99. }
  100. matrix_3x3 matrix_3x3::create_look_at(vector_3 target)
  101. {
  102. vector_3 z_row = target.get_normal();
  103. vector_3 x_row = vector_3(1, 0, -target.x/target.z).get_normal();
  104. vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
  105. // x_row.debug("x_row");
  106. // y_row.debug("y_row");
  107. // z_row.debug("z_row");
  108. // create the matrix already correctly transposed
  109. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  110. // rot.debug("rot");
  111. return rot;
  112. }
  113. matrix_3x3 matrix_3x3::transpose(matrix_3x3 original)
  114. {
  115. matrix_3x3 new_matrix;
  116. new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
  117. new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
  118. new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
  119. return new_matrix;
  120. }
  121. void matrix_3x3::debug(char* title)
  122. {
  123. SERIAL_PROTOCOL(title);
  124. SERIAL_PROTOCOL("\n");
  125. int count = 0;
  126. for(int i=0; i<3; i++)
  127. {
  128. for(int j=0; j<3; j++)
  129. {
  130. SERIAL_PROTOCOL(matrix[count]);
  131. SERIAL_PROTOCOLPGM(" ");
  132. count++;
  133. }
  134. SERIAL_PROTOCOLPGM("\n");
  135. }
  136. }
  137. #endif // #ifdef ENABLE_AUTO_BED_LEVELING