My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vector_3.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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()
  21. {
  22. this->x = 0;
  23. this->y = 0;
  24. this->z = 0;
  25. }
  26. vector_3::vector_3(float x, float y, float z)
  27. {
  28. this->x = x;
  29. this->y = y;
  30. this->z = z;
  31. }
  32. vector_3 vector_3::cross(vector_3 left, vector_3 right)
  33. {
  34. return vector_3(left.y * right.z - left.z * right.y,
  35. left.z * right.x - left.x * right.z,
  36. left.x * right.y - left.y * right.x);
  37. }
  38. vector_3 vector_3::operator+(vector_3 v)
  39. {
  40. return vector_3((x + v.x), (y + v.y), (z + v.z));
  41. }
  42. vector_3 vector_3::operator-(vector_3 v)
  43. {
  44. return vector_3((x - v.x), (y - v.y), (z - v.z));
  45. }
  46. vector_3 vector_3::get_normal()
  47. {
  48. vector_3 normalized = vector_3(x, y, z);
  49. normalized.normalize();
  50. return normalized;
  51. }
  52. float vector_3::get_length()
  53. {
  54. float length = sqrt((x * x) + (y * y) + (z * z));
  55. return length;
  56. }
  57. void vector_3::normalize()
  58. {
  59. float length = get_length();
  60. x /= length;
  61. y /= length;
  62. z /= length;
  63. }
  64. void vector_3::apply_rotation(matrix_3x3 matrix)
  65. {
  66. float resultX = x * matrix.matrix[3*0+0] + y * matrix.matrix[3*1+0] + z * matrix.matrix[3*2+0];
  67. float resultY = x * matrix.matrix[3*0+1] + y * matrix.matrix[3*1+1] + z * matrix.matrix[3*2+1];
  68. float resultZ = x * matrix.matrix[3*0+2] + y * matrix.matrix[3*1+2] + z * matrix.matrix[3*2+2];
  69. x = resultX;
  70. y = resultY;
  71. z = resultZ;
  72. }
  73. void vector_3::debug(char* title)
  74. {
  75. SERIAL_PROTOCOL(title);
  76. SERIAL_PROTOCOLPGM(" x: ");
  77. SERIAL_PROTOCOL(x);
  78. SERIAL_PROTOCOLPGM(" y: ");
  79. SERIAL_PROTOCOL(y);
  80. SERIAL_PROTOCOLPGM(" z: ");
  81. SERIAL_PROTOCOL(z);
  82. SERIAL_PROTOCOLPGM("\n");
  83. }
  84. void apply_rotation_xyz(matrix_3x3 matrix, float &x, float& y, float& z)
  85. {
  86. vector_3 vector = vector_3(x, y, z);
  87. vector.apply_rotation(matrix);
  88. x = vector.x;
  89. y = vector.y;
  90. z = vector.z;
  91. }
  92. matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2)
  93. {
  94. //row_0.debug("row_0");
  95. //row_1.debug("row_1");
  96. //row_2.debug("row_2");
  97. matrix_3x3 new_matrix;
  98. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  99. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  100. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  101. //new_matrix.debug("new_matrix");
  102. return new_matrix;
  103. }
  104. void matrix_3x3::set_to_identity()
  105. {
  106. matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
  107. matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
  108. matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
  109. }
  110. matrix_3x3 matrix_3x3::create_look_at(vector_3 target, vector_3 up)
  111. {
  112. // There are lots of examples of look at code on the internet that don't do all these noramize and also find the position
  113. // through several dot products. The problem with them is that they have a bit of error in that all the vectors arn't normal and need to be.
  114. vector_3 z_row = vector_3(-target.x, -target.y, -target.z).get_normal();
  115. vector_3 x_row = vector_3::cross(up, z_row).get_normal();
  116. vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
  117. //x_row.debug("x_row");
  118. //y_row.debug("y_row");
  119. //z_row.debug("z_row");
  120. matrix_3x3 rot = matrix_3x3::create_from_rows(vector_3(x_row.x, y_row.x, z_row.x),
  121. vector_3(x_row.y, y_row.y, z_row.y),
  122. vector_3(x_row.z, y_row.z, z_row.z));
  123. //rot.debug("rot");
  124. return rot;
  125. }
  126. matrix_3x3 matrix_3x3::create_inverse(matrix_3x3 original)
  127. {
  128. //original.debug("original");
  129. float* A = original.matrix;
  130. float determinant =
  131. + A[0 * 3 + 0] * (A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2])
  132. - A[0 * 3 + 1] * (A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0])
  133. + A[0 * 3 + 2] * (A[1 * 3 + 0] * A[2 * 3 + 1] - A[1 * 3 + 1] * A[2 * 3 + 0]);
  134. matrix_3x3 inverse;
  135. inverse.matrix[0 * 3 + 0] = +(A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2]) / determinant;
  136. inverse.matrix[0 * 3 + 1] = -(A[0 * 3 + 1] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 1]) / determinant;
  137. inverse.matrix[0 * 3 + 2] = +(A[0 * 3 + 1] * A[1 * 3 + 2] - A[0 * 3 + 2] * A[1 * 3 + 1]) / determinant;
  138. inverse.matrix[1 * 3 + 0] = -(A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0]) / determinant;
  139. inverse.matrix[1 * 3 + 1] = +(A[0 * 3 + 0] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 0]) / determinant;
  140. inverse.matrix[1 * 3 + 2] = -(A[0 * 3 + 0] * A[1 * 3 + 2] - A[1 * 3 + 0] * A[0 * 3 + 2]) / determinant;
  141. inverse.matrix[2 * 3 + 0] = +(A[1 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[1 * 3 + 1]) / determinant;
  142. inverse.matrix[2 * 3 + 1] = -(A[0 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[0 * 3 + 1]) / determinant;
  143. inverse.matrix[2 * 3 + 2] = +(A[0 * 3 + 0] * A[1 * 3 + 1] - A[1 * 3 + 0] * A[0 * 3 + 1]) / determinant;
  144. vector_3 row0 = vector_3(inverse.matrix[0 * 3 + 0], inverse.matrix[0 * 3 + 1], inverse.matrix[0 * 3 + 2]);
  145. vector_3 row1 = vector_3(inverse.matrix[1 * 3 + 0], inverse.matrix[1 * 3 + 1], inverse.matrix[1 * 3 + 2]);
  146. vector_3 row2 = vector_3(inverse.matrix[2 * 3 + 0], inverse.matrix[2 * 3 + 1], inverse.matrix[2 * 3 + 2]);
  147. row0.normalize();
  148. row1.normalize();
  149. row2.normalize();
  150. inverse = matrix_3x3::create_from_rows(row0, row1, row2);
  151. //inverse.debug("inverse");
  152. return inverse;
  153. }
  154. void matrix_3x3::debug(char* title)
  155. {
  156. SERIAL_PROTOCOL(title);
  157. SERIAL_PROTOCOL("\n");
  158. int count = 0;
  159. for(int i=0; i<3; i++)
  160. {
  161. for(int j=0; j<3; j++)
  162. {
  163. SERIAL_PROTOCOL(matrix[count]);
  164. SERIAL_PROTOCOLPGM(" ");
  165. count++;
  166. }
  167. SERIAL_PROTOCOLPGM("\n");
  168. }
  169. }
  170. #endif // #ifdef ENABLE_AUTO_BED_LEVELING