Class: GMath3D::Vector3
Overview
Vector3 represents a point or a vector on 3D space.
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Attributes inherited from Geom
Instance Method Summary collapse
-
#*(rhs) ⇒ Object
- Input
-
rsh should be Numeric.
-
#+(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#-(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#/(rhs) ⇒ Object
- Input
-
rhs should be Numeric.
-
#==(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#angle(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#arbitrary_orthogonal ⇒ Object
- Output
-
return orthogonal vector as Vector3.
-
#box ⇒ Object
- Output
-
return axially aligned bounding box as Box.
-
#cross(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#distance(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
-
#dot(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- #eql?(rhs) ⇒ Boolean
-
#hash ⇒ Object
For using Vector3 as hash key.
-
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector3
constructor
- Input
-
x, y, z should be Numeric.
-
#length ⇒ Object
- Output
-
return vector length as Numeric.
-
#normalize ⇒ Object
- Output
-
return normalized vector as Vector3.
-
#parallel?(rhs) ⇒ Boolean
- Input
-
rhs should be Vector3 [Output] return true if myself and rhs is parallel as boolean.
-
#project_to(rhs) ⇒ Object
This function projects self vector to rhs vector.
-
#same_direction?(rhs) ⇒ Boolean
- Input
-
rhs should be Vector3.
- #to_ary ⇒ Object
-
#to_column_vector ⇒ Object
- Output
-
return column vector as Matrix.
- #to_element_s ⇒ Object
- #to_s ⇒ Object
Methods inherited from Geom
Constructor Details
#initialize(x = 0.0, y = 0.0, z = 0.0) ⇒ Vector3
- Input
-
x, y, z should be Numeric.
- Output
-
return new instance as Vector3.
17 18 19 20 21 22 23 24 25 |
# File 'lib/vector3.rb', line 17 def initialize(x=0.0,y=0.0,z=0.0) Util3D.check_arg_type(Numeric, x) Util3D.check_arg_type(Numeric, y) Util3D.check_arg_type(Numeric, z) super() @x = x @y = y @z = z end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
9 10 11 |
# File 'lib/vector3.rb', line 9 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
10 11 12 |
# File 'lib/vector3.rb', line 10 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
11 12 13 |
# File 'lib/vector3.rb', line 11 def z @z end |
Instance Method Details
#*(rhs) ⇒ Object
- Input
-
rsh should be Numeric.
- Output
-
return multiplyed result as Vector3.
82 83 84 |
# File 'lib/vector3.rb', line 82 def *(rhs) multiply(rhs) end |
#+(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return added result as Vector3.
66 67 68 |
# File 'lib/vector3.rb', line 66 def +(rhs) add(rhs) end |
#-(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return subtracted result as Vector3.
74 75 76 |
# File 'lib/vector3.rb', line 74 def -(rhs) subtract(rhs) end |
#/(rhs) ⇒ Object
- Input
-
rhs should be Numeric.
- Output
-
return divided result as Vector3.
90 91 92 |
# File 'lib/vector3.rb', line 90 def /(rhs) divide(rhs) end |
#==(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return true if rhs equals myself.
43 44 45 46 |
# File 'lib/vector3.rb', line 43 def ==(rhs) return false if( !rhs.kind_of?(Vector3) ) equals_inner(rhs) end |
#angle(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return two vectors angle as Numeric (rad).
151 152 153 154 155 156 157 158 |
# File 'lib/vector3.rb', line 151 def angle(rhs) Util3D.check_arg_type(Vector3, rhs) vec1Length = self.length ; vec2Length = rhs.length ; return 0.0 if(vec1Length*vec2Length < self.tolerance ) v = self.dot(rhs)/(vec1Length*vec2Length) Math::acos( v ) end |
#arbitrary_orthogonal ⇒ Object
- Output
-
return orthogonal vector as Vector3.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/vector3.rb', line 117 def arbitrary_orthogonal return Vector3.new() if(self.length < self.tolerance) if(!self.parallel?( Vector3.new(0.0, 1.0, 0.0) )) un_parallel_vec = Vector3.new(0.0,1.0,0.0) elsif(!self.parallel?( Vector3.new(0.0,0.0,1.0) )) un_parallel_vec = Vector3.new(0.0,0.0,1.0) else un_parallel_vec = Vector3.new(1.0,0.0,0.0) end orthogonal = self.cross(un_parallel_vec) return orthogonal.normalize end |
#box ⇒ Object
- Output
-
return axially aligned bounding box as Box.
58 59 60 |
# File 'lib/vector3.rb', line 58 def box return Box.new(self, self) end |
#cross(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return cross product as Vector3.
107 108 109 110 111 112 113 |
# File 'lib/vector3.rb', line 107 def cross(rhs) Util3D.check_arg_type(Vector3, rhs) Vector3.new( self.y*rhs.z - self.z*rhs.y, self.z*rhs.x - self.x*rhs.z, self.x*rhs.y - self.y*rhs.x) end |
#distance(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return distance between two points as Numeric.
142 143 144 145 |
# File 'lib/vector3.rb', line 142 def distance(rhs) Util3D.check_arg_type(Vector3, rhs) Math::sqrt((self.x - rhs.x)*(self.x - rhs.x) + (self.y - rhs.y)*(self.y - rhs.y) + (self.z - rhs.z)*(self.z - rhs.z)) end |
#dot(rhs) ⇒ Object
- Input
-
rhs should be Vector3.
- Output
-
return inner product as Numeric
98 99 100 101 |
# File 'lib/vector3.rb', line 98 def dot(rhs) Util3D.check_arg_type(Vector3, rhs) self.x*rhs.x + self.y*rhs.y + self.z*rhs.z end |
#eql?(rhs) ⇒ Boolean
52 53 54 |
# File 'lib/vector3.rb', line 52 def eql?(rhs) equals_inner(rhs) end |
#hash ⇒ Object
For using Vector3 as hash key
49 50 51 |
# File 'lib/vector3.rb', line 49 def hash [@x.to_f, @y.to_f, @z.to_f].hash end |
#length ⇒ Object
- Output
-
return vector length as Numeric
134 135 136 |
# File 'lib/vector3.rb', line 134 def length Math::sqrt(self.x*self.x + self.y*self.y + self.z*self.z) end |
#normalize ⇒ Object
- Output
-
return normalized vector as Vector3
162 163 164 |
# File 'lib/vector3.rb', line 162 def normalize() self / self.length.to_f end |
#parallel?(rhs) ⇒ Boolean
- Input
-
rhs should be Vector3
- Output
-
return true if myself and rhs is parallel as boolean
170 171 172 173 174 175 |
# File 'lib/vector3.rb', line 170 def parallel?(rhs) Util3D.check_arg_type(Vector3, rhs) return false if(self.length < self.tolerance or rhs.length < rhs.tolerance) return false if(self.cross(rhs).length > self.tolerance) return true end |
#project_to(rhs) ⇒ Object
This function projects self vector to rhs vector.
- Input
-
rhs should be Vector3.
- Output
-
return projected result as Vector3.
193 194 195 196 197 198 |
# File 'lib/vector3.rb', line 193 def project_to(rhs) Util3D.check_arg_type(Vector3, rhs) return Vector3.new, 0.0 if( rhs.length < rhs.tolerance ) parameter = self.dot( rhs ) / ( rhs.x * rhs.x + rhs.y * rhs.y + rhs.z * rhs.z ).to_f return rhs*parameter, parameter end |
#same_direction?(rhs) ⇒ Boolean
- Input
-
rhs should be Vector3.
- Output
-
return true if myself and rhs have same direction as boolean.
181 182 183 184 185 186 |
# File 'lib/vector3.rb', line 181 def same_direction?(rhs) Util3D.check_arg_type(Vector3, rhs) return false if(!parallel?(rhs)) return false if(self.dot(rhs) < self.tolerance) return true end |
#to_ary ⇒ Object
35 36 37 |
# File 'lib/vector3.rb', line 35 def to_ary [@x,@y,@z] end |
#to_column_vector ⇒ Object
- Output
-
return column vector as Matrix
202 203 204 |
# File 'lib/vector3.rb', line 202 def to_column_vector return Matrix.column_vector([x,y,z]) end |
#to_element_s ⇒ Object
27 28 29 |
# File 'lib/vector3.rb', line 27 def to_element_s "[#{@x}, #{@y}, #{@z}]" end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/vector3.rb', line 31 def to_s "Vector3" + to_element_s end |