class GMath3D::Vector3

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

Attributes

x[RW]
y[RW]
z[RW]

Public Class Methods

new(x=0.0,y=0.0,z=0.0) click to toggle source
Input

x, y, z should be Numeric.

Output

return new instance as Vector3.

Calls superclass method GMath3D::Geom::new
# File lib/vector3.rb, line 16
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

Public Instance Methods

*(rhs) click to toggle source
Input

rsh should be Numeric.

Output

return multiplyed result as Vector3.

# File lib/vector3.rb, line 81
def *(rhs)
  multiply(rhs)
end
+(rhs) click to toggle source
Input

rhs should be Vector3.

Output

return added result as Vector3.

# File lib/vector3.rb, line 65
def +(rhs)
  add(rhs)
end
-(rhs) click to toggle source
Input

rhs should be Vector3.

Output

return subtracted result as Vector3.

# File lib/vector3.rb, line 73
def -(rhs)
  subtract(rhs)
end
/(rhs) click to toggle source
Input

rhs should be Numeric.

Output

return divided result as Vector3.

# File lib/vector3.rb, line 89
def /(rhs)
  divide(rhs)
end
==(rhs) click to toggle source
Input

rhs should be Vector3.

Output

return true if rhs equals myself.

# File lib/vector3.rb, line 42
def ==(rhs)
  return false if( !rhs.kind_of?(Vector3) )
  equals_inner(rhs)
end
angle(rhs) click to toggle source
Input

rhs should be Vector3.

Output

return two vectors angle as Numeric (rad).

# File lib/vector3.rb, line 150
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() click to toggle source
Output

return orthogonal vector as Vector3.

# File lib/vector3.rb, line 116
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() click to toggle source
Output

return axially aligned bounding box as Box.

# File lib/vector3.rb, line 57
def box
  return Box.new(self, self)
end
cross(rhs) click to toggle source
Input

rhs should be Vector3.

Output

return cross product as Vector3.

# File lib/vector3.rb, line 106
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) click to toggle source
Input

rhs should be Vector3.

Output

return distance between two points as Numeric.

# File lib/vector3.rb, line 141
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) click to toggle source
Input

rhs should be Vector3.

Output

return inner product as Numeric

# File lib/vector3.rb, line 97
def dot(rhs)
  Util3D.check_arg_type(Vector3, rhs)
  self.x*rhs.x + self.y*rhs.y + self.z*rhs.z
end
eql?(rhs) click to toggle source
# File lib/vector3.rb, line 51
def eql?(rhs)
  equals_inner(rhs)
end
hash() click to toggle source

For using Vector3 as hash key

# File lib/vector3.rb, line 48
def hash
  [@x.to_f, @y.to_f, @z.to_f].hash
end
length() click to toggle source
Output

return vector length as Numeric

# File lib/vector3.rb, line 133
def length
  Math::sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
end
normalize() click to toggle source
Output

return normalized vector as Vector3

# File lib/vector3.rb, line 161
def normalize()
  self / self.length.to_f
end
parallel?(rhs) click to toggle source
Input

rhs should be Vector3

Output

return true if myself and rhs is parallel as boolean

# File lib/vector3.rb, line 169
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) click to toggle source

This function projects self vector to rhs vector.

Input

rhs should be Vector3.

Output

return projected result as Vector3.

# File lib/vector3.rb, line 192
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) click to toggle source
Input

rhs should be Vector3.

Output

return true if myself and rhs have same direction as boolean.

# File lib/vector3.rb, line 180
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() click to toggle source
# File lib/vector3.rb, line 34
def to_ary
  [@x,@y,@z]
end
to_column_vector() click to toggle source
Output

return column vector as Matrix

# File lib/vector3.rb, line 201
def to_column_vector
  return Matrix.column_vector([x,y,z])
end
to_element_s() click to toggle source
# File lib/vector3.rb, line 26
def to_element_s
  "[#{@x}, #{@y}, #{@z}]"
end
to_s() click to toggle source
# File lib/vector3.rb, line 30
def to_s
  "Vector3" + to_element_s
end

Private Instance Methods

add(rhs) click to toggle source
# File lib/vector3.rb, line 212
def add(rhs)
  Util3D.check_arg_type(Vector3, rhs)
  Vector3.new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
end
divide(rhs) click to toggle source
# File lib/vector3.rb, line 224
def divide(rhs)
  Util3D.check_arg_type(::Numeric, rhs)
  Vector3.new(self.x.to_f / rhs, self.y / rhs.to_f, self.z / rhs.to_f)
end
equals_inner(rhs) click to toggle source
# File lib/vector3.rb, line 206
def equals_inner(rhs)
  return false if((self.x - rhs.x).abs > @tolerance) 
  return false if((self.y - rhs.y).abs > @tolerance) 
  return false if((self.z - rhs.z).abs > @tolerance) 
  true
end
multiply(rhs) click to toggle source
# File lib/vector3.rb, line 220
def multiply(rhs)
  Util3D.check_arg_type(::Numeric, rhs)
  Vector3.new(self.x * rhs, self.y * rhs, self.z * rhs)
end
subtract(rhs) click to toggle source
# File lib/vector3.rb, line 216
def subtract(rhs)
  Util3D.check_arg_type(Vector3, rhs)
  Vector3.new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
end