Class: HomogeneousTransformation

Inherits:
Object
  • Object
show all
Defined in:
lib/homogeneous_transformation.rb,
lib/homogeneous_transformation/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(q = UnitQuaternion.new(1, 0, 0, 0), translation = Vector[0, 0, 0], rel_to = nil, local = true) ⇒ HomogeneousTransformation

Creates a new HomogeneousTransformation from a quaternion and a vector.

Params:

q

A UnitQuaternion object representing this frame’s orientation relative to the rel_to frame.

translation

A 3-vector representing this frame’s translation relative to the rel_to frame.

rel_to

A HomogeneousTransformation object relative to which the current frame’s orientation and translation are specified. If rel_to is nil, the orientation and translation will be specified relative to the global reference frame.

local

A boolean value. If local is false, the relative position and orientation will be measured in the global reference frame. If local is true, the relative position and orientation will be meausured in rel_to’s reference frame.



20
21
22
23
24
25
# File 'lib/homogeneous_transformation.rb', line 20

def initialize(q = UnitQuaternion.new(1, 0, 0, 0),
               translation = Vector[0, 0, 0],
               rel_to = nil, local = true)
  setQuaternion(q, rel_to, local)
  setTranslation(translation, rel_to, local)
end

Class Method Details

.fromMatrix(m) ⇒ Object

Creates a homogeneous transformation given a suitable 4x4 matrix.

Params:

m

A 4x4 matrix. The upper left 3x3 submatrix must be orthonormal, and the final row must be [0, 0, 0, 1].



31
32
33
34
35
# File 'lib/homogeneous_transformation.rb', line 31

def self.fromMatrix(m)
  h = HomogeneousTransformation.new()
  h.setMatrix(m)
  return h
end

Instance Method Details

#*(other) ⇒ Object

Compose two homogeneous transformations.



127
128
129
130
131
132
133
# File 'lib/homogeneous_transformation.rb', line 127

def *(other)
  q_result = self.getQuaternion() * other.getQuaternion()
  t_result = self.getQuaternion().transform(other.getTranslation()) +
    self.getTranslation()
  result = HomogeneousTransformation.new(q_result, t_result)
  return result
end

#getMatrixObject

Returns the 4x4 matrix representation of the homogeneous transformation.



119
120
121
122
123
124
# File 'lib/homogeneous_transformation.rb', line 119

def getMatrix
  rot_mat = @q.getRotationMatrix()
  m = Matrix.columns([*rot_mat.transpose(), @t])
  m = Matrix.rows([*m, [0, 0, 0, 1]])
  return m
end

#getQuaternionObject

Returns the quaternion that represents the orientation of the homogeneous transformation.



38
39
40
# File 'lib/homogeneous_transformation.rb', line 38

def getQuaternion()
  return @q
end

#getTranslationObject

Returns the 3-vector that specifies the translation of the homogeneous transformation.



43
44
45
# File 'lib/homogeneous_transformation.rb', line 43

def getTranslation()
  return @t
end

#inverseObject

Returns the inverse of the homogeneous transformation.



112
113
114
115
116
# File 'lib/homogeneous_transformation.rb', line 112

def inverse
  q_inv = @q.inverse()
  t_inv = q_inv.transform(-1 * @t)
  return HomogeneousTransformation.new(q_inv, t_inv)
end

#setMatrix(m) ⇒ Object

Sets the value of the homogeneous transformation given a suitable 4x4 matrix.

Params:

m

A 4x4 matrix. The upper left 3x3 submatrix must be orthonormal, and the final row must be [0, 0, 0, 1].



88
89
90
91
92
93
94
95
96
97
# File 'lib/homogeneous_transformation.rb', line 88

def setMatrix(m)
  if m.row_size() != 4 or m.column_size() != 4
    raise(ArgumentError, "Matrix must be 4x4")
  end
  if (m.row(3) - Vector[0,0,0,1]).norm() > 1e-15
    raise(ArgumentError, "Final row must be [0, 0, 0, 1]")
  end
  @q.setRotationMatrix(m.minor(0..2, 0..2))
  @t = m.column(3)[0..2]
end

#setQuaternion(q, rel_to = nil, local = true) ⇒ Object

Sets the quaternion for the homogeneous transformation.

Params:

q

A UnitQuaternion object representing the orientation of this frame.

rel_to

A HomogeneousTransformation object relative to which the orientation is specified. If rel_to is nil, the orientation will be specified relative to the global reference frame.

local

A boolean value. If local is false, the quaternion q specifies the orientation relative to rel_to as measured in the global coordinate frame. If local is true, then q specifies the orientation relative to rel_to as measured in rel_to’s coordinate frame.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/homogeneous_transformation.rb', line 53

def setQuaternion(q, rel_to = nil, local = true)
  if rel_to
    if local
      @q = rel_to.getQuaternion() * q
    else
      @q = q * rel_to.getQuaternion()
    end
  else
    @q = q
  end
end

#setTranslation(t, rel_to = nil, local = true) ⇒ Object

Sets the translation for the homogeneous transformation.

Params:

t

A vector describing the frame’s translation.

rel_to

A HomogeneousTransformation object relative to which the translation is specified. If rel_to is nil, the translation will be specified relative to the global reference frame.

local

A boolean value. If local is false, then t specifies the translation relative to rel_to as measured in the global coordinate frame. If local is true, then t specifies the translation relative to rel_to as measured in rel_to’s coordinate frame.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/homogeneous_transformation.rb', line 71

def setTranslation(t, rel_to = nil, local = true)
  if rel_to
    if local
      @t = rel_to.getTranslation() +
        rel_to.getQuaternion().transform(t)
    else
      @t = rel_to.getTranslation() + t
    end
  else
    @t = t
  end
end

#transform(v) ⇒ Object

Takes a vector v representing a point in the local frame and returns the vector to that same point relative to the global frame.

Params:

v

A vector in the local reference frame.

Returns: The representation of v in the global reference frame.



107
108
109
# File 'lib/homogeneous_transformation.rb', line 107

def transform(v)
  return @t + @q.transform(v)
end