dwv 0.31.0-beta.6 → 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.6 2022-06-23 18:02:33 */
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
  };
@@ -589,9 +644,40 @@ dwv.App = function () {
589
644
  stage.empty();
590
645
  // set new
591
646
  options.dataViewConfigs = configs;
592
- // re-bind layers
593
- stage.bindLayerGroups();
594
- };
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
+ }
595
681
 
596
682
  /**
597
683
  * Set the layer groups binders.
@@ -621,23 +707,14 @@ dwv.App = function () {
621
707
  }
622
708
  for (var i = 0; i < viewConfigs.length; ++i) {
623
709
  var config = viewConfigs[i];
624
- // create layer group if not done yet
625
- // warn: needs a loaded DOM
626
710
  var layerGroup =
627
711
  stage.getLayerGroupWithElementId(config.divId);
712
+ // layer group must exist
628
713
  if (!layerGroup) {
629
- // create new layer group
630
- var element = document.getElementById(config.divId);
631
- layerGroup = stage.addLayerGroup(element);
632
- // bind events
633
- bindLayerGroup(layerGroup);
634
- // optional orientation
635
- if (typeof config.orientation !== 'undefined') {
636
- layerGroup.setTargetOrientation(
637
- dwv.math.getMatrixFromName(config.orientation));
638
- }
714
+ throw new Error('No layer group for ' + config.divId);
639
715
  }
640
716
  // initialise or add view
717
+ // warn: needs a loaded DOM
641
718
  if (layerGroup.getViewLayersByDataIndex(dataIndex).length === 0) {
642
719
  if (layerGroup.getNumberOfLayers() === 0) {
643
720
  initialiseBaseLayers(dataIndex, config);
@@ -1209,7 +1286,7 @@ dwv.App = function () {
1209
1286
  * @param {object} group The layer group.
1210
1287
  * @private
1211
1288
  */
