gmath3D 0.2.5 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -5
- data/Gemfile +5 -5
- data/LICENSE.txt +20 -20
- data/README.rdoc +19 -19
- data/Rakefile +45 -45
- data/VERSION +1 -1
- data/gmath3D.gemspec +79 -79
- data/lib/box.rb +146 -145
- data/lib/ellipse.rb +11 -11
- data/lib/ext.rb +82 -82
- data/lib/finite_line.rb +244 -244
- data/lib/geom.rb +20 -20
- data/lib/gmath3D.rb +22 -22
- data/lib/line.rb +122 -122
- data/lib/plane.rb +131 -131
- data/lib/polyline.rb +73 -73
- data/lib/quat.rb +170 -170
- data/lib/rectangle.rb +155 -155
- data/lib/tri_mesh.rb +258 -258
- data/lib/triangle.rb +281 -281
- data/lib/util.rb +36 -36
- data/lib/vector3.rb +231 -227
- data/test/helper.rb +15 -15
- data/test/test_box.rb +172 -167
- data/test/test_ellipse.rb +55 -55
- data/test/test_finite_line.rb +306 -306
- data/test/test_geom.rb +17 -17
- data/test/test_line.rb +146 -146
- data/test/test_matrix_util.rb +84 -84
- data/test/test_plane.rb +200 -200
- data/test/test_polyline.rb +93 -93
- data/test/test_quat.rb +144 -144
- data/test/test_rectangle.rb +184 -184
- data/test/test_tri_mesh.rb +186 -186
- data/test/test_triangle.rb +318 -318
- data/test/test_util.rb +88 -88
- data/test/test_vector3.rb +453 -439
- metadata +8 -8
    
        data/lib/polyline.rb
    CHANGED
    
    | @@ -1,73 +1,73 @@ | |
| 1 | 
            -
            require 'gmath3D'
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            module GMath3D
         | 
| 4 | 
            -
              #
         | 
| 5 | 
            -
              # Polyline represents a closed or open polyline on 3D space.
         | 
| 6 | 
            -
              #
         | 
| 7 | 
            -
              class Polyline < Geom
         | 
| 8 | 
            -
                public
         | 
| 9 | 
            -
                attr_accessor :vertices
         | 
| 10 | 
            -
                attr_accessor :is_open
         | 
| 11 | 
            -
             | 
| 12 | 
            -
                include BoxAvailable
         | 
| 13 | 
            -
             | 
| 14 | 
            -
                # [Input]
         | 
| 15 | 
            -
                #  _vertices_ should be Array of Vector3.
         | 
| 16 | 
            -
                # [Output]
         | 
| 17 | 
            -
                #  return new instance of Polyline.
         | 
| 18 | 
            -
                def initialize(vertices = [], is_open = true)
         | 
| 19 | 
            -
                  Util3D.check_arg_type(Array, vertices)
         | 
| 20 | 
            -
                  super()
         | 
| 21 | 
            -
                  @vertices = vertices
         | 
| 22 | 
            -
                  @is_open  = is_open
         | 
| 23 | 
            -
                end
         | 
| 24 | 
            -
             | 
| 25 | 
            -
                def initialize_copy( original_obj )
         | 
| 26 | 
            -
                  @vertices = Array.new(original_obj.vertices.size)
         | 
| 27 | 
            -
                  for i in 0..@vertices.size-1
         | 
| 28 | 
            -
                    @vertices[i] = original_obj.vertices[i].dup
         | 
| 29 | 
            -
                  end
         | 
| 30 | 
            -
                  @is_open = original_obj.is_open
         | 
| 31 | 
            -
                end
         | 
| 32 | 
            -
             | 
| 33 | 
            -
                # [Input]
         | 
| 34 | 
            -
                #  _rhs_ is Polyline.
         | 
| 35 | 
            -
                # [Output]
         | 
| 36 | 
            -
                #  return true if rhs equals myself.
         | 
| 37 | 
            -
                def ==(rhs)
         | 
