<commit_before>/*global define*/
define([
        '../Core/DeveloperError',
        '../Core/Cartesian3',
        '../Core/Ellipsoid',
        '../Core/Transforms',
        '../Scene/SceneMode'
       ], function(
         DeveloperError,
         Cartesian3,
         Ellipsoid,
         Transforms,
         SceneMode) {
    "use strict";

    /**
     * @alias DynamicObject
     * @constructor
     */
    var DynamicObjectView = function(dynamicObject, scene, ellipsoid) {
        this.dynamicObject = dynamicObject;
        this.scene = scene;
        this.ellipsoid = ellipsoid;

        this._camera = undefined;
        this._lastScene = undefined;
        this._lastDynamicObject = undefined;

        this._lastController = undefined;
        this._controller2d = undefined;
        this._controller3d = undefined;
        this._controllerColumbusView = undefined;

        this._lastCartesian = undefined;
        this._lastCartographic = undefined;
        this._lastDistance = undefined;
        this._lastOffset = undefined;
    };

    /**
     *
     * @param time
     */
    DynamicObjectView.prototype.update = function(time) {
        if (typeof time === 'undefined') {
            throw new DeveloperError('time is required.');
        }

        var scene = this.scene;
        if (typeof scene === 'undefined') {
            throw new DeveloperError('DynamicObjectView.scene is required.');
        }

        var dynamicObject = this.dynamicObject;
        if (typeof dynamicObject === 'undefined') {
            throw new DeveloperError('DynamicObjectView.dynamicObject is required.');
        }

        var ellipsoid = this.ellipsoid;
        if (typeof ellipsoid === 'undefined') {
            throw new DeveloperError('DynamicObjectView.ellipsoid is required.');
        }

        var positionProperty = this.dynamicObject.position;
        if (typeof positionProperty === 'undefined') {
            throw new DeveloperError('dynamicObject.position is required.');
        }

        var offset = new Cartesian3(3000, -3000, 3000);
        var objectChanged = dynamicObject !== this._lastDynamicObject;
        if (objectChanged) {
            this._lastDynamicObject = dynamicObject;

            var viewFromProperty = this.dynamicObject.viewFrom;
            if (typeof viewFromProperty !== 'undefined') {
                viewFromProperty.getValue(time, offset);
            }
        }

        var sceneChanged = scene !== this._lastScene;
        if (sceneChanged) {
            this._controller2d = undefined;
            this._controller3d = undefined;
            this._controllerColumbusView = undefined;
            this._lastScene = scene;
            this._camera = this.scene.getCamera();
        }

        var camera = this._camera;

        var controller;
        var controllers;
        var controllerChanged;

        switch (scene.mode) {
        case SceneMode.SCENE2D:
            controller = this._controller2d;
            controllerChanged = typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController;

            if (controllerChanged) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller2d = controller = controllers.add2D(scene.scene2D.projection);
                controller.zoomOnly = true;
            }

            if (objectChanged) {
                this._lastDistance = offset.magnitude();
            } else if (this._lastDistance === 'undefined') {
                this._lastDistance = camera.position.z;
            }

            var cartographic = this._lastCartographic = positionProperty.getValueCartographic(time, this._lastCartographic);
            if (typeof cartographic !== 'undefined') {
                cartographic.height = this._lastDistance;
                if (objectChanged || controllerChanged) {
                    camera.frustum.near = this._lastDistance;
                    controller.setCameraPosition(cartographic);
                } else {
                    camera.position = scene.scene2D.projection.project(cartographic);
                }
            }
            break;
        case SceneMode.SCENE3D:
            controller = this._controller3d;
            controllerChanged = typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController;

            if (controllerChanged) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller3d = controller = controllers.addSpindle();
                controller.constrainedAxis = Cartesian3.UNIT_Z;
            }

            var cartesian = this._lastCartesian = positionProperty.getValueCartesian(time, this._lastCartesian);
            if (typeof cartesian !== 'undefined') {
                if (objectChanged || controllerChanged) {
                    //If looking straight down, move the camera slightly south the avoid gimbal lock.
                    if (Cartesian3.equals(offset.normalize(), Cartesian3.UNIT_Z)) {
                        offset.y -= 0.01;
                    }

                    var viewDistance = offset.magnitude();
                    if (viewDistance < camera.frustum.near) {
                        camera.frustum.near = viewDistance - 1;
                    }
                    camera.lookAt(offset, Cartesian3.ZERO.clone(), Cartesian3.UNIT_Z.clone());
                }

                var transform = Transforms.eastNorthUpToFixedFrame(cartesian, ellipsoid);
                controller.setReferenceFrame(transform, Ellipsoid.UNIT_SPHERE);
            }

            this._lastDistance = camera.position.magnitude();
            break;
        case SceneMode.COLUMBUS_VIEW:
            controller = this._controllerColumbusView;
            if (objectChanged || typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller3d = controller = controllers.addColumbusView();
            }
            break;
        }
    };
