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.3.tar.gz (329.4 kB view details)

Uploaded Source

Built Distributions

pydelatin-0.2.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3-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.3.tar.gz.

File metadata

  • Download URL: pydelatin-0.2.3.tar.gz
  • Upload date:
  • Size: 329.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pydelatin-0.2.3.tar.gz
Algorithm Hash digest
SHA256 6a1303a2110b66462a092a5969bafac02c83b10203e4402307c038720e888737
MD5 f796eaada2c6c67b3b1a14548d374cbd
BLAKE2b-256 c3c5758f4b2a810f6d3d8dca131831f5905faacfe02c78e2a59976059d148c39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydelatin-0.2.3-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 b152833d1fd7e4a860ea586ee1929d726b1db96353b1194a639e843d253e8586
MD5 ce6f9175c9fa93bd40b52983a0e5e4fa
BLAKE2b-256 bd5407a9788021b882dc62fdfaae3051f3f5767bf8a047d1060137a267f2bc13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydelatin-0.2.3-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.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2492d88e5053c1f6e11864466b09373325df9dd81658721270a3cf58ef5f32d2
MD5 0f7666372eb3c55a3e58d79b8547c8c4
BLAKE2b-256 29394089ba6ae1d1cd424ba77b1c6ddc54865dd98961e7116a64569fab215f2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydelatin-0.2.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 bddb65ccf9bdcd2ffecbc54e4ff60071858a8ce9615ab6486e76d62ba4050170
MD5 b0833bd3979d4487bc6f95dd4b1faa35
BLAKE2b-256 57e86c2e1dc1b55c00180135d197f6c7e8dc9df1c0e57e00c524806e498d577a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydelatin-0.2.3-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.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pydelatin-0.2.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f7f10e4b78d4946d4d4deb58d78fb8f6678e57897d47861d8b000ebb5f328dea
MD5 830707a3503d5bef419106f9a712c446
BLAKE2b-256 047e5115122706441d72afb877b34ffb5d444847dfee171885fa6b73864705b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydelatin-0.2.3-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 4a42533ffbbe228bb38daeb91c08a7b6d8d07cfc2609dbd52ab9ac180027f89c
MD5 247b551c73393786a4ea0906e72a2d30
BLAKE2b-256 60331ccaf6772a227d0dbc85db81057a6442ba830d2ba7dd67576672e15feb92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydelatin-0.2.3-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.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pydelatin-0.2.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bdce327123e181f05a34eea71339903f192003e82a6c5de5eee774e51c27899c
MD5 065539dafbeb1051776da62afbd7c391
BLAKE2b-256 ad7fdd644f4045aedce8cba4be921786679143c5415d839c17509d1fc6d8d0f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydelatin-0.2.3-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 33502941ffd91edc1d9b44df9987447979f3c2010490566555149815da6a4043
MD5 086d9d9b820a6751f05958d7df92984b
BLAKE2b-256 f3dc58a1a97331105a6103111ce0bb1ecce2bea1dddc2d0406781a2ea50f7941

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydelatin-0.2.3-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.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pydelatin-0.2.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5cff65cf58271e7aea4408b8b6ad89561574dbae298f98591df25a44c6c5ef8f
MD5 47cd10fafd9070915d51731b0e19cb95
BLAKE2b-256 2b84c8ff7c1455ab55345c9d6f1e1555a3b3327a9a6fc9a1614b711b8c994847

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydelatin-0.2.3-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 ba42e8be4a2fcde512576cf181a394bab0a4604b6e2f007c67c537eda6978056
MD5 75fc51cf1d3977f99dda02309a3bba42
BLAKE2b-256 6d7753e597edffb11359b3fd762addbb925579212c2d7d7c1d91303f1f8b72c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydelatin-0.2.3-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.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for pydelatin-0.2.3-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8270198e222844dcefc63dd4cd638d1533fceaf74e79e2a421e84384e9fafd3a
MD5 add7e2d1ceb326a77021db90db0b03e0
BLAKE2b-256 9620a942f60d104f42d0fdc4d61e8bc876b97cf6eec5da5e52def66ba605e35e

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