| 38 | 
            -
                  return false if rhs == nil
         | 
| 39 | 
            -
                  return false if( !rhs.kind_of?(Polyline) )
         | 
| 40 | 
            -
                  return false if( self.is_open != rhs.is_open )
         | 
| 41 | 
            -
                  return false if(@vertices.size != rhs.vertices.size)
         | 
| 42 | 
            -
                  for i in 0..(@vertices.size-1)
         | 
| 43 | 
            -
                    return false if( self.vertices[i] != rhs.vertices[i])
         | 
| 44 | 
            -
                  end
         | 
| 45 | 
            -
                  return true
         | 
| 46 | 
            -
                end
         | 
| 47 | 
            -
             | 
| 48 | 
            -
                def to_s
         | 
| 49 | 
            -
                  str = "Polyline["
         | 
| 50 | 
            -
                  vertices.each do |vertex|
         | 
| 51 | 
            -
                    str += vertex.to_element_s + ", "
         | 
| 52 | 
            -
                  end
         | 
| 53 | 
            -
                  str.slice!(str.length - 2, 2) if(vertices.size > 0)
         | 
| 54 | 
            -
                  str += "] "
         | 
| 55 | 
            -
                  str += "open" if(@is_open)
         | 
| 56 | 
            -
                  str += "closed" if(!@is_open)
         | 
| 57 | 
            -
                  return str
         | 
| 58 | 
            -
                end
         | 
| 59 | 
            -
             | 
| 60 | 
            -
                # [Output]
         | 
| 61 | 
            -
                #  return center point as Vector3.
         | 
| 62 | 
            -
                def center
         | 
| 63 | 
            -
                  center = Vector3.new()
         | 
| 64 | 
            -
                  @vertices.each do |vertex|
         | 
| 65 | 
            -
                    center += vertex
         | 
| 66 | 
            -
                  end
         | 
| 67 | 
            -
                  center /= @vertices.size
         | 
| 68 | 
            -
                  return center
         | 
| 69 | 
            -
                end
         | 
| 70 | 
            -
             | 
| 71 | 
            -
              end
         | 
| 72 | 
            -
            end
         | 
| 73 | 
            -
             | 
| 1 | 
            +
            require 'gmath3D'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module GMath3D
         | 
| 4 | 
            +
              #
         | 
| 5 | 
            +
              # Polyline represents a closed or open polyline on 3D space.
         | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              class Polyline < Geom
         | 
| 8 | 
            +
                public
         | 
| 9 | 
            +
                attr_accessor :vertices
         | 
| 10 | 
            +
                attr_accessor :is_open
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                include BoxAvailable
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                # [Input]
         | 
| 15 | 
            +
                #  _vertices_ should be Array of Vector3.
         | 
| 16 | 
            +
                # [Output]
         | 
| 17 | 
            +
                #  return new instance of Polyline.
         | 
| 18 | 
            +
                def initialize(vertices = [], is_open = true)
         | 
| 19 | 
            +
                  Util3D.check_arg_type(Array, vertices)
         | 
| 20 | 
            +
                  super()
         | 
| 21 | 
            +
                  @vertices = vertices
         | 
| 22 | 
            +
                  @is_open  = is_open
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def initialize_copy( original_obj )
         | 
| 26 | 
            +
                  @vertices = Array.new(original_obj.vertices.size)
         | 
| 27 | 
            +
                  for i in 0..@vertices.size-1
         | 
| 28 | 
            +
                    @vertices[i] = original_obj.vertices[i].dup
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                  @is_open = original_obj.is_open
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                # [Input]
         | 
| 34 | 
            +
                #  _rhs_ is Polyline.
         | 
| 35 | 
            +
                # [Output]
         | 
| 36 | 
            +
                #  return true if rhs equals myself.
         | 
| 37 | 
            +
                def ==(rhs)
         | 
| 38 | 
            +
                  return false if rhs == nil
         | 
| 39 | 
            +
                  return false if( !rhs.kind_of?(Polyline) )
         | 
