Skip to content

Commit

Permalink
update Builder, keep working on beziers
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 18, 2024
1 parent 67294ad commit 76f13d5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
4 changes: 0 additions & 4 deletions forge/textsdf/glyph.go

This file was deleted.

51 changes: 51 additions & 0 deletions forge/textsdf/glyph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package textsdf

import (
"testing"

"github.com/soypat/glgl/math/ms2"
"github.com/soypat/gsdf"
)

type glyph struct {
}

func TestABC(t *testing.T) {
bz := ms2.SplineBezier()
spline := []ms2.Vec{
{0, 0},
{1, 0},
{1, 0},
{1, 1},
}
// var points []ms2.Vec
var bld gsdf.Builder
var poly ms2.PolygonBuilder
circle := bld.NewCircle(0.1)
shape := bld.Translate2D(circle, spline[0].X, spline[1].Y)
for i := 0; i < len(spline); i += 4 {
v0, v1, v2, v3 := spline[4*i], spline[4*i+1], spline[4*i+2], spline[4*i+3]
shape = bld.Union2D(shape, bld.Translate2D(circle, v1.X, v1.Y))
shape = bld.Union2D(shape, bld.Translate2D(circle, v2.X, v2.Y))
for x := float32(0.0); x < 1; x += 1. / 64 {
vx := bz.Evaluate(x, v0, v1, v2, v3)
poly.Add(vx)

}
}
v, err := poly.AppendVecs(nil)
if err != nil {
t.Fatal(err)
}

shape = bld.Union2D(shape, bld.NewPolygon(v))
// _ = shape
// sdfcpu, err := gleval.NewCPUSDF2(shape)
// if err != nil {
// t.Fatal(err)
// }
// err = gsdfaux.RenderPNGFile("shape.png", sdfcpu, 512, nil)
// if err != nil {
// t.Fatal(err)
// }
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2
github.com/soypat/glgl v0.0.0-20241117161642-84ce0213c9ea
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2 h1:N/GkdilItOR9bBrGXIj+53DbxUzRnG1SF/aOkBQzlxs=
github.com/soypat/glgl v0.0.0-20241019203012-a2ad2ed164c2/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241117161642-84ce0213c9ea h1:bPlNmRe3fBJQPzqNWUN2ChFZJgGo+jk056+wpAzEX+w=
github.com/soypat/glgl v0.0.0-20241117161642-84ce0213c9ea/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
Expand Down
30 changes: 27 additions & 3 deletions gsdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,46 @@ const (
epstol = 6e-7
)

type Flags uint64

const (
// FlagNoDimensionPanic controls panicking behavior on invalid shape dimension errors.
// If set then these errors do not panic, instead storing the error for later inspection with [Builder.Err].
FlagNoDimensionPanic Flags = 1 << iota
)

// Builder wraps all SDF primitive and operation logic generation.
// Provides error handling strategies with panics or error accumulation during shape generation.
type Builder struct {
NoDimensionPanic bool
accumErrs []error
// flags is a bitfield controlling behaviour of Builder.
flags Flags
accumErrs []error
}

func (bld *Builder) Flags() Flags {
return bld.flags
}

func (bld *Builder) SetFlags(flags Flags) error {
bld.flags = flags
return nil
}

// Err returns errors accumulated during SDF primitive creation and operations. The returned error implements `Unwrap() []error`.
func (bld *Builder) Err() error {
if len(bld.accumErrs) == 0 {
return nil
}
return errors.Join(bld.accumErrs...)
}

// ClearErrors clears accumulated errors such that [Builder.Err] returns nil on next call.
func (bld *Builder) ClearErrors() {
bld.accumErrs = bld.accumErrs[:0]
}

func (bld *Builder) shapeErrorf(msg string, args ...any) {
if !bld.NoDimensionPanic {
if bld.flags&FlagNoDimensionPanic == 0 {
panic(fmt.Sprintf(msg, args...))
}
// bld.stacks = append(bld.stacks, string(debug.Stack()))
Expand Down

0 comments on commit 76f13d5

Please sign in to comment.