<commit_msg>Maintaining view state across scene modes now works nicely for 2D and 3D.<commit_after>/*global define*/
define([
        '../Core/DeveloperError',
        '../Core/Cartesian3',
        '../Core/Ellipsoid',
        '../Core/Transforms',
        '../Scene/SceneMode'
       ], function(
         DeveloperError,
         Cartesian3,
         Ellipsoid,
         Transforms,
         SceneMode) {
    "use strict";

    /**
     * @alias DynamicObject
     * @constructor
     */
    var DynamicObjectView = function(dynamicObject, scene, ellipsoid) {
        this.dynamicObject = dynamicObject;
        this.scene = scene;
        this.ellipsoid = ellipsoid;

        this._camera = undefined;
        this._lastScene = undefined;
        this._lastDynamicObject = undefined;

        this._lastController = undefined;
        this._controller2d = undefined;
        this._controller3d = undefined;
        this._controllerColumbusView = undefined;

        this._lastCartesian = undefined;
        this._lastCartographic = undefined;
        this._lastDistance = undefined;
        this._lastOffset = undefined;
    };

    /**
     *
     * @param time
     */
    DynamicObjectView.prototype.update = function(time) {
        if (typeof time === 'undefined') {
            throw new DeveloperError('time is required.');
        }

        var scene = this.scene;
        if (typeof scene === 'undefined') {
            throw new DeveloperError('DynamicObjectView.scene is required.');
        }

        var dynamicObject = this.dynamicObject;
        if (typeof dynamicObject === 'undefined') {
            throw new DeveloperError('DynamicObjectView.dynamicObject is required.');
        }

        var ellipsoid = this.ellipsoid;
        if (typeof ellipsoid === 'undefined') {
            throw new DeveloperError('DynamicObjectView.ellipsoid is required.');
        }

        var positionProperty = this.dynamicObject.position;
        if (typeof positionProperty === 'undefined') {
            throw new DeveloperError('dynamicObject.position is required.');
        }

        var offset = new Cartesian3(3000, -3000, 3000);
        var objectChanged = dynamicObject !== this._lastDynamicObject;
        if (objectChanged) {
            this._lastDynamicObject = dynamicObject;

            var viewFromProperty = this.dynamicObject.viewFrom;
            if (typeof viewFromProperty !== 'undefined') {
                viewFromProperty.getValue(time, offset);
            }
        }

        var sceneChanged = scene !== this._lastScene;
        if (sceneChanged) {
            this._controller2d = undefined;
            this._controller3d = undefined;
            this._controllerColumbusView = undefined;
            this._lastScene = scene;
            this._camera = this.scene.getCamera();
        }

        var camera = this._camera;

        var controller;
        var controllers;
        var viewDistance;
        var controllerChanged;

        switch (scene.mode) {
        case SceneMode.SCENE2D:
            controller = this._controller2d;
            controllerChanged = typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController;

            if (controllerChanged) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller2d = controller = controllers.add2D(scene.scene2D.projection);
                controller.zoomOnly = true;
                viewDistance = this._lastOffset.magnitude();
            } else if (objectChanged) {
                viewDistance = offset.magnitude();
            } else {
                viewDistance = camera.position.z;
            }

            var cartographic = this._lastCartographic = positionProperty.getValueCartographic(time, this._lastCartographic);
            if (typeof cartographic !== 'undefined') {
                cartographic.height = viewDistance;
                if (objectChanged || controllerChanged) {
                    camera.frustum.near = viewDistance;
                    controller.setCameraPosition(cartographic);
                } else {
                    camera.position = scene.scene2D.projection.project(cartographic);
                }
            }
            this._lastDistance = camera.frustum.right - camera.frustum.left;
            break;
        case SceneMode.SCENE3D:
            controller = this._controller3d;
            controllerChanged = typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController;

            if (controllerChanged) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller3d = controller = controllers.addSpindle();
                controller.constrainedAxis = Cartesian3.UNIT_Z;
            }

            var cartesian = this._lastCartesian = positionProperty.getValueCartesian(time, this._lastCartesian);
            if (typeof cartesian !== 'undefined') {
                if (objectChanged) {
                    //If looking straight down, move the camera slightly south the avoid gimbal lock.
                    if (Cartesian3.equals(offset.normalize(), Cartesian3.UNIT_Z)) {
                        offset.y -= 0.01;
                    }
                    camera.lookAt(offset, Cartesian3.ZERO.clone(), Cartesian3.UNIT_Z.clone());

                    //TODO We won't need this once the multi-frustum code is in place.
                    viewDistance = offset.magnitude();
                    if (viewDistance < camera.frustum.near) {
                        camera.frustum.near = viewDistance - 1;
                    }
                } else if (controllerChanged) {
                    this._lastOffset.normalize(offset).multiplyByScalar(this._lastDistance, offset);
                    camera.lookAt(offset, Cartesian3.ZERO.clone(), Cartesian3.UNIT_Z.clone());

                    //TODO We won't need this once the multi-frustum code is in place.
                    viewDistance = offset.magnitude();
                    if (viewDistance < camera.frustum.near) {
                        camera.frustum.near = viewDistance - 1;
                    }
                }

                var transform = Transforms.eastNorthUpToFixedFrame(cartesian, ellipsoid);
                controller.setReferenceFrame(transform, Ellipsoid.UNIT_SPHERE);
                this._lastOffset = camera.position;
                this._lastDistance = camera.position.magnitude();
            }
            break;
        case SceneMode.COLUMBUS_VIEW:
            controller = this._controllerColumbusView;
            if (objectChanged || typeof controller === 'undefined' || controller.isDestroyed() || controller !== this._lastController) {
                controllers = camera.getControllers();
                controllers.removeAll();
                this._lastController = this._controller3d = controller = controllers.addColumbusView();
            }
            break;
        }
    };

{'fraction_non_alphanumeric': 0.0962227530606151, 'fraction_numerical': 0.006046581068975813, 'mean_word_length': 1.8504255319148937, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1069701', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3150, 'n_words': 675}