A fast Python Quantized Mesh encoder
Project description
quantized-mesh-encoder
A fast Python Quantized Mesh encoder. Encodes a mesh with 100k coordinates and 180k triangles in 20ms. Example viewer.
The Grand Canyon and Walhalla Plateau. The mesh is created using
pymartini
, encoded using quantized-mesh-encoder
, served
on-demand using dem-tiler
, and rendered with
deck.gl.
Overview
Quantized Mesh is a format to encode terrain meshes for efficient client-side terrain rendering. Such files are supported in Cesium and deck.gl.
This library is designed to support performant server-side on-demand terrain mesh generation.
Install
pip install quantized-mesh-encoder
Using
API
encode
Parameters:
f
: a writable file-like object in which to write encoded bytespositions
: (array[float]
): a flat Numpy array of 3D positions.indices
(array[int]
): a flat Numpy array indicating triples of coordinates frompositions
to make triangles. For example, if the first three values ofindices
are0
,1
,2
, then that defines a triangle formed by the first 9 values inpositions
, three for the first vertex (index0
), three for the second vertex, and three for the third vertex.bounds
(List[float]
, optional): a list of bounds,[minx, miny, maxx, maxy]
. By default, inferred as the minimum and maximum values ofpositions
.sphere_method
(str
, optional): As part of the header information when encoding Quantized Mesh, it's necessary to compute a bounding sphere, which contains all positions of the mesh.sphere_method
designates the algorithm to use for creating the bounding sphere. Must be one of'bounding_box'
,'naive'
,'ritter'
orNone
. Default isNone
.'bounding_box'
: Finds the bounding box of all positions, then defines the center of the sphere as the center of the bounding box, and defines the radius as the distance back to the corner. This method produces the largest bounding sphere, but is the fastest: roughly 70 µs on my computer.'naive'
: Finds the bounding box of all positions, then defines the center of the sphere as the center of the bounding box. It then checks the distance to every other point and defines the radius as the maximum of these distances. This method will produce a slightly smaller bounding sphere than thebounding_box
method when points are not in the 3D corners. This is the next fastest at roughly 160 µs on my computer.'ritter'
: Implements the Ritter Method for bounding spheres. It first finds the center of the longest span, then checks every point for containment, enlarging the sphere if necessary. This can produce smaller bounding spheres than the naive method, but it does not always, so often both are run, see next option. This is the slowest method, at roughly 300 µs on my computer.None
: Runs both the naive and the ritter methods, then returns the smaller of the two. Since this runs both algorithms, it takes around 500 µs on my computer
Examples
Write to file
from quantized_mesh_encoder import encode
with open('output.terrain', 'wb') as f:
encode(f, positions, indices)
Quantized mesh files are usually saved gzipped. An easy way to create a gzipped
file is to use gzip.open
:
import gzip
from quantized_mesh_encoder import encode
with gzip.open('output.terrain', 'wb') as f:
encode(f, positions, indices)
Write to buffer
It's also pretty simple to write to a buffer instead of a file
from io import BytesIO
from quantized_mesh_encoder import encode
buf = BytesIO()
encode(buf, positions, indices)
To read the bytes out of the buffer, e.g. to gzip the buffer
import zlib
buf.seek(0)
out_bytes = zlib.compress(buf.read())
Generating the mesh
To encode a mesh into a quantized mesh file, you first need a mesh! This project
was designed to be used with pymartini
, a fast elevation
heightmap to terrain mesh generator.
import quantized_mesh_encoder
from imageio import imread
from pymartini import decode_ele, Martini, rescale_positions
import mercantile
png = imread(png_path)
terrain = decode_ele(png, 'terrarium')
terrain = terrain.T
martini = Martini(png.shape[0] + 1)
tile = martini.create_tile(terrain)
vertices, triangles = tile.get_mesh(10)
# Use mercantile to find the bounds in WGS84 of this tile
bounds = mercantile.bounds(mercantile.Tile(x, y, z))
# Rescale positions to WGS84
rescaled = rescale_positions(
vertices,
terrain,
bounds=bounds,
flip_y=True
)
with BytesIO() as f:
quantized_mesh_encoder.encode(f, rescaled, triangles)
f.seek(0)
return ("OK", "application/vnd.quantized-mesh", f.read())
You can also look at the source of
_mesh()
in dem-tiler
for a working reference.
License
Much of this code is ported or derived from
quantized-mesh-tile
in some way. quantized-mesh-tile
is also released under the MIT license.
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 quantized-mesh-encoder-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8d9366fa9616c42b83146a29466d0428605bf70c5ca337e8afe01821a8e5be8 |
|
MD5 | 19871b3a8bb7dbfebe71cb7aae58aff0 |
|
BLAKE2b-256 | 549f5caf66a87605adeb64fe3b1c7268c27dbe429c2890de943d394828747b71 |
Hashes for quantized_mesh_encoder-0.2.1-pp36-pypy36_pp73-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b36fbbbd5462924cb6655d618045282b5cc42d8a61b5cfe328b6651be1da8761 |
|
MD5 | e66efe7ead8f44de306de967357735d8 |
|
BLAKE2b-256 | bb4e77799b237307be4d9bc5a735f9343b0b728aab784da5ab245ac3406cbd36 |
Hashes for quantized_mesh_encoder-0.2.1-pp36-pypy36_pp73-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed354b3285ef36f3f87fb10b27fcdfd9d5893607822f47c86ded3b410e7e164a |
|
MD5 | e2c7bd1dcb70980acf66e9129afa825e |
|
BLAKE2b-256 | 5a40e710d1ccd5d41cd68b4cb71a940e8155bea17f814b84db9a95f44966d143 |
Hashes for quantized_mesh_encoder-0.2.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 604a47216dc299d077f7f7896a3620c5a0c2d03a7d4bf4b7698d11a6c8a8c2cb |
|
MD5 | 49b8599965f40c783e38db252588e76f |
|
BLAKE2b-256 | 4a410f68b70589808e60e9c35367ce700281199f1189c789305f8f6c3b787c8c |
Hashes for quantized_mesh_encoder-0.2.1-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 650fef0baa6200bcc22f1764d61bd70f5a6c2b0d65e95fee9b7c1b6c8e756743 |
|
MD5 | b57c122d2565dbaba34157114cad1a5b |
|
BLAKE2b-256 | bf913c0acfc20fd291906f442054f2e81299edeec22d47c81c6eecc39dbfb66c |
Hashes for quantized_mesh_encoder-0.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 818f73acf93657fcaf603bb55af535b9160a937f4d4ae9fbb53ebe434058483b |
|
MD5 | 84cd9d67b15b62ad59065087cc6c299c |
|
BLAKE2b-256 | 3ddf32f41ef1fc36164f075142005a9a56b261723deb057b9b9875bf84b3eeaf |
Hashes for quantized_mesh_encoder-0.2.1-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 127a8fafcfdbcd9955dd92ddb54df788f7a4a4141523261542c8db325496df8d |
|
MD5 | 18579f6dedbefec979b61aa38193cc43 |
|
BLAKE2b-256 | f55ffabaf797ad6d6b64b8fcf226bfcb267ed1571d676a5075825ae928be8d52 |
Hashes for quantized_mesh_encoder-0.2.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1259ef20c972097dbb94195ffa598cf08706bdf636fa31b6279f68bcf08a4fac |
|
MD5 | ede0b1d2e5697bb20f1a050e32df0fbb |
|
BLAKE2b-256 | 08ece0c93c80397bf179154ca5671cf7f8d1d267b1c401b906c55634f71930b6 |
Hashes for quantized_mesh_encoder-0.2.1-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fad9f37e4b7d76f41c0bda9769a6fee27c6c12bbc6631067b2b9ed574181091e |
|
MD5 | 4cf000bd438c8f82bec64c3c658aefe8 |
|
BLAKE2b-256 | df6224d2644b8a619e11d190f20fce07ad6976e69a2ee8a0d250361384e147fc |
Hashes for quantized_mesh_encoder-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 229649b93b2d939ea9d9bf207376da86368de7bcfbf7b9e2d131d00a115fd839 |
|
MD5 | b34c7f806e28b65a6bd4d4ea321f366f |
|
BLAKE2b-256 | 0e2017a86f222114bf853ad20c727e0122ccb3359078f6931462df028f839ff4 |
Hashes for quantized_mesh_encoder-0.2.1-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 937ae4561b16db3d00fe47e133d933b0e4e11946b3aa72a5ae8df40a118d1814 |
|
MD5 | 171df23119d60523f5dfdb68d0567be9 |
|
BLAKE2b-256 | 8dfb64bbcfefc6ac6c9306d9067e8547a850f33d1e54de65bb7aec1643df8bf9 |
Hashes for quantized_mesh_encoder-0.2.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01f2d8563f69b87a058e9a15be1f92719a6beecbb230514b8b1b54c8e55213cb |
|
MD5 | 035d8532362e2db8db965498a096e9fc |
|
BLAKE2b-256 | a3e39f63dcef652d5e5a460ce8ce5c13b79f65b7af76a5a01fc30e606bb0ff7d |
Hashes for quantized_mesh_encoder-0.2.1-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ece95724fc2561b64d6c0b386f97326976976805c22598a74c027d06d529caf9 |
|
MD5 | 9a41be964d963f432b8552a7ea792546 |
|
BLAKE2b-256 | 86bd7a79fc7260c08e9d485af3fba3c1d8314d811d3e195f102975e58399f73c |
Hashes for quantized_mesh_encoder-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db5e79cb157c980abd24955570989a4ae02f84b3f9adf1ff948c7831ca5d269a |
|
MD5 | 6a74e0db368f73b8337ac937d863a5fa |
|
BLAKE2b-256 | 644ad1fe5b1a9562c5d696d4ad3df0f28bfe6e01baaf415954ed98eadd6b4819 |
Hashes for quantized_mesh_encoder-0.2.1-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d569554f0eaef4c04f6b004dd804e2c4129b62c2d82f2e47bb787f3338053c48 |
|
MD5 | 3b24bdc2ee1fa3ddd260d3e01d28575f |
|
BLAKE2b-256 | c2dc973f75b6b8dd181c4a3c6b0515252cc3ad6cb8c8d0d911742bdfb0840dff |
Hashes for quantized_mesh_encoder-0.2.1-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 081f3cc0e3caf6e29e18bb4d8c53254523457edfa6857eb106c0ea0ab309f0bb |
|
MD5 | 2996ae8f03ede56dafcb435e25a58a3a |
|
BLAKE2b-256 | 5cd574859c66af78db43cea201315f43c35dc1ace352722a8366413bab67d26b |
Hashes for quantized_mesh_encoder-0.2.1-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bc1ed48db3b3104eb0cf336a51fceb8c5dc19f60b7f1b4815fd5c3f31b54bde |
|
MD5 | 4d56bfc5b4dc30c14ea6da5e26a570b0 |
|
BLAKE2b-256 | defc1d7c1e6ba7596c96e904ebb07e2b3fd5451146a6f8c027f0a834eddb364d |
Hashes for quantized_mesh_encoder-0.2.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7117f603085d0c10a4ad96afe24330fac7c235ec4081414d9be5d18fb6b7fad3 |
|
MD5 | 23c286f546cd7a0907956ea835da930e |
|
BLAKE2b-256 | 564fa886db28346f079663dd00ff38cb873390386126712b59c097f6b65c87c1 |
Hashes for quantized_mesh_encoder-0.2.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 530d9afd603044c5a49a62c1f54cd19da215aa96e1a313cec13f3d1ad63c9105 |
|
MD5 | f5d25520441372b1abc9919357a88581 |
|
BLAKE2b-256 | 8b5f9b0c9c2c2f4e4ce6931ae109409051623b93aa526a620cd53e1690cc5758 |