dicom-viewer/index.js

505 lines
17 KiB
JavaScript
Raw Permalink Normal View History

2022-10-27 22:10:43 +03:00
2022-10-24 17:28:07 +03:00
var app = new dwv.App();
2022-10-27 22:10:43 +03:00
2022-10-29 14:59:09 +03:00
var circleFactory = new dwv.tool.draw.CircleFactory();
var roiFactory = new dwv.tool.draw.RoiFactory();
2022-11-01 11:33:54 +03:00
var freeHandFactory = new dwv.tool.draw.FreeHandFactory();
var rulerFactory = new dwv.tool.draw.RulerFactory();
2022-11-01 20:05:30 +03:00
var rectFactory = new dwv.tool.draw.RectangleFactory();
2022-10-31 13:33:51 +03:00
schema = []
2022-10-29 14:59:09 +03:00
2022-10-28 00:44:26 +03:00
var tools = {
2022-10-27 22:10:43 +03:00
Scroll: {},
Draw: {
2022-11-01 20:05:30 +03:00
options: ['Circle', 'Roi', 'FreeHand', 'Ruler', 'Rectangle'],
2022-11-01 11:33:54 +03:00
type: 'factory',
events: ['drawcreate', 'drawchange', 'drawmove', 'drawdelete']
2022-10-30 12:41:43 +03:00
},
2022-10-30 12:53:41 +03:00
ZoomAndPan: {},
2022-10-30 14:43:40 +03:00
WindowLevel: {},
2022-11-01 11:33:54 +03:00
Filter: {},
Livewire: {
events: ['drawcreate', 'drawchange', 'drawmove', 'drawdelete']
},
Floodfill: {
events: ['drawcreate', 'drawchange', 'drawmove', 'drawdelete']
}
2022-10-27 22:10:43 +03:00
};
2022-10-24 17:28:07 +03:00
app.init({
2022-10-27 22:10:43 +03:00
dataViewConfigs: {'*': [{divId: 'layerGroup0'}]},
2022-10-28 00:44:26 +03:00
tools: tools
2022-10-24 17:28:07 +03:00
});
2022-10-29 21:44:10 +03:00
parent.postMessage({'type': 'getDicom', data: ""}, "*")
2022-10-24 17:28:07 +03:00
app.addEventListener('load', function () {
2022-11-01 11:33:54 +03:00
2022-11-02 17:19:08 +03:00
// setTimeout(() => {
// console.log(postRuler())
// }, 3000)
2022-11-02 18:30:05 +03:00
currentframeNumber = 0
setFrame(0)
2022-11-02 17:19:08 +03:00
app.setTool('Draw')
app.setDrawShape('Circle')
2022-11-01 11:33:54 +03:00
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().addEventListener('mouseup', () => {
2022-11-01 20:05:30 +03:00
console.log(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer())
2022-11-01 11:33:54 +03:00
setTimeout(() => {
parent.postMessage({'type': 'returnDraws', data: postDraws()}, "*")
//console.log({'type': 'returnDraws', data: postDraws()})
}, 100)
})
2022-10-24 17:28:07 +03:00
});
2022-10-27 22:10:43 +03:00
2022-10-31 13:33:51 +03:00
function loadSchema(schema) {
console.log(schema, schema.length)
urls = []
picSchema = new Map()
for (var i = 0; i < schema.length; ++i) {
console.log(i)
picSchema.set(schema[i].url, schema[i].figures)
urls.push(schema[i].url)
}
2022-11-01 20:05:30 +03:00
app.loadURLs(urls)
2022-11-02 18:33:49 +03:00
try{
createDraws(schema[0].figures)
} catch {}
2022-10-31 13:33:51 +03:00
return [urls, picSchema]
}
2022-10-30 14:43:40 +03:00
2022-10-29 14:59:09 +03:00
function createCircle(circleData) {
var point1 = new dwv.math.Point2D(circleData.center.x, circleData.center.y)
var point2 = new dwv.math.Point2D(circleData.center.x, circleData.center.y+circleData.radius)
app.setTool('Draw');
var styles = app.getToolboxController().getSelectedTool().style
2022-10-30 12:53:41 +03:00
app.undo()
2022-10-29 14:59:09 +03:00
var draw = circleFactory.create(
[point1, point2],
styles,
app.getActiveLayerGroup().getActiveViewLayer().getViewController()
)
2022-10-30 12:53:41 +03:00
2022-10-30 14:58:20 +03:00
//draw.id(dwv.math.guid());
draw.id("id")
2022-10-30 12:41:43 +03:00
draw.draggable(true)
draw.addEventListener('mouseover', () => {
document.body.style.cursor = 'pointer'
})
draw.addEventListener('mouseout', () => {
document.body.style.cursor = 'default'
})
//drawCommand = new dwv.tool.DrawGroupCommand(draw, 'circle-group', app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer()['.position-group'][0], false)
posGroup = app.getActiveLayerGroup().getActiveDrawLayer().getDrawController().getCurrentPosGroup()
2022-10-31 13:33:51 +03:00
console.log(app.getActiveLayerGroup().getActiveDrawLayer().getDrawController())
2022-10-30 12:41:43 +03:00
posGroup.add(draw)
2022-10-30 12:50:22 +03:00
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().listening(true)
2022-10-30 12:41:43 +03:00
//drawCommand.execute()
//app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.position-group')[0].add(draw)
2022-10-29 14:59:09 +03:00
}
2022-11-01 11:33:54 +03:00
function createRuler(rulerData) {
app.setTool('Draw');
var styles = app.getToolboxController().getSelectedTool().style
points = rulerData.points.map((e) => {
return new dwv.math.Point2D(e.x, e.y)
})
var draw = rulerFactory.create(
points,
styles,
app.getActiveLayerGroup().getActiveViewLayer().getViewController()
)
draw.id(dwv.math.guid());
draw.draggable(true)
draw.addEventListener('mouseover', () => {
document.body.style.cursor = 'pointer'
})
draw.addEventListener('mouseout', () => {
document.body.style.cursor = 'default'
})
//drawCommand = new dwv.tool.DrawGroupCommand(draw, 'circle-group', app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer()['.position-group'][0], false)
posGroup = app.getActiveLayerGroup().getActiveDrawLayer().getDrawController().getCurrentPosGroup()
posGroup.add(draw)
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().listening(true)
}
function createFreeHand(freeHandData) {
app.setTool('Draw');
var styles = app.getToolboxController().getSelectedTool().style
points = freeHandData.points.map((e) => {
return new dwv.math.Point2D(e.x, e.y)
})
var draw = freeHandFactory.create(
points,
styles,
app.getActiveLayerGroup().getActiveViewLayer().getViewController()
)
draw.id(dwv.math.guid());
draw.draggable(true)
draw.addEventListener('mouseover', () => {
document.body.style.cursor = 'pointer'
})
draw.addEventListener('mouseout', () => {
document.body.style.cursor = 'default'
})
//drawCommand = new dwv.tool.DrawGroupCommand(draw, 'circle-group', app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer()['.position-group'][0], false)
posGroup = app.getActiveLayerGroup().getActiveDrawLayer().getDrawController().getCurrentPosGroup()
posGroup.add(draw)
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().listening(true)
}
2022-10-29 14:59:09 +03:00
function createRoi(circleData) {
app.setTool('Draw');
var styles = app.getToolboxController().getSelectedTool().style
points = circleData.points.map((e) => {
return new dwv.math.Point2D(e.x, e.y)
})
var draw = roiFactory.create(
points,
styles,
app.getActiveLayerGroup().getActiveViewLayer().getViewController()
)
2022-10-30 12:41:43 +03:00
draw.id(dwv.math.guid());
draw.draggable(true)
draw.addEventListener('mouseover', () => {
document.body.style.cursor = 'pointer'
})
draw.addEventListener('mouseout', () => {
document.body.style.cursor = 'default'
})
//drawCommand = new dwv.tool.DrawGroupCommand(draw, 'circle-group', app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer()['.position-group'][0], false)
posGroup = app.getActiveLayerGroup().getActiveDrawLayer().getDrawController().getCurrentPosGroup()
posGroup.add(draw)
2022-10-30 12:50:22 +03:00
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().listening(true)
2022-10-29 14:59:09 +03:00
}
2022-11-01 20:05:30 +03:00
function createRectangle(rectangleData) {
app.setTool('Draw');
var styles = app.getToolboxController().getSelectedTool().style
points = rectangleData.points.map((e) => {
return new dwv.math.Point2D(e.x, e.y)
})
var draw = rectFactory.create(
points,
styles,
app.getActiveLayerGroup().getActiveViewLayer().getViewController()
)
draw.id(dwv.math.guid());
draw.draggable(true)
draw.addEventListener('mouseover', () => {
document.body.style.cursor = 'pointer'
})
draw.addEventListener('mouseout', () => {
document.body.style.cursor = 'default'
})
posGroup = app.getActiveLayerGroup().getActiveDrawLayer().getDrawController().getCurrentPosGroup()
posGroup.add(draw)
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().listening(true)
}
2022-11-01 11:33:54 +03:00
2022-10-29 14:59:09 +03:00
function createDraws(drawsData) {
drawsData.data.map((e) => {
if (e.type == 'Circle') {
createCircle(e)
}
if (e.type == 'Roi') {
createRoi(e)
}
2022-11-01 11:33:54 +03:00
if (e.type == 'FreeHand') {
createFreeHand(e)
}
if (e.type == 'Ruler') {
createRuler(e.data)
}
2022-11-01 20:05:30 +03:00
if (e.type == 'Rectangle') {
createRectangle(e.data)
}
2022-10-29 14:59:09 +03:00
})
}
function postCircles() {
2022-11-01 00:44:54 +03:00
2022-10-31 13:33:51 +03:00
setTimeout(() => {
console.log(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().children[0].children)
}, 100)
// circlesAttrs = app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().getChildren().filter(e => e.attrs.name == 'circle-group').map(
// (e) => {
// //console.log(e)
// return e.children.filter(
// (ee) => {
// return ee.attrs.name == 'shape'
// }
// ).map(eee => {return {attrs: eee.attrs, parent: e.attrs}})
// }
// )
circlesAttrs = []
2022-11-02 18:23:23 +03:00
circlesAttrs = circlesAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.circle-group').map((e) => {
2022-10-31 13:33:51 +03:00
return e.children.filter((eee) => eee.attrs.name == 'shape').map(ee => {return {attrs: ee.attrs, parent: e.attrs}})
2022-10-29 15:44:46 +03:00
}))
//circleAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().children[0])
2022-10-29 14:59:09 +03:00
return circlesAttrs.flat().map((e) => {
return {
type: 'Circle',
center: {
2022-10-31 13:33:51 +03:00
x: e.attrs.x,
y: e.attrs.y
2022-10-29 14:59:09 +03:00
},
2022-10-31 13:33:51 +03:00
radius: e.attrs.radius,
id: e.parent.id
2022-10-29 14:59:09 +03:00
}
})
}
function postRois() {
2022-10-31 13:33:51 +03:00
roisAttrs = []
2022-11-02 18:35:23 +03:00
roisAttrs = roisAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.roi-group').map((e) => {
2022-10-31 13:33:51 +03:00
return e.children.filter((e) => e.attrs.name == 'shape').map(e => {return {attrs: e.attrs, parent: e.parent.attrs}})
2022-10-29 15:54:01 +03:00
}))
return roisAttrs.flat().map((e) => {
2022-10-31 13:33:51 +03:00
console.log(e)
2022-10-29 15:54:01 +03:00
var points = [];
2022-10-31 13:33:51 +03:00
for (var i = 0; i < e.attrs.points.length; i+=2) {
points.push({x: e.attrs.points[i], y: e.attrs.points[i+1]})
2022-10-29 14:59:09 +03:00
}
return {
type: 'Roi',
2022-10-30 14:58:20 +03:00
points: points,
2022-10-31 13:33:51 +03:00
id: e.parent.id
2022-10-29 14:59:09 +03:00
}
})
}
function postDraws() {
2022-11-01 20:05:30 +03:00
return JSON.parse(JSON.stringify(
postCircles().concat(postRois()).concat(postFreeHand()).concat(postRuler()).concat(postRects())
))
}
function postRects() {
roisAttrs = []
// setTimeout(() => {
// console.log(roisAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.rectangle-group')), 'rect')
// }, 1000)
roisAttrs = roisAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.rectangle-group').map((e) => {
return e.children.filter((e) => e.attrs.name == 'shape').map(e => {return {attrs: e.attrs, parent: e.parent.attrs}})
}))
return roisAttrs.flat().map((e) => {
e.attrs.points = [e.attrs.x, e.attrs.y, e.attrs.x+e.attrs.width, e.attrs.y+e.attrs.height]
var points = [];
for (var i = 0; i < e.attrs.points.length; i+=2) {
points.push({x: e.attrs.points[i], y: e.attrs.points[i+1]})
}
return {
type: 'Rectangle',
points: points,
id: e.parent.id
}
})
2022-11-01 11:33:54 +03:00
}
function postFreeHand() {
roisAttrs = []
2022-11-02 18:23:23 +03:00
roisAttrs = roisAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.freeHand-group').map((e) => {
2022-11-01 11:33:54 +03:00
return e.children.filter((e) => e.attrs.name == 'shape').map(e => {return {attrs: e.attrs, parent: e.parent.attrs}})
}))
return roisAttrs.flat().map((e) => {
var points = [];
for (var i = 0; i < e.attrs.points.length; i+=2) {
points.push({x: e.attrs.points[i], y: e.attrs.points[i+1]})
}
return {
type: 'FreeHand',
points: points,
id: e.parent.id
}
})
}
function postRuler() {
roisAttrs = []
2022-11-02 18:23:23 +03:00
roisAttrs = roisAttrs.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().find('.ruler-group').map((e) => {
2022-11-01 11:33:54 +03:00
return e.children.filter((e) => e.attrs.name == 'shape').map(e => {return {attrs: e.attrs, parent: e.parent.attrs}})
}))
return roisAttrs.flat().map((e) => {
var points = [];
for (var i = 0; i < e.attrs.points.length; i+=2) {
points.push({x: e.attrs.points[i], y: e.attrs.points[i+1]})
}
return {
2022-11-01 20:09:12 +03:00
type: 'Ruler',
2022-11-01 11:33:54 +03:00
points: points,
id: e.parent.id
}
})
2022-10-29 14:59:09 +03:00
}
2022-10-27 22:10:43 +03:00
function receiveMessage(event)
{
2022-10-29 14:59:09 +03:00
let data = undefined;
try{
data = event.data
}
catch{
return
}
2022-10-27 22:10:43 +03:00
if (data.type == "setDicom"){
app.loadURLs([data.data])
} else if(data.type == "setTool"){
2022-10-29 01:01:36 +03:00
if (data.data == "null"){
app.setTool('Scroll');
2022-11-01 20:05:30 +03:00
} else if (data.data == 'Roi' || data.data == 'Circle' || data.data == 'FreeHand' || data.data == 'Ruler' || data.data == 'Rectangle'){
2022-10-29 01:01:36 +03:00
app.setTool('Draw');
app.setDrawShape(data.data);
}
2022-10-30 12:41:43 +03:00
else if (data.data == 'ZoomAndPan') {
app.setTool('ZoomAndPan');
}
2022-10-30 12:53:41 +03:00
else if (data.data == 'WindowLevel') {
app.setTool('WindowLevel')
2022-11-01 00:44:54 +03:00
}else {
app.setTool('Draw');
app.setDrawShape(data.data);
2022-10-30 12:53:41 +03:00
}
2022-10-29 14:59:09 +03:00
}
else if (data.type == 'setDraws') {
createDraws(data)
}
else if (data.type == 'getDraws') {
2022-11-02 21:11:39 +03:00
parent.postMessage({'type': 'saveDraws', data: postDraws()}, "*")
2022-10-27 22:10:43 +03:00
}
2022-10-29 16:28:57 +03:00
else if (data.type == 'deleteSelected') {
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('#'+findActive())[0].destroy()
2022-10-31 20:17:33 +03:00
parent.postMessage({'type': 'returnDrawsDelete', data: postDraws()}, "*")
2022-10-29 16:28:57 +03:00
}
2022-10-30 14:43:40 +03:00
else if (data.type == 'setContrast') {
document.getElementById("layerGroup0").style = `filter: contrast(${data.data}%);`
}
2022-10-30 14:58:20 +03:00
else if (data.type == 'deleteById') {
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find("#"+data.data)[0].destroy()
}
2022-10-31 13:33:51 +03:00
else if (data.type == 'loadSchema') {
schema = loadSchema(data.data)
}
2022-11-01 11:33:54 +03:00
else if (data.type == 'setIndex') {
setFrame(data.data)
}
2022-10-27 22:10:43 +03:00
}
2022-10-29 21:44:10 +03:00
window.addEventListener("message", receiveMessage, false);
2022-10-29 16:28:57 +03:00
function findActive() {
activeCandidates = app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().getChildren().map(
(e) => {
return e.children.filter(
(ee) => {
return ee.attrs.name == 'anchor'
}
)
}
)
activeCandidates = activeCandidates.concat(app.getActiveLayerGroup().getActiveDrawLayer().getKonvaLayer().children[0].children.map((e) => {
return e.children.filter((e) => e.attrs.name == 'anchor')
}))
active = new Set(activeCandidates.flat().map(e => e.parent.id()))
return [...active][0]
}
2022-10-27 22:10:43 +03:00
2022-10-24 17:28:07 +03:00
app.addEventListener('loadend', function () {
2022-10-30 12:41:43 +03:00
//range.max = app.getImage(0).getGeometry().getSize().get(2) - 1;
2022-10-24 17:28:07 +03:00
});
2022-10-29 14:59:09 +03:00
2022-11-01 11:33:54 +03:00
app.addEventListener('drawchange', (e) => {
console.log(e)
})
2022-10-31 13:33:51 +03:00
2022-11-01 11:33:54 +03:00
schema = [
{
'url': 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm',
'figures': [
{type: 'Circle', center: {x: 10, y: 100}, radius: 10},
{type: 'Roi', points: [{x: 10, y: 20}, {x: 100, y: 50}]}
]
},
{
'url': 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323707.dcm',
'figures': [
{type: 'Circle', center: {x: 10, y: 200}, radius: 10},
{type: 'Roi', points: [{x: 10, y: 30}, {x: 100, y: 50}]}
]
}
]
2022-10-31 13:33:51 +03:00
// console.log(schema[0], "0")
2022-11-02 17:19:08 +03:00
//app.loadURLs(schema[0])
2022-10-31 13:33:51 +03:00
// app.loadURLs([
// 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323851.dcm',
// 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323707.dcm',
// 'https://raw.githubusercontent.com/ivmartel/dwv/master/tests/data/bbmri-53323563.dcm'
// ])
2022-11-02 17:19:08 +03:00
currentframeNumber = 0
2022-10-31 13:33:51 +03:00
function setFrame(frameNumber) {
var lg = app.getLayerGroupById(0);
var vc = lg.getActiveViewLayer().getViewController();
var index = vc.getCurrentIndex();
var values = index.getValues();
2022-11-02 17:19:08 +03:00
pics = schema[1].get(schema[0][frameNumber])
schema[1].set(schema[0][currentframeNumber], postDraws())
console.log(schema)
2022-10-31 13:33:51 +03:00
values[2] = frameNumber;
vc.setCurrentIndex(new dwv.math.Index(values));
loadPictures(
2022-11-02 17:19:08 +03:00
pics
2022-10-31 13:33:51 +03:00
)
2022-11-01 20:05:30 +03:00
app.setTool('Draw')
2022-11-02 17:19:08 +03:00
currentframeNumber = frameNumber
2022-10-31 13:33:51 +03:00
}
function loadPictures(pictures) {
2022-11-02 17:19:08 +03:00
console.log(pictures)
2022-10-31 13:33:51 +03:00
// app.deleteDraws()
deleteDraws()
2022-11-02 17:19:08 +03:00
createDraws({data: pictures})
2022-10-31 13:33:51 +03:00
}
function deleteDraws() {
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('.circle-group').map((e) => e.destroy())
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('.roi-group').map((e) => e.destroy())
2022-11-01 11:33:54 +03:00
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('.freeHand-group').map((e) => e.destroy())
2022-11-01 20:05:30 +03:00
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('.rectangle-group').map((e) => e.destroy())
app.getActiveLayerGroup().getActiveDrawLayer().getKonvaStage().find('.ruler-group').map((e) => e.destroy())
2022-10-31 13:33:51 +03:00
}
2022-10-30 12:41:43 +03:00
2022-10-24 17:28:07 +03:00
app.addEventListener('slicechange', function () {
// update slider on slice change (for ex via mouse wheel)
var lg = app.getLayerGroupById(0);
var vc = lg.getActiveViewLayer().getViewController();
range.value = vc.getCurrentPosition().k;
});
2022-10-30 12:41:43 +03:00
// range.oninput = function () {
// var lg = app.getLayerGroupById(0);
// var vc = lg.getActiveViewLayer().getViewController();
// var index = vc.getCurrentIndex();
// var values = index.getValues();
// values[2] = this.value;
// vc.setCurrentIndex(new dwv.math.Index(values));
// }
2022-10-29 14:59:09 +03:00