dwv 0.31.0-beta.3 → 0.31.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dwv.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! dwv 0.31.0-beta.3 2022-03-22 16:38:42 */
1
+ /*! dwv 0.31.0-beta.6 2022-06-23 18:02:33 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -560,15 +560,25 @@ dwv.App = function () {
560
560
  typeof options.dataViewConfigs === 'undefined') {
561
561
  throw new Error('No available data view configuration');
562
562
  }
563
- var configs = null;
563
+ var configs = [];
564
564
  if (typeof options.dataViewConfigs['*'] !== 'undefined') {
565
565
  configs = options.dataViewConfigs['*'];
566
- } else {
566
+ } else if (typeof options.dataViewConfigs[dataIndex] !== 'undefined') {
567
567
  configs = options.dataViewConfigs[dataIndex];
568
568
  }
569
569
  return configs;
570
570
  }
571
571
 
572
+ /**
573
+ * Get the data view config.
574
+ * Carefull, returns a reference, do not modify without resetting.
575
+ *
576
+ * @returns {object} The configuration list.
577
+ */
578
+ this.getDataViewConfig = function () {
579
+ return options.dataViewConfigs;
580
+ };
581
+
572
582
  /**
573
583
  * Set the data view configuration (see the init options for details).
574
584
  *
@@ -603,8 +613,11 @@ dwv.App = function () {
603
613
  }
604
614
  // loop on all configs
605
615
  var viewConfigs = getViewConfigs(dataIndex);
606
- if (!viewConfigs) {
607
- throw new Error('No view config for data: ' + dataIndex);
616
+ // nothing to do if no view config
617
+ if (viewConfigs.length === 0) {
618
+ dwv.logger.info('Not rendering data: ' + dataIndex +
619
+ ' (no data view config)');
620
+ return;
608
621
  }
609
622
  for (var i = 0; i < viewConfigs.length; ++i) {
610
623
  var config = viewConfigs[i];
@@ -648,7 +661,8 @@ dwv.App = function () {
648
661
  var layerGroup = stage.getActiveLayerGroup();
649
662
  var viewController = layerGroup.getActiveViewLayer().getViewController();
650
663
  var k = viewController.getCurrentScrollPosition();
651
- layerGroup.addScale(step, {x: cx, y: cy, z: k});
664
+ var center = new dwv.math.Point3D(cx, cy, k);
665
+ layerGroup.addScale(step, center);
652
666
  layerGroup.draw();
653
667
  };
654
668
 
@@ -1091,11 +1105,14 @@ dwv.App = function () {
1091
1105
  type: 'loaditem',
1092
1106
  data: eventMetaData,
1093
1107
  source: event.source,
1094
- loadtype: event.loadtype
1108
+ loadtype: event.loadtype,
1109
+ loadid: event.loadid,
1110
+ isfirstitem: event.isfirstitem
1095
1111
  });
1096
1112
 
1097
1113
  // render if first and flag allows
1098
1114
  if (event.loadtype === 'image' &&
1115
+ getViewConfigs(event.loadid).length !== 0 &&
1099
1116
  isFirstLoadItem && options.viewOnFirstLoadItem) {
1100
1117
  self.render(event.loadid);
1101
1118
  }
@@ -1237,7 +1254,7 @@ dwv.App = function () {
1237
1254
  var data = dataController.get(indices[i]);
1238
1255
  var geometry = data.image.getGeometry();
1239
1256
  var viewOrient = dwv.gui.getViewOrientation(
1240
- geometry,
1257
+ geometry.getOrientation(),
1241
1258
  layerGroup.getTargetOrientation()
1242
1259
  );
1243
1260
  var size = geometry.getSize(viewOrient).get2D();
@@ -1281,7 +1298,7 @@ dwv.App = function () {
1281
1298
  new dwv.dicom.DicomElementsWrapper(data.meta),
1282
1299
  data.image);
1283
1300
  var viewOrientation = dwv.gui.getViewOrientation(
1284
- imageGeometry,
1301
+ imageGeometry.getOrientation(),
1285
1302
  layerGroup.getTargetOrientation()
1286
1303
  );
1287
1304
  view.setOrientation(viewOrientation);
@@ -1322,22 +1339,9 @@ dwv.App = function () {
1322
1339
  viewLayer.setView(view);
1323
1340
  var size2D = imageGeometry.getSize(viewOrientation).get2D();
1324
1341
  var spacing2D = imageGeometry.getSpacing(viewOrientation).get2D();
1325
- viewLayer.initialise(size2D, spacing2D, dataIndex);
1326
- viewLayer.setOpacity(opacity);
1327
-
1328
- // compensate origin difference
1329
- var diff = null;
1330
- if (layerGroup.getNumberOfLayers() !== 1) {
1331
- var firstDataIndex = layerGroup.getViewDataIndices()[0];
1332
- var data0 = dataController.get(firstDataIndex);
1333
- // offset from the top, use first origin
1334
- var origin0 = data0.image.getGeometry().getOrigins()[0];
1335
- var origin1 = imageGeometry.getOrigins()[0];
1336
- diff = origin0.minus(origin1);
1337
- // TODO: check why -z...
1338
- viewLayer.setBaseOffset(new dwv.math.Vector3D(
1339
- diff.getX(), diff.getY(), -1 * diff.getZ()));
1340
- }
1342
+ viewLayer.initialise(size2D, spacing2D, dataIndex, opacity);
1343
+ viewLayer.setScale(layerGroup.getScale());
1344
+ viewLayer.setOffset(layerGroup.getOffset());
1341
1345
 
1342
1346
  // listen to image changes
1343
1347
  dataController.addEventListener('imagechange', viewLayer.onimagechange);
@@ -1351,18 +1355,16 @@ dwv.App = function () {
1351
1355
  dl.initialise(size2D, spacing2D, dataIndex);
1352
1356
  dl.setPlaneHelper(viewLayer.getViewController().getPlaneHelper());
1353
1357
 
1358
+ // force positionchange to sync layers
1354
1359
  var vc = viewLayer.getViewController();
1355
- // positionchange event like data
1356
1360
  var value = [
1357
1361
  vc.getCurrentIndex().getValues(),
1358
1362
  vc.getCurrentPosition().getValues()
1359
1363
  ];
1360
- layerGroup.updateLayersToPositionChange({value: value});
1361
-
1362
- // compensate origin difference if needed
1363
- if (diff) {
1364
- dl.setBaseOffset(diff);
1365
- }
1364
+ layerGroup.updateLayersToPositionChange({
1365
+ value: value,
1366
+ srclayerid: viewLayer.getId()
1367
+ });
1366
1368
  }
1367
1369
 
1368
1370
  // fit to the maximum size
@@ -2619,9 +2621,15 @@ dwv.ctrl.ViewController = function (view) {
2619
2621
  // third dimension player ID (created by setInterval)
2620
2622
  var playerID = null;
2621
2623
 
2624
+ // check view
2625
+ if (typeof view.getImage() === 'undefined') {
2626
+ throw new Error('View does not have an image, cannot setup controller');
2627
+ }
2628
+
2622
2629
  // setup the plane helper
2623
2630
  var planeHelper = new dwv.image.PlaneHelper(
2624
- view.getImage().getGeometry().getSpacing(),
2631
+ view.getImage().getGeometry().getRealSpacing(),
2632
+ view.getImage().getGeometry().getOrientation(),
2625
2633
  view.getOrientation()
2626
2634
  );
2627
2635
 
@@ -2746,6 +2754,10 @@ dwv.ctrl.ViewController = function (view) {
2746
2754
  return view.getCurrentIndex().get(view.getScrollIndex());
2747
2755
  };
2748
2756
 
2757
+ this.getOrigin = function (position) {
2758
+ return view.getOrigin(position);
2759
+ };
2760
+
2749
2761
  /**
2750
2762
  * Get the current scroll position value.
2751
2763
  *
@@ -2891,7 +2903,48 @@ dwv.ctrl.ViewController = function (view) {
2891
2903
  * @returns {dwv.image.Size} The size.
2892
2904
  */
2893
2905
  this.getImageSize = function () {
2894
- return view.getImage().getGeometry().getSize();
2906
+ return view.getImage().getGeometry().getSize(view.getOrientation());
2907
+ };
2908
+
2909
+ /**
2910
+ * Get the image rescaled data range.
2911
+ *
2912
+ * @returns {object} The range as {min, max}.
2913
+ */
2914
+ this.getImageRescaledDataRange = function () {
2915
+ return view.getImage().getRescaledDataRange();
2916
+ };
2917
+
2918
+ /**
2919
+ * Compare the input meta data to the associated image one.
2920
+ *
2921
+ * @param {object} meta The meta data.
2922
+ * @returns {boolean} True if the associated image has equal meta data.
2923
+ */
2924
+ this.equalImageMeta = function (meta) {
2925
+ var imageMeta = view.getImage().getMeta();
2926
+ // loop through input meta keys
2927
+ var metaKeys = Object.keys(meta);
2928
+ for (var i = 0; i < metaKeys.length; ++i) {
2929
+ var metaKey = metaKeys[i];
2930
+ if (typeof imageMeta[metaKey] === 'undefined') {
2931
+ return false;
2932
+ }
2933
+ if (imageMeta[metaKey] !== meta[metaKey]) {
2934
+ return false;
2935
+ }
2936
+ }
2937
+ return true;
2938
+ };
2939
+
2940
+ /**
2941
+ * Check is the provided position can be set.
2942
+ *
2943
+ * @param {dwv.math.Point} position The position.
2944
+ * @returns {boolean} True is the position is in bounds.
2945
+ */
2946
+ this.canSetPosition = function (position) {
2947
+ return view.canSetPosition(position);
2895
2948
  };
2896
2949
 
2897
2950
  /**
@@ -2913,8 +2966,18 @@ dwv.ctrl.ViewController = function (view) {
2913
2966
  * @returns {boolean} False if not in bounds.
2914
2967
  */
2915
2968
  this.setCurrentPosition2D = function (x, y) {
2916
- return view.setCurrentPosition(
2917
- this.getPositionFromPlanePoint({x: x, y: y}));
2969
+ // keep third direction
2970
+ var k = this.getCurrentScrollIndexValue();
2971
+ var planePoint = new dwv.math.Point3D(x, y, k);
2972
+ // de-orient
2973
+ var point = planeHelper.getImageOrientedVector3D(planePoint);
2974
+ // ~indexToWorld to not loose precision
2975
+ var geometry = view.getImage().getGeometry();
2976
+ var point3D = geometry.pointToWorld(point);
2977
+ // merge with current position to keep extra dimensions
2978
+ var position = this.getCurrentPosition().mergeWith3D(point3D);
2979
+
2980
+ return view.setCurrentPosition(position);
2918
2981
  };
