Class: Geo3d::Matrix
- Inherits:
-
Object
- Object
- Geo3d::Matrix
- Defined in:
- lib/geo3d/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
- .gl_frustum(l, r, bottom, t, zn, zf) ⇒ Object
- .gl_ortho(l, r, b, t, zn, zf) ⇒ Object
- .glu_perspective_degrees(fovy, aspect, zn, zf) ⇒ Object
- .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
- .viewport(x, y, width, height) ⇒ 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
- #col(i) ⇒ Object
- #determinant ⇒ Object
- #identity? ⇒ Boolean
-
#initialize(*args) ⇒ Matrix
constructor
A new instance of Matrix.
- #inverse(with_determinant = false) ⇒ Object
- #is_rotation_transform? ⇒ Boolean
- #orthogonal? ⇒ Boolean
- #print ⇒ Object
- #rotation_component ⇒ Object
- #row(i) ⇒ Object
- #scaling_component ⇒ Object
- #set_col(i, v) ⇒ Object
- #set_row(i, v) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #trace ⇒ Object
- #transform(vec) ⇒ Object
- #transform_coord(vec) ⇒ Object
- #translation_component ⇒ Object
- #transpose ⇒ Object
Constructor Details
#initialize(*args) ⇒ Matrix
Returns a new instance of Matrix.
8 9 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 |
# File 'lib/geo3d/matrix.rb', line 8 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.
3 4 5 |
# File 'lib/geo3d/matrix.rb', line 3 def _11 @_11 end |
#_12 ⇒ Object
Returns the value of attribute _12.
3 4 5 |
# File 'lib/geo3d/matrix.rb', line 3 def _12 @_12 end |
#_13 ⇒ Object
Returns the value of attribute _13.
3 4 5 |
# File 'lib/geo3d/matrix.rb', line 3 def _13 @_13 end |
#_14 ⇒ Object
Returns the value of attribute _14.
3 4 5 |
# File 'lib/geo3d/matrix.rb', line 3 def _14 @_14 end |
#_21 ⇒ Object
Returns the value of attribute _21.
4 5 6 |
# File 'lib/geo3d/matrix.rb', line 4 def _21 @_21 end |
#_22 ⇒ Object
Returns the value of attribute _22.
4 5 6 |
# File 'lib/geo3d/matrix.rb', line 4 def _22 @_22 end |
#_23 ⇒ Object
Returns the value of attribute _23.
4 5 6 |
# File 'lib/geo3d/matrix.rb', line 4 def _23 @_23 end |
#_24 ⇒ Object
Returns the value of attribute _24.
4 5 6 |
# File 'lib/geo3d/matrix.rb', line 4 def _24 @_24 end |
#_31 ⇒ Object
Returns the value of attribute _31.
5 6 7 |
# File 'lib/geo3d/matrix.rb', line 5 def _31 @_31 end |
#_32 ⇒ Object
Returns the value of attribute _32.
5 6 7 |
# File 'lib/geo3d/matrix.rb', line 5 def _32 @_32 end |
#_33 ⇒ Object
Returns the value of attribute _33.
5 6 7 |
# File 'lib/geo3d/matrix.rb', line 5 def _33 @_33 end |
#_34 ⇒ Object
Returns the value of attribute _34.
5 6 7 |
# File 'lib/geo3d/matrix.rb', line 5 def _34 @_34 end |
#_41 ⇒ Object
Returns the value of attribute _41.
6 7 8 |
# File 'lib/geo3d/matrix.rb', line 6 def _41 @_41 end |
#_42 ⇒ Object
Returns the value of attribute _42.
6 7 8 |
# File 'lib/geo3d/matrix.rb', line 6 def _42 @_42 end |
#_43 ⇒ Object
Returns the value of attribute _43.
6 7 8 |
# File 'lib/geo3d/matrix.rb', line 6 def _43 @_43 end |
#_44 ⇒ Object
Returns the value of attribute _44.
6 7 8 |
# File 'lib/geo3d/matrix.rb', line 6 def _44 @_44 end |
Class Method Details
.gl_frustum(l, r, bottom, t, zn, zf) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/geo3d/matrix.rb', line 458 def self.gl_frustum l, r, bottom, t, zn, zf l = l.to_f r = r.to_f bottom = bottom.to_f t = t.to_f zn = zn.to_f zf = zf.to_f a = (r+l) / (r-l) b = (t+bottom) / (t-bottom) c = -(zf + zn) / (zf - zn) d = -(2 * zf * zn) / (zf - zn) matrix = self.new matrix._11 = 2.0 * zn / (r-l) matrix._31 = a matrix._22 = 2.0 * zn / (t-bottom) matrix._32 = b matrix._33 = c matrix._43 = d matrix._34 = -1.0 matrix end |
.gl_ortho(l, r, b, t, zn, zf) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/geo3d/matrix.rb', line 513 def self.gl_ortho 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 matrix = self.new matrix._11 = 2.0 / (r-l) matrix._22 = 2.0 / (t-b) matrix._33 = -2.0 / (zf - zn) matrix._41 = -(r+l) / (r-l) matrix._42 = -(t+b) / (t-b) matrix._43 = -(zf+zn) / (zf-zn) matrix._44 = 1.0 matrix end |
.glu_perspective_degrees(fovy, aspect, zn, zf) ⇒ Object
449 450 451 452 453 454 455 456 |
# File 'lib/geo3d/matrix.rb', line 449 def self.glu_perspective_degrees fovy, aspect, zn, zf fovy = fovy.to_f aspect = aspect.to_f zn = zn.to_f zf = zf.to_f range = zn*Math.tan(Geo3d::Utils.to_radians(fovy/2.0)) self.gl_frustum -range*aspect, range*aspect, -range, range, zn, zf end |
.identity ⇒ Object
283 284 285 286 287 288 289 290 291 |
# File 'lib/geo3d/matrix.rb', line 283 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
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/geo3d/matrix.rb', line 597 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
565 566 567 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 593 594 595 |
# File 'lib/geo3d/matrix.rb', line 565 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
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/geo3d/matrix.rb', line 548 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
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/geo3d/matrix.rb', line 531 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
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/geo3d/matrix.rb', line 497 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
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/geo3d/matrix.rb', line 481 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
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/geo3d/matrix.rb', line 634 def self.reflection reflection_plane reflection_plane = Geo3d::Vector.new *reflection_plane.to_a 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
752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/geo3d/matrix.rb', line 752 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
719 720 721 722 723 724 725 726 727 728 |
# File 'lib/geo3d/matrix.rb', line 719 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
730 731 732 733 734 735 736 737 738 739 |
# File 'lib/geo3d/matrix.rb', line 730 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
741 742 743 744 745 746 747 748 749 750 |
# File 'lib/geo3d/matrix.rb', line 741 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
706 707 708 709 710 711 712 713 |
# File 'lib/geo3d/matrix.rb', line 706 def self.scaling x, y, z scaling_matrix = self.new scaling_matrix._11 = x.to_f scaling_matrix._22 = y.to_f scaling_matrix._33 = z.to_f scaling_matrix._44 = 1.0 scaling_matrix end |
.shadow(light_position, plane) ⇒ Object
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/geo3d/matrix.rb', line 668 def self.shadow light_position, plane plane = Geo3d::Vector.new *plane.to_a 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
696 697 698 699 700 701 702 703 704 |
# File 'lib/geo3d/matrix.rb', line 696 def self.translation x, y, z translation_matrix = self.new translation_matrix._11 = translation_matrix._22 = translation_matrix._33 = translation_matrix._44 = 1.0 #todo: consider simplifying with identity translation_matrix._41 = x.to_f translation_matrix._42 = y.to_f translation_matrix._43 = z.to_f translation_matrix end |
.uniform_scaling(scale) ⇒ Object
715 716 717 |
# File 'lib/geo3d/matrix.rb', line 715 def self.uniform_scaling scale scaling scale, scale, scale end |
.viewport(x, y, width, height) ⇒ Object
630 631 632 |
# File 'lib/geo3d/matrix.rb', line 630 def self. x, y, width, height self.scaling(width.to_f / 2.0, height.to_f / 2.0, 0.5) * self.translation(x.to_f + width.to_f / 2.0, y.to_f + height.to_f / 2.0, 0.5) end |
Instance Method Details
#!=(m) ⇒ Object
90 91 92 |
# File 'lib/geo3d/matrix.rb', line 90 def != m !(self == m) end |
#*(v) ⇒ Object
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 185 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 213 214 215 216 217 |
# File 'lib/geo3d/matrix.rb', line 154 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 * vec.w transformed_vector.y = _12 * vec.x + _22 * vec.y + _32 * vec.z + _42 * vec.w transformed_vector.z = _13 * vec.x + _23 * vec.y + _33 * vec.z + _43 * vec.w transformed_vector.w = _14 * vec.x + _24 * vec.y + _34 * vec.z + _44 * vec.w return transformed_vector elsif Triangle == v.class tri = v transformed_tri = Triangle.new transformed_tri.a = self * tri.a transformed_tri.b = self * tri.b transformed_tri.c = self * tri.c return transformed_tri elsif Array == v.class return v.map { |i| self * i } 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/geo3d/matrix.rb', line 102 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
94 95 96 |
# File 'lib/geo3d/matrix.rb', line 94 def +@ self * 1 end |
#-(mat) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/geo3d/matrix.rb', line 128 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
98 99 100 |
# File 'lib/geo3d/matrix.rb', line 98 def -@ self * -1 end |
#/(v) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/geo3d/matrix.rb', line 219 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
81 82 83 84 85 86 87 88 |
# File 'lib/geo3d/matrix.rb', line 81 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
47 48 49 |
# File 'lib/geo3d/matrix.rb', line 47 def [] x, y to_a[4*x + y] end |
#[]=(x, y, v) ⇒ Object
51 52 53 |
# File 'lib/geo3d/matrix.rb', line 51 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 |
#col(i) ⇒ Object
61 62 63 64 65 |
# File 'lib/geo3d/matrix.rb', line 61 def col i if i >= 0 && i <= 3 Vector.new self[i, 0], self[i, 1], self [i, 2], self[i, 3] end end |
#determinant ⇒ Object
293 294 295 |
# File 'lib/geo3d/matrix.rb', line 293 def determinant inverse(true).last end |
#identity? ⇒ Boolean
255 256 257 |
# File 'lib/geo3d/matrix.rb', line 255 def identity? self == self.class.identity end |
#inverse(with_determinant = false) ⇒ Object
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/geo3d/matrix.rb', line 309 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 inverse_det = 1.0/det for j in 0..15 dst[j] *= inverse_det end inverted_matrix = self.class.new *dst if with_determinant [inverted_matrix, det] else inverted_matrix end end |
#is_rotation_transform? ⇒ Boolean
305 306 307 |
# File 'lib/geo3d/matrix.rb', line 305 def is_rotation_transform? orthogonal? && Geo3d::Utils.float_cmp(determinant, 1.0) end |
#orthogonal? ⇒ Boolean
301 302 303 |
# File 'lib/geo3d/matrix.rb', line 301 def orthogonal? inverse == transpose end |
#print ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/geo3d/matrix.rb', line 426 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
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/geo3d/matrix.rb', line 267 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 |
#row(i) ⇒ Object
55 56 57 58 59 |
# File 'lib/geo3d/matrix.rb', line 55 def row i if i >= 0 && i <= 3 Vector.new self[0, i], self[1, i], self[2, i], self[3, i] end end |
#scaling_component ⇒ Object
263 264 265 |
# File 'lib/geo3d/matrix.rb', line 263 def scaling_component Vector.new Vector.new(_11, _12, _13).length, Vector.new(_21, _22, _23).length, Vector.new(_31, _32, _33).length end |
#set_col(i, v) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/geo3d/matrix.rb', line 74 def set_col i, v self[i, 0] = v.x self[i, 1] = v.y self[i, 2] = v.z self[i, 3] = v.w end |
#set_row(i, v) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/geo3d/matrix.rb', line 67 def set_row i, v self[0, i] = v.x self[1, i] = v.y self[2, i] = v.z self[3, i] = v.w end |
#to_a ⇒ Object
43 44 45 |
# File 'lib/geo3d/matrix.rb', line 43 def to_a [_11, _12, _13, _14, _21, _22, _23, _24, _31, _32, _33, _34, _41, _42, _43, _44] end |
#to_s ⇒ Object
445 446 447 |
# File 'lib/geo3d/matrix.rb', line 445 def to_s (0..3).to_a.map { |i| row(i).to_s }.join "\n" end |
#trace ⇒ Object
297 298 299 |
# File 'lib/geo3d/matrix.rb', line 297 def trace _11 + _22 + _33 + _44 end |
#transform(vec) ⇒ Object
251 252 253 |
# File 'lib/geo3d/matrix.rb', line 251 def transform vec self * vec end |
#transform_coord(vec) ⇒ Object
247 248 249 |
# File 'lib/geo3d/matrix.rb', line 247 def transform_coord vec self * Vector.new(vec.x, vec.y, vec.z, 1.0) end |
#translation_component ⇒ Object
259 260 261 |
# File 'lib/geo3d/matrix.rb', line 259 def translation_component Vector.new _41, _42, _43 end |
#transpose ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/geo3d/matrix.rb', line 405 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 |