Class: Geometry::Rotation

Inherits:
Object
  • Object
show all
Defined in:
lib/geometry/rotation.rb

Overview

A generalized representation of a rotation transformation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Rotation

Returns a new instance of Rotation.

Parameters:

  • options (Hash)

    a customizable set of options



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/geometry/rotation.rb', line 18

def initialize(*args)
    options, args = args.partition {|a| a.is_a? Hash}
    options = options.reduce({}, :merge)

    @dimensions = options[:dimensions] || nil

    axis_options = [options[:x], options[:y], options[:z]]
    all_axes_options = [options[:x], options[:y], options[:z]].select {|a| a}
    if all_axes_options.count != 0
  @x = options[:x] || nil
  @y = options[:y] || nil
  @z = options[:z] || nil

  raise ArgumentError, "All axis options must be Vectors" unless all_axes_options.all? {|a| a.is_a?(Vector) or a.is_a?(Array) }

  raise ArgumentError, "All provided axes must be the same size" unless all_axes_options.all? {|a| a.size == all_axes_options.first.size}

  @dimensions ||= all_axes_options.first.size

  raise ArgumentError, "Dimensionality mismatch" unless all_axes_options.first.size <= @dimensions
  if all_axes_options.first.size < @dimensions
      @x, @y, @z = [@x, @y, @z].map {|a| (a && (a.size != 0) && (a.size < @dimensions)) ? Array.new(@dimensions) {|i| a[i] || 0 } : a }
  end

  raise ArgumentError, "Too many axes specified (expected #{@dimensions - 1} but got #{all_axes_options.size}" unless all_axes_options.size == (@dimensions - 1)
    end
end

Instance Attribute Details

#dimensionsInteger (readonly)

Returns:

  • (Integer)


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

def dimensions
  @dimensions
end

#matrixMatrix (readonly)

Returns:

  • (Matrix)

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/geometry/rotation.rb', line 57

def matrix
    # Force all axes to be Vectors
    x,y,z = [@x, @y, @z].map {|a| a.is_a?(Array) ? Vector[*a] : a}

    # Force all axes to exist
    if x and y
  z = x ** y
    elsif x and z
  y = x ** z
    elsif y and z
  x = y ** z
    end

    rows = []
    [x, y, z].each_with_index {|a, i| rows.push(a.to_a) if i < @dimensions }

    raise ArgumentError, "Number of axes must match the dimensions of each axis" unless @dimensions == rows.size

    Matrix[*rows]
end

#xObject (readonly)

Returns the value of attribute x.



11
12
13
# File 'lib/geometry/rotation.rb', line 11

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



11
12
13
# File 'lib/geometry/rotation.rb', line 11

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.



11
12
13
# File 'lib/geometry/rotation.rb', line 11

def z
  @z
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


46
47
48
# File 'lib/geometry/rotation.rb', line 46

def eql?(other)
    (self.x.eql? other.x) && (self.y.eql? other.y) && (self.z.eql? other.z)
end

#identity?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/geometry/rotation.rb', line 51

def identity?
    (!@x && !@y && !@z) || ([@x, @y, @z].select {|a| a}.all? {|a| a.respond_to?(:magnitude) ? (1 == a.magnitude) : (1 == a.size)})
end