mpetroff / pannellum

Pannellum is a lightweight, free, and open source panorama viewer for the web.

Home Page:https://pannellum.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generate.py tile size edge case

tstarling opened this issue · comments

I think generate.py is trying to choose a number of levels that would result in it stopping when the last level has one tile. But sometimes it does an extra level, downscaling again after it has already done a level with a single tile.

This little script tries to reproduce what generate.py is doing at different sizes:

import sys
import math

cubeSize = int(sys.argv[1])
tileSize = int(sys.argv[2])

levels = int(math.ceil(math.log(float(cubeSize) / tileSize, 2))) + 1
if round(cubeSize / 2**(levels - 2)) == tileSize:
    levels -= 1  # Handle edge case

size = cubeSize
for level in range(levels, 0, -1):
    tiles = int(math.ceil(float(size) / tileSize))
    print("level=%d, size=%d, tiles=%d" % (level, size, tiles))
    size = int(size / 2)
$ python3 levels.py 2050 512
level=3, size=2050, tiles=5
level=2, size=1025, tiles=3
level=1, size=512, tiles=1
$ python3 levels.py 2051 512
level=4, size=2051, tiles=5
level=3, size=1025, tiles=3
level=2, size=512, tiles=1
level=1, size=256, tiles=1
$ python3 levels.py 2052 512
level=4, size=2052, tiles=5
level=3, size=1026, tiles=3
level=2, size=513, tiles=2
level=1, size=256, tiles=1

In the edge case condition on line 200, if you used int() instead of round(), matching the rounding of the size in the tiling loop, then I think it would work correctly. I can give you that as a pull request if you prefer.

I haven't actually confirmed this by running generate.py. I'm reading the code because I'm reimplementing generate.py in C++.

Thanks! I confirmed that your suggested fix did indeed work with generate.py.