Giter Site home page Giter Site logo

kylebarron / quantized-mesh-encoder Goto Github PK

View Code? Open in Web Editor NEW
79.0 4.0 6.0 6.5 MB

A fast Python Quantized Mesh encoder

Home Page: https://kylebarron.dev/quantized-mesh-encoder

License: MIT License

Python 63.32% HTML 3.64% CSS 1.61% JavaScript 27.24% Cython 4.18%
terrain-mesh-generator cesium deck-gl terrain quantized-mesh mesh mesh-processing

quantized-mesh-encoder's Introduction

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.

quantized-mesh-encoder's People

Contributors

davidbrochart avatar dependabot[bot] avatar kylebarron avatar malaretv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quantized-mesh-encoder's Issues

Finish ritter method for bounding sphere

Currently I only have the "naive" method of finding an axis-aligned bounding box, then finding the sphere from that box.

It would be good to finish the ritter method, then to take the minimum of the two of them.

References:

- change to onViewportLoad to align with Tile3DLayer

// TODO - change to onViewportLoad to align with Tile3DLayer
onViewportLoad: {type: 'function', optional: true, value: null, compare: false},
onTileLoad: {type: 'function', value: tile => {}, compare: false},
// eslint-disable-next-line
onTileError: {type: 'function', value: err => console.error(err), compare: false},
extent: {type: 'array', optional: true, value: null, compare: true},


This issue was generated by todo based on a TODO comment in 204bcfc. It's been assigned to @kylebarron because they committed the code.

Check triangleIndices

This is decoded with quantized-mesh-decoder in js:
image

The triangleIndices looks a little suspect. Especially since there are two zeros in a row.

Example demo is not working properly

Hi @kylebarron,
I can't visualize proper your example demo: https://kylebarron.dev/quantized-mesh-encoder/

This is what I see:
image

Looks like it's related to CORS

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at 
https://us-east-1-lambda.kylebarron.dev/dem/mesh/13/1507/2820.terrain?url=terrarium&mesh_max_error=9.41&mesh_algorithm=pydelatin&flip_y=true. 
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 530.

Vertex normals

Optionally compute vertex normals extension. There's two parts:

Normal Generation

Based on the quantized-mesh-tile implementation, essentially for every vertex you look at all the triangles that touch it. For each triangle, compute its normal, and then average the normals of the touching triangles to find each per-vertex normal.

I think that to make it performant, you'd want to first compute all the normals of the triangles. And then you can use that array of normals somehow as a lookup table, so that you can find all the triangles per vertex at once, and then average within the axis...

Normal Encoding

Quantizing and encoding each x, y, z 96-bit floating point unit vector into an x,y 16-bit representation.

References

Check winding order of input triangles

It looks like (py)martini creates a mesh with vertices (at least sometimes) in clockwise order, while the Quantized Mesh spec requires counter-clockwise winding order.

e.g. here are the first three 2D vertices, and it looks like they define a clockwise triangle

image

So I should add an option, probably True by default, that checks the input geometries and flips the order of indices if the triangle is in clockwise winding order.

It looks like it shouldn't actually be too slow to check, though it might be best in Cython... not sure if I can vectorize it easily

This might solve #9 , #11

Allow indices to be a 2d array

e.g. pydelatin currently returns a 2d array of shape (-1, 3) (which is sensible) but encode_indices expects a 1d array.

You should also cast to np.uint32, the data type expected by the cython encode_indices implementation.

You should also use .astype(np.uint32, casting='safe') so that an error would be produced if the data can't be transformed effectively.

Water mask encoding (not generation)

I don't want to add code to generate a water mask, since you can't assume elevations < 0 are ocean. But you could add an argument to encode to allow a user to pass a pregenerated water mask.

Comparing deck.gl rendering vs maptiler

image

url:
https://api.maptiler.com/tiles/terrain-quantized-mesh/13/3090/5737.terrain?key=...

bounds:
-112.08251953125,
36.0791015625,
-112.1044921875,
36.057128906249986

x, y, level = 3090, 5737, 13

import numpy as np
self =GeographicTilingScheme()
class GeographicTilingScheme:
    def __init__(self):
        self._numberOfLevelZeroTilesX = 2
        self._numberOfLevelZeroTilesY = 1
        self._rectangle = (-np.pi, -np.pi/2, np.pi, np.pi/2)

    def getNumberOfXTilesAtLevel(self, level):
        """
        Gets the total number of tiles in the X direction at a specified level-of-detail

         @param {Number} level The level-of-detail.
         @returns {Number} The number of tiles in the X direction at the given level.

        """
        return self._numberOfLevelZeroTilesX << level

    def getNumberOfYTilesAtLevel(self, level):
        """
        Gets the total number of tiles in the Y direction at a specified level-of-detail.

         @param {Number} level The level-of-detail.
         @returns {Number} The number of tiles in the Y direction at the given level.

        """
        return self._numberOfLevelZeroTilesY << level

    def tileXYToRectangle(self, x, y, level):
        rectangle = self._rectangle

        xTiles = self.getNumberOfXTilesAtLevel(level)
        yTiles = self.getNumberOfYTilesAtLevel(level)

        width = rectangle[2] - rectangle[0]
        xTileWidth = width / xTiles
        west = x * xTileWidth + rectangle[0]
        east = (x + 1) * xTileWidth + rectangle[0]

        height = rectangle[3] - rectangle[1]
        yTileHeight = height / yTiles
        north = rectangle[3] - y * yTileHeight
        south = rectangle[3] - (y + 1) * yTileHeight

        return np.array([east, south, west, north])

    def tileXYToNativeRectangle(self, x, y, level):
        """
        Converts tile x, y coordinates and level to a rectangle expressed in the native coordinates
        of the tiling scheme.

        Args:
            - x: The integer x coordinate of the tile.
            - y: The integer y coordinate of the tile.
            - level: level The tile level-of-detail.  Zero is the least detailed.
        """
        return np.degrees(self.tileXYToRectangle(x, y, level))

from shapely.geometry import box
bounds = list(np.degrees(self.tileXYToRectangle(x, y, level)))
bounds[1] = np.abs(bounds[1])
bounds[3] = np.abs(bounds[3])

print(bounds)
g = box(bounds[0], bounds[3], bounds[2], bounds[1])
from keplergl_cli import Visualize
g.exterior.coords
Visualize(g)
type(g)
g.exterior.coords
box(*np.degrees(self.tileXYToRectangle(x, y, level)))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.