17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-29 17:27:30 +01:00

Cook the ground into a quadtree of mesh tiles, and keep the mesh the scenery drew

The ground in a scenery is already an irregular mesh, so the cook keeps it: it cuts the
triangles along the tile grid, welds what coincides, and builds each coarser level by
collapsing edges. Nothing is resampled, nothing is interpolated, and a hole the scenery left
stays a hole. What was here before threw the triangles away, kept their vertices and built a
fresh Delaunay triangulation over them, which filled every hole, merged the two heights of
every retaining wall into one, and - because it never erased the super-triangle - emitted
indices three too large together with triangles reaching out to the artificial corners.

Tiles form a quadtree, as Cesium Quantized Mesh intends. A distant tile is a large one, so the
horizon costs tens of tiles rather than the thousands a fixed grid needed, and the level to
draw follows from the tile's own geometric error against what the screen can resolve - which is
what the format carries that field for. Every vertex on a tile's border survives into all of
its coarser levels, and so does every edge between two of them, so two neighbours drawn at
different levels share their border exactly: there is nothing to stitch and nothing to morph.

The file layout is the format's, with two deliberate departures, both written down in
quantizedmeshformat.h: the geodetic header fields carry scenery metres, there being no
geographic reference to convert from, and the material a vertex belongs to rides in an
extension of ours. Texture coordinates are not stored at all - ground tiles in world space, so
the coordinate follows from the position and from how many metres one repeat of the material
covers, measured off the scenery once and kept in the terrain's table.

Tiles live in one archive beside the scenery, an entry each, compressed on its own. Baltyk
cooks to 546 kB against 2.2 MB of heightfields, quark to 361 kB against 9.6 MB, tomaszewo to
375 kB against 9.9 MB.

Drawing is one call a frame: every resident tile lives in the same vertex and index buffer,
with its vertices measured from the middle of the terrain so that the whole map fits in floats,
and a frame is one glMultiDrawElementsBaseVertex over the tiles the walk chose and the frustum
kept. On baltyk that settles at 291 tiles resident, 16 MB of mesh, 85 tiles and 6k triangles
drawn.

What counts as ground is what the scenery says, not what its triangles look like. A node under
origin, rotate or scale is an object standing on the ground; a material with an alpha channel is
a tree, a bush, a fence or a catenary mast. Everything else is laid whichever way it faces, so
the side of an embankment or a cutting goes in with the rest instead of leaving a hole along
every bank. Whatever is left out is recorded per node, and the scenery goes on drawing it.

A cook is a cache: it is used again only when the archive, the note of which nodes went into it
and the manifest of what the scenery was made from all agree. A manifest listing a file the
scenery names but does not have used to fail that test for ever, since an absent file never
matches - quark has such an include, and nothing beside it could ever be reused.

tools/terraincook cooks the same way outside the engine, and meshtest beside it checks what the
cook promises: a tile reads back as written, neighbouring tiles agree on their border to the
millimetre, a hole is not filled in, a wall is laid exactly once, and a material's tiling comes
out of the source. seamcheck.py goes: it tested the morphing constants of a heightfield nothing
reads any more.
This commit is contained in:
maj00r
2026-09-20 08:04:46 +02:00
parent 975e62df95
commit b877bc0772
36 changed files with 3356 additions and 3614 deletions

View File

