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
(numpyndarray
): 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 2Dwidth
(int
, default:None
): width of array; required when arr is not 2Dz_scale
(float
, default:1
): z scale relative to x & yz_exag
(float
, default:1
): z exaggerationmax_error
(float
, default:0.001
): maximum triangulation errormax_triangles
(int
, default:None
): maximum number of trianglesmax_points
(int
, default:None
): maximum number of verticesbase_height
(float
, default:0
): solid base heightlevel
(bool
, default:False
): auto level input to full grayscale rangeinvert
(bool
, default:False
): invert heightmapblur
(int
, default:0
): gaussian blur sigmagamma
(float
, default:0
): gamma curve exponentborder_size
(int
, default:0
): border size in pixelsborder_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 thevertices
array. So[0, 1, 3, ...]
would use the first, second, and fourth vertices within thevertices
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 Delatinbounds
: (Tuple[float]
) linearly rescale position values to this extent. Expected to be[minx, miny, maxx, maxy]
.flip_y
: (bool
, defaultFalse
) 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
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 |
Hashes for pydelatin-0.2.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13ab0695e129259b48bdda675ab1adcd2aee6feb33e274563e2d4032d6c4194d |
|
MD5 | 5a9f0c0f7f74bf99bf32c6230cb4eb87 |
|
BLAKE2b-256 | 58cee876488a713d5ddf0f6f6ca8aa191deade7e317cc14cf3488a9b3462ccea |
Hashes for pydelatin-0.2.4-cp310-cp310-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3056e574e5f82ab6c524a21b96472c0bbf1718b06ae3f1a712a255a0a38160ff |
|
MD5 | 630396f212e0717e64c170c53b597bc3 |
|
BLAKE2b-256 | 43aac6a1da05c1371966fd7fc08a7f532f7177d01e17fcf15d100f459405166c |
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 |
Hashes for pydelatin-0.2.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d6f2a9f500ec3e6ba19393bd37b9b0a596721491c6f48341b8e5daed2139e9b |
|
MD5 | 252a745504e20116a547c062d68588bf |
|
BLAKE2b-256 | 846a1bd38cc412f9ef85ac87c04e0543cc2c97d6ee8c48260b4c112c4a15ea85 |
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 |
Hashes for pydelatin-0.2.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6673da3b7ab447bbb86550747e7996bcfe442a27a09d3e0ef583dfe6a1cc588 |
|
MD5 | 77da8fd28d8370a5506e0ec4109c7643 |
|
BLAKE2b-256 | 9570a651f01880f0cf046b3b107c6fa95b4183506f2a572bd3233b72718b63db |
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 |
Hashes for pydelatin-0.2.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3f205a404343c87e5e89bae7465e0a89ded62dcc3e5ec8aadede3bf27a266c7 |
|
MD5 | 290f65d4781ace820909bec8d9fb7739 |
|
BLAKE2b-256 | 9e948139bd8565f423f7fbb1bcdccd27734582915930d1bde0d52ede74c3832a |
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 |
Hashes for pydelatin-0.2.4-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c0523c3fd1c50b182419ee0332c4b361fbb8e9e46bf9cf1144446426d38ca01 |
|
MD5 | ee457cbac07f145d6975bce954cedb63 |
|
BLAKE2b-256 | a94c5abee1cbced7c71d6a1ec48282be7fb00bc92868bc02bea04e482b7ae1bb |