From 76e0496dfd7dade49078d05e5f0da31af404768d Mon Sep 17 00:00:00 2001 From: Amanpreet Singh Date: Fri, 11 Oct 2019 11:05:56 -0700 Subject: [PATCH] Default to random start position and allow URI decoding (#293) * Default to random start position and allow URI decoding * Remove unnecessary check --- src/esp/bindings_js/index.js | 11 ++++------- src/esp/bindings_js/modules/simenv_embind.js | 17 +++++++++++++---- src/esp/bindings_js/modules/utils.js | 10 ++++++++++ src/esp/bindings_js/modules/web_demo.js | 8 +++++++- src/esp/bindings_js/tests/utils.test.js | 17 ++++++++++++++++- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/esp/bindings_js/index.js b/src/esp/bindings_js/index.js index 33e2257624..cf9a3eca39 100644 --- a/src/esp/bindings_js/index.js +++ b/src/esp/bindings_js/index.js @@ -12,7 +12,8 @@ import "./bindings.css"; import { checkWebAssemblySupport, checkWebgl2Support, - getInfoSemanticUrl + getInfoSemanticUrl, + buildConfigFromURLParameters } from "./modules/utils"; function preload(url) { @@ -28,12 +29,7 @@ function preload(url) { Module.preRun.push(() => { let config = {}; config.scene = defaultScene; - for (let arg of window.location.search.substr(1).split("&")) { - let [key, value] = arg.split("="); - if (key && value) { - config[key] = value; - } - } + buildConfigFromURLParameters(config); const scene = config.scene; Module.scene = preload(scene); const fileNoExtension = scene.substr(0, scene.lastIndexOf(".")); @@ -62,6 +58,7 @@ Module.onRuntimeInitialized = () => { if (!demo) { demo = new WebDemo(); } + demo.display(); }; diff --git a/src/esp/bindings_js/modules/simenv_embind.js b/src/esp/bindings_js/modules/simenv_embind.js index 72703c6c05..1f48e58a90 100644 --- a/src/esp/bindings_js/modules/simenv_embind.js +++ b/src/esp/bindings_js/modules/simenv_embind.js @@ -19,10 +19,14 @@ class SimEnv { * @param {Object} episode - episode to run * @param {number} agentId - default agent id */ - constructor(config, episode, agentId) { + constructor(config, episode = {}, agentId = 0) { this.sim = new Module.Simulator(config); this.episode = episode; - this.initialAgentState = this.createAgentState(episode.startState); + this.initialAgentState = null; + + if (Object.keys(episode).length > 0) { + this.initialAgentState = this.createAgentState(episode.startState); + } this.selectedAgentId = agentId; } @@ -31,8 +35,10 @@ class SimEnv { */ reset() { this.sim.reset(); - const agent = this.sim.getAgent(this.selectedAgentId); - agent.setState(this.initialAgentState, true); + if (this.initialAgentState !== null) { + const agent = this.sim.getAgent(this.selectedAgentId); + agent.setState(this.initialAgentState, true); + } } changeAgent(agentId) { @@ -112,6 +118,9 @@ class SimEnv { * @returns {Array} [magnitude, clockwise-angle (in radians)] */ distanceToGoal() { + if (Object.keys(this.episode).length === 0) { + return [0, 0]; + } let dst = this.episode.goal.position; let state = this.getAgentState(); let src = state.position; diff --git a/src/esp/bindings_js/modules/utils.js b/src/esp/bindings_js/modules/utils.js index 252406b0be..2eb258cf45 100644 --- a/src/esp/bindings_js/modules/utils.js +++ b/src/esp/bindings_js/modules/utils.js @@ -91,3 +91,13 @@ export function getInfoSemanticUrl(mainUrl) { } return splits.join("/") + infoSemanticPath; } + +export function buildConfigFromURLParameters(config = {}) { + for (let arg of window.location.search.substr(1).split("&")) { + let [key, value] = arg.split("="); + if (key && value) { + config[key] = decodeURIComponent(value); + } + } + return config; +} diff --git a/src/esp/bindings_js/modules/web_demo.js b/src/esp/bindings_js/modules/web_demo.js index de37f580ca..ab0dfbb06d 100644 --- a/src/esp/bindings_js/modules/web_demo.js +++ b/src/esp/bindings_js/modules/web_demo.js @@ -11,6 +11,7 @@ import { import SimEnv from "./simenv_embind"; import TopDownMap from "./topdown"; import NavigateTask from "./navigate"; +import { buildConfigFromURLParameters } from "./utils"; class WebDemo { currentResolution = defaultResolution; @@ -89,7 +90,12 @@ class WebDemo { return agentConfig; } - display(agentConfig = defaultAgentConfig, episode = defaultEpisode) { + display(agentConfig = defaultAgentConfig, episode = {}) { + const config = buildConfigFromURLParameters(); + if (config.useDefaultEpisode) { + episode = defaultEpisode; + } + this.initializeModules(agentConfig, episode); this.task.init(); diff --git a/src/esp/bindings_js/tests/utils.test.js b/src/esp/bindings_js/tests/utils.test.js index 8a847fede0..d0d6bbacd4 100644 --- a/src/esp/bindings_js/tests/utils.test.js +++ b/src/esp/bindings_js/tests/utils.test.js @@ -1,4 +1,8 @@ -import { throttle, getInfoSemanticUrl } from "../modules/utils"; +import { + throttle, + getInfoSemanticUrl, + buildConfigFromURLParameters +} from "../modules/utils"; test("throttle should work properly", () => { let count = 0; @@ -43,3 +47,14 @@ test("info semantic.json should have correct path", () => { expect(getInfoSemanticUrl(item)).toEqual(expectedInfoPaths[index]); }); }); + +test("configuration should be built from url parameters", () => { + delete window.location; + window.location = {}; + window.location.search = "?a=b&c&d=true&e=1"; + const config = buildConfigFromURLParameters(); + expect(config.a).toEqual("b"); + expect(config.c).toBeUndefined(); + expect(config.d).toEqual("true"); + expect(config.e).toEqual("1"); +});