Skip to content

Commit

Permalink
[geom] Fix push(), Sphere.approximate_closest_surface()
Browse files Browse the repository at this point in the history
  • Loading branch information
holl- committed May 21, 2024
1 parent 93e576b commit 7c31497
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phi/geom/_geom_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def expel(geometry: Geometry,
signed_distance, vec_to_surface, *_ = geometry.approximate_closest_surface(location)
if not invert:
shift_amount = math.maximum(0, min_separation - signed_distance) # expel
direction = vec_to_surface / -signed_distance # this always points outward
direction = math.safe_div(vec_to_surface, -signed_distance) # this always points outward
else:
raise NotImplementedError
return location + direction * shift_amount
13 changes: 12 additions & 1 deletion phi/geom/_sphere.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Union, Dict, Tuple

from phi import math
from phiml.math import Shape, dual, PI
from phiml.math import Shape, dual, PI, non_channel
from ._geom import Geometry, _keep_vector, NO_GEOMETRY
from ..math import wrap, Tensor, expand
from ..math.magic import slicing_dict
Expand Down Expand Up @@ -104,6 +104,17 @@ def approximate_signed_distance(self, location: Union[Tensor, tuple]):
distance = math.sqrt(distance_squared)
return math.min(distance - self.radius, self.shape.instance) # union for instance dimensions

def approximate_closest_surface(self, location: Tensor) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor]:
center_delta = location - self.center
center_dist = math.vec_length(center_delta)
sgn_dist = center_dist - self.radius
normal = center_delta / center_dist
surface_pos = self.center + self.radius * normal
delta = surface_pos - location
face_index = expand(0, non_channel(location))
offset = normal.vector @ surface_pos.vector
return sgn_dist, delta, normal, offset, face_index

def sample_uniform(self, *shape: math.Shape):
raise NotImplementedError('Not yet implemented') # ToDo

Expand Down

0 comments on commit 7c31497

Please sign in to comment.