1 plan.js: A Motion Planning Library for Javascript Clinton Freeman

Abstract—In the past decade, there has been a large move- by the browser ahead of time into machine code in order ment in industry toward web browser based applications and to gain a significant boost in performance. Mozilla claims away from native desktop programs. My semester project was that using and asm.js can result in performance an attempt to build a JavaScript library, plan.js, for motion planning in web applications. This document recounts attempts at that is only twice as slow as native applications. Major game converting two native motion planning libraries into JavaScript engines including Unity3D and UnrealEngine are providing by using Mozilla’s Emscripten. The primary contribution of this asm.js ports of their software that can run at interactive speeds. project is an evaluation of the work required to convert each Portable Native Client (PNaCL) is Google’s alternative to library, preliminary headway on converting both projects, and a Emscripten [4]. It follows a similar scheme of converting functional demo of the PQP collision detection library converted to JavaScript. C++ source code to a LLVM-esque bytecode, but stops short of converting this into JavaScript, instead choosing to add a bytecode interpreter into the browser. This approach gives I.INTRODUCTION PNaCL a significant advantage over Emscripten in terms of N the past decade, there has been a large movement in speed, since the compiled code is able to run at native speeds. I industry toward web browser based applications and away from native desktop programs. This is due in large part because C. Examples of Converting Large Native Projects of the inherent cross-platform nature of in-browser applica- As mentioned in the introduction, ammo.js is a tions. As a part of this movement, numerous open source library for performing collision detection. This library is libraries have been developed that provide functionality that created by compiling the popular Bullet collision detection has been available in other languages such as C/C++ for quite library with Emscripten. This is one route that one can take some time. While several libraries have covered important when attempting to port a library over to JavaScript. areas such as 3D graphics (three.js) and collision detection Google has converted many popular C++ projects to run (ammo.js), there is not a widely used motion planning library under PNaCL in order to demonstrate its capability to handle to the best of my knowledge. a broad range of inputs. In particular, they provide a fully Plan.js is a JavaScript library that aims to address this gap working version of the Boost libraries including boost thread. in available software. It should contain many commonly used This is important to note since the dependency on Boost and motion planning algorithms such as RRT, RRT*, PRM, and threads is the primary roadblock to a full conversion of OMPL exact geometric methods for 2D motion planning. It should with Emscripten. be open source with the source code available to others on GitHub, and users should be able to view visualizations of the III.PROBLEM DEFINITION algorithms live in the browser. Native code is composed of machine instructions specific to a particular computer architecture (e.g. x86). This is in contrast II.RELATED WORK to interpreted code, which is composed of instructions that are A. Native Planning Libraries translated into actual machine instructions by a virtual machine The Open Motion Planning Library (OMPL) is a standard at run time. Native code has historically run significantly faster resource for motion planning algorithms for desktop C++ than interpreted code, although over time the gap has become applications [1]. It contains implementations of a wide variety much smaller. While native code (generated by languages such of algorithms, including RRT*, PRM, and others. Many years as C++) has an edge when it comes to runtime performance, of work have gone into the implementations available in this interpreted code (generated by languages such as Java) is library. generally able to run across a larger number of platforms The motion strategy library (MSL) is a C++ library built without modification. by Steve Lavalle’s group at the University of Iowa and A web worker is a JavaScript utility that is essentially a subsequently at the University of Illinois [2]. It also contains thread that has no shared state and may only communicate via some popular algorithms, but has not been updated in many message passing. This is a problem for converting native pro- years. grams to JavaScript because there is no isomorphism between normal threads and web workers. Thus, any program that uses multithreading of any sort must be modified to account for B. Native to JavaScript Conversion Tools this problem. Emscripten is a compiler developed by Mozilla that com- Emscripten is a compiler from low level virtual machine piles native C/C++ code into a subset of Javascript known (LLVM) bytecode to JavaScript. Clang is a compiler that as asm.js [3]. asm.js javascript files are able to be compiled converts C++ source code into LLVM bytecode. gcc is a 2 compiler that converts C++ source code directly into machine code. emcc is Emscripten’s replacement for gcc that uses Clang behind the scenes to compile to bytecode. Most large projects use Makefiles to automate the build process. A make file specifies how output files are generated from input files by specifying a set of rules. In order to build a project, a user will typically change directory to the root of the project and invoke the command make ¡target¿. Emscripten allows users to instead run emmake make ¡target¿ to compile javascript outputs instead of normal exectuables, archives, and shared objects.