| 40 | 
            +
                  return false if( self.is_open != rhs.is_open )
         | 
| 41 | 
            +
                  return false if(@vertices.size != rhs.vertices.size)
         | 
| 42 | 
            +
                  for i in 0..(@vertices.size-1)
         | 
| 43 | 
            +
                    return false if( self.vertices[i] != rhs.vertices[i])
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                  return true
         | 
| 46 | 
            +
                end
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                def to_s
         | 
| 49 | 
            +
                  str = "Polyline["
         | 
| 50 | 
            +
                  vertices.each do |vertex|
         | 
| 51 | 
            +
                    str += vertex.to_element_s + ", "
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
                  str.slice!(str.length - 2, 2) if(vertices.size > 0)
         | 
| 54 | 
            +
                  str += "] "
         | 
| 55 | 
            +
                  str += "open" if(@is_open)
         | 
| 56 | 
            +
                  str += "closed" if(!@is_open)
         | 
| 57 | 
            +
                  return str
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                # [Output]
         | 
| 61 | 
            +
                #  return center point as Vector3.
         | 
| 62 | 
            +
                def center
         | 
| 63 | 
            +
                  center = Vector3.new()
         | 
| 64 | 
            +
                  @vertices.each do |vertex|
         | 
| 65 | 
            +
                    center += vertex
         | 
| 66 | 
            +
                  end
         | 
| 67 | 
            +
                  center /= @vertices.size
         | 
| 68 | 
            +
                  return center
         | 
| 69 | 
            +
                end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
            end
         | 
| 73 | 
            +
             | 
    
        data/lib/quat.rb
    CHANGED
    
    | @@ -1,170 +1,170 @@ | |
| 1 | 
            -
            require 'gmath3D'
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            module GMath3D
         | 
| 4 | 
            -
              #
         | 
| 5 | 
            -
              # Quat represents quaternion.
         | 
| 6 | 
            -
              #
         | 
| 7 | 
            -
              class Quat
         | 
| 8 | 
            -
            public
         | 
| 9 | 
            -
                attr_accessor :x
         | 
| 10 | 
            -
                attr_accessor :y
         | 
| 11 | 
            -
                attr_accessor :z
         | 
| 12 | 
            -
                attr_accessor :w
         | 
| 13 | 
            -
             | 
| 14 | 
            -
                # [Input]
         | 
| 15 | 
            -
                #  _x_, _y_, _z_, _w_should be Numeric.
         | 
| 16 | 
            -
                # [Output]
         | 
| 17 | 
            -
                #  return new instance of Quat.
         | 
| 18 | 
            -
                def initialize(x=0.0,y=0.0,z=0.0,w=0.0)
         | 
| 19 | 
            -
                  Util3D.check_arg_type(Numeric, x)
         | 
| 20 | 
            -
                  Util3D.check_arg_type(Numeric, y)
         | 
| 21 | 
            -
                  Util3D.check_arg_type(Numeric, z)
         | 
| 22 | 
            -
                  Util3D.check_arg_type(Numeric, w)
         | 
| 23 | 
            -
                  super()
         | 
| 24 | 
            -
                  @x = x
         | 
| 25 | 
            -
                  @y = y
         | 
| 26 | 
            -
                  @z = z
         | 
| 27 | 
            -
                  @w = w
         | 
| 28 | 
            -
                end
         | 
| 29 | 
            -
             | 
| 30 | 
            -
                # [Input]
         | 
| 31 | 
            -
                #  _axsi_ should be Vector3 and _angle_ should be Numeric.
         | 
| 32 | 
            -
                # [Output]
         | 
| 33 | 
            -
                #  return new instance of Quat.
         | 
| 34 | 
            -
                def self.from_axis(axis, angle)
         | 
| 35 | 
            -
                  Util3D.check_arg_type(Vector3, axis)
         | 
| 36 | 
            -
                  Util3D.check_arg_type(Numeric, angle)
         | 
| 37 | 
            -
                  s = Math.sin(0.5*angle)
         | 
| 38 | 
            -
                  x = s * axis.x
         | 
