Skip to content

Commit

Permalink
Bundle using browserify
Browse files Browse the repository at this point in the history
  • Loading branch information
furstenheim committed Apr 23, 2017
1 parent 0646ff9 commit 67f8ee9
Show file tree
Hide file tree
Showing 9 changed files with 18,791 additions and 2,416 deletions.
18,679 changes: 18,679 additions & 0 deletions dist/app.js

Large diffs are not rendered by default.

1,584 changes: 0 additions & 1,584 deletions dist/automatic-label-worker.js

This file was deleted.

708 changes: 0 additions & 708 deletions dist/automatic-scatter-labelling.js

This file was deleted.

37 changes: 15 additions & 22 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,33 @@ const literalify = require('literalify')
const buffer = require('vinyl-buffer')
const browserSync = require('browser-sync').create()
var transform = require('vinyl-transform')
const aliasify = require('aliasify')
const through = require('through2')
const notify = require('gulp-notify')

gulp.task('start-server', ['watch'], function () {
browserSync.init({
server: {
baseDir: './'
}
},
startPath: '/example'
})
})

gulp.task('build:main', function () {
var bundler = watchify(browserify({
externals: ['lodash'],
standalone: 'automaticScatterLabelling',
entries: [
'./index.js'
],
debug: true,
shim: {
lodash: {
exports: 'global:_'
}
}
debug: true
}))

function transformer (file) {
return through(function (buf, enc, next) {
this.push(buf.toString('utf8').replace('require(\'lodash\')', 'global._'))
next()
})
}
function gulpBundle () {
bundler.bundle()
.on('error', function (err) {
return notify().write(err)
})
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulp.dest('dist/'))
Expand All @@ -53,30 +49,27 @@ gulp.task('build:main', function () {
})
}
bundler
.transform('exposify', {expose: {lodash: '_'}})
.transform(babelify, {plugins: ['meaningful-logs']})
.on('update', function (a) {
console.log('update')
gulpBundle()
})
.on('error', function (e) {
console.error(e)
return notify().write(err)
})
.on('log', function (log) {
console.log(log)
})
return gulpBundle()
})/*
gulp.task('server-reload', function () {
browserSync.reload()
})*/
})

