Class: RMath3D::RQuat
- Inherits:
-
Object
- Object
- RMath3D::RQuat
- Defined in:
- lib/rmath3d/rmath3d_plain.rb
Overview
Document-class: RMath3D::RQuat provies quaternion arithmetic.
Class Method Summary collapse
-
.dot(q1, q2) ⇒ Object
call-seq: RQuat.dot(q_a,q_b) -> value.
-
.slerp(q1, q2, t) ⇒ Object
call-seq: RQuat.slerp( q_a, q_b, t ) -> interpolated quaternion.
Instance Method Summary collapse
-
#*(arg) ⇒ Object
call-seq: *.
-
#+(arg) ⇒ Object
call-seq: +.
-
#+@ ⇒ Object
call-seq: +.
-
#-(arg) ⇒ Object
call-seq: -.
-
#-@ ⇒ Object
call-seq: -.
-
#==(other) ⇒ Object
call-seq: ==.
-
#[](i) ⇒ Object
call-seq: quat -> value.
-
#[]=(i, value) ⇒ Object
call-seq: quat= value.
-
#add!(other) ⇒ Object
call-seq: quat1.add!( quat2 ).
-
#coerce(arg) ⇒ Object
call-seq: coerse(other).
-
#conjugate! ⇒ Object
call-seq: conjugate!.
-
#getConjugated ⇒ Object
call-seq: getConjugated.
-
#getInverse ⇒ Object
call-seq: getInverse -> inverse quaternion.
-
#getLength ⇒ Object
call-seq: getLength.
-
#getLengthSq ⇒ Object
call-seq: getLengthSq.
-
#getNormalized ⇒ Object
call-seq: getNormalized -> RQuat.
-
#initialize(*a) ⇒ RQuat
constructor
call-seq: RQuat.new -> (0,0,0,0) RQuat.new(e) -> (e,e,e,e) RQuat.new( other ) : Copy Constructor RQuat.new( e0, e1, e2, e3 ) -> (e0,e1,e2,e3).
-
#invert! ⇒ Object
call-seq: invert! -> self.
-
#mul!(other) ⇒ Object
call-seq: quat1.mul!( quat2 ).
-
#normalize! ⇒ Object
call-seq: normalize! -> self.
-
#rotationAxis(axis, radian) ⇒ Object
call-seq: rotationAxis(axis,radian) -> self.
-
#rotationMatrix(mtx) ⇒ Object
call-seq: rotationMarix(mtx4) -> self.
-
#setElements(x, y, z, w) ⇒ Object
call-seq: setElements( e0, e1, e2, e3 ).
-
#setIdentity ⇒ Object
call-seq: setIdentity.
-
#sub!(other) ⇒ Object
call-seq: quat1.sub!( quat2 ).
-
#to_a ⇒ Object
call-seq: to_a.
-
#to_s ⇒ Object
call-seq: to_s.
-
#toAxisAngle ⇒ Object
call-seq: toAxisAngle -> [axis,radian].
-
#w ⇒ Object
call-seq: w -> value.
-
#w=(value) ⇒ Object
call-seq: w= value.
-
#x ⇒ Object
call-seq: x -> value.
-
#x=(value) ⇒ Object
call-seq: x= value.
-
#xyz ⇒ Object
call-seq: xyz -> RVec3.
-
#xyz=(arg) ⇒ Object
call-seq: xyz= vXYZ.
-
#y ⇒ Object
call-seq: y -> value.
-
#y=(value) ⇒ Object
call-seq: y= value.
-
#z ⇒ Object
call-seq: z -> value.
-
#z=(value) ⇒ Object
call-seq: z= value.
Constructor Details
#initialize(*a) ⇒ RQuat
call-seq:
RQuat.new -> (0,0,0,0)
RQuat.new(e) -> (e,e,e,e)
RQuat.new( other ) : Copy Constructor
RQuat.new( e0, e1, e2, e3 ) -> (e0,e1,e2,e3)
Creates a new quaternion.
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1692 def initialize( *a ) @e = [] case a.length when 0 @e = [0.0, 0.0, 0.0, 0.0] when 1 case a[0] when Fixnum, Float @e = [ a[0], a[0], a[0], a[0] ] when RQuat @e = [ a[0].x, a[0].y, a[0].z, a[0].w ] else raise TypeError, "RQuat#initialize : Unknown type #{a[0].class}." return nil end when 4 a.each_with_index do |elem, index| case elem when Fixnum, Float @e[index] = elem else raise TypeError, "RQuat#initialize : Unknown type #{elem.class}." return nil end end else raise RuntimeError, "RQuat#initialize : wrong # of arguments (#{a.length})" return nil end return self end |
Class Method Details
.dot(q1, q2) ⇒ Object
call-seq: RQuat.dot(q_a,q_b) -> value
Calculates the dot product of q_a
and q_b
.
1891 1892 1893 1894 1895 1896 1897 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1891 def RQuat.dot( q1, q2 ) if q1.class != RQuat || q2.class != RQuat raise TypeError, "RQuat#dot : Unknown type q1:#{q2.class}, q2:#{q2.class}." return nil end return q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w end |
.slerp(q1, q2, t) ⇒ Object
call-seq: RQuat.slerp( q_a, q_b, t ) -> interpolated quaternion
Calculates the spherical linear interpolation between q_a
and q_b
at time t
(0.0~1.0).
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1905 def RQuat.slerp( q1, q2, t ) if q1.class != RQuat || q2.class != RQuat raise TypeError, "RQuat#slerp : Unknown type q1:#{q2.class}, q2:#{q2.class}." return nil end s1 = 0.0 s2 = 0.0 it = 1.0 - t cosine = RQuat.dot( q1, q2 ) qn1 = q1 qn2 = q2 if ( cosine < 0.0 ) cosine *= -1.0 qn1 *= -1.0 end if ( (1.0 - cosine) > TOLERANCE ) theta = Math.acos( cosine ) sin_theta = Math.sin( theta ) s1 = Math.sin( it * theta ) / sin_theta s2 = Math.sin( t * theta ) / sin_theta else s1 = it s2 = t end qn1 *= s1 qn2 *= s2 qResult = qn1 + qn2 return qResult end |
Instance Method Details
#*(arg) ⇒ Object
call-seq: *
quat1 * quat2 : Binary multiply operator.
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2072 def *( arg ) case arg when RQuat q1x = self.x q1y = self.y q1z = self.z q1w = self.w q2x = arg.x q2y = arg.y q2z = arg.z q2w = arg.w x = q1w*q2x + q1x*q2w + q1y*q2z - q1z*q2y y = q1w*q2y - q1x*q2z + q1y*q2w + q1z*q2x z = q1w*q2z + q1x*q2y - q1y*q2x + q1z*q2w w = q1w*q2w - q1x*q2x - q1y*q2y - q1z*q2z return RQuat.new( x, y, z, w ) when Fixnum, Float return RQuat.new( @e[0]*arg, @e[1]*arg, @e[2]*arg, @e[3]*arg ) else raise TypeError, "RQuat#* : Unknown type #{arg}." return nil end end |
#+(arg) ⇒ Object
call-seq: +
quat1 + quat2 : Binary plus operator.
2046 2047 2048 2049 2050 2051 2052 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2046 def +( arg ) if arg.class != RQuat raise TypeError, "RQuat#+ : Unknown type #{arg.class}." return nil end RQuat.new( x+arg.x, y+arg.y, z+arg.z, w+arg.w ) end |
#+@ ⇒ Object
call-seq: +
+quat : Unary plus operator.
2028 2029 2030 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2028 def +@ return self end |
#-(arg) ⇒ Object
call-seq: -
quat1 - quat2 : Binary minus operator.
2059 2060 2061 2062 2063 2064 2065 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2059 def -( arg ) if arg.class != RQuat raise TypeError, "RQuat#- : Unknown type #{arg.class}." return nil end RQuat.new( x-arg.x, y-arg.y, z-arg.z, w-arg.w ) end |
#-@ ⇒ Object
call-seq: -
-quat : Unary minus operator.
2037 2038 2039 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2037 def -@ return RQuat.new( -@e[0], -@e[1], -@e[2], -@e[3] ) end |
#==(other) ⇒ Object
call-seq: ==
quat1 == quat2 : evaluates equality.
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2101 def ==( other ) if other.class != RQuat raise TypeError, "RQuat#== : Unknown type #{other.class}." return nil end if (x-other.x).abs<=Float::EPSILON && (y-other.y).abs<=Float::EPSILON && (z-other.z).abs<=Float::EPSILON && (w-other.w).abs<=Float::EPSILON return true else return false end end |
#[](i) ⇒ Object
call-seq: quat -> value
Returns the element at i
.
1826 1827 1828 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1826 def [](i) @e[i] end |
#[]=(i, value) ⇒ Object
call-seq: quat= value
Stores value
at i
.
1774 1775 1776 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1774 def []=(i,value) @e[i] = value end |
#add!(other) ⇒ Object
call-seq: quat1.add!( quat2 )
quat1 += quat2 : appends the elements of quat2
into corresponding quat1
elements.
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2122 def add!( other ) if other.class != RQuat raise TypeError, "RQ#add! : Unknown type #{other.class}." return nil end self.x += other.x self.y += other.y self.z += other.z self.w += other.w return self end |
#coerce(arg) ⇒ Object
call-seq: coerse(other)
Resolves type mismatch.
1747 1748 1749 1750 1751 1752 1753 1754 1755 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1747 def coerce( arg ) case arg when Fixnum, Float, Bignum return [ self, arg ] else raise TypeError, "RQuat#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end |
#conjugate! ⇒ Object
call-seq: conjugate!
Conjugates itself.
1968 1969 1970 1971 1972 1973 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1968 def conjugate! @e[0] *= -1.0 @e[1] *= -1.0 @e[2] *= -1.0 return self end |
#getConjugated ⇒ Object
call-seq: getConjugated
Returns its conjugate quaternion.
1959 1960 1961 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1959 def getConjugated return RQuat.new( -@e[0], -@e[1], -@e[2], @e[3] ) end |
#getInverse ⇒ Object
call-seq: getInverse -> inverse quaternion
Returns the inverse.
1980 1981 1982 1983 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1980 def getInverse length_sq = getLengthSq() return RQuat.new( -@e[0]/length_sq, -@e[1]/length_sq, -@e[2]/length_sq, @e[3]/length_sq ) end |
#getLength ⇒ Object
call-seq: getLength
Returns the Euclidean length.
1873 1874 1875 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1873 def getLength return Math.sqrt( @e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] + @e[3]*@e[3] ) end |
#getLengthSq ⇒ Object
call-seq: getLengthSq
Returns the squared Euclidean length.
1882 1883 1884 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1882 def getLengthSq return (@e[0]*@e[0] + @e[1]*@e[1] + @e[2]*@e[2] + @e[3]*@e[3]).to_f end |
#getNormalized ⇒ Object
call-seq: getNormalized -> RQuat
Returns normalized quaternion.
2004 2005 2006 2007 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2004 def getNormalized length = getLength() return RQuat.new( @e[0]/length, @e[1]/length, @e[2]/length, @e[3]/length ) end |
#invert! ⇒ Object
call-seq: invert! -> self
Inverts itself.
1990 1991 1992 1993 1994 1995 1996 1997 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1990 def invert! length_sq = getLengthSq() @e[0] /= -length_sq @e[1] /= -length_sq @e[2] /= -length_sq @e[3] /= length_sq return self end |
#mul!(other) ⇒ Object
call-seq: quat1.mul!( quat2 )
quat1 *= quat2
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2160 def mul!( other ) case other when RQuat q1x = self.x q1y = self.y q1z = self.z q1w = self.w q2x = other.x q2y = other.y q2z = other.z q2w = other.w x = q1w*q2x + q1x*q2w + q1y*q2z - q1z*q2y y = q1w*q2y - q1x*q2z + q1y*q2w + q1z*q2x z = q1w*q2z + q1x*q2y - q1y*q2x + q1z*q2w w = q1w*q2w - q1x*q2x - q1y*q2y - q1z*q2z self.x = x self.y = y self.z = z self.w = w return self when Fixnum, Float self.x *= other self.y *= other self.z *= other self.w *= other return self else raise TypeError, "RQuat#mul! : Unknown type #{other.class}." return nil end end |
#normalize! ⇒ Object
call-seq: normalize! -> self
Normalizes itself.
2014 2015 2016 2017 2018 2019 2020 2021 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2014 def normalize! length = getLength() @e[0] /= length @e[1] /= length @e[2] /= length @e[3] /= length return self end |
#rotationAxis(axis, radian) ⇒ Object
call-seq: rotationAxis(axis,radian) -> self
Makes a quaternion that rotates around the axis
.
2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2250 def rotationAxis( axis, radian ) if axis.class != RVec3 raise TypeError, "RQuat#rotationAxis : Unknown type #{axis.class}." return nil end s = Math.sin( radian / 2.0 ) self.x = s * axis.x self.y = s * axis.y self.z = s * axis.z self.w = Math.cos( radian / 2.0 ) return self end |
#rotationMatrix(mtx) ⇒ Object
call-seq: rotationMarix(mtx4) -> self
Makes a rotation quaternion from a rotation matrix mtx4
(RMtx4).
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2202 def rotationMatrix( mtx ) if mtx.class != RMtx3 && mtx.class != RMtx4 raise TypeError, "RQuat#rotationMatrix : Unknown type #{mtx.class}." return nil end diag00 = mtx.getElement(0,0) diag11 = mtx.getElement(1,1) diag22 = mtx.getElement(2,2) if ( diag00 + diag11 + diag22 > 0.0 ) t = diag00 + diag11 + diag22 + 1.0 s = 1.0 / ( Math.sqrt( t ) * 2.0 ) self.w = s * t self.z = (mtx.getElement(1,0) - mtx.getElement(0,1)) * s self.y = (mtx.getElement(0,2) - mtx.getElement(2,0)) * s self.x = (mtx.getElement(2,1) - mtx.getElement(1,2)) * s elsif ( diag00 > diag11 && diag00 > diag22 ) t = diag00 - diag11 - diag22 + 1.0 s = 1.0 / ( Math.sqrt( t ) * 2.0 ) self.x = s * t self.y = (mtx.getElement(1,0) + mtx.getElement(0,1)) * s self.z = (mtx.getElement(0,2) + mtx.getElement(2,0)) * s self.w = (mtx.getElement(2,1) - mtx.getElement(1,2)) * s elsif ( diag11 > diag22 ) t = -diag00 + diag11 - diag22 + 1.0 s = 1.0 / ( Math.sqrt( t ) * 2.0 ) self.y = s * t self.x = (mtx.getElement(1,0) + mtx.getElement(0,1)) * s self.w = (mtx.getElement(0,2) - mtx.getElement(2,0)) * s self.z = (mtx.getElement(2,1) + mtx.getElement(1,2)) * s else t = -diag00 - diag11 + diag22 + 1.0 s = 1.0 / ( Math.sqrt( t ) * 2.0 ) self.z = s * t self.w = (mtx.getElement(1,0) - mtx.getElement(0,1)) * s self.x = (mtx.getElement(0,2) + mtx.getElement(2,0)) * s self.y = (mtx.getElement(2,1) + mtx.getElement(1,2)) * s end return self end |
#setElements(x, y, z, w) ⇒ Object
call-seq: setElements( e0, e1, e2, e3 )
Stores given 4 new values.
1762 1763 1764 1765 1766 1767 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1762 def setElements( x, y, z, w ) self.x = x self.y = y self.z = z self.w = w end |
#setIdentity ⇒ Object
call-seq: setIdentity
Sets as identity quaternion.
1946 1947 1948 1949 1950 1951 1952 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1946 def setIdentity self.x = 0.0 self.y = 0.0 self.z = 0.0 self.w = 1.0 return self end |
#sub!(other) ⇒ Object
call-seq: quat1.sub!( quat2 )
quat1 -= quat2 : subtracts the elements of quat2
from corresponding quat1
elements.
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2141 def sub!( other ) if other.class != RQuat raise TypeError, "RQuat#sub! : Unknown type #{other.class}." return nil end self.x -= other.x self.y -= other.y self.z -= other.z self.w -= other.w return self end |
#to_a ⇒ Object
call-seq: to_a
Returns its elements as a new Array.
1738 1739 1740 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1738 def to_a return @e end |
#to_s ⇒ Object
call-seq: to_s
Returns human-readable string.
1729 1730 1731 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1729 def to_s return "( #{@e[0]}, #{@e[1]}, #{@e[2]}, #{@e[3]} )\n" end |
#toAxisAngle ⇒ Object
call-seq: toAxisAngle -> [axis,radian]
Returns its rotation axis (RVec3) and rotation angle (in radian).
2270 2271 2272 2273 2274 2275 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 2270 def toAxisAngle axis = RVec3.new( self.x, self.y, self.z ).normalize! radian = 2.0 * Math.acos( self.w ) return [ axis, radian ] end |
#w ⇒ Object
call-seq: w -> value
Returns the value of w
.
1857 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1857 def w() return @e[3] end |
#w=(value) ⇒ Object
call-seq: w= value
Stores value
as w
.
1804 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1804 def w=(value) @e[3] = value end |
#x ⇒ Object
call-seq: x -> value
Returns the value of x
.
1836 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1836 def x() return @e[0] end |
#x=(value) ⇒ Object
call-seq: x= value
Stores value
as x
.
1783 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1783 def x=(value) @e[0] = value end |
#xyz ⇒ Object
call-seq: xyz -> RVec3
Returns the values of x
, y
and z
with new RVec3(x
,y
,z
).
1864 1865 1866 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1864 def xyz() return RVec3.new( @e[0], @e[1], @e[2] ) end |
#xyz=(arg) ⇒ Object
call-seq: xyz= vXYZ
Copies the values of vXYZ
(RVec3) into x
, y
and z
.
1811 1812 1813 1814 1815 1816 1817 1818 1819 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1811 def xyz=( arg ) if arg.class != RVec3 raise TypeError, "RQuat#xyz= : Unknown type #{arg.class}." return nil end @e[0] = arg.x @e[1] = arg.y @e[2] = arg.z end |
#y ⇒ Object
call-seq: y -> value
Returns the value of y
.
1843 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1843 def y() return @e[1] end |
#y=(value) ⇒ Object
call-seq: y= value
Stores value
as y
.
1790 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1790 def y=(value) @e[1] = value end |
#z ⇒ Object
call-seq: z -> value
Returns the value of z
.
1850 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1850 def z() return @e[2] end |
#z=(value) ⇒ Object
call-seq: z= value
Stores value
as z
.
1797 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 1797 def z=(value) @e[2] = value end |