1212
- function bindLayerGroup(group) {
1289
+ function bindLayerGroupToApp(group) {
1213
1290
  // propagate layer group events
1214
1291
  group.addEventListener('zoomchange', fireEvent);
1215
1292
  group.addEventListener('offsetchange', fireEvent);
@@ -1240,37 +1317,6 @@ dwv.App = function () {
1240
1317
  }
1241
1318
  }
1242
1319
 
1243
- /**
1244
- * Get the data max size for a layer group.
1245
- *
1246
- * @param {object} layerGroup The layer group.
1247
- * @returns {object} The max size as {x,y}.
1248
- */
1249
- function getLayerGroupMaxSize(layerGroup) {
1250
- var maxSize = {x: 0, y: 0};
1251
- // loop through layer group data
1252
- var indices = layerGroup.getViewDataIndices();
1253
- for (var i = 0; i < indices.length; ++i) {
1254
- var data = dataController.get(indices[i]);
1255
- var geometry = data.image.getGeometry();
1256
- var viewOrient = dwv.gui.getViewOrientation(
1257
- geometry.getOrientation(),
1258
- layerGroup.getTargetOrientation()
1259
- );
1260
- var size = geometry.getSize(viewOrient).get2D();
1261
- var spacing = geometry.getSpacing(viewOrient).get2D();
1262
- var width = size.x * spacing.x;
1263
- if (width > maxSize.x) {
1264
- maxSize.x = width;
1265
- }
1266
- var height = size.y * spacing.y;
1267
- if (height > maxSize.y) {
1268
- maxSize.y = height;
1269
- }
1270
- }
1271
- return maxSize;
1272
- }
1273
-
1274
1320
  /**
1275
1321
  * Add a view layer.
1276
1322
  *
@@ -1367,9 +1413,8 @@ dwv.App = function () {
1367
1413
  });
1368
1414
  }
1369
1415
 
1370
- // fit to the maximum size
1371
- var maxSize = getLayerGroupMaxSize(layerGroup);
1372
- layerGroup.fitToContainer(maxSize);
1416
+ // sync layer groups
1417
+ stage.syncLayerGroupScale();
1373
1418
  }
1374
1419
 
1375
1420
  };
@@ -2906,6 +2951,21 @@ dwv.ctrl.ViewController = function (view) {
2906
2951
  return view.getImage().getGeometry().getSize(view.getOrientation());
2907
2952
  };
2908
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
+
2909
2969
  /**
2910
2970
  * Get the image rescaled data range.
2911
2971
  *
@@ -2991,6 +3051,25 @@ dwv.ctrl.ViewController = function (view) {
2991
3051
  return view.setCurrentIndex(index, silent);
2992
3052
  };
2993
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
+
2994
3073
  /**
2995
3074
  * Get a plane 3D position from a plane 2D position: does not compensate
2996
3075
  * for the image origin. Needed for setting the scale center...
@@ -4550,7 +4629,7 @@ dwv.dicom = dwv.dicom || {};
4550
4629
  * @returns {string} The version of the library.
4551
4630
  */
4552
4631
  dwv.getVersion = function () {
4553
- return '0.31.0-beta.6';
4632
+ return '0.31.0-beta.7';
4554
4633
  };
4555
4634
 
4556
4635
  /**
@@ -11344,6 +11423,22 @@ dwv.gui.DrawLayer = function (containerDiv) {
11344
11423
  */
11345
11424
  var baseOffset = {x: 0, y: 0};
11346
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
+
11347
11442
  /**
11348
11443
  * The draw controller.
11349
11444
  *
@@ -11463,16 +11558,59 @@ dwv.gui.DrawLayer = function (containerDiv) {
11463
11558
  * Set the layer scale.
11464
11559
  *
11465
11560
  * @param {object} newScale The scale as {x,y}.
11561
+ * @param {dwv.math.Point3D} center The scale center.
11466
11562
  */
11467
- this.setScale = function (newScale) {
11468
- var orientedNewScale = planeHelper.getOrientedXYZ(newScale);
11469
- var fullScale = {
11563
+ this.setScale = function (newScale, center) {
11564
+ var orientedNewScale = planeHelper.getTargetOrientedXYZ(newScale);
11565
+ var finalNewScale = {
11470
11566
  x: fitScale.x * orientedNewScale.x,
11471
11567
  y: fitScale.y * orientedNewScale.y
11472
11568
  };
11473
- konvaStage.scale(fullScale);
11474
- // update labelss
11475
- 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);
11476
11614
  };
11477
11615
 
11478
11616
  /**
@@ -11483,8 +11621,8 @@ dwv.gui.DrawLayer = function (containerDiv) {
11483
11621
  this.setOffset = function (newOffset) {
11484
11622
  var planeNewOffset = planeHelper.getPlaneOffsetFromOffset3D(newOffset);
11485
11623
  konvaStage.offset({
11486
- x: baseOffset.x + planeNewOffset.x,
11487
- 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
11488
11626
  });
11489
11627
  };
11490
11628
 
@@ -11582,18 +11720,38 @@ dwv.gui.DrawLayer = function (containerDiv) {
11582
11720
  *
11583
11721
  * @param {number} fitScale1D The 1D fit scale.
11584
11722
  * @param {object} fitSize The fit size as {x,y}.
11723
+ * @param {object} fitOffset The fit offset as {x,y}.
11585
11724
  */
11586
- 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
+ };
11587
11735
  // update fit scale
11588
11736
  fitScale = {
11589
11737
  x: fitScale1D * baseSpacing.x,
11590
11738
  y: fitScale1D * baseSpacing.y
11591
11739
  };
11592
- // update konva
11593
- konvaStage.setWidth(fitSize.x);
11594
- konvaStage.setHeight(fitSize.y);
11595
- // reset scale
11596
- 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
+ });
11597
11755
  };
11598
11756
 
11599
11757
  /**
@@ -11962,6 +12120,36 @@ dwv.gui.getTargetOrientation = function (imageOrientation, viewOrientation) {
11962
12120
  return imageOrientation.asOneAndZeros().multiply(viewOrientation);
11963
12121
  };
11964
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
+
11965
12153
  /**
11966
12154
  * Layer group.
11967
12155
  *
@@ -12433,32 +12621,81 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12433
12621
  };
12434
12622
 
12435
12623
  /**
12436
- * Fit the display to the size of the container.
12437
- * To be called once the image is loaded.
12624
+ * Calculate the fit scale: the scale that fits the largest data.
12438
12625
  *
12439
- * @param {object} realSize 2D real size (in mm) to fit provided as {x,y}.
12626
+ * @returns {number|undefined} The fit scale.
12440
12627
  */
