2019-05-06 16:08:46 +02:00
|
|
|
class DataViewer
|
|
|
|
constructor: (data) ->
|
|
|
|
@target = data
|
|
|
|
@el = ($ "<canvas>").attr("class", "viewer")[0]
|
2019-05-06 20:31:20 +02:00
|
|
|
@offset = 10
|
|
|
|
@points = []
|
|
|
|
@preprocess()
|
2019-05-06 16:08:46 +02:00
|
|
|
@getBound()
|
|
|
|
@prepare()
|
|
|
|
@render()
|
|
|
|
|
|
|
|
canvasPoint: (v) ->
|
2019-05-06 17:51:57 +02:00
|
|
|
return new paper.Point(v[0] / @target.resolution + @base.x, - v[1] / @target.resolution + @base.y)
|
2019-05-06 16:08:46 +02:00
|
|
|
|
2019-05-06 20:31:20 +02:00
|
|
|
preprocess: () ->
|
|
|
|
@points = @target.data
|
|
|
|
|
2019-05-06 16:08:46 +02:00
|
|
|
getBound: () ->
|
|
|
|
peak_tl = { x:0, y:0}
|
|
|
|
peak_rb = { x:0, y:0}
|
|
|
|
start = null
|
2019-05-06 20:31:20 +02:00
|
|
|
for v in @points
|
2019-05-06 16:08:46 +02:00
|
|
|
x = v[0] / @target.resolution
|
|
|
|
y = v[1] / @target.resolution
|
|
|
|
peak_tl.x = x if x < peak_tl.x
|
|
|
|
peak_tl.y = y if y < peak_tl.y
|
|
|
|
peak_rb.x = x if x > peak_rb.x
|
|
|
|
peak_rb.y = y if y > peak_rb.y
|
|
|
|
|
|
|
|
@bound = [ peak_tl, peak_rb ]
|
2019-05-06 17:51:57 +02:00
|
|
|
@base = {x: 0 - @bound[0].x+ @offset, y: @bound[1].y + @offset}
|
2019-05-06 16:08:46 +02:00
|
|
|
@width = peak_rb.x - peak_tl.x + 2*@offset
|
|
|
|
@height = peak_rb.y - peak_tl.y + 2*@offset
|
2019-05-06 19:15:23 +02:00
|
|
|
|
|
|
|
drawPoint: (v, color, size) ->
|
|
|
|
# center
|
|
|
|
new paper.Path.Circle {
|
|
|
|
center: @canvasPoint(v),
|
|
|
|
radius: size,
|
|
|
|
fillColor: color
|
|
|
|
}
|
|
|
|
drawGrid: (size, color) ->
|
|
|
|
wgridsize = @target.resolution*size
|
|
|
|
# draw y line
|
2019-05-06 20:31:20 +02:00
|
|
|
i = Math.ceil(@bound[0].x / size)
|
|
|
|
while i*size < @bound[1].x
|
|
|
|
start = new paper.Point(i*size + @base.x, -@bound[0].y + @base.y )
|
|
|
|
end = new paper.Point(i*size + @base.x, -@bound[1].y + @base.y )
|
2019-05-06 19:15:23 +02:00
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = color
|
|
|
|
path.moveTo start
|
|
|
|
path.lineTo end
|
|
|
|
i++
|
|
|
|
|
|
|
|
# draw x line
|
2019-05-06 20:31:20 +02:00
|
|
|
i = Math.ceil(@bound[0].y / size)
|
|
|
|
while i*size < @bound[1].y
|
|
|
|
start = new paper.Point(@bound[0].x + @base.x,-i*size + @base.y )
|
|
|
|
end = new paper.Point(@bound[1].x + @base.x, -i*size + @base.y )
|
2019-05-06 19:15:23 +02:00
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = color
|
|
|
|
path.moveTo start
|
|
|
|
path.lineTo end
|
|
|
|
i++
|
|
|
|
|
2019-05-06 20:31:20 +02:00
|
|
|
# draw text
|
|
|
|
text = new paper.PointText(@bound[0].x + @base.x, @base.y - @bound[1].y + @offset)
|
|
|
|
text.justification = 'left'
|
|
|
|
text.fillColor = '#494949'
|
|
|
|
text.content = "Resolution: #{@target.resolution}, grid size: #{wgridsize} mm"
|
|
|
|
|
2019-05-06 19:15:23 +02:00
|
|
|
drawAxis: (color) ->
|
|
|
|
# x axis
|
2019-05-06 16:08:46 +02:00
|
|
|
path = new paper.Path()
|
2019-05-06 19:15:23 +02:00
|
|
|
path.strokeColor = color
|
2019-05-06 20:31:20 +02:00
|
|
|
start = new paper.Point( @bound[0].x + @base.x, @base.y)
|
|
|
|
end = new paper.Point( @bound[1].x + @base.x, @base.y)
|
2019-05-06 16:08:46 +02:00
|
|
|
path.moveTo(start)
|
|
|
|
path.lineTo(end)
|
|
|
|
# y axis
|
|
|
|
path = new paper.Path()
|
2019-05-06 19:15:23 +02:00
|
|
|
path.strokeColor = color
|
2019-05-06 20:31:20 +02:00
|
|
|
start = new paper.Point(@base.x, -@bound[0].y + @base.y)
|
|
|
|
end = new paper.Point(@base.x, -@bound[1].y + @base.y)
|
2019-05-06 16:08:46 +02:00
|
|
|
path.moveTo(start)
|
|
|
|
path.lineTo(end)
|
2019-05-06 19:15:23 +02:00
|
|
|
@drawPoint [0,0], color, 3
|
2019-05-06 16:08:46 +02:00
|
|
|
|
2019-05-06 19:15:23 +02:00
|
|
|
prepare: () ->
|
|
|
|
ctx = @el.getContext "2d"
|
|
|
|
ctx.translate @base.x, @base.y
|
|
|
|
ctx.canvas.width = @width
|
|
|
|
ctx.canvas.height = @height
|
|
|
|
paper.setup @el
|
|
|
|
#tool = new paper.Tool()
|
|
|
|
#hitOptions = {
|
|
|
|
# segments: true,
|
|
|
|
# stroke: true,
|
|
|
|
# fill: true,
|
|
|
|
# tolerance: 5
|
|
|
|
#}
|
|
|
|
#tool.onMouseMove = (event) ->
|
|
|
|
# hitResult = paper.project.hitTest event.point, hitOptions
|
|
|
|
# return unless hitResult
|
|
|
|
# console.log hitResult
|
|
|
|
|
2019-05-06 16:08:46 +02:00
|
|
|
render:() ->
|
|
|
|
# sub class responsibility
|
|
|
|
|
|
|
|
class PointCloudViewer extends DataViewer
|
|
|
|
constructor: (data) ->
|
|
|
|
super data
|
|
|
|
|
2019-05-06 20:31:20 +02:00
|
|
|
p2c: (length, angle) ->
|
|
|
|
rad = angle*Math.PI / 180
|
|
|
|
return [length*Math.cos(rad), length*Math.sin(rad)]
|
|
|
|
|
|
|
|
preprocess: () ->
|
|
|
|
return @points = @target.data unless @target.coordinate is "polar"
|
|
|
|
@point = []
|
|
|
|
i = 0
|
|
|
|
for v in @target.data
|
|
|
|
@points.push @p2c v, @target.start + i*@target.angularResolution
|
|
|
|
i = i+1
|
|
|
|
|
2019-05-06 16:08:46 +02:00
|
|
|
# point clound render
|
|
|
|
render: () ->
|
2019-05-06 19:15:23 +02:00
|
|
|
@drawGrid 20, "#DBDBDB" # 20 px
|
|
|
|
@drawAxis("#0A84FF")
|
2019-05-06 20:31:20 +02:00
|
|
|
for v in @points
|
|
|
|
if @target.coordinate is "polar"
|
|
|
|
path = new paper.Path()
|
|
|
|
path.strokeColor = '#c2a10e'
|
|
|
|
start = @canvasPoint [0,0]
|
|
|
|
end = @canvasPoint v
|
|
|
|
path.moveTo start
|
|
|
|
path.lineTo end
|
|
|
|
@drawPoint v, "red", 3
|
2019-05-06 16:08:46 +02:00
|
|
|
paper.view.draw()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
class LuaPlayground extends this.OS.GUI.BaseApplication
|
|
|
|
constructor: ( args ) ->
|
|
|
|
super "LuaPlayground", args
|
|
|
|
|
|
|
|
main: () ->
|
|
|
|
me = @
|
|
|
|
@datarea = @find "editorea"
|
|
|
|
@output = @find "output"
|
|
|
|
@.editor = ace.edit @datarea
|
|
|
|
@.editor.setOptions {
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
enableLiveAutocompletion: true,
|
|
|
|
fontSize: "10pt"
|
|
|
|
}
|
|
|
|
@editor.getSession().setUseWrapMode true
|
|
|
|
@editor.session.setMode "ace/mode/lua"
|
|
|
|
@editor.setTheme "ace/theme/monokai"
|
|
|
|
@on "vboxchange", () ->
|
|
|
|
me.editor.resize()
|
2018-04-14 19:10:58 +02:00
|
|
|
(@find "log-clear").set "onbtclick", (e) ->
|
|
|
|
me.log "clean"
|
2019-05-07 14:34:26 +02:00
|
|
|
(@find "code-run").set "onbtclick", (e) ->
|
|
|
|
me.run()
|
|
|
|
|
|
|
|
(@find "code-stop").set "onbtclick", (e) ->
|
|
|
|
me.socket.close() if me.socket
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket = null
|
2018-04-14 19:10:58 +02:00
|
|
|
@bindKey "CTRL-R", () -> me.run()
|
2018-04-14 17:41:57 +02:00
|
|
|
menu: () ->
|
|
|
|
me = @
|
|
|
|
menu = [{
|
|
|
|
text: "__(Code)",
|
|
|
|
child: [
|
|
|
|
{ text: "__(Run)", dataid: "#{@name}-Run", shortcut: "C-R" }
|
|
|
|
],
|
|
|
|
onmenuselect: (e) -> me.run()
|
|
|
|
}]
|
|
|
|
menu
|
|
|
|
|
|
|
|
log: (t, m) ->
|
|
|
|
return $(@output).empty() if t is "clean"
|
|
|
|
p = ($ "<p>").attr("class", t.toLowerCase())[0]
|
|
|
|
$(p).html "#{t}: #{m.__()}"
|
|
|
|
($ @output).append p
|
|
|
|
($ @output).scrollTop @output.scrollHeight
|
|
|
|
|
|
|
|
run: () ->
|
|
|
|
me = @
|
|
|
|
value = @editor.getValue().trim()
|
|
|
|
return unless value and value isnt ""
|
2019-08-30 13:20:10 +02:00
|
|
|
@socket = @stream()
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket.onopen = () ->
|
|
|
|
#send data to server
|
|
|
|
me.socket.send( JSON.stringify { code: value } )
|
|
|
|
|
2019-05-06 16:08:46 +02:00
|
|
|
@socket.onmessage = (e) ->
|
|
|
|
return unless e.data
|
|
|
|
try
|
|
|
|
obj = JSON.parse e.data
|
|
|
|
me.log "INFO", e.data unless me.view obj
|
|
|
|
catch err
|
|
|
|
me.log "INFO", e.data
|
|
|
|
console.log err
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
@socket.onclose = () ->
|
|
|
|
me.socket = null
|
|
|
|
console.log "socket closed"
|
2019-05-06 16:08:46 +02:00
|
|
|
|
|
|
|
view: (obj) ->
|
|
|
|
return false unless obj and obj.type and @[obj.type]
|
|
|
|
el = @[obj.type](obj).el
|
|
|
|
p = ($ "<p>").attr("class", "info")[0]
|
|
|
|
$(p).append el
|
|
|
|
($ @output).append p
|
|
|
|
($ @output).scrollTop @output.scrollHeight
|
|
|
|
return true
|
|
|
|
|
|
|
|
pc: (data) ->
|
|
|
|
return new PointCloudViewer(data)
|
|
|
|
|
2018-04-14 17:41:57 +02:00
|
|
|
cleanup: (e)->
|
|
|
|
@socket.close() if @socket
|
|
|
|
|
2018-07-24 19:18:07 +02:00
|
|
|
LuaPlayground.dependencies = ["ace/ace"]
|
2018-04-14 17:41:57 +02:00
|
|
|
this.OS.register "LuaPlayground", LuaPlayground
|