ply

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 18 Imported by: 2

README

PLY Format

PLY format implementation for interfacing with polyform meshes.

Saving and Loading

package example

import (
    "github.com/EliCDavis/polyform/formats/ply"
    "github.com/EliCDavis/vector/vector3"
)

func ExampleReadWrite() {
    mesh, _ := ply.Load("model.ply")
    scaledMesh := mesh.Scale(vector3.New(2., 2., 2.))
    ply.Save("scaled.ply", scaledMesh, ply.ASCII)
}

Documentation

Overview

Package ply implements the PLY file format and provides utilities for interacting with the data within the rest of polyform.

Index

Examples

Constants

View Source
const VertexElementName = "vertex"

Variables

This section is empty.

Functions

func Load

func Load(filepath string) (*modeling.Mesh, error)

Build a polyform mesh from the contents of the ply file specied at the filepath

func ReadMesh added in v0.2.0

func ReadMesh(reader io.Reader) (*modeling.Mesh, error)
Example
package main

import (
	"bytes"
	"log"

	"github.com/EliCDavis/polyform/formats/ply"
	"github.com/EliCDavis/polyform/modeling"
)

var exampleFile = []byte(`ply
format ascii 1.0
comment This is an example comment
obj_info This is example info
element vertex 3
property double x
property double y
property double z
end_header
0 0 0
0 1 0
1 0 0
1 1 0
`)

func main() {
	file := bytes.NewBuffer(exampleFile)

	mesh, _ := ply.ReadMesh(file)
	log.Println(mesh.Float3Attributes())

	positionData := mesh.Float3Attribute(modeling.PositionAttribute)
	for i := 0; i < positionData.Len(); i++ {
		log.Println(positionData.At(i).Format("%f %f %f"))
	}
}
Output:

func Save

func Save(plyPath string, meshToSave modeling.Mesh, format Format) error

Save writes the mesh to the path specified in PLY format

func Write added in v0.14.0

func Write(out io.Writer, model modeling.Mesh, format Format) error
Example
package main

import (
	"bytes"
	"log"

	"github.com/EliCDavis/polyform/formats/ply"
	"github.com/EliCDavis/vector/vector3"
)

var exampleFile = []byte(`ply
format ascii 1.0
comment This is an example comment
obj_info This is example info
element vertex 3
property double x
property double y
property double z
end_header
0 0 0
0 1 0
1 0 0
1 1 0
`)

func main() {
	file := bytes.NewBuffer(exampleFile)
	out := &bytes.Buffer{}

	mesh, _ := ply.ReadMesh(file)
	scaledMesh := mesh.Scale(vector3.New(2., 2., 2.))

	ply.Write(out, scaledMesh, ply.ASCII)
	log.Println(out.String())
}
Output:

Types

type Element

type Element struct {
	Name       string     `json:"name"`
	Count      int64      `json:"count"`
	Properties []Property `json:"properties"`
}

func (Element) Write

func (e Element) Write(out io.Writer) error

type Format

type Format string

Format of the data contained within the body of the PLY file

const (
	ASCII              Format = "ascii"
	BinaryBigEndian    Format = "binary_little_endian"
	BinaryLittleEndian Format = "binary_big_endian"
)

func (Format) String added in v0.14.0

func (f Format) String() string

Human readable string representing the format value

type Header struct {
	Format   Format    `json:"format"`
	Elements []Element `json:"elements"`
	Comments []string  `json:"comments"` // Provide informal descriptive and contextual metadata/information
	ObjInfo  []string  `json:"objInfo"`  // Object information (arbitrary text)
}

A PLY Header dictates how to interpret the rest of the file's contents, as well as containing any extra information stored in the comments and obj info

func ReadHeader added in v0.10.0

func ReadHeader(in io.Reader) (Header, error)

Builds a header from the contents of the reader passed in. Reading from the reader passed in stops once we recieve the "end_header" token

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/EliCDavis/polyform/formats/ply"
)

var exampleFile = []byte(`ply
format ascii 1.0
comment This is an example comment
obj_info This is example info
element vertex 3
property double x
property double y
property double z
end_header
0 0 0
0 1 0
1 0 0
1 1 0
`)

func main() {
	file := bytes.NewBuffer(exampleFile)

	header, _ := ply.ReadHeader(file)

	fmt.Println(header.Format)
	fmt.Println(header.Comments[0])
	fmt.Println(header.ObjInfo[0])

	ele := header.Elements[0]
	fmt.Printf("%s contains %d elements\n", ele.Name, ele.Count)
	fmt.Printf("\t%s", ele.Properties[0].Name())
	fmt.Printf("\t%s", ele.Properties[1].Name())
}
Output:

func (Header) Bytes added in v0.14.0

func (h Header) Bytes() []byte

Builds a byte array containing the header information in PLY format.

func (Header) TextureFiles added in v0.14.0

func (h Header) TextureFiles() []string

All texture files found within the comments section of the header

func (Header) Write added in v0.12.0

func (h Header) Write(out io.Writer) (err error)

Writes the contents of the header out in PLY format to the writer provided.

type ListProperty

type ListProperty struct {
	PropertyName string             // Name of the property
	CountType    ScalarPropertyType // Data type of the number used to define how many elements are in the list
	ListType     ScalarPropertyType // Data type of the elements in the list
}

func (ListProperty) Name

func (lp ListProperty) Name() string

Name of the property as found in the PLY header

func (ListProperty) Write

func (lp ListProperty) Write(out io.Writer) (err error)

