Class: GMath3D::Vector3

Inherits:
Geom
  • Object
show all
Defined in:
lib/vector3.rb

Overview

Vector3 represents a point or a vector on 3D space.

Instance Attribute Summary collapse

Attributes inherited from Geom

#tolerance

Instance Method Summary collapse

Methods inherited from Geom

default_tolerance

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

#xObject

Returns the value of attribute x.



9
10
11
# File 'lib/vector3.rb', line 9

def x
  @x
end

#yObject

Returns the value of attribute y.



10
11
12
# File 'lib/vector3.rb', line 10

def y
  @y
end

#zObject

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_orthogonalObject

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

#boxObject

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/vector3.rb', line 52

def eql?(rhs)
  equals_inner(rhs)
end

#hashObject

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

#lengthObject

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

#normalizeObject

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

Returns:

  • (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.

Returns:

  • (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_aryObject



35
36
37
# File 'lib/vector3.rb', line 35

def to_ary
  [@x,@y,@z]
end

#to_column_vectorObject

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_sObject



27
28
29
# File 'lib/vector3.rb', line 27

def to_element_s
  "[#{@x}, #{@y}, #{@z}]"
end

#to_sObject



31
32
33
# File 'lib/vector3.rb', line 31

def to_s
  "Vector3" + to_element_s
end