Skip to content

Commit 1102621

Browse files
authored
Merge pull request #272 from berendbaas/sample-with-seed
Adding an optional seed parameter to `random_points_on_mesh`
2 parents ebf40a9 + 80a5733 commit 1102621

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/random_points_on_mesh.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "default_types.h"
22
#include <igl/random_points_on_mesh.h>
3+
#include <igl/generate_default_urbg.h>
34
#include <nanobind/nanobind.h>
45
#include <nanobind/eigen/dense.h>
56
#include <nanobind/stl/tuple.h>
7+
#include <nanobind/stl/optional.h>
68

79
namespace nb = nanobind;
810
using namespace nb::literals;
@@ -13,11 +15,13 @@ namespace pyigl
1315
auto random_points_on_mesh(
1416
const Integer n,
1517
const nb::DRef<const Eigen::MatrixXN> &V,
16-
const nb::DRef<const Eigen::MatrixXI> &F)
18+
const nb::DRef<const Eigen::MatrixXI> &F,
19+
const std::optional<int> seed)
1720
{
21+
std::mt19937 urbg = seed.has_value() ? std::mt19937(*seed) : igl::generate_default_urbg();
1822
Eigen::VectorXI FI;
1923
Eigen::MatrixXN B,X;
20-
igl::random_points_on_mesh(n, V, F, B, FI, X);
24+
igl::random_points_on_mesh(n, V, F, B, FI, X, urbg);
2125
return std::make_tuple(B, FI, X);
2226
}
2327
}
@@ -32,6 +36,7 @@ void bind_random_points_on_mesh(nb::module_ &m)
3236
"n"_a,
3337
"V"_a,
3438
"F"_a,
39+
"seed"_a = nb::none(),
3540
R"(
3641
Randomly sample a mesh (V,F) n times.
3742

tests/test_all.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ def test_sample():
332332
I = igl.knn(X,X,1,point_indices,CH,CN,W)
333333
B,FI,P = igl.blue_noise(V,F,0.5)
334334

335+
# Test equal seed yields same result
336+
B1,I1,X1 = igl.random_points_on_mesh(10,V,F, 1)
337+
B1_,I1_,X1_ = igl.random_points_on_mesh(10,V,F, 1)
338+
assert np.all(B1 == B1_)
339+
assert np.all(I1 == I1_)
340+
assert np.all(X1 == X1_)
341+
# Test different seed yields different result
342+
B2,I2,X2 = igl.random_points_on_mesh(10,V,F,2)
343+
assert not np.all(B1 == B2)
344+
assert not np.all(I1 == I2)
345+
assert not np.all(X1 == X2)
346+
347+
335348
def test_curvature():
336349
V,F = igl.icosahedron()
337350
K = igl.gaussian_curvature(V,F)

0 commit comments

Comments
 (0)