Browse Source

Fixed the plane vector equation to a simpler one (only dependent on the normal)

Removed the calculation of the inverse matrix since the rotation matrix is orthogonal, therefore inverted == transposed.
Much simpler and mathematically robust.
fsantini 10 years ago
parent
commit
b64661070e
4 changed files with 30 additions and 54 deletions
  1. 2
    3
      Marlin/Marlin_main.cpp
  2. 1
    1
      Marlin/planner.cpp
  3. 25
    48
      Marlin/vector_3.cpp
  4. 2
    2
      Marlin/vector_3.h

+ 2
- 3
Marlin/Marlin_main.cpp View File

@@ -807,11 +807,11 @@ static void set_bed_level_equation(float z_at_xLeft_yFront, float z_at_xRight_yF
807 807
 
808 808
     vector_3 xPositive = (xRightyFront - xLeftyFront).get_normal();
809 809
     vector_3 yPositive = (xLeftyBack - xLeftyFront).get_normal();
810
-    vector_3 planeNormal = vector_3::cross(yPositive, xPositive).get_normal();
810
+    vector_3 planeNormal = vector_3::cross(xPositive, yPositive).get_normal();
811 811
 
812 812
     //planeNormal.debug("planeNormal");
813 813
     //yPositive.debug("yPositive");
814
-    matrix_3x3 bedLevel = matrix_3x3::create_look_at(planeNormal, yPositive);
814
+    plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
815 815
     //bedLevel.debug("bedLevel");
816 816
 
817 817
     //plan_bed_level_matrix.debug("bed level before");
@@ -819,7 +819,6 @@ static void set_bed_level_equation(float z_at_xLeft_yFront, float z_at_xRight_yF
819 819
     //uncorrected_position.debug("position before");
820 820
 
821 821
     // and set our bed level equation to do the right thing
822
-    plan_bed_level_matrix = matrix_3x3::create_inverse(bedLevel);
823 822
     //plan_bed_level_matrix.debug("bed level after");
824 823
 
825 824
     vector_3 corrected_position = plan_get_position();

+ 1
- 1
Marlin/planner.cpp View File

@@ -942,7 +942,7 @@ vector_3 plan_get_position() {
942 942
 
943 943
 	//position.debug("in plan_get position");
944 944
 	//plan_bed_level_matrix.debug("in plan_get bed_level");
945
-	matrix_3x3 inverse = matrix_3x3::create_inverse(plan_bed_level_matrix);
945
+	matrix_3x3 inverse = matrix_3x3::transpose(plan_bed_level_matrix);
946 946
 	//inverse.debug("in plan_get inverse");
947 947
 	position.apply_rotation(inverse);
948 948
 	//position.debug("after rotation");

+ 25
- 48
Marlin/vector_3.cpp View File

@@ -127,57 +127,34 @@ void matrix_3x3::set_to_identity()
127 127
 	matrix[6] = 0; matrix[7] = 0; matrix[8] = 1;
128 128
 }
129 129
 
130
-matrix_3x3 matrix_3x3::create_look_at(vector_3 target, vector_3 up)
131
-{
132
-    // There are lots of examples of look at code on the internet that don't do all these noramize and also find the position
133
-    // through several dot products.  The problem with them is that they have a bit of error in that all the vectors arn't normal and need to be.
134
-    vector_3 z_row = vector_3(-target.x, -target.y, -target.z).get_normal();
135
-    vector_3 x_row = vector_3::cross(up, z_row).get_normal();
136
-    vector_3 y_row = vector_3::cross(z_row, x_row).get_normal();
137
-
138
-    //x_row.debug("x_row");
139
-    //y_row.debug("y_row");
140
-    //z_row.debug("z_row");
141
-    
142
-    matrix_3x3 rot = matrix_3x3::create_from_rows(vector_3(x_row.x, y_row.x, z_row.x),
143
-                                vector_3(x_row.y, y_row.y, z_row.y),
144
-                                vector_3(x_row.z, y_row.z, z_row.z));
145
-
146
-    //rot.debug("rot");
130
+matrix_3x3 matrix_3x3::create_look_at(vector_3 target)
131
+{
132
+    vector_3 z_row = vector_3(target.x, target.y, target.z).get_normal();
133
+    vector_3 x_row = vector_3(1, 0, -target.x/target.z).get_normal();
134
+    vector_3 y_row = vector_3(0, 1, -target.y/target.z).get_normal();
135
+
136
+   // x_row.debug("x_row");
137
+   // y_row.debug("y_row");
138
+   // z_row.debug("z_row");
139
+
140
+ 
141
+     // create the matrix already correctly transposed
142
+    matrix_3x3 rot = matrix_3x3::create_from_rows(vector_3(x_row.x, x_row.y, x_row.z),
143
+                                vector_3(y_row.x, y_row.y, y_row.z),
144
+                                vector_3(z_row.x, z_row.y, z_row.z));
145
+
146
+ //   rot.debug("rot");
147 147
     return rot;
148 148
 }
149 149
 
150
-matrix_3x3 matrix_3x3::create_inverse(matrix_3x3 original)
151
-{
152
-	//original.debug("original");
153
-	float* A = original.matrix;
154
-	float determinant = 
155
-		+ A[0 * 3 + 0] * (A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2])
156
-		- A[0 * 3 + 1] * (A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0])
157
-		+ A[0 * 3 + 2] * (A[1 * 3 + 0] * A[2 * 3 + 1] - A[1 * 3 + 1] * A[2 * 3 + 0]);
158
-	matrix_3x3 inverse;
159
-	inverse.matrix[0 * 3 + 0] = +(A[1 * 3 + 1] * A[2 * 3 + 2] - A[2 * 3 + 1] * A[1 * 3 + 2]) / determinant;
160
-	inverse.matrix[0 * 3 + 1] = -(A[0 * 3 + 1] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 1]) / determinant;
161
-	inverse.matrix[0 * 3 + 2] = +(A[0 * 3 + 1] * A[1 * 3 + 2] - A[0 * 3 + 2] * A[1 * 3 + 1]) / determinant;
162
-	inverse.matrix[1 * 3 + 0] = -(A[1 * 3 + 0] * A[2 * 3 + 2] - A[1 * 3 + 2] * A[2 * 3 + 0]) / determinant;
163
-	inverse.matrix[1 * 3 + 1] = +(A[0 * 3 + 0] * A[2 * 3 + 2] - A[0 * 3 + 2] * A[2 * 3 + 0]) / determinant;
164
-	inverse.matrix[1 * 3 + 2] = -(A[0 * 3 + 0] * A[1 * 3 + 2] - A[1 * 3 + 0] * A[0 * 3 + 2]) / determinant;
165
-	inverse.matrix[2 * 3 + 0] = +(A[1 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[1 * 3 + 1]) / determinant;
166
-	inverse.matrix[2 * 3 + 1] = -(A[0 * 3 + 0] * A[2 * 3 + 1] - A[2 * 3 + 0] * A[0 * 3 + 1]) / determinant;
167
-	inverse.matrix[2 * 3 + 2] = +(A[0 * 3 + 0] * A[1 * 3 + 1] - A[1 * 3 + 0] * A[0 * 3 + 1]) / determinant;
168
-
169
-	vector_3 row0 = vector_3(inverse.matrix[0 * 3 + 0], inverse.matrix[0 * 3 + 1], inverse.matrix[0 * 3 + 2]);
170
-	vector_3 row1 = vector_3(inverse.matrix[1 * 3 + 0], inverse.matrix[1 * 3 + 1], inverse.matrix[1 * 3 + 2]);
171
-	vector_3 row2 = vector_3(inverse.matrix[2 * 3 + 0], inverse.matrix[2 * 3 + 1], inverse.matrix[2 * 3 + 2]);
172
-
173
-    row0.normalize();
174
-    row1.normalize();
175
-    row2.normalize();
176
-
177
-	inverse = matrix_3x3::create_from_rows(row0, row1, row2);
178
-
179
-	//inverse.debug("inverse");
180
-	return inverse;
150
+
151
+matrix_3x3 matrix_3x3::transpose(matrix_3x3 original)
152
+{
153
+  matrix_3x3 new_matrix;
154
+  new_matrix.matrix[0] = original.matrix[0]; new_matrix.matrix[1] = original.matrix[3]; new_matrix.matrix[2] = original.matrix[6]; 
155
+  new_matrix.matrix[3] = original.matrix[1]; new_matrix.matrix[4] = original.matrix[4]; new_matrix.matrix[5] = original.matrix[7]; 
156
+  new_matrix.matrix[6] = original.matrix[2]; new_matrix.matrix[7] = original.matrix[5]; new_matrix.matrix[8] = original.matrix[8];
157
+  return new_matrix;
181 158
 }
182 159
 
183 160
 void matrix_3x3::debug(char* title)

+ 2
- 2
Marlin/vector_3.h View File

@@ -47,8 +47,8 @@ struct matrix_3x3
47 47
 	float matrix[9];
48 48
 
49 49
 	static matrix_3x3 create_from_rows(vector_3 row_0, vector_3 row_1, vector_3 row_2);
50
-	static matrix_3x3 create_look_at(vector_3 target, vector_3 up);
51
-	static matrix_3x3 create_inverse(matrix_3x3 original);
50
+	static matrix_3x3 create_look_at(vector_3 target);
51
+	static matrix_3x3 transpose(matrix_3x3 original);
52 52
 
53 53
 	void set_to_identity();
54 54
 

Loading…
Cancel
Save