-
Notifications
You must be signed in to change notification settings - Fork 3
/
gmshToThree.coffee
63 lines (60 loc) · 2.23 KB
/
gmshToThree.coffee
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
module.exports = gmshToThree = (msh) ->
three =
metadata: formatVersion: 3
vertices: []
faces: []
materials: []
normals: []
colors: []
uvs: []
lines = msh.split '\n'
section = null
countDone = no
for line, i in lines
if line.indexOf('$End') == 0
unless line[4..] == section
throw "end of section #{line[4..]}, but current section is #{section}"
section = null
else if line.indexOf('$') == 0
if section?
throw "starting section #{line[1..]}, but already in section #{section}"
section = line[1..]
else
switch section
when 'MeshFormat'
unless line == '2.2 0 8'
throw "unsupported mesh format '#{line}', expected '2.2 0 8'"
else
# FIXME: Verify support for Comment sections.
if not countDone
countDone = yes
else
switch section
when 'Nodes'
parts = line.split(' ')
# By the way, the plus sign converts a string "12" into the
# number 12.
three.vertices.push +parts[1]
three.vertices.push +parts[2]
three.vertices.push +parts[3]
when 'Elements'
parts = line.split(' ')
numberOfTags = +parts[2]
switch +parts[1]
when 2 # triangle
three.faces.push 0
# <bai> andresj: sounds like your vertices are just in the
# wrong order. flip 'em and your faces will flip too. or if
# you have normals, those might be reversed too
three.faces.push +parts[numberOfTags + 5] - 1
three.faces.push +parts[numberOfTags + 4] - 1
three.faces.push +parts[numberOfTags + 3] - 1
when 3 # quadrangle
three.faces.push 1
three.faces.push +parts[numberOfTags + 6] - 1
three.faces.push +parts[numberOfTags + 5] - 1
three.faces.push +parts[numberOfTags + 4] - 1
three.faces.push +parts[numberOfTags + 3] - 1
else
# ignored, haha!
three