From 0d06b07a4e006111af72a6e8d7fff2ba4350e1d5 Mon Sep 17 00:00:00 2001 From: Alex Nichol Date: Fri, 5 Apr 2024 23:00:30 -0400 Subject: [PATCH] use new flag parser in examples --- cli/profile_mesh/main.go | 21 +++---- examples/experiments/plot_polar_shape/main.go | 24 ++++---- examples/romantic/coin/main.go | 56 ++++++++----------- toolbox3d/flags.go | 2 +- 4 files changed, 47 insertions(+), 56 deletions(-) diff --git a/cli/profile_mesh/main.go b/cli/profile_mesh/main.go index e982f6f9..ee701764 100644 --- a/cli/profile_mesh/main.go +++ b/cli/profile_mesh/main.go @@ -7,16 +7,17 @@ import ( "github.com/unixpickle/model3d/model2d" "github.com/unixpickle/model3d/model3d" + "github.com/unixpickle/model3d/toolbox3d" ) -func main() { - var smoothIters int - var mcDelta float64 - var thickness float64 +type Args struct { + SmoothIters int `default:"20" usage:"2d mesh smoothing iterations"` + Thickness float64 `default:"10.0" usage:"thickness of model (pixels)"` +} - flag.IntVar(&smoothIters, "smooth-iters", 20, "2d mesh smoothing iterations") - flag.Float64Var(&mcDelta, "delta", 0.5, "marching cubes delta (pixels)") - flag.Float64Var(&thickness, "thickness", 10.0, "thickness of model (pixels)") +func main() { + var args Args + toolbox3d.AddFlags(&args, nil) flag.Usage = func() { fmt.Fprintf(os.Stderr, "Usage: %s [flags] \n", os.Args[0]) fmt.Fprintln(os.Stderr) @@ -33,10 +34,10 @@ func main() { inFile, outFile := flag.Args()[0], flag.Args()[1] mesh2d := model2d.MustReadBitmap(inFile, nil).FlipY().Mesh() - if smoothIters > 0 { - mesh2d = mesh2d.SmoothSq(smoothIters) + if args.SmoothIters > 0 { + mesh2d = mesh2d.SmoothSq(args.SmoothIters) } - profile := model3d.ProfileMesh(mesh2d, 0, thickness) + profile := model3d.ProfileMesh(mesh2d, 0, args.Thickness) profile.SaveGroupedSTL(outFile) } diff --git a/examples/experiments/plot_polar_shape/main.go b/examples/experiments/plot_polar_shape/main.go index 8590a3e5..bf71cf1b 100644 --- a/examples/experiments/plot_polar_shape/main.go +++ b/examples/experiments/plot_polar_shape/main.go @@ -6,28 +6,30 @@ import ( "github.com/unixpickle/essentials" "github.com/unixpickle/model3d/model2d" + "github.com/unixpickle/model3d/toolbox3d" ) +type Args struct { + Image string `usage:"input image file to process"` + SmoothIters int `default:"50" usage:"mesh smoothing iterations"` + ContentCenter bool `usage:"use the center of the content instead of the whole image"` +} + func main() { - var inputImage string - var smoothIters int - var useContentCenter bool - flag.StringVar(&inputImage, "image", "", "input image file to process") - flag.IntVar(&smoothIters, "smooth-iters", 50, "mesh smoothing iterations") - flag.BoolVar(&useContentCenter, "content-center", false, - "use the center of the content instead of the whole image") + var args Args + toolbox3d.AddFlags(&args, nil) flag.Parse() - if inputImage == "" { + if args.Image == "" { essentials.Die("Missing -image flag. See -help.") } - bmp := model2d.MustReadBitmap(inputImage, nil) - mesh := bmp.Mesh().SmoothSq(smoothIters) + bmp := model2d.MustReadBitmap(args.Image, nil) + mesh := bmp.Mesh().SmoothSq(args.SmoothIters) collider := model2d.MeshToCollider(mesh) var center model2d.Coord - if useContentCenter { + if args.ContentCenter { center = collider.Min().Mid(collider.Max()) } else { center = model2d.XY(float64(bmp.Width), float64(bmp.Height)).Scale(0.5) diff --git a/examples/romantic/coin/main.go b/examples/romantic/coin/main.go index d4b9ec9d..e29c8d49 100644 --- a/examples/romantic/coin/main.go +++ b/examples/romantic/coin/main.go @@ -15,50 +15,38 @@ import ( "github.com/unixpickle/model3d/render3d" ) -func main() { - var outFile string - var renderFile string - var minHeight float64 - var maxHeight float64 - var msResolution float64 - var mcDelta float64 - var smoothIters int - var radius float64 - var template string - var rounded bool - var roundSamples int - - flag.StringVar(&outFile, "out", "coin.stl", "output file name") - flag.StringVar(&renderFile, "render", "rendering.png", "rendered output file name") - flag.Float64Var(&minHeight, "min-height", 0.1, "minimum height") - flag.Float64Var(&maxHeight, "max-height", 0.13, "maximum height") - flag.Float64Var(&msResolution, "resolution", 0.01, - "resolution of marching squares, relative to radius") - flag.Float64Var(&mcDelta, "mc-delta", 0.01, - "resolution of marching cubes when creating a rounded solid") - flag.IntVar(&smoothIters, "smooth-iters", 50, - "number of mesh smoothing iterations") - flag.Float64Var(&radius, "radius", 0.5, "radius of coin") - flag.StringVar(&template, "template", "example.png", "coin design image") - flag.BoolVar(&rounded, "rounded", false, "use a rounded design instead of flat") - flag.IntVar(&roundSamples, "round-samples", 10000, - "number of samples to use for rounded design") +type Args struct { + Out string `default:"coin.stl" usage:"output filename"` + Render string `default:"rendering.png" usage:"rendered output filename"` + MinHeight float64 `default:"0.1" usage:"minimum height"` + MaxHeight float64 `default:"0.13" usage:"maximum height"` + Resolution float64 `default:"0.01" usage:"resolution of marching squares, relative to radius"` + McDelta float64 `default:"0.01" usage:"resolution of marching cubes when rounding the solid"` + SmoothIters int `default:"50" usage:"number of mesh smoothing iterations"` + Radius float64 `default:"0.5" usage:"radius of coin"` + Template string `default:"example.png" usage:"coin design image"` + Rounded bool `default:"false" usage:"use a rounded design instead of flat"` + RoundSamples int `default:"10000" usage:"number of samples to use for rounded design"` +} +func main() { + var args Args + toolbox3d.AddFlags(&args, nil) flag.Parse() log.Println("Creating 2D mesh from template...") - mesh := ReadTemplateIntoMesh(template, msResolution, smoothIters, radius) + mesh := ReadTemplateIntoMesh(args.Template, args.Resolution, args.SmoothIters, args.Radius) var mesh3d *model3d.Mesh - if rounded { - mesh3d = RoundedModel(mesh, minHeight, maxHeight, mcDelta, roundSamples) + if args.Rounded { + mesh3d = RoundedModel(mesh, args.MinHeight, args.MaxHeight, args.McDelta, args.RoundSamples) } else { - mesh3d = UnroundedModel(mesh, minHeight, maxHeight) + mesh3d = UnroundedModel(mesh, args.MinHeight, args.MaxHeight) } log.Println("Saving...") - essentials.Must(mesh3d.SaveGroupedSTL(outFile)) - essentials.Must(render3d.SaveRandomGrid(renderFile, mesh3d, 4, 4, 200, nil)) + essentials.Must(mesh3d.SaveGroupedSTL(args.Out)) + essentials.Must(render3d.SaveRandomGrid(args.Render, mesh3d, 4, 4, 200, nil)) } func UnroundedModel(mesh *model2d.Mesh, minHeight, maxHeight float64) *model3d.Mesh { diff --git a/toolbox3d/flags.go b/toolbox3d/flags.go index 45ce70ce..6a970453 100644 --- a/toolbox3d/flags.go +++ b/toolbox3d/flags.go @@ -89,7 +89,7 @@ func AddFlags(obj any, f *flag.FlagSet) { var defaultVal bool if defaultStr == "true" { defaultVal = true - } else if defaultStr != "false" { + } else if defaultStr != "false" && defaultStr != "" { panic(fmt.Sprintf("invalid boolean: %#v", defaultStr)) } f.BoolVar(val.FieldByIndex(field.Index).Addr().Interface().(*bool),