Commit 8fdcd854 authored by Neil Gershenfeld's avatar Neil Gershenfeld
Browse files

wip

parent 1f13a1c3
......@@ -243,7 +243,7 @@ function calculate_path() {
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 = h*(zmax-z)/(ymax-ymin)
var dz = 0.1*h*(zmax-z)/(ymax-ymin)
while (1) {
var ixp = ix
var iyp = iy
......@@ -289,215 +289,6 @@ function calculate_path() {
mod.label.nodeValue = 'calculate'
mod.labelspan.style.fontWeight = 'normal'
}
//
// clear_path
//
function clear_path() {
}
//
// accumulate_path
// todo: replace inefficient insertion sort
// todo: move sort out of main thread
//
function accumulate_path(path) {
var forward = mod.forward.checked
var conventional = mod.conventional.checked
var sort = mod.sort.checked
for (var segnew = 0; segnew < path.length; ++segnew) {
if (conventional)
path[segnew].reverse()
if (mod.path.length == 0)
mod.path.splice(0,0,path[segnew])
else if (sort) {
var xnew = path[segnew][0][0]
var ynew = path[segnew][0][1]
var dmin = Number.MAX_VALUE
var segmin = -1
for (var segold = 0; segold < mod.path.length; ++segold) {
var xold = mod.path[segold][0][0]
var yold = mod.path[segold][0][1]
var dx = xnew-xold
var dy = ynew-yold
var d = Math.sqrt(dx*dx+dy*dy)
if (d < dmin) {
dmin = d
segmin = segold
}
}
if (forward)
mod.path.splice(segmin+1,0,path[segnew])
else
mod.path.splice(segmin,0,path[segnew])
}
else {
if (forward)
mod.path.splice(mod.path.length,0,path[segnew])
else
mod.path.splice(0,0,path[segnew])
}
}
}
//
// merge_path
//
function merge_path() {
var dmerge = mod.dpi*parseFloat(mod.merge.value)*parseFloat(mod.dia_in.value)
var seg = 0
while (seg < (mod.path.length-1)) {
var xold = mod.path[seg][mod.path[seg].length-1][0]
var yold = mod.path[seg][mod.path[seg].length-1][1]
var xnew = mod.path[seg+1][0][0]
var ynew = mod.path[seg+1][0][1]
var dx = xnew-xold
var dy = ynew-yold
var d = Math.sqrt(dx*dx+dy*dy)
if (d < dmerge)
mod.path.splice(seg,2,mod.path[seg].concat(mod.path[seg+1]))
else
seg += 1
}
}
//
// add_depth
//
function add_depth() {
var cut = parseFloat(mod.cut_in.value)
var max = parseFloat(mod.max_in.value)
var newpath = []
for (var seg = 0; seg < mod.path.length; ++seg) {
var depth = cut
if (mod.path[seg][0][0] == mod.path[seg][mod.path[seg].length-1][0]) {
var newseg = []
while (depth <= max) {
var idepth = -Math.round(mod.dpi*depth)
for (var pt = 0; pt < mod.path[seg].length; ++pt) {
var point = mod.path[seg][pt].concat(idepth)
newseg.splice(newseg.length,0,point)
}
if (depth == max)
break
depth += cut
if (depth > max)
depth = max
}
newpath.splice(newpath.length,0,newseg)
}
else {
var newseg = []
while (depth <= max) {
var idepth = -Math.round(mod.dpi*depth)
for (var pt = 0; pt < mod.path[seg].length; ++pt) {
var point = mod.path[seg][pt].concat(idepth)
newseg.splice(newseg.length,0,point)
}
newpath.splice(newpath.length,0,newseg)
newseg = []
if (depth == max)
break
depth += cut
if (depth > max)
depth = max
}
}
}
mod.path = newpath
mod.depth = Math.round(parseFloat(mod.max_in.value)*mod.dpi)
}
//
// draw_path
//
function draw_path(path) {
var g = document.getElementById(mod.div.id+'g')
var h = mod.img.height
var w = mod.img.width
var xend = null
var yend = null
//
// loop over segments
//
for (var segment = 0; segment < path.length; ++segment) {
if (path[segment].length > 1) {
//
// loop over points
//
for (var point = 1; point < path[segment].length; ++point) {
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')
var x1 = path[segment][point-1][0]
var y1 = h-path[segment][point-1][1]-1
var x2 = path[segment][point][0]
var y2 = h-path[segment][point][1]-1
xend = x2
yend = y2
line.setAttribute('x1',x1)
line.setAttribute('y1',y1)
line.setAttribute('x2',x2)
line.setAttribute('y2',y2)
var dx = x2-x1
var dy = y2-y1
var d = Math.sqrt(dx*dx+dy*dy)
if (d > 0) {
nx = 6*dx/d
ny = 6*dy/d
var tx = 3*dy/d
var ty = -3*dx/d
g.appendChild(line)
triangle = document.createElementNS('http://www.w3.org/2000/svg','polygon')
triangle.setAttribute('points',x2+','+y2+' '+(x2-nx+tx)+','+(y2-ny+ty)
+' '+(x2-nx-tx)+','+(y2-ny-ty))
triangle.setAttribute('fill','black')
g.appendChild(triangle)
}
}
}
}
}
//
// draw_connections
//
function draw_connections() {
var g = document.getElementById(mod.div.id+'g')
var h = mod.img.height
var w = mod.img.width
//
// loop over segments
//
for (var segment = 1; segment < mod.path.length; ++segment) {
//
// draw connection from previous segment
//
var line = document.createElementNS('http://www.w3.org/2000/svg','line')
line.setAttribute('stroke','red')
line.setAttribute('stroke-width',1)
line.setAttribute('stroke-linecap','round')
var x1 = mod.path[segment-1][mod.path[segment-1].length-1][0]
var y1 = h-mod.path[segment-1][mod.path[segment-1].length-1][1]-1
var x2 = mod.path[segment][0][0]
var y2 = h-mod.path[segment][0][1]-1
line.setAttribute('x1',x1)
line.setAttribute('y1',y1)
line.setAttribute('x2',x2)
line.setAttribute('y2',y2)
var dx = x2-x1
var dy = y2-y1
var d = Math.sqrt(dx*dx+dy*dy)
if (d > 0) {
nx = 6*dx/d
ny = 6*dy/d
var tx = 3*dy/d
var ty = -3*dx/d
g.appendChild(line)
triangle = document.createElementNS('http://www.w3.org/2000/svg','polygon')
triangle.setAttribute('points',x2+','+y2+' '+(x2-nx+tx)+','+(y2-ny+ty)
+' '+(x2-nx-tx)+','+(y2-ny-ty))
triangle.setAttribute('fill','red')
g.appendChild(triangle)
}
}
}
//
// return values
//
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment