question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inaccuracies in mesh.contains

See original GitHub issue

I experiencing some issues with the Trimesh.contains method. I tried to debug it myself, but I couldn’t find any issues in the trimesh code. I suspect it’s inaccuracies in PyEmbree.

Here is the code I’m using:

import numpy as np
import trimesh
from plyfile import PlyElement, PlyData

# Only for exporting the pointcloud
def export_pointcloud(vertices, out_file):
    assert(vertices.shape[1] == 3)
    vertices = vertices.astype(np.float32)
    vector_dtype = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
    vertices = vertices.view(dtype=vector_dtype).flatten()
    plyel = PlyElement.describe(vertices, 'vertex')
    plydata = PlyData([plyel], text=True)
    plydata.write(out_file)

# Load mesh
mesh = trimesh.load('voxels.off', process=False)
points = np.random.rand(100000, 3) - 0.5
contained = mesh.contains(points)
export_pointcloud(points[contained], 'points.ply')

The result looks like this: screenshot

Here are the input / output files: https://www.dropbox.com/sh/6i0222tk9lpkqgr/AAC1mZ0yblFsXxg9cEMmfneJa?dl=0

Of course, in this special case I can do it in a simpler way by exploiting the regular grid structure of the mesh. However, I also experience similar problems for other meshes (usually at higher resolutions).

I would appreciate any help!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:30 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
LMeschedercommented, Nov 3, 2018

I ended up writing my own cython code for checking if a point is inside a mesh, based on this answer stack overflow: https://stackoverflow.com/a/6576840 As far as I can tell, it works quite robustly and yields more accurate results. I would be happy to contribute the code (not now, but in a few weeks) if you are interested.

2reactions
benoitrosacommented, Sep 10, 2019

Hi,

For easy testing, you can download the inside_mesh.py and triangle_hash.pyx files from the Occupancy Network repo, and compile the pyx file. You just have to create a setup.py file with the following lines :

`from distutils.core import setup from Cython.Build import cythonize

setup( ext_modules = cythonize(“triangle_hash.pyx”) )`

And then compile it using cython : python setup.py build_ext --inplace

The compilation creates a triangle_hash.so which can be imported in python normally, making the inside_mesh module work as a standalone one for using the check_mesh_contains method

Read more comments on GitHub >

github_iconTop Results From Across the Web

#AskZBrush: “How can I fix the error: Mesh contains # edges ...
AskZBrush: “How can I fix the error : Mesh contains edges that are shared by more then two polys?”Ask your questions through Twitter...
Read more >
Identifying and Repairing Common Mesh Errors - Simplify3D
This tutorial will teach you about common mesh errors that you may ... But first, let's talk about the signs that you might...
Read more >
HEC-RAS 2D Mesh Generation Errors and Corrections
When working with complex meshes, there may be times when the mesh generation fails to run. When this situation occurs, the user will...
Read more >
Resolving Mesh Failures and Errors in SOLIDWORKS ...
In another example, if we take an up-close look at the logo, we can see that we have Cut-Extrude features that are very...
Read more >
4 Mesh issues causing poor CFD simulation accuracy
25 Jan 4 Mesh issues causing poor CFD simulation accuracy · 1. UNIFORMITY. DEFINITION: The “speed” the cells volume changes from the maximum...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found