Skip to content

Commit

Permalink
use new flag parser in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
unixpickle committed Apr 6, 2024
1 parent 182c2e9 commit 0d06b07
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
21 changes: 11 additions & 10 deletions cli/profile_mesh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] <input_image> <output.stl>\n", os.Args[0])
fmt.Fprintln(os.Stderr)
Expand All @@ -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)
}
24 changes: 13 additions & 11 deletions examples/experiments/plot_polar_shape/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 22 additions & 34 deletions examples/romantic/coin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion toolbox3d/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 0d06b07

Please sign in to comment.