Skip to content

Commit

Permalink
Default to random start position and allow URI decoding (#293)
Browse files Browse the repository at this point in the history
* Default to random start position and allow URI decoding

* Remove unnecessary check
  • Loading branch information
apsdehal authored Oct 11, 2019
1 parent 59d984f commit 76e0496
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/esp/bindings_js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import "./bindings.css";
import {
checkWebAssemblySupport,
checkWebgl2Support,
getInfoSemanticUrl
getInfoSemanticUrl,
buildConfigFromURLParameters
} from "./modules/utils";

function preload(url) {
Expand All @@ -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("."));
Expand Down Expand Up @@ -62,6 +58,7 @@ Module.onRuntimeInitialized = () => {
if (!demo) {
demo = new WebDemo();
}

demo.display();
};

Expand Down
17 changes: 13 additions & 4 deletions src/esp/bindings_js/modules/simenv_embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/esp/bindings_js/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 7 additions & 1 deletion src/esp/bindings_js/modules/web_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion src/esp/bindings_js/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
});

0 comments on commit 76e0496

Please sign in to comment.