Skip to main content

A wrapper for hmm

Project description

pydelatin

A Python wrapper of hmm (of which Delatin is a port) for fast terrain mesh generation.

A screenshot of Glacier National Park taken from the demo. The mesh is created using pydelatin, encoded using quantized-mesh-encoder, served on-demand using dem-tiler, and rendered with deck.gl.

Install

With pip:

pip install pydelatin

or with Conda:

conda install -c conda-forge pydelatin

On Windows, installing via Conda is strongly recommended.

If installing with pip on Windows, glm is a prerequisite for building from source. Open an issue if you'd like to help package binary wheels for Windows.

Using

Example

from pydelatin import Delatin

tin = Delatin(terrain, width, height)
# Mesh vertices
tin.vertices
# Mesh triangles
tin.triangles

API

The API is similar to that of hmm.

Additionally I include a helper function: decode_ele, to decode a Mapbox Terrain RGB or Terrarium PNG array to elevations.

Delatin

Arguments
  • arr (numpy ndarray): data array. If a 2D array, dimensions are expected to be (height, width). If a 1D array, height and width parameters must be passed, and the array is assumed to be in C order.
  • height (int, default: None): height of array; required when arr is not 2D
  • width (int, default: None): width of array; required when arr is not 2D
  • z_scale (float, default: 1): z scale relative to x & y
  • z_exag (float, default: 1): z exaggeration
  • max_error (float, default: 0.001): maximum triangulation error
  • max_triangles (int, default: None): maximum number of triangles
  • max_points (int, default: None): maximum number of vertices
  • base_height (float, default: 0): solid base height
  • level (bool, default: False): auto level input to full grayscale range
  • invert (bool, default: False): invert heightmap
  • blur (int, default: 0): gaussian blur sigma
  • gamma (float, default: 0): gamma curve exponent
  • border_size (int, default: 0): border size in pixels
  • border_height (float, default: 1): border z height
Attributes
  • vertices (ndarray of shape (-1, 3)): the interleaved 3D coordinates of each vertex, e.g. [[x0, y0, z0], [x1, y1, z1], ...].
  • triangles (ndarray of shape (-1, 3)): represents indices within the vertices array. So [0, 1, 3, ...] would use the first, second, and fourth vertices within the vertices array as a single triangle.
  • error (float): the maximum error of the mesh.

util.rescale_positions

A helper function to rescale the vertices output to a new bounding box. Returns an ndarray of shape (-1, 3) with positions rescaled. Each row represents a single 3D point.

Arguments
  • vertices: (np.ndarray) vertices output from Delatin
  • bounds: (Tuple[float]) linearly rescale position values to this extent. Expected to be [minx, miny, maxx, maxy].
  • flip_y: (bool, default False) Flip y coordinates. Can be useful since images' coordinate origin is in the top left.

Saving to mesh formats

Quantized Mesh

A common mesh format for the web is the Quantized Mesh format, which is supported in Cesium and deck.gl (via loaders.gl). You can use quantized-mesh-encoder to save in this format:

import quantized_mesh_encoder
from pydelatin import Delatin
from pydelatin.util import rescale_positions

tin = Delatin(terrain, max_error=30)
vertices, triangles = tin.vertices, tin.triangles

# Rescale vertices linearly from pixel units to world coordinates
rescaled_vertices = rescale_positions(vertices, bounds)

with open('output.terrain', 'wb') as f:
    quantized_mesh_encoder.encode(f, rescaled_vertices, triangles)

Meshio

Alternatively, you can save to a variety of mesh formats using meshio:

from pydelatin import Delatin
import meshio

tin = Delatin(terrain, max_error=30)
vertices, triangles = tin.vertices, tin.triangles

cells = [("triangle", triangles)]
mesh = meshio.Mesh(vertices, cells)
# Example output format
# Refer to meshio documentation
mesh.write('foo.vtk')

Martini or Delatin?

Two popular algorithms for terrain mesh generation are the "Martini" algorithm, found in the JavaScript martini library and the Python pymartini library, and the "Delatin" algorithm, found in the C++ hmm library, this Python pydelatin library, and the JavaScript delatin library.

Which to use?

For most purposes, use pydelatin over pymartini. A good breakdown from a Martini issue:

