Class: Geo3d::Matrix
- Inherits:
-
Object
- Object
- Geo3d::Matrix
- Defined in:
- lib/matrix.rb
Instance Attribute Summary collapse
-
#_11 ⇒ Object
Returns the value of attribute _11.
-
#_12 ⇒ Object
Returns the value of attribute _12.
-
#_13 ⇒ Object
Returns the value of attribute _13.
-
#_14 ⇒ Object
Returns the value of attribute _14.
-
#_21 ⇒ Object
Returns the value of attribute _21.
-
#_22 ⇒ Object
Returns the value of attribute _22.
-
#_23 ⇒ Object
Returns the value of attribute _23.
-
#_24 ⇒ Object
Returns the value of attribute _24.
-
#_31 ⇒ Object
Returns the value of attribute _31.
-
#_32 ⇒ Object
Returns the value of attribute _32.
-
#_33 ⇒ Object
Returns the value of attribute _33.
-
#_34 ⇒ Object
Returns the value of attribute _34.
-
#_41 ⇒ Object
Returns the value of attribute _41.
-
#_42 ⇒ Object
Returns the value of attribute _42.
-
#_43 ⇒ Object
Returns the value of attribute _43.
-
#_44 ⇒ Object
Returns the value of attribute _44.
Class Method Summary collapse
- .identity ⇒ Object
- .look_at_lh(eye_position, look_at_position, up_direction) ⇒ Object
- .look_at_rh(eye_position, look_at_position, up_direction) ⇒ Object
- .ortho_off_center_lh(l, r, b, t, zn, zf) ⇒ Object
- .ortho_off_center_rh(l, r, b, t, zn, zf) ⇒ Object
- .perspective_fov_lh(fovy, aspect, zn, zf) ⇒ Object
- .perspective_fov_rh(fovy, aspect, zn, zf) ⇒ Object
- .reflection(reflection_plane) ⇒ Object
- .rotation(axis, angle) ⇒ Object
- .rotation_x(angle) ⇒ Object
- .rotation_y(angle) ⇒ Object
- .rotation_z(angle) ⇒ Object
- .scaling(x, y, z) ⇒ Object
- .shadow(light_position, plane) ⇒ Object
- .translation(x, y, z) ⇒ Object
- .uniform_scaling(scale) ⇒ Object
Instance Method Summary collapse
- #!=(m) ⇒ Object
- #*(v) ⇒ Object
- #+(mat) ⇒ Object
- #+@ ⇒ Object
- #-(mat) ⇒ Object
- #-@ ⇒ Object
- #/(v) ⇒ Object
- #==(m) ⇒ Object
- #[](x, y) ⇒ Object
- #[]=(x, y, v) ⇒ Object
- #determinant ⇒ Object
- #identity? ⇒ Boolean
-
#initialize(*args) ⇒ Matrix
constructor
A new instance of Matrix.
- #inverse(with_determinant = false) ⇒ Object
- #print ⇒ Object
- #rotation_component ⇒ Object
- #scaling_component ⇒ Object
- #to_a ⇒ Object
- #transform(vec) ⇒ Object
- #transform_coord(vec) ⇒ Object
- #translation_component ⇒ Object
- #transpose ⇒ Object
Constructor Details
#initialize(*args) ⇒ Matrix
Returns a new instance of Matrix.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/matrix.rb', line 10 def initialize *args @_11 = 0 @_12 = 0 @_13 = 0 @_14 = 0 @_21 = 0 @_22 = 0 @_23 = 0 @_24 = 0 @_31 = 0 @_32 = 0 @_33 = 0 @_34 = 0 @_41 = 0 @_42 = 0 @_43 = 0 @_44 = 0 @_11 = args[0] if args.size > 0 @_12 = args[1] if args.size > 1 @_13 = args[2] if args.size > 2 @_14 = args[3] if args.size > 3 @_21 = args[4] if args.size > 4 @_22 = args[5] if args.size > 5 @_23 = args[6] if args.size > 6 @_24 = args[7] if args.size > 7 @_31 = args[8] if args.size > 8 @_32 = args[9] if args.size > 9 @_33 = args[10] if args.size > 10 @_34 = args[11] if args.size > 11 @_41 = args[12] if args.size > 12 @_42 = args[13] if args.size > 13 @_43 = args[14] if args.size > 14 @_44 = args[15] if args.size > 15 end |
Instance Attribute Details
#_11 ⇒ Object
Returns the value of attribute _11.
5 6 7 |
# File 'lib/matrix.rb', line 5 def _11 @_11 end |
#_12 ⇒ Object
Returns the value of attribute _12.
5 6 7 |
# File 'lib/matrix.rb', line 5 def _12 @_12 end |
#_13 ⇒ Object
Returns the value of attribute _13.
5 6 7 |
# File 'lib/matrix.rb', line 5 def _13 @_13 end |
#_14 ⇒ Object
Returns the value of attribute _14.
5 6 7 |
# File 'lib/matrix.rb', line 5 def _14 @_14 end |
#_21 ⇒ Object
Returns the value of attribute _21.
6 7 8 |
# File 'lib/matrix.rb', line 6 def _21 @_21 end |
#_22 ⇒ Object
Returns the value of attribute _22.
6 7 8 |
# File 'lib/matrix.rb', line 6 def _22 @_22 end |
#_23 ⇒ Object
Returns the value of attribute _23.
6 7 8 |
# File 'lib/matrix.rb', line 6 def _23 @_23 end |
#_24 ⇒ Object
Returns the value of attribute _24.
6 7 8 |
# File 'lib/matrix.rb', line 6 def _24 @_24 end |
#_31 ⇒ Object
Returns the value of attribute _31.
7 8 9 |
# File 'lib/matrix.rb', line 7 def _31 @_31 end |
#_32 ⇒ Object
Returns the value of attribute _32.
7 8 9 |
# File 'lib/matrix.rb', line 7 def _32 @_32 end |
#_33 ⇒ Object
Returns the value of attribute _33.
7 8 9 |
# File 'lib/matrix.rb', line 7 def _33 @_33 end |
#_34 ⇒ Object
Returns the value of attribute _34.
7 8 9 |
# File 'lib/matrix.rb', line 7 def _34 @_34 end |
#_41 ⇒ Object
Returns the value of attribute _41.
8 9 10 |
# File 'lib/matrix.rb', line 8 def _41 @_41 end |
#_42 ⇒ Object
Returns the value of attribute _42.
8 9 10 |
# File 'lib/matrix.rb', line 8 def _42 @_42 end |
#_43 ⇒ Object
Returns the value of attribute _43.
8 9 10 |
# File 'lib/matrix.rb', line 8 def _43 @_43 end |
#_44 ⇒ Object
Returns the value of attribute _44.
8 9 10 |
# File 'lib/matrix.rb', line 8 def _44 @_44 end |
Class Method Details
.identity ⇒ Object
255 256 257 258 259 260 261 262 263 |
# File 'lib/matrix.rb', line 255 def self.identity identity_matrix = self.new identity_matrix._12 = identity_matrix._13 = identity_matrix._14 = 0 identity_matrix._21 = identity_matrix._23 = identity_matrix._24 = 0 identity_matrix._31 = identity_matrix._32 = identity_matrix._34 = 0 identity_matrix._41 = identity_matrix._42 = identity_matrix._43 = 0 identity_matrix._11 = identity_matrix._22 = identity_matrix._33 = identity_matrix._44 = 1 identity_matrix end |
.look_at_lh(eye_position, look_at_position, up_direction) ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/matrix.rb', line 503 def self.look_at_lh eye_position, look_at_position, up_direction zaxis = (look_at_position - eye_position).normalize xaxis = up_direction.cross(zaxis).normalize yaxis = zaxis.cross xaxis matrix = self.new # set column one matrix._11 = xaxis.x matrix._21 = xaxis.y matrix._31 = xaxis.z matrix._41 = -xaxis.dot(eye_position) # set column two matrix._12 = yaxis.x matrix._22 = yaxis.y matrix._32 = yaxis.z matrix._42 = -yaxis.dot(eye_position) # set column three matrix._13 = zaxis.x matrix._23 = zaxis.y matrix._33 = zaxis.z matrix._43 = -zaxis.dot(eye_position) # set column four matrix._14 = matrix._24 = matrix._34 = 0 matrix._44 = 1 matrix end |
.look_at_rh(eye_position, look_at_position, up_direction) ⇒ Object
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/matrix.rb', line 471 def self.look_at_rh eye_position, look_at_position, up_direction zaxis = (eye_position - look_at_position).normalize xaxis = up_direction.cross(zaxis).normalize yaxis = zaxis.cross xaxis matrix = self.new # set column one matrix._11 = xaxis.x matrix._21 = xaxis.y matrix._31 = xaxis.z matrix._41 = -xaxis.dot(eye_position) # set column two matrix._12 = yaxis.x matrix._22 = yaxis.y matrix._32 = yaxis.z matrix._42 = -yaxis.dot(eye_position) # set column three matrix._13 = zaxis.x matrix._23 = zaxis.y matrix._33 = zaxis.z matrix._43 = -zaxis.dot(eye_position) # set column four matrix._14 = matrix._24 = matrix._34 = 0 matrix._44 = 1 matrix end |
.ortho_off_center_lh(l, r, b, t, zn, zf) ⇒ Object
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/matrix.rb', line 454 def self.ortho_off_center_lh l, r, b, t, zn, zf l = l.to_f r = r.to_f b = b.to_f t = t.to_f zn = zn.to_f zf = zf.to_f m = identity m._11 = 2.0 / (r - l) m._22 = 2.0 / (t - b) m._33 = 1.0 / (zf -zn) m._41 = -1.0 -2.0 *l / (r - l) m._42 = 1.0 + 2.0 * t / (b - t) m._43 = zn / (zn -zf) m end |
.ortho_off_center_rh(l, r, b, t, zn, zf) ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/matrix.rb', line 437 def self.ortho_off_center_rh l, r, b, t, zn, zf l = l.to_f r = r.to_f b = b.to_f t = t.to_f zn = zn.to_f zf = zf.to_f m = identity m._11 = 2.0 / (r - l) m._22 = 2.0 / (t - b) m._33 = 1.0 / (zn -zf) m._41 = -1.0 -2.0 *l / (r - l) m._42 = 1.0 + 2.0 * t / (b - t) m._43 = zn / (zn -zf) m end |
.perspective_fov_lh(fovy, aspect, zn, zf) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/matrix.rb', line 421 def self.perspective_fov_lh fovy, aspect, zn, zf fovy = fovy.to_f aspect = aspect.to_f zn = zn.to_f zf = zf.to_f y_scale = 1.0 / Math.tan(0.5*fovy) x_scale = y_scale / aspect matrix = self.new matrix._11 = x_scale matrix._22 = y_scale matrix._33 = zf/(zf - zn) matrix._34 = 1 matrix._43 = zn*zf/(zn - zf) matrix end |
.perspective_fov_rh(fovy, aspect, zn, zf) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/matrix.rb', line 405 def self.perspective_fov_rh fovy, aspect, zn, zf fovy = fovy.to_f aspect = aspect.to_f zn = zn.to_f zf = zf.to_f y_scale = 1.0 / Math.tan(0.5*fovy) x_scale = y_scale / aspect matrix = self.new matrix._11 = x_scale matrix._22 = y_scale matrix._33 = zf/(zn - zf) matrix._34 = -1 matrix._43 = zn*zf/(zn - zf) matrix end |
.reflection(reflection_plane) ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
# File 'lib/matrix.rb', line 535 def self.reflection reflection_plane reflection_matrix = self.new plane_magnitude = Vector.new(reflection_plane.x, reflection_plane.y, reflection_plane.z, 0).length normalized_plane = reflection_plane / plane_magnitude # row one reflection_matrix._11 = -2 * normalized_plane.x * normalized_plane.x + 1 reflection_matrix._12 = -2 * normalized_plane.y * normalized_plane.x reflection_matrix._13 = -2 * normalized_plane.z * normalized_plane.x reflection_matrix._14 = 0 # row two reflection_matrix._21 = -2 * normalized_plane.x * normalized_plane.y reflection_matrix._22 = -2 * normalized_plane.y * normalized_plane.y + 1 reflection_matrix._23 = -2 * normalized_plane.z * normalized_plane.y reflection_matrix._24 = 0 # row three reflection_matrix._31 = -2 * normalized_plane.x * normalized_plane.z reflection_matrix._32 = -2 * normalized_plane.y * normalized_plane.z reflection_matrix._33 = -2 * normalized_plane.z * normalized_plane.z + 1 reflection_matrix._34 = 0 # row four reflection_matrix._41 = -2 * normalized_plane.x * normalized_plane.w reflection_matrix._42 = -2 * normalized_plane.y * normalized_plane.w reflection_matrix._43 = -2 * normalized_plane.z * normalized_plane.w reflection_matrix._44 = 1 reflection_matrix end |
.rotation(axis, angle) ⇒ Object
651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
# File 'lib/matrix.rb', line 651 def self.rotation axis, angle v = axis.normalize m = identity m._11 = (1.0 - Math.cos(angle)) * v.x * v.x + Math.cos(angle) m._21 = (1.0 - Math.cos(angle)) * v.x * v.y - Math.sin(angle) * v.z m._31 = (1.0 - Math.cos(angle)) * v.x * v.z + Math.sin(angle) * v.y m._12 = (1.0 - Math.cos(angle)) * v.y * v.x + Math.sin(angle) * v.z m._22 = (1.0 - Math.cos(angle)) * v.y * v.y + Math.cos(angle) m._32 = (1.0 - Math.cos(angle)) * v.y * v.z - Math.sin(angle) * v.x m._13 = (1.0 - Math.cos(angle)) * v.z * v.x - Math.sin(angle) * v.y m._23 = (1.0 - Math.cos(angle)) * v.z * v.y + Math.sin(angle) * v.x m._33 = (1.0 - Math.cos(angle)) * v.z * v.z + Math.cos(angle) m end |
.rotation_x(angle) ⇒ Object
618 619 620 621 622 623 624 625 626 627 |
# File 'lib/matrix.rb', line 618 def self.rotation_x angle m = identity sine = Math.sin angle cosine = Math.cos angle m._22 = cosine m._33 = cosine m._23 = sine m._32 = -sine m end |
.rotation_y(angle) ⇒ Object
629 630 631 632 633 634 635 636 637 638 |
# File 'lib/matrix.rb', line 629 def self.rotation_y angle m = identity sine = Math.sin angle cosine = Math.cos angle m._11 = cosine m._33 = cosine m._13 = -sine m._31 = sine m end |
.rotation_z(angle) ⇒ Object
640 641 642 643 644 645 646 647 648 649 |
# File 'lib/matrix.rb', line 640 def self.rotation_z angle m = identity sine = Math.sin angle cosine = Math.cos angle m._11 = cosine m._22 = cosine m._12 = sine m._21 = -sine m end |
.scaling(x, y, z) ⇒ Object
605 606 607 608 609 610 611 612 |
# File 'lib/matrix.rb', line 605 def self.scaling x, y, z scaling_matrix = self.new scaling_matrix._11 = x scaling_matrix._22 = y scaling_matrix._33 = z scaling_matrix._44 = 1 scaling_matrix end |
.shadow(light_position, plane) ⇒ Object
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/matrix.rb', line 568 def self.shadow light_position, plane norm = plane.x * plane.x + plane.y * plane.y + plane.z * plane.z normalized_plane = plane / norm dot = normalized_plane.dot(light_position) m = self.new m._11 = dot - normalized_plane.a * light_position.x m._12 = -normalized_plane.a * light_position.y m._13 = -normalized_plane.a * light_position.z m._14 = -normalized_plane.a * light_position.w m._21 = -normalized_plane.b * light_position.x m._22 = dot - normalized_plane.b * light_position.y m._23 = -normalized_plane.b * light_position.z m._24 = -normalized_plane.b * light_position.w m._31 = -normalized_plane.c * light_position.x m._32 = -normalized_plane.c * light_position.y m._33 = dot - normalized_plane.c * light_position.z m._34 = -normalized_plane.c * light_position.w m._41 = -normalized_plane.d * light_position.x m._42 = -normalized_plane.d * light_position.y m._43 = -normalized_plane.d * light_position.z m._44 = dot - normalized_plane.d * light_position.w m end |
.translation(x, y, z) ⇒ Object
595 596 597 598 599 600 601 602 603 |
# File 'lib/matrix.rb', line 595 def self.translation x, y, z translation_matrix = self.new translation_matrix._11 = translation_matrix._22 = translation_matrix._33 = translation_matrix._44 = 1 #todo: consider simplifying with identity translation_matrix._41 = x translation_matrix._42 = y translation_matrix._43 = z translation_matrix end |
.uniform_scaling(scale) ⇒ Object
614 615 616 |
# File 'lib/matrix.rb', line 614 def self.uniform_scaling scale scaling scale, scale, scale end |
Instance Method Details
#!=(m) ⇒ Object
66 67 68 |
# File 'lib/matrix.rb', line 66 def != m !(self == m) end |
#*(v) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/matrix.rb', line 130 def * v result = self.class.new if self.class == v.class matrix = v result._11 = _11 * matrix._11 + _12 * matrix._21 + _13 * matrix._31 + _14 * matrix._41 result._12 = _11 * matrix._12 + _12 * matrix._22 + _13 * matrix._32 + _14 * matrix._42 result._13 = _11 * matrix._13 + _12 * matrix._23 + _13 * matrix._33 + _14 * matrix._43 result._14 = _11 * matrix._14 + _12 * matrix._24 + _13 * matrix._34 + _14 * matrix._44 result._21 = _21 * matrix._11 + _22 * matrix._21 + _23 * matrix._31 + _24 * matrix._41 result._22 = _21 * matrix._12 + _22 * matrix._22 + _23 * matrix._32 + _24 * matrix._42 result._23 = _21 * matrix._13 + _22 * matrix._23 + _23 * matrix._33 + _24 * matrix._43 result._24 = _21 * matrix._14 + _22 * matrix._24 + _23 * matrix._34 + _24 * matrix._44 result._31 = _31 * matrix._11 + _32 * matrix._21 + _33 * matrix._31 + _34 * matrix._41 result._32 = _31 * matrix._12 + _32 * matrix._22 + _33 * matrix._32 + _34 * matrix._42 result._33 = _31 * matrix._13 + _32 * matrix._23 + _33 * matrix._33 + _34 * matrix._43 result._34 = _31 * matrix._14 + _32 * matrix._24 + _33 * matrix._34 + _34 * matrix._44 result._41 = _41 * matrix._11 + _42 * matrix._21 + _43 * matrix._31 + _44 * matrix._41 result._42 = _41 * matrix._12 + _42 * matrix._22 + _43 * matrix._32 + _44 * matrix._42 result._43 = _41 * matrix._13 + _42 * matrix._23 + _43 * matrix._33 + _44 * matrix._43 result._44 = _41 * matrix._14 + _42 * matrix._24 + _43 * matrix._34 + _44 * matrix._44 elsif Vector == v.class vec = v transformed_vector = Vector.new transformed_vector.x = _11 * vec.x + _21 * vec.y + _31 * vec.z + _41 transformed_vector.y = _12 * vec.x + _22 * vec.y + _32 * vec.z + _42 transformed_vector.z = _13 * vec.x + _23 * vec.y + _33 * vec.z + _43 transformed_vector.w = _14 * vec.x + _24 * vec.y + _34 * vec.z + _44 return transformed_vector else scalar = v result._11 = _11 * scalar result._12 = _12 * scalar result._13 = _13 * scalar result._14 = _14 * scalar result._21 = _21 * scalar result._22 = _22 * scalar result._23 = _23 * scalar result._24 = _24 * scalar result._31 = _31 * scalar result._32 = _32 * scalar result._33 = _33 * scalar result._34 = _34 * scalar result._41 = _41 * scalar result._42 = _42 * scalar result._43 = _43 * scalar result._44 = _44 * scalar end result end |
#+(mat) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/matrix.rb', line 78 def + mat sum = self.class.new sum._11 = _11 + mat._11 sum._12 = _12 + mat._12 sum._13 = _13 + mat._13 sum._14 = _14 + mat._14 sum._21 = _21 + mat._21 sum._22 = _22 + mat._22 sum._23 = _23 + mat._23 sum._24 = _24 + mat._24 sum._31 = _31 + mat._31 sum._32 = _32 + mat._32 sum._33 = _33 + mat._33 sum._34 = _34 + mat._34 sum._41 = _41 + mat._41 sum._42 = _42 + mat._42 sum._43 = _43 + mat._43 sum._44 = _44 + mat._44 sum end |
#+@ ⇒ Object
70 71 72 |
# File 'lib/matrix.rb', line 70 def +@ self * 1 end |
#-(mat) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/matrix.rb', line 104 def - mat sum = self.class.new sum._11 = _11 - mat._11 sum._12 = _12 - mat._12 sum._13 = _13 - mat._13 sum._14 = _14 - mat._14 sum._21 = _21 - mat._21 sum._22 = _22 - mat._22 sum._23 = _23 - mat._23 sum._24 = _24 - mat._24 sum._31 = _31 - mat._31 sum._32 = _32 - mat._32 sum._33 = _33 - mat._33 sum._34 = _34 - mat._34 sum._41 = _41 - mat._41 sum._42 = _42 - mat._42 sum._43 = _43 - mat._43 sum._44 = _44 - mat._44 sum end |
#-@ ⇒ Object
74 75 76 |
# File 'lib/matrix.rb', line 74 def -@ self * -1 end |
#/(v) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/matrix.rb', line 186 def / v if self.class == v.class self * v.inverse elsif Vector == v.class raise 'dividing matrices by vectors not currently supported' else result = self.class.new scalar = v result._11 = _11 / scalar result._12 = _12 / scalar result._13 = _13 / scalar result._14 = _14 / scalar result._21 = _21 / scalar result._22 = _22 / scalar result._23 = _23 / scalar result._24 = _24 / scalar result._31 = _31 / scalar result._32 = _32 / scalar result._33 = _33 / scalar result._34 = _34 / scalar result._41 = _41 / scalar result._42 = _42 / scalar result._43 = _43 / scalar result._44 = _44 / scalar result end end |
#==(m) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/matrix.rb', line 57 def == m a = to_a b = m.to_a for i in 0..15 return false unless Geo3d::Utils.float_cmp a[i], b[i] end true end |
#[](x, y) ⇒ Object
49 50 51 |
# File 'lib/matrix.rb', line 49 def [] x, y to_a[4*x + y] end |
#[]=(x, y, v) ⇒ Object
53 54 55 |
# File 'lib/matrix.rb', line 53 def []= x, y, v send (%w{_11 _12 _13 _14 _21 _22 _23 _24 _31 _32 _33 _34 _41 _42 _43 _44}[4*x + y] + '=').to_sym, v end |
#determinant ⇒ Object
265 266 267 |
# File 'lib/matrix.rb', line 265 def determinant inverse(true).last end |
#identity? ⇒ Boolean
227 228 229 |
# File 'lib/matrix.rb', line 227 def identity? self == self.class.identity end |
#inverse(with_determinant = false) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/matrix.rb', line 269 def inverse with_determinant = false mat = to_a dst = Array.new 16 tmp = Array.new 12 src = Array.new 16 for i in 0..3 src[i] = mat[i*4] src[i + 4] = mat[i*4 + 1] src[i + 8] = mat[i*4 + 2] src[i + 12] = mat[i*4 + 3] end # calculate pairs for first 8 elements (cofactors) tmp[0] = src[10] * src[15] tmp[1] = src[11] * src[14] tmp[2] = src[9] * src[15] tmp[3] = src[11] * src[13] tmp[4] = src[9] * src[14] tmp[5] = src[10] * src[13] tmp[6] = src[8] * src[15] tmp[7] = src[11] * src[12] tmp[8] = src[8] * src[14] tmp[9] = src[10] * src[12] tmp[10] = src[8] * src[13] tmp[11] = src[9] * src[12] # calculate first 8 elements (cofactors) dst[0] = tmp[0]*src[5] + tmp[3]*src[6] + tmp[4]*src[7] dst[0] -= tmp[1]*src[5] + tmp[2]*src[6] + tmp[5]*src[7] dst[1] = tmp[1]*src[4] + tmp[6]*src[6] + tmp[9]*src[7] dst[1] -= tmp[0]*src[4] + tmp[7]*src[6] + tmp[8]*src[7] dst[2] = tmp[2]*src[4] + tmp[7]*src[5] + tmp[10]*src[7] dst[2] -= tmp[3]*src[4] + tmp[6]*src[5] + tmp[11]*src[7] dst[3] = tmp[5]*src[4] + tmp[8]*src[5] + tmp[11]*src[6] dst[3] -= tmp[4]*src[4] + tmp[9]*src[5] + tmp[10]*src[6] dst[4] = tmp[1]*src[1] + tmp[2]*src[2] + tmp[5]*src[3] dst[4] -= tmp[0]*src[1] + tmp[3]*src[2] + tmp[4]*src[3] dst[5] = tmp[0]*src[0] + tmp[7]*src[2] + tmp[8]*src[3] dst[5] -= tmp[1]*src[0] + tmp[6]*src[2] + tmp[9]*src[3] dst[6] = tmp[3]*src[0] + tmp[6]*src[1] + tmp[11]*src[3] dst[6] -= tmp[2]*src[0] + tmp[7]*src[1] + tmp[10]*src[3] dst[7] = tmp[4]*src[0] + tmp[9]*src[1] + tmp[10]*src[2] dst[7] -= tmp[5]*src[0] + tmp[8]*src[1] + tmp[11]*src[2] # calculate pairs for second 8 elements (cofactors) tmp[0] = src[2]*src[7] tmp[1] = src[3]*src[6] tmp[2] = src[1]*src[7] tmp[3] = src[3]*src[5] tmp[4] = src[1]*src[6] tmp[5] = src[2]*src[5] tmp[6] = src[0]*src[7] tmp[7] = src[3]*src[4] tmp[8] = src[0]*src[6] tmp[9] = src[2]*src[4] tmp[10] = src[0]*src[5] tmp[11] = src[1]*src[4] # calculate second 8 elements (cofactors) dst[8] = tmp[0]*src[13] + tmp[3]*src[14] + tmp[4]*src[15] dst[8] -= tmp[1]*src[13] + tmp[2]*src[14] + tmp[5]*src[15] dst[9] = tmp[1]*src[12] + tmp[6]*src[14] + tmp[9]*src[15] dst[9] -= tmp[0]*src[12] + tmp[7]*src[14] + tmp[8]*src[15] dst[10] = tmp[2]*src[12] + tmp[7]*src[13] + tmp[10]*src[15] dst[10]-= tmp[3]*src[12] + tmp[6]*src[13] + tmp[11]*src[15] dst[11] = tmp[5]*src[12] + tmp[8]*src[13] + tmp[11]*src[14] dst[11]-= tmp[4]*src[12] + tmp[9]*src[13] + tmp[10]*src[14] dst[12] = tmp[2]*src[10] + tmp[5]*src[11] + tmp[1]*src[9] dst[12]-= tmp[4]*src[11] + tmp[0]*src[9] + tmp[3]*src[10] dst[13] = tmp[8]*src[11] + tmp[0]*src[8] + tmp[7]*src[10] dst[13]-= tmp[6]*src[10] + tmp[9]*src[11] + tmp[1]*src[8] dst[14] = tmp[6]*src[9] + tmp[11]*src[11] + tmp[3]*src[8] dst[14]-= tmp[10]*src[11] + tmp[2]*src[8] + tmp[7]*src[9] dst[15] = tmp[10]*src[10] + tmp[4]*src[8] + tmp[9]*src[9] dst[15]-= tmp[8]*src[9] + tmp[11]*src[10] + tmp[5]*src[8] # calculate determinant det=src[0]*dst[0]+src[1]*dst[1]+src[2]*dst[2]+src[3]*dst[3] # calculate matrix inverse det = 1.0/det for j in 0..15 dst[j] *= det end inverted_matrix = self.class.new *dst if with_determinant [inverted_matrix, det] else inverted_matrix end end |
#print ⇒ Object
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/matrix.rb', line 386 def print puts "_11: #{_11}\n" puts "_12: #{_12}\n" puts "_13: #{_13}\n" puts "_14: #{_14}\n" puts "_21: #{_21}\n" puts "_22: #{_22}\n" puts "_23: #{_23}\n" puts "_24: #{_24}\n" puts "_31: #{_31}\n" puts "_32: #{_32}\n" puts "_33: #{_33}\n" puts "_34: #{_34}\n" puts "_41: #{_41}\n" puts "_42: #{_42}\n" puts "_43: #{_43}\n" puts "_44: #{_44}\n" end |
#rotation_component ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/matrix.rb', line 239 def rotation_component scaling = scaling_component return nil if scaling.x.zero? || scaling.y.zero? || scaling.z.zero? m = Matrix.new m._11=_11 / scaling.x m._12=_12/scaling.x m._13=_13/scaling.x m._21=_21/scaling.y m._22=_22/scaling.y m._23=_23/scaling.y m._31=_31/scaling.z m._32=_32/scaling.z m._33=_33/scaling.z Quaternion.from_matrix m end |
#scaling_component ⇒ Object
235 236 237 |
# File 'lib/matrix.rb', line 235 def scaling_component Vector.new Vector.new(_11, _12, _13).length, Vector.new(_21, _22, _23).length, Vector.new(_31, _32, _33).length end |
#to_a ⇒ Object
45 46 47 |
# File 'lib/matrix.rb', line 45 def to_a [_11, _12, _13, _14, _21, _22, _23, _24, _31, _32, _33, _34, _41, _42, _43, _44] end |
#transform(vec) ⇒ Object
223 224 225 |
# File 'lib/matrix.rb', line 223 def transform vec self * vec end |
#transform_coord(vec) ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/matrix.rb', line 214 def transform_coord vec norm =_14 * vec.x + _24 * vec.y + _34 * vec.z + _44 transformed_vector = self * vec transformed_vector.x /= norm transformed_vector.y /= norm transformed_vector.z /= norm transformed_vector end |
#translation_component ⇒ Object
231 232 233 |
# File 'lib/matrix.rb', line 231 def translation_component Vector.new _41, _42, _43 end |
#transpose ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/matrix.rb', line 365 def transpose transposed_matrix = self.class.new transposed_matrix._11 = _11 transposed_matrix._12 = _21 transposed_matrix._13 = _31 transposed_matrix._14 = _41 transposed_matrix._21 = _12 transposed_matrix._22 = _22 transposed_matrix._23 = _32 transposed_matrix._24 = _42 transposed_matrix._31 = _13 transposed_matrix._32 = _23 transposed_matrix._33 = _33 transposed_matrix._34 = _43 transposed_matrix._41 = _14 transposed_matrix._42 = _24 transposed_matrix._43 = _34 transposed_matrix._44 = _44 transposed_matrix end |