Bug fix: matrices' elements should be copied when assigning

This commit is contained in:
Hajime Hoshi 2014-12-26 11:43:31 +09:00
parent d46e551c5c
commit 8ae96875c9
5 changed files with 59 additions and 24 deletions

View File

@ -30,12 +30,12 @@ const ColorMatrixDim = 5
//
// The initial value is identity.
type ColorMatrix struct {
// es represents elements.
es *[ColorMatrixDim - 1][ColorMatrixDim]float64
initialized bool
es [ColorMatrixDim - 1][ColorMatrixDim]float64
}
func colorMatrixEsI() *[ColorMatrixDim - 1][ColorMatrixDim]float64 {
return &[ColorMatrixDim - 1][ColorMatrixDim]float64{
func colorMatrixEsI() [ColorMatrixDim - 1][ColorMatrixDim]float64 {
return [ColorMatrixDim - 1][ColorMatrixDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
@ -49,7 +49,7 @@ func (c ColorMatrix) dim() int {
// Element returns a value of a matrix at (i, j).
func (c ColorMatrix) Element(i, j int) float64 {
if c.es == nil {
if !c.initialized {
if i == j {
return 1
}
@ -60,8 +60,9 @@ func (c ColorMatrix) Element(i, j int) float64 {
// Concat multiplies a color matrix with the other color matrix.
func (c *ColorMatrix) Concat(other ColorMatrix) {
if c.es == nil {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
}
result := ColorMatrix{}
mul(&other, c, &result)
@ -70,8 +71,9 @@ func (c *ColorMatrix) Concat(other ColorMatrix) {
// Add adds a color matrix with the other color matrix.
func (c *ColorMatrix) Add(other ColorMatrix) {
if c.es == nil {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
}
result := ColorMatrix{}
add(&other, c, &result)
@ -80,8 +82,9 @@ func (c *ColorMatrix) Add(other ColorMatrix) {
// SetElement sets an element at (i, j).
func (c *ColorMatrix) SetElement(i, j int, element float64) {
if c.es == nil {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
}
c.es[i][j] = element
}
@ -92,7 +95,8 @@ func Monochrome() ColorMatrix {
const g = 23434.0 / 32768.0
const b = 2366.0 / 32768.0
return ColorMatrix{
&[ColorMatrixDim - 1][ColorMatrixDim]float64{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{r, g, b, 0, 0},
@ -104,7 +108,8 @@ func Monochrome() ColorMatrix {
// ScaleColor returns a color matrix that scales a color matrix by the given color (r, g, b, a).
func ScaleColor(r, g, b, a float64) ColorMatrix {
return ColorMatrix{
&[ColorMatrixDim - 1][ColorMatrixDim]float64{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
{r, 0, 0, 0, 0},
{0, g, 0, 0, 0},
{0, 0, b, 0, 0},
@ -116,7 +121,8 @@ func ScaleColor(r, g, b, a float64) ColorMatrix {
// TranslateColor returns a color matrix that translates a color matrix by the given color (r, g, b, a).
func TranslateColor(r, g, b, a float64) ColorMatrix {
return ColorMatrix{
&[ColorMatrixDim - 1][ColorMatrixDim]float64{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
{1, 0, 0, 0, r},
{0, 1, 0, 0, g},
{0, 0, 1, 0, b},
@ -132,7 +138,8 @@ func RotateHue(theta float64) ColorMatrix {
v2 := (1.0/3.0)*(1.0-cos) - math.Sqrt(1.0/3.0)*sin
v3 := (1.0/3.0)*(1.0-cos) + math.Sqrt(1.0/3.0)*sin
return ColorMatrix{
&[ColorMatrixDim - 1][ColorMatrixDim]float64{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
{v1, v2, v3, 0, 0},
{v3, v1, v2, 0, 0},
{v2, v3, v1, 0, 0},

View File

@ -48,3 +48,14 @@ func TestColorInit(t *testing.T) {
}
}
}
func TestColorAssign(t *testing.T) {
m := ScaleColor(1, 1, 1, 1) // Create elements explicitly
m2 := m
m.SetElement(0, 0, 0)
got := m2.Element(0, 0)
want := 1.0
if want != got {
t.Errorf("m2.Element(%d, %d) = %f, want %f", 0, 0, got, want)
}
}

View File

@ -25,12 +25,12 @@ const GeometryMatrixDim = 3
//
// The initial value is identity.
type GeometryMatrix struct {
// es represents elements.
es *[GeometryMatrixDim - 1][GeometryMatrixDim]float64
initialized bool
es [GeometryMatrixDim - 1][GeometryMatrixDim]float64
}
func geometryMatrixEsI() *[GeometryMatrixDim - 1][GeometryMatrixDim]float64 {
return &[GeometryMatrixDim - 1][GeometryMatrixDim]float64{
func geometryMatrixEsI() [GeometryMatrixDim - 1][GeometryMatrixDim]float64 {
return [GeometryMatrixDim - 1][GeometryMatrixDim]float64{
{1, 0, 0},
{0, 1, 0},
}
@ -42,7 +42,7 @@ func (g GeometryMatrix) dim() int {
// Element returns a value of a matrix at (i, j).
func (g GeometryMatrix) Element(i, j int) float64 {
if g.es == nil {
if !g.initialized {
if i == j {
return 1
}
@ -53,8 +53,9 @@ func (g GeometryMatrix) Element(i, j int) float64 {
// Concat multiplies a geometry matrix with the other geometry matrix.
func (g *GeometryMatrix) Concat(other GeometryMatrix) {
if g.es == nil {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
}
result := GeometryMatrix{}
mul(&other, g, &result)
@ -63,8 +64,9 @@ func (g *GeometryMatrix) Concat(other GeometryMatrix) {
// Add adds a geometry matrix with the other geometry matrix.
func (g *GeometryMatrix) Add(other GeometryMatrix) {
if g.es == nil {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
}
result := GeometryMatrix{}
add(&other, g, &result)
@ -73,8 +75,9 @@ func (g *GeometryMatrix) Add(other GeometryMatrix) {
// SetElement sets an element at (i, j).
func (g *GeometryMatrix) SetElement(i, j int, element float64) {
if g.es == nil {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
}
g.es[i][j] = element
}
@ -82,7 +85,8 @@ func (g *GeometryMatrix) SetElement(i, j int, element float64) {
// ScaleGeometry returns a matrix that scales a geometry matrix by (x, y).
func ScaleGeometry(x, y float64) GeometryMatrix {
return GeometryMatrix{
es: &[2][3]float64{
initialized: true,
es: [2][3]float64{
{x, 0, 0},
{0, y, 0},
},
@ -92,7 +96,8 @@ func ScaleGeometry(x, y float64) GeometryMatrix {
// TranslateGeometry returns a matrix taht translates a geometry matrix by (tx, ty).
func TranslateGeometry(tx, ty float64) GeometryMatrix {
return GeometryMatrix{
es: &[2][3]float64{
initialized: true,
es: [2][3]float64{
{1, 0, tx},
{0, 1, ty},
},
@ -103,7 +108,8 @@ func TranslateGeometry(tx, ty float64) GeometryMatrix {
func RotateGeometry(theta float64) GeometryMatrix {
sin, cos := math.Sincos(theta)
return GeometryMatrix{
es: &[2][3]float64{
initialized: true,
es: [2][3]float64{
{cos, -sin, 0},
{sin, cos, 0},
},

View File

@ -49,6 +49,17 @@ func TestGeometryInit(t *testing.T) {
}
}
func TestGeometryAssign(t *testing.T) {
m := ScaleGeometry(1, 1) // Create elements explicitly
m2 := m
m.SetElement(0, 0, 0)
got := m2.Element(0, 0)
want := 1.0
if want != got {
t.Errorf("m2.Element(%d, %d) = %f, want %f", 0, 0, got, want)
}
}
func TestGeometryConcat(t *testing.T) {
matrix1 := ScaleGeometry(2, 2)
matrix2 := TranslateGeometry(1, 1)

View File

@ -152,7 +152,7 @@ func (i *Image) Fill(clr color.Color) (err error) {
// The parts of the given image at the parts of the destination.
// After determining parts to draw, this applies the geometry matrix and the color matrix.
//
// If options is nil or its members are nil, the default values are used.
// Here are the default values:
// DstParts: (0, 0) - (source width, source height)
// SrcParts: (0, 0) - (source width, source height) (i.e. the whole source image)
// GeometryMatrix: Identity matrix