Skip to main content

A fast Python Quantized Mesh encoder

Project description

quantized-mesh-encoder

Build Status

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 (as of release 8.2, expected July 2020.)

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 bytes
  • positions: (array[float]): a flat Numpy array of 3D positions.
  • indices (array[int]): a flat Numpy array indicating triples of coordinates from positions to make triangles. For example, if the first three values of indices are 0, 1, 2, then that defines a triangle formed by the first 9 values in positions, three for the first vertex (index 0), 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 of positions.
  • 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' or None. Default is None.
    • '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 the bounding_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


Download files

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

Source Distribution

quantized-mesh-encoder-0.2.0.tar.gz (135.6 kB view details)

Uploaded Source

Built Distributions

quantized_mesh_encoder-0.2.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl (200.0 kB view details)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_x86_64.whl (498.5 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_i686.whl (482.0 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl (462.7 kB view details)

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

quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_i686.whl (446.1 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl (462.8 kB view details)

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

quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_i686.whl (445.5 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_x86_64.whl (461.9 kB view details)

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

quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_i686.whl (445.5 kB view details)

Uploaded CPython 3.5m manylinux: glibc 2.12+ i686

File details

Details for the file quantized-mesh-encoder-0.2.0.tar.gz.

File metadata

  • Download URL: quantized-mesh-encoder-0.2.0.tar.gz
  • Upload date:
  • Size: 135.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200604 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for quantized-mesh-encoder-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f8b8370a993994437e5a8eb0e61c1f23cec8750b9774c7100349ce534a4f4ade
MD5 01aa1532b4c5f26f5d3fc006b55668b1
BLAKE2b-256 65ce76f538a3dff6d08e31fdbe183595e1ae4a825271116285ad0b65a0e4f1ee

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-pp36-pypy36_pp73-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5210a127c71117cfc1def1d33b8a225cac7276c8b7d72e8d326c78a971c9b130
MD5 6773779d49bf9f52f68d9620bc95dbca
BLAKE2b-256 a340fb57f8db0e356be687a595c74569ae556b9f1978bcd59f9f7ca311b0a10f

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-pp36-pypy36_pp73-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-pp36-pypy36_pp73-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a881a87fcf4505c53c5ef71c36e3ee78b2068a59d133c9ba0e8a27c208b01d39
MD5 8bb73f7e7b9e383e36758d01f81eafaf
BLAKE2b-256 3cb0f6f8aabd33157ed3b5c772fa90407f4be90677c92d717afc138c8520ab87

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_x86_64.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 498.5 kB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 ce29d1d710ee0f5a5ab087f461fe54b6af213b6933088499f64a3dcaecc236d8
MD5 7587e7cdc75d2c350b5bfb8ee7b2482e
BLAKE2b-256 03dca63f50bb3c9f6055979dd499dbfaedc217ddea1ea16be0c0bc38e62dd0a8

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_i686.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_i686.whl
  • Upload date:
  • Size: 482.0 kB
  • Tags: CPython 3.8, manylinux: glibc 2.12+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux2010_i686.whl
Algorithm Hash digest
SHA256 7cfee8220badeddb7f6134856380d4b74ce15dcaecd391eecee5c227489e6722
MD5 f88dcd3a53f19450739f728535f12d6c
BLAKE2b-256 636a28092fa6c04b482fce4cdd655cedd5063723a70fe4a8ffef1c096ef051e9

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9ad6602aa9cc67fb5cc3a5f8d287fc91f09df2d4a399f8372ed0a844f3e956f0
MD5 e51499380b8397047e13322aeb055b74
BLAKE2b-256 8b7502add3d3ceb38a7b6c562c8faa4612186618e2efaa163bb38a041c297d49

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux1_i686.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp38-cp38-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7657d760be7363c5b143ba34ecf290ae5fb8d12effcaf1caa4c644e5e4c04815
MD5 fabfe118438cdb61ca976c475b33b2c9
BLAKE2b-256 bb7927724e1329ba592272bf7f25cea98d0e31cc81f34ef336e07478faf1897a

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 462.7 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 9799847949197e5515ff1b455bb8a319652d1a18139dfa878eea8228dd70afcd
MD5 6a197de93f8a9b173a5becd57751ba3f
BLAKE2b-256 c0f41a07547f1afe2874579d1eeedb50b4d5bb2f75fe98d86ec991f442675877

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_i686.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_i686.whl
  • Upload date:
  • Size: 446.1 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux2010_i686.whl
Algorithm Hash digest
SHA256 8ce7439f833a5f4ba5837abf36be419749a69b457b6c2a8b6c5b1f12a8acec65
MD5 72929433ac07e11963a8f79bfe4ef4b1
BLAKE2b-256 005f99fabe6a70eb6ff1b37e1c9bf6e5eb0c68c9fc96ed4a30ea8dd39382b514

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d0e6da5013969d99b6da2158cd7b3950d0e2a185a9a7100b631156762b5adeee
MD5 5a9d689459985a3cf169b0b999ee41cb
BLAKE2b-256 2d6197ae68a77079cd682d267a9da4504551c5c27676b448e23e4e2265d77723

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp37-cp37m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f6ae4549d8a827203a2a87e75fc5d3317edf14293ffeefe4afac68faea891f95
MD5 fdaf15cee26c8e252ec89e07768336af
BLAKE2b-256 ea1e1adc86392a727b5366e01827c3f0b91d5c095e5d39f07eac3890eb4f0b5e

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 462.8 kB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 88a91e3deb511b82a72a5655ff8e4b6c05d65bad2d82d7c6fa0c28553806862a
MD5 0b23bddc26a1cd693e2b5550cbf69bed
BLAKE2b-256 3762845847e077238f2a357817ed44fabc1318656c860fbb9d78b0bf176e8494

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_i686.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_i686.whl
  • Upload date:
  • Size: 445.5 kB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux2010_i686.whl
Algorithm Hash digest
SHA256 8674be75071c3771ebdbdb288bf1dede7f580e580aa58073fa8ff7f5fc8d36fc
MD5 40a983748d9e38ef3165e2f333ed6d15
BLAKE2b-256 aeab2e4defa10befc4215f00d060ea1cd9bf7f1610fe991945a6507f2c3797f4

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5dd6ca3b7e1ac0b41e8583c3cd4a4efedf9fe99b99d152d8376567b8e7b340f9
MD5 d6c3bb9aabcff2fcda4862d15c037bbb
BLAKE2b-256 1d744f6339481a0d877cf211c1b07004cf0bc0977bad79e783b9881c0f5431c2

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0f8321393ebff3c5cd4fc612b85aa7aa3acea55f7a437eee71454f0cefae81fc
MD5 69b88cc663dbc0dc34231d79943700bc
BLAKE2b-256 a1cd41a0e3595a6aea115b9a756d59f842ef8834cdc83034a02d324013a8d2a2

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 461.9 kB
  • Tags: CPython 3.5m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5e1318e878b22643955e38bcd8a8cddf66e52cf403fc45bfc590471e9b57727b
MD5 51cf7220ce364a39ef1d99d780b27115
BLAKE2b-256 019528adfaa0b1f42f90286dd0f7fcc5686635f578f0f1aac60ee4ca9914327d

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_i686.whl.

File metadata

  • Download URL: quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_i686.whl
  • Upload date:
  • Size: 445.5 kB
  • Tags: CPython 3.5m, manylinux: glibc 2.12+ i686
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux2010_i686.whl
Algorithm Hash digest
SHA256 5cd67cc413fe412e52fe1c80d5676a23047e7232e8e6141c6b32494ba3ea7cc2
MD5 24fc002fa404465f0fac3c3282f26904
BLAKE2b-256 c5327f6035b2600bf6cc6d3ef1cec3acd8b7ce6cb3e8789e76cbafdfd7375bbb

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5f89de58cc4d2c42de93f1d1fef468d47a4b0f665ee0762014ea4a62fbff88bc
MD5 5667d68ac8127dfdec03a3af1c12b051
BLAKE2b-256 ea8c478dab6cd59be5e1b9d92b73f6d733d6d481504f14deb14fc43cf3ccf71c

See more details on using hashes here.

File details

Details for the file quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for quantized_mesh_encoder-0.2.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 bcf357a59fd4d57dae0e782636e344a0ad7eb2ed8506205eacc067993434b9f3
MD5 a1a9924d74767d8f2baf7a48af2fca75
BLAKE2b-256 f349fe36c1c72e00cf05e24017c07792c03814bd2f19986dd2ea14a26370edd0

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