| 39 | 
            -
                  y = s * axis.y
         | 
| 40 | 
            -
                  z = s * axis.z
         | 
| 41 | 
            -
                  w = Math.cos(0.5*angle)
         | 
| 42 | 
            -
                  return Quat.new(x,y,z,w)
         | 
| 43 | 
            -
                end
         | 
| 44 | 
            -
             | 
| 45 | 
            -
                # [Input]
         | 
| 46 | 
            -
                #  _matrix_ should be Matrix which row and column size are 3.
         | 
| 47 | 
            -
                # [Output]
         | 
| 48 | 
            -
                #  return new instance of Quat.
         | 
| 49 | 
            -
                def self.from_matrix(mat)
         | 
| 50 | 
            -
                  fourWSquaredMinus1 = mat[0,0] + mat[1,1] + mat[2,2]
         | 
| 51 | 
            -
                  fourXSquaredMinus1 = mat[0,0] - mat[1,1] - mat[2,2]
         | 
| 52 | 
            -
                  fourYSquaredMinus1 = mat[1,1] - mat[0,0] - mat[2,2]
         | 
| 53 | 
            -
                  fourZSquaredMinus1 = mat[2,2] - mat[0,0] - mat[1,1]
         | 
| 54 | 
            -
             | 
| 55 | 
            -
                  biggestIndex = 0
         | 
| 56 | 
            -
                  fourBiggestSquaredMinus1 = fourWSquaredMinus1
         | 