2919
2982
 
2920
2983
  /**
@@ -2928,25 +2991,6 @@ dwv.ctrl.ViewController = function (view) {
2928
2991
  return view.setCurrentIndex(index, silent);
2929
2992
  };
2930
2993
 
2931
- /**
2932
- * Get a 3D position from a plane 2D position.
2933
- *
2934
- * @param {dwv.math.Point2D} point2D The 2D position as {x,y}.
2935
- * @returns {dwv.math.Point} The 3D point.
2936
- */
2937
- this.getPositionFromPlanePoint = function (point2D) {
2938
- // keep third direction
2939
- var k = this.getCurrentScrollIndexValue();
2940
- var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2941
- // de-orient
2942
- var point = planeHelper.getDeOrientedVector3D(planePoint);
2943
- // ~indexToWorld to not loose precision
2944
- var geometry = view.getImage().getGeometry();
2945
- var point3D = geometry.pointToWorld(point);
2946
- // merge with current position to keep extra dimensions
2947
- return this.getCurrentPosition().mergeWith3D(point3D);
2948
- };
2949
-
2950
2994
  /**
2951
2995
  * Get a plane 3D position from a plane 2D position: does not compensate
2952
2996
  * for the image origin. Needed for setting the scale center...
@@ -2959,10 +3003,10 @@ dwv.ctrl.ViewController = function (view) {
2959
3003
  var k = this.getCurrentScrollIndexValue();
2960
3004
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2961
3005
  // de-orient
2962
- var point = planeHelper.getDeOrientedVector3D(planePoint);
3006
+ var point = planeHelper.getTargetDeOrientedVector3D(planePoint);
2963
3007
  // ~indexToWorld to not loose precision
2964
3008
  var geometry = view.getImage().getGeometry();
2965
- var spacing = geometry.getSpacing();
3009
+ var spacing = geometry.getRealSpacing();
2966
3010
  return new dwv.math.Point3D(
2967
3011
  point.getX() * spacing.get(0),
2968
3012
  point.getY() * spacing.get(1),
@@ -4506,7 +4550,7 @@ dwv.dicom = dwv.dicom || {};
4506
4550
  * @returns {string} The version of the library.
4507
4551
  */
4508
4552
  dwv.getVersion = function () {
4509
- return '0.31.0-beta.3';
4553
+ return '0.31.0-beta.6';
4510
4554
  };
4511
4555
 
4512
4556
  /**
@@ -6039,10 +6083,24 @@ dwv.dicom.isEven = function (number) {
6039
6083
  * @returns {boolean} True if the VR is a non string one.
6040
6084
  */
6041
6085
  dwv.dicom.isNonStringVr = function (vr) {
6042
- return vr === 'UN' || vr === 'OB' || vr === 'OW' ||
6043
- vr === 'OF' || vr === 'OD' || vr === 'US' || vr === 'SS' ||
6044
- vr === 'UL' || vr === 'SL' || vr === 'FL' || vr === 'FD' ||
6045
- vr === 'SQ' || vr === 'AT';
6086
+ return vr === 'UN' ||
6087
+ vr === 'SQ' ||
6088
+ vr === 'AT' ||
6089
+ dwv.dicom.isTypedArrayVr(vr);
6090
+ };
6091
+
6092
+ /**
6093
+ * Is the input VR a VR that stores data in a typed array.
6094
+ *
6095
+ * @param {string} vr The element VR.
6096
+ * @returns {boolean} True if the VR is a typed array one.
6097
+ */
6098
+ dwv.dicom.isTypedArrayVr = function (vr) {
6099
+ return vr === 'OB' || vr === 'OW' ||
6100
+ vr === 'OF' || vr === 'OD' ||
6101
+ vr === 'US' || vr === 'SS' ||
6102
+ vr === 'UL' || vr === 'SL' ||
6103
+ vr === 'FL' || vr === 'FD';
6046
6104
  };
6047
6105
 
6048
6106
  /**
@@ -6839,43 +6897,55 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
6839
6897
  // set the value and calculate size
6840
6898
  size = 0;
6841
6899
  var paddedValue = dwv.dicom.padElementValue(element, value);
6842
- if (value instanceof Array) {
6900
+ if (element.vr === 'AT') {
6843
6901
  element.value = paddedValue;
6844
- for (var k = 0; k < paddedValue.length; ++k) {
6845
- // spearator
6846
- if (k !== 0) {
6847
- size += 1;
6848
- }
6849
- // value
6850
- size += paddedValue[k].toString().length;
6902
+ size = 4;
6903
+ } else if (dwv.dicom.isTypedArrayVr(element.vr)) {
6904
+ // convert non array (number) value to array
6905
+ if (typeof value.length === 'undefined') {
6906
+ element.value = [paddedValue];
6907
+ } else {
6908
+ element.value = paddedValue;
6909
+ }
6910
+ size = element.value.length;
6911
+
6912
+ // convert size to bytes
6913
+ if (element.vr === 'US' || element.vr === 'OW') {
6914
+ size *= Uint16Array.BYTES_PER_ELEMENT;
6915
+ } else if (element.vr === 'SS') {
6916
+ size *= Int16Array.BYTES_PER_ELEMENT;
6917
+ } else if (element.vr === 'UL') {
6918
+ size *= Uint32Array.BYTES_PER_ELEMENT;
6919
+ } else if (element.vr === 'SL') {
6920
+ size *= Int32Array.BYTES_PER_ELEMENT;
6921
+ } else if (element.vr === 'FL') {
6922
+ size *= Float32Array.BYTES_PER_ELEMENT;
6923
+ } else if (element.vr === 'FD') {
6924
+ size *= Float64Array.BYTES_PER_ELEMENT;
6851
6925
  }
6852
6926
  } else {
6853
- element.value = [paddedValue];
6854
- if (typeof paddedValue !== 'undefined' &&
6855
- typeof paddedValue.length !== 'undefined') {
6856
- size = paddedValue.length;
6927
+ if (value instanceof Array) {
6928
+ element.value = paddedValue;
6929
+ for (var k = 0; k < paddedValue.length; ++k) {
6930
+ // separator
6931
+ if (k !== 0) {
6932
+ size += 1;
6933
+ }
6934
+ // value
6935
+ size += paddedValue[k].toString().length;
6936
+ }
6857
6937
  } else {
6858
- // numbers
6859
- size = 1;
6860
- }
6861
- }
6862
-
6863
- // convert size to bytes
6864
- if (element.vr === 'US' || element.vr === 'OW') {
6865
- size *= Uint16Array.BYTES_PER_ELEMENT;
6866
- } else if (element.vr === 'SS') {
6867
- size *= Int16Array.BYTES_PER_ELEMENT;
6868
- } else if (element.vr === 'UL') {
6869
- size *= Uint32Array.BYTES_PER_ELEMENT;
6870
- } else if (element.vr === 'SL') {
6871
- size *= Int32Array.BYTES_PER_ELEMENT;
6872
- } else if (element.vr === 'FL') {
6873
- size *= Float32Array.BYTES_PER_ELEMENT;
6874
- } else if (element.vr === 'FD') {
6875
- size *= Float64Array.BYTES_PER_ELEMENT;
6876
- } else {
6877
- size *= Uint8Array.BYTES_PER_ELEMENT;
6938
+ element.value = [paddedValue];
6939
+ if (typeof paddedValue !== 'undefined' &&
6940
+ typeof paddedValue.length !== 'undefined') {
6941
+ size = paddedValue.length;
6942
+ } else {
6943
+ // numbers
6944
+ size = 1;
6945
+ }
6946
+ }
6878
6947
  }
6948
+
6879
6949
  element.vl = size;
6880
6950
  }
6881
6951
 
@@ -11419,21 +11489,31 @@ dwv.gui.DrawLayer = function (containerDiv) {
11419
11489
  };
11420
11490
 
11421
11491
  /**
11422
- * Set the base layer offset. Resets the layer offset.
11492
+ * Set the base layer offset. Updates the layer offset.
11423
11493
  *
11424
- * @param {object} off The offset as {x,y}.
11494
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
11495
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
11496
+ * @returns {boolean} True if the offset was updated.
11425
11497
  */
