internal/affine: Replace isInited with isIdentity

This commit is contained in:
Hajime Hoshi 2021-07-26 20:29:24 +09:00
parent d0ae73084b
commit 2c26e85183

View File

@ -72,8 +72,14 @@ func clamp(x float32) float32 {
return x return x
} }
func (c *ColorM) isInited() bool { func (c *ColorM) isIdentity() bool {
return c != nil && c.impl != nil if c == nil {
return true
}
if c.impl == nil {
return true
}
return c.impl.IsIdentity()
} }
func (c *colorMImplBodyTranslate) IsIdentity() bool { func (c *colorMImplBodyTranslate) IsIdentity() bool {
@ -81,7 +87,7 @@ func (c *colorMImplBodyTranslate) IsIdentity() bool {
} }
func (c *ColorM) ScaleOnly() bool { func (c *ColorM) ScaleOnly() bool {
if c == nil || c.impl == nil { if c.isIdentity() {
return true return true
} }
return c.impl.ScaleOnly() return c.impl.ScaleOnly()
@ -133,7 +139,7 @@ func (c *colorMImplBodyTranslate) ScaleOnly() bool {
} }
func (c *ColorM) Apply(clr color.Color) color.Color { func (c *ColorM) Apply(clr color.Color) color.Color {
if !c.isInited() { if c.isIdentity() {
return clr return clr
} }
return c.impl.Apply(clr) return c.impl.Apply(clr)
@ -168,7 +174,7 @@ func (c *colorMImplBodyTranslate) Apply(clr color.Color) color.Color {
} }
func (c *ColorM) UnsafeElements() ([]float32, []float32) { func (c *ColorM) UnsafeElements() ([]float32, []float32) {
if !c.isInited() { if c.isIdentity() {
return colorMIdentityBody[:], colorMIdentityTranslate[:] return colorMIdentityBody[:], colorMIdentityTranslate[:]
} }
return c.impl.UnsafeElements() return c.impl.UnsafeElements()
@ -179,7 +185,7 @@ func (c *colorMImplBodyTranslate) UnsafeElements() ([]float32, []float32) {
} }
func (c *ColorM) det() float32 { func (c *ColorM) det() float32 {
if !c.isInited() { if c.isIdentity() {
return 1 return 1
} }
return c.impl.det() return c.impl.det()
@ -225,7 +231,7 @@ func (c *ColorM) IsInvertible() bool {
// Invert inverts the matrix. // Invert inverts the matrix.
// If c is not invertible, Invert panics. // If c is not invertible, Invert panics.
func (c *ColorM) Invert() *ColorM { func (c *ColorM) Invert() *ColorM {
if !c.isInited() { if c.isIdentity() {
return nil return nil
} }
return c.impl.Invert() return c.impl.Invert()
@ -352,7 +358,7 @@ func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
newImpl := &colorMImplBodyTranslate{ newImpl := &colorMImplBodyTranslate{
body: colorMIdentityBody, body: colorMIdentityBody,
} }
if c.isInited() { if !c.isIdentity() {
*newImpl = *c.impl *newImpl = *c.impl
} }
if j < (ColorMDim - 1) { if j < (ColorMDim - 1) {
@ -366,48 +372,40 @@ func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
} }
func (c *ColorM) Equals(other *ColorM) bool { func (c *ColorM) Equals(other *ColorM) bool {
if !c.isInited() { if c.isIdentity() {
if !other.isInited() { return other.isIdentity()
return true }
} if other.isIdentity() {
return other.impl.IsIdentity() return false
} }
return c.impl.Equals(other) return c.impl.Equals(other)
} }
func (c *colorMImplBodyTranslate) Equals(other *ColorM) bool { func (c *colorMImplBodyTranslate) Equals(other *ColorM) bool {
lhsb := &colorMIdentityBody lhsb := &other.impl.body
lhst := &colorMIdentityTranslate lhst := &other.impl.translate
rhsb := &c.body rhsb := &c.body
rhst := &c.translate rhst := &c.translate
if other.isInited() {
lhsb = &other.impl.body
lhst = &other.impl.translate
}
return *lhsb == *rhsb && *lhst == *rhst return *lhsb == *rhsb && *lhst == *rhst
} }
// Concat multiplies a color matrix with the other color matrix. // Concat multiplies a color matrix with the other color matrix.
// This is same as muptiplying the matrix other and the matrix c in this order. // This is same as muptiplying the matrix other and the matrix c in this order.
func (c *ColorM) Concat(other *ColorM) *ColorM { func (c *ColorM) Concat(other *ColorM) *ColorM {
if !c.isInited() { if c.isIdentity() {
return other return other
} }
if !other.isInited() { if other.isIdentity() {
return c return c
} }
return c.impl.Concat(other) return c.impl.Concat(other)
} }
func (c *colorMImplBodyTranslate) Concat(other *ColorM) *ColorM { func (c *colorMImplBodyTranslate) Concat(other *ColorM) *ColorM {
lhsb := &colorMIdentityBody lhsb := &other.impl.body
lhst := &colorMIdentityTranslate lhst := &other.impl.translate
rhsb := &c.body rhsb := &c.body
rhst := &c.translate rhst := &c.translate
if other.isInited() {
lhsb = &other.impl.body
lhst = &other.impl.translate
}
return &ColorM{ return &ColorM{
impl: &colorMImplBodyTranslate{ impl: &colorMImplBodyTranslate{
@ -426,7 +424,7 @@ func (c *colorMImplBodyTranslate) Concat(other *ColorM) *ColorM {
// Scale scales the matrix by (r, g, b, a). // Scale scales the matrix by (r, g, b, a).
func (c *ColorM) Scale(r, g, b, a float32) *ColorM { func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
if !c.isInited() { if c.isIdentity() {
return getCachedScalingColorM(r, g, b, a) return getCachedScalingColorM(r, g, b, a)
} }
if c.ScaleOnly() { if c.ScaleOnly() {
@ -461,7 +459,7 @@ func (c *colorMImplBodyTranslate) Scale(r, g, b, a float32) *ColorM {
// Translate translates the matrix by (r, g, b, a). // Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float32) *ColorM { func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
if !c.isInited() { if c.isIdentity() {
return &ColorM{ return &ColorM{
impl: &colorMImplBodyTranslate{ impl: &colorMImplBodyTranslate{
body: colorMIdentityBody, body: colorMIdentityBody,