affine: Reduce copying cost of GeoM

This commit is contained in:
Hajime Hoshi 2018-02-18 23:39:24 +09:00
parent c79b795e84
commit 896a47b2ee

View File

@ -15,171 +15,206 @@
package affine package affine
import ( import (
"fmt"
"math" "math"
) )
// GeoMDim is a dimension of a GeoM. // GeoMDim is a dimension of a GeoM.
const GeoMDim = 3 const GeoMDim = 3
type geoMImpl struct {
a float64
b float64
c float64
d float64
tx float64
ty float64
}
// A GeoM represents a matrix to transform geometry when rendering an image. // A GeoM represents a matrix to transform geometry when rendering an image.
// //
// The initial value is identity. // The initial value is identity.
type GeoM struct { type GeoM struct {
a float64 impl *geoMImpl
b float64
c float64
d float64
tx float64
ty float64
inited bool
} }
func (g *GeoM) Reset() { func (g *GeoM) Reset() {
g.inited = false g.impl = nil
} }
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) { func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
if !g.inited { if g.impl == nil {
return x, y return x, y
} }
return g.a*x + g.b*y + g.tx, g.c*x + g.d*y + g.ty i := g.impl
return i.a*x + i.b*y + i.tx, i.c*x + i.d*y + i.ty
} }
func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) { func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) {
if !g.inited { if g.impl == nil {
return float32(x), float32(y) return float32(x), float32(y)
} }
return float32(g.a*x + g.b*y + g.tx), float32(g.c*x + g.d*y + g.ty) i := g.impl
return float32(i.a*x + i.b*y + i.tx), float32(i.c*x + i.d*y + i.ty)
} }
func (g *GeoM) Elements() (a, b, c, d, tx, ty float64) { func (g *GeoM) Elements() (a, b, c, d, tx, ty float64) {
if !g.inited { if g.impl == nil {
return 1, 0, 0, 1, 0, 0 return 1, 0, 0, 1, 0, 0
} }
return g.a, g.b, g.c, g.d, g.tx, g.ty i := g.impl
return i.a, i.b, i.c, i.d, i.tx, i.ty
} }
func (g *GeoM) init() { func (g *GeoM) init() {
g.a = 1 g.impl = &geoMImpl{
g.b = 0 a: 1,
g.c = 0 b: 0,
g.d = 1 c: 0,
g.tx = 0 d: 1,
g.ty = 0 tx: 0,
g.inited = true ty: 0,
}
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) { func (g *GeoM) SetElement(i, j int, element float64) {
if !g.inited { if g.impl == nil {
g.init() g.init()
} }
a, b, c, d, tx, ty := g.impl.a, g.impl.b, g.impl.c, g.impl.d, g.impl.tx, g.impl.ty
switch { switch {
case i == 0 && j == 0: case i == 0 && j == 0:
g.a = element a = element
case i == 0 && j == 1: case i == 0 && j == 1:
g.b = element b = element
case i == 0 && j == 2: case i == 0 && j == 2:
g.tx = element tx = element
case i == 1 && j == 0: case i == 1 && j == 0:
g.c = element c = element
case i == 1 && j == 1: case i == 1 && j == 1:
g.d = element d = element
case i == 1 && j == 2: case i == 1 && j == 2:
g.ty = element ty = element
default: default:
panic("affine: i or j is out of index") panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
}
g.impl = &geoMImpl{
a: a,
b: b,
c: c,
d: d,
tx: tx,
ty: ty,
} }
} }
// Concat multiplies a geometry matrix with the other geometry matrix. // Concat multiplies a geometry matrix with the other geometry matrix.
// This is same as muptiplying the matrix other and the matrix g in this order. // This is same as muptiplying the matrix other and the matrix g in this order.
func (g *GeoM) Concat(other *GeoM) { func (g *GeoM) Concat(other *GeoM) {
if !g.inited { if g.impl == nil {
g.init() g.init()
} }
if !other.inited { if other.impl == nil {
other.init() other.init()
} }
a, b, c, d, tx, ty := g.a, g.b, g.c, g.d, g.tx, g.ty
g.a = other.a*a + other.b*c i := g.impl
g.b = other.a*b + other.b*d oi := other.impl
g.tx = other.a*tx + other.b*ty + other.tx g.impl = &geoMImpl{
g.c = other.c*a + other.d*c a: oi.a*i.a + oi.b*i.c,
g.d = other.c*b + other.d*d b: oi.a*i.b + oi.b*i.d,
g.ty = other.c*tx + other.d*ty + other.ty tx: oi.a*i.tx + oi.b*i.ty + oi.tx,
c: oi.c*i.a + oi.d*i.c,
d: oi.c*i.b + oi.d*i.d,
ty: oi.c*i.tx + oi.d*i.ty + oi.ty,
}
} }
// Add is deprecated. // Add is deprecated.
func (g *GeoM) Add(other GeoM) { func (g *GeoM) Add(other GeoM) {
if !g.inited { if g.impl == nil {
g.init() g.init()
} }
if !other.inited { if other.impl == nil {
other.init() other.init()
} }
g.a += other.a g.impl = &geoMImpl{
g.b += other.b a: g.impl.a + other.impl.a,
g.c += other.c b: g.impl.b + other.impl.b,
g.d += other.d c: g.impl.c + other.impl.c,
g.tx += other.tx d: g.impl.d + other.impl.d,
g.ty += other.ty tx: g.impl.tx + other.impl.tx,
ty: g.impl.ty + other.impl.ty,
}
} }
// Scale scales the matrix by (x, y). // Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) { func (g *GeoM) Scale(x, y float64) {
if !g.inited { if g.impl == nil {
g.a = x g.impl = &geoMImpl{
g.b = 0 a: x,
g.c = 0 b: 0,
g.d = y c: 0,
g.tx = 0 d: y,
g.ty = 0 tx: 0,
g.inited = true ty: 0,
}
return return
} }
g.a *= x g.impl = &geoMImpl{
g.b *= x a: g.impl.a * x,
g.tx *= x b: g.impl.b * x,
g.c *= y tx: g.impl.tx * x,
g.d *= y c: g.impl.c * y,
g.ty *= y d: g.impl.d * y,
ty: g.impl.ty * y,
}
} }
// Translate translates the matrix by (x, y). // Translate translates the matrix by (x, y).
func (g *GeoM) Translate(tx, ty float64) { func (g *GeoM) Translate(tx, ty float64) {
if !g.inited { if g.impl == nil {
g.a = 1 g.impl = &geoMImpl{
g.b = 0 a: 1,
g.c = 0 b: 0,
g.d = 1 c: 0,
g.tx = tx d: 1,
g.ty = ty tx: tx,
g.inited = true ty: ty,
}
return return
} }
g.tx += tx g.impl = &geoMImpl{
g.ty += ty a: g.impl.a,
b: g.impl.b,
c: g.impl.c,
d: g.impl.d,
tx: g.impl.tx + tx,
ty: g.impl.ty + ty,
}
} }
// Rotate rotates the matrix by theta. // Rotate rotates the matrix by theta.
func (g *GeoM) Rotate(theta float64) { func (g *GeoM) Rotate(theta float64) {
sin, cos := math.Sincos(theta) sin, cos := math.Sincos(theta)
if !g.inited { if g.impl == nil {
g.a = cos g.impl = &geoMImpl{
g.b = -sin a: cos,
g.c = sin b: -sin,
g.d = cos c: sin,
g.tx = 0 d: cos,
g.ty = 0 tx: 0,
g.inited = true ty: 0,
}
return return
} }
a, b, c, d, tx, ty := g.a, g.b, g.c, g.d, g.tx, g.ty g.impl = &geoMImpl{
g.a = cos*a - sin*c a: cos*g.impl.a - sin*g.impl.c,
g.b = cos*b - sin*d b: cos*g.impl.b - sin*g.impl.d,
g.tx = cos*tx - sin*ty tx: cos*g.impl.tx - sin*g.impl.ty,
g.c = sin*a + cos*c c: sin*g.impl.a + cos*g.impl.c,
g.d = sin*b + cos*d d: sin*g.impl.b + cos*g.impl.d,
g.ty = sin*tx + cos*ty ty: sin*g.impl.tx + cos*g.impl.ty,
}
} }