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
|
package main
import (
"bufio"
"io"
"strconv"
"strings"
)
func parseVertex(s string) (v Vertex) {
f := strings.Split(s, ",")
if len(f) != 3 {
panic("invalid vertex")
}
v.X, _ = strconv.ParseFloat(f[0], 64)
v.Y, _ = strconv.ParseFloat(f[1], 64)
v.Z, _ = strconv.ParseFloat(f[2], 64)
return v
}
func parseIndex(s string) []int {
f := strings.Split(s, ",")
p := make([]int, len(f))
for i, v := range f {
x, _ := strconv.ParseInt(v, 10, 64)
p[i] = int(x - 1)
}
return p
}
func Parse(r io.Reader) []Patch {
scan := bufio.NewScanner(r)
// first part, patch indices
if !scan.Scan() {
return nil
}
n, _ := strconv.Atoi(scan.Text())
indices := make([][]int, n)
for i := range indices {
if !scan.Scan() {
return nil
}
indices[i] = parseIndex(scan.Text())
}
// second part, vertices
if !scan.Scan() {
return nil
}
m, _ := strconv.Atoi(scan.Text())
vertices := make([]Vertex, m)
for i := range vertices {
if !scan.Scan() {
return nil
}
vertices[i] = parseVertex(scan.Text())
}
// populate patches with vertices
patches := make([]Patch, n)
for u, i := range indices {
patches[u] = make([]Vertex, len(i))
for v, j := range i {
patches[u][v] = vertices[j]
}
}
return patches
}
|