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.
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 numpy
pip install quantized-mesh-encoder
Cython is used internally, and currently only source builds are provided, so you'll need to have Cython and a C compiler available during installation. Additionally Numpy must already exist during install. Pull requests are welcome to package as platform-specific wheels.
Using
API
encode
Parameters:
f
: a file 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
.
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.
martini = Martini(257)
# generate RTIN hierarchy from terrain data (an array of size^2 length)
tile = martini.create_tile(terrain)
# get a mesh (vertices and triangles indices) for a 10m error
vertices, triangles = tile.get_mesh(10)
buf = BytesIO()
encode(buf, vertices, triangles)
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
Hashes for quantized-mesh-encoder-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3269724a134cddaaf57911b212ca7d0d08b674b3e8066c0f8d7a65af8efc6a1c |
|
MD5 | 3db5be6e777a9548b7f0e269c6428f8f |
|
BLAKE2b-256 | 036262b32d4533bf9bff6e54c9410b9f858aeac1599cd880ef1a4de8e72e6c8a |