Skip to content

Commit f8ca81a

Browse files
authored
Merge pull request #100 from nschloe/random-seed
Random seed
2 parents 29a9399 + 97b1e95 commit f8ca81a

16 files changed

Lines changed: 114 additions & 46 deletions

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ mesh = pygalmesh.generate_mesh(s, cell_size=0.2)
5858

5959
# mesh.points, mesh.cells, ...
6060
```
61-
You can write the mesh using [meshio](https://github.com/nschloe/meshio), e.g.,
61+
You can write the mesh with
6262
```python
63-
import meshio
64-
meshio.write("out.vtk", mesh)
63+
mesh.write("out.vtk")
6564
```
65+
You can use any format supported by [meshio](https://github.com/nschloe/meshio).
66+
6667
The mesh generation comes with many more options, described
6768
[here](https://doc.cgal.org/latest/Mesh_3/). Try, for example,
6869
```python
@@ -367,7 +368,7 @@ vol = vol.reshape((Nx,Ny,Nz))
367368

368369

369370
mesh = pygalmesh.generate_from_array(vol, h, facet_distance=.2, cell_size=1.)
370-
meshio.write('breast.vtk',mesh)
371+
mesh.write('breast.vtk')
371372
```
372373

373374
In addition, we can specify different mesh sizes for each tissue type. The code below sets the mesh size to *1 mm* for the skin tissue (label `4`), *0.5 mm* for the vascular tissue (label `5`), and *2 mm* for all other tissues (`default`).
@@ -376,7 +377,7 @@ In addition, we can specify different mesh sizes for each tissue type. The code
376377
cell_sizes_map = {'default': 2., 4: 1., 5: .5}
377378
mesh = pygalmesh.generate_from_array_with_subdomain_sizing(
378379
vol, h, facet_distance=.2, cell_sizes_map=cell_sizes_map)
379-
meshio.write('breast_adapted.vtk',mesh)
380+
mesh.write('breast_adapted.vtk')
380381
```
381382

382383
#### Surface remeshing

pygalmesh/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import tempfile
33

4-
import meshio
54
from _pygalmesh import (
65
_generate_from_inr,
76
_generate_from_inr_with_subdomain_sizing,
@@ -13,6 +12,8 @@
1312
_remesh_surface,
1413
)
1514

15+
import meshio
16+
1617

