Refactoring: define identity matrix variable

This commit is contained in:
Hajime Hoshi 2014-12-27 17:11:05 +09:00
parent 7849a68cd0
commit 969554b10b
2 changed files with 24 additions and 28 deletions

View File

@ -21,6 +21,16 @@ import (
// ColorMDim is a dimension of a ColorM. // ColorMDim is a dimension of a ColorM.
const ColorMDim = 5 const ColorMDim = 5
var colorMI = ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
// A ColorM represents a matrix to transform coloring when rendering an image. // A ColorM represents a matrix to transform coloring when rendering an image.
// //
// A ColorM is applied to the source alpha color // A ColorM is applied to the source alpha color
@ -34,15 +44,6 @@ type ColorM struct {
es [ColorMDim - 1][ColorMDim]float64 es [ColorMDim - 1][ColorMDim]float64
} }
func colorMatrixEsI() [ColorMDim - 1][ColorMDim]float64 {
return [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
}
}
func (c ColorM) dim() int { func (c ColorM) dim() int {
return ColorMDim return ColorMDim
} }
@ -62,8 +63,7 @@ func (c ColorM) Element(i, j int) float64 {
// This returns c. // This returns c.
func (c *ColorM) Concat(other ColorM) ColorM { func (c *ColorM) Concat(other ColorM) ColorM {
if !c.initialized { if !c.initialized {
c.es = colorMatrixEsI() *c = colorMI
c.initialized = true
} }
result := ColorM{} result := ColorM{}
mul(&other, c, &result) mul(&other, c, &result)
@ -75,8 +75,7 @@ func (c *ColorM) Concat(other ColorM) ColorM {
// This returns c. // This returns c.
func (c *ColorM) Add(other ColorM) ColorM { func (c *ColorM) Add(other ColorM) ColorM {
if !c.initialized { if !c.initialized {
c.es = colorMatrixEsI() *c = colorMI
c.initialized = true
} }
result := ColorM{} result := ColorM{}
add(&other, c, &result) add(&other, c, &result)
@ -87,8 +86,7 @@ func (c *ColorM) Add(other ColorM) ColorM {
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) { func (c *ColorM) SetElement(i, j int, element float64) {
if !c.initialized { if !c.initialized {
c.es = colorMatrixEsI() *c = colorMI
c.initialized = true
} }
c.es[i][j] = element c.es[i][j] = element
} }

View File

@ -21,6 +21,14 @@ import (
// GeoMDim is a dimension of a GeoM. // GeoMDim is a dimension of a GeoM.
const GeoMDim = 3 const GeoMDim = 3
var geoMI = GeoM{
initialized: true,
es: [2][3]float64{
{1, 0, 0},
{0, 1, 0},
},
}
// 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.
@ -29,13 +37,6 @@ type GeoM struct {
es [GeoMDim - 1][GeoMDim]float64 es [GeoMDim - 1][GeoMDim]float64
} }
func geometryMatrixEsI() [GeoMDim - 1][GeoMDim]float64 {
return [GeoMDim - 1][GeoMDim]float64{
{1, 0, 0},
{0, 1, 0},
}
}
func (g GeoM) dim() int { func (g GeoM) dim() int {
return GeoMDim return GeoMDim
} }
@ -55,8 +56,7 @@ func (g GeoM) Element(i, j int) float64 {
// This returns g. // This returns g.
func (g *GeoM) Concat(other GeoM) GeoM { func (g *GeoM) Concat(other GeoM) GeoM {
if !g.initialized { if !g.initialized {
g.es = geometryMatrixEsI() *g = geoMI
g.initialized = true
} }
result := GeoM{} result := GeoM{}
mul(&other, g, &result) mul(&other, g, &result)
@ -68,8 +68,7 @@ func (g *GeoM) Concat(other GeoM) GeoM {
// This returns g. // This returns g.
func (g *GeoM) Add(other GeoM) GeoM { func (g *GeoM) Add(other GeoM) GeoM {
if !g.initialized { if !g.initialized {
g.es = geometryMatrixEsI() *g = geoMI
g.initialized = true
} }
result := GeoM{} result := GeoM{}
add(&other, g, &result) add(&other, g, &result)
@ -80,8 +79,7 @@ func (g *GeoM) Add(other GeoM) GeoM {
// 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.initialized { if !g.initialized {
g.es = geometryMatrixEsI() *g = geoMI
g.initialized = true
} }
g.es[i][j] = element g.es[i][j] = element
} }