Writes out the definition of the property in PLY format

type MeshReader added in v0.19.0

type MeshReader struct {
	// PLY Element containing the mesh attribute data on a per vertex basis
	//
	// example: "vertex"
	AttributeElement string

	// All defined translations from PLY data to mesh attributes
	Properties []PropertyReader

	// Whether or not to load extra ply data that wasn't defined by the
	// property readers
	LoadUnspecifiedProperties bool
}

Builds a modeling.Mesh from PLY data

func (MeshReader) Load added in v0.19.0

func (mr MeshReader) Load(file string) (*modeling.Mesh, error)

func (MeshReader) Read added in v0.19.0

func (mr MeshReader) Read(reader io.Reader) (*modeling.Mesh, error)

type MeshWriter added in v0.19.0

type MeshWriter struct {
	Format     Format
	Properties []PropertyWriter

	// Whether or not to save extra ply data that wasn't defined by the
	// property writers
	WriteUnspecifiedProperties bool
}

func (MeshWriter) Write added in v0.19.0

func (mw MeshWriter) Write(mesh modeling.Mesh, writer io.Writer) error

type Property

type Property interface {
	Name() string              // Name of the property as found in the PLY header
	Write(out io.Writer) error // Writes out the definition of the property in PLY format
}

type PropertyReader added in v0.19.0

type PropertyReader interface {
	// contains filtered or unexported methods
}

type PropertyWriter added in v0.19.0

type PropertyWriter interface {
	MeshQualifies(mesh modeling.Mesh) bool
	Properties() []Property
	// contains filtered or unexported methods
}

type ScalarProperty

type ScalarProperty struct {
	PropertyName string             `json:"name"` // Name of the property
	Type         ScalarPropertyType `json:"type"` // Property type
}

func (ScalarProperty) Name

func (sp ScalarProperty) Name() string

Name of the property as found in the PLY header

func (ScalarProperty) Size added in v0.2.0

func (sp ScalarProperty) Size() int

Size of the property on a per point basis when serialized to binary format

func (ScalarProperty) Write

func (sp ScalarProperty) Write(out io.Writer) (err error)

Writes out the definition of the property in PLY format

type ScalarPropertyType

type ScalarPropertyType string
const (
	Char   ScalarPropertyType = "char"
	UChar  ScalarPropertyType = "uchar" //  uint8
	Short  ScalarPropertyType = "short"
	UShort ScalarPropertyType = "ushort"
	Int    ScalarPropertyType = "int"
	UInt   ScalarPropertyType = "uint"
	Float  ScalarPropertyType = "float"
	Double ScalarPropertyType = "double"
)

func ParseScalarPropertyType added in v0.14.0

func ParseScalarPropertyType(str string) ScalarPropertyType

Attempts to interpret the string as some scalar property type, and panics if it can't.

func (ScalarPropertyType) Size added in v0.19.0

func (spt ScalarPropertyType) Size() int

type Vector1PropertyReader added in v0.19.0

type Vector1PropertyReader struct {
	ModelAttribute string
	PlyProperty    string
}

type Vector1PropertyWriter added in v0.19.0

type Vector1PropertyWriter struct {
	ModelAttribute string
	PlyProperty    string
	Type           ScalarPropertyType
}

func (Vector1PropertyWriter) MeshQualifies added in v0.19.0

func (vpw Vector1PropertyWriter) MeshQualifies(mesh modeling.Mesh) bool

func (Vector1PropertyWriter) Properties added in v0.19.0

func (vpw Vector1PropertyWriter) Properties() []Property

type Vector2PropertyReader added in v0.19.0

type Vector2PropertyReader struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
}

type Vector2PropertyWriter added in v0.19.0

type Vector2PropertyWriter struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
	Type           ScalarPropertyType
}

func (Vector2PropertyWriter) MeshQualifies added in v0.19.0

func (v3pw Vector2PropertyWriter) MeshQualifies(mesh modeling.Mesh) bool

func (Vector2PropertyWriter) Properties added in v0.19.0

func (v3pw Vector2PropertyWriter) Properties() []Property

type Vector3PropertyReader added in v0.19.0

type Vector3PropertyReader struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
	PlyPropertyZ   string
}

type Vector3PropertyWriter added in v0.19.0

type Vector3PropertyWriter struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
	PlyPropertyZ   string
	Type           ScalarPropertyType
}

func (Vector3PropertyWriter) MeshQualifies added in v0.19.0

func (v3pw Vector3PropertyWriter) MeshQualifies(mesh modeling.Mesh) bool

func (Vector3PropertyWriter) Properties added in v0.19.0

func (v3pw Vector3PropertyWriter) Properties() []Property

type Vector4PropertyReader added in v0.19.0

type Vector4PropertyReader struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
	PlyPropertyZ   string
	PlyPropertyW   string
	IgnorableW     bool
}

type Vector4PropertyWriter added in v0.19.0

type Vector4PropertyWriter struct {
	ModelAttribute string
	PlyPropertyX   string
	PlyPropertyY   string
	PlyPropertyZ   string
	PlyPropertyW   string
	Type           ScalarPropertyType
}

func (Vector4PropertyWriter) MeshQualifies added in v0.19.0

func (v4pw Vector4PropertyWriter) MeshQualifies(mesh modeling.Mesh) bool

func (Vector4PropertyWriter) Properties added in v0.19.0

func (v4pw Vector4PropertyWriter) Properties() []Property

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL