affine: Remove geoMImpl and use nillable pattern

This commit is contained in:
Hajime Hoshi 2018-02-27 00:45:57 +09:00
parent 479bf18be6
commit 2bd099014b
4 changed files with 82 additions and 109 deletions

16
geom.go
View File

@ -25,12 +25,12 @@ const GeoMDim = affine.GeoMDim
// //
// The initial value is identity. // The initial value is identity.
type GeoM struct { type GeoM struct {
impl affine.GeoM impl *affine.GeoM
} }
// Reset resets the GeoM as identity. // Reset resets the GeoM as identity.
func (g *GeoM) Reset() { func (g *GeoM) Reset() {
g.impl.Reset() g.impl = nil
} }
// Apply pre-multiplies a vector (x, y, 1) by the matrix. // Apply pre-multiplies a vector (x, y, 1) by the matrix.
@ -64,34 +64,34 @@ func (g *GeoM) Element(i, j int) float64 {
// 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) {
g.impl.Concat(&other.impl) g.impl = g.impl.Concat(other.impl)
} }
// Add is deprecated as of 1.5.0-alpha. // Add is deprecated as of 1.5.0-alpha.
// Note that this doesn't make sense as an operation for affine matrices. // Note that this doesn't make sense as an operation for affine matrices.
func (g *GeoM) Add(other GeoM) { func (g *GeoM) Add(other GeoM) {
g.impl.Add(other.impl) g.impl = g.impl.Add(other.impl)
} }
// 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) {
g.impl.Scale(x, y) g.impl = g.impl.Scale(x, y)
} }
// Translate translates the matrix by (tx, ty). // Translate translates the matrix by (tx, ty).
func (g *GeoM) Translate(tx, ty float64) { func (g *GeoM) Translate(tx, ty float64) {
g.impl.Translate(tx, ty) g.impl = g.impl.Translate(tx, ty)
} }
// Rotate rotates the matrix by theta. // Rotate rotates the matrix by theta.
// The unit is radian. // The unit is radian.
func (g *GeoM) Rotate(theta float64) { func (g *GeoM) Rotate(theta float64) {
g.impl.Rotate(theta) g.impl = g.impl.Rotate(theta)
} }
// 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) {
g.impl.SetElement(i, j, element) g.impl = g.impl.SetElement(i, j, element)
} }
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead. // ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.

View File

@ -203,7 +203,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
sy1 = r.Max.Y sy1 = r.Max.Y
} }
} }
vs := vertices(sx0, sy0, sx1, sy1, w, h, &options.GeoM.impl) vs := vertices(sx0, sy0, sx1, sy1, w, h, options.GeoM.impl)
if vs == nil { if vs == nil {
return nil return nil
} }

View File

@ -22,7 +22,10 @@ import (
// GeoMDim is a dimension of a GeoM. // GeoMDim is a dimension of a GeoM.
const GeoMDim = 3 const GeoMDim = 3
type geoMImpl struct { // A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
type GeoM struct {
a float64 a float64
b float64 b float64
c float64 c float64
@ -31,58 +34,33 @@ type geoMImpl struct {
ty float64 ty float64
} }
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
type GeoM struct {
impl *geoMImpl
}
func (g *GeoM) Reset() {
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.impl == nil { if g == nil {
return x, y return x, y
} }
i := g.impl return g.a*x + g.b*y + g.tx, g.c*x + g.d*y + g.ty
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.impl == nil { if g == nil {
return float32(x), float32(y) return float32(x), float32(y)
} }
i := g.impl return float32(g.a*x + g.b*y + g.tx), float32(g.c*x + g.d*y + g.ty)
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.impl == nil { if g == nil {
return 1, 0, 0, 1, 0, 0 return 1, 0, 0, 1, 0, 0
} }
i := g.impl return g.a, g.b, g.c, g.d, g.tx, g.ty
return i.a, i.b, i.c, i.d, i.tx, i.ty
}
func (g *GeoM) init() {
g.impl = &geoMImpl{
a: 1,
b: 0,
c: 0,
d: 1,
tx: 0,
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) *GeoM {
if g.impl == nil { a, b, c, d, tx, ty := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
g.init() if g != nil {
a, b, c, d, tx, ty = g.a, g.b, g.c, g.d, g.tx, g.ty
} }
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:
a = element a = element
@ -99,7 +77,7 @@ func (g *GeoM) SetElement(i, j int, element float64) {
default: default:
panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j)) panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
} }
g.impl = &geoMImpl{ return &GeoM{
a: a, a: a,
b: b, b: b,
c: c, c: c,
@ -111,48 +89,46 @@ func (g *GeoM) SetElement(i, j int, element float64) {
// 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) *GeoM {
if g.impl == nil { if g == nil {
g.init() return other
} }
if other.impl == nil { if other == nil {
other.init() return g
} }
i := g.impl return &GeoM{
oi := other.impl a: other.a*g.a + other.b*g.c,
g.impl = &geoMImpl{ b: other.a*g.b + other.b*g.d,
a: oi.a*i.a + oi.b*i.c, tx: other.a*g.tx + other.b*g.ty + other.tx,
b: oi.a*i.b + oi.b*i.d, c: other.c*g.a + other.d*g.c,
tx: oi.a*i.tx + oi.b*i.ty + oi.tx, d: other.c*g.b + other.d*g.d,
c: oi.c*i.a + oi.d*i.c, ty: other.c*g.tx + other.d*g.ty + other.ty,
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) *GeoM {
if g.impl == nil { if g == nil {
g.init() g = &GeoM{1, 0, 0, 1, 0, 0}
} }
if other.impl == nil { if other == nil {
other.init() other = &GeoM{1, 0, 0, 1, 0, 0}
} }
g.impl = &geoMImpl{ return &GeoM{
a: g.impl.a + other.impl.a, a: g.a + other.a,
b: g.impl.b + other.impl.b, b: g.b + other.b,
c: g.impl.c + other.impl.c, c: g.c + other.c,
d: g.impl.d + other.impl.d, d: g.d + other.d,
tx: g.impl.tx + other.impl.tx, tx: g.tx + other.tx,
ty: g.impl.ty + other.impl.ty, ty: g.ty + other.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) *GeoM {
if g.impl == nil { if g == nil {
g.impl = &geoMImpl{ return &GeoM{
a: x, a: x,
b: 0, b: 0,
c: 0, c: 0,
@ -160,22 +136,21 @@ func (g *GeoM) Scale(x, y float64) {
tx: 0, tx: 0,
ty: 0, ty: 0,
} }
return
} }
g.impl = &geoMImpl{ return &GeoM{
a: g.impl.a * x, a: g.a * x,
b: g.impl.b * x, b: g.b * x,
tx: g.impl.tx * x, tx: g.tx * x,
c: g.impl.c * y, c: g.c * y,
d: g.impl.d * y, d: g.d * y,
ty: g.impl.ty * y, ty: g.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) *GeoM {
if g.impl == nil { if g == nil {
g.impl = &geoMImpl{ return &GeoM{
a: 1, a: 1,
b: 0, b: 0,
c: 0, c: 0,
@ -183,23 +158,22 @@ func (g *GeoM) Translate(tx, ty float64) {
tx: tx, tx: tx,
ty: ty, ty: ty,
} }
return
} }
g.impl = &geoMImpl{ return &GeoM{
a: g.impl.a, a: g.a,
b: g.impl.b, b: g.b,
c: g.impl.c, c: g.c,
d: g.impl.d, d: g.d,
tx: g.impl.tx + tx, tx: g.tx + tx,
ty: g.impl.ty + ty, ty: g.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) *GeoM {
sin, cos := math.Sincos(theta) sin, cos := math.Sincos(theta)
if g.impl == nil { if g == nil {
g.impl = &geoMImpl{ return &GeoM{
a: cos, a: cos,
b: -sin, b: -sin,
c: sin, c: sin,
@ -207,14 +181,13 @@ func (g *GeoM) Rotate(theta float64) {
tx: 0, tx: 0,
ty: 0, ty: 0,
} }
return
} }
g.impl = &geoMImpl{ return &GeoM{
a: cos*g.impl.a - sin*g.impl.c, a: cos*g.a - sin*g.c,
b: cos*g.impl.b - sin*g.impl.d, b: cos*g.b - sin*g.d,
tx: cos*g.impl.tx - sin*g.impl.ty, tx: cos*g.tx - sin*g.ty,
c: sin*g.impl.a + cos*g.impl.c, c: sin*g.a + cos*g.c,
d: sin*g.impl.b + cos*g.impl.d, d: sin*g.b + cos*g.d,
ty: sin*g.impl.tx + cos*g.impl.ty, ty: sin*g.tx + cos*g.ty,
} }
} }

View File

@ -65,10 +65,10 @@ func vertices(sx0, sy0, sx1, sy1 int, width, height int, geo *affine.GeoM) []flo
dy = -float64(sy0) dy = -float64(sy0)
sy0 = 0 sy0 = 0
} }
g := affine.GeoM{} var g *affine.GeoM
g.Translate(dx, dy) g = g.Translate(dx, dy)
g.Concat(geo) g = g.Concat(geo)
geo = &g geo = g
} }
x0, y0 := 0.0, 0.0 x0, y0 := 0.0, 0.0
x1, y1 := float64(sx1-sx0), float64(sy1-sy0) x1, y1 := float64(sx1-sx0), float64(sy1-sy0)