Skip to content

Commit

Permalink
Added Ceil, Floor and Remap filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jphsd committed Sep 23, 2024
1 parent ee8dd5f commit e33865e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,18 @@ Filters are nodes that do something with the value supplied by their source.
They map values in [-1,1] to another in [-1,1].
Some filters take A and B parameters, in which case the value filtered is A*value+B
- [AbsFilter] applies [math.Abs] so the value will be in [0,1]
- [CeilFilter] limits the value to [-1,C]
- [ClipFilter] limits the value to [-1,1]
- [Convolution] calculates value by applying a kernel to the source
- [FloorFilter] limits the value to [C,1]
- [FoldFilter] 'folds' a value outside of [-1,1] back in on itself
- [InvertFilter] applies 0 - value
- Morphological [Erode], [Dilate], [EdgeIn], [EdgeOut], [Edge], [Close], [Open], [TopHat], [BottomHat]
- [NLFilter] applies a [NonLinear] to value
- [OffsScaleFilter] applies A * (B + value)
- [QuantizeFilter] quantizes the value
- [RandQuantFilter] like [QuantizeFilter] but the values are scrambled
- [RemapFilter] maps the value to the new domain [A,B]
# 5.2 Vector Filters (VF)
- [UnitVector] modifies the magnitude of the vector to 1
Expand Down
2 changes: 1 addition & 1 deletion example_decay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Example_decay() {
f2 := texture.NewLinearGradient(w3)

// Remap [-1,1] => [0,1]
f4 := texture.NewOffsScaleFilter(f2, 0.5, 1)
f4 := texture.NewRemapFilter(f2, 0, 1)

// Combine waves to get decay waveform
f3 := texture.NewMulCombiner(f, f4)
Expand Down
64 changes: 64 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,70 @@ func (f *RandQuantFilter) Eval2(x, y float64) float64 {
return f.M[k]
}

// RemapFilter holds the parameters for a linear filter remapping [-1,1] => [a,b].
type RemapFilter struct {
Name string
Src Field
A, B float64
}

func NewRemapFilter(src Field, a, b float64) *RemapFilter {
return &RemapFilter{"RemapFilter", src, a, b}
}

// Eval2 implements the Field interface.
func (f *RemapFilter) Eval2(x, y float64) float64 {
v := f.Src.Eval2(x, y)
t := (v + 1) / 2
return clamp(f.A*(1-t) + f.B*t)
}

// FloorFilter maps Av+B to [C, 1]
type FloorFilter struct {
Name string
Src Field
A, B float64
C float64
}

func NewFloorFilter(src Field, a, b float64, c float64) *FloorFilter {
return &FloorFilter{"FloorFilter", src, a, b, c}
}

// Eval2 implements the Field interface.
func (f *FloorFilter) Eval2(x, y float64) float64 {
v := f.Src.Eval2(x, y)
v *= f.A
v += f.B
if v < f.C {
return f.C
}
return clamp(v)
}

// CeilFilter maps Av+B to [-1,C]
type CeilFilter struct {
Name string
Src Field
A, B float64
C float64
}

func NewCeilFilter(src Field, a, b float64, c float64) *CeilFilter {
return &CeilFilter{"CeilFilter", src, a, b, c}
}

// Eval2 implements the Field interface.
func (f *CeilFilter) Eval2(x, y float64) float64 {
v := f.Src.Eval2(x, y)
v *= f.A
v += f.B
if v > f.C {
return f.C
}
return clamp(v)
}

func clamp(v float64) float64 {
if v < -1 {
return -1
Expand Down

0 comments on commit e33865e

Please sign in to comment.