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

Add Meshes to the pdal API #82

Merged
merged 28 commits into from
Apr 29, 2021
Merged

Add Meshes to the pdal API #82

merged 28 commits into from
Apr 29, 2021

Conversation

runette
Copy link
Contributor

@runette runette commented Apr 28, 2021

Adds the ability to access face data from meshes in the Python API:

  • Adds a new method to pdal.Pipeline called get_meshes() that returns a list of ndarray in the same format as get_arrays(). Each ndarray represents one mesh from one PointViewand is a (1, size) array of Triangles, where each triangle is a tuple of (index of corner a, index of corner b, index of corner c) - the index referring to the points in PointView array.
  • Adds the details to libpdalpython.pyx to implement the above,
  • Adds a method to the cpp Pipeline class called getMeshes() that returns a vector of PyMesh
  • Adds a cpp class PyMesh very much a copy of the PyArray class, in this case the class creates the mesh ndarray.

All of the above is working on my builds and does not introduce any new dependencies. However, I also wanted to create a way to create a useable Mesh object easily.

NOTE - where there is no mesh attached to the PointView, get_meshes will return an ndarray with 0 rows to ensure that the correspondence between arrays and meshes can be retained.

One good way of doing that is using the Meshio package - which allows the Mesh object to be re-used by a wide range of other packages.

So - I have also included the following :

  • Adds a new method to pdal.Pipeline called get_meshio(idx: int) -> meshio.Mesh. This uses get_meshes and get_arrays to create a Mesh object. At the moment it does not add the non-geometry data (a ToDo). This method requires meshio and if it is not installed will fail with an informative error.

Usage

The simplest usage would look like the following:

import pdal

...
pl = pdal.Pipeline(pipeline)
pl.execute()

mesh = pl.get_meshio(0)
mesh.write('test.obj')

NOTE that since we are getting back a vector of meshes - we can do something that was not easy before and was actually the use-case that I needed to solve:

USE-CASE : Take a LiDAR map, create a mesh from the ground points, split into tiles and store the tiles in PostGIS.

(example using 1.2-with-color.las and not doing the ground classification for clarity)

import pdal
import json
import psycopg2
import io

pipe = [
     '.../python/test/data/1.2-with-color.las',
     {"type":  "filters.splitter", "length": 1000}, 
     {"type":  "filters.delaunay"}
]

pl = pdal.Pipeline(json.dumps(pipe))
pl.execute()

conn = psycopg(%CONNNECTION_STRING%)
buffer = io.StringIO

for idx in range(len(pl.meshes)):
    m =  pl.get_meshio(idx)
    if m:
        m.write(buffer,  file_format = "wkt")
        with conn.cursor() as curr:
           curr.execute(
               "INSERT INTO %table-name% (mesh) VALUES (ST_GeomFromEWKT(%(ewkt)s)", 
               { "ewkt": buffer.getvalue()}
           )

conn.commit()
conn.close()
buffer.close()

This creates 24 tile meshes in PostGIS

@runette runette changed the title WPI: Add Meshes to the pdal API WIP: Add Meshes to the pdal API Apr 28, 2021
Copy link
Member

@hobu hobu left a comment

Choose a reason for hiding this comment

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

This looks great!

Can you verify that it doesn't leak PDAL objects?

Once this is merged, I'll make a new PDAL-python release.

pdal/PyMesh.cpp Show resolved Hide resolved
@runette
Copy link
Contributor Author

runette commented Apr 28, 2021

Can you verify that it doesn't leak PDAL objects?

I am not aware of any leaks and tried to follow the existing Array structure as closely as possible - but as usual I don't know what I don't know!

@hobu
Copy link
Member

hobu commented Apr 28, 2021

Please add a test that does filters.delaunay on https://github.com/PDAL/python/blob/master/test/data/1.2-with-color.las and tests that you get what you expect. With that, I think we're good to merge.

@runette
Copy link
Contributor Author

runette commented Apr 28, 2021

Please add a test that does filters.delaunay on https://github.com/PDAL/python/blob/master/test/data/1.2-with-color.las and tests that you get what you expect. With that, I think we're good to merge.

Will do - probably tomorrow

@runette runette changed the title WIP: Add Meshes to the pdal API Add Meshes to the pdal API Apr 28, 2021
@runette
Copy link
Contributor Author

runette commented Apr 28, 2021

@hobu Added tests and documentation

@hobu hobu merged commit fea94f6 into PDAL:master Apr 29, 2021
@hobu
Copy link
Member

hobu commented Apr 29, 2021

2.4.0 pushed https://pypi.org/project/PDAL/

@runette runette deleted the mesh-changes branch April 29, 2021 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants