dwv 0.31.0-beta.4 → 0.31.0-beta.7

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.7 2022-06-29 15:19:10 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -118,7 +118,7 @@ dwv.App = function () {
118
118
  * Get the image.
119
119
  *
120
120
  * @param {number} index The data index.
121
- * @returns {Image} The associated image.
121
+ * @returns {dwv.image.Image} The associated image.
122
122
  */
123
123
  this.getImage = function (index) {
124
124
  return dataController.get(index).image;
@@ -126,16 +126,16 @@ dwv.App = function () {
126
126
  /**
127
127
  * Get the last loaded image.
128
128
  *
129
- * @returns {Image} The image.
129
+ * @returns {dwv.image.Image} The image.
130
130
  */
131
131
  this.getLastImage = function () {
132
132
  return dataController.get(dataController.length() - 1).image;
133
133
  };
134
134
  /**
135
- * Set the image.
135
+ * Set the image at the given index.
136
136
  *
137
137
  * @param {number} index The data index.
138
- * @param {Image} img The associated image.
138
+ * @param {dwv.image.Image} img The associated image.
139
139
  */
140
140
  this.setImage = function (index, img) {
141
141
  dataController.setImage(index, img);
@@ -143,12 +143,65 @@ dwv.App = function () {
143
143
  /**
144
144
  * Set the last image.
145
145
  *
146
- * @param {Image} img The associated image.
146
+ * @param {dwv.image.Image} img The associated image.
147
147
  */
148
148
  this.setLastImage = function (img) {
149
149
  dataController.setImage(dataController.length() - 1, img);
150
150
  };
151
151
 
152
+ /**
153
+ * Add a new image.
154
+ *
155
+ * @param {dwv.image.Image} image The new image.
156
+ * @param {object} meta The image meta.
157
+ * @returns {number} The new image id.
158
+ */
159
+ this.addNewImage = function (image, meta) {
160
+ var id = dataController.length();
161
+
162
+ // load start event
163
+ fireEvent({
164
+ type: 'loadstart',
165
+ loadtype: 'image',
166
+ source: 'internal',
167
+ loadid: id
168
+ });
169
+
170
+ // add image to data controller
171
+ dataController.addNew(id, image, meta);
172
+
173
+ // load item event
174
+ fireEvent({
175
+ type: 'loaditem',
176
+ loadtype: 'image',
177
+ data: meta,
178
+ source: 'internal',
179
+ loadid: id,
180
+ isfirstitem: true
181
+ });
182
+
183
+ // optional render
184
+ if (options.viewOnFirstLoadItem) {
185
+ this.render(id);
186
+ }
187
+
188
+ // load events
189
+ fireEvent({
190
+ type: 'load',
191
+ loadtype: 'image',
192
+ source: 'internal',
193
+ loadid: id
194
+ });
195
+ fireEvent({
196
+ type: 'loadend',
197
+ loadtype: 'image',
198
+ source: 'internal',
199
+ loadid: id
200
+ });
201
+
202
+ return id;
203
+ };
204
+
152
205
  /**
153
206
  * Get the meta data.
154
207
  *
@@ -395,6 +448,9 @@ dwv.App = function () {
395
448
  if (typeof options.binders !== 'undefined') {
396
449
  stage.setBinders(options.binders);
397
450
  }
451
+
452
+ // create layer groups
453
+ createLayerGroups(options.dataViewConfigs);
398
454
  };
399
455
 
400
456
  /**
@@ -532,8 +588,7 @@ dwv.App = function () {
532
588
  this.fitToContainer = function () {
533
589
  for (var i = 0; i < stage.getNumberOfLayerGroups(); ++i) {
534
590
  var layerGroup = stage.getLayerGroup(i);
535
- var mazSize = getLayerGroupMaxSize(layerGroup);
536
- layerGroup.fitToContainer(mazSize);
591
+ layerGroup.fitToContainer();
537
592
  layerGroup.draw();
538
593
  }
539
594
  };
@@ -569,6 +624,16 @@ dwv.App = function () {
569
624
  return configs;
570
625
  }
571
626
 
627
+ /**
628
+ * Get the data view config.
629
+ * Carefull, returns a reference, do not modify without resetting.
630
+ *
631
+ * @returns {object} The configuration list.
632
+ */
633
+ this.getDataViewConfig = function () {
634
+ return options.dataViewConfigs;
635
+ };
636
+
572
637
  /**
573
638
  * Set the data view configuration (see the init options for details).
574
639
  *
@@ -579,9 +644,40 @@ dwv.App = function () {
579
644
  stage.empty();
580
645
  // set new
581
646
  options.dataViewConfigs = configs;
582
- // re-bind layers
583
- stage.bindLayerGroups();
584
- };
647
+ // create layer groups
648
+ createLayerGroups(configs);
649
+ };
650
+
651
+ /**
652
+ * Create layer groups according to a data view config:
653
+ * adds them to stage and bind them.
654
+ *
655
+ * @param {object} dataViewConfigs The data view config.
656
+ */
657
+ function createLayerGroups(dataViewConfigs) {
658
+ var dataKeys = Object.keys(dataViewConfigs);
659
+ var divIds = [];
660
+ for (var i = 0; i < dataKeys.length; ++i) {
661
+ var dataConfigs = dataViewConfigs[dataKeys[i]];
662
+ for (var j = 0; j < dataConfigs.length; ++j) {
663
+ var viewConfig = dataConfigs[j];
664
+ // view configs can contain the same divIds, avoid duplicating
665
+ if (!divIds.includes(viewConfig.divId)) {
666
+ // create new layer group
667
+ var element = document.getElementById(viewConfig.divId);
668
+ var layerGroup = stage.addLayerGroup(element);
669
+ // bind events
670
+ bindLayerGroupToApp(layerGroup);
671
+ // optional orientation
672
+ if (typeof viewConfig.orientation !== 'undefined') {
673
+ layerGroup.setTargetOrientation(
674
+ dwv.math.getMatrixFromName(viewConfig.orientation));
675
+ }
676
+ divIds.push(viewConfig.divId);
677
+ }
678
+ }
679
+ }
680
+ }
585
681
 
586
682
  /**
587
683
  * Set the layer groups binders.
@@ -611,23 +707,14 @@ dwv.App = function () {
611
707
  }
612
708
  for (var i = 0; i < viewConfigs.length; ++i) {
613
709
  var config = viewConfigs[i];
614
- // create layer group if not done yet
615
- // warn: needs a loaded DOM
616
710
  var layerGroup =
617
711
  stage.getLayerGroupWithElementId(config.divId);
712
+ // layer group must exist
618
713
  if (!layerGroup) {
619
- // create new layer group
620
- var element = document.getElementById(config.divId);
621
- layerGroup = stage.addLayerGroup(element);
622
- // bind events
623
- bindLayerGroup(layerGroup);
624
- // optional orientation
625
- if (typeof config.orientation !== 'undefined') {
626
- layerGroup.setTargetOrientation(
627
- dwv.math.getMatrixFromName(config.orientation));
628
- }
714
+ throw new Error('No layer group for ' + config.divId);
629
715
  }
630
716
  // initialise or add view
717
+ // warn: needs a loaded DOM
631
718
  if (layerGroup.getViewLayersByDataIndex(dataIndex).length === 0) {
632
719
  if (layerGroup.getNumberOfLayers() === 0) {
633
720
  initialiseBaseLayers(dataIndex, config);
@@ -651,7 +738,8 @@ dwv.App = function () {
651
738
  var layerGroup = stage.getActiveLayerGroup();
652
739
  var viewController = layerGroup.getActiveViewLayer().getViewController();
653
740
  var k = viewController.getCurrentScrollPosition();
654
- layerGroup.addScale(step, {x: cx, y: cy, z: k});
741
+ var center = new dwv.math.Point3D(cx, cy, k);
742
+ layerGroup.addScale(step, center);
655
743
  layerGroup.draw();
656
744
  };
657
745
 
@@ -1094,7 +1182,9 @@ dwv.App = function () {
1094
1182
  type: 'loaditem',
1095
1183
  data: eventMetaData,
1096
1184
  source: event.source,
1097
- loadtype: event.loadtype
1185
+ loadtype: event.loadtype,
1186
+ loadid: event.loadid,
1187
+ isfirstitem: event.isfirstitem
1098
1188
  });
1099
1189
 
1100
1190
  // render if first and flag allows
@@ -1196,7 +1286,7 @@ dwv.App = function () {
1196
1286
  * @param {object} group The layer group.
1197
1287
  * @private
1198
1288
  */
1199
- function bindLayerGroup(group) {
1289
+ function bindLayerGroupToApp(group) {
1200
1290
  // propagate layer group events
1201
1291
  group.addEventListener('zoomchange', fireEvent);
1202
1292
  group.addEventListener('offsetchange', fireEvent);
@@ -1227,37 +1317,6 @@ dwv.App = function () {
1227
1317
  }
1228
1318
  }
1229
1319
 
1230
- /**
1231
- * Get the data max size for a layer group.
1232
- *
1233
- * @param {object} layerGroup The layer group.
1234
- * @returns {object} The max size as {x,y}.
1235
- */
1236
- function getLayerGroupMaxSize(layerGroup) {
1237
- var maxSize = {x: 0, y: 0};
1238
- // loop through layer group data
1239
- var indices = layerGroup.getViewDataIndices();
1240
- for (var i = 0; i < indices.length; ++i) {
1241
- var data = dataController.get(indices[i]);
1242
- var geometry = data.image.getGeometry();
1243
- var viewOrient = dwv.gui.getViewOrientation(
1244
- geometry,
1245
- layerGroup.getTargetOrientation()
1246
- );
1247
- var size = geometry.getSize(viewOrient).get2D();
1248
- var spacing = geometry.getSpacing(viewOrient).get2D();
1249
- var width = size.x * spacing.x;
1250
- if (width > maxSize.x) {
1251
- maxSize.x = width;
1252
- }
1253
- var height = size.y * spacing.y;
1254
- if (height > maxSize.y) {
1255
- maxSize.y = height;
1256
- }
1257
- }
1258
- return maxSize;
1259
- }
1260
-
1261
1320
  /**
1262
1321
  * Add a view layer.
1263
1322
  *
@@ -1285,7 +1344,7 @@ dwv.App = function () {
1285
1344
  new dwv.dicom.DicomElementsWrapper(data.meta),
1286
1345
  data.image);
1287
1346
  var viewOrientation = dwv.gui.getViewOrientation(
1288
- imageGeometry,
1347
+ imageGeometry.getOrientation(),
1289
1348
  layerGroup.getTargetOrientation()
1290
1349
  );
1291
1350
  view.setOrientation(viewOrientation);
@@ -1326,8 +1385,9 @@ dwv.App = function () {
1326
1385
  viewLayer.setView(view);
1327
1386
  var size2D = imageGeometry.getSize(viewOrientation).get2D();
1328
1387
  var spacing2D = imageGeometry.getSpacing(viewOrientation).get2D();
1329
- viewLayer.initialise(size2D, spacing2D, dataIndex);
1330
- viewLayer.setOpacity(opacity);
1388
+ viewLayer.initialise(size2D, spacing2D, dataIndex, opacity);
1389
+ viewLayer.setScale(layerGroup.getScale());
1390
+ viewLayer.setOffset(layerGroup.getOffset());
1331
1391
 
1332
1392
  // listen to image changes
1333
1393
  dataController.addEventListener('imagechange', viewLayer.onimagechange);
@@ -1341,18 +1401,20 @@ dwv.App = function () {
1341
1401
  dl.initialise(size2D, spacing2D, dataIndex);
1342
1402
  dl.setPlaneHelper(viewLayer.getViewController().getPlaneHelper());
1343
1403
 
1404
+ // force positionchange to sync layers
1344
1405
  var vc = viewLayer.getViewController();
1345
- // positionchange event like data
1346
1406
  var value = [
1347
1407
  vc.getCurrentIndex().getValues(),
1348
1408
  vc.getCurrentPosition().getValues()
1349
1409
  ];
1350
- layerGroup.updateLayersToPositionChange({value: value});
1410
+ layerGroup.updateLayersToPositionChange({
1411
+ value: value,
1412
+ srclayerid: viewLayer.getId()
1413
+ });
1351
1414
  }
1352
1415
 
1353
- // fit to the maximum size
1354
- var maxSize = getLayerGroupMaxSize(layerGroup);
1355
- layerGroup.fitToContainer(maxSize);
1416
+ // sync layer groups
1417
+ stage.syncLayerGroupScale();
1356
1418
  }
1357
1419
 
1358
1420
  };
@@ -2886,9 +2948,61 @@ dwv.ctrl.ViewController = function (view) {
2886
2948
  * @returns {dwv.image.Size} The size.
2887
2949
  */
2888
2950
  this.getImageSize = function () {
2889
- return view.getImage().getGeometry().getSize();
2951
+ return view.getImage().getGeometry().getSize(view.getOrientation());
2952
+ };
2953
+
2954
+ /**
2955
+ * Get the image world (mm) 2D size.
2956
+ *
2957
+ * @returns {object} The 2D size as {x,y}.
2958
+ */
2959
+ this.getImageWorldSize = function () {
2960
+ var geometry = view.getImage().getGeometry();
2961
+ var size = geometry.getSize(view.getOrientation()).get2D();
2962
+ var spacing = geometry.getSpacing(view.getOrientation()).get2D();
2963
+ return {
2964
+ x: size.x * spacing.x,
2965
+ y: size.y * spacing.y
2966
+ };
2967
+ };
2968
+
2969
+ /**
2970
+ * Get the image rescaled data range.
2971
+ *
2972
+ * @returns {object} The range as {min, max}.
2973
+ */
2974
+ this.getImageRescaledDataRange = function () {
2975
+ return view.getImage().getRescaledDataRange();
2890
2976
  };
2891
2977
 
2978
+ /**
2979
+ * Compare the input meta data to the associated image one.
2980
+ *
2981
+ * @param {object} meta The meta data.
2982
+ * @returns {boolean} True if the associated image has equal meta data.
2983
+ */
2984
+ this.equalImageMeta = function (meta) {
2985
+ var imageMeta = view.getImage().getMeta();
2986
+ // loop through input meta keys
2987
+ var metaKeys = Object.keys(meta);
2988
+ for (var i = 0; i < metaKeys.length; ++i) {
2989
+ var metaKey = metaKeys[i];
2990
+ if (typeof imageMeta[metaKey] === 'undefined') {
2991
+ return false;
2992
+ }
2993
+ if (imageMeta[metaKey] !== meta[metaKey]) {
2994
+ return false;
2995
+ }
2996
+ }
2997
+ return true;
2998
+ };
2999
+
3000
+ /**
3001
+ * Check is the provided position can be set.
3002
+ *
3003
+ * @param {dwv.math.Point} position The position.
3004
+ * @returns {boolean} True is the position is in bounds.
3005
+ */
2892
3006
  this.canSetPosition = function (position) {
2893
3007
  return view.canSetPosition(position);
2894
3008
  };
@@ -2937,6 +3051,25 @@ dwv.ctrl.ViewController = function (view) {
2937
3051
  return view.setCurrentIndex(index, silent);
2938
3052
  };
2939
3053
 
3054
+ /**
3055
+ * Get a 3D position from a plane 2D position.
3056
+ *
3057
+ * @param {dwv.math.Point2D} point2D The 2D position as {x,y}.
3058
+ * @returns {dwv.math.Point} The 3D point.
3059
+ */
3060
+ this.getPositionFromPlanePoint = function (point2D) {
3061
+ // keep third direction
3062
+ var k = this.getCurrentScrollIndexValue();
3063
+ var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
3064
+ // de-orient
3065
+ var point = planeHelper.getImageOrientedVector3D(planePoint);
3066
+ // ~indexToWorld to not loose precision
3067
+ var geometry = view.getImage().getGeometry();
3068
+ var point3D = geometry.pointToWorld(point);
3069
+ // merge with current position to keep extra dimensions
3070
+ return this.getCurrentPosition().mergeWith3D(point3D);
3071
+ };
3072
+
2940
3073
  /**
2941
3074
  * Get a plane 3D position from a plane 2D position: does not compensate
2942
3075
  * for the image origin. Needed for setting the scale center...
@@ -2949,7 +3082,7 @@ dwv.ctrl.ViewController = function (view) {
2949
3082
  var k = this.getCurrentScrollIndexValue();
2950
3083
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2951
3084
  // de-orient
2952
- var point = planeHelper.getDeOrientedVector3D(planePoint);
3085
+ var point = planeHelper.getTargetDeOrientedVector3D(planePoint);
2953
3086
  // ~indexToWorld to not loose precision
2954
3087
  var geometry = view.getImage().getGeometry();
2955
3088
  var spacing = geometry.getRealSpacing();
@@ -4496,7 +4629,7 @@ dwv.dicom = dwv.dicom || {};
4496
4629
  * @returns {string} The version of the library.
4497
4630
  */
4498
4631
  dwv.getVersion = function () {
4499
- return '0.31.0-beta.4';
4632
+ return '0.31.0-beta.7';
4500
4633
  };
4501
4634
 
4502
4635
  /**
@@ -11290,6 +11423,22 @@ dwv.gui.DrawLayer = function (containerDiv) {
11290
11423
  */
11291
11424
  var baseOffset = {x: 0, y: 0};
11292
11425
 
11426
+ /**
11427
+ * The view offset.
11428
+ *
11429
+ * @private
11430
+ * @type {object}
11431
+ */
11432
+ var viewOffset = {x: 0, y: 0};
11433
+
11434
+ /**
11435
+ * The zoom offset.
11436
+ *
11437
+ * @private
11438
+ * @type {object}
11439
+ */
11440
+ var zoomOffset = {x: 0, y: 0};
11441
+
11293
11442
  /**
11294
11443
  * The draw controller.
11295
11444
  *
@@ -11409,16 +11558,59 @@ dwv.gui.DrawLayer = function (containerDiv) {
11409
11558
  * Set the layer scale.
11410
11559
  *
11411
11560
  * @param {object} newScale The scale as {x,y}.
11561
+ * @param {dwv.math.Point3D} center The scale center.
11412
11562
  */
11413
- this.setScale = function (newScale) {
11414
- var orientedNewScale = planeHelper.getOrientedXYZ(newScale);
11415
- var fullScale = {
11563
+ this.setScale = function (newScale, center) {
11564
+ var orientedNewScale = planeHelper.getTargetOrientedXYZ(newScale);
11565
+ var finalNewScale = {
11416
11566
  x: fitScale.x * orientedNewScale.x,
11417
11567
  y: fitScale.y * orientedNewScale.y
11418
11568
  };
11419
- konvaStage.scale(fullScale);
11420
- // update labelss
11421
- updateLabelScale(fullScale);
11569
+
11570
+ var offset = konvaStage.offset();
11571
+
11572
+ if (newScale.x === 1 &&
11573
+ newScale.y === 1 &&
11574
+ newScale.z === 1) {
11575
+ // reset zoom offset for scale=1
11576
+ var resetOffset = {
11577
+ x: offset.x - zoomOffset.x,
11578
+ y: offset.y - zoomOffset.y
11579
+ };
11580
+ // store new offset
11581
+ zoomOffset = {x: 0, y: 0};
11582
+ konvaStage.offset(resetOffset);
11583
+ } else {
11584
+ if (typeof center !== 'undefined') {
11585
+ var worldCenter = planeHelper.getPlaneOffsetFromOffset3D({
11586
+ x: center.getX(),
11587
+ y: center.getY(),
11588
+ z: center.getZ()
11589
+ });
11590
+ // center was obtained with viewLayer.displayToPlanePosNoBase
11591
+ // compensated for baseOffset
11592
+ // TODO: justify...
11593
+ worldCenter = {
11594
+ x: worldCenter.x + baseOffset.x,
11595
+ y: worldCenter.y + baseOffset.y
11596
+ };
11597
+
11598
+ var newOffset = dwv.gui.getScaledOffset(
11599
+ offset, konvaStage.scale(), finalNewScale, worldCenter);
11600
+
11601
+ var newZoomOffset = {
11602
+ x: zoomOffset.x + newOffset.x - offset.x,
11603
+ y: zoomOffset.y + newOffset.y - offset.y
11604
+ };
11605
+ // store new offset
11606
+ zoomOffset = newZoomOffset;
11607
+ konvaStage.offset(newOffset);
11608
+ }
11609
+ }
11610
+
11611
+ konvaStage.scale(finalNewScale);
11612
+ // update labels
11613
+ updateLabelScale(finalNewScale);
11422
11614
  };
11423
11615
 
11424
11616
  /**
@@ -11429,8 +11621,8 @@ dwv.gui.DrawLayer = function (containerDiv) {
11429
11621
  this.setOffset = function (newOffset) {
11430
11622
  var planeNewOffset = planeHelper.getPlaneOffsetFromOffset3D(newOffset);
11431
11623
  konvaStage.offset({
11432
- x: baseOffset.x + planeNewOffset.x,
11433
- y: baseOffset.y + planeNewOffset.y
11624
+ x: viewOffset.x + baseOffset.x + zoomOffset.x + planeNewOffset.x,
11625
+ y: viewOffset.y + baseOffset.y + zoomOffset.y + planeNewOffset.y
11434
11626
  });
11435
11627
  };
11436
11628
 
@@ -11528,18 +11720,38 @@ dwv.gui.DrawLayer = function (containerDiv) {
11528
11720
  *
11529
11721
  * @param {number} fitScale1D The 1D fit scale.
11530
11722
  * @param {object} fitSize The fit size as {x,y}.
11723
+ * @param {object} fitOffset The fit offset as {x,y}.
11531
11724
  */
11532
- this.fitToContainer = function (fitScale1D, fitSize) {
11725
+ this.fitToContainer = function (fitScale1D, fitSize, fitOffset) {
11726
+ // update konva
11727
+ konvaStage.setWidth(fitSize.x);
11728
+ konvaStage.setHeight(fitSize.y);
11729
+
11730
+ // previous scale without fit
11731
+ var previousScale = {
11732
+ x: konvaStage.scale().x / fitScale.x,
11733
+ y: konvaStage.scale().y / fitScale.y
11734
+ };
11533
11735
  // update fit scale
11534
11736
  fitScale = {
11535
11737
  x: fitScale1D * baseSpacing.x,
11536
11738
  y: fitScale1D * baseSpacing.y
11537
11739
  };
11538
- // update konva
11539
- konvaStage.setWidth(fitSize.x);
11540
- konvaStage.setHeight(fitSize.y);
11541
- // reset scale
11542
- this.setScale({x: 1, y: 1, z: 1});
11740
+ // update scale
11741
+ konvaStage.scale({
11742
+ x: previousScale.x * fitScale.x,
11743
+ y: previousScale.y * fitScale.y
11744
+ });
11745
+
11746
+ // update offsets
11747
+ viewOffset = {
11748
+ x: fitOffset.x / fitScale.x,
11749
+ y: fitOffset.y / fitScale.y
11750
+ };
11751
+ konvaStage.offset({
11752
+ x: viewOffset.x + baseOffset.x + zoomOffset.x,
11753
+ y: viewOffset.y + baseOffset.y + zoomOffset.y
11754
+ });
11543
11755
  };
11544
11756
 
11545
11757
  /**
@@ -11870,27 +12082,74 @@ dwv.gui.getLayerDetailsFromEvent = function (event) {
11870
12082
  };
11871
12083
 
11872
12084
  /**
11873
- * Get a view orientation according to an image geometry (with its orientation)
11874
- * and target orientation.
12085
+ * Get the view orientation according to an image and target orientation.
12086
+ * The view orientation is used to go from target to image space.
11875
12087
  *
11876
- * @param {dwv.image.Geometry} imageGeometry The image geometry.
12088
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11877
12089
  * @param {dwv.math.Matrix33} targetOrientation The target orientation.
11878
12090
  * @returns {dwv.math.Matrix33} The view orientation.
11879
12091
  */
11880
- dwv.gui.getViewOrientation = function (imageGeometry, targetOrientation) {
12092
+ dwv.gui.getViewOrientation = function (imageOrientation, targetOrientation) {
11881
12093
  var viewOrientation = dwv.math.getIdentityMat33();
11882
12094
  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
12095
+ // i: image, v: view, t: target, O: orientation, P: point
12096
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
12097
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
12098
+ // -> Ov = (Oi)-1 * Ot
12099
+ // TODO: asOneAndZeros simplifies but not nice...
11888
12100
  viewOrientation =
11889
- imgOrientation.getInverse().multiply(targetOrientation);
12101
+ imageOrientation.asOneAndZeros().getInverse().multiply(targetOrientation);
11890
12102
  }
11891
12103
  return viewOrientation;
11892
12104
  };
11893
12105
 
12106
+ /**
12107
+ * Get the target orientation according to an image and view orientation.
12108
+ * The target orientation is used to go from target to real space.
12109
+ *
12110
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
12111
+ * @param {dwv.math.Matrix33} viewOrientation The view orientation.
12112
+ * @returns {dwv.math.Matrix33} The target orientation.
12113
+ */
12114
+ dwv.gui.getTargetOrientation = function (imageOrientation, viewOrientation) {
12115
+ // i: image, v: view, t: target, O: orientation, P: point
12116
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
12117
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
12118
+ // -> Ot = Oi * Ov
12119
+ // note: asOneAndZeros as in dwv.gui.getViewOrientation...
12120
+ return imageOrientation.asOneAndZeros().multiply(viewOrientation);
12121
+ };
12122
+
12123
+ /**
12124
+ * Get a scaled offset to adapt to new scale and such as the input center
12125
+ * stays at the same position.
12126
+ *
12127
+ * @param {object} offset The previous offset as {x,y}.
12128
+ * @param {object} scale The previous scale as {x,y}.
12129
+ * @param {object} newScale The new scale as {x,y}.
12130
+ * @param {object} center The scale center as {x,y}.
12131
+ * @returns {object} The scaled offset as {x,y}.
12132
+ */
12133
+ dwv.gui.getScaledOffset = function (offset, scale, newScale, center) {
12134
+ // worldPoint = indexPoint / scale + offset
12135
+ //=> indexPoint = (worldPoint - offset ) * scale
12136
+
12137
+ // plane center should stay the same:
12138
+ // indexCenter / newScale + newOffset =
12139
+ // indexCenter / oldScale + oldOffset
12140
+ //=> newOffset = indexCenter / oldScale + oldOffset -
12141
+ // indexCenter / newScale
12142
+ //=> newOffset = worldCenter - indexCenter / newScale
12143
+ var indexCenter = {
12144
+ x: (center.x - offset.x) * scale.x,
12145
+ y: (center.y - offset.y) * scale.y
12146
+ };
12147
+ return {
12148
+ x: center.x - (indexCenter.x / newScale.x),
12149
+ y: center.y - (indexCenter.y / newScale.y)
12150
+ };
12151
+ };
12152
+
11894
12153
  /**
11895
12154
  * Layer group.
11896
12155
  *
@@ -12085,6 +12344,24 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12085
12344
  return res;
12086
12345
  };
12087
12346
 
12347
+ /**
12348
+ * Search view layers for equal imae meta data.
12349
+ *
12350
+ * @param {object} meta The meta data to find.
12351
+ * @returns {Array} The list of view layers that contain matched data.
12352
+ */
12353
+ this.searchViewLayers = function (meta) {
12354
+ var res = [];
12355
+ for (var i = 0; i < layers.length; ++i) {
12356
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12357
+ if (layers[i].getViewController().equalImageMeta(meta)) {
12358
+ res.push(layers[i]);
12359
+ }
12360
+ }
12361
+ }
12362
+ return res;
12363
+ };
12364
+
12088
12365
  /**
12089
12366
  * Get the view layers data indices.
12090
12367
  *
@@ -12284,8 +12561,8 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12284
12561
  var index = new dwv.math.Index(event.value[0]);
12285
12562
  var position = new dwv.math.Point(event.value[1]);
12286
12563
  // origin of the first view layer
12287
- var baseOrigin0 = null;
12288
- var baseOrigin = null;
12564
+ var baseViewLayerOrigin0 = null;
12565
+ var baseViewLayerOrigin = null;
12289
12566
  // update position for all layers except the source one
12290
12567
  for (var i = 0; i < layers.length; ++i) {
12291
12568
 
@@ -12294,28 +12571,25 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12294
12571
  var hasSetOffset = false;
12295
12572
  if (layers[i] instanceof dwv.gui.ViewLayer) {
12296
12573
  var vc = layers[i].getViewController();
12574
+ // origin0 should always be there
12297
12575
  var origin0 = vc.getOrigin();
12576
+ // depending on position, origin could be undefined
12298
12577
  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
12578
 
12311
- // TODO: compensate for possible different orientation
12579
+ if (!baseViewLayerOrigin) {
12580
+ baseViewLayerOrigin0 = origin0;
12581
+ baseViewLayerOrigin = origin;
12582
+ } else {
12583
+ if (vc.canSetPosition(position) &&
12584
+ typeof origin !== 'undefined') {
12585
+ // TODO: compensate for possible different orientation between views
12312
12586
  // TODO: check why -z...
12313
12587
 
12314
- var scrollDiff = baseOrigin0.minus(origin0);
12588
+ var scrollDiff = baseViewLayerOrigin0.minus(origin0);
12315
12589
  var scrollOffset = new dwv.math.Vector3D(
12316
12590
  scrollDiff.getX(), scrollDiff.getY(), -1 * scrollDiff.getZ());
12317
12591
 
12318
- var planeDiff = baseOrigin.minus(origin);
12592
+ var planeDiff = baseViewLayerOrigin.minus(origin);
12319
12593
  var planeOffset = new dwv.math.Vector3D(
12320
12594
  planeDiff.getX(), planeDiff.getY(), -1 * planeDiff.getZ());
12321
12595
 
@@ -12347,32 +12621,81 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12347
12621
  };
12348
12622
 
12349
12623
  /**
12350
- * Fit the display to the size of the container.
12351
- * To be called once the image is loaded.
12624
+ * Calculate the fit scale: the scale that fits the largest data.
12352
12625
  *
12353
- * @param {object} realSize 2D real size (in mm) to fit provided as {x,y}.
12626
+ * @returns {number|undefined} The fit scale.
12354
12627
  */
12355
- this.fitToContainer = function (realSize) {
12356
- // check container size
12628
+ this.calculateFitScale = function () {
12629
+ // check container
12357
12630
  if (containerDiv.offsetWidth === 0 &&
12358
12631
  containerDiv.offsetHeight === 0) {
12359
12632
  throw new Error('Cannot fit to zero sized container.');
12360
12633
  }
12361
- // find best fit
12362
- var fitScale = Math.min(
12363
- containerDiv.offsetWidth / realSize.x,
12364
- containerDiv.offsetHeight / realSize.y
12634
+ // get max size
12635
+ var maxSize = this.getMaxSize();
12636
+ if (typeof maxSize === 'undefined') {
12637
+ return undefined;
12638
+ }
12639
+ // return best fit
12640
+ return Math.min(
12641
+ containerDiv.offsetWidth / maxSize.x,
12642
+ containerDiv.offsetHeight / maxSize.y
12365
12643
  );
12366
- var fitSize = {
12367
- x: Math.floor(realSize.x * fitScale),
12368
- y: Math.floor(realSize.y * fitScale)
12644
+ };
12645
+
12646
+ /**
12647
+ * Set the layer group fit scale.
12648
+ *
12649
+ * @param {number} scaleIn The fit scale.
12650
+ */
12651
+ this.setFitScale = function (scaleIn) {
12652
+ // get maximum size
12653
+ var maxSize = this.getMaxSize();
12654
+ // exit if none
12655
+ if (typeof maxSize === 'undefined') {
12656
+ return;
12657
+ }
12658
+
12659
+ var containerSize = {
12660
+ x: containerDiv.offsetWidth,
12661
+ y: containerDiv.offsetHeight
12662
+ };
12663
+ // offset to keep data centered
12664
+ var fitOffset = {
12665
+ x: -0.5 * (containerSize.x - Math.floor(maxSize.x * scaleIn)),
12666
+ y: -0.5 * (containerSize.y - Math.floor(maxSize.y * scaleIn))
12369
12667
  };
12668
+
12370
12669
  // apply to layers
12371
12670
  for (var j = 0; j < layers.length; ++j) {
12372
- layers[j].fitToContainer(fitScale, fitSize);
12671
+ layers[j].fitToContainer(scaleIn, containerSize, fitOffset);
12373
12672
  }
12374
12673
  };
12375
12674
 
12675
+ /**
12676
+ * Get the largest data size.
12677
+ *
12678
+ * @returns {object|undefined} The largest size as {x,y}.
12679
+ */
12680
+ this.getMaxSize = function () {
12681
+ var maxSize = {x: 0, y: 0};
12682
+ for (var j = 0; j < layers.length; ++j) {
12683
+ if (layers[j] instanceof dwv.gui.ViewLayer) {
12684
+ var size = layers[j].getImageWorldSize();
12685
+ if (size.x > maxSize.x) {
12686
+ maxSize.x = size.x;
12687
+ }
12688
+ if (size.y > maxSize.y) {
12689
+ maxSize.y = size.y;
12690
+ }
12691
+ }
12692
+ }
12693
+ if (maxSize.x === 0 && maxSize.y === 0) {
12694
+ maxSize = undefined;
12695
+ }
12696
+ return maxSize;
12697
+ };
12698
+
12376
12699
  /**
12377
12700
  * Add scale to the layers. Scale cannot go lower than 0.1.
12378
12701
  *
@@ -12385,35 +12708,33 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12385
12708
  y: scale.y * (1 + scaleStep),
12386
12709
  z: scale.z * (1 + scaleStep)
12387
12710
  };
12388
- var centerPlane = {
12389
- x: (center.getX() - offset.x) * scale.x,
12390
- y: (center.getY() - offset.y) * scale.y,
12391
- z: (center.getZ() - offset.z) * scale.z
12392
- };
12393
- // center should stay the same:
12394
- // center / newScale + newOffset = center / oldScale + oldOffset
12395
- // => newOffset = center / oldScale + oldOffset - center / newScale
12396
- var newOffset = {
12397
- x: (centerPlane.x / scale.x) + offset.x - (centerPlane.x / newScale.x),
12398
- y: (centerPlane.y / scale.y) + offset.y - (centerPlane.y / newScale.y),
12399
- z: (centerPlane.z / scale.z) + offset.z - (centerPlane.z / newScale.z)
12400
- };
12401
-
12402
- this.setOffset(newOffset);
12403
- this.setScale(newScale);
12711
+ this.setScale(newScale, center);
12404
12712
  };
12405
12713
 
12406
12714
  /**
12407
12715
  * Set the layers' scale.
12408
12716
  *
12409
12717
  * @param {object} newScale The scale to apply as {x,y,z}.
12718
+ * @param {dwv.math.Point3D} center The scale center Point3D.
12410
12719
  * @fires dwv.ctrl.LayerGroup#zoomchange
12411
12720
  */
12412
- this.setScale = function (newScale) {
12721
+ this.setScale = function (newScale, center) {
12413
12722
  scale = newScale;
12414
12723
  // apply to layers
12415
12724
  for (var i = 0; i < layers.length; ++i) {
12416
- layers[i].setScale(scale);
12725
+ layers[i].setScale(scale, center);
12726
+ }
12727
+
12728
+ // event value
12729
+ var value = [
12730
+ newScale.x,
12731
+ newScale.y,
12732
+ newScale.z
12733
+ ];
12734
+ if (typeof center !== 'undefined') {
12735
+ value.push(center.getX());
12736
+ value.push(center.getY());
12737
+ value.push(center.getZ());
12417
12738
  }
12418
12739
 
12419
12740
  /**
@@ -12425,7 +12746,7 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12425
12746
  */
12426
12747
  fireEvent({
12427
12748
  type: 'zoomchange',
12428
- value: [scale.x, scale.y, scale.z],
12749
+ value: value
12429
12750
  });
12430
12751
  };
12431
12752
 
@@ -12578,11 +12899,20 @@ dwv.gui.ZoomBinder = function () {
12578
12899
  };
12579
12900
  this.getCallback = function (layerGroup) {
12580
12901
  return function (event) {
12581
- layerGroup.setScale({
12902
+ var scale = {
12582
12903
  x: event.value[0],
12583
12904
  y: event.value[1],
12584
12905
  z: event.value[2]
12585
- });
12906
+ };
12907
+ var center;
12908
+ if (event.value.length === 6) {
12909
+ center = new dwv.math.Point3D(
12910
+ event.value[3],
12911
+ event.value[4],
12912
+ event.value[5]
12913
+ );
12914
+ }
12915
+ layerGroup.setScale(scale, center);
12586
12916
  layerGroup.draw();
12587
12917
  };
12588
12918
  };
@@ -12770,6 +13100,33 @@ dwv.gui.Stage = function () {
12770
13100
  }
12771
13101
  };
12772
13102
 
13103
+ /**
13104
+ * Synchronise the fit scale of the group layers.
13105
+ */
13106
+ this.syncLayerGroupScale = function () {
13107
+ var minScale;
13108
+ var hasScale = [];
13109
+ for (var i = 0; i < layerGroups.length; ++i) {
13110
+ var scale = layerGroups[i].calculateFitScale();
13111
+ if (typeof scale !== 'undefined') {
13112
+ hasScale.push(i);
13113
+ if (typeof minScale === 'undefined' || scale < minScale) {
13114
+ minScale = scale;
13115
+ }
13116
+ }
13117
+ }
13118
+ // exit if no scale
13119
+ if (typeof minScale === 'undefined') {
13120
+ return;
13121
+ }
13122
+ // apply min scale to layers
13123
+ for (var j = 0; j < layerGroups.length; ++j) {
13124
+ if (hasScale.includes(j)) {
13125
+ layerGroups[j].setFitScale(minScale);
13126
+ }
13127
+ }
13128
+ };
13129
+
12773
13130
  /**
12774
13131
  * Bind the layer groups of the stage.
12775
13132
  */
@@ -13200,12 +13557,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13200
13557
  var context = null;
13201
13558
 
13202
13559
  /**
13203
- * Flag to know if the context has been cleared.
13560
+ * Flag to know if the current position is valid.
13204
13561
  *
13205
13562
  * @private
13206
13563
  * @type {boolean}
13207
13564
  */
13208
- var isContextClear = true;
13565
+ var isValidPosition = true;
13209
13566
 
13210
13567
  /**
13211
13568
  * The image data array.
@@ -13271,6 +13628,22 @@ dwv.gui.ViewLayer = function (containerDiv) {
13271
13628
  */
13272
13629
  var baseOffset = {x: 0, y: 0};
13273
13630
 
13631
+ /**
13632
+ * The view offset.
13633
+ *
13634
+ * @private
13635
+ * @type {object}
13636
+ */
13637
+ var viewOffset = {x: 0, y: 0};
13638
+
13639
+ /**
13640
+ * The zoom offset.
13641
+ *
13642
+ * @private
13643
+ * @type {object}
13644
+ */
13645
+ var zoomOffset = {x: 0, y: 0};
13646
+
13274
13647
  /**
13275
13648
  * Data update flag.
13276
13649
  *
@@ -13350,6 +13723,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
13350
13723
  // event.value = [index, image]
13351
13724
  if (dataIndex === event.value[0]) {
13352
13725
  viewController.setImage(event.value[1]);
13726
+ setBaseSize(viewController.getImageSize().get2D());
13353
13727
  needsDataUpdate = true;
13354
13728
  }
13355
13729
  };
@@ -13374,6 +13748,15 @@ dwv.gui.ViewLayer = function (containerDiv) {
13374
13748
  return baseSize;
13375
13749
  };
13376
13750
 
13751
+ /**
13752
+ * Get the image world (mm) 2D size.
13753
+ *
13754
+ * @returns {object} The 2D size as {x,y}.
13755
+ */
13756
+ this.getImageWorldSize = function () {
13757
+ return viewController.getImageWorldSize();
13758
+ };
13759
+
13377
13760
  /**
13378
13761
  * Get the layer opacity.
13379
13762
  *
@@ -13389,6 +13772,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
13389
13772
  * @param {number} alpha The opacity ([0:1] range).
13390
13773
  */
13391
13774
  this.setOpacity = function (alpha) {
13775
+ if (alpha === opacity) {
13776
+ return;
13777
+ }
13778
+
13392
13779
  opacity = Math.min(Math.max(alpha, 0), 1);
13393
13780
 
13394
13781
  /**
@@ -13409,14 +13796,57 @@ dwv.gui.ViewLayer = function (containerDiv) {
13409
13796
  * Set the layer scale.
13410
13797
  *
13411
13798
  * @param {object} newScale The scale as {x,y}.
13799
+ * @param {dwv.math.Point3D} center The scale center.
13412
13800
  */
13413
- this.setScale = function (newScale) {
13801
+ this.setScale = function (newScale, center) {
13414
13802
  var helper = viewController.getPlaneHelper();
13415
- var orientedNewScale = helper.getOrientedXYZ(newScale);
13416
- scale = {
13803
+ var orientedNewScale = helper.getTargetOrientedXYZ(newScale);
13804
+ var finalNewScale = {
13417
13805
  x: fitScale.x * orientedNewScale.x,
13418
13806
  y: fitScale.y * orientedNewScale.y
13419
13807
  };
13808
+
13809
+ if (newScale.x === 1 &&
13810
+ newScale.y === 1 &&
13811
+ newScale.z === 1) {
13812
+ // reset zoom offset for scale=1
13813
+ var resetOffset = {
13814
+ x: offset.x - zoomOffset.x,
13815
+ y: offset.y - zoomOffset.y
13816
+ };
13817
+ // store new offset
13818
+ zoomOffset = {x: 0, y: 0};
13819
+ offset = resetOffset;
13820
+ } else {
13821
+ if (typeof center !== 'undefined') {
13822
+ var worldCenter = helper.getPlaneOffsetFromOffset3D({
13823
+ x: center.getX(),
13824
+ y: center.getY(),
13825
+ z: center.getZ()
13826
+ });
13827
+ // center was obtained with viewLayer.displayToPlanePosNoBase
13828
+ // compensated for baseOffset
13829
+ // TODO: justify...
13830
+ worldCenter = {
13831
+ x: worldCenter.x + baseOffset.x,
13832
+ y: worldCenter.y + baseOffset.y
13833
+ };
13834
+
13835
+ var newOffset = dwv.gui.getScaledOffset(
13836
+ offset, scale, finalNewScale, worldCenter);
13837
+
13838
+ var newZoomOffset = {
13839
+ x: zoomOffset.x + newOffset.x - offset.x,
13840
+ y: zoomOffset.y + newOffset.y - offset.y
13841
+ };
13842
+ // store new offset
13843
+ zoomOffset = newZoomOffset;
13844
+ offset = newOffset;
13845
+ }
13846
+ }
13847
+
13848
+ // store new scale
13849
+ scale = finalNewScale;
13420
13850
  };
13421
13851
 
13422
13852
  /**
@@ -13456,8 +13886,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13456
13886
  var helper = viewController.getPlaneHelper();
13457
13887
  var planeNewOffset = helper.getPlaneOffsetFromOffset3D(newOffset);
13458
13888
  offset = {
13459
- x: baseOffset.x + planeNewOffset.x,
13460
- y: baseOffset.y + planeNewOffset.y
13889
+ x: viewOffset.x + baseOffset.x + zoomOffset.x + planeNewOffset.x,
13890
+ y: viewOffset.y + baseOffset.y + zoomOffset.y + planeNewOffset.y
13461
13891
  };
13462
13892
  };
13463
13893
 
@@ -13546,6 +13976,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13546
13976
  * @fires dwv.App#renderend
13547
13977
  */
13548
13978
  this.draw = function () {
13979
+ // skip for non valid position
13980
+ if (!isValidPosition) {
13981
+ return;
13982
+ }
13983
+
13549
13984
  /**
13550
13985
  * Render start event.
13551
13986
  *
@@ -13555,7 +13990,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13555
13990
  */
13556
13991
  var event = {
13557
13992
  type: 'renderstart',
13558
- layerid: this.getId()
13993
+ layerid: this.getId(),
13994
+ dataid: this.getDataIndex()
13559
13995
  };
13560
13996
  fireEvent(event);
13561
13997
 
@@ -13590,9 +14026,6 @@ dwv.gui.ViewLayer = function (containerDiv) {
13590
14026
  // draw image
13591
14027
  context.drawImage(offscreenCanvas, 0, 0);
13592
14028
 
13593
- // set clear flag
13594
- isContextClear = false;
13595
-
13596
14029
  /**
13597
14030
  * Render end event.
13598
14031
  *
@@ -13602,7 +14035,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13602
14035
  */
13603
14036
  event = {
13604
14037
  type: 'renderend',
13605
- layerid: this.getId()
14038
+ layerid: this.getId(),
14039
+ dataid: this.getDataIndex()
13606
14040
  };
13607
14041
  fireEvent(event);
13608
14042
  };
@@ -13613,12 +14047,13 @@ dwv.gui.ViewLayer = function (containerDiv) {
13613
14047
  * @param {object} size The image size as {x,y}.
13614
14048
  * @param {object} spacing The image spacing as {x,y}.
13615
14049
  * @param {number} index The associated data index.
14050
+ * @param {number} alpha The initial data opacity.
13616
14051
  */
13617
- this.initialise = function (size, spacing, index) {
14052
+ this.initialise = function (size, spacing, index, alpha) {
13618
14053
  // set locals
13619
- baseSize = size;
13620
14054
  baseSpacing = spacing;
13621
14055
  dataIndex = index;
14056
+ opacity = Math.min(Math.max(alpha, 0), 1);
13622
14057
 
13623
14058
  // create canvas
13624
14059
  // (canvas size is set in fitToContainer)
@@ -13637,36 +14072,47 @@ dwv.gui.ViewLayer = function (containerDiv) {
13637
14072
  return;
13638
14073
  }
13639
14074
 
13640
- // check canvas
13641
- if (!dwv.gui.canCreateCanvas(baseSize.x, baseSize.y)) {
13642
- throw new Error('Cannot create canvas ' + baseSize.x + ', ' + baseSize.y);
14075
+ // off screen canvas
14076
+ offscreenCanvas = document.createElement('canvas');
14077
+
14078
+ // set base size: needs an existing context and off screen canvas
14079
+ setBaseSize(size);
14080
+
14081
+ // update data on first draw
14082
+ needsDataUpdate = true;
14083
+ };
14084
+
14085
+ /**
14086
+ * Set the base size of the layer.
14087
+ *
14088
+ * @param {object} size The size as {x,y}.
14089
+ */
14090
+ function setBaseSize(size) {
14091
+ // check canvas creation
14092
+ if (!dwv.gui.canCreateCanvas(size.x, size.y)) {
14093
+ throw new Error('Cannot create canvas ' + size.x + ', ' + size.y);
13643
14094
  }
13644
14095
 
14096
+ // set local
14097
+ baseSize = size;
14098
+
13645
14099
  // off screen canvas
13646
- offscreenCanvas = document.createElement('canvas');
13647
14100
  offscreenCanvas.width = baseSize.x;
13648
14101
  offscreenCanvas.height = baseSize.y;
13649
14102
  // original empty image data array
13650
14103
  context.clearRect(0, 0, baseSize.x, baseSize.y);
13651
14104
  imageData = context.createImageData(baseSize.x, baseSize.y);
13652
-
13653
- // update data on first draw
13654
- needsDataUpdate = true;
13655
- };
14105
+ }
13656
14106
 
13657
14107
  /**
13658
14108
  * Fit the layer to its parent container.
13659
14109
  *
13660
14110
  * @param {number} fitScale1D The 1D fit scale.
13661
14111
  * @param {object} fitSize The fit size as {x,y}.
14112
+ * @param {object} fitOffset The fit offset as {x,y}.
13662
14113
  */
13663
- this.fitToContainer = function (fitScale1D, fitSize) {
13664
- // update fit scale
13665
- fitScale = {
13666
- x: fitScale1D * baseSpacing.x,
13667
- y: fitScale1D * baseSpacing.y
13668
- };
13669
- // new canvas size
14114
+ this.fitToContainer = function (fitScale1D, fitSize, fitOffset) {
14115
+ // update canvas size
13670
14116
  var width = fitSize.x;
13671
14117
  var height = fitSize.y;
13672
14118
  if (!dwv.gui.canCreateCanvas(width, height)) {
@@ -13674,8 +14120,32 @@ dwv.gui.ViewLayer = function (containerDiv) {
13674
14120
  }
13675
14121
  canvas.width = width;
13676
14122
  canvas.height = height;
13677
- // reset scale
13678
- this.setScale({x: 1, y: 1, z: 1});
14123
+
14124
+ // previous scale without fit
14125
+ var previousScale = {
14126
+ x: scale.x / fitScale.x,
14127
+ y: scale.y / fitScale.y
14128
+ };
14129
+ // update fit scale
14130
+ fitScale = {
14131
+ x: fitScale1D * baseSpacing.x,
14132
+ y: fitScale1D * baseSpacing.y
14133
+ };
14134
+ // update scale
14135
+ scale = {
14136
+ x: previousScale.x * fitScale.x,
14137
+ y: previousScale.y * fitScale.y
14138
+ };
14139
+
14140
+ // update offsets
14141
+ viewOffset = {
14142
+ x: fitOffset.x / fitScale.x,
14143
+ y: fitOffset.y / fitScale.y
14144
+ };
14145
+ offset = {
14146
+ x: viewOffset.x + baseOffset.x + zoomOffset.x,
14147
+ y: viewOffset.y + baseOffset.y + zoomOffset.y
14148
+ };
13679
14149
  };
13680
14150
 
13681
14151
  /**
@@ -13760,8 +14230,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13760
14230
  */
13761
14231
  function onWLChange(event) {
13762
14232
  // generate and draw if no skip flag
13763
- if (typeof event.skipGenerate === 'undefined' ||
13764
- event.skipGenerate === false) {
14233
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14234
+ event.skipGenerate === true;
14235
+ if (!skip) {
13765
14236
  needsDataUpdate = true;
13766
14237
  self.draw();
13767
14238
  }
@@ -13774,8 +14245,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13774
14245
  * @private
13775
14246
  */
13776
14247
  function onColourChange(_event) {
13777
- needsDataUpdate = true;
13778
- self.draw();
14248
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14249
+ event.skipGenerate === true;
14250
+ if (!skip) {
14251
+ needsDataUpdate = true;
14252
+ self.draw();
14253
+ }
13779
14254
  }
13780
14255
 
13781
14256
  /**
@@ -13785,26 +14260,35 @@ dwv.gui.ViewLayer = function (containerDiv) {
13785
14260
  * @private
13786
14261
  */
13787
14262
  function onPositionChange(event) {
13788
- if (typeof event.skipGenerate === 'undefined' ||
13789
- event.skipGenerate === false) {
14263
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14264
+ event.skipGenerate === true;
14265
+ if (!skip) {
14266
+ var valid = typeof event.valid === 'undefined' || event.valid;
13790
14267
  // 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();
14268
+ if (!valid) {
14269
+ // clear only once
14270
+ if (isValidPosition) {
14271
+ isValidPosition = false;
14272
+ self.clear();
14273
+ }
14274
+ } else {
14275
+ // 3D dimensions
14276
+ var dims3D = [0, 1, 2];
14277
+ // remove scroll index
14278
+ var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
14279
+ dims3D.splice(indexScrollIndex, 1);
14280
+ // remove non scroll index from diff dims
14281
+ var diffDims = event.diffDims.filter(function (item) {
14282
+ return dims3D.indexOf(item) === -1;
14283
+ });
14284
+ // update if we have something left
14285
+ if (diffDims.length !== 0 || !isValidPosition) {
14286
+ // reset valid flag
14287
+ isValidPosition = true;
14288
+ // reset update flag
14289
+ needsDataUpdate = true;
14290
+ self.draw();
14291
+ }
13808
14292
  }
13809
14293
  }
13810
14294
  }
@@ -13816,8 +14300,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13816
14300
  * @private
13817
14301
  */
13818
14302
  function onAlphaFuncChange(event) {
13819
- if (typeof event.skipGenerate === 'undefined' ||
13820
- event.skipGenerate === false) {
14303
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14304
+ event.skipGenerate === true;
14305
+ if (!skip) {
13821
14306
  needsDataUpdate = true;
13822
14307
  self.draw();
13823
14308
  }
@@ -13838,19 +14323,14 @@ dwv.gui.ViewLayer = function (containerDiv) {
13838
14323
  * Clear the context.
13839
14324
  */
13840
14325
  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
- }
14326
+ // clear the context: reset the transform first
14327
+ // store the current transformation matrix
14328
+ context.save();
14329
+ // use the identity matrix while clearing the canvas
14330
+ context.setTransform(1, 0, 0, 1, 0, 0);
14331
+ context.clearRect(0, 0, canvas.width, canvas.height);
14332
+ // restore the transform
14333
+ context.restore();
13854
14334
  };
13855
14335
 
13856
14336
  /**
@@ -15120,6 +15600,22 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15120
15600
  this.getOrigins = function () {
15121
15601
  return origins;
15122
15602
  };
15603
+
15604
+ /**
15605
+ * Check if a point is in the origin list.
15606
+ *
15607
+ * @param {dwv.math.Point3D} point3D The point to check.
15608
+ * @returns {boolean} True if in list.
15609
+ */
15610
+ this.includesOrigin = function (point3D) {
15611
+ for (var i = 0; i < origins.length; ++i) {
15612
+ if (origins[i].isSimilar(point3D, dwv.math.BIG_EPSILON)) {
15613
+ return true;
15614
+ }
15615
+ }
15616
+ return false;
15617
+ };
15618
+
15123
15619
  /**
15124
15620
  * Get the object size.
15125
15621
  * Warning: the size comes as stored in DICOM, meaning that it could
@@ -15138,7 +15634,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15138
15634
  size.get(2)
15139
15635
  ],
15140
15636
  viewOrientation);
15141
- res = new dwv.image.Size(values);
15637
+ res = new dwv.image.Size(values.concat(size.getValues().slice(3)));
15142
15638
  }
15143
15639
  return res;
15144
15640
  };
@@ -15153,26 +15649,26 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15153
15649
  if (origins.length === 1) {
15154
15650
  return 1;
15155
15651
  }
15156
- var spacing = null;
15652
+ var sliceSpacing = null;
15157
15653
  // (x, y, z) = orientationMatrix * (i, j, k)
15158
15654
  // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
15159
15655
  // applied on the patient position, reorders indices
15160
15656
  // so that Z is the slice direction
15161
- var orientation2 = orientation.getInverse().asOneAndZeros();
15657
+ var invOrientation = orientation.getInverse();
15162
15658
  var deltas = [];
15163
15659
  for (var i = 0; i < origins.length - 1; ++i) {
15164
- var origin1 = orientation2.multiplyVector3D(origins[i]);
15165
- var origin2 = orientation2.multiplyVector3D(origins[i + 1]);
15660
+ var origin1 = invOrientation.multiplyVector3D(origins[i]);
15661
+ var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
15166
15662
  var diff = Math.abs(origin1.getZ() - origin2.getZ());
15167
15663
  if (diff === 0) {
15168
15664
  throw new Error('Zero slice spacing.' +
15169
15665
  origin1.toString() + ' ' + origin2.toString());
15170
15666
  }
15171
- if (spacing === null) {
15172
- spacing = diff;
15667
+ if (sliceSpacing === null) {
15668
+ sliceSpacing = diff;
15173
15669
  } else {
15174
- if (!dwv.math.isSimilar(spacing, diff, dwv.math.BIG_EPSILON)) {
15175
- deltas.push(Math.abs(spacing - diff));
15670
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
15671
+ deltas.push(Math.abs(sliceSpacing - diff));
15176
15672
  }
15177
15673
  }
15178
15674
  }
@@ -15187,7 +15683,8 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15187
15683
  mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
15188
15684
  }
15189
15685
  }
15190
- return spacing;
15686
+
15687
+ return sliceSpacing;
15191
15688
  };
15192
15689
 
15193
15690
  /**
@@ -15226,6 +15723,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15226
15723
  * @returns {dwv.image.Spacing} The object spacing.
15227
15724
  */
15228
15725
  this.getRealSpacing = function () {
15726
+ // asOneAndZeros to not change spacing values...
15229
15727
  return this.getSpacing(orientation.getInverse().asOneAndZeros());
15230
15728
  };
15231
15729
 
@@ -15278,8 +15776,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15278
15776
  // -> >0 => vectors are codirectional
15279
15777
  // -> <0 => vectors are oposite
15280
15778
  var dotProd = normal.dotProduct(pointDir);
15779
+ var test = dotProd < 0;
15780
+ // TODO: check why coronal behaves differently...
15781
+ if (orientation.getThirdColMajorDirection() === 1) {
15782
+ test = dotProd > 0;
15783
+ }
15281
15784
  // oposite vectors get higher index
15282
- var sliceIndex = (dotProd < 0) ? closestSliceIndex + 1 : closestSliceIndex;
15785
+ var sliceIndex = test ? closestSliceIndex + 1 : closestSliceIndex;
15283
15786
  return sliceIndex;
15284
15787
  };
15285
15788
 
@@ -15357,10 +15860,11 @@ dwv.image.Geometry.prototype.isInBounds = function (point) {
15357
15860
  * Check that a index is within bounds.
15358
15861
  *
15359
15862
  * @param {dwv.math.Index} index The index to check.
15863
+ * @param {number} dir Optional direction to check.
15360
15864
  * @returns {boolean} True if the given coordinates are within bounds.
15361
15865
  */
15362
- dwv.image.Geometry.prototype.isIndexInBounds = function (index) {
15363
- return this.getSize().isInBounds(index);
15866
+ dwv.image.Geometry.prototype.isIndexInBounds = function (index, dir) {
15867
+ return this.getSize().isInBounds(index, dir);
15364
15868
  };
15365
15869
 
15366
15870
  /**
@@ -15457,9 +15961,11 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
15457
15961
  values[0] = Math.round(orientedPoint3D.getX() / spacing.get(0));
15458
15962
  values[1] = Math.round(orientedPoint3D.getY() / spacing.get(1));
15459
15963
  // 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
- );
15964
+ var k = Math.round(orientedPoint3D.getZ() / spacing.get(2));
15965
+ // abs to fix #1163
15966
+ // TODO: find out why k can sometimes be negative...
15967
+ values[2] = flipK(this.getSize(), k);
15968
+
15463
15969
  // return index
15464
15970
  return new dwv.math.Index(values);
15465
15971
  };
@@ -15898,7 +16404,7 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15898
16404
  }
15899
16405
  // all meta should be equal
15900
16406
  for (var key in meta) {
15901
- if (key === 'windowPresets') {
16407
+ if (key === 'windowPresets' || key === 'numberOfFiles') {
15902
16408
  continue;
15903
16409
  }
15904
16410
  if (meta[key] !== rhs.getMeta()[key]) {
@@ -15929,8 +16435,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15929
16435
  values[3] = timeId;
15930
16436
  }
15931
16437
  var index = new dwv.math.Index(values);
15932
- var primaryOffset = size.indexToOffset(index);
15933
- var secondaryOffset = this.getSecondaryOffset(index);
16438
+ var primaryOffset = size.indexToOffset(index) * numberOfComponents;
16439
+ var secondaryOffset = this.getSecondaryOffset(index) * numberOfComponents;
15934
16440
 
15935
16441
  // first frame special slice by slice append
15936
16442
  if (timeId === 0) {
@@ -17908,7 +18414,7 @@ dwv.image.MaskFactory.prototype.create = function (
17908
18414
  }
17909
18415
  // create frame info object from per frame func
17910
18416
  var frameInfos = [];
17911
- var posPats = [];
18417
+ var framePosPats = [];
17912
18418
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
17913
18419
  var frameFunc = perFrameFuncGroupSequence[j];
17914
18420
  // Derivation Image Sequence
@@ -17935,8 +18441,8 @@ dwv.image.MaskFactory.prototype.create = function (
17935
18441
  for (var p = 0; p < imagePosPat.length; ++p) {
17936
18442
  imagePosPat[p] = parseFloat(imagePosPat[p], 10);
17937
18443
  }
17938
- if (!includesPosPat(posPats, imagePosPat)) {
17939
- posPats.push(imagePosPat);
18444
+ if (!includesPosPat(framePosPats, imagePosPat)) {
18445
+ framePosPats.push(imagePosPat);
17940
18446
  }
17941
18447
  frameInfos.push({
17942
18448
  dimIndex: dimIndex,
@@ -17980,7 +18486,22 @@ dwv.image.MaskFactory.prototype.create = function (
17980
18486
  }
17981
18487
  }
17982
18488
  // sort positions patient
17983
- posPats.sort(comparePosPat);
18489
+ framePosPats.sort(comparePosPat);
18490
+
18491
+ // add missing posPats
18492
+ var posPats = [];
18493
+ var sliceSpacing = spacing.get(2);
18494
+ for (var g = 0; g < framePosPats.length - 1; ++g) {
18495
+ posPats.push(framePosPats[g]);
18496
+ var nextZ = framePosPats[g][2] - sliceSpacing;
18497
+ var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18498
+ while (diff > sliceSpacing) {
18499
+ posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
18500
+ nextZ -= sliceSpacing;
18501
+ diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18502
+ }
18503
+ }
18504
+ posPats.push(framePosPats[framePosPats.length - 1]);
17984
18505
 
17985
18506
  // create output buffer
17986
18507
  // as many slices as posPats -> gap slices between groups are not represented
@@ -18070,12 +18591,8 @@ dwv.image = dwv.image || {};
18070
18591
  */
18071
18592
  dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18072
18593
 
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);
18594
+ var targetOrientation = dwv.gui.getTargetOrientation(
18595
+ imageOrientation, viewOrientation);
18079
18596
 
18080
18597
  /**
18081
18598
  * Get a 3D offset from a plane one.
@@ -18088,9 +18605,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18088
18605
  var planeOffset = new dwv.math.Vector3D(
18089
18606
  offset2D.x, offset2D.y, 0);
18090
18607
  // de-orient
18091
- var pixelOffset = this.getDeOrientedVector3D(planeOffset);
18092
- // offset indexToWorld
18093
- return offsetIndexToWorld(pixelOffset);
18608
+ var pixelOffset = this.getTargetDeOrientedVector3D(planeOffset);
18609
+ // ~indexToWorld
18610
+ return new dwv.math.Vector3D(
18611
+ pixelOffset.getX() * spacing.get(0),
18612
+ pixelOffset.getY() * spacing.get(1),
18613
+ pixelOffset.getZ() * spacing.get(2));
18094
18614
  };
18095
18615
 
18096
18616
  /**
@@ -18100,10 +18620,13 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18100
18620
  * @returns {object} The plane offset as {x,y}.
18101
18621
  */
18102
18622
  this.getPlaneOffsetFromOffset3D = function (offset3D) {
18103
- // offset worldToIndex
18104
- var pixelOffset = offsetWorldToIndex(offset3D);
18623
+ // ~worldToIndex
18624
+ var pixelOffset = new dwv.math.Vector3D(
18625
+ offset3D.x / spacing.get(0),
18626
+ offset3D.y / spacing.get(1),
18627
+ offset3D.z / spacing.get(2));
18105
18628
  // orient
18106
- var planeOffset = this.getOrientedVector3D(pixelOffset);
18629
+ var planeOffset = this.getTargetOrientedVector3D(pixelOffset);
18107
18630
  // make 2D
18108
18631
  return {
18109
18632
  x: planeOffset.getX(),
@@ -18112,38 +18635,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18112
18635
  };
18113
18636
 
18114
18637
  /**
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.
18638
+ * Orient an input vector from real to target space.
18142
18639
  *
18143
18640
  * @param {dwv.math.Vector3D} vector The input vector.
18144
18641
  * @returns {dwv.math.Vector3D} The oriented vector.
18145
18642
  */
18146
- this.getOrientedVector3D = function (vector) {
18643
+ this.getTargetOrientedVector3D = function (vector) {
18147
18644
  var planeVector = vector;
18148
18645
  if (typeof targetOrientation !== 'undefined') {
18149
18646
  planeVector = targetOrientation.getInverse().multiplyVector3D(vector);
@@ -18152,46 +18649,12 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18152
18649
  };
18153
18650
 
18154
18651
  /**
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.
18652
+ * De-orient an input vector from target to real space.
18190
18653
  *
18191
18654
  * @param {dwv.math.Vector3D} planeVector The input vector.
18192
18655
  * @returns {dwv.math.Vector3D} The de-orienteded vector.
18193
18656
  */
18194
- this.getDeOrientedVector3D = function (planeVector) {
18657
+ this.getTargetDeOrientedVector3D = function (planeVector) {
18195
18658
  var vector = planeVector;
18196
18659
  if (typeof targetOrientation !== 'undefined') {
18197
18660
  vector = targetOrientation.multiplyVector3D(planeVector);
@@ -18200,10 +18663,10 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18200
18663
  };
18201
18664
 
18202
18665
  /**
18203
- * Get the image oriented values for an input vector.
18666
+ * Orient an input vector from target to image space.
18204
18667
  *
18205
18668
  * @param {dwv.math.Vector3D} planeVector The input vector.
18206
- * @returns {dwv.math.Vector3D} The de-orienteded vector.
18669
+ * @returns {dwv.math.Vector3D} The orienteded vector.
18207
18670
  */
18208
18671
  this.getImageOrientedVector3D = function (planeVector) {
18209
18672
  var vector = planeVector;
@@ -18216,19 +18679,19 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18216
18679
  };
18217
18680
 
18218
18681
  /**
18219
- * Reorder values to follow view orientation.
18682
+ * Reorder values to follow target orientation.
18220
18683
  *
18221
18684
  * @param {object} values Values as {x,y,z}.
18222
18685
  * @returns {object} Reoriented values as {x,y,z}.
18223
18686
  */
18224
- this.getOrientedXYZ = function (values) {
18687
+ this.getTargetOrientedXYZ = function (values) {
18225
18688
  var orientedValues = dwv.math.getOrientedArray3D(
18226
18689
  [
18227
18690
  values.x,
18228
18691
  values.y,
18229
18692
  values.z
18230
18693
  ],
18231
- viewOrientation);
18694
+ targetOrientation);
18232
18695
  return {
18233
18696
  x: orientedValues[0],
18234
18697
  y: orientedValues[1],
@@ -18236,28 +18699,6 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18236
18699
  };
18237
18700
  };
18238
18701
 
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
18702
  /**
18262
18703
  * Get the (view) scroll dimension index.
18263
18704
  *
@@ -18633,9 +19074,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
18633
19074
  * Check that an index is within bounds.
18634
19075
  *
18635
19076
  * @param {dwv.math.Index} index The index to check.
19077
+ * @param {number} dir Optional direction to check.
18636
19078
  * @returns {boolean} True if the given coordinates are within bounds.
18637
19079
  */
18638
- dwv.image.Size.prototype.isInBounds = function (index) {
19080
+ dwv.image.Size.prototype.isInBounds = function (index, dir) {
18639
19081
  // check input
18640
19082
  if (!index) {
18641
19083
  return false;
@@ -18645,11 +19087,20 @@ dwv.image.Size.prototype.isInBounds = function (index) {
18645
19087
  if (length !== index.length()) {
18646
19088
  return false;
18647
19089
  }
18648
- // check values
18649
- for (var i = 0; i < length; ++i) {
18650
- if (index.get(i) < 0 || index.get(i) > this.get(i) - 1) {
19090
+ // check values is 0 <= v < size
19091
+ var inBound = function (value, size) {
19092
+ return value >= 0 && value < size;
19093
+ };
19094
+ if (typeof dir !== 'undefined') {
19095
+ if (!inBound(index.get(dir), this.get(dir))) {
18651
19096
  return false;
18652
19097
  }
19098
+ } else {
19099
+ for (var i = 0; i < length; ++i) {
19100
+ if (!inBound(index.get(i), this.get(i))) {
19101
+ return false;
19102
+ }
19103
+ }
18653
19104
  }
18654
19105
  // seems ok!
18655
19106
  return true;
@@ -18900,11 +19351,12 @@ dwv.image.View = function (image) {
18900
19351
  var colourMap = dwv.image.lut.plain;
18901
19352
  /**
18902
19353
  * Current position as a Point3D.
19354
+ * Store position and not index to stay geometry independent.
18903
19355
  *
18904
19356
  * @private
18905
19357
  * @type {dwv.math.Index}
18906
19358
  */
18907
- var currentIndex = null;
19359
+ var currentPosition = null;
18908
19360
  /**
18909
19361
  * View orientation. Undefined will use the original slice ordering.
18910
19362
  *
@@ -19042,6 +19494,7 @@ dwv.image.View = function (image) {
19042
19494
  if (!this.getCurrentIndex()) {
19043
19495
  this.setInitialIndex();
19044
19496
  }
19497
+ var currentIndex = this.getCurrentIndex();
19045
19498
  // use current rsi if not provided
19046
19499
  if (typeof rsi === 'undefined') {
19047
19500
  rsi = image.getRescaleSlopeAndIntercept(currentIndex);
@@ -19224,8 +19677,7 @@ dwv.image.View = function (image) {
19224
19677
  * @returns {dwv.math.Point} The current position.
19225
19678
  */
19226
19679
  this.getCurrentPosition = function () {
19227
- var geometry = this.getImage().getGeometry();
19228
- return geometry.indexToWorld(currentIndex);
19680
+ return currentPosition;
19229
19681
  };
19230
19682
 
19231
19683
  /**
@@ -19234,15 +19686,32 @@ dwv.image.View = function (image) {
19234
19686
  * @returns {dwv.math.Index} The current index.
19235
19687
  */
19236
19688
  this.getCurrentIndex = function () {
19237
- return currentIndex;
19689
+ var position = this.getCurrentPosition();
19690
+ if (!position) {
19691
+ return null;
19692
+ }
19693
+ var geometry = this.getImage().getGeometry();
19694
+ return geometry.worldToIndex(position);
19238
19695
  };
19239
19696
 
19697
+ /**
19698
+ * Check is the provided position can be set.
19699
+ *
19700
+ * @param {dwv.math.Point} position The position.
19701
+ * @returns {boolean} True is the position is in bounds.
19702
+ */
19240
19703
  this.canSetPosition = function (position) {
19241
19704
  var geometry = image.getGeometry();
19242
19705
  var index = geometry.worldToIndex(position);
19243
- return geometry.isIndexInBounds(index);
19706
+ return geometry.isIndexInBounds(index, this.getScrollIndex());
19244
19707
  };
19245
19708
 
19709
+ /**
19710
+ * Get the origin at a given position.
19711
+ *
19712
+ * @param {dwv.math.Point} position The position.
19713
+ * @returns {dwv.math.Point} The origin.
19714
+ */
19246
19715
  this.getOrigin = function (position) {
19247
19716
  var geometry = image.getGeometry();
19248
19717
  var originIndex = 0;
@@ -19266,7 +19735,7 @@ dwv.image.View = function (image) {
19266
19735
  // send invalid event if not in bounds
19267
19736
  var geometry = image.getGeometry();
19268
19737
  var index = geometry.worldToIndex(position);
19269
- if (!geometry.isIndexInBounds(index)) {
19738
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19270
19739
  if (!silent) {
19271
19740
  // fire event with valid: false
19272
19741
  fireEvent({
@@ -19288,7 +19757,7 @@ dwv.image.View = function (image) {
19288
19757
  *
19289
19758
  * @param {dwv.math.Index} index The new index.
19290
19759
  * @param {boolean} silent Flag to fire event or not.
19291
- * @returns {boolean} False if not in bounds
19760
+ * @returns {boolean} False if not in bounds.
19292
19761
  * @fires dwv.image.View#positionchange
19293
19762
  */
19294
19763
  this.setCurrentIndex = function (index, silent) {
@@ -19301,73 +19770,72 @@ dwv.image.View = function (image) {
19301
19770
  var position = geometry.indexToWorld(index);
19302
19771
 
19303
19772
  // check if possible
19304
- if (!geometry.isIndexInBounds(index)) {
19773
+ if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
19305
19774
  // do no send invalid positionchange event: avoid empty repaint
19306
19775
  return false;
19307
19776
  }
19308
19777
 
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
- }
19778
+ // calculate diff dims before updating internal
19779
+ var diffDims = null;
19780
+ var currentIndex = null;
19781
+ if (this.getCurrentPosition()) {
19782
+ currentIndex = this.getCurrentIndex();
19783
+ }
19784
+ if (currentIndex) {
19785
+ if (currentIndex.canCompare(index)) {
19786
+ diffDims = currentIndex.compare(index);
19329
19787
  } else {
19330
19788
  diffDims = [];
19331
- for (var k = 0; k < index.length(); ++k) {
19332
- diffDims.push(k);
19789
+ var minLen = Math.min(currentIndex.length(), index.length());
19790
+ for (var i = 0; i < minLen; ++i) {
19791
+ if (currentIndex.get(i) !== index.get(i)) {
19792
+ diffDims.push(i);
19793
+ }
19794
+ }
19795
+ var maxLen = Math.max(currentIndex.length(), index.length());
19796
+ for (var j = minLen; j < maxLen; ++j) {
19797
+ diffDims.push(j);
19333
19798
  }
19334
19799
  }
19800
+ } else {
19801
+ diffDims = [];
19802
+ for (var k = 0; k < index.length(); ++k) {
19803
+ diffDims.push(k);
19804
+ }
19805
+ }
19335
19806
 
19336
- // assign
19337
- currentIndex = index;
19338
-
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
- };
19807
+ // assign
19808
+ currentPosition = position;
19359
19809
 
19360
- // add value if possible
19361
- if (image.canQuantify()) {
19362
- var pixValue = image.getRescaledValueAtIndex(index);
19363
- posEvent.value.push(pixValue);
19810
+ if (!silent) {
19811
+ /**
19812
+ * Position change event.
19813
+ *
19814
+ * @event dwv.image.View#positionchange
19815
+ * @type {object}
19816
+ * @property {Array} value The changed value as [index, pixelValue].
19817
+ * @property {Array} diffDims An array of modified indices.
19818
+ */
19819
+ var posEvent = {
19820
+ type: 'positionchange',
19821
+ value: [
19822
+ index.getValues(),
19823
+ position.getValues(),
19824
+ ],
19825
+ diffDims: diffDims,
19826
+ data: {
19827
+ imageUid: image.getImageUid(index)
19364
19828
  }
19829
+ };
19365
19830
 
19366
- // fire
19367
- fireEvent(posEvent);
19831
+ // add value if possible
19832
+ if (image.canQuantify()) {
19833
+ var pixValue = image.getRescaledValueAtIndex(index);
19834
+ posEvent.value.push(pixValue);
19368
19835
  }
19369
- } else {
19370
- return false;
19836
+
19837
+ // fire
19838
+ fireEvent(posEvent);
19371
19839
  }
19372
19840
 
19373
19841
  // all good
@@ -19533,7 +20001,7 @@ dwv.image.View.prototype.getWindowLevelMinMax = function () {
19533
20001
  var width = max - min;
19534
20002
  // full black / white images, defaults to 1.
19535
20003
  if (width < 1) {
19536
- dwv.logger.warn('Zero or negative width, defaulting to one.');
20004
+ dwv.logger.warn('Zero or negative window width, defaulting to one.');
19537
20005
  width = 1;
19538
20006
  }
19539
20007
  var center = min + width / 2;
@@ -23870,6 +24338,60 @@ dwv.math.Ellipse.prototype.quantify = function (viewController, flags) {
23870
24338
  return quant;
23871
24339
  };
23872
24340
 
24341
+ /**
24342
+ * Get the indices that form a ellpise.
24343
+ *
24344
+ * @param {dwv.math.Index} center The ellipse center.
24345
+ * @param {Array} radius The 2 ellipse radiuses.
24346
+ * @param {Array} dir The 2 ellipse directions.
24347
+ * @returns {Array} The indices of the ellipse.
24348
+ */
24349
+ dwv.math.getEllipseIndices = function (center, radius, dir) {
24350
+ var centerValues = center.getValues();
24351
+ // keep all values for possible extra dimensions
24352
+ var values = centerValues.slice();
24353
+ var indices = [];
24354
+ var radiusI = radius[0];
24355
+ var radiusJ = radius[1];
24356
+ var radiusRatio = radiusI / radiusJ;
24357
+ var radiusJ2 = Math.pow(radiusJ, 2);
24358
+ var di = dir[0];
24359
+ var dj = dir[1];
24360
+ // deduce 4 positions from top right
24361
+ for (var j = 0; j < radiusJ; ++j) {
24362
+ // right triangle formed by radiuses, j and len
24363
+ // ellipse: i*i / a*a + j*j / b*b = 1
24364
+ // -> i = a/b * sqrt(b*b - j*j)
24365
+ var len = Math.round(
24366
+ radiusRatio * Math.sqrt(radiusJ2 - Math.pow(j, 2)));
24367
+ var jmax = centerValues[dj] + j;
24368
+ var jmin = centerValues[dj] - j;
24369
+ for (var i = 0; i < len; ++i) {
24370
+ var imax = centerValues[di] + i;
24371
+ var imin = centerValues[di] - i;
24372
+
24373
+ // right
24374
+ values[di] = imax;
24375
+ // right - top
24376
+ values[dj] = jmax;
24377
+ indices.push(new dwv.math.Index(values.slice()));
24378
+ // right - bottom
24379
+ values[dj] = jmin;
24380
+ indices.push(new dwv.math.Index(values.slice()));
24381
+
24382
+ // left
24383
+ values[di] = imin;
24384
+ // left - top
24385
+ values[dj] = jmax;
24386
+ indices.push(new dwv.math.Index(values.slice()));
24387
+ // left - bottom
24388
+ values[dj] = jmin;
24389
+ indices.push(new dwv.math.Index(values.slice()));
24390
+ }
24391
+ }
24392
+ return indices;
24393
+ };
24394
+
23873
24395
  // namespaces
23874
24396
  var dwv = dwv || {};
23875
24397
  dwv.math = dwv.math || {};
@@ -24043,6 +24565,21 @@ dwv.math.getZeroIndex = function (size) {
24043
24565
  return new dwv.math.Index(values);
24044
24566
  };
24045
24567
 
24568
+ /**
24569
+ * Get an array sort callback.
24570
+ * f(a,b) > 0 -> b,a
24571
+ * f(a,b) < 0 -> a,b
24572
+ * f(a,b) = 0 -> original order
24573
+ *
24574
+ * @param {number} direction The direction to use to compare indices.
24575
+ * @returns {Function} A function that compares two dwv.math.Index.
24576
+ */
24577
+ dwv.math.getIndexCompareFunction = function (direction) {
24578
+ return function (a, b) {
24579
+ return a.get(direction) - b.get(direction);
24580
+ };
24581
+ };
24582
+
24046
24583
  /**
24047
24584
  * Get a string id from the index values in the form of: '#0-1_#1-2'.
24048
24585
  *
@@ -24372,6 +24909,9 @@ dwv.math.isSimilar = function (a, b, tol) {
24372
24909
  * @class
24373
24910
  */
24374
24911
  dwv.math.Matrix33 = function (values) {
24912
+ // matrix inverse, calculated at first ask
24913
+ var inverse = null;
24914
+
24375
24915
  /**
24376
24916
  * Get a value of the matrix.
24377
24917
  *
@@ -24382,6 +24922,20 @@ dwv.math.Matrix33 = function (values) {
24382
24922
  this.get = function (row, col) {
24383
24923
  return values[row * 3 + col];
24384
24924
  };
24925
+
24926
+ /**
24927
+ * Get the inverse of this matrix.
24928
+ *
24929
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24930
+ * if the determinant is zero.
24931
+ */
24932
+ this.getInverse = function () {
24933
+ if (inverse === null) {
24934
+ inverse = dwv.math.getMatrixInverse(this);
24935
+ }
24936
+ return inverse;
24937
+ };
24938
+
24385
24939
  }; // Matrix33
24386
24940
 
24387
24941
  /**
@@ -24393,15 +24947,16 @@ dwv.math.Matrix33 = function (values) {
24393
24947
  * @returns {boolean} True if both matrices are equal.
24394
24948
  */
24395
24949
  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);
24950
+ // TODO: add type check
24951
+ // check values
24952
+ for (var i = 0; i < 3; ++i) {
24953
+ for (var j = 0; j < 3; ++j) {
24954
+ if (!dwv.math.isSimilar(this.get(i, j), rhs.get(i, j), p)) {
24955
+ return false;
24956
+ }
24957
+ }
24958
+ }
24959
+ return true;
24405
24960
  };
24406
24961
 
24407
24962
  /**
@@ -24410,10 +24965,20 @@ dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24410
24965
  * @returns {string} The matrix as a string.
24411
24966
  */
24412
24967
  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
- ']';
24968
+ var str = '[';
24969
+ for (var i = 0; i < 3; ++i) {
24970
+ if (i !== 0) {
24971
+ str += ', \n ';
24972
+ }
24973
+ for (var j = 0; j < 3; ++j) {
24974
+ if (j !== 0) {
24975
+ str += ', ';
24976
+ }
24977
+ str += this.get(i, j);
24978
+ }
24979
+ }
24980
+ str += ']';
24981
+ return str;
24417
24982
  };
24418
24983
 
24419
24984
  /**
@@ -24511,42 +25076,46 @@ dwv.math.Matrix33.prototype.multiplyIndex3D = function (index3D) {
24511
25076
  };
24512
25077
 
24513
25078
  /**
24514
- * Get the inverse of this matrix.
25079
+ * Get the inverse of an input 3*3 matrix.
24515
25080
  *
24516
- * @returns {dwv.math.Matrix33} The inverse matrix.
25081
+ * @param {dwv.math.Matrix33} m The input matrix.
25082
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
25083
+ * if the determinant is zero.
24517
25084
  * @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;
25085
+ * @see https://github.com/willnode/N-Matrix-Programmer
25086
+ */
25087
+ dwv.math.getMatrixInverse = function (m) {
25088
+ var m00 = m.get(0, 0);
25089
+ var m01 = m.get(0, 1);
25090
+ var m02 = m.get(0, 2);
25091
+ var m10 = m.get(1, 0);
25092
+ var m11 = m.get(1, 1);
25093
+ var m12 = m.get(1, 2);
25094
+ var m20 = m.get(2, 0);
25095
+ var m21 = m.get(2, 1);
25096
+ var m22 = m.get(2, 2);
25097
+
25098
+ var a1212 = m11 * m22 - m12 * m21;
25099
+ var a2012 = m12 * m20 - m10 * m22;
25100
+ var a0112 = m10 * m21 - m11 * m20;
25101
+
25102
+ var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
24535
25103
  if (det === 0) {
24536
- dwv.logger.warn('Cannot invert matrix with zero determinant.');
25104
+ dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
24537
25105
  return;
24538
25106
  }
25107
+ det = 1 / det;
24539
25108
 
24540
25109
  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
25110
+ det * a1212,
25111
+ det * (m02 * m21 - m01 * m22),
25112
+ det * (m01 * m12 - m02 * m11),
25113
+ det * a2012,
25114
+ det * (m00 * m22 - m02 * m20),
25115
+ det * (m02 * m10 - m00 * m12),
25116
+ det * a0112,
25117
+ det * (m01 * m20 - m00 * m21),
25118
+ det * (m00 * m11 - m01 * m10)
24550
25119
  ];
24551
25120
 
24552
25121
  return new dwv.math.Matrix33(values);
@@ -24957,6 +25526,21 @@ dwv.math.Point3D.prototype.equals = function (rhs) {
24957
25526
  this.getZ() === rhs.getZ();
24958
25527
  };
24959
25528
 
25529
+ /**
25530
+ * Check for Point3D similarity.
25531
+ *
25532
+ * @param {dwv.math.Point3D} rhs The other point to compare to.
25533
+ * @param {number} tol Optional comparison tolerance,
25534
+ * default to Number.EPSILON.
25535
+ * @returns {boolean} True if both points are equal.
25536
+ */
25537
+ dwv.math.Point3D.prototype.isSimilar = function (rhs, tol) {
25538
+ return rhs !== null &&
25539
+ dwv.math.isSimilar(this.getX(), rhs.getX(), tol) &&
25540
+ dwv.math.isSimilar(this.getY(), rhs.getY(), tol) &&
25541
+ dwv.math.isSimilar(this.getZ(), rhs.getZ(), tol);
25542
+ };
25543
+
24960
25544
  /**
24961
25545
  * Get a string representation of the Point3D.
24962
25546
  *
@@ -24994,6 +25578,19 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
24994
25578
  (this.getZ() - point3D.getZ()));
24995
25579
  };
24996
25580
 
25581
+ /**
25582
+ * Get an array find callback for a given point.
25583
+ *
25584
+ * @param {dwv.math.Point3D} point The point to compare to.
25585
+ * @returns {Function} A function that compares its input point to the one
25586
+ * given as input to this function.
25587
+ */
25588
+ dwv.math.getEqualPoint3DFunction = function (point) {
25589
+ return function (element) {
25590
+ return element.equals(point);
25591
+ };
25592
+ };
25593
+
24997
25594
  /**
24998
25595
  * Immutable point.
24999
25596
  * Warning: the input array is NOT cloned, modifying it will
@@ -27296,11 +27893,15 @@ dwv.tool.Draw = function (app) {
27296
27893
  };
27297
27894
 
27298
27895
  /**
27299
- * Handle double click event.
27896
+ * Handle double click event: some tools use it to finish interaction.
27300
27897
  *
27301
- * @param {object} event The mouse up event.
27898
+ * @param {object} event The double click event.
27302
27899
  */
27303
27900
  this.dblclick = function (event) {
27901
+ // only end by double click undefined NPoints
27902
+ if (typeof currentFactory.getNPoints() !== 'undefined') {
27903
+ return;
27904
+ }
27304
27905
  // exit if not started draw
27305
27906
  if (!started) {
27306
27907
  return;
@@ -30242,10 +30843,11 @@ dwv.tool.draw.FreeHandFactory = function () {
30242
30843
  /**
30243
30844
  * Get the number of points needed to build the shape.
30244
30845
  *
30245
- * @returns {number} The number of points.
30846
+ * @returns {number|undefined} The number of points.
30246
30847
  */
30247
30848
  this.getNPoints = function () {
30248
- return 1000;
30849
+ // undefined to end with double click
30850
+ return undefined;
30249
30851
  };
30250
30852
  /**
30251
30853
  * Get the timeout between point storage.
@@ -31673,10 +32275,11 @@ dwv.tool.draw.RoiFactory = function () {
31673
32275
  /**
31674
32276
  * Get the number of points needed to build the shape.
31675
32277
  *
31676
- * @returns {number} The number of points.
32278
+ * @returns {number|undefined} The number of points.
31677
32279
  */
31678
32280
  this.getNPoints = function () {
31679
- return 50;
32281
+ // undefined to end with double click
32282
+ return undefined;
31680
32283
  };
31681
32284
  /**
31682
32285
  * Get the timeout between point storage.
@@ -32593,11 +33196,16 @@ dwv.tool.WindowLevel = function (app) {
32593
33196
  // difference to last position
32594
33197
  var diffX = event._x - self.x0;
32595
33198
  var diffY = self.y0 - event._y;
33199
+ // data range
33200
+ var range = viewController.getImageRescaledDataRange();
33201
+ // 1/1000 seems to give reasonable results...
33202
+ var pixelToIntensity = (range.max - range.min) * 0.01;
33203
+
32596
33204
  // calculate new window level
32597
- var windowCenter =
32598
- parseInt(viewController.getWindowLevel().center, 10) + diffY;
32599
- var windowWidth =
32600
- parseInt(viewController.getWindowLevel().width, 10) + diffX;
33205
+ var center = parseInt(viewController.getWindowLevel().center, 10);
33206
+ var width = parseInt(viewController.getWindowLevel().width, 10);
33207
+ var windowCenter = center + Math.round(diffY * pixelToIntensity);
33208
+ var windowWidth = width + Math.round(diffX * pixelToIntensity);
32601
33209
  // bound window width
32602
33210
  windowWidth = dwv.image.validateWindowWidth(windowWidth);
32603
33211