@@ -13,19 +13,28 @@ if(NOT zstd_FOUND)
add_library(zstd::libzstd ALIAS PkgConfig::ZSTD)
endif()
find_package(CDT CONFIG REQUIRED)
add_executable(terraincook
add_executable(terraincook
main.cpp
../scene/terraincooker_tin.cpp
../../scene/quantizedmeshcooker.cpp
)
# what the cook promises: a tile reads back as it was written, neighbouring tiles share their border
# whatever level each is drawn at, a hole stays a hole and a wall is laid once
add_executable(meshtest
meshtest.cpp
../../scene/quantizedmeshcooker.cpp
)
target_include_directories(meshtest PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../..")
target_link_libraries(meshtest PRIVATE zstd::libzstd)
if(NOT MSVC)
target_compile_options(meshtest PRIVATE -O2 -Wall)
endif()
add_test(NAME meshtest COMMAND meshtest)
enable_testing()
# the on-disk format lives with the engine, so the tool and the reader cannot drift apart
target_include_directories(terraincook PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../..")
target_link_libraries(terraincook PRIVATE
zstd::libzstd
CDT::CDT
)
target_link_libraries(terraincook PRIVATE zstd::libzstd)
if(NOT MSVC)
target_compile_options(terraincook PRIVATE -O2 -Wall)

View File

@@ -33,8 +33,8 @@ http://mozilla.org/MPL/2.0/.
#include "scene/heightfieldformat.h"
#include "scene/heightfieldreader.h"
#include "scene/terraincooker.h"
#include "scene/terraincooker_tin.h"
#include "scene/quantizedmeshformat.h"
#include "scene/quantizedmeshcooker.h"
#include "scene/quantizedmeshreader.h"
namespace {
@@ -570,18 +570,14 @@ main( int argc, char *argv[] ) {
std::filesystem::path outputdirectory { "." };
double step { 0.0 };
bool cooking { false };
double tin_tilesize { 256.0 };
std::uint32_t tin_lodlevels { 4 };
bool compress { true };
std::filesystem::path archive { "terrain.pak" };
for( auto index { 1 }; index < argc; ++index ) {
std::string const argument { argv[ index ] };
if( argument == "-step" && index + 1 < argc ) { step = std::strtod( argv[ ++index ], nullptr ); continue; }
if( argument == "-out" && index + 1 < argc ) { outputdirectory = argv[ ++index ]; continue; }
if( argument == "-cook" ) { cooking = true; continue; }
if( argument == "-tilesize" && index + 1 < argc ) { tin_tilesize = std::strtod( argv[ ++index ], nullptr ); continue; }
if( argument == "-lodlevels" && index + 1 < argc ) { tin_lodlevels = static_cast<std::uint32_t>( std::strtoul( argv[ ++index ], nullptr, 10 ) ); continue; }
if( argument == "-raw" ) { compress = false; continue; }
if( argument == "-archive" && index + 1 < argc ) { archive = argv[ ++index ]; continue; }
if( argument == "-tracks" && index + 1 < argc ) {
std::filesystem::path const trackpath { argv[ ++index ] };
if( std::filesystem::is_directory( trackpath ) ) {
@@ -639,11 +635,16 @@ main( int argc, char *argv[] ) {
std::array<double, bandcount> bandedgesum {};
std::array<std::size_t, bandcount> bandfine {};
terrain::tin_cooker tin_cooker;
// the same cooker the engine's first-run cook uses, so the two cannot drift apart
quantizedmesh::cooker cooker;
if( cooking ) {
tin_cooker.configure( tin_tilesize, tin_lodlevels );
std::printf( "TIN cooking enabled: tile size %.0f m, %u LOD levels\n",
tin_tilesize, tin_lodlevels );
auto const scratch { ( outputdirectory / "terrain.cook" ).string() };
std::filesystem::create_directories( outputdirectory );
if( false == cooker.begin( scratch ) ) {
std::cerr << "cannot write " << scratch << "\n";
return 1;
}
std::printf( "cooking terrain mesh tiles into %s\n", ( outputdirectory / archive ).string().c_str() );
}
statistics stats;
@@ -663,7 +664,13 @@ main( int argc, char *argv[] ) {
parse( content, [ & ]( vertex const &A, vertex const &B, vertex const &C, std::string_view Material ) {
if( cooking ) { tin_cooker.rasterize( A, B, C, Material ); }
if( cooking ) {
auto const corner = []( vertex const &Vertex ) {
return quantizedmesh::cooker::corner {
Vertex.x, Vertex.y, Vertex.z,
static_cast<float>( Vertex.u ), static_cast<float>( Vertex.v ) }; };
cooker.add( corner( A ), corner( B ), corner( C ), Material );
}
++stats.triangles;
stats.vertices += 3;
vertex const points[ 3 ] { A, B, C };
@@ -876,31 +883,42 @@ main( int argc, char *argv[] ) {
writepreview( outputdirectory / "overlay.pgm", overlaypreview, previewwidth, previewheight, -1.0f );
if( cooking ) {
std::filesystem::create_directories( outputdirectory );
std::printf( "\n-- generating TIN tiles\n" );
auto tiles = tin_cooker.finish();
std::printf( "Writing %zu tiles...\n", tiles.size() );
std::size_t written = 0;
for( auto const &tile : tiles ) {
for( std::uint32_t lod = 0; lod < tile.lods.size(); ++lod ) {
char filename[ 256 ];
std::snprintf( filename, sizeof( filename ), "terrain_%d_%d_lod%u.qm",
tile.x, tile.z, lod );
std::filesystem::path const filepath = outputdirectory / filename;
if( terrain::tin_cooker::write_tile( tile, lod, filepath.string(), compress ) ) {
++written;
} else {
std::fprintf( stderr, "Failed to write %s\n", filename );
}
}
std::printf( "\n-- cooking terrain mesh tiles\n" );
pak::writer writer;
auto const archivepath { ( outputdirectory / archive ).string() };
if( false == writer.open( archivepath ) ) {
std::cerr << "cannot write " << archivepath << "\n";
return 1;
}
auto written { true };
quantizedmesh::terrain_table table;
auto const sink {
[ &writer, &written ](
quantizedmesh::tile_address const &Tile, std::vector<std::uint8_t> const &Bytes ) {
written = writer.add( quantizedmesh::tile_name( Tile ), Bytes ) && written; } };
if( ( false == cooker.finish( sink, table ) ) || ( false == written ) ) {
std::cerr << "cooking failed\n";
return 1;
}
written = writer.add( quantizedmesh::tablename, quantizedmesh::write_table( table ) ) && written;
if( ( false == writer.close() ) || ( false == written ) ) {
std::cerr << "cannot finish " << archivepath << "\n";
return 1;
}
auto const &report { cooker.result() };
std::printf( " %zu triangles laid in %zu tiles of %.0f m over %u levels\n",
report.triangles, report.tiles, cooker.finesttile(), report.levels );
std::printf( " %zu vertices, %llu kB, %zu triangles refused\n",
report.vertices, static_cast<unsigned long long>( writer.bytes() / 1024 ), report.refused );
for( std::uint32_t level = 0; level < table.levels; ++level ) {
std::printf( " level %u: tile %.0f m, off by %.3f m\n",
level, table.tilesize( level ), table.error( level ) );
}
for( auto const &material : table.materials ) {
std::printf( " material %-40s one repeat every %6.2f m\n", material.name.c_str(), material.repeat );
}
std::printf( "Wrote %zu quantized mesh files to %s\n", written, outputdirectory.string().c_str() );
}
std::printf( "\n previews written to %s (%zux%zu, 1 px = %.0f m)\n",

View File

@@ -0,0 +1,221 @@
// What the cook promises, checked on a terrain made up for the purpose: a tile reads back as it was
// written, two tiles sharing a border agree on it whatever level each is drawn at, a hole the scenery
// left stays a hole, a wall is ground and is laid exactly once, and the tiling of a material is
// measured off the source rather than guessed.
//
// Run it with no arguments; it writes its files to a temporary directory and removes them.
#include <cmath>
#include <cstdio>
#include <filesystem>
#include <set>
#include <system_error>
#include <map>
#include <tuple>
#include <set>
#include <vector>
#include "scene/quantizedmeshcooker.h"
#include "scene/quantizedmeshreader.h"
namespace qm = quantizedmesh;
int failures = 0;
void check( bool ok, char const *what ) {
if( !ok ) { std::printf( "FAIL: %s\n", what ); ++failures; }
}
int main() {
auto const directory = std::filesystem::temp_directory_path() / "eu07-meshtest";
std::error_code ignored;
std::filesystem::create_directories( directory, ignored );
std::string const scratch = ( directory / "terrain.cook" ).string();
std::string const archive = ( directory / "terrain.pak" ).string();
qm::cooker cooker;
cooker.quiet();
check( cooker.begin( scratch, 2000 ), "begin" );
auto const height = []( double x, double z ) {
return 20.0 * std::sin( x / 300.0 ) * std::cos( z / 250.0 ) + 0.01 * x;
};
// a 1200 x 1200 m field on a 10 m grid, with a square hole and one big triangle
double const step = 10.0;
int const cells = 120;
for( int ix = 0; ix < cells; ++ix ) {
for( int iz = 0; iz < cells; ++iz ) {
double const x0 = -600.0 + ix * step, x1 = x0 + step;
double const z0 = -600.0 + iz * step, z1 = z0 + step;
// a hole in the middle, as a scenery leaves where water or a building is
if( ( x0 >= -100.0 ) && ( x1 <= 100.0 ) && ( z0 >= -100.0 ) && ( z1 <= 100.0 ) ) { continue; }
char const *material = ( x0 < 0.0 ) ? "grass" : "gravel";
auto const corner = [&]( double x, double z ) {
return qm::cooker::corner{ x, height( x, z ), z,
static_cast<float>( x / 25.0 ), static_cast<float>( z / 25.0 ) };
};
cooker.add( corner( x0, z0 ), corner( x1, z0 ), corner( x1, z1 ), material );
cooker.add( corner( x0, z0 ), corner( x1, z1 ), corner( x0, z1 ), material );
}
}
// an upright wall, as the side of an embankment is. it is ground too, and must be laid - once,
// although it lies exactly along the border between two tiles (z = -512)
for( int piece = 0; piece < 4; ++piece ) {
double const xa = 100.0 + piece * 20.0, xb = xa + 20.0;
cooker.add( { xa, 0.0, -512.0, 0.f, 0.f }, { xb, 0.0, -512.0, 1.f, 0.f }, { xb, 8.0, -512.0, 1.f, 1.f }, "wall" );
cooker.add( { xa, 0.0, -512.0, 0.f, 0.f }, { xb, 8.0, -512.0, 1.f, 1.f }, { xa, 8.0, -512.0, 0.f, 1.f }, "wall" );
}
// one big triangle spanning many tiles, as 3ds Max terrain has
cooker.add( { -600.0, -5.0, 700.0, 0.f, 0.f }, { 600.0, -5.0, 700.0, 24.f, 0.f }, { 0.0, -5.0, 1100.0, 12.f, 16.f }, "grass" );
pak::writer writer;
check( writer.open( archive ), "pak open" );
qm::terrain_table table;
std::map<std::int64_t, qm::tile_address> written;
auto const sink = [&]( qm::tile_address const &tile, std::vector<std::uint8_t> const &bytes ) {
written.emplace( qm::tile_key( tile ), tile );
check( writer.add( qm::tile_name( tile ), bytes ), "pak add" );
};
check( cooker.finish( sink, table ), "finish" );
auto const tabletext = qm::write_table( table );
check( writer.add( qm::tablename, tabletext ), "table add" );
check( writer.close(), "pak close" );
auto const &report = cooker.result();
std::printf( "added %zu, laid %zu triangles in %zu tiles, %u levels, %zu vertices, %zu kB, refused %zu\n",
cooker.added(), report.triangles, report.tiles, report.levels, report.vertices,
report.bytes / 1024, report.refused );
check( cooker.refusals().empty(), "nothing refused: a wall is ground too" );
check( report.levels >= 3, "quadtree deeper than the root" );
qm::reader reader;
check( reader.open( archive ), "reader open" );
check( reader.table().materials.size() == 3, "grass, gravel and the wall" );
for( auto const &material : reader.table().materials ) {
std::printf( " material %s, one repeat every %.2f m\n", material.name.c_str(), material.repeat );
}
for( auto const &material : reader.table().materials ) {
if( material.name != "wall" ) { check( std::abs( material.repeat - 25.0 ) < 0.5, "the tiling was measured off the source" ); }
}
check( reader.table().levels == report.levels, "levels agree" );
for( std::uint32_t level = 0; level < reader.table().levels; ++level ) {
std::printf( " level %u error %.3f m, tile %.0f m\n", level,
reader.table().error( level ), reader.table().tilesize( level ) );
}
check( reader.table().error( reader.table().levels - 1 ) == 0.f, "the finest level is the ground as drawn" );
check( reader.table().error( 0 ) > reader.table().error( reader.table().levels - 2 ), "a coarser level is further off" );
// every tile reads back, and its vertices lie inside its own square
// per level and border point, the heights each tile put there. with walls in the mesh one
// point can carry several heights, so every height of one tile has to find a match in the
// other rather than the lists being compared one to one
std::map<std::tuple<std::uint32_t,long long,long long>, std::map<std::int64_t, std::vector<double>>> bordery;
std::size_t read = 0, trianglesread = 0;
std::map<std::uint32_t, std::size_t> perlevel;
for( auto const &[ key, tile ] : written ) {
qm::reader::tile_data data;
if( !reader.read_tile( tile, data, 0.0, 0.0, 0.0 ) ) { std::printf( "FAIL: read %s\n", qm::tile_name( tile ).c_str() ); ++failures; continue; }
++read;
trianglesread += data.indices.size() / 3;
perlevel[ tile.level ] += data.indices.size() / 3;
auto const side = reader.table().tilesize( tile.level );
auto const west = reader.table().west + tile.x * side;
auto const north = reader.table().north + tile.z * side;
for( auto const &v : data.vertices ) {
double const x = data.originx + v.x, z = data.originz + v.z, y = data.originy + v.y;
check( x >= west - 0.01 && x <= west + side + 0.01, "vertex inside its tile in x" );
check( z >= north - 0.01 && z <= north + side + 0.01, "vertex inside its tile in z" );
check( y >= data.lowest - 0.01 && y <= data.highest + 0.01, "vertex inside its height range" );
// remember heights on tile borders, to compare with the neighbour later
auto const onborder = std::abs( x - west ) < 0.01 || std::abs( x - west - side ) < 0.01
|| std::abs( z - north ) < 0.01 || std::abs( z - north - side ) < 0.01;
if( onborder ) {
auto const gx = static_cast<long long>( std::llround( x * 100.0 ) );
auto const gz = static_cast<long long>( std::llround( z * 100.0 ) );
bordery[ { tile.level, gx, gz } ][ key ].push_back( y );
}
}
}
std::printf( "read %zu tiles, %zu triangles\n", read, trianglesread );
for( auto const &[ level, count ] : perlevel ) { std::printf( " level %u: %zu triangles\n", level, count ); }
// a vertex two tiles of the same level share must decode to the same height in both
// where two tiles meet, a point on the border can carry more than one height - a wall standing
// on the ground is two surfaces at one place - and only one of them belongs to the neighbour.
// What has to agree, and is what keeps the ground from cracking, is the single-surface case
double worst = 0.0;
std::size_t shared = 0, manysurfaced = 0;
for( auto const &[ where, pertile ] : bordery ) {
if( pertile.size() < 2 ) { continue; }
++shared;
auto simple = true;
for( auto const &[ which, heights ] : pertile ) { if( heights.size() != 1 ) { simple = false; } }
if( false == simple ) { ++manysurfaced; continue; }
auto const first = pertile.begin()->second.front();
for( auto const &[ which, heights ] : pertile ) {
worst = std::max( worst, std::abs( heights.front() - first ) );
}
}
std::printf( "%zu of %zu shared border points carry more than one surface\n", manysurfaced, shared );
std::printf( "%zu border points shared by two tiles\n", shared );
std::printf( "worst height disagreement on a shared border: %.4f mm\n", worst * 1000.0 );
check( worst < 0.01, "shared border vertices agree within a centimetre" );
// the coarse levels must actually be coarser
if( perlevel.size() >= 2 ) {
auto const finest = perlevel.rbegin()->second;
auto const next = std::next( perlevel.rbegin() )->second;
check( next < finest, "a coarser level has fewer triangles" );
}
// the wall along the tile border must be there, and exactly once: 8 triangles, not 16
{
std::size_t wallpieces = 0;
std::set<std::pair<long long,long long>> wallcentres;
for( auto const &[ key, tile ] : written ) {
if( tile.level + 1 != reader.table().levels ) { continue; }
qm::reader::tile_data data;
if( !reader.read_tile( tile, data, 0.0, 0.0, 0.0 ) ) { continue; }
for( std::size_t i = 0; i + 2 < data.indices.size(); i += 3 ) {
bool wall = true;
for( int c = 0; c < 3; ++c ) {
auto const &v = data.vertices[ data.indices[ i + c ] ];
if( std::abs( data.originz + v.z + 512.0 ) > 0.01 ) { wall = false; }
}
if( wall ) {
++wallpieces;
double cx = 0.0, cy = 0.0;
for( int c = 0; c < 3; ++c ) {
cx += data.originx + data.vertices[ data.indices[ i + c ] ].x;
cy += data.originy + data.vertices[ data.indices[ i + c ] ].y;
}
wallcentres.insert( { std::llround( cx * 100.0 ), std::llround( cy * 100.0 ) } );
}
}
}
std::printf( "wall triangles on the tile border: %zu (%zu distinct)\n", wallpieces, wallcentres.size() );
check( wallpieces > 0, "the wall on the border was laid" );
check( wallcentres.size() == wallpieces, "no piece of the wall was laid twice" );
}
// a hole stays a hole: nothing may be drawn in the middle of the map at the finest level
std::size_t inhole = 0;
for( auto const &[ key, tile ] : written ) {
if( tile.level + 1 != reader.table().levels ) { continue; }
qm::reader::tile_data data;
if( !reader.read_tile( tile, data, 0.0, 0.0, 0.0 ) ) { continue; }
for( std::size_t i = 0; i + 2 < data.indices.size(); i += 3 ) {
double cx = 0.0, cz = 0.0;
for( int c = 0; c < 3; ++c ) {
cx += data.originx + data.vertices[ data.indices[ i + c ] ].x;
cz += data.originz + data.vertices[ data.indices[ i + c ] ].z;
}
cx /= 3.0; cz /= 3.0;
if( cx > -95.0 && cx < 95.0 && cz > -95.0 && cz < 95.0 ) { ++inhole; }
}
}
std::printf( "triangles inside the hole: %zu\n", inhole );
check( inhole == 0, "the hole was not filled in" );
std::filesystem::remove_all( directory, ignored );
std::printf( failures ? "\n%d checks failed\n" : "\nall checks passed\n", failures );
return failures ? 1 : 0;
}

View File

@@ -1,99 +0,0 @@
#!/usr/bin/env python3
"""Checks that terrain tiles at neighbouring levels meet without gaps.
The renderer picks each tile's level from its distance at the last rescan, and morphs every
vertex towards the next level as the camera moves (rendering/terrainclipmap.vert). Two
neighbours at levels L and L+1 share their edge exactly when, all along it, the finer tile
has finished morphing and the coarser one has not started. This walks random camera
positions, lets the camera drift up to a rescan's distance from where the levels were
chosen, and counts every edge vertex where that does not hold.
The constants mirror rendering/terrainstatus.cppm; change them together. Exits non-zero
when a gap is found.
"""
import math
import random
import sys
# finest ranges tried, in tile sides: terrain_finest_tiles is the minimum, and the screen
# can push it further (terrain_finest_range)
FINEST_TILES = (4.0, 5.7, 11.3)
RESCAN_TILES = 0.25 # terrain_rescan_tiles
MORPH_BAND = 0.2 # terrain_morph_band
LEVELS = 6
def check(tilesize, step, trials, reach, finest_tiles):
finest = finest_tiles * tilesize
def level_range(level):
return finest * 2 ** level
def level_for(distance):
if distance <= finest:
return 0
return min(int(math.log2(distance / finest)) + 1, LEVELS - 1)
def nearest(x, z, tilex, tilez):
nx = min(max(x, tilex * tilesize), (tilex + 1) * tilesize)
nz = min(max(z, tilez * tilesize), (tilez + 1) * tilesize)
return math.hypot(nx - x, nz - z)
def morph(level, distance):
if level + 1 >= LEVELS:
return 0.0
end = level_range(level) - RESCAN_TILES * tilesize
begin = end - MORPH_BAND * level_range(level)
return min(max((distance - begin) / (end - begin), 0.0), 1.0)
gaps = 0
example = ""
for _ in range(trials):
scanx, scanz = random.uniform(-5000, 5000), random.uniform(-5000, 5000)
drift, heading = random.uniform(0, RESCAN_TILES * tilesize), random.uniform(0, 2 * math.pi)
camx, camz = scanx + drift * math.cos(heading), scanz + drift * math.sin(heading)
basex, basez = math.floor(camx / tilesize), math.floor(camz / tilesize)
for tilex in range(basex - reach, basex + reach):
for tilez in range(basez - reach, basez + reach):
here = level_for(nearest(scanx, scanz, tilex, tilez))
for stepx, stepz in ((1, 0), (0, 1)):
there = level_for(nearest(scanx, scanz, tilex + stepx, tilez + stepz))
if here == there:
continue
if abs(here - there) > 1:
gaps += 1
continue
fine, coarse = min(here, there), max(here, there)
count = int(tilesize / step) >> fine
for index in range(count + 1):
along = index * tilesize / count
if stepx:
x, z = (tilex + 1) * tilesize, tilez * tilesize + along
else:
x, z = tilex * tilesize + along, (tilez + 1) * tilesize
distance = math.hypot(x - camx, z - camz)
# an odd vertex of the finer tile is not on the coarser grid and has
# to have finished its move; the coarser edge must not be moving yet
if (index % 2 == 1 and morph(fine, distance) < 1.0) or morph(coarse, distance) > 0.0:
gaps += 1
if not example:
example = (f"camera {camx:.0f},{camz:.0f}, levels {fine}/{coarse}, "
f"distance {distance:.0f} m")
return gaps, example
def main():
random.seed(1)
failed = False
for finest_tiles in FINEST_TILES:
for tilesize, step, trials, reach in ((128, 1.0, 150, 60), (256, 2.0, 150, 60), (12800, 100.0, 30, 8)):
gaps, example = check(tilesize, step, trials, reach, finest_tiles)
print(f"finest {finest_tiles} tiles, tile {tilesize} m, grid {step} m: {gaps} gaps"
+ (f" (e.g. {example})" if gaps else ""))
failed = failed or gaps > 0
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())