Class: Roby::Pos::Vector3D

Inherits:
Object show all
Defined in:
lib/roby/state/pos.rb

Overview

A (x, y, z) vector

Direct Known Subclasses

Euler3D

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, z = 0) ⇒ Vector3D

Initializes a 3D vector



8
9
10
# File 'lib/roby/state/pos.rb', line 8

def initialize(x = 0, y = 0, z = 0)
    @x, @y, @z = x, y, z
end

Instance Attribute Details

#xObject

The vector coordinates



6
7
8
# File 'lib/roby/state/pos.rb', line 6

def x
  @x
end

#yObject

The vector coordinates



6
7
8
# File 'lib/roby/state/pos.rb', line 6

def y
  @y
end

#zObject

The vector coordinates



6
7
8
# File 'lib/roby/state/pos.rb', line 6

def z
  @z
end

Instance Method Details

#*(a) ⇒ Object

Returns the product of this vector with the scalar a



26
# File 'lib/roby/state/pos.rb', line 26

def *(a); Vector3D.new(x * a, y * a, z * a) end

#+(v) ⇒ Object

Returns self + v



22
# File 'lib/roby/state/pos.rb', line 22

def +(v); Vector3D.new(x + v.x, y + v.y, z + v.z) end

#-(v) ⇒ Object

Returns self - v



24
# File 'lib/roby/state/pos.rb', line 24

def -(v); Vector3D.new(x - v.x, y - v.y, z - v.z) end

#-@Object

Returns the opposite of this vector



30
# File 'lib/roby/state/pos.rb', line 30

def -@; Vector3D.new(-x, -y, -z) end

#/(a) ⇒ Object

Returns the division of this vector with the scalar a



28
# File 'lib/roby/state/pos.rb', line 28

def /(a); Vector3D.new(x / a, y / a, z / a) end

#==(v) ⇒ Object

True if v is the same vector than self



35
36
37
38
# File 'lib/roby/state/pos.rb', line 35

def ==(v)
    v.kind_of?(Vector3D) &&
	v.x == x && v.y == y && v.z == z
end

#distance(x = 0, y = nil, z = nil) ⇒ Object

call-seq:

v.distance2d w
v.distance2d x, y
v.distance2d x, y, z

Returns the euclidian distance in the (X,Y,Z) space, between this vector and the given coordinates. In the first form, w can be a vector in which case the distance is computed between (self.x, self.y, self.z) and (w.x, w.y, w.z). If w is a scalar, it is taken as the X coordinate and y = z = 0.

In the second form, both x and y must be scalars and z == 0.



77
78
79
80
81
82
83
84
85
86
# File 'lib/roby/state/pos.rb', line 77

def distance(x = 0, y = nil, z = nil)
    if !y && x.respond_to?(:x)
	x, y, z = x.x, x.y, x.z
    else
	y ||= 0
	z ||= 0
    end

    Math.sqrt( (x - self.x) ** 2 + (y - self.y) ** 2 + (z - self.z) ** 2)
end

#distance2d(x = 0, y = nil) ⇒ Object

call-seq:

v.distance2d w
v.distance2d x, y

Returns the euclidian distance in the (X,Y) plane, between this vector and the given coordinates. In the first form, w can be a vector in which case the distance is computed between (self.x, self.y) and (w.x, w.y). If w is a scalar, it is taken as the X coordinate and y = 0.

In the second form, both x and y must be scalars.



56
57
58
59
60
61
62
63
64
# File 'lib/roby/state/pos.rb', line 56

def distance2d(x = 0, y = nil)
    if !y && x.respond_to?(:x)
	x, y = x.x, x.y
    else
	y ||= 0
    end

    Math.sqrt( (x - self.x) ** 2 + (y - self.y) ** 2 )
end

#lengthObject

The length of the vector



20
# File 'lib/roby/state/pos.rb', line 20

def length; distance(0, 0, 0) end

#null?(tolerance = 0) ⇒ Boolean

True if this vector is of zero length. If tolerance is non-zero, returns true if length <= tolerance.

Returns:

  • (Boolean)


42
43
44
# File 'lib/roby/state/pos.rb', line 42

def null?(tolerance = 0)
    length <= tolerance
end

#pretty_print(pp) ⇒ Object



15
16
17
# File 'lib/roby/state/pos.rb', line 15

def pretty_print(pp)
    pp.text to_s
end

#to_sObject

:nodoc:



12
13
14
# File 'lib/roby/state/pos.rb', line 12

def to_s # :nodoc:
    "Vector3D(x=%f,y=%f,z=%f)" % [x,y,z] 
end

#xyzObject

Returns the [x, y, z] array



33
# File 'lib/roby/state/pos.rb', line 33

def xyz; [x, y, z] end