Class: Geometry::Rotation
- Inherits:
-
Object
- Object
- Geometry::Rotation
- Defined in:
- lib/geometry/rotation.rb
Overview
A generalized representation of a rotation transformation.
Instance Attribute Summary collapse
- #dimensions ⇒ Integer readonly
- #matrix ⇒ Matrix readonly
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #identity? ⇒ Boolean
-
#initialize(options = {}) ⇒ Rotation
constructor
A new instance of Rotation.
Constructor Details
#initialize(options = {}) ⇒ Rotation
Returns a new instance of Rotation.
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) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) @dimensions = [:dimensions] || nil = [[:x], [:y], [:z]] = [[:x], [:y], [:z]].select {|a| a} if .count != 0 @x = [:x] || nil @y = [:y] || nil @z = [:z] || nil raise ArgumentError, "All axis options must be Vectors" unless .all? {|a| a.is_a?(Vector) or a.is_a?(Array) } raise ArgumentError, "All provided axes must be the same size" unless .all? {|a| a.size == .first.size} @dimensions ||= .first.size raise ArgumentError, "Dimensionality mismatch" unless .first.size <= @dimensions if .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 .size == (@dimensions - 1) end end |
Instance Attribute Details
#dimensions ⇒ Integer (readonly)
10 11 12 |
# File 'lib/geometry/rotation.rb', line 10 def dimensions @dimensions end |
#matrix ⇒ Matrix (readonly)
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 |
#x ⇒ Object (readonly)
Returns the value of attribute x.
11 12 13 |
# File 'lib/geometry/rotation.rb', line 11 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
11 12 13 |
# File 'lib/geometry/rotation.rb', line 11 def y @y end |
#z ⇒ Object (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: ==
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
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 |