forked from ImperialCollegeLondon/multifluids_icferst
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclean_mayavi_mesh.py
executable file
·103 lines (69 loc) · 2.64 KB
/
clean_mayavi_mesh.py
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
#!/usr/bin/env python3
import re
import sys
import math
from optparse import OptionParser
line_re = re.compile(r'''^(?P<x>\d+\.\d+)\s+(?P<y>\d+\.\d+)\s+L''')
bb_re=re.compile(r'''^%%BoundingBox:''')
linestyle_re = re.compile(r'''^\d+\s+setlinecap\s+\d+\s+setlinejoin\s*\n$''')
class PostscriptError(Exception):
"""Exception for malformed Postscript."""
pass
def find_bounding_box(ps, margin=10):
'''Find the ends of all the lines in the mesh and set the bounding box
to slightly larger than their hull'''
bb=[float('inf'),float('inf'),-float('inf'),-float('inf')]
for line in ps:
m=line_re.match(line)
if m:
x=float(m.group("x"))
y=float(m.group("y"))
bb[0]=min(bb[0],x)
bb[2]=max(bb[2],x)
bb[1]=min(bb[1],y)
bb[3]=max(bb[3],y)
bb[0]=int(math.floor(bb[0])-margin)
bb[1]=int(math.floor(bb[1])-margin)
bb[2]=int(math.ceil(bb[2])+margin)
bb[3]=int(math.ceil(bb[3])+margin)
return bb
def set_bounding_box(ps, margin):
bb=find_bounding_box(ps, margin)
for i in range(len(ps)):
if (bb_re.match(ps[i])):
ps[i]="%%BoundingBox: "+" ".join(map(str,bb))+"\n"
# Once we find the bounding box, we can stop.
return
raise PostscriptError("No bounding box in input file.")
def set_linestyle(ps):
for i in range(len(ps)):
if (linestyle_re.match(ps[i])):
ps[i]="1 setlinecap 1 setlinejoin\n"
return
raise PostscriptError("No line style parameters in input file.")
def process_file(inname, outname, options):
ps=open(inname,'r').readlines()
set_bounding_box(ps,options.margin)
set_linestyle(ps)
open(outname,"w").writelines(ps)
if __name__=='__main__':
optparser=OptionParser(usage='''usage: %prog [options] <input_filename> <output_filename>
This program cleans up vector eps mesh images output by Mayavi2.
The input file should be an eps file output by the "save scene as"
"Vector PS/EPS/PDF/TeX" option in Mayavi2.''',
add_help_option=True)
optparser.add_option("--margin",
action="store",
type="int",
dest="margin",
default=10,
help="margin around the mesh in points. Default is 10.")
optparser.set_defaults(dir=".", project="")
(options, argv) = optparser.parse_args()
try:
infile=argv[0]
outfile=argv[1]
except IndexError:
optparser.print_help()
sys.exit(1)
process_file(infile, outfile, options)