瀏覽代碼

Avoid copies in vector and matrix (#11959)

AnoNymous 6 年之前
父節點
當前提交
a644d8cb93
共有 2 個檔案被更改,包括 28 行新增33 行删除
  1. 17
    19
      Marlin/src/libs/vector_3.cpp
  2. 11
    14
      Marlin/src/libs/vector_3.h

+ 17
- 19
Marlin/src/libs/vector_3.cpp 查看文件

@@ -51,22 +51,22 @@ vector_3::vector_3() : x(0), y(0), z(0) { }
51 51
 
52 52
 vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
53 53
 
54
-vector_3 vector_3::cross(vector_3 left, vector_3 right) {
54
+vector_3 vector_3::cross(const vector_3 &left, const vector_3 &right) {
55 55
   return vector_3(left.y * right.z - left.z * right.y,
56 56
                   left.z * right.x - left.x * right.z,
57 57
                   left.x * right.y - left.y * right.x);
58 58
 }
59 59
 
60
-vector_3 vector_3::operator+(vector_3 v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
61
-vector_3 vector_3::operator-(vector_3 v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
60
+vector_3 vector_3::operator+(const vector_3 &v) { return vector_3((x + v.x), (y + v.y), (z + v.z)); }
61
+vector_3 vector_3::operator-(const vector_3 &v) { return vector_3((x - v.x), (y - v.y), (z - v.z)); }
62 62
 
63
-vector_3 vector_3::get_normal() {
63
+vector_3 vector_3::get_normal() const {
64 64
   vector_3 normalized = vector_3(x, y, z);
65 65
   normalized.normalize();
66 66
   return normalized;
67 67
 }
68 68
 
69
-float vector_3::get_length() { return SQRT(sq(x) + sq(y) + sq(z)); }
69
+float vector_3::get_length() const { return SQRT(sq(x) + sq(y) + sq(z)); }
70 70
 
71 71
 void vector_3::normalize() {
72 72
   const float inv_length = RSQRT(sq(x) + sq(y) + sq(z));
@@ -75,13 +75,11 @@ void vector_3::normalize() {
75 75
   z *= inv_length;
76 76
 }
77 77
 
78
-void vector_3::apply_rotation(matrix_3x3 matrix) {
79
-  const float resultX = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0],
80
-              resultY = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1],
81
-              resultZ = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
82
-  x = resultX;
83
-  y = resultY;
84
-  z = resultZ;
78
+void vector_3::apply_rotation(const matrix_3x3 &matrix) {
79
+  const float _x = x * matrix.matrix[3 * 0 + 0] + y * matrix.matrix[3 * 1 + 0] + z * matrix.matrix[3 * 2 + 0],
80
+              _y = x * matrix.matrix[3 * 0 + 1] + y * matrix.matrix[3 * 1 + 1] + z * matrix.matrix[3 * 2 + 1],
81
+              _z = x * matrix.matrix[3 * 0 + 2] + y * matrix.matrix[3 * 1 + 2] + z * matrix.matrix[3 * 2 + 2];
82
+  x = _x; y = _y; z = _z;
85 83
 }
86 84
 
87 85
 void vector_3::debug(const char * const title) {
@@ -95,7 +93,7 @@ void vector_3::debug(const char * const title) {
95 93
   SERIAL_EOL();
96 94
 }
97 95
 
98
-void apply_rotation_xyz(matrix_3x3 matrix, float &x, float &y, float &z) {
96
+void apply_rotation_xyz(const matrix_3x3 &matrix, float &x, float &y, float &z) {
99 97
   vector_3 vector = vector_3(x, y, z);
100 98
   vector.apply_rotation(matrix);
101 99
   x = vector.x;
@@ -103,7 +101,7 @@ void apply_rotation_xyz(matrix_3x3 matrix, float &x, float &y, float &z) {
103 101
   z = vector.z;
104 102
 }
105 103
 
106
-matrix_3x3 matrix_3x3::create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2) {
104
+matrix_3x3 matrix_3x3::create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2) {
107 105
   //row_0.debug(PSTR("row_0"));
108 106
   //row_1.debug(PSTR("row_1"));
109 107
   //row_2.debug(PSTR("row_2"));
@@ -121,10 +119,10 @@ void matrix_3x3::set_to_identity() {
121 119
   matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
122 120
 }
123 121
 
124
-matrix_3x3 matrix_3x3::create_look_at(vector_3 target) {
125
-  vector_3 z_row = target.get_normal();
126
-  vector_3 x_row = vector_3(1, 0, -target.x / target.z).get_normal();
127
-  vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
122
+matrix_3x3 matrix_3x3::create_look_at(const vector_3 &target) {
123
+  vector_3 z_row = target.get_normal(),
124
+           x_row = vector_3(1, 0, -target.x / target.z).get_normal(),
125
+           y_row = vector_3::cross(z_row, x_row).get_normal();
128 126
 
129 127
   // x_row.debug(PSTR("x_row"));
130 128
   // y_row.debug(PSTR("y_row"));
@@ -137,7 +135,7 @@ matrix_3x3 matrix_3x3::create_look_at(vector_3 target) {
137 135
   return rot;
138 136
 }
139 137
 
140
-matrix_3x3 matrix_3x3::transpose(matrix_3x3 original) {
138
+matrix_3x3 matrix_3x3::transpose(const matrix_3x3 &original) {
141 139
   matrix_3x3 new_matrix;
142 140
   new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
143 141
   new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];

+ 11
- 14
Marlin/src/libs/vector_3.h 查看文件

@@ -39,8 +39,7 @@
39 39
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
40 40
  */
41 41
 
42
-#ifndef VECTOR_3_H
43
-#define VECTOR_3_H
42
+#pragma once
44 43
 
45 44
 class matrix_3x3;
46 45
 
@@ -50,25 +49,24 @@ struct vector_3 {
50 49
   vector_3();
51 50
   vector_3(float x, float y, float z);
52 51
 
53
-  static vector_3 cross(vector_3 a, vector_3 b);
52
+  static vector_3 cross(const vector_3 &a, const vector_3 &b);
54 53
 
55
-  vector_3 operator+(vector_3 v);
56
-  vector_3 operator-(vector_3 v);
54
+  vector_3 operator+(const vector_3 &v);
55
+  vector_3 operator-(const vector_3 &v);
57 56
   void normalize();
58
-  float get_length();
59
-  vector_3 get_normal();
57
+  float get_length() const;
58
+  vector_3 get_normal() const;
60 59
 
61 60
   void debug(const char * const title);
62
-
63
-  void apply_rotation(matrix_3x3 matrix);
61
+  void apply_rotation(const matrix_3x3 &matrix);
64 62
 };
65 63
 
66 64
 struct matrix_3x3 {
67 65
   float matrix[9];
68 66
 
69
-  static matrix_3x3 create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2);
70
-  static matrix_3x3 create_look_at(vector_3 target);
71
-  static matrix_3x3 transpose(matrix_3x3 original);
67
+  static matrix_3x3 create_from_rows(const vector_3 &row_0, const vector_3 &row_1, const vector_3 &row_2);
68
+  static matrix_3x3 create_look_at(const vector_3 &target);
69
+  static matrix_3x3 transpose(const matrix_3x3 &original);
72 70
 
73 71
   void set_to_identity();
74 72
 
@@ -76,6 +74,5 @@ struct matrix_3x3 {
76 74
 };
77 75
 
78 76
 
79
-void apply_rotation_xyz(matrix_3x3 rotationMatrix, float &x, float &y, float &z);
77
+void apply_rotation_xyz(const matrix_3x3 &rotationMatrix, float &x, float &y, float &z);
80 78
 
81
-#endif // VECTOR_3_H

Loading…
取消
儲存