Class: Geometry::Rotation
- Inherits:
-
Object
- Object
- Geometry::Rotation
- Includes:
- ClusterFactory
- Defined in:
- lib/geometry/rotation.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#dimensions ⇒ Integer
readonly
Dimensions.
-
#matrix ⇒ Matrix
readonly
The transformation Matrix representing the Rotation.
-
#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.
Class Method Summary collapse
-
.new(angle) ⇒ Object
Create a planar Rotation with an angle.
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #identity? ⇒ Boolean
-
#initialize(options = {}) ⇒ Rotation
constructor
A new instance of Rotation.
-
#transform(point) ⇒ Point
Transform and return a new Point.
Methods included from ClusterFactory
Constructor Details
#initialize(options = {}) ⇒ Rotation
Returns a new instance of Rotation.
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) , 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 #{.size}" unless .size == (@dimensions - 1) end end |
Instance Attribute Details
#dimensions ⇒ Integer (readonly)
Returns dimensions.
18 19 20 |
# File 'lib/geometry/rotation.rb', line 18 def dimensions @dimensions end |
#matrix ⇒ Matrix (readonly)
Returns the transformation Matrix representing the Geometry::Rotation.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/geometry/rotation.rb', line 80 def matrix return nil unless [@x, @y, @z].compact.size >= 2 # 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.
19 20 21 |
# File 'lib/geometry/rotation.rb', line 19 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
19 20 21 |
# File 'lib/geometry/rotation.rb', line 19 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
19 20 21 |
# File 'lib/geometry/rotation.rb', line 19 def z @z end |
Class Method Details
.new(angle) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/geometry/rotation.rb', line 23 def self.new(*args) = args.select {|a| a.is_a? Hash}.reduce({}, :merge) if .has_key? :angle RotationAngle.new [:angle] elsif .has_key?(:x) && [:x, :y, :z].one? {|k| .has_key? k } RotationAngle.new x:[:x] else self.allocate.tap {|rotation| rotation.send :initialize, *args } end end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
69 70 71 |
# File 'lib/geometry/rotation.rb', line 69 def eql?(other) (self.x.eql? other.x) && (self.y.eql? other.y) && (self.z.eql? other.z) end |
#identity? ⇒ Boolean
74 75 76 |
# File 'lib/geometry/rotation.rb', line 74 def identity? (!@x && !@y && !@z) || ([@x, @y, @z].select {|a| a}.all? {|a| a.respond_to?(:magnitude) ? (1 == a.magnitude) : (1 == a.size)}) end |