from __future__ import annotations
import math
from qewton.config.variables import Variable
from qewton.geometries.base import Geometry, DiscreteGeometry, Structure
from qewton.backends.base import TensorType, ComputingBackend
from qewton.backends import DEFAULT_DL_BACKEND
from qewton.config.devices import Device, cpu
from qewton.config.dtypes import Bool
[docs]
class PointCloud(DiscreteGeometry[TensorType]):
"""A discrete collection of points.
Args:
variable (Variable): The variable connected to this geometry.
discretization_points (TensorType): The discrete points contained in this
point cloud.
discretization_of (Geometry | None, optional): The geometry this
mesh is a discretization of. Defaults to None.
backend (type[ComputingBackend[TensorType]], optional):
Defaults to DEFAULT_DL_BACKEND.
"""
def __init__(
self,
variable: Variable,
discretization_points: TensorType,
discretization_of: Geometry | None = None,
backend: type[ComputingBackend[TensorType]] = DEFAULT_DL_BACKEND,
):
super().__init__(
# Excludes the trailing coordinate-component axis, same
# convention as MeshGeometry (mesh.vertices.shape[0]) and
# GridGeometry (point_grid.shape[:-1]) - a DiscreteGeometry's
# own shape is "which point", never the point's own coordinate
# components.
shape=discretization_points.shape[:-1],
variable=variable,
dim=variable.dim,
discretization_points=backend.build_tensor(discretization_points),
backend=backend,
)
self.discretization_of = discretization_of
@property
def structure(self) -> Structure:
return Structure.POINTS
@property
def topological_dim(self) -> int:
return 0
@property
def cells(self) -> None:
return None
def _move_data(self, device: Device | str) -> None:
self.discretization_points = self.backend.to(
self.discretization_points, device=device
)
def _get_volume(self):
bounding_box = self.bounding_box()
side_dist = bounding_box[1::2] - bounding_box[::2]
return math.prod(side_dist)
[docs]
def create_boundary(self):
raise NotImplementedError("Point clouds dont have a boundary.")
[docs]
def sample_grid_from_discretization(
self, n_points: int, device: Device | str = cpu
) -> TensorType:
target_device, device = device, self.device
# TODO: Could be done distance based
point_count = len(self.discretization_points)
if n_points <= point_count:
return self._transfer(self.discretization_points[:n_points], target_device)
reps = n_points // point_count
rem = n_points % point_count
base = self.backend.math.tile(self.discretization_points, (reps, 1))
extra_idx = self.backend.random.permutation(point_count, device=device)[:rem]
extra = self.discretization_points[extra_idx]
return self._transfer(self.backend.math.vstack([base, extra]), target_device)
[docs]
def sample_grid(self, n_points: int, device: Device | str = cpu) -> TensorType:
return self.sample_grid_from_discretization(n_points=n_points, device=device)
[docs]
def contains(self, points):
points_device = self._device_of(points)
points = self._transfer(points, self.device)
point_check = self.backend.math.zeros(
(len(points), 1), dtype=self.backend.dtypes[Bool], device=self.device
)
for i, p in enumerate(points):
point_check[i] = p in self.discretization_points
return self._transfer(point_check, points_device, warn=False)