def v_dot(self, vector): """ calculate dot product of Matrix3D with inverted(Vector3D) | a1 | b1 | c1 | d1 | | x | | a1*x + b1*y + c1*z + d1*h | | a2 | b2 | c2 | d2 | . | y | = | a2*x + b2*y + c2*z + d2*h | | a3 | b3 | c3 | d3 | | z | | a3*x + b3*y + c3*z + d3*h | | a4 | b4 | c4 | d4 | | h | | a4*x + b4*y + c4*z + d4*h | """ assert isinstance(vector, Vector3D) ret_data = [0, 0, 0, 0] for index in range(4): row_vec = Vector3D.from_list(self.__data[index]) ret_data[index] = row_vec.dot(vector) return Vector3D.from_list(ret_data)
def v_dot(self, vector): """ calculate dot product of Matrix3D with inverted(Vector3D) | a1 | b1 | c1 | d1 | | x | | a1*x + b1*y + c1*z + d1*h | | a2 | b2 | c2 | d2 | . | y | = | a2*x + b2*y + c2*z + d2*h | | a3 | b3 | c3 | d3 | | z | | a3*x + b3*y + c3*z + d3*h | | a4 | b4 | c4 | d4 | | h | | a4*x + b4*y + c4*z + d4*h | """ assert isinstance(vector, Vector3D) ret_data = [0, 0, 0, 0] for index in range(4): row_vec = Vector3D.from_list(self.__data[index]) ret_data[index] = row_vec.dot(vector) return Vector3D.from_list(ret_data)
def test_rot_matrices(self): m = Matrix3D.get_rot_x_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_y_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_z_matrix(100) assert m.dot(Matrix3D.identity()) == m # rotate vector only in x-axis around x - nothing should happen v1 = Vector3D(1, 0, 0, 1) assert Matrix3D.get_rot_x_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 1, 0, 1) assert Matrix3D.get_rot_y_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 0, 1, 1) assert Matrix3D.get_rot_z_matrix(100).v_dot(v1) == v1 # rotate vectors really v1 = Vector3D(1.0, 0.0, 0.0, 1.0) # 90 degrees or pi/2 real_v = Matrix3D.get_rot_z_matrix(math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 180 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 270 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi + math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, -1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 360 degrees real_v = Matrix3D.get_rot_z_matrix(2 * math.pi).v_dot(v1) test_v = Vector3D.from_list([1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate around Y-Axis about 180 degrees real_v = Matrix3D.get_rot_y_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate y:90 and x:90 -> (0, 1, 0, 1) real_v = Matrix3D.get_rot_y_matrix(math.pi / 2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 0.000000, -1.000000, 1.000000]) assert real_v.nearly_equal(test_v) real_v = Matrix3D.get_rot_x_matrix(math.pi / 2).v_dot(real_v) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # and this is the combined version rot_y = Matrix3D.get_rot_y_matrix(math.pi / 2) print "rotation around y:\n", rot_y rot_x = Matrix3D.get_rot_x_matrix(math.pi / 2) print "rotation around x:\n", rot_x rot_z = Matrix3D.get_rot_z_matrix(math.pi / 2) print "rotation around z:\n", rot_z rot_m = rot_x.dot(rot_y.dot(rot_z)) print "combined rotation matrix:\n", rot_m real_v = rot_m.v_dot(v1) print "resulting vector:", real_v test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v)
def test_rot_matrices(self): m = Matrix3D.get_rot_x_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_y_matrix(100) assert m.dot(Matrix3D.identity()) == m m = Matrix3D.get_rot_z_matrix(100) assert m.dot(Matrix3D.identity()) == m # rotate vector only in x-axis around x - nothing should happen v1 = Vector3D(1, 0, 0, 1) assert Matrix3D.get_rot_x_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 1, 0, 1) assert Matrix3D.get_rot_y_matrix(100).v_dot(v1) == v1 v1 = Vector3D(0, 0, 1, 1) assert Matrix3D.get_rot_z_matrix(100).v_dot(v1) == v1 # rotate vectors really v1 = Vector3D(1.0, 0.0, 0.0, 1.0) # 90 degrees or pi/2 real_v = Matrix3D.get_rot_z_matrix(math.pi/2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 180 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 270 degrees real_v = Matrix3D.get_rot_z_matrix(math.pi + math.pi/2).v_dot(v1) test_v = Vector3D.from_list([0.000000, -1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # 360 degrees real_v = Matrix3D.get_rot_z_matrix(2 * math.pi).v_dot(v1) test_v = Vector3D.from_list([1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate around Y-Axis about 180 degrees real_v = Matrix3D.get_rot_y_matrix(math.pi).v_dot(v1) test_v = Vector3D.from_list([-1.000000, 0.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # rotate y:90 and x:90 -> (0, 1, 0, 1) real_v = Matrix3D.get_rot_y_matrix(math.pi/2).v_dot(v1) test_v = Vector3D.from_list([0.000000, 0.000000, -1.000000, 1.000000]) assert real_v.nearly_equal(test_v) real_v = Matrix3D.get_rot_x_matrix(math.pi/2).v_dot(real_v) test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v) # and this is the combined version rot_y = Matrix3D.get_rot_y_matrix(math.pi/2) print "rotation around y:\n", rot_y rot_x = Matrix3D.get_rot_x_matrix(math.pi/2) print "rotation around x:\n", rot_x rot_z = Matrix3D.get_rot_z_matrix(math.pi/2) print "rotation around z:\n", rot_z rot_m = rot_x.dot(rot_y.dot(rot_z)) print "combined rotation matrix:\n", rot_m real_v = rot_m.v_dot(v1) print "resulting vector:", real_v test_v = Vector3D.from_list([0.000000, 1.000000, 0.000000, 1.000000]) assert real_v.nearly_equal(test_v)
def dot(self, other): """ return dot product of these to 4x4 matrices Matrix A : self Matrix B : other TODO: find a way to describe this in short terms | a11 | a12 | a13 | a14 | | b11 | b12 | b13 | b14 | | row[1].col[1] row[1].col[2] ... ... | | a21 | a22 | a23 | a24 | . | b21 | b22 | b23 | b24 | = | row[2].col[1] row[2].col[2] ... ... | | a31 | a32 | a33 | a34 | | b31 | b32 | b33 | b34 | | ... ... | | a41 | a42 | a43 | a44 | | b41 | b42 | b43 | b44 | | | """ assert isinstance(other, Matrix3D) ret_matrix = self.zeros() for rownum in range(4): row_vec = Vector3D.from_list(self.row(rownum)) for colnum in range(4): col_vec = Vector3D.from_list(other.col(colnum)) ret_matrix[rownum][colnum] = row_vec.dot(col_vec) return ret_matrix
def dot(self, other): """ return dot product of these to 4x4 matrices Matrix A : self Matrix B : other TODO: find a way to describe this in short terms | a11 | a12 | a13 | a14 | | b11 | b12 | b13 | b14 | | row[1].col[1] row[1].col[2] ... ... | | a21 | a22 | a23 | a24 | . | b21 | b22 | b23 | b24 | = | row[2].col[1] row[2].col[2] ... ... | | a31 | a32 | a33 | a34 | | b31 | b32 | b33 | b34 | | ... ... | | a41 | a42 | a43 | a44 | | b41 | b42 | b43 | b44 | | | """ assert isinstance(other, Matrix3D) ret_matrix = self.zeros() for rownum in range(4): row_vec = Vector3D.from_list(self.row(rownum)) for colnum in range(4): col_vec = Vector3D.from_list(other.col(colnum)) ret_matrix[rownum][colnum] = row_vec.dot(col_vec) return ret_matrix