Skip to content

Commit

Permalink
Fixed wave bugs with mirror and once. Added utility methods for using…
Browse files Browse the repository at this point in the history
… gradients with g2d
  • Loading branch information
jphsd committed Mar 25, 2023
1 parent 7cbb885 commit f2c1b87
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
17 changes: 17 additions & 0 deletions texture.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ func NewRGBA(width, height int, src ColorField, ox, oy, dx, dy float64) *image.R
return img
}

// NewGray16 renders the src ColorField into a new Gray16 image.
func NewGray16(width, height int, src ColorField, ox, oy, dx, dy float64) *image.Gray16 {
img := image.NewGray16(image.Rect(0, 0, width, height))
y := oy
for r := 0; r < height; r++ {
x := ox
for c := 0; c < width; c++ {
v := src.Eval2(x, y)
img.Set(c, r, v)
x += dx
}
y += dy
}

return img
}

// TextureRGBA is a lazily evaluated RGBA image. For expensive textures this allows only the requested pixels
// to be calculated, and not the entire image.
type TextureRGBA struct {
Expand Down
69 changes: 69 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package texture

import (
"github.com/jphsd/graphics2d"
"github.com/jphsd/graphics2d/image"
"image/color"
"math"
)

// Utility functions for producing gradient images for use with graphics2d

func NewLinearGray16(w, h int, p1, p2 []float64, wf *NonLinear, mirror, once bool) *TextureGray16 {
dx, dy := p2[0]-p1[0], p2[1]-p1[1]
th := math.Atan2(dy, dx)
lambda := math.Sqrt(dx*dx + dy*dy)

if wf == nil {
wf = NewNLLinear()
}
nl := NewNLWave([]float64{lambda}, []*NonLinear{wf}, mirror, once)
f1 := NewLinearGradient(nl)
xfm := graphics2d.NewAff3()
xfm.Rotate(-th)
xfm.Translate(-p1[0], -p1[1])
f2 := NewTransform(f1, xfm)
cf := NewColorGray(f2)
return NewTextureGray16(w, h, cf, 0, 0, 1, 1)
}

func NewRadialGray16(w, h int, c []float64, r float64, wf *NonLinear, mirror, once bool) *TextureGray16 {
if wf == nil {
wf = NewNLLinear()
}
nl := NewNLWave([]float64{r}, []*NonLinear{wf}, mirror, once)
f1 := NewRadialGradient(nl)
xfm := graphics2d.NewAff3()
xfm.Translate(-c[0], -c[1])
f2 := NewTransform(f1, xfm)
cf := NewColorGray(f2)
return NewTextureGray16(w, h, cf, 0, 0, 1, 1)
}

func NewConicGray16(w, h int, c []float64, th float64, wf *NonLinear) *TextureGray16 {
if wf == nil {
wf = NewNLLinear()
}
nl := NewNLWave([]float64{256}, []*NonLinear{wf}, true, false)
f1 := NewConicGradient(nl)
xfm := graphics2d.NewAff3()
xfm.Rotate(-th)
xfm.Translate(-c[0], -c[1])
f2 := NewTransform(f1, xfm)
cf := NewColorGray(f2)
return NewTextureGray16(w, h, cf, 0, 0, 1, 1)
}

// Tinting wrappers around the grayscale gradients

func NewLinearRGBA(w, h int, p1, p2 []float64, c1, c2 color.Color, wf *NonLinear, mirror, once bool) *image.Tinter {
return image.NewTinter(NewLinearGray16(w, h, p1, p2, wf, mirror, once), c1, c2)
}

func NewRadialRGBA(w, h int, c []float64, r float64, c1, c2 color.Color, wf *NonLinear, mirror, once bool) *image.Tinter {
return image.NewTinter(NewRadialGray16(w, h, c, r, wf, mirror, once), c1, c2)
}

func NewConicRGBA(w, h int, c []float64, th float64, c1, c2 color.Color, wf *NonLinear) *image.Tinter {
return image.NewTinter(NewConicGray16(w, h, c, th, wf), c1, c2)
}
24 changes: 19 additions & 5 deletions wave.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ func (g *NLWave) Eval(v float64) float64 {
ov := v
r, v := MapValueToLambda(v, sum)

if g.Once && ((!g.Mirrored && r > 0) || (g.Mirrored && r > 1)) {
return -1
if g.Once {
if g.Mirrored && r > 1 {
return -1
}
if !g.Mirrored && r > 0 {
return 1
}
}

// Find nlf for v
Expand Down Expand Up @@ -104,7 +109,11 @@ func (g *NLWave) Eval(v float64) float64 {
}

func (g *NLWave) Lambda() float64 {
return g.CumLambda[len(g.Lambdas)-1]
l := g.CumLambda[len(g.Lambdas)-1]
if g.Mirrored {
l *= 2
}
return l
}

// Patterns
Expand Down Expand Up @@ -180,8 +189,13 @@ func (g *PatternWave) Eval(v float64) float64 {
ov := v
r, v := MapValueToLambda(v, sum)

if g.Once && ((!g.Mirrored && r > 0) || (g.Mirrored && r > 1)) {
return -1
if g.Once {
if g.Mirrored && r > 1 {
return -1
}
if !g.Mirrored && r > 0 {
return 1
}
}

// Find pattern for v
Expand Down

0 comments on commit f2c1b87

Please sign in to comment.