Skip to content

Commit

Permalink
ply-utils: new to-gltf command
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 31, 2024
1 parent 7b118f3 commit 587e9b7
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
7 changes: 7 additions & 0 deletions examples/ply-utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"

"github.com/EliCDavis/polyform/examples/ply-utils/properties"
"github.com/EliCDavis/polyform/formats/ply"
"github.com/EliCDavis/polyform/modeling"
"github.com/urfave/cli/v2"
)

Expand All @@ -13,6 +15,10 @@ func openPlyFile() (*os.File, error) {
return os.Open(inFilePath)
}

func getPlyFile() (*modeling.Mesh, error) {
return ply.Load(inFilePath)
}

func main() {

cmd := cli.App{
Expand All @@ -32,6 +38,7 @@ func main() {
},
Commands: []*cli.Command{
HeaderCommand,
ToGLTFCommand,
properties.PropertiesCommand,
},
}
Expand Down
48 changes: 48 additions & 0 deletions examples/ply-utils/to_gltf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"os"
"path/filepath"

"github.com/EliCDavis/polyform/formats/gltf"
"github.com/EliCDavis/polyform/modeling/meshops"
"github.com/urfave/cli/v2"
)

var ToGLTFCommand = &cli.Command{
Name: "to-gltf",
Usage: "converts a PLY file to gltf",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out",
Usage: "Path to write the GLTF to",
Aliases: []string{"o"},
Value: "out.glb",
},
},
Action: func(ctx *cli.Context) error {
mesh, err := getPlyFile()
if err != nil {
return err
}

meshPath := ctx.String("out")
err = os.MkdirAll(filepath.Dir(meshPath), 0777)
if err != nil {
return err
}

cleanedMesh := mesh.Transform(
meshops.VertexColorSpaceTransformer{
Transformation: meshops.VertexColorSpaceSRGBToLinear,
SkipOnMissingAttribute: true,
},
)

return gltf.Save(meshPath, gltf.PolyformScene{
Models: []gltf.PolyformModel{
{Name: "PLY", Mesh: &cleanedMesh},
},
})
},
}
6 changes: 4 additions & 2 deletions formats/ply/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func readLine(in io.Reader) (string, error) {
return "", err
}

if buf[0] == '\n' || buf[0] == '\r' {
if buf[0] == '\n' {
return data.String(), nil
}

data.WriteByte(buf[0])
if buf[0] != '\r' {
data.WriteByte(buf[0])
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions modeling/meshops/filter_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ func FilterFloat3(m modeling.Mesh, attribute string, filter func(v vector3.Float
check(RequireV3Attribute(m, attribute))

vertices := m.Float3Attribute(attribute)
verticeToKeep := make(map[int]struct{}, 0)
verticeToKeep := make([]bool, vertices.Len())

for i := 0; i < vertices.Len(); i++ {
if filter(vertices.At(i)) {
verticeToKeep[i] = struct{}{}
verticeToKeep[i] = true
}
}

indices := m.Indices()
finalIndices := make([]int, 0)
for i := 0; i < indices.Len(); i++ {
if _, ok := verticeToKeep[indices.At(i)]; ok {
if verticeToKeep[indices.At(i)] {
finalIndices = append(finalIndices, i)
}
}
Expand Down
29 changes: 29 additions & 0 deletions modeling/meshops/remove_unreferenced_vertices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package meshops_test

import (
"testing"

"github.com/EliCDavis/polyform/modeling"
"github.com/EliCDavis/polyform/modeling/meshops"
"github.com/stretchr/testify/assert"
)

func TestRemoveUnreferencedVertices(t *testing.T) {
// ARRANGE ================================================================
m := modeling.NewMesh(modeling.PointTopology, []int{1}).SetFloat1Attribute("test", []float64{1, 2})

// ACT ====================================================================
cleanedMesh := meshops.RemovedUnreferencedVertices(m)

// ASSERT =================================================================
indices := cleanedMesh.Indices()
assert.Equal(t, 1, indices.Len())
assert.Equal(t, 0, indices.At(0))

assert.Equal(t, 1, cleanedMesh.AttributeLength())
assert.True(t, cleanedMesh.HasFloat1Attribute("test"))

testAttr := cleanedMesh.Float1Attribute("test")
assert.Equal(t, 1, testAttr.Len())
assert.Equal(t, 2., testAttr.At(0))
}
8 changes: 6 additions & 2 deletions modeling/meshops/vertex_color_space.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const (
)

type VertexColorSpaceTransformer struct {
Attribute string
Transformation VertexColorSpaceTransformation
Attribute string
SkipOnMissingAttribute bool
Transformation VertexColorSpaceTransformation
}

func (vcst VertexColorSpaceTransformer) attribute() string {
Expand All @@ -26,6 +27,9 @@ func (vcst VertexColorSpaceTransformer) Transform(m modeling.Mesh) (results mode
attribute := getAttribute(vcst, modeling.ColorAttribute)

if err = RequireV3Attribute(m, attribute); err != nil {
if vcst.SkipOnMissingAttribute {
return m, nil
}
return
}

Expand Down

0 comments on commit 587e9b7

Please sign in to comment.