Class: Sunniesnow::Charter::Transform

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/sscharter/charter/events_manip.rb

Overview

Implements homography.

Instance Attribute Summary collapse

DSL Methods collapse

Instance Method Summary collapse

Constructor Details

#initializeTransform

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

#t1Rational (readonly)

Returns:

  • (Rational)


13
14
15
# File 'lib/sscharter/charter/events_manip.rb', line 13

def t1
  @t1
end

#ttRational (readonly)

Returns:

  • (Rational)


13
14
15
# File 'lib/sscharter/charter/events_manip.rb', line 13

def tt
  @tt
end

#xxFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def xx
  @xx
end

#xyFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def xy
  @xy
end

#xzFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def xz
  @xz
end

#yxFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def yx
  @yx
end

#yyFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def yy
  @yy
end

#yzFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def yz
  @yz
end

#zxFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def zx
  @zx
end

#zyFloat (readonly)

Returns:

  • (Float)


10
11
12
# File 'lib/sscharter/charter/events_manip.rb', line 10

def zy
  @zy
end

#zzFloat (readonly)

Returns:

  • (Float)


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.

Parameters:

Returns:

  • (Event)

    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.

Parameters:

  • delta_beat (Integer, Rational)

Raises:

  • (ArgumentError)


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.

Parameters:

  • xx (Numeric)
  • xy (Numeric)
  • yx (Numeric)
  • yy (Numeric)

Raises:

  • (ArgumentError)


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_flipvoid

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.

Parameters:

  • angle (Numeric)

    in radians.

Raises:

  • (ArgumentError)


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.

Parameters:

  • sx (Numeric)
  • sy (Numeric) (defaults to: sx)

Raises:

  • (ArgumentError)


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.

Parameters:

  • dx (Numeric)
  • dy (Numeric)

Raises:

  • (ArgumentError)


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_flipvoid

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