Martini:

  • Only works on square 2^n+1 x 2^n+1 grids.
  • Generates a hierarchy of meshes (pick arbitrary detail after a single run)
  • Optimized for meshing speed rather than quality.

Delatin:

  • Works on arbitrary raster grids.
  • Generates a single mesh for a particular detail.
  • Optimized for quality (as few triangles as possible for a given error).

Benchmark

The following uses the same dataset as the pymartini benchmarks, a 512x512 pixel heightmap of Mt. Fuji.

For the 30-meter mesh, pydelatin is 25% slower than pymartini, but the mesh is much more efficient: it has 40% fewer vertices and triangles.

pydelatin is 4-5x faster than the JavaScript delatin package.

Python

git clone https://github.com/kylebarron/pydelatin
cd pydelatin
pip install '.[test]'
python bench.py
mesh (max_error=30m): 27.322ms
vertices: 5668, triangles: 11140

mesh (max_error=1m): 282.946ms
mesh (max_error=2m): 215.839ms
mesh (max_error=3m): 163.424ms
mesh (max_error=4m): 127.203ms
mesh (max_error=5m): 106.596ms
mesh (max_error=6m): 91.868ms
mesh (max_error=7m): 82.572ms
mesh (max_error=8m): 74.335ms
mesh (max_error=9m): 65.893ms
mesh (max_error=10m): 60.999ms
mesh (max_error=11m): 55.213ms
mesh (max_error=12m): 54.475ms
mesh (max_error=13m): 48.662ms
mesh (max_error=14m): 47.029ms
mesh (max_error=15m): 44.517ms
mesh (max_error=16m): 42.059ms
mesh (max_error=17m): 39.699ms
mesh (max_error=18m): 37.657ms
mesh (max_error=19m): 36.333ms
mesh (max_error=20m): 34.131ms

JS (Node)

This benchmarks against the delatin JavaScript module.

git clone https://github.com/kylebarron/pydelatin
cd test/bench_js/
yarn
wget https://raw.githubusercontent.com/mapbox/delatin/master/index.js
node -r esm bench.js
mesh (max_error=30m): 143.038ms
vertices: 5668
triangles: 11140

mesh (max_error=0m): 1169.226ms
mesh (max_error=1m): 917.290ms
mesh (max_error=2m): 629.776ms
mesh (max_error=3m): 476.958ms
mesh (max_error=4m): 352.907ms
mesh (max_error=5m): 290.946ms
mesh (max_error=6m): 240.556ms
mesh (max_error=7m): 234.181ms
mesh (max_error=8m): 188.273ms
mesh (max_error=9m): 162.743ms
mesh (max_error=10m): 145.734ms
mesh (max_error=11m): 130.119ms
mesh (max_error=12m): 119.865ms
mesh (max_error=13m): 114.645ms
mesh (max_error=14m): 101.390ms
mesh (max_error=15m): 100.065ms
mesh (max_error=16m): 96.247ms
mesh (max_error=17m): 89.508ms
mesh (max_error=18m): 85.754ms
mesh (max_error=19m): 79.838ms
mesh (max_error=20m): 75.607ms

License

This package wraps @fogleman's hmm, a C++ library that is also MIT-licensed.

Project details


Download files

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

Source Distribution

pydelatin-0.2.4.tar.gz (329.5 kB view details)

Uploaded Source

Built Distributions

pydelatin-0.2.4-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

