Top Level Namespace
- Includes:
- Math
Defined Under Namespace
Modules: VoxelammingGem
Instance Method Summary collapse
- #add_vectors(vector1, vector2) ⇒ Object
- #degrees_to_radians(degrees) ⇒ Object
- #get_rotation_matrix(pitch, yaw, roll) ⇒ Object
- #matrix_multiply(a, b) ⇒ Object
- #transform_point_by_rotation_matrix(point, r) ⇒ Object
- #transpose_3x3(matrix) ⇒ Object
Instance Method Details
#add_vectors(vector1, vector2) ⇒ Object
51 52 53 |
# File 'lib/matrix_util.rb', line 51 def add_vectors(vector1, vector2) vector1.zip(vector2).map { |v1, v2| v1 + v2 } end |
#degrees_to_radians(degrees) ⇒ Object
63 64 65 |
# File 'lib/matrix_util.rb', line 63 def degrees_to_radians(degrees) degrees * Math::PI / 180.0 end |
#get_rotation_matrix(pitch, yaw, roll) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/matrix_util.rb', line 3 def get_rotation_matrix(pitch, yaw, roll) pitch, yaw, roll = [pitch, yaw, roll].map { |angle| degrees_to_radians(angle) } # Pitch (Rotation around X-axis) rx = [ [1, 0, 0], [0, cos(pitch), -sin(pitch)], [0, sin(pitch), cos(pitch)] ] # Yaw (Rotation around Y-axis) ry = [ [cos(yaw), 0, sin(yaw)], [0, 1, 0], [-sin(yaw), 0, cos(yaw)] ] # Roll (Rotation around Z-axis) rz = [ [cos(roll), -sin(roll), 0], [sin(roll), cos(roll), 0], [0, 0, 1] ] # Multiplication of matrices in the order Rx * Rz * Ry r = matrix_multiply(rx, matrix_multiply(rz, ry)) return r end |
#matrix_multiply(a, b) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/matrix_util.rb', line 31 def matrix_multiply(a, b) result = Array.new(3) { Array.new(3, 0) } for i in 0...3 for j in 0...3 for k in 0...3 result[i][j] += a[i][k] * b[k][j] end end end return result end |
#transform_point_by_rotation_matrix(point, r) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/matrix_util.rb', line 43 def transform_point_by_rotation_matrix(point, r) x, y, z = point x_new = r[0][0] * x + r[0][1] * y + r[0][2] * z y_new = r[1][0] * x + r[1][1] * y + r[1][2] * z z_new = r[2][0] * x + r[2][1] * y + r[2][2] * z return [x_new, y_new, z_new] end |
#transpose_3x3(matrix) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/matrix_util.rb', line 55 def transpose_3x3(matrix) return [ [matrix[0][0], matrix[1][0], matrix[2][0]], [matrix[0][1], matrix[1][1], matrix[2][1]], [matrix[0][2], matrix[1][2], matrix[2][2]] ] end |