2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2016-03-24 19:01:20 +01:00
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 22:06:53 +01:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-03-24 19:01:20 +01:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-25 07:19:46 +01:00
|
|
|
/**
|
2017-11-18 09:08:03 +01:00
|
|
|
* vector_3.cpp - Vector library for bed leveling
|
|
|
|
* Copyright (c) 2012 Lars Brubaker. All right reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2017-09-06 13:28:32 +02:00
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
2013-09-29 18:20:06 +02:00
|
|
|
|
2018-05-01 14:05:18 +02:00
|
|
|
#if ABL_PLANAR || ENABLED(AUTO_BED_LEVELING_UBL)
|
2017-09-06 13:28:32 +02:00
|
|
|
|
2013-09-29 18:20:06 +02:00
|
|
|
#include "vector_3.h"
|
|
|
|
|
2017-09-06 13:28:32 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2013-12-19 22:21:46 +01:00
|
|
|
vector_3::vector_3() : x(0), y(0), z(0) { }
|
2013-09-29 18:20:06 +02:00
|
|
|
|
2013-12-19 22:21:46 +01:00
|
|
|
vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
|
2013-09-29 18:20:06 +02:00
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
vector_3 vector_3::cross(const vector_3 &left, const vector_3 &right) {
|
2015-10-03 08:08:58 +02:00
|
|
|
return vector_3(left.y * right.z - left.z * right.y,
|
|
|
|
left.z * right.x - left.x * right.z,
|
|
|
|
left.x * right.y - left.y * right.x);
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
vector_3 vector_3::operator+(const vector_3 &v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
|
|
|
|
vector_3 vector_3::operator-(const vector_3 &v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
|
2013-09-29 18:20:06 +02:00
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
vector_3 vector_3::get_normal() const {
|
2015-10-03 08:08:58 +02:00
|
|
|
vector_3 normalized = vector_3(x, y, z);
|
|
|
|
normalized.normalize();
|
|
|
|
return normalized;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
float vector_3::get_length() const { return SQRT(sq(x) + sq(y) + sq(z)); }
|
2015-03-26 06:13:59 +01:00
|
|
|
|
|
|
|
void vector_3::normalize() {
|
2018-07-01 22:20:28 +02:00
|
|
|
const float inv_length = RSQRT(sq(x) + sq(y) + sq(z));
|
2017-03-24 01:39:24 +01:00
|
|
|
x *= inv_length;
|
|
|
|
y *= inv_length;
|
|
|
|
z *= inv_length;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
void vector_3::apply_rotation(const matrix_3x3 &matrix) {
|
2018-09-30 08:51:21 +02:00
|
|
|
const float _x = x, _y = y;
|
|
|
|
x = _x * matrix.matrix[3 * 0 + 0] + _y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0];
|
|
|
|
y = _x * matrix.matrix[3 * 0 + 1] + _y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1];
|
|
|
|
z = _x * matrix.matrix[3 * 0 + 2] + _y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:44:33 +02:00
|
|
|
void vector_3::debug(PGM_P const title) {
|
2017-05-02 23:46:11 +02:00
|
|
|
serialprintPGM(title);
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOPAIR_F(" x: ", x, 6);
|
|
|
|
SERIAL_ECHOPAIR_F(" y: ", y, 6);
|
|
|
|
SERIAL_ECHOLNPAIR_F(" z: ", z, 6);
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
void apply_rotation_xyz(const matrix_3x3 &matrix, float &x, float &y, float &z) {
|
2015-10-03 08:08:58 +02:00
|
|
|
vector_3 vector = vector_3(x, y, z);
|
|
|
|
vector.apply_rotation(matrix);
|
|
|
|
x = vector.x;
|
|
|
|
y = vector.y;
|
|
|
|
z = vector.z;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
matrix_3x3 matrix_3x3::create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2) {
|
2017-05-02 23:46:11 +02:00
|
|
|
//row_0.debug(PSTR("row_0"));
|
|
|
|
//row_1.debug(PSTR("row_1"));
|
|
|
|
//row_2.debug(PSTR("row_2"));
|
2015-10-03 08:08:58 +02:00
|
|
|
matrix_3x3 new_matrix;
|
|
|
|
new_matrix.matrix[0] = row_0.x; new_matrix.matrix[1] = row_0.y; new_matrix.matrix[2] = row_0.z;
|
|
|
|
new_matrix.matrix[3] = row_1.x; new_matrix.matrix[4] = row_1.y; new_matrix.matrix[5] = row_1.z;
|
|
|
|
new_matrix.matrix[6] = row_2.x; new_matrix.matrix[7] = row_2.y; new_matrix.matrix[8] = row_2.z;
|
2017-05-02 23:46:11 +02:00
|
|
|
//new_matrix.debug(PSTR("new_matrix"));
|
2015-10-03 08:08:58 +02:00
|
|
|
return new_matrix;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 06:13:59 +01:00
|
|
|
void matrix_3x3::set_to_identity() {
|
2015-10-03 08:08:58 +02:00
|
|
|
matrix[0] = 1; matrix[1] = 0; matrix[2] = 0;
|
|
|
|
matrix[3] = 0; matrix[4] = 1; matrix[5] = 0;
|
|
|
|
matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
matrix_3x3 matrix_3x3::create_look_at(const vector_3 &target) {
|
|
|
|
vector_3 z_row = target.get_normal(),
|
|
|
|
x_row = vector_3(1, 0, -target.x / target.z).get_normal(),
|
|
|
|
y_row = vector_3::cross(z_row, x_row).get_normal();
|
2013-12-06 21:32:21 +01:00
|
|
|
|
2017-05-02 23:46:11 +02:00
|
|
|
// x_row.debug(PSTR("x_row"));
|
|
|
|
// y_row.debug(PSTR("y_row"));
|
|
|
|
// z_row.debug(PSTR("z_row"));
|
2013-12-06 21:32:21 +01:00
|
|
|
|
2015-03-26 06:13:59 +01:00
|
|
|
// create the matrix already correctly transposed
|
|
|
|
matrix_3x3 rot = matrix_3x3::create_from_rows(x_row, y_row, z_row);
|
2013-12-06 21:32:21 +01:00
|
|
|
|
2017-05-02 23:46:11 +02:00
|
|
|
// rot.debug(PSTR("rot"));
|
2015-03-26 06:13:59 +01:00
|
|
|
return rot;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-09-30 06:04:40 +02:00
|
|
|
matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
|
2013-12-06 21:32:21 +01:00
|
|
|
matrix_3x3 new_matrix;
|
2015-10-03 08:08:58 +02:00
|
|
|
new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
|
|
|
|
new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
|
2013-12-06 21:32:21 +01:00
|
|
|
new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
|
|
|
|
return new_matrix;
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-01 06:44:33 +02:00
|
|
|
void matrix_3x3::debug(PGM_P const title) {
|
2019-05-09 18:45:55 +02:00
|
|
|
if (title != nullptr) {
|
2018-03-19 02:02:08 +01:00
|
|
|
serialprintPGM(title);
|
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
2017-03-24 01:39:24 +01:00
|
|
|
uint8_t count = 0;
|
|
|
|
for (uint8_t i = 0; i < 3; i++) {
|
|
|
|
for (uint8_t j = 0; j < 3; j++) {
|
2018-11-29 23:58:58 +01:00
|
|
|
if (matrix[count] >= 0.0) SERIAL_CHAR('+');
|
|
|
|
SERIAL_ECHO_F(matrix[count], 6);
|
|
|
|
SERIAL_CHAR(' ');
|
2015-03-07 07:14:34 +01:00
|
|
|
count++;
|
|
|
|
}
|
2017-06-09 17:51:23 +02:00
|
|
|
SERIAL_EOL();
|
2015-03-07 07:14:34 +01:00
|
|
|
}
|
2013-09-29 18:20:06 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 03:29:03 +01:00
|
|
|
#endif // HAS_ABL_OR_UBL
|