summaryrefslogtreecommitdiff
path: root/dat.go
blob: 042d848a7544cb8fc067480d485f4432abf684ab (plain)
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
package main

import (
	"bufio"
	"io"
	"strconv"
	"strings"
)

type Vertex struct{ X, Y, Z float64 }
type Patch []Vertex

func parseVertex(s string) []float64 {
	f := strings.Split(s, ",")
	p := make([]float64, len(f))
	for i, v := range f {
		p[i], _ = strconv.ParseFloat(v, 64)
	}
	return p
}

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) [][][]float64 {
	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([][]float64, m)
	for i := range vertices {
		if !scan.Scan() {
			return nil
		}
		vertices[i] = parseVertex(scan.Text())
	}

	// populate patches with vertices
	patches := make([][][]float64, n)
	for u, i := range indices {
		patches[u] = make([][]float64, len(i))
		for v, j := range i {
			patches[u][v] = vertices[j]
		}
	}

	return patches
}