1718
def generate_mesh(
1819
domain,
@@ -29,6 +30,7 @@ def generate_mesh(
2930
cell_radius_edge_ratio=0.0,
3031
cell_size=0.0,
3132
verbose=True,
33+
seed=0,
3234
):
3335
feature_edges = [] if feature_edges is None else feature_edges
3436

@@ -51,6 +53,7 @@ def generate_mesh(
5153
cell_radius_edge_ratio=cell_radius_edge_ratio,
5254
cell_size=cell_size,
5355
verbose=verbose,
56+
seed=seed,
5457
)
5558

5659
mesh = meshio.read(outfile)
@@ -73,6 +76,7 @@ def generate_with_sizing_field(
7376
cell_radius_edge_ratio=0.0,
7477
cell_size=None,
7578
verbose=True,
79+
seed=0,
7680
):
7781
feature_edges = [] if feature_edges is None else feature_edges
7882

@@ -95,6 +99,7 @@ def generate_with_sizing_field(
9599
cell_radius_edge_ratio=cell_radius_edge_ratio,
96100
cell_size=cell_size,
97101
verbose=verbose,
102+
seed=seed,
98103
)
99104

100105
mesh = meshio.read(outfile)
@@ -117,6 +122,7 @@ def generate_periodic_mesh(
117122
cell_size=0.0,
118123
number_of_copies_in_output=1,
119124
verbose=True,
125+
seed=0,
120126
):
121127
fh, outfile = tempfile.mkstemp(suffix=".mesh")
122128
os.close(fh)
@@ -139,6 +145,7 @@ def generate_periodic_mesh(
139145
cell_size=cell_size,
140146
number_of_copies_in_output=number_of_copies_in_output,
141147
verbose=verbose,
148+
seed=seed,
142149
)
143150

144151
mesh = meshio.read(outfile)
@@ -153,6 +160,7 @@ def generate_surface_mesh(
153160
radius_bound=0.0,
154161
distance_bound=0.0,
155162
verbose=True,
163+
seed=0,
156164
):
157165
fh, outfile = tempfile.mkstemp(suffix=".off")
158166
os.close(fh)
@@ -165,6 +173,7 @@ def generate_surface_mesh(
165173
radius_bound=radius_bound,
166174
distance_bound=distance_bound,
167175
verbose=verbose,
176+
seed=seed,
168177
)
169178

170179
mesh = meshio.read(outfile)
@@ -185,6 +194,7 @@ def generate_volume_mesh_from_surface_mesh(
185194
cell_radius_edge_ratio=0.0,
186195
cell_size=0.0,
187196
verbose=True,
197+
seed=0,
188198
):
189199
mesh = meshio.read(filename)
190200

@@ -209,6 +219,7 @@ def generate_volume_mesh_from_surface_mesh(
209219
cell_radius_edge_ratio=cell_radius_edge_ratio,
210220
cell_size=cell_size,
211221
verbose=verbose,
222+
seed=seed,
212223
)
213224

214225
mesh = meshio.read(outfile)
@@ -230,6 +241,7 @@ def generate_from_inr(
230241
cell_radius_edge_ratio=0.0,
231242
cell_size=0.0,
232243
verbose=True,
244+
seed=0,
233245
):
234246
fh, outfile = tempfile.mkstemp(suffix=".mesh")
235247
os.close(fh)
@@ -248,6 +260,7 @@ def generate_from_inr(
248260
cell_radius_edge_ratio=cell_radius_edge_ratio,
249261
cell_size=cell_size,
250262
verbose=verbose,
263+
seed=seed,
251264
)
252265

253266
mesh = meshio.read(outfile)
@@ -268,6 +281,7 @@ def generate_from_inr_with_subdomain_sizing(
268281
facet_distance=0.0,
269282
cell_radius_edge_ratio=0.0,
270283
verbose=True,
284+
seed=0,
271285
):
272286
fh, outfile = tempfile.mkstemp(suffix=".mesh")
273287
os.close(fh)
@@ -296,6 +310,7 @@ def generate_from_inr_with_subdomain_sizing(
296310
facet_distance=facet_distance,
297311
cell_radius_edge_ratio=cell_radius_edge_ratio,
298312
verbose=verbose,
313+
seed=seed,
299314
)
300315

301316
mesh = meshio.read(outfile)
@@ -310,6 +325,7 @@ def remesh_surface(
310325
facet_size=0.0,
311326
facet_distance=0.0,
312327
verbose=True,
328+
seed=0,
313329
):
314330
mesh = meshio.read(filename)
315331

@@ -328,6 +344,7 @@ def remesh_surface(
328344
facet_size=facet_size,
329345
facet_distance=facet_distance,
330346
verbose=verbose,
347+
seed=seed,
331348
)
332349

333350
mesh = meshio.read(outfile)
@@ -425,6 +442,7 @@ def generate_from_array_with_subdomain_sizing(
425442
facet_distance=0.0,
426443
cell_radius_edge_ratio=0.0,
427444
verbose=True,
445+
seed=0,
428446
):
429447
assert vol.dtype in ["uint8", "uint16"]
430448
fh, inr_filename = tempfile.mkstemp(suffix=".inr")
@@ -443,6 +461,7 @@ def generate_from_array_with_subdomain_sizing(
443461
facet_distance,
444462
cell_radius_edge_ratio,
445463
verbose,
464+
seed,
446465
)
447466
os.remove(inr_filename)
448467
return mesh

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pygalmesh
3-
version = 0.6.6
3+
version = 0.6.7
44
url = https://github.com/nschloe/pygalmesh
55
author = Nico Schlömer
66
email = nico.schloemer@gmail.com
@@ -27,7 +27,7 @@ classifiers =
2727
packages = find:
2828
setup_requires = pybind11 >= 2.2
2929
install_requires =
30-
importlib_metadata
30+
importlib_metadata;python_version<"3.8"
3131
meshio >= 4.0.0, < 5.0.0
3232
numpy
3333
pybind11 >= 2.2

src/generate.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ generate_mesh(
6060
const double facet_distance,
6161
const double cell_radius_edge_ratio,
6262
const double cell_size,
63-
const bool verbose
63+
const bool verbose,
64+
const int seed
6465
)
6566
{
67+
CGAL::get_default_random() = CGAL::Random(seed);
68+
6669
const double bounding_sphere_radius2 = bounding_sphere_radius > 0 ?
6770
bounding_sphere_radius*bounding_sphere_radius :
6871
// some wiggle room
@@ -134,9 +137,12 @@ generate_with_sizing_field(
134137
const double facet_distance,
135138
const double cell_radius_edge_ratio,
136139
const std::shared_ptr<pygalmesh::SizingFieldBase> & cell_size,
137-
const bool verbose
140+
const bool verbose,
141+
const int seed
138142
)
139143
{
144+
CGAL::get_default_random() = CGAL::Random(seed);
145+
140146
const double bounding_sphere_radius2 = bounding_sphere_radius > 0 ?
141147
bounding_sphere_radius*bounding_sphere_radius :
142148
// some wiggle room

src/generate.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ void generate_mesh(
2525
const double facet_distance = 0.0,
2626
const double cell_radius_edge_ratio = 0.0,
2727
const double cell_size = 0.0,
28-
const bool verbose = true
28+
const bool verbose = true,
29+
const int seed = 0
2930
);
3031

3132
void generate_with_sizing_field(
@@ -43,7 +44,8 @@ void generate_with_sizing_field(
4344
const double facet_distance = 0.0,
4445
const double cell_radius_edge_ratio = 0.0,
4546
const std::shared_ptr<pygalmesh::SizingFieldBase> & cell_size = nullptr,
46-
const bool verbose = true
47+
const bool verbose = true,
48+
const int seed = 0
4749
);
4850

4951
} // namespace pygalmesh

src/generate_from_inr.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ generate_from_inr(
4747
const double facet_distance,
4848
const double cell_radius_edge_ratio,
4949
const double cell_size,
50-
const bool verbose
50+
const bool verbose,
51+
const int seed
5152
)
5253
{
54+
CGAL::get_default_random() = CGAL::Random(seed);
55+
5356
CGAL::Image_3 image;
5457
const bool success = image.read(inr_filename.c_str());
5558
if (!success) {
@@ -95,9 +98,9 @@ void
9598
generate_from_inr_with_subdomain_sizing(
9699
const std::string & inr_filename,
97100
const std::string & outfile,
98-
const double default_cell_size,
101+
const double default_cell_size,
99102
const std::vector<double> & cell_sizes,
100-
const std::vector<int> & cell_labels,
103+
const std::vector<int> & cell_labels,
101104
const bool lloyd,
102105
const bool odt,
103106
const bool perturb,
@@ -107,9 +110,12 @@ generate_from_inr_with_subdomain_sizing(
107110
const double facet_size,
108111
const double facet_distance,
109112
const double cell_radius_edge_ratio,
110-
const bool verbose
113+
const bool verbose,
114+
const int seed
111115
)
112116
{
117+
CGAL::get_default_random() = CGAL::Random(seed);
118+
113119
CGAL::Image_3 image;
114120
const bool success = image.read(inr_filename.c_str());
115121
if (!success) {
@@ -120,7 +126,7 @@ generate_from_inr_with_subdomain_sizing(
120126
Sizing_field_cell cell_size(default_cell_size);
121127
const int ndimensions = 3;
122128
for(std::vector<double>::size_type i(0); i < cell_sizes.size(); ++i)
123-
cell_size.set_size(cell_sizes[i], ndimensions, cgal_domain.index_from_subdomain_index(cell_labels[i]));
129+
cell_size.set_size(cell_sizes[i], ndimensions, cgal_domain.index_from_subdomain_index(cell_labels[i]));
124130

125131
Mesh_criteria criteria(
126132
CGAL::parameters::edge_size=edge_size,

src/generate_from_inr.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ void generate_from_inr(
1919
const double facet_distance = 0.0,
2020
const double cell_radius_edge_ratio = 0.0,
2121
const double cell_size = 0.0,
22-
const bool verbose = true
22+
const bool verbose = true,
23+
const int seed = 0
2324
);
2425

2526
void
2627
generate_from_inr_with_subdomain_sizing(
2728
const std::string & inr_filename,
2829
const std::string & outfile,
29-
const double default_cell_size,
30+
const double default_cell_size,
3031
const std::vector<double> & cell_sizes,
31-
const std::vector<int> & cell_labels,
32+
const std::vector<int> & cell_labels,
3233
const bool lloyd = false,
3334
const bool odt = false,
3435
const bool perturb = true,
@@ -38,7 +39,8 @@ generate_from_inr_with_subdomain_sizing(
3839
const double facet_size = 0.0,
3940
const double facet_distance = 0.0,
4041
const double cell_radius_edge_ratio = 0.0,
41-
const bool verbose = true
42+
const bool verbose = true,
43+
const int seed = 0
4244
);
4345

4446
} // namespace pygalmesh

src/generate_from_off.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,24 @@ typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;
2929
// To avoid verbose function and named parameters call
3030
using namespace CGAL::parameters;
3131

32-
void generate_from_off(const std::string& infile, const std::string& outfile,
33-
const bool lloyd, const bool odt, const bool perturb,
34-
const bool exude, const double edge_size,
35-
const double facet_angle, const double facet_size,
36-
const double facet_distance,
37-
const double cell_radius_edge_ratio,
38-
const double cell_size, const bool verbose) {
32+
void generate_from_off(
33+
const std::string& infile,
34+
const std::string& outfile,
35+
const bool lloyd,
36+
const bool odt,
37+
const bool perturb,
38+
const bool exude,
39+
const double edge_size,
40+
const double facet_angle,
41+
const double facet_size,
42+
const double facet_distance,
43+
const double cell_radius_edge_ratio,
44+
const double cell_size,
45+
const bool verbose,
46+
const int seed
47+
) {
48+
CGAL::get_default_random() = CGAL::Random(seed);
49+
3950
// Create input polyhedron
4051
Polyhedron polyhedron;
4152
std::ifstream input(infile);

src/generate_from_off.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ generate_from_off(
2020
const double facet_distance = 0.0,
2121
const double cell_radius_edge_ratio = 0.0,
2222
const double cell_size = 0.0,
23-
const bool verbose = true
23+
const bool verbose = true,
24+
const int seed = 0
2425
);
2526

2627
} // namespace pygalmesh

src/generate_periodic.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ generate_periodic_mesh(
6464
const double cell_radius_edge_ratio,
6565
const double cell_size,
6666
const int number_of_copies_in_output,
67-
const bool verbose
67+
const bool verbose,
68+
const int seed
6869
)
6970
{
71+
CGAL::get_default_random() = CGAL::Random(seed);
72+
7073
K::Iso_cuboid_3 cuboid(
7174
bounding_cuboid[0],
7275
bounding_cuboid[1],

0 commit comments

Comments
 (0)