Class: Sunniesnow::Charter::Transform

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

Overview

Implements homography

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransform

Returns a new instance of Transform.



140
141
142
143
144
145
# File 'lib/sscharter.rb', line 140

def initialize
	@xx = @yy = @zz = 1.0
	@xy = @xz = @yx = @yz = @zx = @zy = 0.0
	@t1 = 0r
	@tt = 1r
end

Instance Attribute Details

#t1Object (readonly)

Returns the value of attribute t1.



138
139
140
# File 'lib/sscharter.rb', line 138

def t1
  @t1
end

#ttObject (readonly)

Returns the value of attribute tt.



138
139
140
# File 'lib/sscharter.rb', line 138

def tt
  @tt
end

#xxObject (readonly)

Returns the value of attribute xx.



138
139
140
# File 'lib/sscharter.rb', line 138

def xx
  @xx
end

#xyObject (readonly)

Returns the value of attribute xy.



138
139
140
# File 'lib/sscharter.rb', line 138

def xy
  @xy
end

#xzObject (readonly)

Returns the value of attribute xz.



138
139
140
# File 'lib/sscharter.rb', line 138

def xz
  @xz
end

#yxObject (readonly)

Returns the value of attribute yx.



138
139
140
# File 'lib/sscharter.rb', line 138

def yx
  @yx
end

#yyObject (readonly)

Returns the value of attribute yy.



138
139
140
# File 'lib/sscharter.rb', line 138

def yy
  @yy
end

#yzObject (readonly)

Returns the value of attribute yz.



138
139
140
# File 'lib/sscharter.rb', line 138

def yz
  @yz
end

#zxObject (readonly)

Returns the value of attribute zx.



138
139
140
# File 'lib/sscharter.rb', line 138

def zx
  @zx
end

#zyObject (readonly)

Returns the value of attribute zy.



138
139
140
# File 'lib/sscharter.rb', line 138

def zy
  @zy
end

#zzObject (readonly)

Returns the value of attribute zz.



138
139
140
# File 'lib/sscharter.rb', line 138

def zz
  @zz
end

Instance Method Details

#apply(event) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/sscharter.rb', line 147

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) ⇒ Object

Raises:

  • (ArgumentError)


214
215
216
217
218
# File 'lib/sscharter.rb', line 214

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
end

#compound_linear(xx, xy, yx, yy) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/sscharter.rb', line 176

def compound_linear xx, xy, yx, yy
	@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,
	]
end

#horizontal_flipObject



193
194
195
# File 'lib/sscharter.rb', line 193

def horizontal_flip
	compound_linear -1, 0, 0, 1
end

#rotate(angle) ⇒ Object

Raises:

  • (ArgumentError)


201
202
203
204
205
206
207
# File 'lib/sscharter.rb', line 201

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) ⇒ Object

Raises:

  • (ArgumentError)


209
210
211
212
# File 'lib/sscharter.rb', line 209

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) ⇒ Object

Raises:

  • (ArgumentError)


187
188
189
190
191
# File 'lib/sscharter.rb', line 187

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_flipObject



197
198
199
# File 'lib/sscharter.rb', line 197

def vertical_flip
	compound_linear 1, 0, 0, -1
end