pydelatin-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl (199.5 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

pydelatin-0.2.4-cp310-cp310-macosx_10_9_universal2.whl (378.2 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

pydelatin-0.2.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pydelatin-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl (199.6 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

pydelatin-0.2.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pydelatin-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl (199.4 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

pydelatin-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pydelatin-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl (198.7 kB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

pydelatin-0.2.4-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

pydelatin-0.2.4-cp36-cp36m-macosx_10_9_x86_64.whl (198.7 kB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

File details

Details for the file pydelatin-0.2.4.tar.gz.

File metadata

  • Download URL: pydelatin-0.2.4.tar.gz
  • Upload date:
  • Size: 329.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4.tar.gz
Algorithm Hash digest
SHA256 d6b192ed30117018586dbe5c1cc1c3bb8ac96551493317829a6c3a29a6b77274
MD5 c063d78b6d88161d17effe43d23a9677
BLAKE2b-256 91b6c35e64611220a7e866a602f23ea1fc5bfe1e60b26791758621414ad7cf62

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pydelatin-0.2.4-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 aacdd9c085d265b7f1025e36a6f7f8067f8c95ea3a88f17de121acf5e415982c
MD5 536a0086edc2cf8c49894a3ef79f30bc
BLAKE2b-256 0683f7478f917460689b74e7c9b759ee424239bee81bfe0e6811fe2903634be4

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 199.5 kB
  • Tags: CPython 3.10, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 13ab0695e129259b48bdda675ab1adcd2aee6feb33e274563e2d4032d6c4194d
MD5 5a9f0c0f7f74bf99bf32c6230cb4eb87
BLAKE2b-256 58cee876488a713d5ddf0f6f6ca8aa191deade7e317cc14cf3488a9b3462ccea

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp310-cp310-macosx_10_9_universal2.whl
  • Upload date:
  • Size: 378.2 kB
  • Tags: CPython 3.10, macOS 10.9+ universal2 (ARM64, x86-64)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 3056e574e5f82ab6c524a21b96472c0bbf1718b06ae3f1a712a255a0a38160ff
MD5 630396f212e0717e64c170c53b597bc3
BLAKE2b-256 43aac6a1da05c1371966fd7fc08a7f532f7177d01e17fcf15d100f459405166c

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pydelatin-0.2.4-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 fce442ea2874c94812021313acd513f6d5a4d1c2d8109b1b945ce9f428f710ef
MD5 39199bcac285695bc53c4b665a00ab91
BLAKE2b-256 031e876a3704be9fd2a142ee79aae407a0a43909a23e50e309a34620dac774b8

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 199.6 kB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7d6f2a9f500ec3e6ba19393bd37b9b0a596721491c6f48341b8e5daed2139e9b
MD5 252a745504e20116a547c062d68588bf
BLAKE2b-256 846a1bd38cc412f9ef85ac87c04e0543cc2c97d6ee8c48260b4c112c4a15ea85

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pydelatin-0.2.4-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 d88798bcc65989ec9b00d53f205614ef8ee93baee07fd22ef2170a76c5f5706d
MD5 f5085b351e132abca5e3107a0451886f
BLAKE2b-256 def31874ea42dde9dbb3706bf7d72b58d201e402f460644d7b2101f1b474e8c4

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 199.4 kB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d6673da3b7ab447bbb86550747e7996bcfe442a27a09d3e0ef583dfe6a1cc588
MD5 77da8fd28d8370a5506e0ec4109c7643
BLAKE2b-256 9570a651f01880f0cf046b3b107c6fa95b4183506f2a572bd3233b72718b63db

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pydelatin-0.2.4-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 bb7fdd6d2ef0957d0b2409178a246db2a5c250385aab2fa39f9bb8ab8a12d3b0
MD5 d74b4f81920c57b6020729732be52a25
BLAKE2b-256 5ccf327fa371953561d205985a8d30a16e2168dd58c00859c281914d8d5f1bd7

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 198.7 kB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e3f205a404343c87e5e89bae7465e0a89ded62dcc3e5ec8aadede3bf27a266c7
MD5 290f65d4781ace820909bec8d9fb7739
BLAKE2b-256 9e948139bd8565f423f7fbb1bcdccd27734582915930d1bde0d52ede74c3832a

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for pydelatin-0.2.4-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 114056f9ca4ba1aac8d462988f1b8eafe5fe1b69ed25310062bcf08f9c36ce1d
MD5 4526a60ab04a1fc0eaf1949de5cd220d
BLAKE2b-256 341f5af7defd18b93c7614086c1bca573c2d0e7556bb456a71063f9106332f13

See more details on using hashes here.

File details

Details for the file pydelatin-0.2.4-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: pydelatin-0.2.4-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 198.7 kB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pydelatin-0.2.4-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7c0523c3fd1c50b182419ee0332c4b361fbb8e9e46bf9cf1144446426d38ca01
MD5 ee457cbac07f145d6975bce954cedb63
BLAKE2b-256 a94c5abee1cbced7c71d6a1ec48282be7fb00bc92868bc02bea04e482b7ae1bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page