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

3D vectorization working?

parent d124f0e7
Branches
No related tags found
No related merge requests found
...@@ -27,7 +27,7 @@ var init = function() { ...@@ -27,7 +27,7 @@ var init = function() {
mod.dia_in.value = 0.0156 mod.dia_in.value = 0.0156
mod.dia_mm.value = 25.4*parseFloat(mod.dia_in.value) mod.dia_mm.value = 25.4*parseFloat(mod.dia_in.value)
mod.stepover.value = 0.5 mod.stepover.value = 0.5
mod.error.value = 1 mod.error.value = 0.001
} }
// //
// inputs // inputs
...@@ -38,12 +38,14 @@ var inputs = { ...@@ -38,12 +38,14 @@ var inputs = {
mod.map = evt.detail.map mod.map = evt.detail.map
mod.width = evt.detail.width mod.width = evt.detail.width
mod.height = evt.detail.height mod.height = evt.detail.height
mod.depth = Math.floor((mod.zmax-mod.zmin)*mod.width/(mod.xmax-mod.xmin))
mod.xmin = evt.detail.xmin mod.xmin = evt.detail.xmin
mod.xmax = evt.detail.xmax mod.xmax = evt.detail.xmax
mod.ymin = evt.detail.ymin mod.ymin = evt.detail.ymin
mod.ymax = evt.detail.ymax mod.ymax = evt.detail.ymax
mod.zmin = evt.detail.zmin mod.zmin = evt.detail.zmin
mod.zmax = evt.detail.zmax mod.zmax = evt.detail.zmax
mod.dpi = mod.width/(mod.xmax-mod.xmin)
var ctx = mod.img.getContext("2d") var ctx = mod.img.getContext("2d")
ctx.canvas.width = mod.width ctx.canvas.width = mod.width
ctx.canvas.height = mod.height ctx.canvas.height = mod.height
...@@ -54,14 +56,15 @@ var inputs = { ...@@ -54,14 +56,15 @@ var inputs = {
var outputs = { var outputs = {
toolpath:{type:'', toolpath:{type:'',
event:function(){ event:function(){
cmd = {} obj = {}
cmd.path = mod.path obj.path = mod.path
cmd.name = mod.name obj.name = "mill raster 3D"
cmd.dpi = mod.dpi obj.dpi = mod.dpi
cmd.width = mod.width obj.width = mod.width
cmd.height = mod.height obj.height = mod.height
cmd.depth = mod.depth obj.depth = mod.depth
mods.output(mod,'toolpath',cmd) mods.output(mod,'toolpath',obj)
console.log(mod.path[0].length)
}}} }}}
// //
// interface // interface
...@@ -216,7 +219,9 @@ function calculate_path() { ...@@ -216,7 +219,9 @@ function calculate_path() {
var url = window.URL.createObjectURL(blob) var url = window.URL.createObjectURL(blob)
var webworker = new Worker(url) var webworker = new Worker(url)
webworker.addEventListener('message',function(evt) { webworker.addEventListener('message',function(evt) {
mod.triangles = evt.data.triangles //
// webworker handler
//
mod.path = evt.data.path mod.path = evt.data.path
mod.label.nodeValue = 'calculate' mod.label.nodeValue = 'calculate'
mod.labelspan.style.fontWeight = 'normal' mod.labelspan.style.fontWeight = 'normal'
...@@ -240,7 +245,8 @@ function calculate_path() { ...@@ -240,7 +245,8 @@ function calculate_path() {
var ix = mod.path[0][i][0] var ix = mod.path[0][i][0]
var iy = mod.path[0][i][1] var iy = mod.path[0][i][1]
var iz = 0.1*mod.path[0][i][2] var iz = 0.1*mod.path[0][i][2]
var line = document.createElementNS('http://www.w3.org/2000/svg','line') var line = document.createElementNS(
'http://www.w3.org/2000/svg','line')
line.setAttribute('stroke','black') line.setAttribute('stroke','black')
line.setAttribute('stroke-width',1) line.setAttribute('stroke-width',1)
line.setAttribute('stroke-linecap','round') line.setAttribute('stroke-linecap','round')
...@@ -250,7 +256,14 @@ function calculate_path() { ...@@ -250,7 +256,14 @@ function calculate_path() {
line.setAttribute('y2',iy-iz) line.setAttribute('y2',iy-iz)
g.appendChild(line) g.appendChild(line)
} }
//
// output path
//
outputs.toolpath.event()
}) })
//
// call webworker
//
webworker.postMessage({ webworker.postMessage({
h:mod.height,w:mod.width,error:mod.error.value, h:mod.height,w:mod.width,error:mod.error.value,
xmin:mod.xmin,xmax:mod.xmax, xmin:mod.xmin,xmax:mod.xmax,
...@@ -275,7 +288,7 @@ function calculate_path_worker() { ...@@ -275,7 +288,7 @@ function calculate_path_worker() {
var map = evt.data.map var map = evt.data.map
var path = [[]] var path = [[]]
// //
// line loop // loop over lines
// //
xstart = 0 xstart = 0
ystart = h-1 ystart = h-1
...@@ -287,6 +300,9 @@ function calculate_path_worker() { ...@@ -287,6 +300,9 @@ function calculate_path_worker() {
dx = 1 dx = 1
dy = 0 dy = 0
while (1) { while (1) {
//
// vectorize
//
xnext = xcur+dx xnext = xcur+dx
ynext = ycur+dy ynext = ycur+dy
znext = Math.floor((map[ynext*w+xnext]-zmax)*w/(xmax-xmin)) znext = Math.floor((map[ynext*w+xnext]-zmax)*w/(xmax-xmin))
...@@ -301,13 +317,13 @@ function calculate_path_worker() { ...@@ -301,13 +317,13 @@ function calculate_path_worker() {
nzcur = dzcur/dcur nzcur = dzcur/dcur
dxnext = xnext-xcur dxnext = xnext-xcur
dynext = ynext-ycur dynext = ynext-ycur
dznext = znext-xcur dznext = znext-zcur
dnext = Math.sqrt(dxnext*dxnext+dynext*dynext+dznext*dznext) dnext = Math.sqrt(dxnext*dxnext+dynext*dynext+dznext*dznext)
nxnext = dxnext/dnext nxnext = dxnext/dnext
nynext = dynext/dnext nynext = dynext/dnext
nznext = dznext/dnext nznext = dznext/dnext
dot = nxcur*nxnext+nycur*nynext+nzcur*nznext dot = nxcur*nxnext+nycur*nynext+nzcur*nznext
if (Math.abs(dot) >= error) { if (dot <= (1-error)) {
path[0].push([xcur,ycur,zcur]) path[0].push([xcur,ycur,zcur])
xstart = xcur xstart = xcur
ystart = ycur ystart = ycur
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment