Commit 5d580e30 authored by dboe's avatar dboe
Browse files

tests are running again. Changed return signature of in_triangles for speed

parent 9469dd0e
Pipeline #105164 passed with stage
in 24 seconds
......@@ -26,6 +26,12 @@ class Triangles3D_Test(unittest.TestCase):
indices = self.inst.in_triangles(self.points, delta=self.delta)
self.assertTrue(np.array_equal(indices, self.expected_face_indices))
def test_bounding_box_and_mesh_in_faces(self):
tree = tfields.bounding_box.Searcher(self.mesh)
box_res = tree.in_faces(self.points, delta=self.delta)
usual_res = self.mesh.in_faces(self.points, delta=self.delta)
self.assertTrue(np.array_equal(box_res, usual_res))
class Triangles3D_Test_Raises(Triangles3D_Test):
def test_raises(self):
......@@ -96,6 +102,11 @@ class Triangles3D_TestDeltaIn(Triangles3D_TestDeltaOut):
self.expected_face_indices = np.array([0], dtype=int)
self.delta = 0.2
def test_bounding_box_and_mesh_in_faces(self):
# TODO-0(@dboe): this is a known issue. You have to allow delta for boxes in bounding box.
# NYI
pass
class Triangles3D_TestDeltaNone(Triangles3D_TestDeltaOut):
# if you set delta to None, the minimal distance point(s) are accepted automatically
......@@ -106,3 +117,8 @@ class Triangles3D_TestDeltaNone(Triangles3D_TestDeltaOut):
self.points = tfields.Points3D([self.point])
self.expected_face_indices = np.array([0], dtype=int)
self.delta = None
def test_bounding_box_and_mesh_in_faces(self):
# TODO-0(@dboe): this is a known issue. You have to allow delta for boxes in bounding box.
# NYI
pass
......@@ -434,8 +434,8 @@ class Searcher(Node):
def in_faces(self, tensors, delta=-1, assign_multiple=False):
"""
TODO:
* check rare case of point+-delta outside box
TODO-0:
* check case of point+-delta outside box!
Examples:
>>> import tfields
......@@ -454,12 +454,16 @@ class Searcher(Node):
if not self.is_root():
raise ValueError("in_faces may only be called by root Node.")
assert self.at_intersection == "keep", "Intersection method must be 'keep'"
assert not assign_multiple, "Not yet implemented. Does interfere with delta."
if delta == -1:
delta = self.delta
indices = np.full(tensors.shape[0], np.nan, dtype=int)
indices = np.full(tensors.shape[0], -1, dtype=int)
if self.mesh.nfaces() == 0:
return indices
if assign_multiple:
return [[-1] * len(indices)]
else:
return indices
with tensors.tmp_transform(self.mesh.coord_sys):
for i, point in enumerate(iter(tensors)):
......@@ -470,9 +474,9 @@ class Searcher(Node):
continue
leaf_mask = leaf.template.triangles()._in_triangles(point, delta)
original_face_indices = leaf.template.maps[3].fields[0][leaf_mask]
if not assign_multiple and len(original_face_indices) > 0:
original_face_indices = original_face_indices[:1]
indices[i] = original_face_indices
if len(original_face_indices):
indices[i] = original_face_indices[0]
# if len(original_face_indices) > 1 -> assign_multiple_faces
return indices
......
......@@ -556,13 +556,19 @@ class Mesh3D(tfields.TensorMaps):
hugely optimize 'in_faces' searches
Examples:
>>> import numpy as np
>>> import tfields
>>> mesh = tfields.Mesh3D.grid((0, 1, 3), (1, 2, 3), (2, 3, 3))
>>> _ = mesh.tree
>>> assert hasattr(mesh, '_cache')
>>> assert 'mesh_tree' in mesh._cache
>>> mask = mesh.in_faces(tfields.Points3D([[0.2, 1.2, 2.0]]),
>>> face_indices = mesh.in_faces(tfields.Points3D([[0.2, 1.2, 2.0]]),
... 0.00001)
>>> assert mask.sum() == 1 # one point in one triangle
You might want to know the number of points per face
>>> unique, counts = np.unique(face_indices, return_counts=True)
>>> dict(zip(unique, counts)) # one point on triangle number 16
{16: 1}
"""
if not hasattr(self, "_cache"):
self._cache = {}
......@@ -609,15 +615,11 @@ class Mesh3D(tfields.TensorMaps):
TODO:
fields template not yet implemented
"""
face_indices = np.arange(self.maps[3].shape[0])
cents = tfields.Tensors(sub_mesh.centroids())
mask = self.in_faces(cents, delta=None)
face_indices = self.in_faces(cents, delta=None)
inst = sub_mesh.copy()
if inst.maps:
scalars = []
for face_mask in mask:
scalars.append(face_indices[face_mask][0])
inst.maps[3].fields = [tfields.Tensors(scalars, dim=1)]
inst.maps[3].fields = [tfields.Tensors(face_indices, dim=1)]
else:
inst.maps = [
tfields.TensorFields([], tfields.Tensors([], dim=1), dim=3, dtype=int)
......
......@@ -609,7 +609,10 @@ class Triangles3D(tfields.TensorFields):
"""
indices = np.full(tensors.shape[0], -1, dtype=int)
if self.ntriangles() == 0:
return indices
if assign_multiple:
return [[-1] * len(indices)]
else:
return indices
with tensors.tmp_transform(self.coord_sys):
for i, point in enumerate(iter(tensors)):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment