Skip to main content

Repairs triangular meshes

Project description

Python/Cython wrapper of Marco Attene’s wonderful, award-winning MeshFix software. This module brings the speed of C++ with the portability and ease of installation of Python.

This software takes as input a polygon mesh and produces a copy of the input where all the occurrences of a specific set of “defects” are corrected. MeshFix has been designed to correct typical flaws present in raw digitized mesh models, thus it might fail or produce coarse results if run on other sorts of input meshes (e.g. tessellated CAD models).

The input is assumed to represent a single closed solid object, thus the output will be a single watertight triangle mesh bounding a polyhedron. All the singularities, self-intersections and degenerate elements are removed from the input, while regions of the surface without defects are left unmodified.

Installation

From PyPI

pip install pymeshfix

From source at GitHub

git clone https://github.com/akaszynski/pymeshfix
cd pymeshfix
pip install .

Dependencies

Required: numpy, cython, vtkInterface

Minimum requirements are numpy and cython. To enable plotting and several other features, vtkInterface can be installed using:

pip install vtk

Tests

Test installation with the following from Python:

from pymeshfix import tests

# Test of pymeshfix without VTK module
tests.Native()

# Performs same mesh repair while leveraging VTK's plotting/mesh loading
tests.WithVTK()

Easy Example

This example uses the cython wrapper directly. No bells or whistles here:

from pymeshfix import _meshfix

# Read mesh from infile and output cleaned mesh to outfile
_meshfix.CleanFromFile(infile, outfile)

This example assumes the user has vertex and faces arrays in Python. Again, no bells or whistles.

from pymeshfix import _meshfix

# Generate vertex and face arrays of cleaned mesh
vclean, fclean = CleanFromVF(v, f) # v and f are numpy arrays or python lists

Fuller Examples with and without VTK

One of the main reasons to bring MeshFix to Python is to allow the library to communicate to other python programs without having to use the hard drive. Therefore, this example assumes that you have a mesh within memory and wish to repair it using MeshFix.

#Load module
from pymeshfix import meshfix

# Create object from arrays or lists
meshfix = meshfix.MeshFixClass(v, f)
meshfix.LoadVF(v, f) #

# Plot input (if vtk is available)
meshfix.Plot()

# Repair input mesh
meshfix.Repair()

# Access the repaired mesh with vtk
meshfix.mesh

# Or, access the resulting arrays directly from the object
meshfix.v # numpy np.float array
meshfix.f # numpy np.int32 array

# View the repaired mesh (requires vtkInterface)
meshfix.Plot()

# Save the mesh
meshfix.Write('out.ply')

Alternatively, the user could use the cython wrapper of MeshFix directly if vtk is unavailable or they wish to have more control over the cleaning algorthim.

from pymeshfix import _meshfix

# Create TMesh object
tin = _meshfix.PyTMesh()

tin.LoadFile(infile)
# tin.LoadArray(v, f) # or read arrays from memory

# Attempt to join nearby components
# tin.JoinClosestComponents()

# Fill holes
tin.FillSmallBoundaries()
print('There are {:d} boundaries'.format(tin.Boundaries())

# Clean (removes self intersections)
tin.MeshClean(max_iters=10, inner_loops=3)

# Check mesh for holes again
print('There are {:d} boundaries'.format(tin.Boundaries())

# Clean again if necessary...

# Output mesh
tin.SaveFile(outfile)
# vclean, fclean = tin.ReturnArrays() # or return numpy arrays

Algorithim and Citation Policy

To better understand how the algorithm works, please refer to the following paper:

  1. Attene. A lightweight approach to repairing digitized polygon meshes. The Visual Computer, 2010. (c) Springer. DOI: 10.1007/s00371-010-0416-3

This software is based on ideas published therein. If you use MeshFix for research purposes you should cite the above paper in your published results. MeshFix cannot be used for commercial purposes without a proper licensing contract.

Project details


Release history Release notifications

This version
History Node

0.11.0

History Node

0.10.1

History Node

0.10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pymeshfix-0.11.0.tar.gz (251.0 kB) Copy SHA256 hash SHA256 Source None May 14, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page