scimba_torch.geometry.monte_carlo_hypersurface

A module for sampling hypersurfaces.

Classes

HyperSurfaceSampler([points_file, ...])

Sampler for HyperSurfaces.

class HyperSurfaceSampler(points_file=None, parametric_hyper_surface=None, bounding_domain=None, **kwargs)[source]

Bases: DomainSampler

Sampler for HyperSurfaces.

It is constructed either from a .txt files containing points on the hypersurface, or from a ParametricHyperSurface.

Parameters:
  • points_file (str | None) – A .txt file of points on the curve, default to None.

  • parametric_hyper_surface (ParametricHyperSurface | None) – a parametric HyperSurface, default to None. One among points_file, parametric_hyper_surface must be provided.

  • bounding_domain (VolumetricDomain | list[tuple[float, float]] | Tensor | None) – a bounding domain for the surface. If None whereas parametric_hyper_surface is given, estimated by sampling.

  • **kwargs – arbitrary keyword arguments

Keyword Arguments:
  • nb_points_for_estimation – in case where bounding box is estimated, number of points for estimation; default in 10 000.

  • inflation_for_estimation – in case where bounding box is estimated, inflation factor used after estimation by sampling.

Raises:

ValueError – Arguments are not correct.

Examples: Sampling from a hypersurface defined parametrically

import matplotlib.pyplot as plt

from scimba_torch.geometry.monte_carlo_hypersurface import (
    HyperSurfaceSampler,
)
from scimba_torch.geometry.utils import (
    write_points_normals_to_file,
)

bean_2d = ParametricHyperSurface.bean_2d()
bean_2d_bb = [(-0.4, 1.2), (-1.2, 0.4)]

sampler_from_surf = HyperSurfaceSampler(
    points_file=None,
    parametric_hyper_surface=bean_2d,
    bounding_domain=bean_2d_bb,
)

points_in = sampler_from_surf.sample(1000)

points, normals = sampler_from_surf.bc_sample(1000)

points_in_np = points_in.x.cpu().detach().numpy()
points_np = points.x.cpu().detach().numpy()
normals_np = normals.x.cpu().detach().numpy()

plt.figure(figsize=(7, 7))
plt.scatter(points_in_np[:, 0], points_in_np[:, 1], s=1, label="inside")
plt.scatter(points_np[:, 0], points_np[:, 1], s=1, color="red", label="bc")
plt.quiver(
    points_np[::20, 0],
    points_np[::20, 1],
    normals_np[::20, 0],
    normals_np[::20, 1],
    color="red",
    label="normals",
    alpha=0.5,
)
plt.legend()

plt.show()

points_, normals_ = bean_2d.sample(1000)
write_points_normals_to_file(points_, normals_, "test.xy")
sampler_from_file = HyperSurfaceSampler(
    points_file="test.xy",
    parametric_hyper_surface=None,
    bounding_domain=None,
)

points_in = sampler_from_file.sample(1000)

points, normals = sampler_from_file.bc_sample(1000)

points_in_np = points_in.x.cpu().detach().numpy()
points_np = points.x.cpu().detach().numpy()
normals_np = normals.x.cpu().detach().numpy()

plt.figure(figsize=(7, 7))
plt.scatter(points_in_np[:, 0], points_in_np[:, 1], s=1, label="inside")
plt.scatter(points_np[:, 0], points_np[:, 1], s=1, color="red", label="bc")
plt.quiver(
    points_np[::20, 0],
    points_np[::20, 1],
    normals_np[::20, 0],
    normals_np[::20, 1],
    color="red",
    label="normals",
    alpha=0.5,
)
plt.legend()

plt.show()

sampler_from_file = HyperSurfaceSampler(
    points_file="test.xy",
    parametric_hyper_surface=None,
    bounding_domain=[(-1.0, 2.0), (-2.0, 1.0)],
)

points_in = sampler_from_file.sample(1000)

points, normals = sampler_from_file.bc_sample(1000)

points_in_np = points_in.x.cpu().detach().numpy()
points_np = points.x.cpu().detach().numpy()
normals_np = normals.x.cpu().detach().numpy()

plt.figure(figsize=(7, 7))
plt.scatter(points_in_np[:, 0], points_in_np[:, 1], s=1, label="inside")
plt.scatter(points_np[:, 0], points_np[:, 1], s=1, color="red", label="bc")
plt.quiver(
    points_np[::20, 0],
    points_np[::20, 1],
    normals_np[::20, 0],
    normals_np[::20, 1],
    color="red",
    label="normals",
    alpha=0.5,
)
plt.legend()

plt.show()
bc_sample(n)[source]

Samples n points on the hypersurface.

Parameters:

n (int | list[int]) – Number of points to sample.

Return type:

tuple[LabelTensor, LabelTensor]

Returns:

A tuple of tensors of sampled points and normals.

Raises:

NotImplementedError – when the first argument is a list