summaryrefslogtreecommitdiff
path: root/dat.go
diff options
context:
space:
mode:
Diffstat (limited to 'dat.go')
-rw-r--r--dat.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/dat.go b/dat.go
new file mode 100644
index 0000000..cf1e545
--- /dev/null
+++ b/dat.go
@@ -0,0 +1,79 @@
+package main
+
+import (
+ "bufio"
+ "io"
+ "strconv"
+ "strings"
+)
+
+type Vertex struct {
+ X, Y, Z float64
+}
+
+type Patch []int
+
+type Surface [4][4]Vertex
+
+type Bezier []Vertex
+
+func ParseVertex(s string) (v Vertex) {
+ ss := strings.Split(s, ",")
+ if len(ss) != 3 {
+ return
+ }
+ v.X, _ = strconv.ParseFloat(ss[0], 64)
+ v.Y, _ = strconv.ParseFloat(ss[1], 64)
+ v.Z, _ = strconv.ParseFloat(ss[2], 64)
+ return
+}
+
+func ParsePatch(s string) (p Patch) {
+ f := strings.Split(s, ",")
+ for _, v := range f {
+ i, _ := strconv.ParseInt(v, 10, 64)
+ p = append(p, int(i-1))
+ }
+ return
+}
+
+func Parse(r io.Reader) (s []Surface) {
+ scan := bufio.NewScanner(r)
+
+ if !scan.Scan() {
+ return
+ }
+ n, _ := strconv.Atoi(scan.Text())
+
+ p := make([]Patch, n)
+ for i := 0; i < n; i++ {
+ if !scan.Scan() {
+ return
+ }
+ p[i] = ParsePatch(scan.Text())
+ }
+
+ if !scan.Scan() {
+ return
+ }
+ m, _ := strconv.Atoi(scan.Text())
+
+ v := make([]Vertex, m)
+ for i := 0; i < m; i++ {
+ if !scan.Scan() {
+ return
+ }
+ v[i] = ParseVertex(scan.Text())
+ }
+
+ s = make([]Surface, n)
+ for i, patch := range p {
+ for x := 0; x < 4; x++ {
+ for y := 0; y < 4; y++ {
+ s[i][x][y] = v[patch[x*4+y]]
+ }
+ }
+ }
+
+ return
+}