11426
- this.setBaseOffset = function (off) {
11427
- baseOffset = planeHelper.getPlaneOffsetFromOffset3D({
11428
- x: off.getX(),
11429
- y: off.getY(),
11430
- z: off.getZ()
11431
- });
11432
- // reset offset
11433
- konvaStage.offset({
11434
- x: baseOffset.x,
11435
- y: baseOffset.y
11498
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
11499
+ var scrollIndex = planeHelper.getNativeScrollIndex();
11500
+ var newOffset = planeHelper.getPlaneOffsetFromOffset3D({
11501
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
11502
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
11503
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
11436
11504
  });
11505
+ var needsUpdate = baseOffset.x !== newOffset.x ||
11506
+ baseOffset.y !== newOffset.y;
11507
+ // reset offset if needed
11508
+ if (needsUpdate) {
11509
+ var offset = konvaStage.offset();
11510
+ konvaStage.offset({
11511
+ x: offset.x - baseOffset.x + newOffset.x,
11512
+ y: offset.y - baseOffset.y + newOffset.y
11513
+ });
11514
+ baseOffset = newOffset;
11515
+ }
11516
+ return needsUpdate;
11437
11517
  };
11438
11518
 
11439
11519
  /**
@@ -11549,10 +11629,13 @@ dwv.gui.DrawLayer = function (containerDiv) {
11549
11629
  *
11550
11630
  * @param {dwv.math.Point} position The new position.
11551
11631
  * @param {dwv.math.Index} index The new index.
11632
+ * @returns {boolean} True if the position was updated.
11552
11633
  */
11553
11634
  this.setCurrentPosition = function (position, index) {
11554
11635
  this.getDrawController().activateDrawLayer(
11555
11636
  index, planeHelper.getScrollIndex());
11637
+ // TODO: add check
11638
+ return true;
11556
11639
  };
11557
11640
 
11558
11641
  /**
@@ -11841,27 +11924,44 @@ dwv.gui.getLayerDetailsFromEvent = function (event) {
11841
11924
  };
11842
11925
 
11843
11926
  /**
11844
- * Get a view orientation according to an image geometry (with its orientation)
11845
- * and target orientation.
11927
+ * Get the view orientation according to an image and target orientation.
11928
+ * The view orientation is used to go from target to image space.
11846
11929
  *
11847
- * @param {dwv.image.Geometry} imageGeometry The image geometry.
11930
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11848
11931
  * @param {dwv.math.Matrix33} targetOrientation The target orientation.
11849
11932
  * @returns {dwv.math.Matrix33} The view orientation.
11850
11933
  */
11851
- dwv.gui.getViewOrientation = function (imageGeometry, targetOrientation) {
11934
+ dwv.gui.getViewOrientation = function (imageOrientation, targetOrientation) {
11852
11935
  var viewOrientation = dwv.math.getIdentityMat33();
11853
11936
  if (typeof targetOrientation !== 'undefined') {
11854
- // image orientation as one and zeros
11855
- // -> view orientation is one and zeros
11856
- var imgOrientation = imageGeometry.getOrientation().asOneAndZeros();
11857
- // imgOrientation * viewOrientation = targetOrientation
11858
- // -> viewOrientation = inv(imgOrientation) * targetOrientation
11937
+ // i: image, v: view, t: target, O: orientation, P: point
11938
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
11939
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
11940
+ // -> Ov = (Oi)-1 * Ot
11941
+ // TODO: asOneAndZeros simplifies but not nice...
11859
11942
  viewOrientation =
11860
- imgOrientation.getInverse().multiply(targetOrientation);
11943
+ imageOrientation.asOneAndZeros().getInverse().multiply(targetOrientation);
11861
11944
  }
11862
11945
  return viewOrientation;
11863
11946
  };
11864
11947
 
11948
+ /**
11949
+ * Get the target orientation according to an image and view orientation.
11950
+ * The target orientation is used to go from target to real space.
11951
+ *
11952
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11953
+ * @param {dwv.math.Matrix33} viewOrientation The view orientation.
11954
+ * @returns {dwv.math.Matrix33} The target orientation.
11955
+ */
11956
+ dwv.gui.getTargetOrientation = function (imageOrientation, viewOrientation) {
11957
+ // i: image, v: view, t: target, O: orientation, P: point
11958
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
11959
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
11960
+ // -> Ot = Oi * Ov
11961
+ // note: asOneAndZeros as in dwv.gui.getViewOrientation...
11962
+ return imageOrientation.asOneAndZeros().multiply(viewOrientation);
11963
+ };
11964
+
11865
11965
  /**
11866
11966
  * Layer group.
11867
11967
  *
@@ -12056,6 +12156,24 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12056
12156
  return res;
12057
12157
  };
12058
12158
 
12159
+ /**
12160
+ * Search view layers for equal imae meta data.
12161
+ *
12162
+ * @param {object} meta The meta data to find.
12163
+ * @returns {Array} The list of view layers that contain matched data.
12164
+ */
12165
+ this.searchViewLayers = function (meta) {
12166
+ var res = [];
12167
+ for (var i = 0; i < layers.length; ++i) {
12168
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12169
+ if (layers[i].getViewController().equalImageMeta(meta)) {
12170
+ res.push(layers[i]);
12171
+ }
12172
+ }
12173
+ }
12174
+ return res;
12175
+ };
12176
+
12059
12177
  /**
12060
12178
  * Get the view layers data indices.
12061
12179
  *
@@ -12254,10 +12372,53 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12254
12372
 
12255
12373
  var index = new dwv.math.Index(event.value[0]);
12256
12374
  var position = new dwv.math.Point(event.value[1]);
12375
+ // origin of the first view layer
12376
+ var baseViewLayerOrigin0 = null;
12377
+ var baseViewLayerOrigin = null;
12257
12378
  // update position for all layers except the source one
12258
12379
  for (var i = 0; i < layers.length; ++i) {
12380
+
12381
+ // update base offset (does not trigger redraw)
12382
+ // TODO check draw layers update
12383
+ var hasSetOffset = false;
12384
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12385
+ var vc = layers[i].getViewController();
12386
+ // origin0 should always be there
12387
+ var origin0 = vc.getOrigin();
12388
+ // depending on position, origin could be undefined
12389
+ var origin = vc.getOrigin(position);
12390
+
12391
+ if (!baseViewLayerOrigin) {
12392
+ baseViewLayerOrigin0 = origin0;
12393
+ baseViewLayerOrigin = origin;
12394
+ } else {
12395
+ if (vc.canSetPosition(position) &&
12396
+ typeof origin !== 'undefined') {
12397
+ // TODO: compensate for possible different orientation between views
12398
+ // TODO: check why -z...
12399
+
12400
+ var scrollDiff = baseViewLayerOrigin0.minus(origin0);
12401
+ var scrollOffset = new dwv.math.Vector3D(
12402
+ scrollDiff.getX(), scrollDiff.getY(), -1 * scrollDiff.getZ());
12403
+
12404
+ var planeDiff = baseViewLayerOrigin.minus(origin);
12405
+ var planeOffset = new dwv.math.Vector3D(
12406
+ planeDiff.getX(), planeDiff.getY(), -1 * planeDiff.getZ());
12407
+
12408
+ hasSetOffset = layers[i].setBaseOffset(scrollOffset, planeOffset);
12409
+ }
12410
+ }
12411
+ }
12412
+
12413
+ // update position (triggers redraw)
12414
+ var hasSetPos = false;
12259
12415
  if (layers[i].getId() !== event.srclayerid) {
12260
- layers[i].setCurrentPosition(position, index);
12416
+ hasSetPos = layers[i].setCurrentPosition(position, index);
12417
+ }
12418
+
12419
+ // force redraw if needed
12420
+ if (!hasSetPos && hasSetOffset) {
12421
+ layers[i].draw();
12261
12422
  }
12262
12423
  }
12263
12424
 
@@ -13125,12 +13286,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13125
13286
  var context = null;
13126
13287
 
13127
13288
  /**
13128
- * Flag to know if the context has been cleared.
13289
+ * Flag to know if the current position is valid.
13129
13290
  *
13130
13291
  * @private
13131
13292
  * @type {boolean}
13132
13293
  */
13133
- var isContextClear = true;
13294
+ var isValidPosition = true;
13134
13295
 
13135
13296
  /**
13136
13297
  * The image data array.
@@ -13275,6 +13436,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
13275
13436
  // event.value = [index, image]
13276
13437
  if (dataIndex === event.value[0]) {
13277
13438
  viewController.setImage(event.value[1]);
13439
+ setBaseSize(viewController.getImageSize().get2D());
13278
13440
  needsDataUpdate = true;
13279
13441
  }
13280
13442
  };
@@ -13314,6 +13476,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
13314
13476
  * @param {number} alpha The opacity ([0:1] range).
13315
13477
  */
13316
13478
  this.setOpacity = function (alpha) {
13479
+ if (alpha === opacity) {
13480
+ return;
13481
+ }
13482
+
13317
13483
  opacity = Math.min(Math.max(alpha, 0), 1);
13318
13484
 
13319
13485
  /**
@@ -13345,19 +13511,31 @@ dwv.gui.ViewLayer = function (containerDiv) {
13345
13511
  };
13346
13512
 
13347
13513
  /**
13348
- * Set the base layer offset. Resets the layer offset.
13514
+ * Set the base layer offset. Updates the layer offset.
13349
13515
  *
13350
- * @param {object} off The offset as {x,y}.
13516
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
13517
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
13518
+ * @returns {boolean} True if the offset was updated.
13351
13519
  */
13352
- this.setBaseOffset = function (off) {
13520
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
13353
13521
  var helper = viewController.getPlaneHelper();
13354
- baseOffset = helper.getPlaneOffsetFromOffset3D({
13355
- x: off.getX(),
13356
- y: off.getY(),
13357
- z: off.getZ()
13522
+ var scrollIndex = helper.getNativeScrollIndex();
13523
+ var newOffset = helper.getPlaneOffsetFromOffset3D({
13524
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
13525
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
13526
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
13358
13527
  });
13359
- // reset offset
13360
- offset = baseOffset;
13528
+ var needsUpdate = baseOffset.x !== newOffset.x ||
13529
+ baseOffset.y !== newOffset.y;
13530
+ // reset offset if needed
13531
+ if (needsUpdate) {
13532
+ offset = {
13533
+ x: offset.x - baseOffset.x + newOffset.x,
13534
+ y: offset.y - baseOffset.y + newOffset.y
13535
+ };
13536
+ baseOffset = newOffset;
13537
+ }
13538
+ return needsUpdate;
13361
13539
  };
13362
13540
 
13363
13541
  /**
@@ -13459,6 +13637,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13459
13637
  * @fires dwv.App#renderend
13460
13638
  */
13461
13639
  this.draw = function () {
13640
+ // skip for non valid position
13641
+ if (!isValidPosition) {
13642
+ return;
13643
+ }
13644
+
13462
13645
  /**
13463
13646
  * Render start event.
13464
13647
  *
@@ -13468,7 +13651,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13468
13651
  */
13469
13652
  var event = {
13470
13653
  type: 'renderstart',
13471
- layerid: this.getId()
13654
+ layerid: this.getId(),
13655
+ dataid: this.getDataIndex()
13472
13656
  };
13473
13657
  fireEvent(event);
13474
13658
 
@@ -13503,9 +13687,6 @@ dwv.gui.ViewLayer = function (containerDiv) {
13503
13687
  // draw image
13504
13688
  context.drawImage(offscreenCanvas, 0, 0);
13505
13689
 
13506
- // set clear flag
13507
- isContextClear = false;
13508
-
13509
13690
  /**
13510
13691
  * Render end event.
13511
13692
  *
@@ -13515,7 +13696,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13515
13696
  */
13516
13697
  event = {
13517
13698
  type: 'renderend',
13518
- layerid: this.getId()
13699
+ layerid: this.getId(),
13700
+ dataid: this.getDataIndex()
13519
13701
  };
13520
13702
  fireEvent(event);
13521
13703
  };
@@ -13526,12 +13708,13 @@ dwv.gui.ViewLayer = function (containerDiv) {
13526
13708
  * @param {object} size The image size as {x,y}.
13527
13709
  * @param {object} spacing The image spacing as {x,y}.
13528
13710
  * @param {number} index The associated data index.
13711
+ * @param {number} alpha The initial data opacity.
13529
13712
  */
13530
- this.initialise = function (size, spacing, index) {
13713
+ this.initialise = function (size, spacing, index, alpha) {
13531
13714
  // set locals
13532
- baseSize = size;
13533
13715
  baseSpacing = spacing;
13534
13716
  dataIndex = index;
13717
+ opacity = Math.min(Math.max(alpha, 0), 1);
13535
13718
 
13536
13719
  // create canvas
13537
13720
  // (canvas size is set in fitToContainer)
@@ -13550,22 +13733,37 @@ dwv.gui.ViewLayer = function (containerDiv) {
13550
13733
  return;
13551
13734
  }
13552
13735
 
13553
- // check canvas
13554
- if (!dwv.gui.canCreateCanvas(baseSize.x, baseSize.y)) {
13555
- throw new Error('Cannot create canvas ' + baseSize.x + ', ' + baseSize.y);
13736
+ // off screen canvas
13737
+ offscreenCanvas = document.createElement('canvas');
13738
+
13739
+ // set base size: needs an existing context and off screen canvas
13740
+ setBaseSize(size);
13741
+
13742
+ // update data on first draw
13743
+ needsDataUpdate = true;
13744
+ };
13745
+
13746
+ /**
13747
+ * Set the base size of the layer.
13748
+ *
13749
+ * @param {object} size The size as {x,y}.
13750
+ */
13751
+ function setBaseSize(size) {
13752
+ // check canvas creation
13753
+ if (!dwv.gui.canCreateCanvas(size.x, size.y)) {
13754
+ throw new Error('Cannot create canvas ' + size.x + ', ' + size.y);
13556
13755
  }
13557
13756
 
13757
+ // set local
13758
+ baseSize = size;
13759
+
13558
13760
  // off screen canvas
13559
- offscreenCanvas = document.createElement('canvas');
13560
13761
  offscreenCanvas.width = baseSize.x;
13561
13762
  offscreenCanvas.height = baseSize.y;
13562
13763
  // original empty image data array
13563
13764
  context.clearRect(0, 0, baseSize.x, baseSize.y);
13564
13765
  imageData = context.createImageData(baseSize.x, baseSize.y);
13565
-
13566
- // update data on first draw
13567
- needsDataUpdate = true;
13568
- };
13766
+ }
13569
13767
 
13570
13768
  /**
13571
13769
  * Fit the layer to its parent container.
@@ -13574,6 +13772,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13574
13772
  * @param {object} fitSize The fit size as {x,y}.
13575
13773
  */
13576
13774
  this.fitToContainer = function (fitScale1D, fitSize) {
13775
+ // previous scale without fit
13776
+ var previousScale = {
13777
+ x: scale.x / fitScale.x,
13778
+ y: scale.y / fitScale.y
13779
+ };
13577
13780
  // update fit scale
13578
13781
  fitScale = {
13579
13782
  x: fitScale1D * baseSpacing.x,
@@ -13587,8 +13790,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13587
13790
  }
13588
13791
  canvas.width = width;
13589
13792
  canvas.height = height;
13590
- // reset scale
13591
- this.setScale({x: 1, y: 1, z: 1});
13793
+ // reset previous scale with new fit
13794
+ scale = {
13795
+ x: previousScale.x * fitScale.x,
13796
+ y: previousScale.y * fitScale.y
13797
+ };
13592
13798
  };
13593
13799
 
13594
13800
  /**
@@ -13673,8 +13879,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13673
13879
  */
13674
13880
  function onWLChange(event) {
13675
13881
  // generate and draw if no skip flag
13676
- if (typeof event.skipGenerate === 'undefined' ||
13677
- event.skipGenerate === false) {
13882
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13883
+ event.skipGenerate === true;
13884
+ if (!skip) {
13678
13885
  needsDataUpdate = true;
13679
13886
  self.draw();
13680
13887
  }
@@ -13687,8 +13894,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13687
13894
  * @private
13688
13895
  */
13689
13896
  function onColourChange(_event) {
13690
- needsDataUpdate = true;
13691
- self.draw();
13897
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13898
+ event.skipGenerate === true;
13899
+ if (!skip) {
13900
+ needsDataUpdate = true;
13901
+ self.draw();
13902
+ }
13692
13903
  }
13693
13904
 
13694
13905
  /**
@@ -13698,26 +13909,35 @@ dwv.gui.ViewLayer = function (containerDiv) {
13698
13909
  * @private
13699
13910
  */
13700
13911
  function onPositionChange(event) {
13701
- if (typeof event.skipGenerate === 'undefined' ||
13702
- event.skipGenerate === false) {
13912
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13913
+ event.skipGenerate === true;
13914
+ if (!skip) {
13915
+ var valid = typeof event.valid === 'undefined' || event.valid;
13703
13916
  // clear for non valid events
13704
- if (typeof event.valid !== 'undefined' && !event.valid) {
13705
- self.clear();
13706
- return;
13707
- }
13708
- // 3D dimensions
13709
- var dims3D = [0, 1, 2];
13710
- // remove scroll index
13711
- var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13712
- dims3D.splice(indexScrollIndex, 1);
13713
- // remove non scroll index from diff dims
13714
- var diffDims = event.diffDims.filter(function (item) {
13715
- return dims3D.indexOf(item) === -1;
13716
- });
13717
- // update if we have something left
13718
- if (diffDims.length !== 0) {
13719
- needsDataUpdate = true;
13720
- self.draw();
13917
+ if (!valid) {
13918
+ // clear only once
13919
+ if (isValidPosition) {
13920
+ isValidPosition = false;
13921
+ self.clear();
13922
+ }
13923
+ } else {
13924
+ // 3D dimensions
13925
+ var dims3D = [0, 1, 2];
13926
+ // remove scroll index
13927
+ var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13928
+ dims3D.splice(indexScrollIndex, 1);
13929
+ // remove non scroll index from diff dims
13930
+ var diffDims = event.diffDims.filter(function (item) {
13931
+ return dims3D.indexOf(item) === -1;
13932
+ });
13933
+ // update if we have something left
13934
+ if (diffDims.length !== 0 || !isValidPosition) {
13935
+ // reset valid flag
13936
+ isValidPosition = true;
13937
+ // reset update flag
13938
+ needsDataUpdate = true;
13939
+ self.draw();
13940
+ }
13721
13941
  }
13722
13942
  }
13723
13943
  }
@@ -13729,8 +13949,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13729
13949
  * @private
13730
13950
  */
13731
13951
  function onAlphaFuncChange(event) {
13732
- if (typeof event.skipGenerate === 'undefined' ||
13733
- event.skipGenerate === false) {
13952
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13953
+ event.skipGenerate === true;
13954
+ if (!skip) {
13734
13955
  needsDataUpdate = true;
13735
13956
  self.draw();
13736
13957
  }
@@ -13741,28 +13962,24 @@ dwv.gui.ViewLayer = function (containerDiv) {
13741
13962
  *
13742
13963
  * @param {dwv.math.Point} position The new position.
13743
13964
  * @param {dwv.math.Index} _index The new index.
13965
+ * @returns {boolean} True if the position was updated.
13744
13966
  */
13745
13967
  this.setCurrentPosition = function (position, _index) {
13746
- viewController.setCurrentPosition(position);
13968
+ return viewController.setCurrentPosition(position);
13747
13969
  };
13748
13970
 
13749
13971
  /**
13750
13972
  * Clear the context.
13751
13973
  */
13752
13974
  this.clear = function () {
13753
- if (!isContextClear) {
13754
- // clear the context: reset the transform first
13755
- // store the current transformation matrix
13756
- context.save();
13757
- // use the identity matrix while clearing the canvas
13758
- context.setTransform(1, 0, 0, 1, 0, 0);
13759
- context.clearRect(0, 0, canvas.width, canvas.height);
13760
- // restore the transform
13761
- context.restore();
13762
-
13763
- // update clear flag
13764
- isContextClear = true;
13765
- }
13975
+ // clear the context: reset the transform first
13976
+ // store the current transformation matrix
13977
+ context.save();
13978
+ // use the identity matrix while clearing the canvas
13979
+ context.setTransform(1, 0, 0, 1, 0, 0);
13980
+ context.clearRect(0, 0, canvas.width, canvas.height);
13981
+ // restore the transform
13982
+ context.restore();
13766
13983
  };
13767
13984
 
13768
13985
  /**
@@ -15032,6 +15249,22 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15032
15249
  this.getOrigins = function () {
15033
15250
  return origins;
15034
15251
  };
15252
+
15253
+ /**
15254
+ * Check if a point is in the origin list.
15255
+ *
15256
+ * @param {dwv.math.Point3D} point3D The point to check.
15257
+ * @returns {boolean} True if in list.
15258
+ */
15259
+ this.includesOrigin = function (point3D) {
15260
+ for (var i = 0; i < origins.length; ++i) {
15261
+ if (origins[i].isSimilar(point3D, dwv.math.BIG_EPSILON)) {
15262
+ return true;
15263
+ }
15264
+ }
15265
+ return false;
15266
+ };
15267
+
15035
15268
  /**
15036
15269
  * Get the object size.
15037
15270
  * Warning: the size comes as stored in DICOM, meaning that it could
@@ -15050,7 +15283,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15050
15283
  size.get(2)
15051
15284
  ],
15052
15285
  viewOrientation);
15053
- res = new dwv.image.Size(values);
15286
+ res = new dwv.image.Size(values.concat(size.getValues().slice(3)));
15054
15287
  }
15055
15288
  return res;
15056
15289
  };
@@ -15065,26 +15298,26 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15065
15298
  if (origins.length === 1) {
15066
15299
  return 1;
15067
15300
  }
15068
- var spacing = null;
15301
+ var sliceSpacing = null;
15069
15302
  // (x, y, z) = orientationMatrix * (i, j, k)
15070
15303
  // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
15071
15304
  // applied on the patient position, reorders indices
15072
15305
  // so that Z is the slice direction
15073
- var orientation2 = orientation.getInverse().asOneAndZeros();
15306
+ var invOrientation = orientation.getInverse();
15074
15307
  var deltas = [];
15075
15308
  for (var i = 0; i < origins.length - 1; ++i) {
15076
- var origin1 = orientation2.multiplyVector3D(origins[i]);
15077
- var origin2 = orientation2.multiplyVector3D(origins[i + 1]);
15309
+ var origin1 = invOrientation.multiplyVector3D(origins[i]);
15310
+ var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
15078
15311
  var diff = Math.abs(origin1.getZ() - origin2.getZ());
15079
15312
  if (diff === 0) {
15080
15313
  throw new Error('Zero slice spacing.' +
15081
15314
  origin1.toString() + ' ' + origin2.toString());
15082
15315
  }
15083
- if (spacing === null) {
15084
- spacing = diff;
15316
+ if (sliceSpacing === null) {
15317
+ sliceSpacing = diff;
15085
15318
  } else {
15086
- if (!dwv.math.isSimilar(spacing, diff, dwv.math.BIG_EPSILON)) {
15087
- deltas.push(Math.abs(spacing - diff));
15319
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
15320
+ deltas.push(Math.abs(sliceSpacing - diff));
15088
15321
  }
15089
15322
  }
15090
15323
  }
@@ -15099,12 +15332,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15099
15332
  mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
15100
15333
  }
15101
15334
  }
15102
- return spacing;
15335
+
15336
+ return sliceSpacing;
15103
15337
  };
15104
15338
 
15105
15339
  /**
15106
15340
  * Get the object spacing.
15107
- * Warning: the size comes as stored in DICOM, meaning that it could
15341
+ * Warning: the spacing comes as stored in DICOM, meaning that it could
15108
15342
  * be oriented.
15109
15343
  *
15110
15344
  * @param {dwv.math.Matrix33} viewOrientation The view orientation (optional)
@@ -15132,6 +15366,16 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15132
15366
  return res;
15133
15367
  };
15134
15368
 
15369
+ /**
15370
+ * Get the image spacing in real world.
15371
+ *
15372
+ * @returns {dwv.image.Spacing} The object spacing.
15373
+ */
15374
+ this.getRealSpacing = function () {
15375
+ // asOneAndZeros to not change spacing values...
15376
+ return this.getSpacing(orientation.getInverse().asOneAndZeros());
15377
+ };
15378
+
15135
15379
  /**
15136
15380
  * Get the object orientation.
15137
15381
  *
@@ -15181,8 +15425,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15181
15425
  // -> >0 => vectors are codirectional
15182
15426
  // -> <0 => vectors are oposite
15183
15427
  var dotProd = normal.dotProduct(pointDir);
15428
+ var test = dotProd < 0;
15429
+ // TODO: check why coronal behaves differently...
15430
+ if (orientation.getThirdColMajorDirection() === 1) {
15431
+ test = dotProd > 0;
15432
+ }
15184
15433
  // oposite vectors get higher index
15185
- var sliceIndex = (dotProd < 0) ? closestSliceIndex + 1 : closestSliceIndex;
15434
+ var sliceIndex = test ? closestSliceIndex + 1 : closestSliceIndex;
15186
15435
  return sliceIndex;
15187
15436
  };
15188
15437
 
@@ -15260,10 +15509,11 @@ dwv.image.Geometry.prototype.isInBounds = function (point) {
15260
15509
  * Check that a index is within bounds.
15261
15510
  *
15262
15511
  * @param {dwv.math.Index} index The index to check.
15512
+ * @param {number} dir Optional direction to check.
15263
15513
  * @returns {boolean} True if the given coordinates are within bounds.
15264
15514
  */
15265
- dwv.image.Geometry.prototype.isIndexInBounds = function (index) {
15266
- return this.getSize().isInBounds(index);
15515
+ dwv.image.Geometry.prototype.isIndexInBounds = function (index, dir) {
15516
+ return this.getSize().isInBounds(index, dir);
15267
15517
  };
15268
15518
 
15269
15519
  /**
@@ -15343,6 +15593,7 @@ dwv.image.Geometry.prototype.pointToWorld = function (point) {
15343
15593
  dwv.image.Geometry.prototype.worldToIndex = function (point) {
15344
15594
  // compensate for origin
15345
15595
  // (origin is not oriented, compensate before orientation)
15596
+ // TODO: use slice origin...
15346
15597
  var origin = this.getOrigin();
15347
15598
  var point3D = new dwv.math.Point3D(
15348
15599
  point.get(0) - origin.getX(),
@@ -15359,9 +15610,11 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
15359
15610
  values[0] = Math.round(orientedPoint3D.getX() / spacing.get(0));
15360
15611
  values[1] = Math.round(orientedPoint3D.getY() / spacing.get(1));
15361
15612
  // flip K index (because of the slice order given by getSliceIndex)
15362
- values[2] = flipK(this.getSize(),
15363
- Math.round(orientedPoint3D.getZ() / spacing.get(2))
15364
- );
15613
+ var k = Math.round(orientedPoint3D.getZ() / spacing.get(2));
15614
+ // abs to fix #1163
15615
+ // TODO: find out why k can sometimes be negative...
15616
+ values[2] = flipK(this.getSize(), k);
15617
+
15365
15618
  // return index
15366
15619
  return new dwv.math.Index(values);
15367
15620
  };
@@ -15800,7 +16053,7 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15800
16053
  }
15801
16054
  // all meta should be equal
15802
16055
  for (var key in meta) {
15803
- if (key === 'windowPresets') {
16056
+ if (key === 'windowPresets' || key === 'numberOfFiles') {
15804
16057
  continue;
15805
16058
  }
15806
16059
  if (meta[key] !== rhs.getMeta()[key]) {
@@ -15831,8 +16084,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15831
16084
  values[3] = timeId;
15832
16085
  }
15833
16086
  var index = new dwv.math.Index(values);
15834
- var primaryOffset = size.indexToOffset(index);
15835
- var secondaryOffset = this.getSecondaryOffset(index);
16087
+ var primaryOffset = size.indexToOffset(index) * numberOfComponents;
16088
+ var secondaryOffset = this.getSecondaryOffset(index) * numberOfComponents;
15836
16089
 
15837
16090
  // first frame special slice by slice append
15838
16091
  if (timeId === 0) {
@@ -17810,7 +18063,7 @@ dwv.image.MaskFactory.prototype.create = function (
17810
18063
  }
17811
18064
  // create frame info object from per frame func
17812
18065
  var frameInfos = [];
17813
- var posPats = [];
18066
+ var framePosPats = [];
17814
18067
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
17815
18068
  var frameFunc = perFrameFuncGroupSequence[j];
17816
18069
  // Derivation Image Sequence
@@ -17837,8 +18090,8 @@ dwv.image.MaskFactory.prototype.create = function (
17837
18090
  for (var p = 0; p < imagePosPat.length; ++p) {
17838
18091
  imagePosPat[p] = parseFloat(imagePosPat[p], 10);
17839
18092
  }
17840
- if (!includesPosPat(posPats, imagePosPat)) {
17841
- posPats.push(imagePosPat);
18093
+ if (!includesPosPat(framePosPats, imagePosPat)) {
18094
+ framePosPats.push(imagePosPat);
17842
18095
  }
17843
18096
  frameInfos.push({
17844
18097
  dimIndex: dimIndex,
@@ -17882,7 +18135,22 @@ dwv.image.MaskFactory.prototype.create = function (
17882
18135
  }
17883
18136
  }
17884
18137
  // sort positions patient
17885
- posPats.sort(comparePosPat);
18138
+ framePosPats.sort(comparePosPat);
18139
+
18140
+ // add missing posPats
18141
+ var posPats = [];
18142
+ var sliceSpacing = spacing.get(2);
18143
+ for (var g = 0; g < framePosPats.length - 1; ++g) {
18144
+ posPats.push(framePosPats[g]);
18145
+ var nextZ = framePosPats[g][2] - sliceSpacing;
18146
+ var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18147
+ while (diff > sliceSpacing) {
18148
+ posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
18149
+ nextZ -= sliceSpacing;
18150
+ diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18151
+ }
18152
+ }
18153
+ posPats.push(framePosPats[framePosPats.length - 1]);
17886
18154
 
17887
18155
  // create output buffer
17888
18156
  // as many slices as posPats -> gap slices between groups are not represented
@@ -17967,9 +18235,13 @@ dwv.image = dwv.image || {};
17967
18235
  *
17968
18236
  * @class
17969
18237
  * @param {dwv.image.Spacing} spacing The spacing.
17970
- * @param {dwv.math.Matrix} orientation The orientation.
18238
+ * @param {dwv.math.Matrix} imageOrientation The image oientation.
18239
+ * @param {dwv.math.Matrix} viewOrientation The view orientation.
17971
18240
  */
17972
- dwv.image.PlaneHelper = function (spacing, orientation) {
18241
+ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18242
+
18243
+ var targetOrientation = dwv.gui.getTargetOrientation(
18244
+ imageOrientation, viewOrientation);
17973
18245
 
17974
18246
  /**
17975
18247
  * Get a 3D offset from a plane one.
@@ -17982,22 +18254,28 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
17982
18254
  var planeOffset = new dwv.math.Vector3D(
17983
18255
  offset2D.x, offset2D.y, 0);
17984
18256
  // de-orient
17985
- var pixelOffset = this.getDeOrientedVector3D(planeOffset);
17986
- // offset indexToWorld
17987
- return offsetIndexToWorld(pixelOffset);
18257
+ var pixelOffset = this.getTargetDeOrientedVector3D(planeOffset);
18258
+ // ~indexToWorld
18259
+ return new dwv.math.Vector3D(
18260
+ pixelOffset.getX() * spacing.get(0),
18261
+ pixelOffset.getY() * spacing.get(1),
18262
+ pixelOffset.getZ() * spacing.get(2));
17988
18263
  };
17989
18264
 
17990
18265
  /**
17991
18266
  * Get a plane offset from a 3D one.
17992
18267
  *
17993
- * @param {dwv.math.Point3D} offset3D The 3D offset.
18268
+ * @param {object} offset3D The 3D offset as {x,y,z}.
17994
18269
  * @returns {object} The plane offset as {x,y}.
17995
18270
  */
17996
18271
  this.getPlaneOffsetFromOffset3D = function (offset3D) {
17997
- // offset worldToIndex
17998
- var pixelOffset = offsetWorldToIndex(offset3D);
18272
+ // ~worldToIndex
18273
+ var pixelOffset = new dwv.math.Vector3D(
18274
+ offset3D.x / spacing.get(0),
18275
+ offset3D.y / spacing.get(1),
18276
+ offset3D.z / spacing.get(2));
17999
18277
  // orient
18000
- var planeOffset = this.getOrientedVector3D(pixelOffset);
18278
+ var planeOffset = this.getTargetOrientedVector3D(pixelOffset);
18001
18279
  // make 2D
18002
18280
  return {
18003
18281
  x: planeOffset.getX(),
@@ -18006,99 +18284,51 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18006
18284
  };
18007
18285
 
18008
18286
  /**
18009
- * Apply spacing to an offset.
18010
- *
18011
- * @param {dwv.math.Point3D} off The 3D offset.
18012
- * @returns {dwv.math.Vector3D} The world offset.
18013
- */
18014
- function offsetIndexToWorld(off) {
18015
- return new dwv.math.Vector3D(
18016
- off.getX() * spacing.get(0),
18017
- off.getY() * spacing.get(1),
18018
- off.getZ() * spacing.get(2));
18019
- }
18020
-
18021
- /**
18022
- * Remove spacing from an offset.
18023
- *
18024
- * @param {object} off The world offset object as {x,y,z}.
18025
- * @returns {dwv.math.Vector3D} The 3D offset.
18026
- */
18027
- function offsetWorldToIndex(off) {
18028
- return new dwv.math.Vector3D(
18029
- off.x / spacing.get(0),
18030
- off.y / spacing.get(1),
18031
- off.z / spacing.get(2));
18032
- }
18033
-
18034
- /**
18035
- * Orient an input vector.
18287
+ * Orient an input vector from real to target space.
18036
18288
  *
18037
18289
  * @param {dwv.math.Vector3D} vector The input vector.
18038
18290
  * @returns {dwv.math.Vector3D} The oriented vector.
18039
18291
  */
18040
- this.getOrientedVector3D = function (vector) {
18292
+ this.getTargetOrientedVector3D = function (vector) {
18041
18293
  var planeVector = vector;
18042
- if (typeof orientation !== 'undefined') {
18043
- // abs? otherwise negative index...
18044
- // vector = orientation * planeVector
18045
- planeVector = orientation.getInverse().getAbs().multiplyVector3D(vector);
18294
+ if (typeof targetOrientation !== 'undefined') {
18295
+ planeVector = targetOrientation.getInverse().multiplyVector3D(vector);
18046
18296
  }
18047
18297
  return planeVector;
18048
18298
  };
18049
18299
 
18050
18300
  /**
18051
- * Orient an input index.
18052
- *
18053
- * @param {dwv.math.Index} index The input index.
18054
- * @returns {dwv.math.Index} The oriented index.
18055
- */
18056
- this.getOrientedIndex = function (index) {
18057
- var planeIndex = index;
18058
- if (typeof orientation !== 'undefined') {
18059
- // abs? otherwise negative index...
18060
- // vector = orientation * planeVector
18061
- planeIndex = orientation.getInverse().getAbs().multiplyIndex3D(index);
18062
- }
18063
- return planeIndex;
18064
- };
18065
-
18066
- /**
18067
- * Orient an input point.
18301
+ * De-orient an input vector from target to real space.
18068
18302
  *
18069
- * @param {dwv.math.Point3D} point The input point.
18070
- * @returns {dwv.math.Point3D} The oriented point.
18303
+ * @param {dwv.math.Vector3D} planeVector The input vector.
18304
+ * @returns {dwv.math.Vector3D} The de-orienteded vector.
18071
18305
  */
18072
- this.getOrientedPoint = function (point) {
18073
- var planePoint = point;
18074
- if (typeof orientation !== 'undefined') {
18075
- // abs? otherwise negative index...
18076
- // vector = orientation * planeVector
18077
- var point3D =
18078
- orientation.getInverse().getAbs().multiplyPoint3D(point.get3D());
18079
- planePoint = point.mergeWith3D(point3D);
18306
+ this.getTargetDeOrientedVector3D = function (planeVector) {
18307
+ var vector = planeVector;
18308
+ if (typeof targetOrientation !== 'undefined') {
18309
+ vector = targetOrientation.multiplyVector3D(planeVector);
18080
18310
  }
18081
- return planePoint;
18311
+ return vector;
18082
18312
  };
18083
18313
 
18084
18314
  /**
18085
- * De-orient an input vector.
18315
+ * Orient an input vector from target to image space.
18086
18316
  *
18087
18317
  * @param {dwv.math.Vector3D} planeVector The input vector.
18088
- * @returns {dwv.math.Vector3D} The de-orienteded vector.
18318
+ * @returns {dwv.math.Vector3D} The orienteded vector.
18089
18319
  */
18090
- this.getDeOrientedVector3D = function (planeVector) {
18320
+ this.getImageOrientedVector3D = function (planeVector) {
18091
18321
  var vector = planeVector;
18092
- if (typeof orientation !== 'undefined') {
18322
+ if (typeof viewOrientation !== 'undefined') {
18093
18323
  // abs? otherwise negative index...
18094
- // vector = orientation * planePoint
18095
- vector = orientation.getAbs().multiplyVector3D(planeVector);
18324
+ // vector = viewOrientation * planePoint
18325
+ vector = viewOrientation.getAbs().multiplyVector3D(planeVector);
18096
18326
  }
18097
18327
  return vector;
18098
18328
  };
18099
18329
 
18100
18330
  /**
18101
- * Reorder values to follow orientation.
18331
+ * Reorder values to follow view orientation.
18102
18332
  *
18103
18333
  * @param {object} values Values as {x,y,z}.
18104
18334
  * @returns {object} Reoriented values as {x,y,z}.
@@ -18110,7 +18340,7 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18110
18340
  values.y,
18111
18341
  values.z
18112
18342
  ],
18113
- orientation);
18343
+ viewOrientation);
18114
18344
  return {
18115
18345
  x: orientedValues[0],
18116
18346
  y: orientedValues[1],
@@ -18119,36 +18349,29 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18119
18349
  };
18120
18350
 
18121
18351
  /**
18122
- * Reorder values to compensate for orientation.
18352
+ * Get the (view) scroll dimension index.
18123
18353
  *
18124
- * @param {object} values Values as {x,y,z}.
18125
- * @returns {object} 'Deoriented' values as {x,y,z}.
18354
+ * @returns {number} The index.
18126
18355
  */
18127
- this.getDeOrientedXYZ = function (values) {
18128
- var deOrientedValues = dwv.math.getDeOrientedArray3D(
18129
- [
18130
- values.x,
18131
- values.y,
18132
- values.z
18133
- ],
18134
- orientation
18135
- );
18136
- return {
18137
- x: deOrientedValues[0],
18138
- y: deOrientedValues[1],
18139
- z: deOrientedValues[2]
18140
- };
18356
+ this.getScrollIndex = function () {
18357
+ var index = null;
18358
+ if (typeof viewOrientation !== 'undefined') {
18359
+ index = viewOrientation.getThirdColMajorDirection();
18360
+ } else {
18361
+ index = 2;
18362
+ }
18363
+ return index;
18141
18364
  };
18142
18365
 
18143
18366
  /**
18144
- * Get the scroll dimension index.
18367
+ * Get the native (image) scroll dimension index.
18145
18368
  *
18146
18369
  * @returns {number} The index.
18147
18370
  */
18148
- this.getScrollIndex = function () {
18371
+ this.getNativeScrollIndex = function () {
18149
18372
  var index = null;
18150
- if (typeof orientation !== 'undefined') {
18151
- index = orientation.getThirdColMajorDirection();
18373
+ if (typeof imageOrientation !== 'undefined') {
18374
+ index = imageOrientation.getThirdColMajorDirection();
18152
18375
  } else {
18153
18376
  index = 2;
18154
18377
  }
@@ -18500,9 +18723,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
18500
18723
  * Check that an index is within bounds.
18501
18724
  *
18502
18725
  * @param {dwv.math.Index} index The index to check.
18726
+ * @param {number} dir Optional direction to check.
18503
18727
  * @returns {boolean} True if the given coordinates are within bounds.
18504
18728
  */
18505
- dwv.image.Size.prototype.isInBounds = function (index) {
18729
+ dwv.image.Size.prototype.isInBounds = function (index, dir) {
18506
18730
  // check input
18507
18731
  if (!index) {
18508
18732
  return false;
@@ -18512,11 +18736,20 @@ dwv.image.Size.prototype.isInBounds = function (index) {
18512
18736
  if (length !== index.length()) {
18513
18737
  return false;
18514
18738
  }
18515
- // check values
18516
- for (var i = 0; i < length; ++i) {
18517
- if (index.get(i) < 0 || index.get(i) > this.get(i) - 1) {
18739
+ // check values is 0 <= v < size
18740
+ var inBound = function (value, size) {
18741
+ return value >= 0 && value < size;
18742
+ };
18743
+ if (typeof dir !== 'undefined') {
18744
+ if (!inBound(index.get(dir), this.get(dir))) {
18518
18745
  return false;
18519
18746
  }
18747
+ } else {
18748
+ for (var i = 0; i < length; ++i) {
18749
+ if (!inBound(index.get(i), this.get(i))) {
18750
+ return false;
18751
+ }
18752
+ }
18520
18753
  }
18521
18754
  // seems ok!
18522
18755
  return true;
@@ -18767,11 +19000,12 @@ dwv.image.View = function (image) {
18767
19000
  var colourMap = dwv.image.lut.plain;
18768
19001
  /**
18769
19002
  * Current position as a Point3D.
19003
+ * Store position and not index to stay geometry independent.
18770
19004
  *
18771
19005
  * @private
18772
19006
  * @type {dwv.math.Index}
18773
19007
  */
18774
- var currentIndex = null;
19008
+ var currentPosition = null;
18775
19009
  /**
18776
19010
  * View orientation. Undefined will use the original slice ordering.
18777
19011
  *
@@ -18909,6 +19143,7 @@ dwv.image.View = function (image) {
18909
19143
  if (!this.getCurrentIndex()) {
18910
19144
  this.setInitialIndex();
18911
19145
  }
19146
+ var currentIndex = this.getCurrentIndex();
18912
19147
  // use current rsi if not provided
18913
19148
  if (typeof rsi === 'undefined') {
18914
19149
  rsi = image.getRescaleSlopeAndIntercept(currentIndex);
@@ -19091,8 +19326,7 @@ dwv.image.View = function (image) {
19091
19326
  * @returns {dwv.math.Point} The current position.
19092
19327
  */
19093
19328
  this.getCurrentPosition = function () {
19094
- var geometry = this.getImage().getGeometry();
19095
- return geometry.indexToWorld(currentIndex);
19329
+ return currentPosition;
19096
19330
  };
19097
19331
 
19098
19332
  /**
@@ -19101,7 +19335,41 @@ dwv.image.View = function (image) {
19101
19335
  * @returns {dwv.math.Index} The current index.
19102
19336
  */
19103
19337
  this.getCurrentIndex = function () {
19104
- return currentIndex;
19338
+ var position = this.getCurrentPosition();
19339
+ if (!position) {
19340
+ return null;
19341
+ }
19342
+ var geometry = this.getImage().getGeometry();
19343
+ return geometry.worldToIndex(position);
19344
+ };
19345
+
19346
+ /**
19347
+ * Check is the provided position can be set.
19348
+ *
19349
+ * @param {dwv.math.Point} position The position.
19350
+ * @returns {boolean} True is the position is in bounds.
19351
+ */
19352
+ this.canSetPosition = function (position) {
19353
+ var geometry = image.getGeometry();
19354
+ var index = geometry.worldToIndex(position);
19355
+ return geometry.isIndexInBounds(index, this.getScrollIndex());
19356
+ };
19357
+
19358
+ /**
19359
+ * Get the origin at a given position.
19360
+ *
19361
+ * @param {dwv.math.Point} position The position.
19362
+ * @returns {dwv.math.Point} The origin.
19363
+ */
19364
+ this.getOrigin = function (position) {
19365
+ var geometry = image.getGeometry();
19366
+ var originIndex = 0;
19367
+ if (typeof position !== 'undefined') {
19368
+ var index = geometry.worldToIndex(position);
19369
+ // index is reoriented, 2 is scroll index
19370
+ originIndex = index.get(2);
19371
+ }
19372
+ return geometry.getOrigins()[originIndex];
19105
19373
  };
19106
19374
 
19107
19375
  /**
@@ -19116,7 +19384,7 @@ dwv.image.View = function (image) {
19116
19384
  // send invalid event if not in bounds
19117
19385
  var geometry = image.getGeometry();
19118
19386
  var index = geometry.worldToIndex(position);
19119
- if (!geometry.isIndexInBounds(index)) {
19387
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19120
19388
  if (!silent) {
19121
19389
  // fire event with valid: false
19122
19390
  fireEvent({
@@ -19138,7 +19406,7 @@ dwv.image.View = function (image) {
19138
19406
  *
19139
19407
  * @param {dwv.math.Index} index The new index.
19140
19408
  * @param {boolean} silent Flag to fire event or not.
19141
- * @returns {boolean} False if not in bounds
19409
+ * @returns {boolean} False if not in bounds.
19142
19410
  * @fires dwv.image.View#positionchange
19143
19411
  */
19144
19412
  this.setCurrentIndex = function (index, silent) {
@@ -19151,70 +19419,72 @@ dwv.image.View = function (image) {
19151
19419
  var position = geometry.indexToWorld(index);
19152
19420
 
19153
19421
  // check if possible
19154
- if (!geometry.isIndexInBounds(index)) {
19422
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19423
+ // do no send invalid positionchange event: avoid empty repaint
19155
19424
  return false;
19156
19425
  }
19157
19426
 
19158
- var isNew = !currentIndex || !currentIndex.equals(index);
19159
-
19160
- if (isNew) {
19161
- var diffDims = null;
19162
- if (currentIndex) {
19163
- if (currentIndex.canCompare(index)) {
19164
- diffDims = currentIndex.compare(index);
19165
- } else {
19166
- diffDims = [];
19167
- var minLen = Math.min(currentIndex.length(), index.length());
19168
- for (var i = 0; i < minLen; ++i) {
19169
- if (currentIndex.get(i) !== index.get(i)) {
19170
- diffDims.push(i);
19171
- }
19172
- }
19173
- var maxLen = Math.max(currentIndex.length(), index.length());
19174
- for (var j = minLen; j < maxLen; ++j) {
19175
- diffDims.push(j);
19176
- }
19177
- }
19427
+ // calculate diff dims before updating internal
19428
+ var diffDims = null;
19429
+ var currentIndex = null;
19430
+ if (this.getCurrentPosition()) {
19431
+ currentIndex = this.getCurrentIndex();
19432
+ }
19433
+ if (currentIndex) {
19434
+ if (currentIndex.canCompare(index)) {
19435
+ diffDims = currentIndex.compare(index);
19178
19436
  } else {
19179
19437
  diffDims = [];
19180
- for (var k = 0; k < index.length(); ++k) {
19181
- diffDims.push(k);
19438
+ var minLen = Math.min(currentIndex.length(), index.length());
19439
+ for (var i = 0; i < minLen; ++i) {
19440
+ if (currentIndex.get(i) !== index.get(i)) {
19441
+ diffDims.push(i);
19442
+ }
19443
+ }
19444
+ var maxLen = Math.max(currentIndex.length(), index.length());
19445
+ for (var j = minLen; j < maxLen; ++j) {
19446
+ diffDims.push(j);
19182
19447
  }
19183
19448
  }
19449
+ } else {
19450
+ diffDims = [];
19451
+ for (var k = 0; k < index.length(); ++k) {
19452
+ diffDims.push(k);
19453
+ }
19454
+ }
19184
19455
 
19185
- // assign
19186
- currentIndex = index;
19456
+ // assign
19457
+ currentPosition = position;
19187
19458
 
19188
- if (!silent) {
19189
- /**
19190
- * Position change event.
19191
- *
19192
- * @event dwv.image.View#positionchange
19193
- * @type {object}
19194
- * @property {Array} value The changed value as [index, pixelValue].
19195
- * @property {Array} diffDims An array of modified indices.
19196
- */
19197
- var posEvent = {
19198
- type: 'positionchange',
19199
- value: [
19200
- index.getValues(),
19201
- position.getValues(),
19202
- ],
19203
- diffDims: diffDims,
19204
- data: {
19205
- imageUid: image.getImageUid(index)
19206
- }
19207
- };
19208
-
19209
- // add value if possible
19210
- if (image.canQuantify()) {
19211
- var pixValue = image.getRescaledValueAtIndex(index);
19212
- posEvent.value.push(pixValue);
19459
+ if (!silent) {
19460
+ /**
19461
+ * Position change event.
19462
+ *
19463
+ * @event dwv.image.View#positionchange
19464
+ * @type {object}
19465
+ * @property {Array} value The changed value as [index, pixelValue].
19466
+ * @property {Array} diffDims An array of modified indices.
19467
+ */
19468
+ var posEvent = {
19469
+ type: 'positionchange',
19470
+ value: [
19471
+ index.getValues(),
19472
+ position.getValues(),
19473
+ ],
19474
+ diffDims: diffDims,
19475
+ data: {
19476
+ imageUid: image.getImageUid(index)
19213
19477
  }
19478
+ };
19214
19479
 
19215
- // fire
19216
- fireEvent(posEvent);
19480
+ // add value if possible
19481
+ if (image.canQuantify()) {
19482
+ var pixValue = image.getRescaledValueAtIndex(index);
19483
+ posEvent.value.push(pixValue);
19217
19484
  }
19485
+
19486
+ // fire
19487
+ fireEvent(posEvent);
19218
19488
  }
19219
19489
 
19220
19490
  // all good
@@ -19380,7 +19650,7 @@ dwv.image.View.prototype.getWindowLevelMinMax = function () {
19380
19650
  var width = max - min;
19381
19651
  // full black / white images, defaults to 1.
19382
19652
  if (width < 1) {
19383
- dwv.logger.warn('Zero or negative width, defaulting to one.');
19653
+ dwv.logger.warn('Zero or negative window width, defaulting to one.');
19384
19654
  width = 1;
19385
19655
  }
19386
19656
  var center = min + width / 2;
@@ -23717,6 +23987,60 @@ dwv.math.Ellipse.prototype.quantify = function (viewController, flags) {
23717
23987
  return quant;
23718
23988
  };
23719
23989
 
23990
+ /**
23991
+ * Get the indices that form a ellpise.
23992
+ *
23993
+ * @param {dwv.math.Index} center The ellipse center.
23994
+ * @param {Array} radius The 2 ellipse radiuses.
23995
+ * @param {Array} dir The 2 ellipse directions.
23996
+ * @returns {Array} The indices of the ellipse.
23997
+ */
23998
+ dwv.math.getEllipseIndices = function (center, radius, dir) {
23999
+ var centerValues = center.getValues();
24000
+ // keep all values for possible extra dimensions
24001
+ var values = centerValues.slice();
24002
+ var indices = [];
24003
+ var radiusI = radius[0];
24004
+ var radiusJ = radius[1];
24005
+ var radiusRatio = radiusI / radiusJ;
24006
+ var radiusJ2 = Math.pow(radiusJ, 2);
24007
+ var di = dir[0];
24008
+ var dj = dir[1];
24009
+ // deduce 4 positions from top right
24010
+ for (var j = 0; j < radiusJ; ++j) {
24011
+ // right triangle formed by radiuses, j and len
24012
+ // ellipse: i*i / a*a + j*j / b*b = 1
24013
+ // -> i = a/b * sqrt(b*b - j*j)
24014
+ var len = Math.round(
24015
+ radiusRatio * Math.sqrt(radiusJ2 - Math.pow(j, 2)));
24016
+ var jmax = centerValues[dj] + j;
24017
+ var jmin = centerValues[dj] - j;
24018
+ for (var i = 0; i < len; ++i) {
24019
+ var imax = centerValues[di] + i;
24020
+ var imin = centerValues[di] - i;
24021
+
24022
+ // right
24023
+ values[di] = imax;
24024
+ // right - top
24025
+ values[dj] = jmax;
24026
+ indices.push(new dwv.math.Index(values.slice()));
24027
+ // right - bottom
24028
+ values[dj] = jmin;
24029
+ indices.push(new dwv.math.Index(values.slice()));
24030
+
24031
+ // left
24032
+ values[di] = imin;
24033
+ // left - top
24034
+ values[dj] = jmax;
24035
+ indices.push(new dwv.math.Index(values.slice()));
24036
+ // left - bottom
24037
+ values[dj] = jmin;
24038
+ indices.push(new dwv.math.Index(values.slice()));
24039
+ }
24040
+ }
24041
+ return indices;
24042
+ };
24043
+
23720
24044
  // namespaces
23721
24045
  var dwv = dwv || {};
23722
24046
  dwv.math = dwv.math || {};
@@ -23890,6 +24214,21 @@ dwv.math.getZeroIndex = function (size) {
23890
24214
  return new dwv.math.Index(values);
23891
24215
  };
23892
24216
 
24217
+ /**
24218
+ * Get an array sort callback.
24219
+ * f(a,b) > 0 -> b,a
24220
+ * f(a,b) < 0 -> a,b
24221
+ * f(a,b) = 0 -> original order
24222
+ *
24223
+ * @param {number} direction The direction to use to compare indices.
24224
+ * @returns {Function} A function that compares two dwv.math.Index.
24225
+ */
24226
+ dwv.math.getIndexCompareFunction = function (direction) {
24227
+ return function (a, b) {
24228
+ return a.get(direction) - b.get(direction);
24229
+ };
24230
+ };
24231
+
23893
24232
  /**
23894
24233
  * Get a string id from the index values in the form of: '#0-1_#1-2'.
23895
24234
  *
@@ -24219,6 +24558,9 @@ dwv.math.isSimilar = function (a, b, tol) {
24219
24558
  * @class
24220
24559
  */
24221
24560
  dwv.math.Matrix33 = function (values) {
24561
+ // matrix inverse, calculated at first ask
24562
+ var inverse = null;
24563
+
24222
24564
  /**
24223
24565
  * Get a value of the matrix.
24224
24566
  *
@@ -24229,6 +24571,20 @@ dwv.math.Matrix33 = function (values) {
24229
24571
  this.get = function (row, col) {
24230
24572
  return values[row * 3 + col];
24231
24573
  };
24574
+
24575
+ /**
24576
+ * Get the inverse of this matrix.
24577
+ *
24578
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24579
+ * if the determinant is zero.
24580
+ */
24581
+ this.getInverse = function () {
24582
+ if (inverse === null) {
24583
+ inverse = dwv.math.getMatrixInverse(this);
24584
+ }
24585
+ return inverse;
24586
+ };
24587
+
24232
24588
  }; // Matrix33
24233
24589
 
24234
24590
  /**
@@ -24240,15 +24596,16 @@ dwv.math.Matrix33 = function (values) {
24240
24596
  * @returns {boolean} True if both matrices are equal.
24241
24597
  */
24242
24598
  dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24243
- return dwv.math.isSimilar(this.get(0, 0), rhs.get(0, 0), p) &&
24244
- dwv.math.isSimilar(this.get(0, 1), rhs.get(0, 1), p) &&
24245
- dwv.math.isSimilar(this.get(0, 2), rhs.get(0, 2), p) &&
24246
- dwv.math.isSimilar(this.get(1, 0), rhs.get(1, 0), p) &&
24247
- dwv.math.isSimilar(this.get(1, 1), rhs.get(1, 1), p) &&
24248
- dwv.math.isSimilar(this.get(1, 2), rhs.get(1, 2), p) &&
24249
- dwv.math.isSimilar(this.get(2, 0), rhs.get(2, 0), p) &&
24250
- dwv.math.isSimilar(this.get(2, 1), rhs.get(2, 1), p) &&
24251
- dwv.math.isSimilar(this.get(2, 2), rhs.get(2, 2), p);
24599
+ // TODO: add type check
24600
+ // check values
24601
+ for (var i = 0; i < 3; ++i) {
24602
+ for (var j = 0; j < 3; ++j) {
24603
+ if (!dwv.math.isSimilar(this.get(i, j), rhs.get(i, j), p)) {
24604
+ return false;
24605
+ }
24606
+ }
24607
+ }
24608
+ return true;
24252
24609
  };
24253
24610
 
24254
24611
  /**
@@ -24257,10 +24614,20 @@ dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24257
24614
  * @returns {string} The matrix as a string.
24258
24615
  */
24259
24616
  dwv.math.Matrix33.prototype.toString = function () {
24260
- return '[' + this.get(0, 0) + ', ' + this.get(0, 1) + ', ' + this.get(0, 2) +
24261
- '\n ' + this.get(1, 0) + ', ' + this.get(1, 1) + ', ' + this.get(1, 2) +
24262
- '\n ' + this.get(2, 0) + ', ' + this.get(2, 1) + ', ' + this.get(2, 2) +
24263
- ']';
24617
+ var str = '[';
24618
+ for (var i = 0; i < 3; ++i) {
24619
+ if (i !== 0) {
24620
+ str += ', \n ';
24621
+ }
24622
+ for (var j = 0; j < 3; ++j) {
24623
+ if (j !== 0) {
24624
+ str += ', ';
24625
+ }
24626
+ str += this.get(i, j);
24627
+ }
24628
+ }
24629
+ str += ']';
24630
+ return str;
24264
24631
  };
24265
24632
 
24266
24633
  /**
@@ -24358,42 +24725,46 @@ dwv.math.Matrix33.prototype.multiplyIndex3D = function (index3D) {
24358
24725
  };
24359
24726
 
24360
24727
  /**
24361
- * Get the inverse of this matrix.
24728
+ * Get the inverse of an input 3*3 matrix.
24362
24729
  *
24363
- * @returns {dwv.math.Matrix33} The inverse matrix.
24730
+ * @param {dwv.math.Matrix33} m The input matrix.
24731
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24732
+ * if the determinant is zero.
24364
24733
  * @see https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
24365
- */
24366
- dwv.math.Matrix33.prototype.getInverse = function () {
24367
- var a = this.get(0, 0);
24368
- var b = this.get(0, 1);
24369
- var c = this.get(0, 2);
24370
- var d = this.get(1, 0);
24371
- var e = this.get(1, 1);
24372
- var f = this.get(1, 2);
24373
- var g = this.get(2, 0);
24374
- var h = this.get(2, 1);
24375
- var i = this.get(2, 2);
24376
-
24377
- var a2 = e * i - f * h;
24378
- var b2 = f * g - d * i;
24379
- var c2 = d * h - e * g;
24380
-
24381
- var det = a * a2 + b * b2 + c * c2;
24734
+ * @see https://github.com/willnode/N-Matrix-Programmer
24735
+ */
24736
+ dwv.math.getMatrixInverse = function (m) {
24737
+ var m00 = m.get(0, 0);
24738
+ var m01 = m.get(0, 1);
24739
+ var m02 = m.get(0, 2);
24740
+ var m10 = m.get(1, 0);
24741
+ var m11 = m.get(1, 1);
24742
+ var m12 = m.get(1, 2);
24743
+ var m20 = m.get(2, 0);
24744
+ var m21 = m.get(2, 1);
24745
+ var m22 = m.get(2, 2);
24746
+
24747
+ var a1212 = m11 * m22 - m12 * m21;
24748
+ var a2012 = m12 * m20 - m10 * m22;
24749
+ var a0112 = m10 * m21 - m11 * m20;
24750
+
24751
+ var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
24382
24752
  if (det === 0) {
24383
- dwv.logger.warn('Cannot invert matrix with zero determinant.');
24753
+ dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
24384
24754
  return;
24385
24755
  }
24756
+ det = 1 / det;
24386
24757
 
24387
24758
  var values = [
24388
- a2 / det,
24389
- (c * h - b * i) / det,
24390
- (b * f - c * e) / det,
24391
- b2 / det,
24392
- (a * i - c * g) / det,
24393
- (c * d - a * f) / det,
24394
- c2 / det,
24395
- (b * g - a * h) / det,
24396
- (a * e - b * d) / det
24759
+ det * a1212,
24760
+ det * (m02 * m21 - m01 * m22),
24761
+ det * (m01 * m12 - m02 * m11),
24762
+ det * a2012,
24763
+ det * (m00 * m22 - m02 * m20),
24764
+ det * (m02 * m10 - m00 * m12),
24765
+ det * a0112,
24766
+ det * (m01 * m20 - m00 * m21),
24767
+ det * (m00 * m11 - m01 * m10)
24397
24768
  ];
24398
24769
 
24399
24770
  return new dwv.math.Matrix33(values);
@@ -24804,6 +25175,21 @@ dwv.math.Point3D.prototype.equals = function (rhs) {
24804
25175
  this.getZ() === rhs.getZ();
24805
25176
  };
24806
25177
 
25178
+ /**
25179
+ * Check for Point3D similarity.
25180
+ *
25181
+ * @param {dwv.math.Point3D} rhs The other point to compare to.
25182
+ * @param {number} tol Optional comparison tolerance,
25183
+ * default to Number.EPSILON.
25184
+ * @returns {boolean} True if both points are equal.
25185
+ */
25186
+ dwv.math.Point3D.prototype.isSimilar = function (rhs, tol) {
25187
+ return rhs !== null &&
25188
+ dwv.math.isSimilar(this.getX(), rhs.getX(), tol) &&
25189
+ dwv.math.isSimilar(this.getY(), rhs.getY(), tol) &&
25190
+ dwv.math.isSimilar(this.getZ(), rhs.getZ(), tol);
25191
+ };
25192
+
24807
25193
  /**
24808
25194
  * Get a string representation of the Point3D.
24809
25195
  *
@@ -24841,6 +25227,19 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
24841
25227
  (this.getZ() - point3D.getZ()));
24842
25228
  };
24843
25229
 
25230
+ /**
25231
+ * Get an array find callback for a given point.
25232
+ *
25233
+ * @param {dwv.math.Point3D} point The point to compare to.
25234
+ * @returns {Function} A function that compares its input point to the one
25235
+ * given as input to this function.
25236
+ */
25237
+ dwv.math.getEqualPoint3DFunction = function (point) {
25238
+ return function (element) {
25239
+ return element.equals(point);
25240
+ };
25241
+ };
25242
+
24844
25243
  /**
24845
25244
  * Immutable point.
24846
25245
  * Warning: the input array is NOT cloned, modifying it will
@@ -27143,11 +27542,15 @@ dwv.tool.Draw = function (app) {
27143
27542
  };
27144
27543
 
27145
27544
  /**
27146
- * Handle double click event.
27545
+ * Handle double click event: some tools use it to finish interaction.
27147
27546
  *
27148
- * @param {object} event The mouse up event.
27547
+ * @param {object} event The double click event.
27149
27548
  */
27150
27549
  this.dblclick = function (event) {
27550
+ // only end by double click undefined NPoints
27551
+ if (typeof currentFactory.getNPoints() !== 'undefined') {
27552
+ return;
27553
+ }
27151
27554
  // exit if not started draw
27152
27555
  if (!started) {
27153
27556
  return;
@@ -30089,10 +30492,11 @@ dwv.tool.draw.FreeHandFactory = function () {
30089
30492
  /**
30090
30493
  * Get the number of points needed to build the shape.
30091
30494
  *
30092
- * @returns {number} The number of points.
30495
+ * @returns {number|undefined} The number of points.
30093
30496
  */
30094
30497
  this.getNPoints = function () {
30095
- return 1000;
30498
+ // undefined to end with double click
30499
+ return undefined;
30096
30500
  };
30097
30501
  /**
30098
30502
  * Get the timeout between point storage.
@@ -31520,10 +31924,11 @@ dwv.tool.draw.RoiFactory = function () {
31520
31924
  /**
31521
31925
  * Get the number of points needed to build the shape.
31522
31926
  *
31523
- * @returns {number} The number of points.
31927
+ * @returns {number|undefined} The number of points.
31524
31928
  */
31525
31929
  this.getNPoints = function () {
31526
- return 50;
31930
+ // undefined to end with double click
31931
+ return undefined;
31527
31932
  };
31528
31933
  /**
31529
31934
  * Get the timeout between point storage.
@@ -32440,11 +32845,16 @@ dwv.tool.WindowLevel = function (app) {
32440
32845
  // difference to last position
32441
32846
  var diffX = event._x - self.x0;
32442
32847
  var diffY = self.y0 - event._y;
32848
+ // data range
32849
+ var range = viewController.getImageRescaledDataRange();
32850
+ // 1/1000 seems to give reasonable results...
32851
+ var pixelToIntensity = (range.max - range.min) * 0.01;
32852
+
32443
32853
  // calculate new window level
32444
- var windowCenter =
32445
- parseInt(viewController.getWindowLevel().center, 10) + diffY;
32446
- var windowWidth =
32447
- parseInt(viewController.getWindowLevel().width, 10) + diffX;
32854
+ var center = parseInt(viewController.getWindowLevel().center, 10);
32855
+ var width = parseInt(viewController.getWindowLevel().width, 10);
32856
+ var windowCenter = center + Math.round(diffY * pixelToIntensity);
32857
+ var windowWidth = width + Math.round(diffX * pixelToIntensity);
32448
32858
  // bound window width
32449
32859
  windowWidth = dwv.image.validateWindowWidth(windowWidth);
32450
32860