Commit 227e74e3 authored by Laureline Nevin's avatar Laureline Nevin ⌨️
Browse files

Added export params and auto-setting from current view

parent 8d4aedb8
Loading
Loading
Loading
Loading
+4 −5
Changes for index.html: 4 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@
            <label for="button-reset-controls" v-t="controls.reset"></label>
        </span>
        <span v-show="phase === 'render'">
            <button class="btn-dark btn-icon" @click="fn.exportImage" v-icon="export"></button>
            <button id="button-export" class="btn-dark btn-icon" @click="fn.exportImage" v-icon="export"></button>
            <label for="button-export" v-t="export.button"></label>
        </span>
        <div class="description-container">
@@ -116,16 +116,15 @@

    <main id="app">
        <div class="view-controls">
            <button class="btn-dark btn-icon" :title="t('controls.fitview')" @click="zoomFit()"
            <button class="btn-dark btn-icon" :title="t('controls.fitview')" @click="fn.zoomFit()"
                    v-icon="zoom-fit"></button>
            <button class="btn-dark btn-icon" :title="t('controls.fitviewv')" @click="zoomFit('v')"
            <button class="btn-dark btn-icon" :title="t('controls.fitviewv')" @click="fn.zoomFit('v')"
                    v-icon="zoom-vfit"></button>
            <button class="btn-dark btn-icon" :title="t('controls.fitviewh')" @click="zoomFit('h')"
            <button class="btn-dark btn-icon" :title="t('controls.fitviewh')" @click="fn.zoomFit('h')"
                    v-icon="zoom-hfit"></button>
        </div>
        <canvas id="ivoire-display"></canvas>
        <progress aria-label="Downloading data" id="load-progress" class="hidden" max="100"></progress>
        <canvas id="debug-albedo" style="position: absolute; bottom: 0; left: 0;"></canvas>
    </main>
</div>

+15 −0
Changes for package-lock.json: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -8,11 +8,13 @@
      "name": "ivoire",
      "version": "0.1.0",
      "dependencies": {
        "canvas-size": "^2.0.0",
        "gl-matrix": "^3.4.3",
        "i18n-js": "^4.5.1"
      },
      "devDependencies": {
        "@modyfi/vite-plugin-yaml": "^1.1.1",
        "@types/canvas-size": "^1.2.2",
        "petite-vue": "^0.4.1",
        "prettier": "^3.3.1",
        "sass-embedded": "^1.89.1",
@@ -1007,6 +1009,13 @@
        "win32"
      ]
    },
    "node_modules/@types/canvas-size": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/@types/canvas-size/-/canvas-size-1.2.2.tgz",
      "integrity": "sha512-yuTXFWC4tHV3lt5ZtbIP9VeeMNbDYm5mPyqaQnaMuSSx2mjsfZGXMNmHTnfdsR5qZdB6dtbaV5IP2PKv79vmKg==",
      "dev": true,
      "license": "MIT"
    },
    "node_modules/@types/estree": {
      "version": "1.0.7",
      "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.7.tgz",
@@ -1154,6 +1163,12 @@
        "node": ">=6"
      }
    },
    "node_modules/canvas-size": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/canvas-size/-/canvas-size-2.0.0.tgz",
      "integrity": "sha512-D7yvTTPX5ZvmocInOaRXyAsLtvjy7nctyfA+SNOlTG0KPjsmhw/eoJJkx8risaY/VSUW04vd8UPN7j3Ge634OQ==",
      "license": "MIT"
    },
    "node_modules/chalk": {
      "version": "4.1.2",
      "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
+2 −0
Changes for package.json: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@
  },
  "devDependencies": {
    "@modyfi/vite-plugin-yaml": "^1.1.1",
    "@types/canvas-size": "^1.2.2",
    "petite-vue": "^0.4.1",
    "prettier": "^3.3.1",
    "sass-embedded": "^1.89.1",
@@ -21,6 +22,7 @@
    "yaml": "^2.8.0"
  },
  "dependencies": {
    "canvas-size": "^2.0.0",
    "gl-matrix": "^3.4.3",
    "i18n-js": "^4.5.1"
  }
