Skip to content
Snippets Groups Projects
Commit 237096a8 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

to worker

parent 8fdcd854
Branches
No related tags found
No related merge requests found
......@@ -212,60 +212,88 @@ var interface = function(div){
//
// local functions
//
// calculate_path
// calculate path
//
function calculate_path() {
var h = mod.height
var w = mod.width
var xmin = mod.xmin
var xmax = mod.xmax
var ymin = mod.ymin
var ymax = mod.ymax
var zmin = mod.zmin
var zmax = mod.zmax
var blob = new Blob(['('+calculate_path_worker.toString()+'())'])
var url = window.URL.createObjectURL(blob)
var webworker = new Worker(url)
webworker.addEventListener('message',function(evt) {
mod.triangles = evt.data.triangles
mod.path = evt.data.path
mod.label.nodeValue = 'calculate'
mod.labelspan.style.fontWeight = 'normal'
//
// clear SVG
//
var svg = document.getElementById(mod.div.id+'svg')
svg.setAttribute('viewBox',"0 0 "+(w-1)+" "+(h-1))
svg.setAttribute('viewBox',"0 0 "+(mod.width-1)+" "+(mod.height-1))
var g = document.getElementById(mod.div.id+'g')
svg.removeChild(g)
var g = document.createElementNS('http://www.w3.org/2000/svg','g')
g.setAttribute('id',mod.div.id+'g')
svg.appendChild(g)
//
// plot path
//
for (var i = 1; i < mod.path[0].length; ++i) {
var ixp = mod.path[0][i-1][0]
var iyp = mod.path[0][i-1][1]
var izp = 0.1*mod.path[0][i-1][2]
var ix = mod.path[0][i][0]
var iy = mod.path[0][i][1]
var iz = 0.1*mod.path[0][i][2]
var line = document.createElementNS('http://www.w3.org/2000/svg','line')
line.setAttribute('stroke','black')
line.setAttribute('stroke-width',1)
line.setAttribute('stroke-linecap','round')
line.setAttribute('x1',ixp)
line.setAttribute('y1',iyp-izp)
line.setAttribute('x2',ix)
line.setAttribute('y2',iy-iz)
g.appendChild(line)
}
})
webworker.postMessage({
h:mod.height,w:mod.width,
xmin:mod.xmin,xmax:mod.xmax,
ymin:mod.ymin,ymax:mod.ymax,
zmin:mod.zmin,zmax:mod.zmax,
map:mod.map})
}
//
// calculate path worker
//
function calculate_path_worker() {
self.addEventListener('message',function(evt) {
var h = evt.data.h
var w = evt.data.w
var xmin = evt.data.xmin
var xmax = evt.data.xmax
var ymin = evt.data.ymin
var ymax = evt.data.ymax
var zmin = evt.data.zmin
var zmax = evt.data.zmax
var map = evt.data.map
var path = [[]]
//
// line loop
//
var ix = 0
var iy = h-1
var dx = 1
var dy = 0
var x = xmin+(xmax-xmin)*ix/(w-1)
var y = ymin+(ymax-ymin)*iy/(h-1)
var z = mod.map[(h-1-iy)*w+ix]
var dz = 0.1*h*(zmax-z)/(ymax-ymin)
var iz = Math.floor((map[iy*w+ix]-zmax)*w/(xmax-xmin))
while (1) {
var ixp = ix
var iyp = iy
var dzp = dz
ix += dx
iy += dy
if (iy <= 0)
break;
var x = xmin+(xmax-xmin)*ix/(w-1)
var y = ymin+(ymax-ymin)*iy/(h-1)
var z = mod.map[iy*w+ix]
var dz = 0.1*h*(zmax-z)/(zmax-zmin)
var line = document.createElementNS('http://www.w3.org/2000/svg','line')
line.setAttribute('stroke','black')
line.setAttribute('stroke-width',1)
line.setAttribute('stroke-linecap','round')
line.setAttribute('x1',ixp)
line.setAttribute('y1',iyp+dzp)
line.setAttribute('x2',ix)
line.setAttribute('y2',iy+dz)
g.appendChild(line)
if (ix == (mod.width-1)) {
var iz = Math.floor((map[iy*w+ix]-zmax)*w/(xmax-xmin))
path[0].push([ix,iy,iz])
if (ix == (w-1)) {
if (dx == 1) {
dx = 0
dy = -10
......@@ -286,8 +314,12 @@ function calculate_path() {
}
}
}
mod.label.nodeValue = 'calculate'
mod.labelspan.style.fontWeight = 'normal'
//
// return
//
self.postMessage({path:path})
self.close()
})
}
//
// return values
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment