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 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * vector_3.cpp - Vector library for bed leveling
  24. * Copyright (c) 2012 Lars Brubaker. All right reserved.
  25. *
  26. * This library is free software; you can redistribute it and/or
  27. * modify it under the terms of the GNU Lesser General Public
  28. * License as published by the Free Software Foundation; either
  29. * version 2.1 of the License, or (at your option) any later version.
  30. *
  31. * This library is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. * Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public
  37. * License along with this library; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  39. */
  40. #include "../inc/MarlinConfig.h"
  41. #if ABL_PLANAR || ENABLED(AUTO_BED_LEVELING_UBL)
  42. #include "vector_3.h"
  43. #include <math.h>
  44. vector_3::vector_3() : x(0), y(0), z(0) { }
  45. vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
  46. vector_3 vector_3::cross(const vector_3 &left, const vector_3 &right) {
  47. return vector_3(left.y * right.z - left.z * right.y,
  48. left.z * right.x - left.x * right.z,
  49. left.x * right.y - left.y * right.x);
  50. }
  51. vector_3 vector_3::operator+(const vector_3 &v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
  52. vector_3 vector_3::operator-(const vector_3 &v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
  53. vector_3 vector_3::get_normal() const {
  54. vector_3 normalized = vector_3(x, y, z);
  55. normalized.normalize();
  56. return normalized;
  57. }
  58. float vector_3::get_length() const { return SQRT(sq(x) + sq(y) + sq(z)); }
  59. void vector_3::normalize() {
  60. const float inv_length = RSQRT(sq(x) + sq(y) + sq(z));
  61. x *= inv_length;
  62. y *= inv_length;
  63. z *= inv_length;
  64. }
  65. void vector_3::apply_rotation(const matrix_3x3 &matrix) {
  66. const float _x = x, _y = y;
  67. x = _x * matrix.matrix[3 * 0 + 0] + _y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0];
  68. y = _x * matrix.matrix[3 * 0 + 1] + _y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1];
  69. z = _x * matrix.matrix[3 * 0 + 2] + _y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
  70. }
  71. void vector_3::debug(PGM_P const title) {
  72. serialprintPGM(title);
  73. SERIAL_ECHOPAIR_F(" x: ", x, 6);
  74. SERIAL_ECHOPAIR_F(" y: ", y, 6);
  75. SERIAL_ECHOLNPAIR_F(" z: ", z, 6);
  76. }
  77. void apply_rotation_xyz(const matrix_3x3 &matrix, float &x, float &y, float &z) {
  78. vector_3 vector = vector_3(x, y, z);
  79. vector.apply_rotation(matrix);
  80. x = vector.x;
  81. y = vector.y;
  82. z = vector.z;
  83. }
  84. matrix_3x3 matrix_3x3::create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2) {
  85. //row_0.debug(PSTR("row_0"));
  86. //row_1.debug(PSTR("row_1"));
  87. //row_2.debug(PSTR("row_2"));
  88. matrix_3x3 new_matrix;
  89. new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
  90. new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
  91. new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
  92. //new_matrix.debug(PSTR("new_matrix"));
  93. return new_matrix;
  94. }
  95. void matrix_3x3::set_to_identity() {
  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(const vector_3 &target) {
  101. vector_3 z_row = target.get_normal(),
  102. x_row = vector_3(1, 0, -target.x / target.z).get_normal(),
  103. y_row = vector_3::cross(z_row, x_row).get_normal();
  104. // x_row.debug(PSTR("x_row"));
  105. // y_row.debug(PSTR("y_row"));
  106. // z_row.debug(PSTR("z_row"));
  107. // create the matrix already correctly transposed
  108. matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
  109. // rot.debug(PSTR("rot"));
  110. return rot;
  111. }
  112. matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
  113. matrix_3x3 new_matrix;
  114. new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
  115. new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
  116. new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
  117. return new_matrix;
  118. }
  119. void matrix_3x3::debug(PGM_P const title) {
  120. if (title != nullptr) {
  121. serialprintPGM(title);
  122. SERIAL_EOL();
  123. }
  124. uint8_t count = 0;
  125. for (uint8_t i = 0; i < 3; i++) {
  126. for (uint8_t j = 0; j < 3; j++) {
  127. if (matrix[count] >= 0.0) SERIAL_CHAR('+');
  128. SERIAL_ECHO_F(matrix[count], 6);
  129. SERIAL_CHAR(' ');
  130. count++;
  131. }
  132. SERIAL_EOL();
  133. }
  134. }
  135. #endif // HAS_ABL_OR_UBL