dwv 0.31.0-beta.4 → 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.4 2022-04-21 16:49: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) {
@@ -569,6 +569,16 @@ dwv.App = function () {
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
  *
@@ -651,7 +661,8 @@ dwv.App = function () {
651
661
  var layerGroup = stage.getActiveLayerGroup();
652
662
  var viewController = layerGroup.getActiveViewLayer().getViewController();
653
663
  var k = viewController.getCurrentScrollPosition();
654
- layerGroup.addScale(step, {x: cx, y: cy, z: k});
664
+ var center = new dwv.math.Point3D(cx, cy, k);
665
+ layerGroup.addScale(step, center);
655
666
  layerGroup.draw();
656
667
  };
657
668
 
@@ -1241,7 +1252,7 @@ dwv.App = function () {
1241
1252
  var data = dataController.get(indices[i]);
1242
1253
  var geometry = data.image.getGeometry();
1243
1254
  var viewOrient = dwv.gui.getViewOrientation(
1244
- geometry,
1255
+ geometry.getOrientation(),
1245
1256
  layerGroup.getTargetOrientation()
1246
1257
  );
1247
1258
  var size = geometry.getSize(viewOrient).get2D();
@@ -1285,7 +1296,7 @@ dwv.App = function () {
1285
1296
  new dwv.dicom.DicomElementsWrapper(data.meta),
1286
1297
  data.image);
1287
1298
  var viewOrientation = dwv.gui.getViewOrientation(
1288
- imageGeometry,
1299
+ imageGeometry.getOrientation(),
1289
1300
  layerGroup.getTargetOrientation()
1290
1301
  );
1291
1302
  view.setOrientation(viewOrientation);
@@ -1326,8 +1337,9 @@ dwv.App = function () {
1326
1337
  viewLayer.setView(view);
1327
1338
  var size2D = imageGeometry.getSize(viewOrientation).get2D();
1328
1339
  var spacing2D = imageGeometry.getSpacing(viewOrientation).get2D();
1329
- viewLayer.initialise(size2D, spacing2D, dataIndex);
1330
- viewLayer.setOpacity(opacity);
1340
+ viewLayer.initialise(size2D, spacing2D, dataIndex, opacity);
1341
+ viewLayer.setScale(layerGroup.getScale());
1342
+ viewLayer.setOffset(layerGroup.getOffset());
1331
1343
 
1332
1344
  // listen to image changes
1333
1345
  dataController.addEventListener('imagechange', viewLayer.onimagechange);
@@ -1341,13 +1353,16 @@ dwv.App = function () {
1341
1353
  dl.initialise(size2D, spacing2D, dataIndex);
1342
1354
  dl.setPlaneHelper(viewLayer.getViewController().getPlaneHelper());
1343
1355
 
1356
+ // force positionchange to sync layers
1344
1357
  var vc = viewLayer.getViewController();
1345
- // positionchange event like data
1346
1358
  var value = [
1347
1359
  vc.getCurrentIndex().getValues(),
1348
1360
  vc.getCurrentPosition().getValues()
1349
1361
  ];
1350
- layerGroup.updateLayersToPositionChange({value: value});
1362
+ layerGroup.updateLayersToPositionChange({
1363
+ value: value,
1364
+ srclayerid: viewLayer.getId()
1365
+ });
1351
1366
  }
1352
1367
 
1353
1368
  // fit to the maximum size
@@ -2886,9 +2901,46 @@ dwv.ctrl.ViewController = function (view) {
2886
2901
  * @returns {dwv.image.Size} The size.
2887
2902
  */
2888
2903
  this.getImageSize = function () {
2889
- 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();
2890
2914
  };
2891
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
+ */
2892
2944
  this.canSetPosition = function (position) {
2893
2945
  return view.canSetPosition(position);
2894
2946
  };
@@ -2949,7 +3001,7 @@ dwv.ctrl.ViewController = function (view) {
2949
3001
  var k = this.getCurrentScrollIndexValue();
2950
3002
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2951
3003
  // de-orient
2952
- var point = planeHelper.getDeOrientedVector3D(planePoint);
3004
+ var point = planeHelper.getTargetDeOrientedVector3D(planePoint);
2953
3005
  // ~indexToWorld to not loose precision
2954
3006
  var geometry = view.getImage().getGeometry();
2955
3007
  var spacing = geometry.getRealSpacing();
@@ -4496,7 +4548,7 @@ dwv.dicom = dwv.dicom || {};
4496
4548
  * @returns {string} The version of the library.
4497
4549
  */
4498
4550
  dwv.getVersion = function () {
4499
- return '0.31.0-beta.4';
4551
+ return '0.31.0-beta.5';
4500
4552
  };
4501
4553
 
4502
4554
  /**
@@ -11870,27 +11922,44 @@ dwv.gui.getLayerDetailsFromEvent = function (event) {
11870
11922
  };
11871
11923
 
11872
11924
  /**
11873
- * Get a view orientation according to an image geometry (with its orientation)
11874
- * 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.
11875
11927
  *
11876
- * @param {dwv.image.Geometry} imageGeometry The image geometry.
11928
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11877
11929
  * @param {dwv.math.Matrix33} targetOrientation The target orientation.
11878
11930
  * @returns {dwv.math.Matrix33} The view orientation.
11879
11931
  */
11880
- dwv.gui.getViewOrientation = function (imageGeometry, targetOrientation) {
11932
+ dwv.gui.getViewOrientation = function (imageOrientation, targetOrientation) {
11881
11933
  var viewOrientation = dwv.math.getIdentityMat33();
11882
11934
  if (typeof targetOrientation !== 'undefined') {
11883
- // image orientation as one and zeros
11884
- // -> view orientation is one and zeros
11885
- var imgOrientation = imageGeometry.getOrientation().asOneAndZeros();
11886
- // imgOrientation * viewOrientation = targetOrientation
11887
- // -> 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...
11888
11940
  viewOrientation =
11889
- imgOrientation.getInverse().multiply(targetOrientation);
11941
+ imageOrientation.asOneAndZeros().getInverse().multiply(targetOrientation);
11890
11942
  }
11891
11943
  return viewOrientation;
11892
11944
  };
11893
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
+
11894
11963
  /**
11895
11964
  * Layer group.
11896
11965
  *
@@ -12085,6 +12154,24 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12085
12154
  return res;
12086
12155
  };
12087
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
+
12088
12175
  /**
12089
12176
  * Get the view layers data indices.
12090
12177
  *
@@ -12284,8 +12371,8 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12284
12371
  var index = new dwv.math.Index(event.value[0]);
12285
12372
  var position = new dwv.math.Point(event.value[1]);
12286
12373
  // origin of the first view layer
12287
- var baseOrigin0 = null;
12288
- var baseOrigin = null;
12374
+ var baseViewLayerOrigin0 = null;
12375
+ var baseViewLayerOrigin = null;
12289
12376
  // update position for all layers except the source one
12290
12377
  for (var i = 0; i < layers.length; ++i) {
12291
12378
 
@@ -12294,28 +12381,25 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12294
12381
  var hasSetOffset = false;
12295
12382
  if (layers[i] instanceof dwv.gui.ViewLayer) {
12296
12383
  var vc = layers[i].getViewController();
12384
+ // origin0 should always be there
12297
12385
  var origin0 = vc.getOrigin();
12386
+ // depending on position, origin could be undefined
12298
12387
  var origin = vc.getOrigin(position);
12299
- if (!baseOrigin) {
12300
- baseOrigin0 = origin0;
12301
- baseOrigin = origin;
12302
- } else {
12303
- if (vc.canSetPosition(position)) {
12304
- if (typeof origin0 === 'undefined') {
12305
- console.log('no origin0', layers[i].getId());
12306
- }
12307
- if (typeof origin === 'undefined') {
12308
- console.log('no origin', layers[i].getId());
12309
- }
12310
12388
 
12311
- // TODO: compensate for possible different orientation
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
12312
12396
  // TODO: check why -z...
12313
12397
 
12314
- var scrollDiff = baseOrigin0.minus(origin0);
12398
+ var scrollDiff = baseViewLayerOrigin0.minus(origin0);
12315
12399
  var scrollOffset = new dwv.math.Vector3D(
12316
12400
  scrollDiff.getX(), scrollDiff.getY(), -1 * scrollDiff.getZ());
12317
12401
 
12318
- var planeDiff = baseOrigin.minus(origin);
12402
+ var planeDiff = baseViewLayerOrigin.minus(origin);
12319
12403
  var planeOffset = new dwv.math.Vector3D(
12320
12404
  planeDiff.getX(), planeDiff.getY(), -1 * planeDiff.getZ());
12321
12405
 
@@ -13200,12 +13284,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13200
13284
  var context = null;
13201
13285
 
13202
13286
  /**
13203
- * Flag to know if the context has been cleared.
13287
+ * Flag to know if the current position is valid.
13204
13288
  *
13205
13289
  * @private
13206
13290
  * @type {boolean}
13207
13291
  */
13208
- var isContextClear = true;
13292
+ var isValidPosition = true;
13209
13293
 
13210
13294
  /**
13211
13295
  * The image data array.
@@ -13350,6 +13434,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
13350
13434
  // event.value = [index, image]
13351
13435
  if (dataIndex === event.value[0]) {
13352
13436
  viewController.setImage(event.value[1]);
13437
+ setBaseSize(viewController.getImageSize().get2D());
13353
13438
  needsDataUpdate = true;
13354
13439
  }
13355
13440
  };
@@ -13389,6 +13474,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
13389
13474
  * @param {number} alpha The opacity ([0:1] range).
13390
13475
  */
13391
13476
  this.setOpacity = function (alpha) {
13477
+ if (alpha === opacity) {
13478
+ return;
13479
+ }
13480
+
13392
13481
  opacity = Math.min(Math.max(alpha, 0), 1);
13393
13482
 
13394
13483
  /**
@@ -13546,6 +13635,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13546
13635
  * @fires dwv.App#renderend
13547
13636
  */
13548
13637
  this.draw = function () {
13638
+ // skip for non valid position
13639
+ if (!isValidPosition) {
13640
+ return;
13641
+ }
13642
+
13549
13643
  /**
13550
13644
  * Render start event.
13551
13645
  *
@@ -13555,7 +13649,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13555
13649
  */
13556
13650
  var event = {
13557
13651
  type: 'renderstart',
13558
- layerid: this.getId()
13652
+ layerid: this.getId(),
13653
+ dataid: this.getDataIndex()
13559
13654
  };
13560
13655
  fireEvent(event);
13561
13656
 
@@ -13590,9 +13685,6 @@ dwv.gui.ViewLayer = function (containerDiv) {
13590
13685
  // draw image
13591
13686
  context.drawImage(offscreenCanvas, 0, 0);
13592
13687
 
13593
- // set clear flag
13594
- isContextClear = false;
13595
-
13596
13688
  /**
13597
13689
  * Render end event.
13598
13690
  *
@@ -13602,7 +13694,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13602
13694
  */
13603
13695
  event = {
13604
13696
  type: 'renderend',
13605
- layerid: this.getId()
13697
+ layerid: this.getId(),
13698
+ dataid: this.getDataIndex()
13606
13699
  };
13607
13700
  fireEvent(event);
13608
13701
  };
@@ -13613,12 +13706,13 @@ dwv.gui.ViewLayer = function (containerDiv) {
13613
13706
  * @param {object} size The image size as {x,y}.
13614
13707
  * @param {object} spacing The image spacing as {x,y}.
13615
13708
  * @param {number} index The associated data index.
13709
+ * @param {number} alpha The initial data opacity.
13616
13710
  */
13617
- this.initialise = function (size, spacing, index) {
13711
+ this.initialise = function (size, spacing, index, alpha) {
13618
13712
  // set locals
13619
- baseSize = size;
13620
13713
  baseSpacing = spacing;
13621
13714
  dataIndex = index;
13715
+ opacity = Math.min(Math.max(alpha, 0), 1);
13622
13716
 
13623
13717
  // create canvas
13624
13718
  // (canvas size is set in fitToContainer)
@@ -13637,22 +13731,37 @@ dwv.gui.ViewLayer = function (containerDiv) {
13637
13731
  return;
13638
13732
  }
13639
13733
 
13640
- // check canvas
13641
- if (!dwv.gui.canCreateCanvas(baseSize.x, baseSize.y)) {
13642
- 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);
13643
13753
  }
13644
13754
 
13755
+ // set local
13756
+ baseSize = size;
13757
+
13645
13758
  // off screen canvas
13646
- offscreenCanvas = document.createElement('canvas');
13647
13759
  offscreenCanvas.width = baseSize.x;
13648
13760
  offscreenCanvas.height = baseSize.y;
13649
13761
  // original empty image data array
13650
13762
  context.clearRect(0, 0, baseSize.x, baseSize.y);
13651
13763
  imageData = context.createImageData(baseSize.x, baseSize.y);
13652
-
13653
- // update data on first draw
13654
- needsDataUpdate = true;
13655
- };
13764
+ }
13656
13765
 
13657
13766
  /**
13658
13767
  * Fit the layer to its parent container.
@@ -13661,6 +13770,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13661
13770
  * @param {object} fitSize The fit size as {x,y}.
13662
13771
  */
13663
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
+ };
13664
13778
  // update fit scale
13665
13779
  fitScale = {
13666
13780
  x: fitScale1D * baseSpacing.x,
@@ -13674,8 +13788,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13674
13788
  }
13675
13789
  canvas.width = width;
13676
13790
  canvas.height = height;
13677
- // reset scale
13678
- 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
+ };
13679
13796
  };
13680
13797
 
13681
13798
  /**
@@ -13760,8 +13877,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13760
13877
  */
13761
13878
  function onWLChange(event) {
13762
13879
  // generate and draw if no skip flag
13763
- if (typeof event.skipGenerate === 'undefined' ||
13764
- event.skipGenerate === false) {
13880
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13881
+ event.skipGenerate === true;
13882
+ if (!skip) {
13765
13883
  needsDataUpdate = true;
13766
13884
  self.draw();
13767
13885
  }
@@ -13774,8 +13892,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13774
13892
  * @private
13775
13893
  */
13776
13894
  function onColourChange(_event) {
13777
- needsDataUpdate = true;
13778
- self.draw();
13895
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13896
+ event.skipGenerate === true;
13897
+ if (!skip) {
13898
+ needsDataUpdate = true;
13899
+ self.draw();
13900
+ }
13779
13901
  }
13780
13902
 
13781
13903
  /**
@@ -13785,26 +13907,35 @@ dwv.gui.ViewLayer = function (containerDiv) {
13785
13907
  * @private
13786
13908
  */
13787
13909
  function onPositionChange(event) {
13788
- if (typeof event.skipGenerate === 'undefined' ||
13789
- 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;
13790
13914
  // clear for non valid events
13791
- if (typeof event.valid !== 'undefined' && !event.valid) {
13792
- self.clear();
13793
- return;
13794
- }
13795
- // 3D dimensions
13796
- var dims3D = [0, 1, 2];
13797
- // remove scroll index
13798
- var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13799
- dims3D.splice(indexScrollIndex, 1);
13800
- // remove non scroll index from diff dims
13801
- var diffDims = event.diffDims.filter(function (item) {
13802
- return dims3D.indexOf(item) === -1;
13803
- });
13804
- // update if we have something left
13805
- if (diffDims.length !== 0) {
13806
- needsDataUpdate = true;
13807
- 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
+ }
13808
13939
  }
13809
13940
  }
13810
13941
  }
@@ -13816,8 +13947,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13816
13947
  * @private
13817
13948
  */
13818
13949
  function onAlphaFuncChange(event) {
13819
- if (typeof event.skipGenerate === 'undefined' ||
13820
- event.skipGenerate === false) {
13950
+ var skip = typeof event.skipGenerate !== 'undefined' &&
13951
+ event.skipGenerate === true;
13952
+ if (!skip) {
13821
13953
  needsDataUpdate = true;
13822
13954
  self.draw();
13823
13955
  }
@@ -13838,19 +13970,14 @@ dwv.gui.ViewLayer = function (containerDiv) {
13838
13970
  * Clear the context.
13839
13971
  */
13840
13972
  this.clear = function () {
13841
- if (!isContextClear) {
13842
- // clear the context: reset the transform first
13843
- // store the current transformation matrix
13844
- context.save();
13845
- // use the identity matrix while clearing the canvas
13846
- context.setTransform(1, 0, 0, 1, 0, 0);
13847
- context.clearRect(0, 0, canvas.width, canvas.height);
13848
- // restore the transform
13849
- context.restore();
13850
-
13851
- // update clear flag
13852
- isContextClear = true;
13853
- }
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();
13854
13981
  };
13855
13982
 
13856
13983
  /**
@@ -15120,6 +15247,22 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15120
15247
  this.getOrigins = function () {
15121
15248
  return origins;
15122
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
+
15123
15266
  /**
15124
15267
  * Get the object size.
15125
15268
  * Warning: the size comes as stored in DICOM, meaning that it could
@@ -15138,7 +15281,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15138
15281
  size.get(2)
15139
15282
  ],
15140
15283
  viewOrientation);
15141
- res = new dwv.image.Size(values);
15284
+ res = new dwv.image.Size(values.concat(size.getValues().slice(3)));
15142
15285
  }
15143
15286
  return res;
15144
15287
  };
@@ -15153,26 +15296,26 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15153
15296
  if (origins.length === 1) {
15154
15297
  return 1;
15155
15298
  }
15156
- var spacing = null;
15299
+ var sliceSpacing = null;
15157
15300
  // (x, y, z) = orientationMatrix * (i, j, k)
15158
15301
  // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
15159
15302
  // applied on the patient position, reorders indices
15160
15303
  // so that Z is the slice direction
15161
- var orientation2 = orientation.getInverse().asOneAndZeros();
15304
+ var invOrientation = orientation.getInverse();
15162
15305
  var deltas = [];
15163
15306
  for (var i = 0; i < origins.length - 1; ++i) {
15164
- var origin1 = orientation2.multiplyVector3D(origins[i]);
15165
- var origin2 = orientation2.multiplyVector3D(origins[i + 1]);
15307
+ var origin1 = invOrientation.multiplyVector3D(origins[i]);
15308
+ var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
15166
15309
  var diff = Math.abs(origin1.getZ() - origin2.getZ());
15167
15310
  if (diff === 0) {
15168
15311
  throw new Error('Zero slice spacing.' +
15169
15312
  origin1.toString() + ' ' + origin2.toString());
15170
15313
  }
15171
- if (spacing === null) {
15172
- spacing = diff;
15314
+ if (sliceSpacing === null) {
15315
+ sliceSpacing = diff;
15173
15316
  } else {
15174
- if (!dwv.math.isSimilar(spacing, diff, dwv.math.BIG_EPSILON)) {
15175
- deltas.push(Math.abs(spacing - diff));
15317
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
15318
+ deltas.push(Math.abs(sliceSpacing - diff));
15176
15319
  }
15177
15320
  }
15178
15321
  }
@@ -15187,7 +15330,8 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15187
15330
  mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
15188
15331
  }
15189
15332
  }
15190
- return spacing;
15333
+
15334
+ return sliceSpacing;
15191
15335
  };
15192
15336
 
15193
15337
  /**
@@ -15226,6 +15370,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15226
15370
  * @returns {dwv.image.Spacing} The object spacing.
15227
15371
  */
15228
15372
  this.getRealSpacing = function () {
15373
+ // asOneAndZeros to not change spacing values...
15229
15374
  return this.getSpacing(orientation.getInverse().asOneAndZeros());
15230
15375
  };
15231
15376
 
@@ -15278,8 +15423,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15278
15423
  // -> >0 => vectors are codirectional
15279
15424
  // -> <0 => vectors are oposite
15280
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
+ }
15281
15431
  // oposite vectors get higher index
15282
- var sliceIndex = (dotProd < 0) ? closestSliceIndex + 1 : closestSliceIndex;
15432
+ var sliceIndex = test ? closestSliceIndex + 1 : closestSliceIndex;
15283
15433
  return sliceIndex;
15284
15434
  };
15285
15435
 
@@ -15357,10 +15507,11 @@ dwv.image.Geometry.prototype.isInBounds = function (point) {
15357
15507
  * Check that a index is within bounds.
15358
15508
  *
15359
15509
  * @param {dwv.math.Index} index The index to check.
15510
+ * @param {number} dir Optional direction to check.
15360
15511
  * @returns {boolean} True if the given coordinates are within bounds.
15361
15512
  */
15362
- dwv.image.Geometry.prototype.isIndexInBounds = function (index) {
15363
- return this.getSize().isInBounds(index);
15513
+ dwv.image.Geometry.prototype.isIndexInBounds = function (index, dir) {
15514
+ return this.getSize().isInBounds(index, dir);
15364
15515
  };
15365
15516
 
15366
15517
  /**
@@ -15457,9 +15608,11 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
15457
15608
  values[0] = Math.round(orientedPoint3D.getX() / spacing.get(0));
15458
15609
  values[1] = Math.round(orientedPoint3D.getY() / spacing.get(1));
15459
15610
  // flip K index (because of the slice order given by getSliceIndex)
15460
- values[2] = flipK(this.getSize(),
15461
- Math.round(orientedPoint3D.getZ() / spacing.get(2))
15462
- );
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
+
15463
15616
  // return index
15464
15617
  return new dwv.math.Index(values);
15465
15618
  };
@@ -15898,7 +16051,7 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15898
16051
  }
15899
16052
  // all meta should be equal
15900
16053
  for (var key in meta) {
15901
- if (key === 'windowPresets') {
16054
+ if (key === 'windowPresets' || key === 'numberOfFiles') {
15902
16055
  continue;
15903
16056
  }
15904
16057
  if (meta[key] !== rhs.getMeta()[key]) {
@@ -15929,8 +16082,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15929
16082
  values[3] = timeId;
15930
16083
  }
15931
16084
  var index = new dwv.math.Index(values);
15932
- var primaryOffset = size.indexToOffset(index);
15933
- var secondaryOffset = this.getSecondaryOffset(index);
16085
+ var primaryOffset = size.indexToOffset(index) * numberOfComponents;
16086
+ var secondaryOffset = this.getSecondaryOffset(index) * numberOfComponents;
15934
16087
 
15935
16088
  // first frame special slice by slice append
15936
16089
  if (timeId === 0) {
@@ -17908,7 +18061,7 @@ dwv.image.MaskFactory.prototype.create = function (
17908
18061
  }
17909
18062
  // create frame info object from per frame func
17910
18063
  var frameInfos = [];
17911
- var posPats = [];
18064
+ var framePosPats = [];
17912
18065
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
17913
18066
  var frameFunc = perFrameFuncGroupSequence[j];
17914
18067
  // Derivation Image Sequence
@@ -17935,8 +18088,8 @@ dwv.image.MaskFactory.prototype.create = function (
17935
18088
  for (var p = 0; p < imagePosPat.length; ++p) {
17936
18089
  imagePosPat[p] = parseFloat(imagePosPat[p], 10);
17937
18090
  }
17938
- if (!includesPosPat(posPats, imagePosPat)) {
17939
- posPats.push(imagePosPat);
18091
+ if (!includesPosPat(framePosPats, imagePosPat)) {
18092
+ framePosPats.push(imagePosPat);
17940
18093
  }
17941
18094
  frameInfos.push({
17942
18095
  dimIndex: dimIndex,
@@ -17980,7 +18133,22 @@ dwv.image.MaskFactory.prototype.create = function (
17980
18133
  }
17981
18134
  }
17982
18135
  // sort positions patient
17983
- 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]);
17984
18152
 
17985
18153
  // create output buffer
17986
18154
  // as many slices as posPats -> gap slices between groups are not represented
@@ -18070,12 +18238,8 @@ dwv.image = dwv.image || {};
18070
18238
  */
18071
18239
  dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18072
18240
 
18073
- // [Img] -- Oi --> [Real] <-- Ot -- [Target]
18074
- // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
18075
- // Ov = (Oi)-1 * Ot
18076
- // Ot = Oi * Ov
18077
- var targetOrientation =
18078
- imageOrientation.asOneAndZeros().multiply(viewOrientation);
18241
+ var targetOrientation = dwv.gui.getTargetOrientation(
18242
+ imageOrientation, viewOrientation);
18079
18243
 
18080
18244
  /**
18081
18245
  * Get a 3D offset from a plane one.
@@ -18088,9 +18252,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18088
18252
  var planeOffset = new dwv.math.Vector3D(
18089
18253
  offset2D.x, offset2D.y, 0);
18090
18254
  // de-orient
18091
- var pixelOffset = this.getDeOrientedVector3D(planeOffset);
18092
- // offset indexToWorld
18093
- 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));
18094
18261
  };
18095
18262
 
18096
18263
  /**
@@ -18100,10 +18267,13 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18100
18267
  * @returns {object} The plane offset as {x,y}.
18101
18268
  */
18102
18269
  this.getPlaneOffsetFromOffset3D = function (offset3D) {
18103
- // offset worldToIndex
18104
- 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));
18105
18275
  // orient
18106
- var planeOffset = this.getOrientedVector3D(pixelOffset);
18276
+ var planeOffset = this.getTargetOrientedVector3D(pixelOffset);
18107
18277
  // make 2D
18108
18278
  return {
18109
18279
  x: planeOffset.getX(),
@@ -18112,38 +18282,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18112
18282
  };
18113
18283
 
18114
18284
  /**
18115
- * Apply spacing to an offset.
18116
- *
18117
- * @param {dwv.math.Point3D} off The 3D offset.
18118
- * @returns {dwv.math.Vector3D} The world offset.
18119
- */
18120
- function offsetIndexToWorld(off) {
18121
- return new dwv.math.Vector3D(
18122
- off.getX() * spacing.get(0),
18123
- off.getY() * spacing.get(1),
18124
- off.getZ() * spacing.get(2));
18125
- }
18126
-
18127
- /**
18128
- * Remove spacing from an offset.
18129
- *
18130
- * @param {object} off The world offset object as {x,y,z}.
18131
- * @returns {dwv.math.Vector3D} The 3D offset.
18132
- */
18133
- function offsetWorldToIndex(off) {
18134
- return new dwv.math.Vector3D(
18135
- off.x / spacing.get(0),
18136
- off.y / spacing.get(1),
18137
- off.z / spacing.get(2));
18138
- }
18139
-
18140
- /**
18141
- * Orient an input vector.
18285
+ * Orient an input vector from real to target space.
18142
18286
  *
18143
18287
  * @param {dwv.math.Vector3D} vector The input vector.
18144
18288
  * @returns {dwv.math.Vector3D} The oriented vector.
18145
18289
  */
18146
- this.getOrientedVector3D = function (vector) {
18290
+ this.getTargetOrientedVector3D = function (vector) {
18147
18291
  var planeVector = vector;
18148
18292
  if (typeof targetOrientation !== 'undefined') {
18149
18293
  planeVector = targetOrientation.getInverse().multiplyVector3D(vector);
@@ -18152,46 +18296,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18152
18296
  };
18153
18297
 
18154
18298
  /**
18155
- * Orient an input index.
18156
- *
18157
- * @param {dwv.math.Index} index The input index.
18158
- * @returns {dwv.math.Index} The oriented index.
18159
- */
18160
- this.getOrientedIndex = function (index) {
18161
- var planeIndex = index;
18162
- if (typeof viewOrientation !== 'undefined') {
18163
- // abs? otherwise negative index...
18164
- // vector = viewOrientation * planeVector
18165
- planeIndex = viewOrientation.getInverse().getAbs().multiplyIndex3D(index);
18166
- }
18167
- return planeIndex;
18168
- };
18169
-
18170
- /**
18171
- * Orient an input point.
18172
- *
18173
- * @param {dwv.math.Point3D} point The input point.
18174
- * @returns {dwv.math.Point3D} The oriented point.
18175
- */
18176
- this.getOrientedPoint = function (point) {
18177
- var planePoint = point;
18178
- if (typeof viewOrientation !== 'undefined') {
18179
- // abs? otherwise negative index...
18180
- // vector = viewOrientation * planeVector
18181
- var point3D =
18182
- viewOrientation.getInverse().getAbs().multiplyPoint3D(point.get3D());
18183
- planePoint = point.mergeWith3D(point3D);
18184
- }
18185
- return planePoint;
18186
- };
18187
-
18188
- /**
18189
- * De-orient an input vector.
18299
+ * De-orient an input vector from target to real space.
18190
18300
  *
18191
18301
  * @param {dwv.math.Vector3D} planeVector The input vector.
18192
18302
  * @returns {dwv.math.Vector3D} The de-orienteded vector.
18193
18303
  */
18194
- this.getDeOrientedVector3D = function (planeVector) {
18304
+ this.getTargetDeOrientedVector3D = function (planeVector) {
18195
18305
  var vector = planeVector;
18196
18306
  if (typeof targetOrientation !== 'undefined') {
18197
18307
  vector = targetOrientation.multiplyVector3D(planeVector);
@@ -18200,10 +18310,10 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18200
18310
  };
18201
18311
 
18202
18312
  /**
18203
- * Get the image oriented values for an input vector.
18313
+ * Orient an input vector from target to image space.
18204
18314
  *
18205
18315
  * @param {dwv.math.Vector3D} planeVector The input vector.
18206
- * @returns {dwv.math.Vector3D} The de-orienteded vector.
18316
+ * @returns {dwv.math.Vector3D} The orienteded vector.
18207
18317
  */
18208
18318
  this.getImageOrientedVector3D = function (planeVector) {
18209
18319
  var vector = planeVector;
@@ -18236,28 +18346,6 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18236
18346
  };
18237
18347
  };
18238
18348
 
18239
- /**
18240
- * Reorder values to compensate for view orientation.
18241
- *
18242
- * @param {object} values Values as {x,y,z}.
18243
- * @returns {object} 'Deoriented' values as {x,y,z}.
18244
- */
18245
- this.getDeOrientedXYZ = function (values) {
18246
- var deOrientedValues = dwv.math.getDeOrientedArray3D(
18247
- [
18248
- values.x,
18249
- values.y,
18250
- values.z
18251
- ],
18252
- viewOrientation
18253
- );
18254
- return {
18255
- x: deOrientedValues[0],
18256
- y: deOrientedValues[1],
18257
- z: deOrientedValues[2]
18258
- };
18259
- };
18260
-
18261
18349
  /**
18262
18350
  * Get the (view) scroll dimension index.
18263
18351
  *
@@ -18633,9 +18721,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
18633
18721
  * Check that an index is within bounds.
18634
18722
  *
18635
18723
  * @param {dwv.math.Index} index The index to check.
18724
+ * @param {number} dir Optional direction to check.
18636
18725
  * @returns {boolean} True if the given coordinates are within bounds.
18637
18726
  */
18638
- dwv.image.Size.prototype.isInBounds = function (index) {
18727
+ dwv.image.Size.prototype.isInBounds = function (index, dir) {
18639
18728
  // check input
18640
18729
  if (!index) {
18641
18730
  return false;
@@ -18645,11 +18734,20 @@ dwv.image.Size.prototype.isInBounds = function (index) {
18645
18734
  if (length !== index.length()) {
18646
18735
  return false;
18647
18736
  }
18648
- // check values
18649
- for (var i = 0; i < length; ++i) {
18650
- 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))) {
18651
18743
  return false;
18652
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
+ }
18653
18751
  }
18654
18752
  // seems ok!
18655
18753
  return true;
@@ -18900,11 +18998,12 @@ dwv.image.View = function (image) {
18900
18998
  var colourMap = dwv.image.lut.plain;
18901
18999
  /**
18902
19000
  * Current position as a Point3D.
19001
+ * Store position and not index to stay geometry independent.
18903
19002
  *
18904
19003
  * @private
18905
19004
  * @type {dwv.math.Index}
18906
19005
  */
18907
- var currentIndex = null;
19006
+ var currentPosition = null;
18908
19007
  /**
18909
19008
  * View orientation. Undefined will use the original slice ordering.
18910
19009
  *
@@ -19042,6 +19141,7 @@ dwv.image.View = function (image) {
19042
19141
  if (!this.getCurrentIndex()) {
19043
19142
  this.setInitialIndex();
19044
19143
  }
19144
+ var currentIndex = this.getCurrentIndex();
19045
19145
  // use current rsi if not provided
19046
19146
  if (typeof rsi === 'undefined') {
19047
19147
  rsi = image.getRescaleSlopeAndIntercept(currentIndex);
@@ -19224,8 +19324,7 @@ dwv.image.View = function (image) {
19224
19324
  * @returns {dwv.math.Point} The current position.
19225
19325
  */
19226
19326
  this.getCurrentPosition = function () {
19227
- var geometry = this.getImage().getGeometry();
19228
- return geometry.indexToWorld(currentIndex);
19327
+ return currentPosition;
19229
19328
  };
19230
19329
 
19231
19330
  /**
@@ -19234,15 +19333,32 @@ dwv.image.View = function (image) {
19234
19333
  * @returns {dwv.math.Index} The current index.
19235
19334
  */
19236
19335
  this.getCurrentIndex = function () {
19237
- 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);
19238
19342
  };
19239
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
+ */
19240
19350
  this.canSetPosition = function (position) {
19241
19351
  var geometry = image.getGeometry();
19242
19352
  var index = geometry.worldToIndex(position);
19243
- return geometry.isIndexInBounds(index);
19353
+ return geometry.isIndexInBounds(index, this.getScrollIndex());
19244
19354
  };
19245
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
+ */
19246
19362
  this.getOrigin = function (position) {
19247
19363
  var geometry = image.getGeometry();
19248
19364
  var originIndex = 0;
@@ -19266,7 +19382,7 @@ dwv.image.View = function (image) {
19266
19382
  // send invalid event if not in bounds
19267
19383
  var geometry = image.getGeometry();
19268
19384
  var index = geometry.worldToIndex(position);
19269
- if (!geometry.isIndexInBounds(index)) {
19385
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19270
19386
  if (!silent) {
19271
19387
  // fire event with valid: false
19272
19388
  fireEvent({
@@ -19288,7 +19404,7 @@ dwv.image.View = function (image) {
19288
19404
  *
19289
19405
  * @param {dwv.math.Index} index The new index.
19290
19406
  * @param {boolean} silent Flag to fire event or not.
19291
- * @returns {boolean} False if not in bounds
19407
+ * @returns {boolean} False if not in bounds.
19292
19408
  * @fires dwv.image.View#positionchange
19293
19409
  */
19294
19410
  this.setCurrentIndex = function (index, silent) {
@@ -19301,73 +19417,72 @@ dwv.image.View = function (image) {
19301
19417
  var position = geometry.indexToWorld(index);
19302
19418
 
19303
19419
  // check if possible
19304
- if (!geometry.isIndexInBounds(index)) {
19420
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19305
19421
  // do no send invalid positionchange event: avoid empty repaint
19306
19422
  return false;
19307
19423
  }
19308
19424
 
19309
- var isNew = !currentIndex || !currentIndex.equals(index);
19310
-
19311
- if (isNew) {
19312
- var diffDims = null;
19313
- if (currentIndex) {
19314
- if (currentIndex.canCompare(index)) {
19315
- diffDims = currentIndex.compare(index);
19316
- } else {
19317
- diffDims = [];
19318
- var minLen = Math.min(currentIndex.length(), index.length());
19319
- for (var i = 0; i < minLen; ++i) {
19320
- if (currentIndex.get(i) !== index.get(i)) {
19321
- diffDims.push(i);
19322
- }
19323
- }
19324
- var maxLen = Math.max(currentIndex.length(), index.length());
19325
- for (var j = minLen; j < maxLen; ++j) {
19326
- diffDims.push(j);
19327
- }
19328
- }
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);
19329
19434
  } else {
19330
19435
  diffDims = [];
19331
- for (var k = 0; k < index.length(); ++k) {
19332
- 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);
19333
19445
  }
19334
19446
  }
19447
+ } else {
19448
+ diffDims = [];
19449
+ for (var k = 0; k < index.length(); ++k) {
19450
+ diffDims.push(k);
19451
+ }
19452
+ }
19335
19453
 
19336
- // assign
19337
- currentIndex = index;
19454
+ // assign
19455
+ currentPosition = position;
19338
19456
 
19339
- if (!silent) {
19340
- /**
19341
- * Position change event.
19342
- *
19343
- * @event dwv.image.View#positionchange
19344
- * @type {object}
19345
- * @property {Array} value The changed value as [index, pixelValue].
19346
- * @property {Array} diffDims An array of modified indices.
19347
- */
19348
- var posEvent = {
19349
- type: 'positionchange',
19350
- value: [
19351
- index.getValues(),
19352
- position.getValues(),
19353
- ],
19354
- diffDims: diffDims,
19355
- data: {
19356
- imageUid: image.getImageUid(index)
19357
- }
19358
- };
19359
-
19360
- // add value if possible
19361
- if (image.canQuantify()) {
19362
- var pixValue = image.getRescaledValueAtIndex(index);
19363
- 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)
19364
19475
  }
19476
+ };
19365
19477
 
19366
- // fire
19367
- fireEvent(posEvent);
19478
+ // add value if possible
19479
+ if (image.canQuantify()) {
19480
+ var pixValue = image.getRescaledValueAtIndex(index);
19481
+ posEvent.value.push(pixValue);
19368
19482
  }
19369
- } else {
19370
- return false;
19483
+
19484
+ // fire
19485
+ fireEvent(posEvent);
19371
19486
  }
19372
19487
 
19373
19488
  // all good
@@ -19533,7 +19648,7 @@ dwv.image.View.prototype.getWindowLevelMinMax = function () {
19533
19648
  var width = max - min;
19534
19649
  // full black / white images, defaults to 1.
19535
19650
  if (width < 1) {
19536
- dwv.logger.warn('Zero or negative width, defaulting to one.');
19651
+ dwv.logger.warn('Zero or negative window width, defaulting to one.');
19537
19652
  width = 1;
19538
19653
  }
19539
19654
  var center = min + width / 2;
@@ -23870,6 +23985,60 @@ dwv.math.Ellipse.prototype.quantify = function (viewController, flags) {
23870
23985
  return quant;
23871
23986
  };
23872
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
+
23873
24042
  // namespaces
23874
24043
  var dwv = dwv || {};
23875
24044
  dwv.math = dwv.math || {};
@@ -24043,6 +24212,21 @@ dwv.math.getZeroIndex = function (size) {
24043
24212
  return new dwv.math.Index(values);
24044
24213
  };
24045
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
+
24046
24230
  /**
24047
24231
  * Get a string id from the index values in the form of: '#0-1_#1-2'.
24048
24232
  *
@@ -24372,6 +24556,9 @@ dwv.math.isSimilar = function (a, b, tol) {
24372
24556
  * @class
24373
24557
  */
24374
24558
  dwv.math.Matrix33 = function (values) {
24559
+ // matrix inverse, calculated at first ask
24560
+ var inverse = null;
24561
+
24375
24562
  /**
24376
24563
  * Get a value of the matrix.
24377
24564
  *
@@ -24382,6 +24569,20 @@ dwv.math.Matrix33 = function (values) {
24382
24569
  this.get = function (row, col) {
24383
24570
  return values[row * 3 + col];
24384
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
+
24385
24586
  }; // Matrix33
24386
24587
 
24387
24588
  /**
@@ -24393,15 +24594,16 @@ dwv.math.Matrix33 = function (values) {
24393
24594
  * @returns {boolean} True if both matrices are equal.
24394
24595
  */
24395
24596
  dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24396
- return dwv.math.isSimilar(this.get(0, 0), rhs.get(0, 0), p) &&
24397
- dwv.math.isSimilar(this.get(0, 1), rhs.get(0, 1), p) &&
24398
- dwv.math.isSimilar(this.get(0, 2), rhs.get(0, 2), p) &&
24399
- dwv.math.isSimilar(this.get(1, 0), rhs.get(1, 0), p) &&
24400
- dwv.math.isSimilar(this.get(1, 1), rhs.get(1, 1), p) &&
24401
- dwv.math.isSimilar(this.get(1, 2), rhs.get(1, 2), p) &&
24402
- dwv.math.isSimilar(this.get(2, 0), rhs.get(2, 0), p) &&
24403
- dwv.math.isSimilar(this.get(2, 1), rhs.get(2, 1), p) &&
24404
- 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;
24405
24607
  };
24406
24608
 
24407
24609
  /**
@@ -24410,10 +24612,20 @@ dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24410
24612
  * @returns {string} The matrix as a string.
24411
24613
  */
24412
24614
  dwv.math.Matrix33.prototype.toString = function () {
24413
- return '[' + this.get(0, 0) + ', ' + this.get(0, 1) + ', ' + this.get(0, 2) +
24414
- '\n ' + this.get(1, 0) + ', ' + this.get(1, 1) + ', ' + this.get(1, 2) +
24415
- '\n ' + this.get(2, 0) + ', ' + this.get(2, 1) + ', ' + this.get(2, 2) +
24416
- ']';
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;
24417
24629
  };
24418
24630
 
24419
24631
  /**
@@ -24511,42 +24723,46 @@ dwv.math.Matrix33.prototype.multiplyIndex3D = function (index3D) {
24511
24723
  };
24512
24724
 
24513
24725
  /**
24514
- * Get the inverse of this matrix.
24726
+ * Get the inverse of an input 3*3 matrix.
24515
24727
  *
24516
- * @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.
24517
24731
  * @see https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
24518
- */
24519
- dwv.math.Matrix33.prototype.getInverse = function () {
24520
- var a = this.get(0, 0);
24521
- var b = this.get(0, 1);
24522
- var c = this.get(0, 2);
24523
- var d = this.get(1, 0);
24524
- var e = this.get(1, 1);
24525
- var f = this.get(1, 2);
24526
- var g = this.get(2, 0);
24527
- var h = this.get(2, 1);
24528
- var i = this.get(2, 2);
24529
-
24530
- var a2 = e * i - f * h;
24531
- var b2 = f * g - d * i;
24532
- var c2 = d * h - e * g;
24533
-
24534
- 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;
24535
24750
  if (det === 0) {
24536
- dwv.logger.warn('Cannot invert matrix with zero determinant.');
24751
+ dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
24537
24752
  return;
24538
24753
  }
24754
+ det = 1 / det;
24539
24755
 
24540
24756
  var values = [
24541
- a2 / det,
24542
- (c * h - b * i) / det,
24543
- (b * f - c * e) / det,
24544
- b2 / det,
24545
- (a * i - c * g) / det,
24546
- (c * d - a * f) / det,
24547
- c2 / det,
24548
- (b * g - a * h) / det,
24549
- (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)
24550
24766
  ];
24551
24767
 
24552
24768
  return new dwv.math.Matrix33(values);
@@ -24957,6 +25173,21 @@ dwv.math.Point3D.prototype.equals = function (rhs) {
24957
25173
  this.getZ() === rhs.getZ();
24958
25174
  };
24959
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
+
24960
25191
  /**
24961
25192
  * Get a string representation of the Point3D.
24962
25193
  *
@@ -24994,6 +25225,19 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
24994
25225
  (this.getZ() - point3D.getZ()));
24995
25226
  };
24996
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
+
24997
25241
  /**
24998
25242
  * Immutable point.
24999
25243
  * Warning: the input array is NOT cloned, modifying it will
@@ -27296,11 +27540,15 @@ dwv.tool.Draw = function (app) {
27296
27540
  };
27297
27541
 
27298
27542
  /**
27299
- * Handle double click event.
27543
+ * Handle double click event: some tools use it to finish interaction.
27300
27544
  *
27301
- * @param {object} event The mouse up event.
27545
+ * @param {object} event The double click event.
27302
27546
  */
27303
27547
  this.dblclick = function (event) {
27548
+ // only end by double click undefined NPoints
27549
+ if (typeof currentFactory.getNPoints() !== 'undefined') {
27550
+ return;
27551
+ }
27304
27552
  // exit if not started draw
27305
27553
  if (!started) {
27306
27554
  return;
@@ -30242,10 +30490,11 @@ dwv.tool.draw.FreeHandFactory = function () {
30242
30490
  /**
30243
30491
  * Get the number of points needed to build the shape.
30244
30492
  *
30245
- * @returns {number} The number of points.
30493
+ * @returns {number|undefined} The number of points.
30246
30494
  */
30247
30495
  this.getNPoints = function () {
30248
- return 1000;
30496
+ // undefined to end with double click
30497
+ return undefined;
30249
30498
  };
30250
30499
  /**
30251
30500
  * Get the timeout between point storage.
@@ -31673,10 +31922,11 @@ dwv.tool.draw.RoiFactory = function () {
31673
31922
  /**
31674
31923
  * Get the number of points needed to build the shape.
31675
31924
  *
31676
- * @returns {number} The number of points.
31925
+ * @returns {number|undefined} The number of points.
31677
31926
  */
31678
31927
  this.getNPoints = function () {
31679
- return 50;
31928
+ // undefined to end with double click
31929
+ return undefined;
31680
31930
  };
31681
31931
  /**
31682
31932
  * Get the timeout between point storage.
@@ -32593,11 +32843,16 @@ dwv.tool.WindowLevel = function (app) {
32593
32843
  // difference to last position
32594
32844
  var diffX = event._x - self.x0;
32595
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
+
32596
32851
  // calculate new window level
32597
- var windowCenter =
32598
- parseInt(viewController.getWindowLevel().center, 10) + diffY;
32599
- var windowWidth =
32600
- 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);
32601
32856
  // bound window width
32602
32857
  windowWidth = dwv.image.validateWindowWidth(windowWidth);
32603
32858