dwv 0.31.0-beta.2 → 0.31.0-beta.5

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.2 2022-03-22 15:40:31 */
1
+ /*! dwv 0.31.0-beta.5 2022-06-16 10:33:58 */
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
 
@@ -1096,6 +1110,7 @@ dwv.App = function () {
1096
1110
 
1097
1111
  // render if first and flag allows
1098
1112
  if (event.loadtype === 'image' &&
1113
+ getViewConfigs(event.loadid).length !== 0 &&
1099
1114
  isFirstLoadItem && options.viewOnFirstLoadItem) {
1100
1115
  self.render(event.loadid);
1101
1116
  }
@@ -1237,7 +1252,7 @@ dwv.App = function () {
1237
1252
  var data = dataController.get(indices[i]);
1238
1253
  var geometry = data.image.getGeometry();
1239
1254
  var viewOrient = dwv.gui.getViewOrientation(
1240
- geometry,
1255
+ geometry.getOrientation(),
1241
1256
  layerGroup.getTargetOrientation()
1242
1257
  );
1243
1258
  var size = geometry.getSize(viewOrient).get2D();
@@ -1281,7 +1296,7 @@ dwv.App = function () {
1281
1296
  new dwv.dicom.DicomElementsWrapper(data.meta),
1282
1297
  data.image);
1283
1298
  var viewOrientation = dwv.gui.getViewOrientation(
1284
- imageGeometry,
1299
+ imageGeometry.getOrientation(),
1285
1300
  layerGroup.getTargetOrientation()
1286
1301
  );
1287
1302
  view.setOrientation(viewOrientation);
@@ -1322,22 +1337,9 @@ dwv.App = function () {
1322
1337
  viewLayer.setView(view);
1323
1338
  var size2D = imageGeometry.getSize(viewOrientation).get2D();
1324
1339
  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
- }
1340
+ viewLayer.initialise(size2D, spacing2D, dataIndex, opacity);
1341
+ viewLayer.setScale(layerGroup.getScale());
1342
+ viewLayer.setOffset(layerGroup.getOffset());
1341
1343
 
1342
1344
  // listen to image changes
1343
1345
  dataController.addEventListener('imagechange', viewLayer.onimagechange);
@@ -1351,18 +1353,16 @@ dwv.App = function () {
1351
1353
  dl.initialise(size2D, spacing2D, dataIndex);
1352
1354
  dl.setPlaneHelper(viewLayer.getViewController().getPlaneHelper());
1353
1355
 
1356
+ // force positionchange to sync layers
1354
1357
  var vc = viewLayer.getViewController();
1355
- // positionchange event like data
1356
1358
  var value = [
1357
1359
  vc.getCurrentIndex().getValues(),
1358
1360
  vc.getCurrentPosition().getValues()
1359
1361
  ];
1360
- layerGroup.updateLayersToPositionChange({value: value});
1361
-
1362
- // compensate origin difference if needed
1363
- if (diff) {
1364
- dl.setBaseOffset(diff);
1365
- }
1362
+ layerGroup.updateLayersToPositionChange({
1363
+ value: value,
1364
+ srclayerid: viewLayer.getId()
1365
+ });
1366
1366
  }
1367
1367
 
1368
1368
  // fit to the maximum size
@@ -2619,9 +2619,15 @@ dwv.ctrl.ViewController = function (view) {
2619
2619
  // third dimension player ID (created by setInterval)
2620
2620
  var playerID = null;
2621
2621
 
2622
+ // check view
2623
+ if (typeof view.getImage() === 'undefined') {
2624
+ throw new Error('View does not have an image, cannot setup controller');
2625
+ }
2626
+
2622
2627
  // setup the plane helper
2623
2628
  var planeHelper = new dwv.image.PlaneHelper(
2624
- view.getImage().getGeometry().getSpacing(),
2629
+ view.getImage().getGeometry().getRealSpacing(),
2630
+ view.getImage().getGeometry().getOrientation(),
2625
2631
  view.getOrientation()
2626
2632
  );
2627
2633
 
@@ -2746,6 +2752,10 @@ dwv.ctrl.ViewController = function (view) {
2746
2752
  return view.getCurrentIndex().get(view.getScrollIndex());
2747
2753
  };
2748
2754
 
2755
+ this.getOrigin = function (position) {
2756
+ return view.getOrigin(position);
2757
+ };
2758
+
2749
2759
  /**
2750
2760
  * Get the current scroll position value.
2751
2761
  *
@@ -2891,7 +2901,48 @@ dwv.ctrl.ViewController = function (view) {
2891
2901
  * @returns {dwv.image.Size} The size.
2892
2902
  */
2893
2903
  this.getImageSize = function () {
2894
- return view.getImage().getGeometry().getSize();
2904
+ return view.getImage().getGeometry().getSize(view.getOrientation());
2905
+ };
2906
+
2907
+ /**
2908
+ * Get the image rescaled data range.
2909
+ *
2910
+ * @returns {object} The range as {min, max}.
2911
+ */
2912
+ this.getImageRescaledDataRange = function () {
2913
+ return view.getImage().getRescaledDataRange();
2914
+ };
2915
+
2916
+ /**
2917
+ * Compare the input meta data to the associated image one.
2918
+ *
2919
+ * @param {object} meta The meta data.
2920
+ * @returns {boolean} True if the associated image has equal meta data.
2921
+ */
2922
+ this.equalImageMeta = function (meta) {
2923
+ var imageMeta = view.getImage().getMeta();
2924
+ // loop through input meta keys
2925
+ var metaKeys = Object.keys(meta);
2926
+ for (var i = 0; i < metaKeys.length; ++i) {
2927
+ var metaKey = metaKeys[i];
2928
+ if (typeof imageMeta[metaKey] === 'undefined') {
2929
+ return false;
2930
+ }
2931
+ if (imageMeta[metaKey] !== meta[metaKey]) {
2932
+ return false;
2933
+ }
2934
+ }
2935
+ return true;
2936
+ };
2937
+
2938
+ /**
2939
+ * Check is the provided position can be set.
2940
+ *
2941
+ * @param {dwv.math.Point} position The position.
2942
+ * @returns {boolean} True is the position is in bounds.
2943
+ */
2944
+ this.canSetPosition = function (position) {
2945
+ return view.canSetPosition(position);
2895
2946
  };
2896
2947
 
2897
2948
  /**
@@ -2913,27 +2964,29 @@ dwv.ctrl.ViewController = function (view) {
2913
2964
  * @returns {boolean} False if not in bounds.
2914
2965
  */
2915
2966
  this.setCurrentPosition2D = function (x, y) {
2916
- return view.setCurrentPosition(
2917
- this.getPositionFromPlanePoint({x: x, y: y}));
2918
- };
2919
-
2920
- /**
2921
- * Get a 3D position from a plane 2D position.
2922
- *
2923
- * @param {dwv.math.Point2D} point2D The 2D position as {x,y}.
2924
- * @returns {dwv.math.Point} The 3D point.
2925
- */
2926
- this.getPositionFromPlanePoint = function (point2D) {
2927
2967
  // keep third direction
2928
2968
  var k = this.getCurrentScrollIndexValue();
2929
- var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2969
+ var planePoint = new dwv.math.Point3D(x, y, k);
2930
2970
  // de-orient
2931
- var point = planeHelper.getDeOrientedVector3D(planePoint);
2971
+ var point = planeHelper.getImageOrientedVector3D(planePoint);
2932
2972
  // ~indexToWorld to not loose precision
2933
2973
  var geometry = view.getImage().getGeometry();
2934
2974
  var point3D = geometry.pointToWorld(point);
2935
2975
  // merge with current position to keep extra dimensions
2936
- return this.getCurrentPosition().mergeWith3D(point3D);
2976
+ var position = this.getCurrentPosition().mergeWith3D(point3D);
2977
+
2978
+ return view.setCurrentPosition(position);
2979
+ };
2980
+
2981
+ /**
2982
+ * Set the current index.
2983
+ *
2984
+ * @param {dwv.math.Index} index The index.
2985
+ * @param {boolean} silent If true, does not fire a positionchange event.
2986
+ * @returns {boolean} False if not in bounds.
2987
+ */
2988
+ this.setCurrentIndex = function (index, silent) {
2989
+ return view.setCurrentIndex(index, silent);
2937
2990
  };
2938
2991
 
2939
2992
  /**
@@ -2948,10 +3001,10 @@ dwv.ctrl.ViewController = function (view) {
2948
3001
  var k = this.getCurrentScrollIndexValue();
2949
3002
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2950
3003
  // de-orient
2951
- var point = planeHelper.getDeOrientedVector3D(planePoint);
3004
+ var point = planeHelper.getTargetDeOrientedVector3D(planePoint);
2952
3005
  // ~indexToWorld to not loose precision
2953
3006
  var geometry = view.getImage().getGeometry();
2954
- var spacing = geometry.getSpacing();
3007
+ var spacing = geometry.getRealSpacing();
2955
3008
  return new dwv.math.Point3D(
2956
3009
  point.getX() * spacing.get(0),
2957
3010
  point.getY() * spacing.get(1),
@@ -4495,7 +4548,7 @@ dwv.dicom = dwv.dicom || {};
4495
4548
  * @returns {string} The version of the library.
4496
4549
  */
4497
4550
  dwv.getVersion = function () {
4498
- return '0.31.0-beta.2';
4551
+ return '0.31.0-beta.5';
4499
4552
  };
4500
4553
 
4501
4554
  /**
@@ -6028,10 +6081,24 @@ dwv.dicom.isEven = function (number) {
6028
6081
  * @returns {boolean} True if the VR is a non string one.
6029
6082
  */
6030
6083
  dwv.dicom.isNonStringVr = function (vr) {
6031
- return vr === 'UN' || vr === 'OB' || vr === 'OW' ||
6032
- vr === 'OF' || vr === 'OD' || vr === 'US' || vr === 'SS' ||
6033
- vr === 'UL' || vr === 'SL' || vr === 'FL' || vr === 'FD' ||
6034
- vr === 'SQ' || vr === 'AT';
6084
+ return vr === 'UN' ||
6085
+ vr === 'SQ' ||
6086
+ vr === 'AT' ||
6087
+ dwv.dicom.isTypedArrayVr(vr);
6088
+ };
6089
+
6090
+ /**
6091
+ * Is the input VR a VR that stores data in a typed array.
6092
+ *
6093
+ * @param {string} vr The element VR.
6094
+ * @returns {boolean} True if the VR is a typed array one.
6095
+ */
6096
+ dwv.dicom.isTypedArrayVr = function (vr) {
6097
+ return vr === 'OB' || vr === 'OW' ||
6098
+ vr === 'OF' || vr === 'OD' ||
6099
+ vr === 'US' || vr === 'SS' ||
6100
+ vr === 'UL' || vr === 'SL' ||
6101
+ vr === 'FL' || vr === 'FD';
6035
6102
  };
6036
6103
 
6037
6104
  /**
@@ -6828,43 +6895,55 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
6828
6895
  // set the value and calculate size
6829
6896
  size = 0;
6830
6897
  var paddedValue = dwv.dicom.padElementValue(element, value);
6831
- if (value instanceof Array) {
6898
+ if (element.vr === 'AT') {
6832
6899
  element.value = paddedValue;
6833
- for (var k = 0; k < paddedValue.length; ++k) {
6834
- // spearator
6835
- if (k !== 0) {
6836
- size += 1;
6837
- }
6838
- // value
6839
- size += paddedValue[k].toString().length;
6900
+ size = 4;
6901
+ } else if (dwv.dicom.isTypedArrayVr(element.vr)) {
6902
+ // convert non array (number) value to array
6903
+ if (typeof value.length === 'undefined') {
6904
+ element.value = [paddedValue];
6905
+ } else {
6906
+ element.value = paddedValue;
6907
+ }
6908
+ size = element.value.length;
6909
+
6910
+ // convert size to bytes
6911
+ if (element.vr === 'US' || element.vr === 'OW') {
6912
+ size *= Uint16Array.BYTES_PER_ELEMENT;
6913
+ } else if (element.vr === 'SS') {
6914
+ size *= Int16Array.BYTES_PER_ELEMENT;
6915
+ } else if (element.vr === 'UL') {
6916
+ size *= Uint32Array.BYTES_PER_ELEMENT;
6917
+ } else if (element.vr === 'SL') {
6918
+ size *= Int32Array.BYTES_PER_ELEMENT;
6919
+ } else if (element.vr === 'FL') {
6920
+ size *= Float32Array.BYTES_PER_ELEMENT;
6921
+ } else if (element.vr === 'FD') {
6922
+ size *= Float64Array.BYTES_PER_ELEMENT;
6840
6923
  }
6841
6924
  } else {
6842
- element.value = [paddedValue];
6843
- if (typeof paddedValue !== 'undefined' &&
6844
- typeof paddedValue.length !== 'undefined') {
6845
- size = paddedValue.length;
6925
+ if (value instanceof Array) {
6926
+ element.value = paddedValue;
6927
+ for (var k = 0; k < paddedValue.length; ++k) {
6928
+ // separator
6929
+ if (k !== 0) {
6930
+ size += 1;
6931
+ }
6932
+ // value
6933
+ size += paddedValue[k].toString().length;
6934
+ }
6846
6935
  } else {
6847
- // numbers
6848
- size = 1;
6849
- }
6850
- }
6851
-
6852
- // convert size to bytes
6853
- if (element.vr === 'US' || element.vr === 'OW') {
6854
- size *= Uint16Array.BYTES_PER_ELEMENT;
6855
- } else if (element.vr === 'SS') {
6856
- size *= Int16Array.BYTES_PER_ELEMENT;
6857
- } else if (element.vr === 'UL') {
6858
- size *= Uint32Array.BYTES_PER_ELEMENT;
6859
- } else if (element.vr === 'SL') {
6860
- size *= Int32Array.BYTES_PER_ELEMENT;
6861
- } else if (element.vr === 'FL') {
6862
- size *= Float32Array.BYTES_PER_ELEMENT;
6863
- } else if (element.vr === 'FD') {
6864
- size *= Float64Array.BYTES_PER_ELEMENT;
6865
- } else {
6866
- size *= Uint8Array.BYTES_PER_ELEMENT;
6936
+ element.value = [paddedValue];
6937
+ if (typeof paddedValue !== 'undefined' &&
6938
+ typeof paddedValue.length !== 'undefined') {
6939
+ size = paddedValue.length;
6940
+ } else {
6941
+ // numbers
6942
+ size = 1;
6943
+ }
6944
+ }
6867
6945
  }
6946
+
6868
6947
  element.vl = size;
6869
6948
  }
6870
6949
 
@@ -11408,21 +11487,31 @@ dwv.gui.DrawLayer = function (containerDiv) {
11408
11487
  };
11409
11488
 
11410
11489
  /**
11411
- * Set the base layer offset. Resets the layer offset.
11490
+ * Set the base layer offset. Updates the layer offset.
11412
11491
  *
11413
- * @param {object} off The offset as {x,y}.
11492
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
11493
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
11494
+ * @returns {boolean} True if the offset was updated.
11414
11495
  */
11415
- this.setBaseOffset = function (off) {
11416
- baseOffset = planeHelper.getPlaneOffsetFromOffset3D({
11417
- x: off.getX(),
11418
- y: off.getY(),
11419
- z: off.getZ()
11420
- });
11421
- // reset offset
11422
- konvaStage.offset({
11423
- x: baseOffset.x,
11424
- y: baseOffset.y
11496
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
11497
+ var scrollIndex = planeHelper.getNativeScrollIndex();
11498
+ var newOffset = planeHelper.getPlaneOffsetFromOffset3D({
11499
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
11500
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
11501
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
11425
11502
  });
11503
+ var needsUpdate = baseOffset.x !== newOffset.x ||
11504
+ baseOffset.y !== newOffset.y;
11505
+ // reset offset if needed
11506
+ if (needsUpdate) {
11507
+ var offset = konvaStage.offset();
11508
+ konvaStage.offset({
11509
+ x: offset.x - baseOffset.x + newOffset.x,
11510
+ y: offset.y - baseOffset.y + newOffset.y
11511
+ });
11512
+ baseOffset = newOffset;
11513
+ }
11514
+ return needsUpdate;
11426
11515
  };
11427
11516
 
11428
11517
  /**
@@ -11538,10 +11627,13 @@ dwv.gui.DrawLayer = function (containerDiv) {
11538
11627
  *
11539
11628
  * @param {dwv.math.Point} position The new position.
11540
11629
  * @param {dwv.math.Index} index The new index.
11630
+ * @returns {boolean} True if the position was updated.
11541
11631
  */
11542
11632
  this.setCurrentPosition = function (position, index) {
11543
11633
  this.getDrawController().activateDrawLayer(
11544
11634
  index, planeHelper.getScrollIndex());
11635
+ // TODO: add check
11636
+ return true;
11545
11637
  };
11546
11638
 
11547
11639
  /**
@@ -11830,27 +11922,44 @@ dwv.gui.getLayerDetailsFromEvent = function (event) {
11830
11922
  };
11831
11923
 
11832
11924
  /**
11833
- * Get a view orientation according to an image geometry (with its orientation)
11834
- * and target orientation.
11925
+ * Get the view orientation according to an image and target orientation.
11926
+ * The view orientation is used to go from target to image space.
11835
11927
  *
11836
- * @param {dwv.image.Geometry} imageGeometry The image geometry.
11928
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11837
11929
  * @param {dwv.math.Matrix33} targetOrientation The target orientation.
11838
11930
  * @returns {dwv.math.Matrix33} The view orientation.
11839
11931
  */
11840
- dwv.gui.getViewOrientation = function (imageGeometry, targetOrientation) {
11932
+ dwv.gui.getViewOrientation = function (imageOrientation, targetOrientation) {
11841
11933
  var viewOrientation = dwv.math.getIdentityMat33();
11842
11934
  if (typeof targetOrientation !== 'undefined') {
11843
- // image orientation as one and zeros
11844
- // -> view orientation is one and zeros
11845
- var imgOrientation = imageGeometry.getOrientation().asOneAndZeros();
11846
- // imgOrientation * viewOrientation = targetOrientation
11847
- // -> viewOrientation = inv(imgOrientation) * targetOrientation
11935
+ // i: image, v: view, t: target, O: orientation, P: point
11936
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
11937
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
11938
+ // -> Ov = (Oi)-1 * Ot
11939
+ // TODO: asOneAndZeros simplifies but not nice...
11848
11940
  viewOrientation =
11849
- imgOrientation.getInverse().multiply(targetOrientation);
11941
+ imageOrientation.asOneAndZeros().getInverse().multiply(targetOrientation);
11850
11942
  }
11851
11943
  return viewOrientation;
11852
11944
  };
11853
11945
 
11946
+ /**
11947
+ * Get the target orientation according to an image and view orientation.
11948
+ * The target orientation is used to go from target to real space.
11949
+ *
11950
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11951
+ * @param {dwv.math.Matrix33} viewOrientation The view orientation.
11952
+ * @returns {dwv.math.Matrix33} The target orientation.
11953
+ */
11954
+ dwv.gui.getTargetOrientation = function (imageOrientation, viewOrientation) {
11955
+ // i: image, v: view, t: target, O: orientation, P: point
11956
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
11957
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
11958
+ // -> Ot = Oi * Ov
11959
+ // note: asOneAndZeros as in dwv.gui.getViewOrientation...
11960
+ return imageOrientation.asOneAndZeros().multiply(viewOrientation);
11961
+ };
11962
+
11854
11963
  /**
11855
11964
  * Layer group.
11856
11965
  *
@@ -12045,6 +12154,24 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12045
12154
  return res;
12046
12155
  };
12047
12156
 
12157
+ /**
12158
+ * Search view layers for equal imae meta data.
12159
+ *
12160
+ * @param {object} meta The meta data to find.
12161
+ * @returns {Array} The list of view layers that contain matched data.
12162
+ */
12163
+ this.searchViewLayers = function (meta) {
12164
+ var res = [];
12165
+ for (var i = 0; i < layers.length; ++i) {
12166
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12167
+ if (layers[i].getViewController().equalImageMeta(meta)) {
12168
+ res.push(layers[i]);
12169
+ }
12170
+ }
12171
+ }
12172
+ return res;
12173
+ };
12174
+
12048
12175
  /**
12049
12176
  * Get the view layers data indices.
12050
12177
  *
@@ -12243,10 +12370,53 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12243
12370
 
12244
12371
  var index = new dwv.math.Index(event.value[0]);
12245
12372
  var position = new dwv.math.Point(event.value[1]);
12373
+ // origin of the first view layer
12374
+ var baseViewLayerOrigin0 = null;
12375
+ var baseViewLayerOrigin = null;
12246
12376
  // update position for all layers except the source one
12247
12377
  for (var i = 0; i < layers.length; ++i) {
12378
+
12379
+ // update base offset (does not trigger redraw)
12380
+ // TODO check draw layers update
12381
+ var hasSetOffset = false;
12382
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12383
+ var vc = layers[i].getViewController();
12384
+ // origin0 should always be there
12385
+ var origin0 = vc.getOrigin();
12386
+ // depending on position, origin could be undefined
12387
+ var origin = vc.getOrigin(position);
12388
+
12389
+ if (!baseViewLayerOrigin) {
12390
+ baseViewLayerOrigin0 = origin0;
12391
+ baseViewLayerOrigin = origin;
12392
+ } else {
12393
+ if (vc.canSetPosition(position) &&
12394
+ typeof origin !== 'undefined') {
12395
+ // TODO: compensate for possible different orientation between views
12396
+ // TODO: check why -z...
12397
+
12398
+ var scrollDiff = baseViewLayerOrigin0.minus(origin0);
12399
+ var scrollOffset = new dwv.math.Vector3D(
12400
+ scrollDiff.getX(), scrollDiff.getY(), -1 * scrollDiff.getZ());
12401
+
12402
+ var planeDiff = baseViewLayerOrigin.minus(origin);
12403
+ var planeOffset = new dwv.math.Vector3D(
12404
+ planeDiff.getX(), planeDiff.getY(), -1 * planeDiff.getZ());
12405
+
12406
+ hasSetOffset = layers[i].setBaseOffset(scrollOffset, planeOffset);
12407
+ }
12408
+ }
12409
+ }
12410
+
12411
+ // update position (triggers redraw)
12412
+ var hasSetPos = false;
12248
12413
  if (layers[i].getId() !== event.srclayerid) {
12249
- layers[i].setCurrentPosition(position, index);
12414
+ hasSetPos = layers[i].setCurrentPosition(position, index);
12415
+ }
12416
+
12417
+ // force redraw if needed
12418
+ if (!hasSetPos && hasSetOffset) {
12419
+ layers[i].draw();
12250
12420
  }
12251
12421
  }
12252
12422
 
@@ -13114,12 +13284,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13114
13284
  var context = null;
13115
13285
 
13116
13286
  /**
13117
- * Flag to know if the context has been cleared.
13287
+ * Flag to know if the current position is valid.
13118
13288
  *
13119
13289
  * @private
13120
13290
  * @type {boolean}
13121
13291
  */
13122
- var isContextClear = true;
13292
+ var isValidPosition = true;
13123
13293
 
13124
13294
  /**
13125
13295
  * The image data array.
@@ -13264,6 +13434,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
13264
13434
  // event.value = [index, image]
13265
13435
  if (dataIndex === event.value[0]) {
13266
13436
  viewController.setImage(event.value[1]);
13437
+ setBaseSize(viewController.getImageSize().get2D());
13267
13438
  needsDataUpdate = true;
13268
13439
  }
13269
13440
  };
@@ -13303,6 +13474,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
13303
13474
  * @param {number} alpha The opacity ([0:1] range).
13304
13475
  */
13305
13476
  this.setOpacity = function (alpha) {
13477
+ if (alpha === opacity) {
13478
+ return;
13479
+ }
13480
+
13306
13481
  opacity = Math.min(Math.max(alpha, 0), 1);
13307
13482
 
13308
13483
  /**
@@ -13334,19 +13509,31 @@ dwv.gui.ViewLayer = function (containerDiv) {
13334
13509
  };
13335
13510
 
13336
13511
  /**
13337
- * Set the base layer offset. Resets the layer offset.
13512
+ * Set the base layer offset. Updates the layer offset.
13338
13513
  *
13339
- * @param {object} off The offset as {x,y}.
13514
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
13515
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
13516
+ * @returns {boolean} True if the offset was updated.
13340
13517
  */
13341
- this.setBaseOffset = function (off) {
13518
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
13342
13519
  var helper = viewController.getPlaneHelper();
13343
- baseOffset = helper.getPlaneOffsetFromOffset3D({
13344
- x: off.getX(),
13345
- y: off.getY(),
13346
- z: off.getZ()
13520
+ var scrollIndex = helper.getNativeScrollIndex();
13521
+ var newOffset = helper.getPlaneOffsetFromOffset3D({
13522
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
13523
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
13524
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
13347
13525
  });
13348
- // reset offset
13349
- offset = baseOffset;
13526
+ var needsUpdate = baseOffset.x !== newOffset.x ||
13527
+ baseOffset.y !== newOffset.y;
13528
+ // reset offset if needed
13529
+ if (needsUpdate) {
13530
+ offset = {
13531
+ x: offset.x - baseOffset.x + newOffset.x,
13532
+ y: offset.y - baseOffset.y + newOffset.y
13533
+ };
13534
+ baseOffset = newOffset;
13535
+ }
13536
+ return needsUpdate;
13350
13537
  };
13351
13538
 
13352
13539
  /**
@@ -13448,6 +13635,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13448
13635
  * @fires dwv.App#renderend
13449
13636
  */
13450
13637
  this.draw = function () {
13638
+ // skip for non valid position
13639
+ if (!isValidPosition) {
13640
+ return;
13641
+ }
13642
+
13451
13643
  /**
13452
13644
  * Render start event.
13453
13645
  *
@@ -13457,7 +13649,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13457
13649
  */
13458
13650
  var event = {
13459
13651
  type: 'renderstart',
13460
- layerid: this.getId()
13652
+ layerid: this.getId(),
13653
+ dataid: this.getDataIndex()
13461
13654
  };
13462
13655
  fireEvent(event);
13463
13656
 
@@ -13492,9 +13685,6 @@ dwv.gui.ViewLayer = function (containerDiv) {
13492
13685
  // draw image
13493
13686
  context.drawImage(offscreenCanvas, 0, 0);
13494
13687
 
13495
- // set clear flag
13496
- isContextClear = false;
13497
-
13498
13688
  /**
13499
13689
  * Render end event.
13500
13690
  *
@@ -13504,7 +13694,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13504
13694
  */
13505
13695
  event = {
13506
13696
  type: 'renderend',
13507
- layerid: this.getId()
13697
+ layerid: this.getId(),
13698
+ dataid: this.getDataIndex()
13508
13699
  };
13509
13700
  fireEvent(event);
13510
13701
  };
@@ -13515,12 +13706,13 @@ dwv.gui.ViewLayer = function (containerDiv) {
13515
13706
  * @param {object} size The image size as {x,y}.
13516
13707
  * @param {object} spacing The image spacing as {x,y}.
13517
13708
  * @param {number} index The associated data index.
13709
+ * @param {number} alpha The initial data opacity.
13518
13710
  */
13519
- this.initialise = function (size, spacing, index) {
13711
+ this.initialise = function (size, spacing, index, alpha) {
13520
13712
  // set locals
13521
- baseSize = size;
13522
13713
  baseSpacing = spacing;
13523
13714
  dataIndex = index;
13715
+ opacity = Math.min(Math.max(alpha, 0), 1);
13524
13716
 
13525
13717
  // create canvas
13526
13718
  // (canvas size is set in fitToContainer)
@@ -13539,22 +13731,37 @@ dwv.gui.ViewLayer = function (containerDiv) {
13539
13731
  return;
13540
13732
  }
13541
13733
 
13542
- // check canvas
13543
- if (!dwv.gui.canCreateCanvas(baseSize.x, baseSize.y)) {
13544
- throw new Error('Cannot create canvas ' + baseSize.x + ', ' + baseSize.y);
13734
+ // off screen canvas
13735
+ offscreenCanvas = document.createElement('canvas');
13736
+
13737
+ // set base size: needs an existing context and off screen canvas
13738
+ setBaseSize(size);
13739
+
13740
+ // update data on first draw
13741
+ needsDataUpdate = true;
13742
+ };
13743
+
13744
+ /**
13745
+ * Set the base size of the layer.
13746
+ *
13747
+ * @param {object} size The size as {x,y}.
13748
+ */
13749
+ function setBaseSize(size) {
13750
+ // check canvas creation
13751
+ if (!dwv.gui.canCreateCanvas(size.x, size.y)) {
13752
+ throw new Error('Cannot create canvas ' + size.x + ', ' + size.y);
13545
13753
  }
13546
13754
 
13755
+ // set local
13756
+ baseSize = size;
13757
+
13547
13758
  // off screen canvas
13548
- offscreenCanvas = document.createElement('canvas');
13549
13759
  offscreenCanvas.width = baseSize.x;
13550
13760
  offscreenCanvas.height = baseSize.y;
13551
13761
  // original empty image data array
13552
13762
  context.clearRect(0, 0, baseSize.x, baseSize.y);
13553
13763
  imageData = context.createImageData(baseSize.x, baseSize.y);
13554
-
13555
- // update data on first draw
13556
- needsDataUpdate = true;
13557
- };
13764
+ }
13558
13765
 
13559
13766
  /**
13560
13767
  * Fit the layer to its parent container.
@@ -13563,6 +13770,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13563
13770
  * @param {object} fitSize The fit size as {x,y}.
13564
13771
  */
13565
13772
  this.fitToContainer = function (fitScale1D, fitSize) {
13773
+ // previous scale without fit
13774
+ var previousScale = {
13775
+ x: scale.x / fitScale.x,
13776
+ y: scale.y / fitScale.y
13777
+ };
13566
13778
  // update fit scale
13567
13779
  fitScale = {
13568
13780
  x: fitScale1D * baseSpacing.x,
@@ -13576,8 +13788,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13576
13788
  }
13577
13789
  canvas.width = width;
13578
13790
  canvas.height = height;
13579
- // reset scale
13580
- this.setScale({x: 1, y: 1, z: 1});
13791
+ // reset previous scale with new fit
13792
+ scale = {
13793
+ x: previousScale.x * fitScale.x,
13794
+ y: previousScale.y * fitScale.y
13795
+ };
13581
13796
  };
13582
13797
 
13583
13798
  /**
@@ -13662,8 +13877,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13662
13877
  */
13663
13878
  function onWLChange(event) {
13664
13879
  // generate and draw if no skip flag
13665
- if (typeof event.skipGenerate === 'undefined' ||
13666
- event.skipGenerate === false) {
13880
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13881
+ event.skipGenerate === true;
13882
+ if (!skip) {
13667
13883
  needsDataUpdate = true;
13668
13884
  self.draw();
13669
13885
  }
@@ -13676,8 +13892,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13676
13892
  * @private
13677
13893
  */
13678
13894
  function onColourChange(_event) {
13679
- needsDataUpdate = true;
13680
- self.draw();
13895
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13896
+ event.skipGenerate === true;
13897
+ if (!skip) {
13898
+ needsDataUpdate = true;
13899
+ self.draw();
13900
+ }
13681
13901
  }
13682
13902
 
13683
13903
  /**
@@ -13687,26 +13907,35 @@ dwv.gui.ViewLayer = function (containerDiv) {
13687
13907
  * @private
13688
13908
  */
13689
13909
  function onPositionChange(event) {
13690
- if (typeof event.skipGenerate === 'undefined' ||
13691
- event.skipGenerate === false) {
13910
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13911
+ event.skipGenerate === true;
13912
+ if (!skip) {
13913
+ var valid = typeof event.valid === 'undefined' || event.valid;
13692
13914
  // clear for non valid events
13693
- if (typeof event.valid !== 'undefined' && !event.valid) {
13694
- self.clear();
13695
- return;
13696
- }
13697
- // 3D dimensions
13698
- var dims3D = [0, 1, 2];
13699
- // remove scroll index
13700
- var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13701
- dims3D.splice(indexScrollIndex, 1);
13702
- // remove non scroll index from diff dims
13703
- var diffDims = event.diffDims.filter(function (item) {
13704
- return dims3D.indexOf(item) === -1;
13705
- });
13706
- // update if we have something left
13707
- if (diffDims.length !== 0) {
13708
- needsDataUpdate = true;
13709
- self.draw();
13915
+ if (!valid) {
13916
+ // clear only once
13917
+ if (isValidPosition) {
13918
+ isValidPosition = false;
13919
+ self.clear();
13920
+ }
13921
+ } else {
13922
+ // 3D dimensions
13923
+ var dims3D = [0, 1, 2];
13924
+ // remove scroll index
13925
+ var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13926
+ dims3D.splice(indexScrollIndex, 1);
13927
+ // remove non scroll index from diff dims
13928
+ var diffDims = event.diffDims.filter(function (item) {
13929
+ return dims3D.indexOf(item) === -1;
13930
+ });
13931
+ // update if we have something left
13932
+ if (diffDims.length !== 0 || !isValidPosition) {
13933
+ // reset valid flag
13934
+ isValidPosition = true;
13935
+ // reset update flag
13936
+ needsDataUpdate = true;
13937
+ self.draw();
13938
+ }
13710
13939
  }
13711
13940
  }
13712
13941
  }
@@ -13718,8 +13947,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13718
13947
  * @private
13719
13948
  */
13720
13949
  function onAlphaFuncChange(event) {
13721
- if (typeof event.skipGenerate === 'undefined' ||
13722
- event.skipGenerate === false) {
13950
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13951
+ event.skipGenerate === true;
13952
+ if (!skip) {
13723
13953
  needsDataUpdate = true;
13724
13954
  self.draw();
13725
13955
  }
@@ -13730,28 +13960,24 @@ dwv.gui.ViewLayer = function (containerDiv) {
13730
13960
  *
13731
13961
  * @param {dwv.math.Point} position The new position.
13732
13962
  * @param {dwv.math.Index} _index The new index.
13963
+ * @returns {boolean} True if the position was updated.
13733
13964
  */
13734
13965
  this.setCurrentPosition = function (position, _index) {
13735
- viewController.setCurrentPosition(position);
13966
+ return viewController.setCurrentPosition(position);
13736
13967
  };
13737
13968
 
13738
13969
  /**
13739
13970
  * Clear the context.
13740
13971
  */
13741
13972
  this.clear = function () {
13742
- if (!isContextClear) {
13743
- // clear the context: reset the transform first
13744
- // store the current transformation matrix
13745
- context.save();
13746
- // use the identity matrix while clearing the canvas
13747
- context.setTransform(1, 0, 0, 1, 0, 0);
13748
- context.clearRect(0, 0, canvas.width, canvas.height);
13749
- // restore the transform
13750
- context.restore();
13751
-
13752
- // update clear flag
13753
- isContextClear = true;
13754
- }
13973
+ // clear the context: reset the transform first
13974
+ // store the current transformation matrix
13975
+ context.save();
13976
+ // use the identity matrix while clearing the canvas
13977
+ context.setTransform(1, 0, 0, 1, 0, 0);
13978
+ context.clearRect(0, 0, canvas.width, canvas.height);
13979
+ // restore the transform
13980
+ context.restore();
13755
13981
  };
13756
13982
 
13757
13983
  /**
@@ -15021,6 +15247,22 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15021
15247
  this.getOrigins = function () {
15022
15248
  return origins;
15023
15249
  };
15250
+
15251
+ /**
15252
+ * Check if a point is in the origin list.
15253
+ *
15254
+ * @param {dwv.math.Point3D} point3D The point to check.
15255
+ * @returns {boolean} True if in list.
15256
+ */
15257
+ this.includesOrigin = function (point3D) {
15258
+ for (var i = 0; i < origins.length; ++i) {
15259
+ if (origins[i].isSimilar(point3D, dwv.math.BIG_EPSILON)) {
15260
+ return true;
15261
+ }
15262
+ }
15263
+ return false;
15264
+ };
15265
+
15024
15266
  /**
15025
15267
  * Get the object size.
15026
15268
  * Warning: the size comes as stored in DICOM, meaning that it could
@@ -15039,7 +15281,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15039
15281
  size.get(2)
15040
15282
  ],
15041
15283
  viewOrientation);
15042
- res = new dwv.image.Size(values);
15284
+ res = new dwv.image.Size(values.concat(size.getValues().slice(3)));
15043
15285
  }
15044
15286
  return res;
15045
15287
  };
@@ -15054,26 +15296,26 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15054
15296
  if (origins.length === 1) {
15055
15297
  return 1;
15056
15298
  }
15057
- var spacing = null;
15299
+ var sliceSpacing = null;
15058
15300
  // (x, y, z) = orientationMatrix * (i, j, k)
15059
15301
  // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
15060
15302
  // applied on the patient position, reorders indices
15061
15303
  // so that Z is the slice direction
15062
- var orientation2 = orientation.getInverse().asOneAndZeros();
15304
+ var invOrientation = orientation.getInverse();
15063
15305
  var deltas = [];
15064
15306
  for (var i = 0; i < origins.length - 1; ++i) {
15065
- var origin1 = orientation2.multiplyVector3D(origins[i]);
15066
- var origin2 = orientation2.multiplyVector3D(origins[i + 1]);
15307
+ var origin1 = invOrientation.multiplyVector3D(origins[i]);
15308
+ var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
15067
15309
  var diff = Math.abs(origin1.getZ() - origin2.getZ());
15068
15310
  if (diff === 0) {
15069
15311
  throw new Error('Zero slice spacing.' +
15070
15312
  origin1.toString() + ' ' + origin2.toString());
15071
15313
  }
15072
- if (spacing === null) {
15073
- spacing = diff;
15314
+ if (sliceSpacing === null) {
15315
+ sliceSpacing = diff;
15074
15316
  } else {
15075
- if (!dwv.math.isSimilar(spacing, diff, dwv.math.BIG_EPSILON)) {
15076
- deltas.push(Math.abs(spacing - diff));
15317
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
15318
+ deltas.push(Math.abs(sliceSpacing - diff));
15077
15319
  }
15078
15320
  }
15079
15321
  }
@@ -15088,12 +15330,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15088
15330
  mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
15089
15331
  }
15090
15332
  }
15091
- return spacing;
15333
+
15334
+ return sliceSpacing;
15092
15335
  };
15093
15336
 
15094
15337
  /**
15095
15338
  * Get the object spacing.
15096
- * Warning: the size comes as stored in DICOM, meaning that it could
15339
+ * Warning: the spacing comes as stored in DICOM, meaning that it could
15097
15340
  * be oriented.
15098
15341
  *
15099
15342
  * @param {dwv.math.Matrix33} viewOrientation The view orientation (optional)
@@ -15121,6 +15364,16 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15121
15364
  return res;
15122
15365
  };
15123
15366
 
15367
+ /**
15368
+ * Get the image spacing in real world.
15369
+ *
15370
+ * @returns {dwv.image.Spacing} The object spacing.
15371
+ */
15372
+ this.getRealSpacing = function () {
15373
+ // asOneAndZeros to not change spacing values...
15374
+ return this.getSpacing(orientation.getInverse().asOneAndZeros());
15375
+ };
15376
+
15124
15377
  /**
15125
15378
  * Get the object orientation.
15126
15379
  *
@@ -15170,8 +15423,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15170
15423
  // -> >0 => vectors are codirectional
15171
15424
  // -> <0 => vectors are oposite
15172
15425
  var dotProd = normal.dotProduct(pointDir);
15426
+ var test = dotProd < 0;
15427
+ // TODO: check why coronal behaves differently...
15428
+ if (orientation.getThirdColMajorDirection() === 1) {
15429
+ test = dotProd > 0;
15430
+ }
15173
15431
  // oposite vectors get higher index
15174
- var sliceIndex = (dotProd < 0) ? closestSliceIndex + 1 : closestSliceIndex;
15432
+ var sliceIndex = test ? closestSliceIndex + 1 : closestSliceIndex;
15175
15433
  return sliceIndex;
15176
15434
  };
15177
15435
 
@@ -15249,10 +15507,11 @@ dwv.image.Geometry.prototype.isInBounds = function (point) {
15249
15507
  * Check that a index is within bounds.
15250
15508
  *
15251
15509
  * @param {dwv.math.Index} index The index to check.
15510
+ * @param {number} dir Optional direction to check.
15252
15511
  * @returns {boolean} True if the given coordinates are within bounds.
15253
15512
  */
15254
- dwv.image.Geometry.prototype.isIndexInBounds = function (index) {
15255
- return this.getSize().isInBounds(index);
15513
+ dwv.image.Geometry.prototype.isIndexInBounds = function (index, dir) {
15514
+ return this.getSize().isInBounds(index, dir);
15256
15515
  };
15257
15516
 
15258
15517
  /**
@@ -15332,6 +15591,7 @@ dwv.image.Geometry.prototype.pointToWorld = function (point) {
15332
15591
  dwv.image.Geometry.prototype.worldToIndex = function (point) {
15333
15592
  // compensate for origin
15334
15593
  // (origin is not oriented, compensate before orientation)
15594
+ // TODO: use slice origin...
15335
15595
  var origin = this.getOrigin();
15336
15596
  var point3D = new dwv.math.Point3D(
15337
15597
  point.get(0) - origin.getX(),
@@ -15348,9 +15608,11 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
15348
15608
  values[0] = Math.round(orientedPoint3D.getX() / spacing.get(0));
15349
15609
  values[1] = Math.round(orientedPoint3D.getY() / spacing.get(1));
15350
15610
  // flip K index (because of the slice order given by getSliceIndex)
15351
- values[2] = flipK(this.getSize(),
15352
- Math.round(orientedPoint3D.getZ() / spacing.get(2))
15353
- );
15611
+ var k = Math.round(orientedPoint3D.getZ() / spacing.get(2));
15612
+ // abs to fix #1163
15613
+ // TODO: find out why k can sometimes be negative...
15614
+ values[2] = flipK(this.getSize(), k);
15615
+
15354
15616
  // return index
15355
15617
  return new dwv.math.Index(values);
15356
15618
  };
@@ -15789,7 +16051,7 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15789
16051
  }
15790
16052
  // all meta should be equal
15791
16053
  for (var key in meta) {
15792
- if (key === 'windowPresets') {
16054
+ if (key === 'windowPresets' || key === 'numberOfFiles') {
15793
16055
  continue;
15794
16056
  }
15795
16057
  if (meta[key] !== rhs.getMeta()[key]) {
@@ -15820,8 +16082,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15820
16082
  values[3] = timeId;
15821
16083
  }
15822
16084
  var index = new dwv.math.Index(values);
15823
- var primaryOffset = size.indexToOffset(index);
15824
- var secondaryOffset = this.getSecondaryOffset(index);
16085
+ var primaryOffset = size.indexToOffset(index) * numberOfComponents;
16086
+ var secondaryOffset = this.getSecondaryOffset(index) * numberOfComponents;
15825
16087
 
15826
16088
  // first frame special slice by slice append
15827
16089
  if (timeId === 0) {
@@ -17799,7 +18061,7 @@ dwv.image.MaskFactory.prototype.create = function (
17799
18061
  }
17800
18062
  // create frame info object from per frame func
17801
18063
  var frameInfos = [];
17802
- var posPats = [];
18064
+ var framePosPats = [];
17803
18065
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
17804
18066
  var frameFunc = perFrameFuncGroupSequence[j];
17805
18067
  // Derivation Image Sequence
@@ -17826,8 +18088,8 @@ dwv.image.MaskFactory.prototype.create = function (
17826
18088
  for (var p = 0; p < imagePosPat.length; ++p) {
17827
18089
  imagePosPat[p] = parseFloat(imagePosPat[p], 10);
17828
18090
  }
17829
- if (!includesPosPat(posPats, imagePosPat)) {
17830
- posPats.push(imagePosPat);
18091
+ if (!includesPosPat(framePosPats, imagePosPat)) {
18092
+ framePosPats.push(imagePosPat);
17831
18093
  }
17832
18094
  frameInfos.push({
17833
18095
  dimIndex: dimIndex,
@@ -17871,7 +18133,22 @@ dwv.image.MaskFactory.prototype.create = function (
17871
18133
  }
17872
18134
  }
17873
18135
  // sort positions patient
17874
- posPats.sort(comparePosPat);
18136
+ framePosPats.sort(comparePosPat);
18137
+
18138
+ // add missing posPats
18139
+ var posPats = [];
18140
+ var sliceSpacing = spacing.get(2);
18141
+ for (var g = 0; g < framePosPats.length - 1; ++g) {
18142
+ posPats.push(framePosPats[g]);
18143
+ var nextZ = framePosPats[g][2] - sliceSpacing;
18144
+ var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18145
+ while (diff > sliceSpacing) {
18146
+ posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
18147
+ nextZ -= sliceSpacing;
18148
+ diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18149
+ }
18150
+ }
18151
+ posPats.push(framePosPats[framePosPats.length - 1]);
17875
18152
 
17876
18153
  // create output buffer
17877
18154
  // as many slices as posPats -> gap slices between groups are not represented
@@ -17956,9 +18233,13 @@ dwv.image = dwv.image || {};
17956
18233
  *
17957
18234
  * @class
17958
18235
  * @param {dwv.image.Spacing} spacing The spacing.
17959
- * @param {dwv.math.Matrix} orientation The orientation.
18236
+ * @param {dwv.math.Matrix} imageOrientation The image oientation.
18237
+ * @param {dwv.math.Matrix} viewOrientation The view orientation.
17960
18238
  */
17961
- dwv.image.PlaneHelper = function (spacing, orientation) {
18239
+ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18240
+
18241
+ var targetOrientation = dwv.gui.getTargetOrientation(
18242
+ imageOrientation, viewOrientation);
17962
18243
 
17963
18244
  /**
17964
18245
  * Get a 3D offset from a plane one.
@@ -17971,22 +18252,28 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
17971
18252
  var planeOffset = new dwv.math.Vector3D(
17972
18253
  offset2D.x, offset2D.y, 0);
17973
18254
  // de-orient
17974
- var pixelOffset = this.getDeOrientedVector3D(planeOffset);
17975
- // offset indexToWorld
17976
- return offsetIndexToWorld(pixelOffset);
18255
+ var pixelOffset = this.getTargetDeOrientedVector3D(planeOffset);
18256
+ // ~indexToWorld
18257
+ return new dwv.math.Vector3D(
18258
+ pixelOffset.getX() * spacing.get(0),
18259
+ pixelOffset.getY() * spacing.get(1),
18260
+ pixelOffset.getZ() * spacing.get(2));
17977
18261
  };
17978
18262
 
17979
18263
  /**
17980
18264
  * Get a plane offset from a 3D one.
17981
18265
  *
17982
- * @param {dwv.math.Point3D} offset3D The 3D offset.
18266
+ * @param {object} offset3D The 3D offset as {x,y,z}.
17983
18267
  * @returns {object} The plane offset as {x,y}.
17984
18268
  */
17985
18269
  this.getPlaneOffsetFromOffset3D = function (offset3D) {
17986
- // offset worldToIndex
17987
- var pixelOffset = offsetWorldToIndex(offset3D);
18270
+ // ~worldToIndex
18271
+ var pixelOffset = new dwv.math.Vector3D(
18272
+ offset3D.x / spacing.get(0),
18273
+ offset3D.y / spacing.get(1),
18274
+ offset3D.z / spacing.get(2));
17988
18275
  // orient
17989
- var planeOffset = this.getOrientedVector3D(pixelOffset);
18276
+ var planeOffset = this.getTargetOrientedVector3D(pixelOffset);
17990
18277
  // make 2D
17991
18278
  return {
17992
18279
  x: planeOffset.getX(),
@@ -17995,99 +18282,51 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
17995
18282
  };
17996
18283
 
17997
18284
  /**
17998
- * Apply spacing to an offset.
17999
- *
18000
- * @param {dwv.math.Point3D} off The 3D offset.
18001
- * @returns {dwv.math.Vector3D} The world offset.
18002
- */
18003
- function offsetIndexToWorld(off) {
18004
- return new dwv.math.Vector3D(
18005
- off.getX() * spacing.get(0),
18006
- off.getY() * spacing.get(1),
18007
- off.getZ() * spacing.get(2));
18008
- }
18009
-
18010
- /**
18011
- * Remove spacing from an offset.
18012
- *
18013
- * @param {object} off The world offset object as {x,y,z}.
18014
- * @returns {dwv.math.Vector3D} The 3D offset.
18015
- */
18016
- function offsetWorldToIndex(off) {
18017
- return new dwv.math.Vector3D(
18018
- off.x / spacing.get(0),
18019
- off.y / spacing.get(1),
18020
- off.z / spacing.get(2));
18021
- }
18022
-
18023
- /**
18024
- * Orient an input vector.
18285
+ * Orient an input vector from real to target space.
18025
18286
  *
18026
18287
  * @param {dwv.math.Vector3D} vector The input vector.
18027
18288
  * @returns {dwv.math.Vector3D} The oriented vector.
18028
18289
  */
18029
- this.getOrientedVector3D = function (vector) {
18290
+ this.getTargetOrientedVector3D = function (vector) {
18030
18291
  var planeVector = vector;
18031
- if (typeof orientation !== 'undefined') {
18032
- // abs? otherwise negative index...
18033
- // vector = orientation * planeVector
18034
- planeVector = orientation.getInverse().getAbs().multiplyVector3D(vector);
18292
+ if (typeof targetOrientation !== 'undefined') {
18293
+ planeVector = targetOrientation.getInverse().multiplyVector3D(vector);
18035
18294
  }
18036
18295
  return planeVector;
18037
18296
  };
18038
18297
 
18039
18298
  /**
18040
- * Orient an input index.
18041
- *
18042
- * @param {dwv.math.Index} index The input index.
18043
- * @returns {dwv.math.Index} The oriented index.
18044
- */
18045
- this.getOrientedIndex = function (index) {
18046
- var planeIndex = index;
18047
- if (typeof orientation !== 'undefined') {
18048
- // abs? otherwise negative index...
18049
- // vector = orientation * planeVector
18050
- planeIndex = orientation.getInverse().getAbs().multiplyIndex3D(index);
18051
- }
18052
- return planeIndex;
18053
- };
18054
-
18055
- /**
18056
- * Orient an input point.
18299
+ * De-orient an input vector from target to real space.
18057
18300
  *
18058
- * @param {dwv.math.Point3D} point The input point.
18059
- * @returns {dwv.math.Point3D} The oriented point.
18301
+ * @param {dwv.math.Vector3D} planeVector The input vector.
18302
+ * @returns {dwv.math.Vector3D} The de-orienteded vector.
18060
18303
  */
18061
- this.getOrientedPoint = function (point) {
18062
- var planePoint = point;
18063
- if (typeof orientation !== 'undefined') {
18064
- // abs? otherwise negative index...
18065
- // vector = orientation * planeVector
18066
- var point3D =
18067
- orientation.getInverse().getAbs().multiplyPoint3D(point.get3D());
18068
- planePoint = point.mergeWith3D(point3D);
18304
+ this.getTargetDeOrientedVector3D = function (planeVector) {
18305
+ var vector = planeVector;
18306
+ if (typeof targetOrientation !== 'undefined') {
18307
+ vector = targetOrientation.multiplyVector3D(planeVector);
18069
18308
  }
18070
- return planePoint;
18309
+ return vector;
18071
18310
  };
18072
18311
 
18073
18312
  /**
18074
- * De-orient an input vector.
18313
+ * Orient an input vector from target to image space.
18075
18314
  *
18076
18315
  * @param {dwv.math.Vector3D} planeVector The input vector.
18077
- * @returns {dwv.math.Vector3D} The de-orienteded vector.
18316
+ * @returns {dwv.math.Vector3D} The orienteded vector.
18078
18317
  */
18079
- this.getDeOrientedVector3D = function (planeVector) {
18318
+ this.getImageOrientedVector3D = function (planeVector) {
18080
18319
  var vector = planeVector;
18081
- if (typeof orientation !== 'undefined') {
18320
+ if (typeof viewOrientation !== 'undefined') {
18082
18321
  // abs? otherwise negative index...
18083
- // vector = orientation * planePoint
18084
- vector = orientation.getAbs().multiplyVector3D(planeVector);
18322
+ // vector = viewOrientation * planePoint
18323
+ vector = viewOrientation.getAbs().multiplyVector3D(planeVector);
18085
18324
  }
18086
18325
  return vector;
18087
18326
  };
18088
18327
 
18089
18328
  /**
18090
- * Reorder values to follow orientation.
18329
+ * Reorder values to follow view orientation.
18091
18330
  *
18092
18331
  * @param {object} values Values as {x,y,z}.
18093
18332
  * @returns {object} Reoriented values as {x,y,z}.
@@ -18099,7 +18338,7 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18099
18338
  values.y,
18100
18339
  values.z
18101
18340
  ],
18102
- orientation);
18341
+ viewOrientation);
18103
18342
  return {
18104
18343
  x: orientedValues[0],
18105
18344
  y: orientedValues[1],
@@ -18108,36 +18347,29 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18108
18347
  };
18109
18348
 
18110
18349
  /**
18111
- * Reorder values to compensate for orientation.
18350
+ * Get the (view) scroll dimension index.
18112
18351
  *
18113
- * @param {object} values Values as {x,y,z}.
18114
- * @returns {object} 'Deoriented' values as {x,y,z}.
18352
+ * @returns {number} The index.
18115
18353
  */
18116
- this.getDeOrientedXYZ = function (values) {
18117
- var deOrientedValues = dwv.math.getDeOrientedArray3D(
18118
- [
18119
- values.x,
18120
- values.y,
18121
- values.z
18122
- ],
18123
- orientation
18124
- );
18125
- return {
18126
- x: deOrientedValues[0],
18127
- y: deOrientedValues[1],
18128
- z: deOrientedValues[2]
18129
- };
18354
+ this.getScrollIndex = function () {
18355
+ var index = null;
18356
+ if (typeof viewOrientation !== 'undefined') {
18357
+ index = viewOrientation.getThirdColMajorDirection();
18358
+ } else {
18359
+ index = 2;
18360
+ }
18361
+ return index;
18130
18362
  };
18131
18363
 
18132
18364
  /**
18133
- * Get the scroll dimension index.
18365
+ * Get the native (image) scroll dimension index.
18134
18366
  *
18135
18367
  * @returns {number} The index.
18136
18368
  */
18137
- this.getScrollIndex = function () {
18369
+ this.getNativeScrollIndex = function () {
18138
18370
  var index = null;
18139
- if (typeof orientation !== 'undefined') {
18140
- index = orientation.getThirdColMajorDirection();
18371
+ if (typeof imageOrientation !== 'undefined') {
18372
+ index = imageOrientation.getThirdColMajorDirection();
18141
18373
  } else {
18142
18374
  index = 2;
18143
18375
  }
@@ -18489,9 +18721,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
18489
18721
  * Check that an index is within bounds.
18490
18722
  *
18491
18723
  * @param {dwv.math.Index} index The index to check.
18724
+ * @param {number} dir Optional direction to check.
18492
18725
  * @returns {boolean} True if the given coordinates are within bounds.
18493
18726
  */
18494
- dwv.image.Size.prototype.isInBounds = function (index) {
18727
+ dwv.image.Size.prototype.isInBounds = function (index, dir) {
18495
18728
  // check input
18496
18729
  if (!index) {
18497
18730
  return false;
@@ -18501,11 +18734,20 @@ dwv.image.Size.prototype.isInBounds = function (index) {
18501
18734
  if (length !== index.length()) {
18502
18735
  return false;
18503
18736
  }
18504
- // check values
18505
- for (var i = 0; i < length; ++i) {
18506
- if (index.get(i) < 0 || index.get(i) > this.get(i) - 1) {
18737
+ // check values is 0 <= v < size
18738
+ var inBound = function (value, size) {
18739
+ return value >= 0 && value < size;
18740
+ };
18741
+ if (typeof dir !== 'undefined') {
18742
+ if (!inBound(index.get(dir), this.get(dir))) {
18507
18743
  return false;
18508
18744
  }
18745
+ } else {
18746
+ for (var i = 0; i < length; ++i) {
18747
+ if (!inBound(index.get(i), this.get(i))) {
18748
+ return false;
18749
+ }
18750
+ }
18509
18751
  }
18510
18752
  // seems ok!
18511
18753
  return true;
@@ -18756,11 +18998,12 @@ dwv.image.View = function (image) {
18756
18998
  var colourMap = dwv.image.lut.plain;
18757
18999
  /**
18758
19000
  * Current position as a Point3D.
19001
+ * Store position and not index to stay geometry independent.
18759
19002
  *
18760
19003
  * @private
18761
19004
  * @type {dwv.math.Index}
18762
19005
  */
18763
- var currentIndex = null;
19006
+ var currentPosition = null;
18764
19007
  /**
18765
19008
  * View orientation. Undefined will use the original slice ordering.
18766
19009
  *
@@ -18898,6 +19141,7 @@ dwv.image.View = function (image) {
18898
19141
  if (!this.getCurrentIndex()) {
18899
19142
  this.setInitialIndex();
18900
19143
  }
19144
+ var currentIndex = this.getCurrentIndex();
18901
19145
  // use current rsi if not provided
18902
19146
  if (typeof rsi === 'undefined') {
18903
19147
  rsi = image.getRescaleSlopeAndIntercept(currentIndex);
@@ -19080,8 +19324,7 @@ dwv.image.View = function (image) {
19080
19324
  * @returns {dwv.math.Point} The current position.
19081
19325
  */
19082
19326
  this.getCurrentPosition = function () {
19083
- var geometry = this.getImage().getGeometry();
19084
- return geometry.indexToWorld(currentIndex);
19327
+ return currentPosition;
19085
19328
  };
19086
19329
 
19087
19330
  /**
@@ -19090,7 +19333,41 @@ dwv.image.View = function (image) {
19090
19333
  * @returns {dwv.math.Index} The current index.
19091
19334
  */
19092
19335
  this.getCurrentIndex = function () {
19093
- return currentIndex;
19336
+ var position = this.getCurrentPosition();
19337
+ if (!position) {
19338
+ return null;
19339
+ }
19340
+ var geometry = this.getImage().getGeometry();
19341
+ return geometry.worldToIndex(position);
19342
+ };
19343
+
19344
+ /**
19345
+ * Check is the provided position can be set.
19346
+ *
19347
+ * @param {dwv.math.Point} position The position.
19348
+ * @returns {boolean} True is the position is in bounds.
19349
+ */
19350
+ this.canSetPosition = function (position) {
19351
+ var geometry = image.getGeometry();
19352
+ var index = geometry.worldToIndex(position);
19353
+ return geometry.isIndexInBounds(index, this.getScrollIndex());
19354
+ };
19355
+
19356
+ /**
19357
+ * Get the origin at a given position.
19358
+ *
19359
+ * @param {dwv.math.Point} position The position.
19360
+ * @returns {dwv.math.Point} The origin.
19361
+ */
19362
+ this.getOrigin = function (position) {
19363
+ var geometry = image.getGeometry();
19364
+ var originIndex = 0;
19365
+ if (typeof position !== 'undefined') {
19366
+ var index = geometry.worldToIndex(position);
19367
+ // index is reoriented, 2 is scroll index
19368
+ originIndex = index.get(2);
19369
+ }
19370
+ return geometry.getOrigins()[originIndex];
19094
19371
  };
19095
19372
 
19096
19373
  /**
@@ -19105,7 +19382,7 @@ dwv.image.View = function (image) {
19105
19382
  // send invalid event if not in bounds
19106
19383
  var geometry = image.getGeometry();
19107
19384
  var index = geometry.worldToIndex(position);
19108
- if (!geometry.isIndexInBounds(index)) {
19385
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19109
19386
  if (!silent) {
19110
19387
  // fire event with valid: false
19111
19388
  fireEvent({
@@ -19127,7 +19404,7 @@ dwv.image.View = function (image) {
19127
19404
  *
19128
19405
  * @param {dwv.math.Index} index The new index.
19129
19406
  * @param {boolean} silent Flag to fire event or not.
19130
- * @returns {boolean} False if not in bounds
19407
+ * @returns {boolean} False if not in bounds.
19131
19408
  * @fires dwv.image.View#positionchange
19132
19409
  */
19133
19410
  this.setCurrentIndex = function (index, silent) {
@@ -19140,70 +19417,72 @@ dwv.image.View = function (image) {
19140
19417
  var position = geometry.indexToWorld(index);
19141
19418
 
19142
19419
  // check if possible
19143
- if (!geometry.isIndexInBounds(index)) {
19420
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19421
+ // do no send invalid positionchange event: avoid empty repaint
19144
19422
  return false;
19145
19423
  }
19146
19424
 
19147
- var isNew = !currentIndex || !currentIndex.equals(index);
19148
-
19149
- if (isNew) {
19150
- var diffDims = null;
19151
- if (currentIndex) {
19152
- if (currentIndex.canCompare(index)) {
19153
- diffDims = currentIndex.compare(index);
19154
- } else {
19155
- diffDims = [];
19156
- var minLen = Math.min(currentIndex.length(), index.length());
19157
- for (var i = 0; i < minLen; ++i) {
19158
- if (currentIndex.get(i) !== index.get(i)) {
19159
- diffDims.push(i);
19160
- }
19161
- }
19162
- var maxLen = Math.max(currentIndex.length(), index.length());
19163
- for (var j = minLen; j < maxLen; ++j) {
19164
- diffDims.push(j);
19165
- }
19166
- }
19425
+ // calculate diff dims before updating internal
19426
+ var diffDims = null;
19427
+ var currentIndex = null;
19428
+ if (this.getCurrentPosition()) {
19429
+ currentIndex = this.getCurrentIndex();
19430
+ }
19431
+ if (currentIndex) {
19432
+ if (currentIndex.canCompare(index)) {
19433
+ diffDims = currentIndex.compare(index);
19167
19434
  } else {
19168
19435
  diffDims = [];
19169
- for (var k = 0; k < index.length(); ++k) {
19170
- diffDims.push(k);
19436
+ var minLen = Math.min(currentIndex.length(), index.length());
19437
+ for (var i = 0; i < minLen; ++i) {
19438
+ if (currentIndex.get(i) !== index.get(i)) {
19439
+ diffDims.push(i);
19440
+ }
19441
+ }
19442
+ var maxLen = Math.max(currentIndex.length(), index.length());
19443
+ for (var j = minLen; j < maxLen; ++j) {
19444
+ diffDims.push(j);
19171
19445
  }
19172
19446
  }
19447
+ } else {
19448
+ diffDims = [];
19449
+ for (var k = 0; k < index.length(); ++k) {
19450
+ diffDims.push(k);
19451
+ }
19452
+ }
19173
19453
 
19174
- // assign
19175
- currentIndex = index;
19176
-
19177
- if (!silent) {
19178
- /**
19179
- * Position change event.
19180
- *
19181
- * @event dwv.image.View#positionchange
19182
- * @type {object}
19183
- * @property {Array} value The changed value as [index, pixelValue].
19184
- * @property {Array} diffDims An array of modified indices.
19185
- */
19186
- var posEvent = {
19187
- type: 'positionchange',
19188
- value: [
19189
- index.getValues(),
19190
- position.getValues(),
19191
- ],
19192
- diffDims: diffDims,
19193
- data: {
19194
- imageUid: image.getImageUid(index)
19195
- }
19196
- };
19454
+ // assign
19455
+ currentPosition = position;
19197
19456
 
19198
- // add value if possible
19199
- if (image.canQuantify()) {
19200
- var pixValue = image.getRescaledValueAtIndex(index);
19201
- posEvent.value.push(pixValue);
19457
+ if (!silent) {
19458
+ /**
19459
+ * Position change event.
19460
+ *
19461
+ * @event dwv.image.View#positionchange
19462
+ * @type {object}
19463
+ * @property {Array} value The changed value as [index, pixelValue].
19464
+ * @property {Array} diffDims An array of modified indices.
19465
+ */
19466
+ var posEvent = {
19467
+ type: 'positionchange',
19468
+ value: [
19469
+ index.getValues(),
19470
+ position.getValues(),
19471
+ ],
19472
+ diffDims: diffDims,
19473
+ data: {
19474
+ imageUid: image.getImageUid(index)
19202
19475
  }
19476
+ };
19203
19477
 
19204
- // fire
19205
- fireEvent(posEvent);
19478
+ // add value if possible
19479
+ if (image.canQuantify()) {
19480
+ var pixValue = image.getRescaledValueAtIndex(index);
19481
+ posEvent.value.push(pixValue);
19206
19482
  }
19483
+
19484
+ // fire
19485
+ fireEvent(posEvent);
19207
19486
  }
19208
19487
 
19209
19488
  // all good
@@ -19369,7 +19648,7 @@ dwv.image.View.prototype.getWindowLevelMinMax = function () {
19369
19648
  var width = max - min;
19370
19649
  // full black / white images, defaults to 1.
19371
19650
  if (width < 1) {
19372
- dwv.logger.warn('Zero or negative width, defaulting to one.');
19651
+ dwv.logger.warn('Zero or negative window width, defaulting to one.');
19373
19652
  width = 1;
19374
19653
  }
19375
19654
  var center = min + width / 2;
@@ -23706,6 +23985,60 @@ dwv.math.Ellipse.prototype.quantify = function (viewController, flags) {
23706
23985
  return quant;
23707
23986
  };
23708
23987
 
23988
+ /**
23989
+ * Get the indices that form a ellpise.
23990
+ *
23991
+ * @param {dwv.math.Index} center The ellipse center.
23992
+ * @param {Array} radius The 2 ellipse radiuses.
23993
+ * @param {Array} dir The 2 ellipse directions.
23994
+ * @returns {Array} The indices of the ellipse.
23995
+ */
23996
+ dwv.math.getEllipseIndices = function (center, radius, dir) {
23997
+ var centerValues = center.getValues();
23998
+ // keep all values for possible extra dimensions
23999
+ var values = centerValues.slice();
24000
+ var indices = [];
24001
+ var radiusI = radius[0];
24002
+ var radiusJ = radius[1];
24003
+ var radiusRatio = radiusI / radiusJ;
24004
+ var radiusJ2 = Math.pow(radiusJ, 2);
24005
+ var di = dir[0];
24006
+ var dj = dir[1];
24007
+ // deduce 4 positions from top right
24008
+ for (var j = 0; j < radiusJ; ++j) {
24009
+ // right triangle formed by radiuses, j and len
24010
+ // ellipse: i*i / a*a + j*j / b*b = 1
24011
+ // -> i = a/b * sqrt(b*b - j*j)
24012
+ var len = Math.round(
24013
+ radiusRatio * Math.sqrt(radiusJ2 - Math.pow(j, 2)));
24014
+ var jmax = centerValues[dj] + j;
24015
+ var jmin = centerValues[dj] - j;
24016
+ for (var i = 0; i < len; ++i) {
24017
+ var imax = centerValues[di] + i;
24018
+ var imin = centerValues[di] - i;
24019
+
24020
+ // right
24021
+ values[di] = imax;
24022
+ // right - top
24023
+ values[dj] = jmax;
24024
+ indices.push(new dwv.math.Index(values.slice()));
24025
+ // right - bottom
24026
+ values[dj] = jmin;
24027
+ indices.push(new dwv.math.Index(values.slice()));
24028
+
24029
+ // left
24030
+ values[di] = imin;
24031
+ // left - top
24032
+ values[dj] = jmax;
24033
+ indices.push(new dwv.math.Index(values.slice()));
24034
+ // left - bottom
24035
+ values[dj] = jmin;
24036
+ indices.push(new dwv.math.Index(values.slice()));
24037
+ }
24038
+ }
24039
+ return indices;
24040
+ };
24041
+
23709
24042
  // namespaces
23710
24043
  var dwv = dwv || {};
23711
24044
  dwv.math = dwv.math || {};
@@ -23879,6 +24212,21 @@ dwv.math.getZeroIndex = function (size) {
23879
24212
  return new dwv.math.Index(values);
23880
24213
  };
23881
24214
 
24215
+ /**
24216
+ * Get an array sort callback.
24217
+ * f(a,b) > 0 -> b,a
24218
+ * f(a,b) < 0 -> a,b
24219
+ * f(a,b) = 0 -> original order
24220
+ *
24221
+ * @param {number} direction The direction to use to compare indices.
24222
+ * @returns {Function} A function that compares two dwv.math.Index.
24223
+ */
24224
+ dwv.math.getIndexCompareFunction = function (direction) {
24225
+ return function (a, b) {
24226
+ return a.get(direction) - b.get(direction);
24227
+ };
24228
+ };
24229
+
23882
24230
  /**
23883
24231
  * Get a string id from the index values in the form of: '#0-1_#1-2'.
23884
24232
  *
@@ -24208,6 +24556,9 @@ dwv.math.isSimilar = function (a, b, tol) {
24208
24556
  * @class
24209
24557
  */
24210
24558
  dwv.math.Matrix33 = function (values) {
24559
+ // matrix inverse, calculated at first ask
24560
+ var inverse = null;
24561
+
24211
24562
  /**
24212
24563
  * Get a value of the matrix.
24213
24564
  *
@@ -24218,6 +24569,20 @@ dwv.math.Matrix33 = function (values) {
24218
24569
  this.get = function (row, col) {
24219
24570
  return values[row * 3 + col];
24220
24571
  };
24572
+
24573
+ /**
24574
+ * Get the inverse of this matrix.
24575
+ *
24576
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24577
+ * if the determinant is zero.
24578
+ */
24579
+ this.getInverse = function () {
24580
+ if (inverse === null) {
24581
+ inverse = dwv.math.getMatrixInverse(this);
24582
+ }
24583
+ return inverse;
24584
+ };
24585
+
24221
24586
  }; // Matrix33
24222
24587
 
24223
24588
  /**
@@ -24229,15 +24594,16 @@ dwv.math.Matrix33 = function (values) {
24229
24594
  * @returns {boolean} True if both matrices are equal.
24230
24595
  */
24231
24596
  dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24232
- return dwv.math.isSimilar(this.get(0, 0), rhs.get(0, 0), p) &&
24233
- dwv.math.isSimilar(this.get(0, 1), rhs.get(0, 1), p) &&
24234
- dwv.math.isSimilar(this.get(0, 2), rhs.get(0, 2), p) &&
24235
- dwv.math.isSimilar(this.get(1, 0), rhs.get(1, 0), p) &&
24236
- dwv.math.isSimilar(this.get(1, 1), rhs.get(1, 1), p) &&
24237
- dwv.math.isSimilar(this.get(1, 2), rhs.get(1, 2), p) &&
24238
- dwv.math.isSimilar(this.get(2, 0), rhs.get(2, 0), p) &&
24239
- dwv.math.isSimilar(this.get(2, 1), rhs.get(2, 1), p) &&
24240
- dwv.math.isSimilar(this.get(2, 2), rhs.get(2, 2), p);
24597
+ // TODO: add type check
24598
+ // check values
24599
+ for (var i = 0; i < 3; ++i) {
24600
+ for (var j = 0; j < 3; ++j) {
24601
+ if (!dwv.math.isSimilar(this.get(i, j), rhs.get(i, j), p)) {
24602
+ return false;
24603
+ }
24604
+ }
24605
+ }
24606
+ return true;
24241
24607
  };
24242
24608
 
24243
24609
  /**
@@ -24246,10 +24612,20 @@ dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24246
24612
  * @returns {string} The matrix as a string.
24247
24613
  */
24248
24614
  dwv.math.Matrix33.prototype.toString = function () {
24249
- return '[' + this.get(0, 0) + ', ' + this.get(0, 1) + ', ' + this.get(0, 2) +
24250
- '\n ' + this.get(1, 0) + ', ' + this.get(1, 1) + ', ' + this.get(1, 2) +
24251
- '\n ' + this.get(2, 0) + ', ' + this.get(2, 1) + ', ' + this.get(2, 2) +
24252
- ']';
24615
+ var str = '[';
24616
+ for (var i = 0; i < 3; ++i) {
24617
+ if (i !== 0) {
24618
+ str += ', \n ';
24619
+ }
24620
+ for (var j = 0; j < 3; ++j) {
24621
+ if (j !== 0) {
24622
+ str += ', ';
24623
+ }
24624
+ str += this.get(i, j);
24625
+ }
24626
+ }
24627
+ str += ']';
24628
+ return str;
24253
24629
  };
24254
24630
 
24255
24631
  /**
@@ -24347,42 +24723,46 @@ dwv.math.Matrix33.prototype.multiplyIndex3D = function (index3D) {
24347
24723
  };
24348
24724
 
24349
24725
  /**
24350
- * Get the inverse of this matrix.
24726
+ * Get the inverse of an input 3*3 matrix.
24351
24727
  *
24352
- * @returns {dwv.math.Matrix33} The inverse matrix.
24728
+ * @param {dwv.math.Matrix33} m The input matrix.
24729
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24730
+ * if the determinant is zero.
24353
24731
  * @see https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
24354
- */
24355
- dwv.math.Matrix33.prototype.getInverse = function () {
24356
- var a = this.get(0, 0);
24357
- var b = this.get(0, 1);
24358
- var c = this.get(0, 2);
24359
- var d = this.get(1, 0);
24360
- var e = this.get(1, 1);
24361
- var f = this.get(1, 2);
24362
- var g = this.get(2, 0);
24363
- var h = this.get(2, 1);
24364
- var i = this.get(2, 2);
24365
-
24366
- var a2 = e * i - f * h;
24367
- var b2 = f * g - d * i;
24368
- var c2 = d * h - e * g;
24369
-
24370
- var det = a * a2 + b * b2 + c * c2;
24732
+ * @see https://github.com/willnode/N-Matrix-Programmer
24733
+ */
24734
+ dwv.math.getMatrixInverse = function (m) {
24735
+ var m00 = m.get(0, 0);
24736
+ var m01 = m.get(0, 1);
24737
+ var m02 = m.get(0, 2);
24738
+ var m10 = m.get(1, 0);
24739
+ var m11 = m.get(1, 1);
24740
+ var m12 = m.get(1, 2);
24741
+ var m20 = m.get(2, 0);
24742
+ var m21 = m.get(2, 1);
24743
+ var m22 = m.get(2, 2);
24744
+
24745
+ var a1212 = m11 * m22 - m12 * m21;
24746
+ var a2012 = m12 * m20 - m10 * m22;
24747
+ var a0112 = m10 * m21 - m11 * m20;
24748
+
24749
+ var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
24371
24750
  if (det === 0) {
24372
- dwv.logger.warn('Cannot invert matrix with zero determinant.');
24751
+ dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
24373
24752
  return;
24374
24753
  }
24754
+ det = 1 / det;
24375
24755
 
24376
24756
  var values = [
24377
- a2 / det,
24378
- (c * h - b * i) / det,
24379
- (b * f - c * e) / det,
24380
- b2 / det,
24381
- (a * i - c * g) / det,
24382
- (c * d - a * f) / det,
24383
- c2 / det,
24384
- (b * g - a * h) / det,
24385
- (a * e - b * d) / det
24757
+ det * a1212,
24758
+ det * (m02 * m21 - m01 * m22),
24759
+ det * (m01 * m12 - m02 * m11),
24760
+ det * a2012,
24761
+ det * (m00 * m22 - m02 * m20),
24762
+ det * (m02 * m10 - m00 * m12),
24763
+ det * a0112,
24764
+ det * (m01 * m20 - m00 * m21),
24765
+ det * (m00 * m11 - m01 * m10)
24386
24766
  ];
24387
24767
 
24388
24768
  return new dwv.math.Matrix33(values);
@@ -24793,6 +25173,21 @@ dwv.math.Point3D.prototype.equals = function (rhs) {
24793
25173
  this.getZ() === rhs.getZ();
24794
25174
  };
24795
25175
 
25176
+ /**
25177
+ * Check for Point3D similarity.
25178
+ *
25179
+ * @param {dwv.math.Point3D} rhs The other point to compare to.
25180
+ * @param {number} tol Optional comparison tolerance,
25181
+ * default to Number.EPSILON.
25182
+ * @returns {boolean} True if both points are equal.
25183
+ */
25184
+ dwv.math.Point3D.prototype.isSimilar = function (rhs, tol) {
25185
+ return rhs !== null &&
25186
+ dwv.math.isSimilar(this.getX(), rhs.getX(), tol) &&
25187
+ dwv.math.isSimilar(this.getY(), rhs.getY(), tol) &&
25188
+ dwv.math.isSimilar(this.getZ(), rhs.getZ(), tol);
25189
+ };
25190
+
24796
25191
  /**
24797
25192
  * Get a string representation of the Point3D.
24798
25193
  *
@@ -24830,6 +25225,19 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
24830
25225
  (this.getZ() - point3D.getZ()));
24831
25226
  };
24832
25227
 
25228
+ /**
25229
+ * Get an array find callback for a given point.
25230
+ *
25231
+ * @param {dwv.math.Point3D} point The point to compare to.
25232
+ * @returns {Function} A function that compares its input point to the one
25233
+ * given as input to this function.
25234
+ */
25235
+ dwv.math.getEqualPoint3DFunction = function (point) {
25236
+ return function (element) {
25237
+ return element.equals(point);
25238
+ };
25239
+ };
25240
+
24833
25241
  /**
24834
25242
  * Immutable point.
24835
25243
  * Warning: the input array is NOT cloned, modifying it will
@@ -27132,11 +27540,15 @@ dwv.tool.Draw = function (app) {
27132
27540
  };
27133
27541
 
27134
27542
  /**
27135
- * Handle double click event.
27543
+ * Handle double click event: some tools use it to finish interaction.
27136
27544
  *
27137
- * @param {object} event The mouse up event.
27545
+ * @param {object} event The double click event.
27138
27546
  */
27139
27547
  this.dblclick = function (event) {
27548
+ // only end by double click undefined NPoints
27549
+ if (typeof currentFactory.getNPoints() !== 'undefined') {
27550
+ return;
27551
+ }
27140
27552
  // exit if not started draw
27141
27553
  if (!started) {
27142
27554
  return;
@@ -30078,10 +30490,11 @@ dwv.tool.draw.FreeHandFactory = function () {
30078
30490
  /**
30079
30491
  * Get the number of points needed to build the shape.
30080
30492
  *
30081
- * @returns {number} The number of points.
30493
+ * @returns {number|undefined} The number of points.
30082
30494
  */
30083
30495
  this.getNPoints = function () {
30084
- return 1000;
30496
+ // undefined to end with double click
30497
+ return undefined;
30085
30498
  };
30086
30499
  /**
30087
30500
  * Get the timeout between point storage.
@@ -31509,10 +31922,11 @@ dwv.tool.draw.RoiFactory = function () {
31509
31922
  /**
31510
31923
  * Get the number of points needed to build the shape.
31511
31924
  *
31512
- * @returns {number} The number of points.
31925
+ * @returns {number|undefined} The number of points.
31513
31926
  */
31514
31927
  this.getNPoints = function () {
31515
- return 50;
31928
+ // undefined to end with double click
31929
+ return undefined;
31516
31930
  };
31517
31931
  /**
31518
31932
  * Get the timeout between point storage.
@@ -32429,11 +32843,16 @@ dwv.tool.WindowLevel = function (app) {
32429
32843
  // difference to last position
32430
32844
  var diffX = event._x - self.x0;
32431
32845
  var diffY = self.y0 - event._y;
32846
+ // data range
32847
+ var range = viewController.getImageRescaledDataRange();
32848
+ // 1/1000 seems to give reasonable results...
32849
+ var pixelToIntensity = (range.max - range.min) * 0.01;
32850
+
32432
32851
  // calculate new window level
32433
- var windowCenter =
32434
- parseInt(viewController.getWindowLevel().center, 10) + diffY;
32435
- var windowWidth =
32436
- parseInt(viewController.getWindowLevel().width, 10) + diffX;
32852
+ var center = parseInt(viewController.getWindowLevel().center, 10);
32853
+ var width = parseInt(viewController.getWindowLevel().width, 10);
32854
+ var windowCenter = center + Math.round(diffY * pixelToIntensity);
32855
+ var windowWidth = width + Math.round(diffX * pixelToIntensity);
32437
32856
  // bound window width
32438
32857
  windowWidth = dwv.image.validateWindowWidth(windowWidth);
32439
32858