Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement insert points #69

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/test_insert_points.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging
import numpy as np

from meshpy.tet import MeshInfo, Options, build

if __name__ == '__main__':
logger = logging.getLogger('test_insert_points.py')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a version of this to the (pytest-driven) tests under test/, to make sure this doesn't get inadvertently broken?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Please also take care of the Flake8 linter failures.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Fixed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't you mention in #70 that this PR was no longer needed? Could you explain?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But this appears to be the correct way to implement this and IMO it makes sense to cover tetgen's interface regardless of whether there are parts of it that are a little redundant. Some people looking for a wrapper library for tetgen may be familiar with one approach and not the other.


points = [(0, 0, 0),
(0, 0, 1),
(1, 0, 0),
(1, 0, 1),
(1, 1, 0),
(1, 1, 1),
(0, 1, 0),
(0, 1, 1)]

facets = [(0, 1, 3, 2),
(2, 3, 5, 4),
(4, 5, 7, 6),
(6, 7, 1, 0),
(0, 2, 4, 6),
(1, 3, 5, 7)]

mesh_info = MeshInfo()
mesh_info.set_points(points)
mesh_info.set_facets(facets)

# Insert an interior point of the cube as a constrained point
interior_point = (0.33, 0.7, 0.91)

insert_points_mesh_info = MeshInfo()
insert_points_mesh_info.set_points([interior_point])

mesh = build(mesh_info, max_volume=0.25,
insert_points=insert_points_mesh_info)

mesh_points = np.array(mesh.points)
min_dist = np.sqrt(np.sum((interior_point - mesh_points), axis=1)**2).min()

if min_dist > 0:
logger.error('tetrahedron mesh does not contain contrained point')
8 changes: 5 additions & 3 deletions meshpy/tet.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self, switches, **kwargs):
setattr(self, k, v)


def tetrahedralize(mesh_info, options):
def tetrahedralize(mesh_info, options, insert_points=None):
mesh = MeshInfo()

# restore "C" locale--otherwise tetgen might mis-parse stuff like "a0.01"
Expand All @@ -155,7 +155,7 @@ def tetrahedralize(mesh_info, options):
locale.setlocale(locale.LC_NUMERIC, "C")

try:
internals.tetrahedralize(options, mesh_info, mesh)
internals.tetrahedralize(options, mesh_info, mesh, insert_points)
finally:
# restore previous locale if we've changed it
if have_locale:
Expand All @@ -171,6 +171,8 @@ def build(mesh_info, options=Options("pq"), verbose=False,
options.quiet = 1

if insert_points is not None:
if not isinstance(insert_points, MeshInfo):
raise ValueError('insert_points should be an instance of MeshInfo')
options.insertaddpoints = 1

if attributes:
Expand All @@ -183,4 +185,4 @@ def build(mesh_info, options=Options("pq"), verbose=False,
if diagnose:
options.diagnose = 1

return tetrahedralize(mesh_info, options)
return tetrahedralize(mesh_info, options, insert_points)