+88 −6
Changes for src/app.ts: 88 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -16,12 +16,15 @@ import { Viewport } from './viewport.ts';
import { reactive } from 'petite-vue';
import { I18n } from 'i18n-js';
import { vec2 } from 'gl-matrix';
import canvasSize from 'canvas-size';

interface HistoryState {
    dataset: string | null;
}

export const RUN_PHASES = ['initial', 'load-datasets', 'load-program', 'render', 'create-inmemory'] as const;
export type ExportParams = typeof App.DEFAULT_EXPORT_PARAMS;

export const RUN_PHASES = ['initial', 'load-datasets', 'load-program', 'render', 'create-inmemory', 'export'] as const;
export type RunPhase = (typeof RUN_PHASES)[number];

type LocString = string | Record<string, string>;
@@ -34,6 +37,7 @@ export class App {
    public readonly i18n: I18n;
    public readonly rx: typeof this.DEFAULT_RX_STATE;
    private renderRequested = false;
    private maxCanvasSize: [number, number] | undefined = undefined;
    public readonly gls: {
        readonly relight: WebGLCanvasRenderingContext;
        readonly lightball: WebGLCanvasRenderingContext;
@@ -150,6 +154,7 @@ export class App {
    private get DEFAULT_RX_STATE() {
        // Give access to those methods
        const fn = Object.fromEntries(
            (
                [
                    this.getLocString,
                    this.setLocale,
@@ -161,14 +166,17 @@ export class App {
                    this.submitCreateMemoryDataset,
                    this.cancelCreateMemoryDataset,
                    this.onCreateMemoryFileInput,
                    this.setExportMode,
                    this.exportImage,
                    this.onExportDialogClose,
                    this.downloadExportImage,
            ].map((m) => [m.name, m.bind(this)] as const),
                ] as Array<(..._: any[]) => any>
            )
                .map((m) => [m.name, m.bind(this)] as const)
                .concat([['zoomFit', this.state.viewport.zoomFit.bind(this.state.viewport)]]),
        );
        return {
        const rx = {
            fn,
            zoomFit: this.state.viewport.zoomFit.bind(this.state.viewport),
            phase: 'initial' as RunPhase,
            datasets: {
                server: new Array<{ id: string; name: LocString }>(),
@@ -183,6 +191,17 @@ export class App {
            locales: App.LANGS.map((id) => ({ id, name: this.i18n.translations[id].lang as string })),
            description: null as LocString | null,
            output: null as string | null,
            exportParams: App.DEFAULT_EXPORT_PARAMS,
            raw: () => rx, // Function to un-proxify the rx object
        };
        return rx as Omit<typeof rx, 'raw'> & { raw: () => typeof rx };
    }

    static get DEFAULT_EXPORT_PARAMS() {
        return {
            size: [0, 0] as [number, number],
            offset: [0, 0] as [number, number],
            zoom: 1,
        };
    }

@@ -244,15 +263,20 @@ export class App {
            this.rx.phase = 'load-program';
            const abort = (this.source = new AbortController()).signal;
            this.rx.datasetsDisabled = true;
            DownloadManager.withProgressBar(() =>
            let sourcePromise = DownloadManager.withProgressBar(() =>
                ds.getTextureSource(this.gls.relight, () => this.requestRender(), abort),
            )
            );
            if (this.maxCanvasSize === undefined) {
                sourcePromise = Promise.all([sourcePromise, this.calculateMaxRenderSize()]).then(([s]) => s);
            }
            sourcePromise
                .then(async (source) => {
                    if (abort.aborted) {
                        source.unload();
                        return;
                    }
                    this.source = source;
                    this.rx.exportParams = App.DEFAULT_EXPORT_PARAMS;
                    return this.updateRenderArea(true);
                })
                .catch((e) => catchError(false)(e))
@@ -587,6 +611,64 @@ export class App {
        }
    }

    private async calculateMaxRenderSize(): Promise<void> {
        if (this.maxCanvasSize !== undefined) return;

        let canvas = [16384, 16384];
        try {
            ({ width: canvas[0], height: canvas[1] } = await canvasSize.maxArea({ usePromise: true }));
        } catch (e) {
            console.error('Error calculating max canvas size', e);
        }

        const webgl = this.gls.relight.getParameter(this.gls.relight.MAX_TEXTURE_SIZE) as number;
        this.maxCanvasSize = [Math.min(canvas[0], webgl), Math.min(canvas[1], webgl)];
        console.debug('Calculated max render size', this.maxCanvasSize);
    }

    private setExportMode() {
        if (this.rx.phase !== 'render' || !this.maxCanvasSize) return;
        const source = this.source;
        if (!source) return;
        this.rx.phase = 'export';
        if (this.rx.exportParams.size[0] === 0 || this.rx.exportParams.size[1] === 0) {
            this.setExportParams();
        }
    }

    private setExportParams(from?: 'view' | 'full' | 'auto') {
        const source = this.source;
        if (!source) return;
        if (!from || from === 'auto')
            from =
                this.maxCanvasSize &&
                (source.totalSize[0] > this.maxCanvasSize[0] || source.totalSize[1] > this.maxCanvasSize[1])
                    ? 'view'
                    : 'full';
        if (from === 'view') {
            const tr = this.state.viewport.clipToTexture([1, 1]);
            const bl = this.state.viewport.clipToTexture([-1, -1]);
            let canvas = new Rect(0, 0, ...source.totalSize),
                area = Rect.fromCorners(vec2.floor(tr, tr), vec2.ceil(bl, bl));
            area = area.intersection(canvas)!;
            let zoom = Math.pow(
                2,
                Math.max(-source.zoomDepth, Math.min(0, Math.ceil(Math.log2(this.state.viewport.zoom)))),
            );
            this.rx.exportParams = {
                size: [area.w, area.h],
                offset: [area.x, area.y],
                zoom,
            };
        } else if (from === 'full') {
            this.rx.exportParams = {
                size: [...source.totalSize],
                offset: [0, 0],
                zoom: 1,
            };
        } else throw new Error(`Unknown argument from = ${from}`);
    }

    private exportImage() {
        const dataset = this.currentDataset?.data;
        if (!dataset) return;
+6 −8
Changes for src/tiling.ts: 6 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -7,13 +7,10 @@ type TexturePair = DataPair<WebGLSizedTexture>;

export interface TextureSource {
    updateTiles(dezoom: number, area: Rect, now: boolean): Promise<void>;

    get dataset(): Dataset;

    get visibleTiles(): TextureTile[];

    get totalSize(): [number, number];

    get zoomDepth(): number;
    unload(): void;
}

@@ -91,10 +88,10 @@ export class Rect {
    }

    intersection(other: Rect): Rect | null {
        const xmin = Math.min(this.xmin, other.xmin),
            ymin = Math.min(this.ymin, other.ymin),
            xmax = Math.max(this.xmax, other.xmax),
            ymax = Math.max(this.ymax, other.ymax);
        const xmin = Math.max(this.xmin, other.xmin),
            ymin = Math.max(this.ymin, other.ymin),
            xmax = Math.min(this.xmax, other.xmax),
            ymax = Math.min(this.ymax, other.ymax);
        if (xmax > xmin && ymax > ymin) {
            return new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
        } else {
@@ -537,6 +534,7 @@ export class TileGrid implements TextureSource {
    private gl: WebGLRenderingContext;
    readonly visibleTiles: TextureTile[];
    readonly totalSize: [number, number];
    readonly zoomDepth = 0;

    public static async makeGrid(
        dataset: Dataset,
Loading