Class: Sunniesnow::Charter::Transform
- Inherits:
-
Object
- Object
- Sunniesnow::Charter::Transform
- Includes:
- Math
- Defined in:
- lib/sscharter/charter/events_manip.rb
Overview
Implements homography.
Instance Attribute Summary collapse
- #t1 ⇒ Rational readonly
- #tt ⇒ Rational readonly
- #xx ⇒ Float readonly
- #xy ⇒ Float readonly
- #xz ⇒ Float readonly
- #yx ⇒ Float readonly
- #yy ⇒ Float readonly
- #yz ⇒ Float readonly
- #zx ⇒ Float readonly
- #zy ⇒ Float readonly
- #zz ⇒ Float readonly
DSL Methods collapse
- #beat_translate(delta_beat) ⇒ void
- #compound_linear(xx, xy, yx, yy) ⇒ void
- #horizontal_flip ⇒ void
- #rotate(angle) ⇒ void
- #scale(sx, sy = sx) ⇒ void
- #translate(dx, dy) ⇒ void
- #vertical_flip ⇒ void
Instance Method Summary collapse
-
#apply(event) ⇒ Event
Same as
event. -
#initialize ⇒ Transform
constructor
A new instance of Transform.
Constructor Details
#initialize ⇒ Transform
Returns a new instance of Transform.
15 16 17 18 19 20 |
# File 'lib/sscharter/charter/events_manip.rb', line 15 def initialize @xx = @yy = @zz = 1.0 @xy = @xz = @yx = @yz = @zx = @zy = 0.0 @t1 = 0r @tt = 1r end |
Instance Attribute Details
#t1 ⇒ Rational (readonly)
13 14 15 |
# File 'lib/sscharter/charter/events_manip.rb', line 13 def t1 @t1 end |
#tt ⇒ Rational (readonly)
13 14 15 |
# File 'lib/sscharter/charter/events_manip.rb', line 13 def tt @tt end |
#xx ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def xx @xx end |
#xy ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def xy @xy end |
#xz ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def xz @xz end |
#yx ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def yx @yx end |
#yy ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def yy @yy end |
#yz ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def yz @yz end |
#zx ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def zx @zx end |
#zy ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def zy @zy end |
#zz ⇒ Float (readonly)
10 11 12 |
# File 'lib/sscharter/charter/events_manip.rb', line 10 def zz @zz end |
Instance Method Details
#apply(event) ⇒ Event
Returns same as event.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sscharter/charter/events_manip.rb', line 24 def apply event event.beat = @t1 + @tt * event.beat return unless x = event[:x] return unless y = event[:y] rx = xx*x + xy*y + xz ry = yx*x + yy*y + yz d = zx*x + zy*y + zz event[:x] = xp = rx / d event[:y] = yp = ry / d return event unless angle = event[:angle] dx = cos angle dy = sin angle cross = y*dx - x*dy cx0 = zy*xx - xy*zx cxx = zz*xx - xz*zx cxy = zz*xy - xz*zy dxp = cx0*cross + cxx*dx + cxy*dy cy0 = zx*yy - yx*zy cyy = zz*yy - yz*zy cyx = zz*yx - yz*zx dyp = cy0*-cross + cyy*dy + cyx*dx event[:angle] = atan2 dyp, dxp event end |
#beat_translate(delta_beat) ⇒ void
This method returns an undefined value.
112 113 114 115 116 117 |
# File 'lib/sscharter/charter/events_manip.rb', line 112 def beat_translate delta_beat raise ArgumentError, 'delta_beat must be a number' unless delta_beat.is_a? Numeric warn 'Rational is recommended over Float for delta_beat' if delta_beat.is_a? Float @t1 += delta_beat.to_r nil end |
#compound_linear(xx, xy, yx, yy) ⇒ void
This method returns an undefined value.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sscharter/charter/events_manip.rb', line 60 def compound_linear xx, xy, yx, yy raise ArgumentError, 'arguments must be numbers' unless [xx, xy, yx, yy].all? { _1.is_a? Numeric } @xx, @xy, @xz, @yx, @yy, @yz = [ xx * @xx + xy * @yx, xx * @xy + xy * @yy, xx * @xz + xy * @yz, yx * @xx + yy * @yx, yx * @xy + yy * @yy, yx * @xz + yy * @yz, ] nil end |
#horizontal_flip ⇒ void
This method returns an undefined value.
83 84 85 |
# File 'lib/sscharter/charter/events_manip.rb', line 83 def horizontal_flip compound_linear -1, 0, 0, 1 end |
#rotate(angle) ⇒ void
This method returns an undefined value.
94 95 96 97 98 99 100 |
# File 'lib/sscharter/charter/events_manip.rb', line 94 def rotate angle raise ArgumentError, 'angle must be a number' unless angle.is_a? Numeric warn 'Are you using degrees as angle unit instead of radians?' if angle != 0 && angle % 45 == 0 c = cos angle s = sin angle compound_linear c, -s, s, c end |
#scale(sx, sy = sx) ⇒ void
This method returns an undefined value.
105 106 107 108 |
# File 'lib/sscharter/charter/events_manip.rb', line 105 def scale sx, sy = sx raise ArgumentError, 'sx and sy must be numbers' unless sx.is_a?(Numeric) && sy.is_a?(Numeric) compound_linear sx, 0, 0, sy end |
#translate(dx, dy) ⇒ void
This method returns an undefined value.
76 77 78 79 80 |
# File 'lib/sscharter/charter/events_manip.rb', line 76 def translate dx, dy raise ArgumentError, 'dx and dy must be numbers' unless dx.is_a?(Numeric) && dy.is_a?(Numeric) @xz += dx @yz += dy end |
#vertical_flip ⇒ void
This method returns an undefined value.
88 89 90 |
# File 'lib/sscharter/charter/events_manip.rb', line 88 def vertical_flip compound_linear 1, 0, 0, -1 end |