mesh_bed_leveling.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "mesh_bed_leveling.h"
  2. #ifdef MESH_BED_LEVELING
  3. mesh_bed_leveling mbl;
  4. mesh_bed_leveling::mesh_bed_leveling() { reset(); }
  5. void mesh_bed_leveling::reset() {
  6. active = 0;
  7. for (int y = 0; y < MESH_NUM_Y_POINTS; y++)
  8. for (int x = 0; x < MESH_NUM_X_POINTS; x++)
  9. z_values[y][x] = 0;
  10. }
  11. #if MESH_NUM_X_POINTS>=5 && MESH_NUM_Y_POINTS>=5 && (MESH_NUM_X_POINTS&1)==1 && (MESH_NUM_Y_POINTS&1)==1
  12. // Works for an odd number of MESH_NUM_X_POINTS and MESH_NUM_Y_POINTS
  13. void mesh_bed_leveling::upsample_3x3()
  14. {
  15. int idx0 = 0;
  16. int idx1 = MESH_NUM_X_POINTS / 2;
  17. int idx2 = MESH_NUM_X_POINTS - 1;
  18. {
  19. // First interpolate the points in X axis.
  20. static const float x0 = MESH_MIN_X;
  21. static const float x1 = 0.5f * float(MESH_MIN_X + MESH_MAX_X);
  22. static const float x2 = MESH_MAX_X;
  23. for (int j = 0; j < 3; ++ j) {
  24. // 1) Copy the source points to their new destination.
  25. z_values[j][idx2] = z_values[j][2];
  26. z_values[j][idx1] = z_values[j][1];
  27. // 2) Interpolate the remaining values by Largrangian polynomials.
  28. for (int i = idx0 + 1; i < idx2; ++ i) {
  29. if (i == idx1)
  30. continue;
  31. float x = get_x(i);
  32. z_values[j][i] = z_values[j][idx0] * (x - x1) * (x - x2) / ((x0 - x1) * (x0 - x2)) +
  33. z_values[j][idx1] * (x - x0) * (x - x2) / ((x1 - x0) * (x1 - x2)) +
  34. z_values[j][idx2] * (x - x0) * (x - x1) / ((x2 - x0) * (x2 - x1));
  35. }
  36. }
  37. }
  38. {
  39. // Second interpolate the points in Y axis.
  40. static const float y0 = MESH_MIN_Y;
  41. static const float y1 = 0.5f * float(MESH_MIN_Y + MESH_MAX_Y);
  42. static const float y2 = MESH_MAX_Y;
  43. for (int i = 0; i < MESH_NUM_X_POINTS; ++ i) {
  44. // 1) Copy the intermediate points to their new destination.
  45. z_values[idx2][i] = z_values[2][i];
  46. z_values[idx1][i] = z_values[1][i];
  47. // 2) Interpolate the remaining values by Largrangian polynomials.
  48. for (int j = 1; j + 1 < MESH_NUM_Y_POINTS; ++ j) {
  49. if (j == idx1)
  50. continue;
  51. float y = get_y(j);
  52. z_values[j][i] = z_values[idx0][i] * (y - y1) * (y - y2) / ((y0 - y1) * (y0 - y2)) +
  53. z_values[idx1][i] * (y - y0) * (y - y2) / ((y1 - y0) * (y1 - y2)) +
  54. z_values[idx2][i] * (y - y0) * (y - y1) / ((y2 - y0) * (y2 - y1));
  55. }
  56. }
  57. }
  58. }
  59. #endif
  60. #endif // MESH_BED_LEVELING