gulp.task('test', function () {
gulp
.src('test/*-test.js')
.pipe(mocha().on('error', console.error))
})
gulp.task('watch', ['build:main'], function () {
gulp.watch(['example/*.*', 'src/*.*', 'index.js']/*, ['server-reload']*/)
//gulp.watch(['example/*.*', 'src/*.*', 'index.js']/*, ['server-reload']*/)
//gulp.watch(['test/*.*'], ['test'])
})
gulp.task('default', [/*'start-server',*/'start-server'])
gulp.task('default', [/*'start-server',*/'start-server'])
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const lodash = require('lodash')
const mainAlgorithmLoader = require('./src/main-algorithm-loader')
module.exports = mainAlgorithmLoader.mainAlgorithm
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,24 @@
"author": "Gabriel Fürstenheim <furstenheim@gmail.com>",
"license": "ISC",
"devDependencies": {
"aliasify": "^2.1.0",
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-plugin-meaningful-logs": "^1.0.2",
"babel-plugin-transform-async-to-generator": "^6.16.0",
"babelify": "^7.3.0",
"browser-sync": "^2.16.0",
"browserify": "^14.3.0",
"browserify-shim": "^3.8.14",
"chai": "^3.5.0",
"exposify": "^0.5.0",
"gulp": "3.x.x",
"gulp-spawn-mocha": "^3.1.0",
"gulp-webpack": "^1.5.0",
"imports-loader": "^0.6.5",
"istanbul": "^0.4.5",
"karma": "^1.3.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.3.0",
"karma-webpack": "^1.8.0",
"literalify": "^0.4.0",
"mocha": "^3.0.2",
"sinon": "2.0.0-pre.4",
"through2": "^2.0.3",
Expand All @@ -46,8 +43,7 @@
"watchify": "^3.9.0",
"webpack": "^2.2.0-rc.4",
"webpack-dev-server": "^1.16.2",
"webworkify": "^1.4.0",
"worker-loader": "git://github.com/furstenheim/worker-loader.git#489ad4ae39ca46469b694330e5500f4badff9591"
"webworkify": "^1.4.0"
},
"dependencies": {
"iterative-greedy": "0.0.4"
Expand Down
3 changes: 1 addition & 2 deletions src/main-algorithm-loader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = {mainAlgorithm}
const work = require('webworkify')
var MainAlgorithmWorker = work(require('./main-algorithm.js'))
const algorithm = work(require('./main-algorithm.js'))
const webgl = require('./webgl/webgl')
const algorithm = new MainAlgorithmWorker
const webGLFunctions = {} // Here we store the reference to the functions
const promiseResolutions = {}
function mainAlgorithm (extendedPoints, params = {}) {
Expand Down
185 changes: 92 additions & 93 deletions src/main-algorithm.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//module.exports = {mainAlgorithm}

const extendedPointMethods = require('./extended-point-methods')
const rayIntersection = require('./ray-intersection').rayIntersection
//const _ = require('lodash')
const iterativeGreedy = require('iterative-greedy')
const webgl = require('./webgl/webgl')

let NUMBER_OF_RAYS
// In this object we register the callbacks for computations in the main thread (gpu case)
const callbacks = {}
// Called as webworker
module.exports = function (self) {
self.importScripts('https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js')

importScripts('https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js')
const extendedPointMethods = require('./extended-point-methods')
const _ = require('lodash')
const rayIntersection = require('./ray-intersection').rayIntersection
const iterativeGreedy = require('iterative-greedy')
const webgl = require('./webgl/webgl')
if (typeof postMessage !== 'undefined') {
self.onmessage = function (event) {
var data = event.data
Expand All @@ -27,104 +27,103 @@ module.exports = function (self) {
}
}
}

}

function returnGPUComputation (event) {
const uuid = event.data.uuid
if (_.isFunction(callbacks[uuid])) {
callbacks[uuid](event)
delete callbacks[uuid]
} else {
console.error('Callback should be a function, uuid:', uuid)
function returnGPUComputation (event) {
const uuid = event.data.uuid
if (_.isFunction(callbacks[uuid])) {
callbacks[uuid](event)
delete callbacks[uuid]
} else {
console.error('Callback should be a function, uuid:', uuid)
}
}
}

function launchMainAlgorithmFromEvent (event) {
const data = event.data
const extendedPoints = data.extendedPoints
const params = data.params
const processUUID = data.processUUID // we use this in case the algorihm is required several times
if (params.isWebgl) {
params.intersectionData = data.intersectionData
params.rectangleData = data.rectangleData
params.computeIntersection = _.partialRight(computeIntersectionWithGPU, processUUID)
function launchMainAlgorithmFromEvent (event) {
const data = event.data
const extendedPoints = data.extendedPoints
const params = data.params
const processUUID = data.processUUID // we use this in case the algorihm is required several times
if (params.isWebgl) {
params.intersectionData = data.intersectionData
params.rectangleData = data.rectangleData
params.computeIntersection = _.partialRight(computeIntersectionWithGPU, processUUID)
}
mainAlgorithm(extendedPoints, params)
.then(function (result) {
postMessage({
type: 'end',
processUUID,
result
})
})
}
mainAlgorithm(extendedPoints, params)
.then(function (result) {

function computeIntersectionWithGPU (rectangleData, pix, piy, intersectionData, processUUID) {
var uuid = parseInt(Math.random() * 1000000).toString() // no need for anything fancy
return new Promise(function (resolve, reject) {
postMessage({
type: 'end',
processUUID,
result
})
type: 'computeIntersection',
rectangleData,
pix,
piy,
intersectionData,
uuid,
processUUID
}, [rectangleData.buffer, intersectionData.buffer])
callbacks[uuid] = function (event) {
resolve({intersectionData: event.data.intersectionData, rectangleData: event.data.rectangleData})
}
})
}
}

function computeIntersectionWithGPU (rectangleData, pix, piy, intersectionData, processUUID) {
var uuid = parseInt(Math.random() * 1000000).toString() // no need for anything fancy
return new Promise(function (resolve, reject) {
postMessage({
type: 'computeIntersection',
rectangleData,
pix,
piy,
intersectionData,
uuid,
processUUID
}, [rectangleData.buffer, intersectionData.buffer])
callbacks[uuid] = function (event) {
resolve({intersectionData: event.data.intersectionData, rectangleData: event.data.rectangleData})
function mainAlgorithm (extendedPoints, params = {}) {
NUMBER_OF_RAYS = _.isNumber(params.NUMBER_OF_RAYS) ? params.NUMBER_OF_RAYS : 3
const MAX_NUMBER_OF_ITERATIONS = _.isNumber(params.MAX_NUMBER_OF_ITERATIONS) ? params.MAX_NUMBER_OF_ITERATIONS : 1
const isWebgl = params.isWebgl
computeRays(extendedPoints)
var intersectionData, computeIntersection, rectangleData
if (isWebgl && !params.intersectionData) {
({intersectionData, computeIntersection, rectangleData} = webgl.setUp(extendedPoints, NUMBER_OF_RAYS))
} else if (isWebgl && params.intersectionData) {
({intersectionData, computeIntersection, rectangleData} = params)
}
})
}

function mainAlgorithm (extendedPoints, params = {}) {
NUMBER_OF_RAYS = _.isNumber(params.NUMBER_OF_RAYS) ? params.NUMBER_OF_RAYS : 3
const MAX_NUMBER_OF_ITERATIONS = _.isNumber(params.MAX_NUMBER_OF_ITERATIONS) ? params.MAX_NUMBER_OF_ITERATIONS : 1
const isWebgl = params.isWebgl
computeRays(extendedPoints)
var intersectionData, computeIntersection, rectangleData
if (isWebgl && !params.intersectionData) {
({intersectionData, computeIntersection, rectangleData} = webgl.setUp(extendedPoints, NUMBER_OF_RAYS))
} else if (isWebgl && params.intersectionData) {
({intersectionData, computeIntersection, rectangleData} = params)
extendedPointMethods.computeInitialAvailabeSpaces(extendedPoints, {radius: params.radius || 2, bbox: params.bbox})
extendedPoints.forEach(function (p) {
extendedPointMethods.resetAvailableSpace(p)
extendedPointMethods.updateAvailableSpace(p)
})
const possiblePoints = extendedPoints.filter(p => p.availableMeasure > 0)
return iterativeGreedy.solve(_.partialRight(rayIntersection, isWebgl, {intersectionData, computeIntersection, rectangleData}), possiblePoints, resetFunction, {serializeFunction, MAX_NUMBER_OF_ITERATIONS})
}
extendedPointMethods.computeInitialAvailabeSpaces(extendedPoints, {radius: params.radius || 2, bbox: params.bbox})
extendedPoints.forEach(function (p) {
extendedPointMethods.resetAvailableSpace(p)
extendedPointMethods.updateAvailableSpace(p)
})
const possiblePoints = extendedPoints.filter(p => p.availableMeasure > 0)
return iterativeGreedy.solve(_.partialRight(rayIntersection, isWebgl, {intersectionData, computeIntersection, rectangleData}), possiblePoints, resetFunction, {serializeFunction, MAX_NUMBER_OF_ITERATIONS})
}

function computeRays (extendedPoints) {
for (let i = 0; i < extendedPoints.length; i++) {
let pi = extendedPoints[i]
pi.rays = []
for (let j = 0; j < NUMBER_OF_RAYS; j++) {
pi.rays.push( {
index: i*NUMBER_OF_RAYS * NUMBER_OF_RAYS *4 + j * NUMBER_OF_RAYS * 4,
selfIndex: j,
vector : {
x: Math.sin(2 * Math.PI * j / NUMBER_OF_RAYS),
y: Math.cos(2 * Math.PI * j / NUMBER_OF_RAYS)
}
})
function computeRays (extendedPoints) {
for (let i = 0; i < extendedPoints.length; i++) {
let pi = extendedPoints[i]
pi.rays = []
for (let j = 0; j < NUMBER_OF_RAYS; j++) {
pi.rays.push( {
index: i*NUMBER_OF_RAYS * NUMBER_OF_RAYS *4 + j * NUMBER_OF_RAYS * 4,
selfIndex: j,
vector : {
x: Math.sin(2 * Math.PI * j / NUMBER_OF_RAYS),
y: Math.cos(2 * Math.PI * j / NUMBER_OF_RAYS)
}
})
}
}
}
}

// At each iteration of iterative greedy if the solution is better we serialize what we obtained
function serializeFunction (arrayOfPoints) {
// When we label a point we promote label to rectangle and we reset it at each iteration
const labeledPoints = arrayOfPoints.filter(point => !!point.rectangle)
// To serialize we need an id
return labeledPoints.map(point => { return {id: point.id, rectangle: _.clone(point.rectangle)} })
}
function serializeFunction (arrayOfPoints) {
// When we label a point we promote label to rectangle and we reset it at each iteration
const labeledPoints = arrayOfPoints.filter(point => !!point.rectangle)
// To serialize we need an id
return labeledPoints.map(point => { return {id: point.id, rectangle: _.clone(point.rectangle)} })
}

// At each iteration of iterative greedy we reset the conditions
function resetFunction (generalizedPoint) {
generalizedPoint.rectangle = null
extendedPointMethods.resetAvailableSpace(generalizedPoint)
}
function resetFunction (generalizedPoint) {
generalizedPoint.rectangle = null
extendedPointMethods.resetAvailableSpace(generalizedPoint)
}
}

2 changes: 1 addition & 1 deletion src/webgl/webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {setUp}

const mainFragment = require('./main-fragment').mainFragment
const utils = require('./utils')
const _ = require('lodash')
const _ = require('lodash');


/**
Expand Down

0 comments on commit 67f8ee9

Please sign in to comment.