12441
- this.fitToContainer = function (realSize) {
12442
- // check container size
12628
+ this.calculateFitScale = function () {
12629
+ // check container
12443
12630
  if (containerDiv.offsetWidth === 0 &&
12444
12631
  containerDiv.offsetHeight === 0) {
12445
12632
  throw new Error('Cannot fit to zero sized container.');
12446
12633
  }
12447
- // find best fit
12448
- var fitScale = Math.min(
12449
- containerDiv.offsetWidth / realSize.x,
12450
- 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
12451
12643
  );
12452
- var fitSize = {
12453
- x: Math.floor(realSize.x * fitScale),
12454
- 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))
12455
12667
  };
12668
+
12456
12669
  // apply to layers
12457
12670
  for (var j = 0; j < layers.length; ++j) {
12458
- layers[j].fitToContainer(fitScale, fitSize);
12671
+ layers[j].fitToContainer(scaleIn, containerSize, fitOffset);
12459
12672
  }
12460
12673
  };
12461
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
+
12462
12699
  /**
12463
12700
  * Add scale to the layers. Scale cannot go lower than 0.1.
12464
12701
  *
@@ -12471,35 +12708,33 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12471
12708
  y: scale.y * (1 + scaleStep),
12472
12709
  z: scale.z * (1 + scaleStep)
12473
12710
  };
12474
- var centerPlane = {
12475
- x: (center.getX() - offset.x) * scale.x,
12476
- y: (center.getY() - offset.y) * scale.y,
12477
- z: (center.getZ() - offset.z) * scale.z
12478
- };
12479
- // center should stay the same:
12480
- // center / newScale + newOffset = center / oldScale + oldOffset
12481
- // => newOffset = center / oldScale + oldOffset - center / newScale
12482
- var newOffset = {
12483
- x: (centerPlane.x / scale.x) + offset.x - (centerPlane.x / newScale.x),
12484
- y: (centerPlane.y / scale.y) + offset.y - (centerPlane.y / newScale.y),
12485
- z: (centerPlane.z / scale.z) + offset.z - (centerPlane.z / newScale.z)
12486
- };
12487
-
12488
- this.setOffset(newOffset);
12489
- this.setScale(newScale);
12711
+ this.setScale(newScale, center);
12490
12712
  };
12491
12713
 
12492
12714
  /**
12493
12715
  * Set the layers' scale.
12494
12716
  *
12495
12717
  * @param {object} newScale The scale to apply as {x,y,z}.
12718
+ * @param {dwv.math.Point3D} center The scale center Point3D.
12496
12719
  * @fires dwv.ctrl.LayerGroup#zoomchange
12497
12720
  */
12498
- this.setScale = function (newScale) {
12721
+ this.setScale = function (newScale, center) {
12499
12722
  scale = newScale;
12500
12723
  // apply to layers
12501
12724
  for (var i = 0; i < layers.length; ++i) {
12502
- 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());
12503
12738
  }
12504
12739
 
12505
12740
  /**
@@ -12511,7 +12746,7 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12511
12746
  */
12512
12747
  fireEvent({
12513
12748
  type: 'zoomchange',
12514
- value: [scale.x, scale.y, scale.z],
12749
+ value: value
12515
12750
  });
12516
12751
  };
12517
12752
 
@@ -12664,11 +12899,20 @@ dwv.gui.ZoomBinder = function () {
12664
12899
  };
12665
12900
  this.getCallback = function (layerGroup) {
12666
12901
  return function (event) {
12667
- layerGroup.setScale({
12902
+ var scale = {
12668
12903
  x: event.value[0],
12669
12904
  y: event.value[1],
12670
12905
  z: event.value[2]
12671
- });
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);
12672
12916
  layerGroup.draw();
12673
12917
  };
12674
12918
  };
@@ -12856,6 +13100,33 @@ dwv.gui.Stage = function () {
12856
13100
  }
12857
13101
  };
12858
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
+
12859
13130
  /**
12860
13131
  * Bind the layer groups of the stage.
12861
13132
  */
@@ -13357,6 +13628,22 @@ dwv.gui.ViewLayer = function (containerDiv) {
13357
13628
  */
13358
13629
  var baseOffset = {x: 0, y: 0};
13359
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
+
13360
13647
  /**
13361
13648
  * Data update flag.
13362
13649
  *
@@ -13461,6 +13748,15 @@ dwv.gui.ViewLayer = function (containerDiv) {
13461
13748
  return baseSize;
13462
13749
  };
13463
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
+
13464
13760
  /**
13465
13761
  * Get the layer opacity.
13466
13762
  *
@@ -13500,14 +13796,57 @@ dwv.gui.ViewLayer = function (containerDiv) {
13500
13796
  * Set the layer scale.
13501
13797
  *
13502
13798
  * @param {object} newScale The scale as {x,y}.
13799
+ * @param {dwv.math.Point3D} center The scale center.
13503
13800
  */
13504
- this.setScale = function (newScale) {
13801
+ this.setScale = function (newScale, center) {
13505
13802
  var helper = viewController.getPlaneHelper();
13506
- var orientedNewScale = helper.getOrientedXYZ(newScale);
13507
- scale = {
13803
+ var orientedNewScale = helper.getTargetOrientedXYZ(newScale);
13804
+ var finalNewScale = {
13508
13805
  x: fitScale.x * orientedNewScale.x,
13509
13806
  y: fitScale.y * orientedNewScale.y
13510
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;
13511
13850
  };
13512
13851
 
13513
13852
  /**
@@ -13547,8 +13886,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13547
13886
  var helper = viewController.getPlaneHelper();
13548
13887
  var planeNewOffset = helper.getPlaneOffsetFromOffset3D(newOffset);
13549
13888
  offset = {
13550
- x: baseOffset.x + planeNewOffset.x,
13551
- 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
13552
13891
  };
13553
13892
  };
13554
13893
 
@@ -13770,8 +14109,18 @@ dwv.gui.ViewLayer = function (containerDiv) {
13770
14109
  *
13771
14110
  * @param {number} fitScale1D The 1D fit scale.
13772
14111
  * @param {object} fitSize The fit size as {x,y}.
14112
+ * @param {object} fitOffset The fit offset as {x,y}.
13773
14113
  */
13774
- this.fitToContainer = function (fitScale1D, fitSize) {
14114
+ this.fitToContainer = function (fitScale1D, fitSize, fitOffset) {
14115
+ // update canvas size
14116
+ var width = fitSize.x;
14117
+ var height = fitSize.y;
14118
+ if (!dwv.gui.canCreateCanvas(width, height)) {
14119
+ throw new Error('Cannot resize canvas ' + width + ', ' + height);
14120
+ }
14121
+ canvas.width = width;
14122
+ canvas.height = height;
14123
+
13775
14124
  // previous scale without fit
13776
14125
  var previousScale = {
13777
14126
  x: scale.x / fitScale.x,
@@ -13782,19 +14131,21 @@ dwv.gui.ViewLayer = function (containerDiv) {
13782
14131
  x: fitScale1D * baseSpacing.x,
13783
14132
  y: fitScale1D * baseSpacing.y
13784
14133
  };
13785
- // new canvas size
13786
- var width = fitSize.x;
13787
- var height = fitSize.y;
13788
- if (!dwv.gui.canCreateCanvas(width, height)) {
13789
- throw new Error('Cannot resize canvas ' + width + ', ' + height);
13790
- }
13791
- canvas.width = width;
13792
- canvas.height = height;
13793
- // reset previous scale with new fit
14134
+ // update scale
13794
14135
  scale = {
13795
14136
  x: previousScale.x * fitScale.x,
13796
14137
  y: previousScale.y * fitScale.y
13797
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
+ };
13798
14149
  };
13799
14150
 
13800
14151
  /**
@@ -18328,19 +18679,19 @@ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18328
18679
  };
18329
18680
 
18330
18681
  /**
18331
- * Reorder values to follow view orientation.
18682
+ * Reorder values to follow target orientation.
18332
18683
  *
18333
18684
  * @param {object} values Values as {x,y,z}.
18334
18685
  * @returns {object} Reoriented values as {x,y,z}.
18335
18686
  */
18336
- this.getOrientedXYZ = function (values) {
18687
+ this.getTargetOrientedXYZ = function (values) {
18337
18688
  var orientedValues = dwv.math.getOrientedArray3D(
18338
18689
  [
18339
18690
  values.x,
18340
18691
  values.y,
18341
18692
  values.z
18342
18693
  ],
18343
- viewOrientation);
18694
+ targetOrientation);
18344
18695
  return {
18345
18696
  x: orientedValues[0],
18346
18697
  y: orientedValues[1],