Keep toolData and Measurements in sync (LT-149)

This commit is contained in:
Erik Ziegler 2016-02-12 20:38:00 +01:00
parent 5b07ec372c
commit f7e24b8b10
8 changed files with 139 additions and 78 deletions

View File

@ -152,6 +152,8 @@ Template.viewer.onCreated(function() {
// This is used to re-add tools from the database into the
// Cornerstone ToolData structure
var syncTimeout,
syncDelay = 50;
Measurements.find().observe({
added: function(data) {
if (data.toolDataInsertedManually === true) {
@ -171,9 +173,27 @@ Template.viewer.onCreated(function() {
log.info('Measurement added');
syncMeasurementAndToolData(data);
updateRelatedElements(data.imageId);
// Update each displayed viewport
var viewports = $('.imageViewerViewport').not('.empty');
viewports.each(function(index, element) {
cornerstone.updateImage(element);
});
},
changed: function(data) {
if (OHIF.viewer.manuallyModifyingMeasurement === true) {
return;
}
log.info('Measurement changed');
syncMeasurementAndToolData(data);
// Update each displayed viewport
var viewports = $('.imageViewerViewport').not('.empty');
viewports.each(function(index, element) {
cornerstone.updateImage(element);
});
TrialResponseCriteria.validateAllDelayed();
},
removed: function(data) {
@ -204,13 +224,15 @@ Template.viewer.onCreated(function() {
// find the Measurements, whereas on the server it's
// only "greater than", since inside this callback the
// Measurements have already been decremented.
Measurements.find({
var measurements = Measurements.find({
patientId: data.patientId,
lesionNumberAbsolute: {
$gte: data.lesionNumberAbsolute
}
}).forEach(function(measurementData) {
syncMeasurementAndToolData(measurementData);
});
measurements.forEach(function(measurement) {
syncMeasurementAndToolData(measurement);
});
// Update each displayed viewport

View File

@ -142,10 +142,6 @@
if (cornerstoneTools.anyHandlesOutsideImage(touchEventData, measurementData.handles)) {
// delete the measurement
cornerstoneTools.removeToolState(element, toolType, measurementData);
} else {
// Set lesionMeasurementData Session
var config = cornerstoneTools.lesion.getConfiguration();
//config.getLesionLocationCallback(measurementData, touchEventData, doneCallback);
}
// perpendicular line is not connected to long-line

View File

@ -86,25 +86,41 @@ function updateLesionData(lesionData) {
// Insert this into the Measurements Collection
// Save the ID into the toolData (not sure if this works?)
console.log('LesionManager inserting Measurement');
measurement.id = Measurements.insert(measurement);
// Update the database entry so it can be readded next time the study is loaded
// Update the database entry so it can be re-added next time the study is loaded
Measurements.update(measurement.id, {
$set: {
toolDataInsertedManually: false
}
}, function(error) {
if (error) {
log.warn(error);
}
OHIF.viewer.manuallyModifyingMeasurement = false;
});
} else {
lesionData.id = existingMeasurement._id;
lesionData.isNodal = existingMeasurement.isNodal;
if (_.isEqual(existingMeasurement.timepoints[timepoint.timepointId], timepointData)) {
return;
}
// Update timepoints from lesion data
existingMeasurement.timepoints[timepoint.timepointId] = timepointData;
console.log('LesionManager updating Measurement');
Measurements.update(existingMeasurement._id, {
$set: {
timepoints: existingMeasurement.timepoints
}
}, function(error) {
if (error) {
log.warn(error);
}
OHIF.viewer.manuallyModifyingMeasurement = false;
});
}
}

View File

@ -5,6 +5,7 @@ handleMeasurementAdded = function(e, eventData) {
case 'nonTarget':
case 'lesion':
log.info('CornerstoneToolsMeasurementAdded');
OHIF.viewer.manuallyModifyingMeasurement = true;
LesionManager.updateLesionData(measurementData);
TrialResponseCriteria.validateDelayed(measurementData);
break;

View File

@ -5,6 +5,7 @@ handleMeasurementModified = function(e, eventData) {
case 'nonTarget':
case 'lesion':
log.info('CornerstoneToolsMeasurementModified');
OHIF.viewer.manuallyModifyingMeasurement = true;
LesionManager.updateLesionData(measurementData);
TrialResponseCriteria.validateDelayed(measurementData);
break;

View File

@ -1,10 +1,11 @@
handleMeasurementRemoved = function(e, eventData) {
log.info('CornerstoneToolsMeasurementRemoved');
var measurementData = eventData.measurementData;
switch (eventData.toolType) {
case 'nonTarget':
case 'lesion':
log.info('CornerstoneToolsMeasurementRemoved');
var measurement = Measurements.findOne(measurementData.id, {
reactive: false
});

View File

@ -1,4 +1,5 @@
removeToolDataWithMeasurementId = function(imageId, toolType, measurementId) {
log.info('removeToolDataWithMeasurementId');
var toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
// Find any related toolData
@ -20,6 +21,9 @@ removeToolDataWithMeasurementId = function(imageId, toolType, measurementId) {
}
});
console.log("Removing Indices: ");
console.log(toRemove);
// If any toolData entries need to be removed, splice them from
// the toolData array
toRemove.forEach(function(index) {

View File

@ -1,75 +1,95 @@
syncMeasurementAndToolData = function(data) {
syncMeasurementAndToolData = function(measurement) {
console.log('syncMeasurementAndToolData');
// Check what toolType we should be adding this to, based on the isTarget value
// of the stored Measurement
var toolType = data.isTarget ? 'lesion' : 'nonTarget';
var toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
var toolType = measurement.isTarget ? 'lesion' : 'nonTarget';
// Loop through the timepoint data for this measurement
Object.keys(data.timepoints).forEach(function(key) {
var storedData = data.timepoints[key];
var imageId = storedData.imageId;
Object.keys(measurement.timepoints).forEach(function(key) {
var timepointData = measurement.timepoints[key];
var imageId = timepointData.imageId;
if (!toolState[imageId]) {
toolState[imageId] = {};
}
// This is probably not the best approach to prevent duplicates
if (toolState[imageId][toolType] && toolState[imageId][toolType].data) {
var measurementHasNoIdYet = false;
toolState[imageId][toolType].data.forEach(function(measurement) {
if (measurement.id === 'notready') {
measurementHasNoIdYet = true;
return false;
}
});
// Stop here if it appears that we are creating this measurement right now,
// and would not like this function to add another copy of it to the toolData
if (measurementHasNoIdYet === true) {
return;
}
}
if (!toolState[imageId][toolType]) {
toolState[imageId][toolType] = {
data: []
};
} else {
var alreadyExists = false;
if (toolState[imageId][toolType].data.length) {
toolState[imageId][toolType].data.forEach(function(measurement) {
if (measurement.id === data._id) {
alreadyExists = true;
// Update the toolData lesionNumber from the Measurement
measurement.lesionNumber = data.lesionNumber;
return false;
}
});
}
if (alreadyExists === true) {
return;
}
}
// Create measurementData structure based on the lesion data at this timepoint
// We will add this into the toolData for this imageId
var measurementData = storedData;
measurementData.isTarget = data.isTarget;
measurementData.lesionNumber = data.lesionNumber;
measurementData.measurementText = data.measurementText;
measurementData.isDeleted = data.isDeleted;
measurementData.location = data.location;
measurementData.locationUID = data.locationUID;
measurementData.patientId = data.patientId;
measurementData.visible = data.visible;
measurementData.active = data.active;
measurementData.uid = data.uid;
measurementData.id = data._id;
toolState[imageId][toolType].data.push(measurementData);
TrialResponseCriteria.validateSingleMeasurement(measurementData);
syncTimepointDataWithToolData(measurement, timepointData, imageId, toolType);
});
};
function syncTimepointDataWithToolData(measurement, timepointData, imageId, toolType) {
var toolState = cornerstoneTools.globalImageIdSpecificToolStateManager.toolState;
if (!toolState[imageId]) {
toolState[imageId] = {};
}
// This is probably not the best approach to prevent duplicates
if (toolState[imageId][toolType] && toolState[imageId][toolType].data) {
var measurementHasNoIdYet = false;
toolState[imageId][toolType].data.forEach(function(measurement) {
if (measurement.id !== 'notready') {
return;
}
measurementHasNoIdYet = true;
return false;
});
// Stop here if it appears that we are creating this measurement right now,
// and would not like this function to add another copy of it to the toolData
if (measurementHasNoIdYet === true) {
return;
}
}
if (toolState[imageId][toolType]) {
var alreadyExists = false;
var toolData = toolState[imageId][toolType].data;
if (!toolData.length) {
return;
}
toolData.forEach(function(tool) {
if (tool.id !== measurement._id) {
return;
}
alreadyExists = true;
// Update the toolData lesionNumber from the Measurement
tool.lesionNumber = measurement.lesionNumber;
tool.isTarget = measurement.isTarget;
tool.active = timepointData.active;
tool.visible = timepointData.visible;
tool.isDeleted = timepointData.isDeleted;
tool.handles = timepointData.handles;
return false;
});
if (alreadyExists === true) {
return;
}
} else {
toolState[imageId][toolType] = {
data: []
};
}
// Create measurementData structure based on the lesion data at this timepoint
// We will add this into the toolData for this imageId
var measurementData = timepointData;
measurementData.isTarget = measurement.isTarget;
measurementData.lesionNumber = measurement.lesionNumber;
measurementData.measurementText = measurement.measurementText;
measurementData.isDeleted = measurement.isDeleted;
measurementData.location = measurement.location;
measurementData.locationUID = measurement.locationUID;
measurementData.patientId = measurement.patientId;
measurementData.visible = measurement.visible;
measurementData.active = measurement.active;
measurementData.uid = measurement.uid;
measurementData.id = measurement._id;
toolState[imageId][toolType].data.push(measurementData);
TrialResponseCriteria.validateSingleMeasurement(measurementData);
}