Method: Geometry::Rotation#initialize

Defined in:
lib/geometry/rotation.rb

#initialize(options = {}) ⇒ Rotation

Returns a new instance of Rotation.

Parameters:

  • options (Hash)

    a customizable set of options



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/geometry/rotation.rb', line 41

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