-
Notifications
You must be signed in to change notification settings - Fork 14
/
writeascii.go
71 lines (65 loc) · 1.46 KB
/
writeascii.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
package stl
// This file defines functions to emit STL ASCII files.
import (
"fmt"
"io"
"strings"
)
func writeSolidASCII(w io.Writer, solid *Solid) error {
var writeErr error
_, writeErr = w.Write([]byte("solid " + escapeName(solid.Name)))
if writeErr != nil {
return writeErr
}
for _, triangle := range solid.Triangles {
writeErr = writeTriangleASCII(w, &triangle)
if writeErr != nil {
return writeErr
}
}
_, writeErr = w.Write([]byte("\nendsolid " + solid.Name + "\n"))
if writeErr != nil {
return writeErr
}
return nil
}
func escapeName(name string) string {
name = strings.ReplaceAll(name, "\r", "\\r")
name = strings.ReplaceAll(name, "\n", "\\n")
return name
}
func writeTriangleASCII(w io.Writer, t *Triangle) error {
var err error
_, err = w.Write([]byte("\nfacet normal "))
if err != nil {
return err
}
err = writePointString(w, &(t.Normal))
if err != nil {
return err
}
_, err = w.Write([]byte("\n outer loop"))
if err != nil {
return err
}
for i := 0; i < 3; i++ {
_, err = w.Write([]byte("\n vertex "))
if err != nil {
return err
}
err = writePointString(w, &(t.Vertices[i]))
if err != nil {
return err
}
}
_, err = w.Write([]byte("\n endloop\nendfacet"))
if err != nil {
return err
}
return nil
}
func writePointString(w io.Writer, p *Vec3) error {
// %v is the easiest way I know to write floats as compact as possible
_, err := w.Write([]byte(fmt.Sprintf("%v %v %v", p[0], p[1], p[2])))
return err
}