-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomesh.go
132 lines (118 loc) · 2.47 KB
/
gomesh.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gomesh
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type nextLineFunc func() string
func nextLineReader(scanner *bufio.Scanner) nextLineFunc {
return func() string {
scanner.Scan()
return scanner.Text()
}
}
type Msh struct {
Version string
IsAscii bool
DataSize int
Nodes []Node
Elements []Element
}
type Node struct {
Tag, X, Y, Z string
}
func NewNode(tag, x, y, z string) Node {
return Node{
Tag: tag,
X: x,
Y: y,
Z: z,
}
}
type Element struct {
Tag, Type string
Tags []string
NodeTags []string
Data []string
}
func NewElement(tag, typ string, tags, nodeTags []string) Element {
return Element{
Tag: tag,
Type: typ,
Tags: tags,
NodeTags: nodeTags,
}
}
func (m *Msh) SetElementData(data map[string][]string) {
ind := map[string]int{}
for i, el := range m.Elements {
ind[el.Tag] = i
}
for k, v := range data {
m.Elements[ind[k]].Data = v
}
}
func ReadVersion(filename string) (string, bool, int, error) {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
switch line {
case "$MeshFormat":
return ParseFormat( nextLineReader(scanner) )
}
}
return "", false, 0, errors.New("Mesh Format not found ")
}
func Parse(filename string) (*Msh, error) {
version, _, _, err := ReadVersion(filename)
if err != nil {
log.Fatalf("Failed read version %v", err)
}
//0 Some mesh files out there have the version specified as version "2" when it really is
// "2.2". Same with "4" vs "4.1".
switch version {
case "2":
return Parse22(filename)
case "2.2":
return Parse22(filename)
case "4.0":
// implement me?
break
case "4":
return Parse41(filename)
case "4.1":
return Parse41(filename)
}
return nil, fmt.Errorf("version is not recognized %v", version)
}
func ParseFormat(nextLine nextLineFunc) (string, bool, int, error) {
line := nextLine()
x := strings.Split(line, " ")
if len(x) != 3 {
return "", false, 0, errors.New("format len!=3: " + line)
}
isAscii := false
switch x[1] {
case "0":
isAscii = true
case "1":
isAscii = false
default:
return "", false, 0, errors.New("invalid isAscii value in the format: " + line)
}
datasize, err := strconv.Atoi(x[2])
if err != nil {
return "", false, 0, errors.New("invalid datasize value in the format: " + line)
}
nextLine() // read last $EndMeshFormat line
return x[0], isAscii, datasize, nil
}