| 57 | 
            -
                  if(fourXSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 58 | 
            -
                    fourBiggestSquaredMinus1 = fourXSquaredMinus1
         | 
| 59 | 
            -
                    biggestIndex = 1
         | 
| 60 | 
            -
                  end
         | 
| 61 | 
            -
                  if(fourYSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 62 | 
            -
                    fourBiggestSquaredMinus1 = fourYSquaredMinus1
         | 
| 63 | 
            -
                    biggestIndex = 2
         | 
| 64 | 
            -
                  end
         | 
| 65 | 
            -
                  if(fourZSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 66 | 
            -
                    fourBiggestSquaredMinus1 = fourZSquaredMinus1
         | 
| 67 | 
            -
                    biggestIndex = 3
         | 
| 68 | 
            -
                  end
         | 
| 69 | 
            -
             | 
| 70 | 
            -
                  biggestVal = Math.sqrt(fourBiggestSquaredMinus1 + 1.0) * 0.5
         | 
| 71 | 
            -
                  multi = 0.25 / biggestVal
         | 
| 72 | 
            -
             | 
| 73 | 
            -
                  case biggestIndex
         | 
| 74 | 
            -
                  when 0
         | 
| 75 | 
            -
                    w = biggestVal
         | 
| 76 | 
            -
                    x = (mat[1,2] - mat[2,1]) *multi
         | 
| 77 | 
            -
                    y = (mat[2,0] - mat[0,2]) *multi
         | 
| 78 | 
            -
                    z = (mat[0,1] - mat[1,0]) *multi
         | 
| 79 | 
            -
                  when 1
         | 
| 80 | 
            -
                    x = biggestVal;
         | 
| 81 | 
            -
                    w = (mat[1,2] - mat[2,1]) *multi
         | 
| 82 | 
            -
                    y = (mat[0,1] + mat[1,0]) *multi
         | 
| 83 | 
            -
                    z = (mat[2,0] + mat[0,2]) *multi
         | 
| 84 | 
            -
                  when 2
         | 
| 85 | 
            -
                    y = biggestVal;
         | 
| 86 | 
            -
                    w = (mat[2,0] - mat[0,2]) *multi
         | 
| 87 | 
            -
                    x = (mat[0,1] + mat[1,0]) *multi
         | 
| 88 | 
            -
                    z = (mat[1,2] + mat[2,1]) *multi
         | 
| 89 | 
            -
                  when 3
         | 
| 90 | 
            -
                    z = biggestVal;
         | 
| 91 | 
            -
                    w = (mat[0,1] - mat[1,0]) *multi
         | 
| 92 | 
            -
                    x = (mat[2,0] + mat[0,2]) *multi
         | 
| 93 | 
            -
                    y = (mat[1,2] + mat[2,1]) *multi
         | 
| 94 | 
            -
                  end
         | 
| 95 | 
            -
                  return Quat.new(x,y,z,w)
         | 
| 96 | 
            -
                end
         | 
| 97 | 
            -
             | 
| 98 | 
            -
                def to_element_s
         | 
| 99 | 
            -
                  "[#{@x}, #{@y}, #{@z}, #{@w}]"
         | 
| 100 | 
            -
                end
         | 
| 101 | 
            -
             | 
| 102 | 
            -
                def to_s
         | 
| 103 | 
            -
                  "Quat" + to_element_s
         | 
| 104 | 
            -
                end
         | 
| 105 | 
            -
             | 
| 106 | 
            -
                # [Input]
         | 
| 107 | 
            -
                #  _rhs_ should be Quat.
         | 
| 108 | 
            -
                # [Output]
         | 
| 109 | 
            -
                #  return true if rhs equals myself.
         | 
| 110 | 
            -
                def ==(rhs)
         | 
| 111 | 
            -
                  return false if( !rhs.kind_of?(Quat) )
         | 
| 112 | 
            -
                  return false if(self.x != rhs.x)
         | 
| 113 | 
            -
                  return false if(self.y != rhs.y)
         | 
| 114 | 
            -
                  return false if(self.z != rhs.z)
         | 
| 115 | 
            -
                  return false if(self.w != rhs.w)
         | 
| 116 | 
            -
                  true
         | 
| 117 | 
            -
                end
         | 
| 118 | 
            -
             | 
| 119 | 
            -
                # [Output]
         | 
| 120 | 
            -
                #  return conjugated Quat.
         | 
| 121 | 
            -
                def conjugate
         | 
| 122 | 
            -
                  return Quat.new( -self.x, -self.y, -self.z, self.w)
         | 
| 123 | 
            -
                end
         | 
| 124 | 
            -
             | 
| 125 | 
            -
                # [Output]
         | 
| 126 | 
            -
                #  return normalized result as Quat.
         | 
| 127 | 
            -
                def normalize()
         | 
| 128 | 
            -
                  mag = Math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
         | 
| 129 | 
            -
                  return Quat.new(self.x/mag, self.y/mag, self.z/mag, self.w/mag)
         | 
| 130 | 
            -
                end
         | 
| 131 | 
            -
             | 
| 132 | 
            -
                # [Input]
         | 
| 133 | 
            -
                #  _rhs_ should be Quat.
         | 
| 134 | 
            -
                # [Output]
         | 
| 135 | 
            -
                #  return added result as Quat.
         | 
| 136 | 
            -
                def +(rhs)
         | 
| 137 | 
            -
                  Util3D.check_arg_type(Quat, rhs)
         | 
| 138 | 
            -
                  t1 = Vector3.new(self.x, self.y, self.z)
         | 
| 139 | 
            -
                  t2 = Vector3.new(rhs.x, rhs.y, rhs.z)
         | 
| 140 | 
            -
                  dot = t1.dot(t2)
         | 
| 141 | 
            -
                  t3 = t2.cross(t1)
         | 
| 142 | 
            -
             | 
| 143 | 
            -
                  t1 *= rhs.w
         | 
| 144 | 
            -
                  t2 *= self.w
         | 
| 145 | 
            -
             | 
| 146 | 
            -
                  tf = t1 + t2 + t3
         | 
| 147 | 
            -
                  rtn_w = self.w * rhs.w - dot
         | 
| 148 | 
            -
             | 
| 149 | 
            -
                  return Quat.new(tf.x, tf.y, tf.z, rtn_w)
         | 
| 150 | 
            -
                end
         | 
| 151 | 
            -
             | 
| 152 | 
            -
                # [Input]
         | 
| 153 | 
            -
                #  _rsh_ should be Quat.
         | 
| 154 | 
            -
                # [Output]
         | 
| 155 | 
            -
                #  return (outer products) multiplyed result as Quat.
         | 
| 156 | 
            -
                def *(rhs)
         | 
| 157 | 
            -
                  Util3D.check_arg_type(Quat, rhs)
         | 
| 158 | 
            -
             | 
| 159 | 
            -
                  pw = self.w; px = self.x; py = self.y; pz = self.z;
         | 
| 160 | 
            -
                  qw = rhs.w ; qx = rhs.x ; qy = rhs.y ; qz = rhs.z;
         | 
| 161 | 
            -
             | 
| 162 | 
            -
                  w = pw * qw - px * qx - py * qy - pz * qz
         | 
| 163 | 
            -
                  x = pw * qx + px * qw + py * qz - pz * qy
         | 
| 164 | 
            -
                  y = pw * qy - px * qz + py * qw + pz * qx
         | 
| 165 | 
            -
                  z = pw * qz + px * qy - py * qx + pz * qw
         | 
| 166 | 
            -
                  return Quat.new( x,y,z,w )
         | 
| 167 | 
            -
                end
         | 
| 168 | 
            -
              end
         | 
| 169 | 
            -
            end
         | 
| 170 | 
            -
             | 
| 1 | 
            +
            require 'gmath3D'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module GMath3D
         | 
| 4 | 
            +
              #
         | 
| 5 | 
            +
              # Quat represents quaternion.
         | 
| 6 | 
            +
              #
         | 
| 7 | 
            +
              class Quat
         | 
| 8 | 
            +
            public
         | 
| 9 | 
            +
                attr_accessor :x
         | 
| 10 | 
            +
                attr_accessor :y
         | 
| 11 | 
            +
                attr_accessor :z
         | 
| 12 | 
            +
                attr_accessor :w
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                # [Input]
         | 
| 15 | 
            +
                #  _x_, _y_, _z_, _w_should be Numeric.
         | 
| 16 | 
            +
                # [Output]
         | 
| 17 | 
            +
                #  return new instance of Quat.
         | 
| 18 | 
            +
                def initialize(x=0.0,y=0.0,z=0.0,w=0.0)
         | 
| 19 | 
            +
                  Util3D.check_arg_type(Numeric, x)
         | 
| 20 | 
            +
                  Util3D.check_arg_type(Numeric, y)
         | 
| 21 | 
            +
                  Util3D.check_arg_type(Numeric, z)
         | 
| 22 | 
            +
                  Util3D.check_arg_type(Numeric, w)
         | 
| 23 | 
            +
                  super()
         | 
| 24 | 
            +
                  @x = x
         | 
| 25 | 
            +
                  @y = y
         | 
| 26 | 
            +
                  @z = z
         | 
| 27 | 
            +
                  @w = w
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                # [Input]
         | 
| 31 | 
            +
                #  _axsi_ should be Vector3 and _angle_ should be Numeric.
         | 
| 32 | 
            +
                # [Output]
         | 
| 33 | 
            +
                #  return new instance of Quat.
         | 
| 34 | 
            +
                def self.from_axis(axis, angle)
         | 
| 35 | 
            +
                  Util3D.check_arg_type(Vector3, axis)
         | 
| 36 | 
            +
                  Util3D.check_arg_type(Numeric, angle)
         | 
| 37 | 
            +
                  s = Math.sin(0.5*angle)
         | 
| 38 | 
            +
                  x = s * axis.x
         | 
| 39 | 
            +
                  y = s * axis.y
         | 
| 40 | 
            +
                  z = s * axis.z
         | 
| 41 | 
            +
                  w = Math.cos(0.5*angle)
         | 
| 42 | 
            +
                  return Quat.new(x,y,z,w)
         | 
| 43 | 
            +
                end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                # [Input]
         | 
| 46 | 
            +
                #  _matrix_ should be Matrix which row and column size are 3.
         | 
| 47 | 
            +
                # [Output]
         | 
| 48 | 
            +
                #  return new instance of Quat.
         | 
| 49 | 
            +
                def self.from_matrix(mat)
         | 
| 50 | 
            +
                  fourWSquaredMinus1 = mat[0,0] + mat[1,1] + mat[2,2]
         | 
| 51 | 
            +
                  fourXSquaredMinus1 = mat[0,0] - mat[1,1] - mat[2,2]
         | 
| 52 | 
            +
                  fourYSquaredMinus1 = mat[1,1] - mat[0,0] - mat[2,2]
         | 
| 53 | 
            +
                  fourZSquaredMinus1 = mat[2,2] - mat[0,0] - mat[1,1]
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                  biggestIndex = 0
         | 
| 56 | 
            +
                  fourBiggestSquaredMinus1 = fourWSquaredMinus1
         | 
| 57 | 
            +
                  if(fourXSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 58 | 
            +
                    fourBiggestSquaredMinus1 = fourXSquaredMinus1
         | 
| 59 | 
            +
                    biggestIndex = 1
         | 
| 60 | 
            +
                  end
         | 
| 61 | 
            +
                  if(fourYSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 62 | 
            +
                    fourBiggestSquaredMinus1 = fourYSquaredMinus1
         | 
| 63 | 
            +
                    biggestIndex = 2
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
                  if(fourZSquaredMinus1 > fourBiggestSquaredMinus1)
         | 
| 66 | 
            +
                    fourBiggestSquaredMinus1 = fourZSquaredMinus1
         | 
| 67 | 
            +
                    biggestIndex = 3
         | 
| 68 | 
            +
                  end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                  biggestVal = Math.sqrt(fourBiggestSquaredMinus1 + 1.0) * 0.5
         | 
| 71 | 
            +
                  multi = 0.25 / biggestVal
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                  case biggestIndex
         | 
| 74 | 
            +
                  when 0
         | 
| 75 | 
            +
                    w = biggestVal
         | 
| 76 | 
            +
                    x = (mat[1,2] - mat[2,1]) *multi
         | 
| 77 | 
            +
                    y = (mat[2,0] - mat[0,2]) *multi
         | 
| 78 | 
            +
                    z = (mat[0,1] - mat[1,0]) *multi
         | 
| 79 | 
            +
                  when 1
         | 
| 80 | 
            +
                    x = biggestVal;
         | 
| 81 | 
            +
                    w = (mat[1,2] - mat[2,1]) *multi
         | 
| 82 | 
            +
                    y = (mat[0,1] + mat[1,0]) *multi
         | 
| 83 | 
            +
                    z = (mat[2,0] + mat[0,2]) *multi
         | 
| 84 | 
            +
                  when 2
         | 
| 85 | 
            +
                    y = biggestVal;
         | 
| 86 | 
            +
                    w = (mat[2,0] - mat[0,2]) *multi
         | 
| 87 | 
            +
                    x = (mat[0,1] + mat[1,0]) *multi
         | 
| 88 | 
            +
                    z = (mat[1,2] + mat[2,1]) *multi
         | 
| 89 | 
            +
                  when 3
         | 
| 90 | 
            +
                    z = biggestVal;
         | 
| 91 | 
            +
                    w = (mat[0,1] - mat[1,0]) *multi
         | 
| 92 | 
            +
                    x = (mat[2,0] + mat[0,2]) *multi
         | 
| 93 | 
            +
                    y = (mat[1,2] + mat[2,1]) *multi
         | 
| 94 | 
            +
                  end
         | 
| 95 | 
            +
                  return Quat.new(x,y,z,w)
         | 
| 96 | 
            +
                end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                def to_element_s
         | 
| 99 | 
            +
                  "[#{@x}, #{@y}, #{@z}, #{@w}]"
         | 
| 100 | 
            +
                end
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                def to_s
         | 
| 103 | 
            +
                  "Quat" + to_element_s
         | 
| 104 | 
            +
                end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                # [Input]
         | 
| 107 | 
            +
                #  _rhs_ should be Quat.
         | 
| 108 | 
            +
                # [Output]
         | 
| 109 | 
            +
                #  return true if rhs equals myself.
         | 
| 110 | 
            +
                def ==(rhs)
         | 
| 111 | 
            +
                  return false if( !rhs.kind_of?(Quat) )
         | 
| 112 | 
            +
                  return false if(self.x != rhs.x)
         | 
| 113 | 
            +
                  return false if(self.y != rhs.y)
         | 
| 114 | 
            +
                  return false if(self.z != rhs.z)
         | 
| 115 | 
            +
                  return false if(self.w != rhs.w)
         | 
| 116 | 
            +
                  true
         | 
| 117 | 
            +
                end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                # [Output]
         | 
| 120 | 
            +
                #  return conjugated Quat.
         | 
| 121 | 
            +
                def conjugate
         | 
| 122 | 
            +
                  return Quat.new( -self.x, -self.y, -self.z, self.w)
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
             | 
| 125 | 
            +
                # [Output]
         | 
| 126 | 
            +
                #  return normalized result as Quat.
         | 
| 127 | 
            +
                def normalize()
         | 
| 128 | 
            +
                  mag = Math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
         | 
| 129 | 
            +
                  return Quat.new(self.x/mag, self.y/mag, self.z/mag, self.w/mag)
         | 
| 130 | 
            +
                end
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                # [Input]
         | 
| 133 | 
            +
                #  _rhs_ should be Quat.
         | 
| 134 | 
            +
                # [Output]
         | 
| 135 | 
            +
                #  return added result as Quat.
         | 
| 136 | 
            +
                def +(rhs)
         | 
| 137 | 
            +
                  Util3D.check_arg_type(Quat, rhs)
         | 
| 138 | 
            +
                  t1 = Vector3.new(self.x, self.y, self.z)
         | 
| 139 | 
            +
                  t2 = Vector3.new(rhs.x, rhs.y, rhs.z)
         | 
| 140 | 
            +
                  dot = t1.dot(t2)
         | 
| 141 | 
            +
                  t3 = t2.cross(t1)
         | 
| 142 | 
            +
             | 
| 143 | 
            +
                  t1 *= rhs.w
         | 
| 144 | 
            +
                  t2 *= self.w
         | 
| 145 | 
            +
             | 
| 146 | 
            +
                  tf = t1 + t2 + t3
         | 
| 147 | 
            +
                  rtn_w = self.w * rhs.w - dot
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                  return Quat.new(tf.x, tf.y, tf.z, rtn_w)
         | 
| 150 | 
            +
                end
         | 
| 151 | 
            +
             | 
| 152 | 
            +
                # [Input]
         | 
| 153 | 
            +
                #  _rsh_ should be Quat.
         | 
| 154 | 
            +
                # [Output]
         | 
| 155 | 
            +
                #  return (outer products) multiplyed result as Quat.
         | 
| 156 | 
            +
                def *(rhs)
         | 
| 157 | 
            +
                  Util3D.check_arg_type(Quat, rhs)
         | 
| 158 | 
            +
             | 
| 159 | 
            +
                  pw = self.w; px = self.x; py = self.y; pz = self.z;
         | 
| 160 | 
            +
                  qw = rhs.w ; qx = rhs.x ; qy = rhs.y ; qz = rhs.z;
         | 
| 161 | 
            +
             | 
| 162 | 
            +
                  w = pw * qw - px * qx - py * qy - pz * qz
         | 
| 163 | 
            +
                  x = pw * qx + px * qw + py * qz - pz * qy
         | 
| 164 | 
            +
                  y = pw * qy - px * qz + py * qw + pz * qx
         | 
| 165 | 
            +
                  z = pw * qz + px * qy - py * qx + pz * qw
         | 
| 166 | 
            +
                  return Quat.new( x,y,z,w )
         | 
| 167 | 
            +
                end
         | 
| 168 | 
            +
              end
         | 
| 169 | 
            +
            end
         | 
| 170 | 
            +
             |