浏览代码

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
 
51
 
52
 vector_3::vector_3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) { }
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
   return vector_3(left.y * right.z - left.z * right.y,
55
   return vector_3(left.y * right.z - left.z * right.y,
56
                   left.z * right.x - left.x * right.z,
56
                   left.z * right.x - left.x * right.z,
57
                   left.x * right.y - left.y * right.x);
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
   vector_3 normalized = vector_3(x, y, z);
64
   vector_3 normalized = vector_3(x, y, z);
65
   normalized.normalize();
65
   normalized.normalize();
66
   return normalized;
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
 void vector_3::normalize() {
71
 void vector_3::normalize() {
72
   const float inv_length = RSQRT(sq(x) + sq(y) + sq(z));
72
   const float inv_length = RSQRT(sq(x) + sq(y) + sq(z));
75
   z *= inv_length;
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
 void vector_3::debug(const char * const title) {
85
 void vector_3::debug(const char * const title) {
95
   SERIAL_EOL();
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
   vector_3 vector = vector_3(x, y, z);
97
   vector_3 vector = vector_3(x, y, z);
100
   vector.apply_rotation(matrix);
98
   vector.apply_rotation(matrix);
101
   x = vector.x;
99
   x = vector.x;
103
   z = vector.z;
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
   //row_0.debug(PSTR("row_0"));
105
   //row_0.debug(PSTR("row_0"));
108
   //row_1.debug(PSTR("row_1"));
106
   //row_1.debug(PSTR("row_1"));
109
   //row_2.debug(PSTR("row_2"));
107
   //row_2.debug(PSTR("row_2"));
121
   matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
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
   // x_row.debug(PSTR("x_row"));
127
   // x_row.debug(PSTR("x_row"));
130
   // y_row.debug(PSTR("y_row"));
128
   // y_row.debug(PSTR("y_row"));
137
   return rot;
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
   matrix_3x3 new_matrix;
139
   matrix_3x3 new_matrix;
142
   new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
140
   new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6];
143
   new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7];
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
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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
 class matrix_3x3;
44
 class matrix_3x3;
46
 
45
 
50
   vector_3();
49
   vector_3();
51
   vector_3(float x, float y, float z);
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
   void normalize();
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
   void debug(const char * const title);
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
 struct matrix_3x3 {
64
 struct matrix_3x3 {
67
   float matrix[9];
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
   void set_to_identity();
71
   void set_to_identity();
74
 
72
 
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

正在加载...
取消
保存