Skip to content

Commit 4fa8897

Browse files
update inflated sampling code for mesh (#351)
1 parent 0e0089b commit 4fa8897

File tree

1 file changed

+21
-90
lines changed

1 file changed

+21
-90
lines changed

ppsci/geometry/mesh.py

Lines changed: 21 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def uniform_boundary_points(self, n: int):
172172
"""Compute the equispaced points on the boundary."""
173173
return self.pysdf.sample_surface(n)
174174

175-
def inflated_random_points(self, n, distance, random="pseudo"):
175+
def inflated_random_points(self, n, distance, random="pseudo", criteria=None):
176176
if not isinstance(n, (tuple, list)):
177177
n = [n]
178178
if not isinstance(distance, (tuple, list)):
@@ -185,30 +185,20 @@ def inflated_random_points(self, n, distance, random="pseudo"):
185185
from ppsci.geometry import inflation
186186

187187
all_points = []
188+
all_areas = []
188189
for _n, _dist in zip(n, distance):
189190
inflated_mesh = Mesh(inflation.pymesh_inflation(self.py_mesh, _dist))
190-
cur_n = 0
191-
inflated_points = []
192-
while cur_n < _n:
193-
random_points = [
194-
sampler.sample(_n, 1, random) * (e[1] - e[0]) + e[0]
195-
for e in inflated_mesh.bounds
196-
]
197-
random_points = np.concatenate(random_points, axis=1)
198-
inner_mask = inflated_mesh.pysdf.contains(random_points)
199-
valid_random_points = random_points[inner_mask]
200-
201-
inflated_points.append(valid_random_points)
202-
cur_n += len(valid_random_points)
203-
204-
inflated_points = np.concatenate(inflated_points, axis=0)
205-
if cur_n > _n:
206-
inflated_points = inflated_points[:_n]
207-
all_points.append(inflated_points)
208-
209-
return np.concatenate(all_points, axis=0)
210-
211-
def inflated_random_boundary_points(self, n, distance, random="pseudo"):
191+
points, areas = inflated_mesh.random_points(_n, random, criteria)
192+
all_points.append(points)
193+
all_areas.append(areas)
194+
195+
all_points = np.concatenate(all_points, axis=0)
196+
all_areas = np.concatenate(all_areas, axis=0)
197+
return all_points, all_areas
198+
199+
def inflated_random_boundary_points(
200+
self, n, distance, random="pseudo", criteria=None
201+
):
212202
if not isinstance(n, (tuple, list)):
213203
n = [n]
214204
if not isinstance(distance, (tuple, list)):
@@ -225,58 +215,16 @@ def inflated_random_boundary_points(self, n, distance, random="pseudo"):
225215

226216
for _n, _dist in zip(n, distance):
227217
inflated_mesh = Mesh(inflation.pymesh_inflation(self.py_mesh, _dist))
228-
triangle_areas = area_of_triangles(
229-
inflated_mesh.v0, inflated_mesh.v1, inflated_mesh.v2
230-
)
231-
triangle_prob = triangle_areas / np.linalg.norm(triangle_areas, 1)
232-
triangle_index = np.arange(triangle_prob.shape[0])
233-
points_per_triangle = np.random.choice(triangle_index, _n, p=triangle_prob)
234-
points_per_triangle, _ = np.histogram(
235-
points_per_triangle, np.arange(triangle_prob.shape[0] + 1) - 0.5
236-
)
237-
238-
all_points_n = []
239-
all_normal_n = []
240-
all_area_n = []
241-
for index, nr_p in enumerate(points_per_triangle):
242-
if nr_p == 0:
243-
continue
244-
sampled_points = sample_in_triangle(
245-
inflated_mesh.v0[index],
246-
inflated_mesh.v1[index],
247-
inflated_mesh.v2[index],
248-
nr_p,
249-
random,
250-
)
251-
normal = np.tile(inflated_mesh.face_normal[index], [nr_p, 1])
252-
area = np.full([nr_p, 1], triangle_areas[index] / nr_p)
253-
254-
all_points_n.append(sampled_points)
255-
all_normal_n.append(normal)
256-
all_area_n.append(area)
257-
258-
all_points_n = np.concatenate(
259-
all_points_n, axis=0, dtype=paddle.get_default_dtype()
260-
)
261-
all_normal_n = np.concatenate(
262-
all_normal_n, axis=0, dtype=paddle.get_default_dtype()
263-
)
264-
all_area_n = np.concatenate(
265-
all_area_n, axis=0, dtype=paddle.get_default_dtype()
218+
points, normal, area = inflated_mesh.random_boundary_points(
219+
_n, random, criteria
266220
)
267-
all_area_n = np.full_like(all_area_n, all_area_n.sum() / _n)
221+
all_points.append(points)
222+
all_points.append(normal)
223+
all_points.append(area)
268224

269-
all_points.append(all_points_n)
270-
all_normal.append(all_normal_n)
271-
all_area.append(all_area_n)
272-
273-
all_points = np.concatenate(
274-
all_points, axis=0, dtype=paddle.get_default_dtype()
275-
)
276-
all_normal = np.concatenate(
277-
all_normal, axis=0, dtype=paddle.get_default_dtype()
278-
)
279-
all_area = np.concatenate(all_area, axis=0, dtype=paddle.get_default_dtype())
225+
all_point = np.concatenate(all_point, axis=0)
226+
all_normal = np.concatenate(all_normal, axis=0)
227+
all_area = np.concatenate(all_area, axis=0)
280228
return all_points, all_normal, all_area
281229

282230
def _approximate_area(
@@ -312,23 +260,6 @@ def _approximate_area(
312260

313261
return np.asarray(appr_areas, paddle.get_default_dtype())
314262

315-
# def precise_on_boundary(self, points: np.ndarray, normals: np.ndarray):
316-
# """judge whether points is accurately on boundary.
317-
318-
# Args:
319-
# points (np.ndarray): Points.
320-
# normals (np.ndarray): Normals for each points.
321-
322-
# Returns:
323-
# np.ndarray: If on boundary, true for yes, false for not.
324-
# """
325-
# EPS = 1e-6
326-
# points_pos_normals = points + normals * EPS
327-
# points_neg_normals = points - normals * EPS
328-
# pos_sdf = self.sdf_func(points_pos_normals)
329-
# neg_sdf = self.sdf_func(points_neg_normals)
330-
# return (pos_sdf * neg_sdf <= 0)[:, 0]
331-
332263
def random_boundary_points(self, n, random="pseudo", criteria=None):
333264
valid_areas = self._approximate_area(random, criteria)
334265
triangle_prob = valid_areas / np.linalg.norm(valid_areas, ord=1)

0 commit comments

Comments
 (0)