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.
type GeoM struct {
impl affine.GeoM
impl *affine.GeoM
}
// Reset resets the GeoM as identity.
func (g *GeoM) Reset() {
g.impl.Reset()
g.impl = nil
}
// 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.
// This is same as muptiplying the matrix other and the matrix g in this order.
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.
// Note that this doesn't make sense as an operation for affine matrices.
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).
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).
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.
// The unit is radian.
func (g *GeoM) Rotate(theta float64) {
g.impl.Rotate(theta)
g.impl = g.impl.Rotate(theta)
}
// SetElement sets an element at (i, j).
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.

View File

@ -203,7 +203,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
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 {
return nil
}

View File

@ -22,7 +22,10 @@ import (
// GeoMDim is a dimension of a GeoM.
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
b float64
c float64
@ -31,58 +34,33 @@ type geoMImpl struct {
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) {
if g.impl == nil {
if g == nil {
return x, y
}
i := g.impl
return i.a*x + i.b*y + i.tx, i.c*x + i.d*y + i.ty
return g.a*x + g.b*y + g.tx, g.c*x + g.d*y + g.ty
}
func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) {
if g.impl == nil {
if g == nil {
return float32(x), float32(y)
}
i := g.impl
return float32(i.a*x + i.b*y + i.tx), float32(i.c*x + i.d*y + i.ty)
return float32(g.a*x + g.b*y + g.tx), float32(g.c*x + g.d*y + g.ty)
}
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
}
i := g.impl
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,
}
return g.a, g.b, g.c, g.d, g.tx, g.ty
}
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
if g.impl == nil {
g.init()
func (g *GeoM) SetElement(i, j int, element float64) *GeoM {
a, b, c, d, tx, ty := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
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 {
case i == 0 && j == 0:
a = element
@ -99,7 +77,7 @@ func (g *GeoM) SetElement(i, j int, element float64) {
default:
panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
}
g.impl = &geoMImpl{
return &GeoM{
a: a,
b: b,
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.
// This is same as muptiplying the matrix other and the matrix g in this order.
func (g *GeoM) Concat(other *GeoM) {
if g.impl == nil {
g.init()
func (g *GeoM) Concat(other *GeoM) *GeoM {
if g == nil {
return other
}
if other.impl == nil {
other.init()
if other == nil {
return g
}
i := g.impl
oi := other.impl
g.impl = &geoMImpl{
a: oi.a*i.a + oi.b*i.c,
b: oi.a*i.b + oi.b*i.d,
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,
return &GeoM{
a: other.a*g.a + other.b*g.c,
b: other.a*g.b + other.b*g.d,
tx: other.a*g.tx + other.b*g.ty + other.tx,
c: other.c*g.a + other.d*g.c,
d: other.c*g.b + other.d*g.d,
ty: other.c*g.tx + other.d*g.ty + other.ty,
}
}
// Add is deprecated.
func (g *GeoM) Add(other GeoM) {
if g.impl == nil {
g.init()
func (g *GeoM) Add(other *GeoM) *GeoM {
if g == nil {
g = &GeoM{1, 0, 0, 1, 0, 0}
}
if other.impl == nil {
other.init()
if other == nil {
other = &GeoM{1, 0, 0, 1, 0, 0}
}
g.impl = &geoMImpl{
a: g.impl.a + other.impl.a,
b: g.impl.b + other.impl.b,
c: g.impl.c + other.impl.c,
d: g.impl.d + other.impl.d,
tx: g.impl.tx + other.impl.tx,
ty: g.impl.ty + other.impl.ty,
return &GeoM{
a: g.a + other.a,
b: g.b + other.b,
c: g.c + other.c,
d: g.d + other.d,
tx: g.tx + other.tx,
ty: g.ty + other.ty,
}
}
// Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) {
if g.impl == nil {
g.impl = &geoMImpl{
func (g *GeoM) Scale(x, y float64) *GeoM {
if g == nil {
return &GeoM{
a: x,
b: 0,
c: 0,
@ -160,22 +136,21 @@ func (g *GeoM) Scale(x, y float64) {
tx: 0,
ty: 0,
}
return
}
g.impl = &geoMImpl{
a: g.impl.a * x,
b: g.impl.b * x,
tx: g.impl.tx * x,
c: g.impl.c * y,
d: g.impl.d * y,
ty: g.impl.ty * y,
return &GeoM{
a: g.a * x,
b: g.b * x,
tx: g.tx * x,
c: g.c * y,
d: g.d * y,
ty: g.ty * y,
}
}
// Translate translates the matrix by (x, y).
func (g *GeoM) Translate(tx, ty float64) {
if g.impl == nil {
g.impl = &geoMImpl{
func (g *GeoM) Translate(tx, ty float64) *GeoM {
if g == nil {
return &GeoM{
a: 1,
b: 0,
c: 0,
@ -183,23 +158,22 @@ func (g *GeoM) Translate(tx, ty float64) {
tx: tx,
ty: ty,
}
return
}
g.impl = &geoMImpl{
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,
return &GeoM{
a: g.a,
b: g.b,
c: g.c,
d: g.d,
tx: g.tx + tx,
ty: g.ty + ty,
}
}
// Rotate rotates the matrix by theta.
func (g *GeoM) Rotate(theta float64) {
func (g *GeoM) Rotate(theta float64) *GeoM {
sin, cos := math.Sincos(theta)
if g.impl == nil {
g.impl = &geoMImpl{
if g == nil {
return &GeoM{
a: cos,
b: -sin,
c: sin,
@ -207,14 +181,13 @@ func (g *GeoM) Rotate(theta float64) {
tx: 0,
ty: 0,
}
return
}
g.impl = &geoMImpl{
a: cos*g.impl.a - sin*g.impl.c,
b: cos*g.impl.b - sin*g.impl.d,
tx: cos*g.impl.tx - sin*g.impl.ty,
c: sin*g.impl.a + cos*g.impl.c,
d: sin*g.impl.b + cos*g.impl.d,
ty: sin*g.impl.tx + cos*g.impl.ty,
return &GeoM{
a: cos*g.a - sin*g.c,
b: cos*g.b - sin*g.d,
tx: cos*g.tx - sin*g.ty,
c: sin*g.a + cos*g.c,
d: sin*g.b + cos*g.d,
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)
sy0 = 0
}
g := affine.GeoM{}
g.Translate(dx, dy)
g.Concat(geo)
geo = &g
var g *affine.GeoM
g = g.Translate(dx, dy)
g = g.Concat(geo)
geo = g
}
x0, y0 := 0.0, 0.0
x1, y1 := float64(sx1-sx0), float64(sy1-sy0)