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 pydelatin or 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

With pip:

pip install quantized-mesh-encoder

or with Conda:

conda install -c conda-forge quantized-mesh-encoder

Using

API

quantized_mesh_encoder.encode

Arguments:

  • f: a writable file-like object in which to write encoded bytes
  • positions: (array[float]): either a 1D Numpy array or a 2D Numpy array of shape (-1, 3) containing 3D positions.
  • indices (array[int]): either a 1D Numpy array or a 2D Numpy array of shape (-1, 3) 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.

Keyword arguments:

  • 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
  • ellipsoid (quantized_mesh_encoder.Ellipsoid, optional): ellipsoid defined by its semi-major a and semi-minor b axes. Default: WGS84 ellipsoid.
  • extensions: list of extensions to encode in quantized mesh object. These must be Extension instances. See Quantized Mesh Extensions.

quantized_mesh_encoder.Ellipsoid

Ellipsoid used for mesh calculations.

Arguments:

  • a (float): semi-major axis
  • b (float): semi-minor axis

quantized_mesh_encoder.WGS84

Default WGS84 ellipsoid. Has a semi-major axis a of 6378137.0 meters and semi-minor axis b of 6356752.3142451793 meters.

Quantized Mesh Extensions

There are a variety of extensions to the Quantized Mesh spec.

quantized_mesh_encoder.VertexNormalsExtension

Implements the Terrain Lighting extension. Per-vertex normals will be generated from your mesh data.

Keyword Arguments:

  • indices: mesh indices
  • positions: mesh positions
  • ellipsoid: instance of Ellipsoid class, default: WGS84 ellipsoid
quantized_mesh_encoder.WaterMaskExtension

Implements the Water Mask extension.

Keyword Arguments:

  • data (Union[np.ndarray, np.uint8, int]): Data for water mask.
quantized_mesh_encoder.MetadataExtension

Implements the Metadata extension.

  • data (Union[Dict, bytes]): Metadata data to encode. If a dictionary, json.dumps will be called to create bytes in UTF-8 encoding.

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 an in-memory buffer instead of a file

from io import BytesIO
from quantized_mesh_encoder import encode
with BytesIO() as bio:
    encode(bio, positions, indices)

Or to gzip the in-memory buffer:

import gzip
from io import BytesIO
with BytesIO() as bio:
    with gzip.open(bio, 'wb') as gzipf:
        encode(gzipf, positions, indices)

Alternate Ellipsoid

By default, the WGS84 ellipsoid is used for all calculations. An alternate ellipsoid may be useful for non-Earth planetary bodies.

from quantized_mesh_encoder import encode, Ellipsoid

# From https://ui.adsabs.harvard.edu/abs/2010EM%26P..106....1A/abstract
mars_ellipsoid = Ellipsoid(3_395_428, 3_377_678)

with open('output.terrain', 'wb') as f:
    encode(f, positions, indices, ellipsoid=mars_ellipsoid)

Quantized Mesh Extensions

from quantized_mesh_encoder import encode, VertexNormalsExtension, MetadataExtension

vertex_normals = VertexNormalsExtension(positions=positions, indices=indices)
metadata = MetadataExtension(data={'hello': 'world'})

with open('output.terrain', 'wb') as f:
    encode(f, positions, indices, extensions=(vertex_normals, metadata))

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 pydelatin or pymartini, fast elevation heightmap to terrain mesh generators.

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.4.3.tar.gz (138.0 kB view hashes)

Uploaded Source

Built Distributions

quantized_mesh_encoder-0.4.3-cp311-cp311-win_amd64.whl (199.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

quantized_mesh_encoder-0.4.3-cp311-cp311-musllinux_1_1_x86_64.whl (540.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp311-cp311-musllinux_1_1_i686.whl (513.9 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (548.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

quantized_mesh_encoder-0.4.3-cp311-cp311-macosx_11_0_arm64.whl (200.5 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

quantized_mesh_encoder-0.4.3-cp311-cp311-macosx_10_9_x86_64.whl (208.3 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

quantized_mesh_encoder-0.4.3-cp310-cp310-win_amd64.whl (200.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

quantized_mesh_encoder-0.4.3-cp310-cp310-musllinux_1_1_x86_64.whl (530.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp310-cp310-musllinux_1_1_i686.whl (509.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (532.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

quantized_mesh_encoder-0.4.3-cp310-cp310-macosx_11_0_arm64.whl (201.5 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

quantized_mesh_encoder-0.4.3-cp310-cp310-macosx_10_9_x86_64.whl (209.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

quantized_mesh_encoder-0.4.3-cp39-cp39-win_amd64.whl (200.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

quantized_mesh_encoder-0.4.3-cp39-cp39-musllinux_1_1_x86_64.whl (539.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp39-cp39-musllinux_1_1_i686.whl (517.5 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (536.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

quantized_mesh_encoder-0.4.3-cp39-cp39-macosx_11_0_arm64.whl (200.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

quantized_mesh_encoder-0.4.3-cp39-cp39-macosx_10_9_x86_64.whl (208.5 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

quantized_mesh_encoder-0.4.3-cp38-cp38-win_amd64.whl (209.8 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

quantized_mesh_encoder-0.4.3-cp38-cp38-musllinux_1_1_x86_64.whl (559.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp38-cp38-musllinux_1_1_i686.whl (540.4 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (547.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

quantized_mesh_encoder-0.4.3-cp38-cp38-macosx_11_0_arm64.whl (208.9 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

quantized_mesh_encoder-0.4.3-cp38-cp38-macosx_10_9_x86_64.whl (216.7 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

quantized_mesh_encoder-0.4.3-cp37-cp37m-win_amd64.whl (209.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

quantized_mesh_encoder-0.4.3-cp37-cp37m-musllinux_1_1_x86_64.whl (524.5 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp37-cp37m-musllinux_1_1_i686.whl (507.6 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.6 kB view hashes)

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

quantized_mesh_encoder-0.4.3-cp37-cp37m-macosx_10_9_x86_64.whl (217.1 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

quantized_mesh_encoder-0.4.3-cp36-cp36m-win_amd64.whl (218.7 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

quantized_mesh_encoder-0.4.3-cp36-cp36m-musllinux_1_1_x86_64.whl (524.5 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

quantized_mesh_encoder-0.4.3-cp36-cp36m-musllinux_1_1_i686.whl (505.8 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

quantized_mesh_encoder-0.4.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (521.9 kB view hashes)

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

quantized_mesh_encoder-0.4.3-cp36-cp36m-macosx_10_9_x86_64.whl (217.0 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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