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.

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 Cython
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 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.

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


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

quantized_mesh_encoder-0.1.2-pp36-pypy36_pp73-win32.whl (172.6 kB view hashes)

Uploaded PyPy Windows x86

quantized_mesh_encoder-0.1.2-pp36-pypy36_pp73-manylinux2010_x86_64.whl (192.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.12+ x86-64

quantized_mesh_encoder-0.1.2-pp36-pypy36_pp73-macosx_10_9_x86_64.whl (177.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

quantized_mesh_encoder-0.1.2-cp38-cp38-win_amd64.whl (193.4 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

quantized_mesh_encoder-0.1.2-cp38-cp38-win32.whl (181.2 kB view hashes)

Uploaded CPython 3.8 Windows x86

quantized_mesh_encoder-0.1.2-cp38-cp38-manylinux2010_x86_64.whl (472.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

quantized_mesh_encoder-0.1.2-cp38-cp38-manylinux2010_i686.whl (457.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.1.2-cp38-cp38-macosx_10_9_x86_64.whl (193.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

quantized_mesh_encoder-0.1.2-cp37-cp37m-win_amd64.whl (192.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

quantized_mesh_encoder-0.1.2-cp37-cp37m-win32.whl (180.3 kB view hashes)

Uploaded CPython 3.7m Windows x86

quantized_mesh_encoder-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl (439.3 kB view hashes)

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

quantized_mesh_encoder-0.1.2-cp37-cp37m-manylinux2010_i686.whl (423.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.1.2-cp37-cp37m-macosx_10_9_x86_64.whl (193.6 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

quantized_mesh_encoder-0.1.2-cp36-cp36m-win_amd64.whl (192.5 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

quantized_mesh_encoder-0.1.2-cp36-cp36m-win32.whl (180.3 kB view hashes)

Uploaded CPython 3.6m Windows x86

quantized_mesh_encoder-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl (439.3 kB view hashes)

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

quantized_mesh_encoder-0.1.2-cp36-cp36m-manylinux2010_i686.whl (422.9 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.1.2-cp36-cp36m-macosx_10_9_x86_64.whl (193.4 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

quantized_mesh_encoder-0.1.2-cp35-cp35m-win_amd64.whl (191.6 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

quantized_mesh_encoder-0.1.2-cp35-cp35m-win32.whl (179.8 kB view hashes)

Uploaded CPython 3.5m Windows x86

quantized_mesh_encoder-0.1.2-cp35-cp35m-manylinux2010_x86_64.whl (435.2 kB view hashes)

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

quantized_mesh_encoder-0.1.2-cp35-cp35m-manylinux2010_i686.whl (419.9 kB view hashes)

Uploaded CPython 3.5m manylinux: glibc 2.12+ i686

quantized_mesh_encoder-0.1.2-cp35-cp35m-macosx_10_9_x86_64.whl (191.6 kB view hashes)

Uploaded CPython 3.5m 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