IV. METHODS My goal with this project was to build a motion planning library that is actually useful to web programmers. There are (broadly) two ways to reach this goal. The first is to hand-write JavaScript implementations of the desired algorithms. While this method is an attractive way to cement my understanding of each algorithm I successfully implement, it is very work intensive and will take a great deal of time and effort to produce something that may be considered useful to other programmers. Fig. 1. PQP’s “spinning” demo converted to WebGL with Emscripten. The The second is to convert an existing native library using an red line denotes the shortest distance between the blue bunny and green torus. automated tool such as Emscripten or PNaCL. This method is attractive from a productivity point of view: if successful, reasons: it does not use threads at all, it comes with a Makefile I can reuse a great deal of work in order to benefit other tailored to using gcc in a Linux environment, and its only programmers. A downside is that the problem becomes not dependence is on PQP. implementing motion planning algorithms (i.e. learning the The proximity query package (PQP) is a C++ collision subject matter of the course), but instead is an engineering detection library written by the GAMMA group at UNC- challenge of converting code from one language to another. Chapel Hill [5], [6]. The implementation and source code I chose the second method because I wanted the project is extremely simple in comparison to monolithic packages to have a real product that could be used by others at the such as Bullet. It operates on pairs of triangle meshes and end. I chose to use Emscripten over PNaCL to convert an places no restriction on mesh topology. It comes with several existing library because the results would be immediate and demo applications that show various objects and the computed the general consensus on the web is that PNaCL will not enjoy results from PQP (intersection set, shortest distance, etc). wide adoption beyond . I initially chose to Converting PQP with Emscripten was very straightforward. convert OMPL instead of MSL because OMPL is significantly The primary challenge was removing outdated OpenGL calls more active and contains newer algorithms. that are not supported by Emscripten/WebGL.

V. RESULTS REFERENCES A. Converting OMPL [1] I. A. S¸ucan, M. Moll, and L. E. Kavraki, “The Open Motion Planning OMPL depends on the Boost C++ libraries for much of Library,” IEEE Robotics & Automation Magazine, vol. 19, no. 4, pp. 72–82, December 2012, http://ompl.kavrakilab.org. its functionality. In particular, the planner classes all inherit [2] “Motion strategy library,” http://msl.cs.uiuc.edu/msl/, accessed: 2014-05- from a base class that inherits from boost thread. Removing 01. this dependence is rather problematic since it occurs at such [3] A. Zakai, “Emscripten: an llvm-to-javascript compiler,” in Proceedings of the ACM international conference companion on Object oriented an abstract level of OMPL and affects virtually all motion programming systems languages and applications companion. ACM, planning algorithms. It is possible that a programmer more 2011, pp. 301–312. intimately familiar with the inner workings of OMPL would [4] B. Yee, D. Sehr, G. Dardyk, J. B. Chen, R. Muth, T. Ormandy, S. Okasaka, N. Narula, and N. Fullagar, “Native client: A sandbox for portable, be able to remove boost thread without too much trouble, but untrusted x86 native code,” in Security and Privacy, 2009 30th IEEE it was not feasible for me to do so within the time frame of Symposium on. IEEE, 2009, pp. 79–93. this class. [5] S. Gottschalk, M. C. Lin, and D. Manocha, “Obbtree: A hierarchical structure for rapid interference detection,” in Proceedings of the 23rd annual conference on Computer graphics and interactive techniques. B. Converting MSL ACM, 1996, pp. 171–180. [6] E. Larsen, S. Gottschalk, M. C. Lin, and D. Manocha, “Fast proximity While converting OMPL is an attractive goal since it is more queries with swept sphere volumes,” Tech. Rep. widely supported than MSL, MSL is an order of magnitude easier to convert using Emscripten. This is due to three