Repository navigation
Expand file tree
/
Copy pathmicrostructure_gen.py
More file actions
executable file
·213 lines (182 loc) · 7.16 KB
/
Copy pathmicrostructure_gen.py
File metadata and controls
executable file
·213 lines (182 loc) · 7.16 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
"""
Tile a given wire network pattern according to a grid or hex mesh.
"""
import argparse
import json
import numpy as np
import os.path
import pymesh
from pymesh.wires import WireNetwork, Tiler, Parameters, Inflator
from find_file import find_file
def parse_config_file(config_file):
""" syntax:
{
# opitional options:
"trim": bool,
"periodic": bool,
"subdiv": #,
"subdiv_method": "simple" or "loop"
"rel_geometry_correction": [#, #, #],
"abs_geometry_correction": [#, #, #],
"geometry_correction_cap": #,
"geometry_spread": #
"geometry_correction_lookup": filename,
"output_wire_network": filename,
# options for specifying parameters
"thickness": float,
"modifier_file": modifier_file,
"dof_file": dof_file,
# options needed by guide bbox
"wire_network": single_cell_wire_network,
"bbox_min": [min_x, min_y, min_z],
"bbox_max": [max_x, max_y, max_z],
"repeats": [x_reps, y_reps, z_reps],
# options needed by guide mesh
"wire_network": single_cell_wire_network,
"guide_mesh": guide_mesh,
"dof_type": "isotropic" | "orthotropic",
"thickness_type": "vertex" | "edge"
# options neede by mixed pattern tiling
"guide_mesh": guide_mesh,
"wire_list_file": wire_list_file,
}
"""
config_dir = os.path.dirname(config_file);
with open(config_file, 'r') as fin:
config = json.load(fin);
def convert_to_abs_path(field_name):
field = config[field_name];
if isinstance(field, str):
config[field_name] = str(find_file(field, config_dir));
if "wire_network" in config:
convert_to_abs_path("wire_network");
if "guide_mesh" in config:
convert_to_abs_path("guide_mesh");
if "modifier_file" in config:
convert_to_abs_path("modifier_file");
if "dof_file" in config:
convert_to_abs_path("dof_file");
if "wire_list_file" in config:
convert_to_abs_path("wire_list_file");
if "geometry_correction_lookup" in config:
convert_to_abs_path("geometry_correction_lookup");
return config;
def load_wire(wire_file):
network = WireNetwork();
network.load_from_file(wire_file);
return network;
def load_wires(wire_list_file):
root_dir = os.path.dirname(wire_list_file);
with open(wire_list_file, 'r') as fin:
wire_files = [str(name.strip()) for name in fin];
wire_files = [find_file(name, root_dir) for name in wire_files];
wires = [load_wire(name) for name in wire_files];
return wires;
def load_parameters(wire_network, config):
parameters = Parameters(wire_network, config["thickness"]);
if "modifier_file" in config:
if "dof_file" in config:
print("dof_file is shadowed by modifier file!");
parameters.load_modifier_file(config["modifier_file"]);
elif "dof_file" in config:
parameters.load_dof_file(config["dof_file"]);
return parameters;
def extract_options(config):
options = {
"trim": config.get("trim", False),
"periodic": config.get("periodic", False),
"subdiv": config.get("subdiv", 1),
"subdiv_method": str(config.get("subdiv_method", "simple")),
"rel_geometry_correction": config.get("rel_geometry_correction"),
"abs_geometry_correction": config.get("abs_geometry_correction"),
"geometry_correction_cap": config.get("geometry_correction_cap"),
"geometry_spread": config.get("geometry_spread"),
"geometry_correction_lookup": config.get("geometry_correction_lookup"),
}
return options;
def tile(config):
if "guide_mesh" in config:
if "wire_list_file" in config:
return tile_with_mixed_patterns(config);
else:
return tile_with_guide_mesh(config);
else:
return tile_with_guide_box(config);
def tile_with_guide_box(config):
options = extract_options(config);
network = load_wire(str(config["wire_network"]));
parameters = load_parameters(network, config);
tiler = Tiler();
tiler.set_base_pattern(network);
tiler.tile_with_guide_bbox(
config["bbox_min"][:network.dim],
config["bbox_max"][:network.dim],
config["repeats"][:network.dim],
parameters);
network = tiler.wire_network;
if config.get("trim", False):
network.trim();
if "output_wire_network" in config:
network.write_to_file(config["output_wire_network"]);
inflator = Inflator(network);
inflator.subdivide_order = options["subdiv"];
inflator.subdivide_method = options["subdiv_method"];
inflator.inflate(network.get_attribute("thickness").ravel(),
parameters.per_vertex_thickness);
return inflator.mesh;
def tile_with_guide_mesh(config):
options = extract_options(config);
network = load_wire(str(config["wire_network"]));
parameters = load_parameters(network, config);
guide_mesh = pymesh.load_mesh(config["guide_mesh"]);
tiler = Tiler();
tiler.set_base_pattern(network);
tiler.tile_with_guide_mesh(guide_mesh, parameters);
network = tiler.wire_network;
if config.get("trim", False):
network.trim();
if "output_wire_network" in config:
network.write_to_file(config["output_wire_network"]);
inflator = Inflator(network);
inflator.subdivide_order = options["subdiv"];
inflator.subdivide_method = options["subdiv_method"];
inflator.inflate(network.get_attribute("thickness").ravel(),
parameters.per_vertex_thickness);
return inflator.mesh;
def tile_with_mixed_patterns(config):
options = extract_options(config);
options["dof_type"] = str(config.get("dof_type", "isotropic"));
options["thickness_type"] = str(config.get("thickness_type", "vertex"));
networks = load_wires(str(config["wire_list_file"]));
guide_mesh = pymesh.load_mesh(config["guide_mesh"]);
per_vertex_thickness = options["thickness_type"] == "vertex";
tiler = Tiler();
tiler.tile_with_mixed_patterns(mesh,
per_vertex_thickness, options["dof_type"] == "isotropic");
network = tiler.wire_network;
if config.get("trim", False):
network.trim();
if "output_wire_network" in config:
network.write_to_file(config["output_wire_network"]);
inflator = Inflator(network);
inflator.subdivide_order = options["subdiv"];
inflator.subdivide_method = options["subdiv_method"];
inflator.inflate(network.get_attribute("thickness").ravel(),
per_vertex_thickness);
return inflator.mesh;
def parse_args():
parser = argparse.ArgumentParser(description=__doc__);
parser.add_argument("config_file", help="pattern configuration file.");
parser.add_argument("output", help="output mesh");
args = parser.parse_args();
return args;
def main():
args = parse_args();
config = parse_config_file(args.config_file);
if args.output is not None:
config["output"] = args.output;
mesh = tile(config);
pymesh.save_mesh(config["output"], mesh, *mesh.get_attribute_names());
if __name__ == "__main__":
main();