/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Rendering
 */
import { ClipStyle, ColorDef, Frustum, GlobeMode, Hilite, MonochromeMode, Npc, ViewFlags, WhiteOnWhiteReversalSettings, } from "@itwin/core-common";
import { Constant, Point3d, Vector3d } from "@itwin/core-geometry";
import { FlashSettings } from "../FlashSettings";
const scratchPoint3a = new Point3d();
const scratchPoint3b = new Point3d();
const scratchPoint3c = new Point3d();
/**
 * @internal
 */
export class RenderPlanEllipsoid {
    constructor(ellipsoidCenter, ellipsoidRotation, ellipsoidRadii) {
        this.ellipsoidCenter = ellipsoidCenter;
        this.ellipsoidRotation = ellipsoidRotation;
        this.ellipsoidRadii = ellipsoidRadii;
    }
    equals(other) {
        if (this.ellipsoidCenter.isAlmostEqual(other.ellipsoidCenter))
            return false;
        if (this.ellipsoidRotation.isAlmostEqual(other.ellipsoidRotation))
            return false;
        if (this.ellipsoidRadii.isAlmostEqual(other.ellipsoidRadii))
            return false;
        return true;
    }
}
/** @internal */
export function createEmptyRenderPlan() {
    return {
        is3d: true,
        viewFlags: new ViewFlags(),
        bgColor: ColorDef.white,
        monoColor: ColorDef.white,
        monochromeMode: MonochromeMode.Scaled,
        hiliteSettings: new Hilite.Settings(),
        emphasisSettings: new Hilite.Settings(),
        flashSettings: new FlashSettings(),
        clipStyle: ClipStyle.defaults,
        frustum: new Frustum(),
        fraction: 0,
        isFadeOutActive: false,
        globalViewTransition: 0,
        isGlobeMode3D: false,
        backgroundMapOn: false,
        upVector: Vector3d.unitZ(),
        whiteOnWhiteReversal: WhiteOnWhiteReversalSettings.fromJSON(),
    };
}
/** @internal */
export function createRenderPlanFromViewport(vp) {
    const view = vp.view;
    const style = view.displayStyle;
    const is3d = view.is3d();
    const globalViewTransition = view.is3d() ? view.globalViewTransition() : 0.0;
    const isGlobeMode3D = GlobeMode.Ellipsoid === view.globeMode;
    const backgroundMapOn = view.displayStyle.viewFlags.backgroundMap;
    const frustum = vp.viewingSpace.getFrustum();
    const fraction = vp.viewingSpace.frustFraction;
    const viewFlags = style.viewFlags;
    const bgColor = view.backgroundColor;
    const monoColor = style.monochromeColor;
    const monochromeMode = style.settings.monochromeMode;
    const hiliteSettings = vp.hilite;
    const emphasisSettings = vp.emphasisSettings;
    const flashSettings = vp.flashSettings;
    const lights = vp.lightSettings;
    const isFadeOutActive = vp.isFadeOutActive;
    const clip = view.getViewClip();
    const clipStyle = view.displayStyle.settings.clipStyle;
    const hline = style.is3d() ? style.settings.hiddenLineSettings : undefined;
    const ao = style.is3d() ? style.settings.ambientOcclusionSettings : undefined;
    const analysisStyle = style.settings.analysisStyle;
    const thematic = (style.is3d() && view.displayStyle.viewFlags.thematicDisplay) ? style.settings.thematic : undefined;
    const shouldDisplayAtmosphere = (style.is3d() && GlobeMode.Ellipsoid === view.globeMode && vp.iModel.isGeoLocated && style.viewFlags.backgroundMap) ? vp.view.getDisplayStyle3d().environment.displayAtmosphere : false;
    const atmosphere = shouldDisplayAtmosphere ? vp.view.getDisplayStyle3d().environment.atmosphere : undefined;
    let upVector;
    if (GlobeMode.Ellipsoid === view.globeMode) {
        const lb = frustum.getCorner(Npc.LeftBottomRear).interpolate(0.5, frustum.getCorner(Npc.LeftBottomFront), scratchPoint3a);
        const rt = frustum.getCorner(Npc.RightTopRear).interpolate(0.5, frustum.getCorner(Npc.RightTopFront), scratchPoint3b);
        const cntr = lb.interpolate(0.5, rt, scratchPoint3c);
        upVector = view.getUpVector(cntr);
    }
    else {
        upVector = Vector3d.unitZ();
    }
    let analysisTexture;
    if (analysisStyle?.thematic)
        analysisTexture = vp.target.renderSystem.getGradientTexture(analysisStyle.thematic.gradient, vp.iModel);
    let ellipsoid;
    if (GlobeMode.Ellipsoid === view.globeMode) {
        const mapEcefToDb = view.iModel.getMapEcefToDb(0);
        ellipsoid = new RenderPlanEllipsoid(Point3d.fromJSON(mapEcefToDb.origin), mapEcefToDb.matrix, Point3d.fromJSON({ x: Constant.earthRadiusWGS84.equator, y: Constant.earthRadiusWGS84.equator, z: Constant.earthRadiusWGS84.polar }));
    }
    return {
        is3d,
        viewFlags,
        bgColor,
        monoColor,
        monochromeMode,
        hiliteSettings,
        emphasisSettings,
        flashSettings,
        clip,
        clipStyle,
        hline,
        analysisStyle,
        ao,
        thematic,
        atmosphere,
        isFadeOutActive,
        analysisTexture,
        frustum,
        fraction,
        globalViewTransition,
        isGlobeMode3D,
        backgroundMapOn,
        upVector,
        lights,
        whiteOnWhiteReversal: vp.displayStyle.settings.whiteOnWhiteReversal,
        ellipsoid,
    };
}
//# sourceMappingURL=RenderPlan.js.map