-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
header.go
89 lines (72 loc) · 1.96 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ply
import (
"bytes"
"fmt"
"io"
"strings"
)
// 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
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)
}
// Builds a byte array containing the header information in PLY format.
func (h Header) Bytes() []byte {
buf := &bytes.Buffer{}
h.Write(buf)
return buf.Bytes()
}
// All texture files found within the comments section of the header
func (h Header) TextureFiles() []string {
textures := make([]string, 0)
for _, c := range h.Comments {
contents := strings.Fields(c)
if len(contents) == 0 {
continue
}
if len(contents) < 2 {
continue
}
if strings.ToLower(contents[0]) != "texturefile" {
continue
}
start := strings.Index(strings.ToLower(c), "texturefile")
// len("texturefile") == 11
textures = append(textures, strings.TrimSpace(c[start+11:]))
}
return textures
}
// Writes the contents of the header out in PLY format to the writer provided.
func (h Header) Write(out io.Writer) (err error) {
switch h.Format {
case ASCII:
_, err = out.Write([]byte("ply\nformat ascii 1.0\n"))
case BinaryLittleEndian:
_, err = out.Write([]byte("ply\nformat binary_little_endian 1.0\n"))
case BinaryBigEndian:
_, err = out.Write([]byte("ply\nformat binary_big_endian 1.0\n"))
}
if err != nil {
return
}
for _, comment := range h.Comments {
_, err = fmt.Fprintf(out, "comment %s\n", comment)
if err != nil {
return
}
}
for _, info := range h.ObjInfo {
fmt.Fprintf(out, "obj_info %s\n", info)
}
for _, element := range h.Elements {
err = element.Write(out)
if err != nil {
return
}
}
_, err = out.Write([]byte("end_header\n"))
return
}