Browse Source

Adjust vector_3 code with const, some optimization

Scott Lahteine 7 years ago
parent
commit
8d54ffbf05
1 changed files with 11 additions and 11 deletions
  1. 11
    11
      Marlin/vector_3.cpp

+ 11
- 11
Marlin/vector_3.cpp View File

@@ -66,16 +66,16 @@ vector_3 vector_3::get_normal() {
66 66
 float vector_3::get_length() { return sqrt((x * x) + (y * y) + (z * z)); }
67 67
 
68 68
 void vector_3::normalize() {
69
-  float length = get_length();
70
-  x /= length;
71
-  y /= length;
72
-  z /= length;
69
+  const float inv_length = 1.0 / get_length();
70
+  x *= inv_length;
71
+  y *= inv_length;
72
+  z *= inv_length;
73 73
 }
74 74
 
75 75
 void vector_3::apply_rotation(matrix_3x3 matrix) {
76
-  float resultX = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0];
77
-  float resultY = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1];
78
-  float resultZ = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
76
+  const float resultX = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0],
77
+              resultY = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1],
78
+              resultZ = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
79 79
   x = resultX;
80 80
   y = resultY;
81 81
   z = resultZ;
@@ -92,7 +92,7 @@ void vector_3::debug(const char title[]) {
92 92
   SERIAL_EOL;
93 93
 }
94 94
 
95
-void apply_rotation_xyz(matrix_3x3 matrix, float& x, float& y, float& z) {
95
+void apply_rotation_xyz(matrix_3x3 matrix, float &x, float &y, float &z) {
96 96
   vector_3 vector = vector_3(x, y, z);
97 97
   vector.apply_rotation(matrix);
98 98
   x = vector.x;
@@ -144,9 +144,9 @@ matrix_3x3 matrix_3x3::transpose(matrix_3x3 original) {
144 144
 
145 145
 void matrix_3x3::debug(const char title[]) {
146 146
   SERIAL_PROTOCOLLN(title);
147
-  int count = 0;
148
-  for (int i = 0; i < 3; i++) {
149
-    for (int j = 0; j < 3; j++) {
147
+  uint8_t count = 0;
148
+  for (uint8_t i = 0; i < 3; i++) {
149
+    for (uint8_t j = 0; j < 3; j++) {
150 150
       if (matrix[count] >= 0.0) SERIAL_PROTOCOLCHAR('+');
151 151
       SERIAL_PROTOCOL_F(matrix[count], 6);
152 152
       SERIAL_PROTOCOLCHAR(' ');

Loading…
Cancel
Save