|
| 1 | +from io import BytesIO |
| 2 | +from numbers import Number |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from quantized_mesh_encoder.encode import ( |
| 6 | + compute_header, encode, encode_header, interp_positions) |
| 7 | +from quantized_mesh_tile import TerrainTile |
| 8 | + |
| 9 | + |
| 10 | +def test_compute_header(): |
| 11 | + positions = np.array([0, 0, 0, 1, 1, 1, 0, 1, 4], dtype=np.float32).reshape( |
| 12 | + -1, 3) |
| 13 | + |
| 14 | + header = compute_header(positions) |
| 15 | + keys = [ |
| 16 | + 'centerX', 'centerY', 'centerZ', 'minimumHeight', 'maximumHeight', |
| 17 | + 'boundingSphereCenterX', 'boundingSphereCenterY', |
| 18 | + 'boundingSphereCenterZ', 'boundingSphereRadius', |
| 19 | + 'horizonOcclusionPointX', 'horizonOcclusionPointY', |
| 20 | + 'horizonOcclusionPointZ'] |
| 21 | + assert all(k in header.keys() for k in keys), 'Header key missing' |
| 22 | + assert all( |
| 23 | + isinstance(v, Number) |
| 24 | + for v in header.values()), 'Header value not numeric' |
| 25 | + |
| 26 | + |
| 27 | +def test_encode_header(): |
| 28 | + header = { |
| 29 | + 'centerX': 6377169.5, |
| 30 | + 'centerY': 55648.50390625, |
| 31 | + 'centerZ': 55284.421875, |
| 32 | + 'minimumHeight': 0.0, |
| 33 | + 'maximumHeight': 4.0, |
| 34 | + 'boundingSphereCenterX': 6377169.5, |
| 35 | + 'boundingSphereCenterY': 55648.504, |
| 36 | + 'boundingSphereCenterZ': 55284.42, |
| 37 | + 'boundingSphereRadius': 78447.81, |
| 38 | + 'horizonOcclusionPointX': 6378226.71931529, |
| 39 | + 'horizonOcclusionPointY': 55657.731270568445, |
| 40 | + 'horizonOcclusionPointZ': 55293.58686988714} |
| 41 | + |
| 42 | + buf = BytesIO() |
| 43 | + encode_header(buf, header) |
| 44 | + buf.seek(0) |
| 45 | + b = buf.read() |
| 46 | + |
| 47 | + assert len(b) == 88, 'Header incorrect number of bytes' |
| 48 | + |
| 49 | + |
| 50 | +def test_encode_decode(): |
| 51 | + positions = np.array( |
| 52 | + [0, 0, 0, 1, 1, 1, 0, 1, 4, 2, 3, 4, 8, 9, 10, 12, 13, 14], |
| 53 | + dtype=np.float32) |
| 54 | + triangles = np.array([0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5], dtype=np.uint32) |
| 55 | + with open('test2.terrain', 'wb') as f: |
| 56 | + encode(f, positions, triangles) |
| 57 | + f = BytesIO() |
| 58 | + encode(f, positions, triangles) |
| 59 | + |
| 60 | + f.seek(0) |
| 61 | + tile = TerrainTile() |
| 62 | + tile.fromBytesIO(f) |
| 63 | + |
| 64 | + assert np.array_equal( |
| 65 | + triangles, np.array(tile.indices, dtype=np.uint32)), 'Indices incorrect' |
| 66 | + u, v, h = interp_positions(positions.reshape(-1, 3)).T |
| 67 | + |
| 68 | + assert np.array_equal( |
| 69 | + u, np.array(tile.u, dtype=np.uint32)), 'Vertices incorrect' |
| 70 | + assert np.array_equal( |
| 71 | + v, np.array(tile.v, dtype=np.uint32)), 'Vertices incorrect' |
| 72 | + assert np.array_equal( |
| 73 | + h, np.array(tile.h, dtype=np.uint32)), 'Vertices incorrect' |
| 74 | + |
| 75 | + assert tile.westI == [0, 2] |
| 76 | + assert tile.southI == [0] |
| 77 | + assert tile.eastI == [5] |
| 78 | + assert tile.northI == [5] |
0 commit comments