dwv 0.31.0-beta.17 → 0.31.0-beta.18

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.17 2022-11-17 16:14:43 */
1
+ /*! dwv 0.31.0-beta.18 2022-11-30 11:36:21 */
2
2
  // Inspired from umdjs
3
3
  // See https://github.com/umdjs/umd/blob/master/templates/returnExports.js
4
4
  (function (root, factory) {
@@ -374,7 +374,8 @@ dwv.App = function () {
374
374
  * string (default undefined keeps the original slice order)
375
375
  * - `binders`: array of layerGroup binders
376
376
  * - `tools`: tool name indexed object containing individual tool
377
- * configurations
377
+ * configurations in the form of a list of objects containing:
378
+ * - options: array of tool options
378
379
  * - `viewOnFirstLoadItem`: boolean flag to trigger the first data render
379
380
  * after the first loaded data or not
380
381
  * - `defaultCharacterSet`: the default chraracter set string used for DICOM
@@ -433,18 +434,16 @@ dwv.App = function () {
433
434
  toolList[toolName] = new dwv.tool[toolName](this);
434
435
  // register listeners
435
436
  if (typeof toolList[toolName].addEventListener !== 'undefined') {
436
- if (typeof toolParams.events !== 'undefined') {
437
- for (var j = 0; j < toolParams.events.length; ++j) {
438
- var eventName = toolParams.events[j];
439
- toolList[toolName].addEventListener(eventName, fireEvent);
440
- }
437
+ var names = toolList[toolName].getEventNames();
438
+ for (var j = 0; j < names.length; ++j) {
439
+ toolList[toolName].addEventListener(names[j], fireEvent);
441
440
  }
442
441
  }
443
442
  // tool options
444
443
  if (typeof toolParams.options !== 'undefined') {
445
444
  var type = 'raw';
446
- if (typeof toolParams.type !== 'undefined') {
447
- type = toolParams.type;
445
+ if (typeof toolList[toolName].getOptionsType !== 'undefined') {
446
+ type = toolList[toolName].getOptionsType();
448
447
  }
449
448
  var toolOptions = toolParams.options;
450
449
  if (type === 'instance' ||
@@ -1026,21 +1025,10 @@ dwv.App = function () {
1026
1025
  * @param {string} tool The tool.
1027
1026
  */
1028
1027
  this.setTool = function (tool) {
1029
- // bind tool to layer: not really important which layer since
1030
- // tools are responsible for finding the event source layer
1031
- // but there needs to be at least one binding...
1028
+ // bind tool to active layer
1032
1029
  for (var i = 0; i < stage.getNumberOfLayerGroups(); ++i) {
1033
1030
  var layerGroup = stage.getLayerGroup(i);
1034
- // unbind previous layer
1035
- var vl = layerGroup.getActiveViewLayer();
1036
- if (vl) {
1037
- toolboxController.unbindLayer(vl);
1038
- }
1039
- var dl = layerGroup.getActiveDrawLayer();
1040
- if (dl) {
1041
- toolboxController.unbindLayer(dl);
1042
- }
1043
- // bind new layer
1031
+ // draw or view layer
1044
1032
  var layer = null;
1045
1033
  if (tool === 'Draw' ||
1046
1034
  tool === 'Livewire' ||
@@ -1050,7 +1038,7 @@ dwv.App = function () {
1050
1038
  layer = layerGroup.getActiveViewLayer();
1051
1039
  }
1052
1040
  if (layer) {
1053
- toolboxController.bindLayer(layer);
1041
+ toolboxController.bindLayer(layer, layerGroup.getGroupId());
1054
1042
  }
1055
1043
  }
1056
1044
 
@@ -1402,6 +1390,9 @@ dwv.App = function () {
1402
1390
 
1403
1391
  // bind
1404
1392
  stage.bindLayerGroups();
1393
+ if (toolboxController) {
1394
+ toolboxController.bindLayer(viewLayer, layerGroup.getGroupId());
1395
+ }
1405
1396
 
1406
1397
  // optional draw layer
1407
1398
  if (toolboxController && toolboxController.hasTool('Draw')) {
@@ -2412,6 +2403,14 @@ dwv.ctrl.ToolboxController = function (toolList) {
2412
2403
  */
2413
2404
  var callbackStore = [];
2414
2405
 
2406
+ /**
2407
+ * Current layers bound to tool.
2408
+ *
2409
+ * @type {object}
2410
+ * @private
2411
+ */
2412
+ var boundLayers = {};
2413
+
2415
2414
  /**
2416
2415
  * Initialise.
2417
2416
  */
@@ -2497,8 +2496,12 @@ dwv.ctrl.ToolboxController = function (toolList) {
2497
2496
  * Listen to layer interaction events.
2498
2497
  *
2499
2498
  * @param {object} layer The layer to listen to.
2499
+ * @param {number} layerGroupId The associated layer group id.
2500
2500
  */
2501
- this.bindLayer = function (layer) {
2501
+ this.bindLayer = function (layer, layerGroupId) {
2502
+ if (typeof boundLayers[layerGroupId] !== 'undefined') {
2503
+ unbindLayer(boundLayers[layerGroupId]);
2504
+ }
2502
2505
  layer.bindInteraction();
2503
2506
  // interaction events
2504
2507
  var names = dwv.gui.interactionEventNames;
@@ -2506,6 +2509,8 @@ dwv.ctrl.ToolboxController = function (toolList) {
2506
2509
  layer.addEventListener(names[i],
2507
2510
  getOnMouch(layer.getId(), names[i]));
2508
2511
  }
2512
+ // update class var
2513
+ boundLayers[layerGroupId] = layer;
2509
2514
  };
2510
2515
 
2511
2516
  /**
@@ -2513,7 +2518,7 @@ dwv.ctrl.ToolboxController = function (toolList) {
2513
2518
  *
2514
2519
  * @param {object} layer The layer to stop listening to.
2515
2520
  */
2516
- this.unbindLayer = function (layer) {
2521
+ function unbindLayer(layer) {
2517
2522
  layer.unbindInteraction();
2518
2523
  // interaction events
2519
2524
  var names = dwv.gui.interactionEventNames;
@@ -2521,7 +2526,7 @@ dwv.ctrl.ToolboxController = function (toolList) {
2521
2526
  layer.removeEventListener(names[i],
2522
2527
  getOnMouch(layer.getId(), names[i]));
2523
2528
  }
2524
- };
2529
+ }
2525
2530
 
2526
2531
  /**
2527
2532
  * Mou(se) and (T)ouch event handler. This function just determines
@@ -2634,7 +2639,7 @@ dwv.ctrl.ViewController = function (view) {
2634
2639
  // set window/level to first preset
2635
2640
  this.setWindowLevelPresetById(0);
2636
2641
  // default position
2637
- this.setCurrentPosition2D(0, 0);
2642
+ this.setCurrentPosition(this.getPositionFrom2D(0, 0));
2638
2643
  };
2639
2644
 
2640
2645
  /**
@@ -2783,6 +2788,36 @@ dwv.ctrl.ViewController = function (view) {
2783
2788
  return [spacing.get(0), spacing.get(1)];
2784
2789
  };
2785
2790
 
2791
+ /**
2792
+ * Get the image rescaled value at the input position.
2793
+ *
2794
+ * @param {dwv.math.Point} position the input position.
2795
+ * @returns {number|undefined} The image value or undefined if out of bounds
2796
+ * or no quantifiable (for ex RGB).
2797
+ */
2798
+ this.getRescaledImageValue = function (position) {
2799
+ var image = view.getImage();
2800
+ if (!image.canQuantify()) {
2801
+ return;
2802
+ }
2803
+ var geometry = image.getGeometry();
2804
+ var index = geometry.worldToIndex(position);
2805
+ var value;
2806
+ if (geometry.isIndexInBounds(index)) {
2807
+ value = image.getRescaledValueAtIndex(index);
2808
+ }
2809
+ return value;
2810
+ };
2811
+
2812
+ /**
2813
+ * Get the image pixel unit.
2814
+ *
2815
+ * @returns {string} The unit
2816
+ */
2817
+ this.getPixelUnit = function () {
2818
+ return view.getImage().getMeta().pixelUnit;
2819
+ };
2820
+
2786
2821
  /**
2787
2822
  * Get some values from the associated image in a region.
2788
2823
  *
@@ -2959,13 +2994,13 @@ dwv.ctrl.ViewController = function (view) {
2959
2994
  };
2960
2995
 
2961
2996
  /**
2962
- * Set the current 2D (x,y) position.
2997
+ * Get a position from a 2D (x,y) position.
2963
2998
  *
2964
2999
  * @param {number} x The column position.
2965
3000
  * @param {number} y The row position.
2966
- * @returns {boolean} False if not in bounds.
3001
+ * @returns {dwv.math.Point} The associated position.
2967
3002
  */
2968
- this.setCurrentPosition2D = function (x, y) {
3003
+ this.getPositionFrom2D = function (x, y) {
2969
3004
  // keep third direction
2970
3005
  var k = this.getCurrentScrollIndexValue();
2971
3006
  var planePoint = new dwv.math.Point3D(x, y, k);
@@ -2975,9 +3010,7 @@ dwv.ctrl.ViewController = function (view) {
2975
3010
  var geometry = view.getImage().getGeometry();
2976
3011
  var point3D = geometry.pointToWorld(point);
2977
3012
  // merge with current position to keep extra dimensions
2978
- var position = this.getCurrentPosition().mergeWith3D(point3D);
2979
-
2980
- return view.setCurrentPosition(position);
3013
+ return this.getCurrentPosition().mergeWith3D(point3D);
2981
3014
  };
2982
3015
 
2983
3016
  /**
@@ -4526,6 +4559,28 @@ dwv.dicom.DicomElementsWrapper.prototype.getTime = function () {
4526
4559
  return undefined;
4527
4560
  };
4528
4561
 
4562
+ /**
4563
+ * Get the pixel data unit.
4564
+ *
4565
+ * @returns {string|null} The unit value if available.
4566
+ */
4567
+ dwv.dicom.DicomElementsWrapper.prototype.getPixelUnit = function () {
4568
+ // RescaleType
4569
+ var unit = this.getFromKey('x00281054');
4570
+ if (!unit) {
4571
+ // Units (for PET)
4572
+ unit = this.getFromKey('x00541001');
4573
+ }
4574
+ // default rescale type for CT
4575
+ if (!unit) {
4576
+ var modality = this.getFromKey('x00080060');
4577
+ if (modality === 'CT') {
4578
+ unit = 'HU';
4579
+ }
4580
+ }
4581
+ return unit;
4582
+ };
4583
+
4529
4584
  /**
4530
4585
  * Get the file list from a DICOMDIR
4531
4586
  *
@@ -4602,7 +4657,7 @@ dwv.dicom = dwv.dicom || {};
4602
4657
  * @returns {string} The version of the library.
4603
4658
  */
4604
4659
  dwv.getVersion = function () {
4605
- return '0.31.0-beta.17';
4660
+ return '0.31.0-beta.18';
4606
4661
  };
4607
4662
 
4608
4663
  /**
@@ -18712,6 +18767,11 @@ dwv.image.ImageFactory.prototype.create = function (
18712
18767
  };
18713
18768
  // PixelRepresentation -> is signed
18714
18769
  meta.IsSigned = meta.PixelRepresentation === 1;
18770
+ // local pixel unit
18771
+ var pixelUnit = dicomElements.getPixelUnit();
18772
+ if (pixelUnit) {
18773
+ meta.pixelUnit = pixelUnit;
18774
+ }
18715
18775
  // FrameOfReferenceUID (optional)
18716
18776
  var frameOfReferenceUID = dicomElements.getFromKey('x00200052');
18717
18777
  if (frameOfReferenceUID) {
@@ -30452,6 +30512,16 @@ dwv.tool.Draw = function (app) {
30452
30512
  shapeEditor.setFactoryList(options);
30453
30513
  };
30454
30514
 
30515
+ /**
30516
+ * Get the type of tool options: here 'factory' since the shape
30517
+ * list contains factories to create each possible shape.
30518
+ *
30519
+ * @returns {string} The type.
30520
+ */
30521
+ this.getOptionsType = function () {
30522
+ return 'factory';
30523
+ };
30524
+
30455
30525
  /**
30456
30526
  * Set the tool live features: shape colour and shape name.
30457
30527
  *
@@ -30477,6 +30547,15 @@ dwv.tool.Draw = function (app) {
30477
30547
  // does nothing
30478
30548
  };
30479
30549
 
30550
+ /**
30551
+ * Get the list of event names that this tool can fire.
30552
+ *
30553
+ * @returns {Array} The list of event names.
30554
+ */
30555
+ this.getEventNames = function () {
30556
+ return ['drawcreate', 'drawchange', 'drawmove', 'drawdelete'];
30557
+ };
30558
+
30480
30559
  /**
30481
30560
  * Add an event listener on the app.
30482
30561
  *
@@ -31846,6 +31925,16 @@ dwv.tool.Filter = function (app) {
31846
31925
  }
31847
31926
  };
31848
31927
 
31928
+ /**
31929
+ * Get the type of tool options: here 'instance' since the filter
31930
+ * list contains instances of each possible filter.
31931
+ *
31932
+ * @returns {string} The type.
31933
+ */
31934
+ this.getOptionsType = function () {
31935
+ return 'instance';
31936
+ };
31937
+
31849
31938
  /**
31850
31939
  * Initialise the filter. Called once the image is loaded.
31851
31940
  */
@@ -31866,6 +31955,15 @@ dwv.tool.Filter = function (app) {
31866
31955
  app.onKeydown(event);
31867
31956
  };
31868
31957
 
31958
+ /**
31959
+ * Get the list of event names that this tool can fire.
31960
+ *
31961
+ * @returns {Array} The list of event names.
31962
+ */
31963
+ this.getEventNames = function () {
31964
+ return ['filterrun', 'filterundo'];
31965
+ };
31966
+
31869
31967
  /**
31870
31968
  * Add an event listener to this class.
31871
31969
  *
@@ -32796,6 +32894,15 @@ dwv.tool.Floodfill = function (app) {
32796
32894
  // does nothing
32797
32895
  };
32798
32896
 
32897
+ /**
32898
+ * Get the list of event names that this tool can fire.
32899
+ *
32900
+ * @returns {Array} The list of event names.
32901
+ */
32902
+ this.getEventNames = function () {
32903
+ return ['drawcreate', 'drawchange', 'drawmove', 'drawdelete'];
32904
+ };
32905
+
32799
32906
  /**
32800
32907
  * Add an event listener to this class.
32801
32908
  *
@@ -33409,6 +33516,15 @@ dwv.tool.Livewire = function (app) {
33409
33516
  // does nothing
33410
33517
  };
33411
33518
 
33519
+ /**
33520
+ * Get the list of event names that this tool can fire.
33521
+ *
33522
+ * @returns {Array} The list of event names.
33523
+ */
33524
+ this.getEventNames = function () {
33525
+ return ['drawcreate', 'drawchange', 'drawmove', 'drawdelete'];
33526
+ };
33527
+
33412
33528
  /**
33413
33529
  * Add an event listener to this class.
33414
33530
  *
@@ -33464,7 +33580,6 @@ dwv.tool.Livewire.prototype.setFeatures = function (features) {
33464
33580
  }
33465
33581
  };
33466
33582
 
33467
-
33468
33583
  // namespaces
33469
33584
  var dwv = dwv || {};
33470
33585
  dwv.tool = dwv.tool || {};
@@ -34881,12 +34996,22 @@ dwv.tool.Scroll = function (app) {
34881
34996
  */
34882
34997
  var wheelDeltaY = 0;
34883
34998
 
34999
+ /**
35000
+ * Option to show or not a value tooltip on mousemove.
35001
+ *
35002
+ * @type {boolean}
35003
+ */
35004
+ var displayTooltip = false;
35005
+
34884
35006
  /**
34885
35007
  * Handle mouse down event.
34886
35008
  *
34887
35009
  * @param {object} event The mouse down event.
34888
35010
  */
34889
35011
  this.mousedown = function (event) {
35012
+ // optional tooltip
35013
+ removeTooltipDiv();
35014
+
34890
35015
  // stop viewer if playing
34891
35016
  var layerDetails = dwv.gui.getLayerDetailsFromEvent(event);
34892
35017
  var layerGroup = app.getLayerGroupById(layerDetails.groupId);
@@ -34903,7 +35028,8 @@ dwv.tool.Scroll = function (app) {
34903
35028
 
34904
35029
  // update controller position
34905
35030
  var planePos = viewLayer.displayToPlanePos(event._x, event._y);
34906
- viewController.setCurrentPosition2D(planePos.x, planePos.y);
35031
+ var position = viewController.getPositionFrom2D(planePos.x, planePos.y);
35032
+ viewController.setCurrentPosition(position);
34907
35033
  };
34908
35034
 
34909
35035
  /**
@@ -34913,6 +35039,10 @@ dwv.tool.Scroll = function (app) {
34913
35039
  */
34914
35040
  this.mousemove = function (event) {
34915
35041
  if (!self.started) {
35042
+ // optional tooltip
35043
+ if (displayTooltip) {
35044
+ showTooltip(event);
35045
+ }
34916
35046
  return;
34917
35047
  }
34918
35048
 
@@ -34976,6 +35106,8 @@ dwv.tool.Scroll = function (app) {
34976
35106
  */
34977
35107
  this.mouseout = function (event) {
34978
35108
  self.mouseup(event);
35109
+ // remove possible tooltip div
35110
+ removeTooltipDiv();
34979
35111
  };
34980
35112
 
34981
35113
  /**
@@ -35067,6 +35199,7 @@ dwv.tool.Scroll = function (app) {
35067
35199
  event.context = 'dwv.tool.Scroll';
35068
35200
  app.onKeydown(event);
35069
35201
  };
35202
+
35070
35203
  /**
35071
35204
  * Handle double click.
35072
35205
  *
@@ -35080,13 +35213,74 @@ dwv.tool.Scroll = function (app) {
35080
35213
  viewController.play();
35081
35214
  };
35082
35215
 
35216
+ /**
35217
+ * Displays a tooltip in a temparary `span`.
35218
+ * Works with css to hide/show the span only on mouse hover.
35219
+ *
35220
+ * @param {object} event The mouse move event.
35221
+ */
35222
+ function showTooltip(event) {
35223
+ // remove previous div
35224
+ removeTooltipDiv();
35225
+
35226
+ // get image value at position
35227
+ var layerDetails = dwv.gui.getLayerDetailsFromEvent(event);
35228
+ var layerGroup = app.getLayerGroupById(layerDetails.groupId);
35229
+ var viewLayer = layerGroup.getActiveViewLayer();
35230
+ var viewController = viewLayer.getViewController();
35231
+ var planePos = viewLayer.displayToPlanePos(event._x, event._y);
35232
+ var position = viewController.getPositionFrom2D(planePos.x, planePos.y);
35233
+ var value = viewController.getRescaledImageValue(position);
35234
+
35235
+ // create
35236
+ if (typeof value !== 'undefined') {
35237
+ var span = document.createElement('span');
35238
+ span.id = 'scroll-tooltip';
35239
+ // place span in layer group to avoid upper layer opacity
35240
+ var layerDiv = document.getElementById(viewLayer.getId());
35241
+ layerDiv.parentElement.appendChild(span);
35242
+ // position tooltip
35243
+ span.style.left = (event._x + 10) + 'px';
35244
+ span.style.top = (event._y + 10) + 'px';
35245
+ var text = dwv.utils.precisionRound(value, 3);
35246
+ if (typeof viewController.getPixelUnit() !== 'undefined') {
35247
+ text += ' ' + viewController.getPixelUnit();
35248
+ }
35249
+ span.appendChild(document.createTextNode(text));
35250
+ }
35251
+ }
35252
+
35253
+ /**
35254
+ * Remove the tooltip html div.
35255
+ */
35256
+ function removeTooltipDiv() {
35257
+ var div = document.getElementById('scroll-tooltip');
35258
+ if (div) {
35259
+ div.remove();
35260
+ }
35261
+ }
35262
+
35083
35263
  /**
35084
35264
  * Activate the tool.
35085
35265
  *
35086
35266
  * @param {boolean} _bool The flag to activate or not.
35087
35267
  */
35088
35268
  this.activate = function (_bool) {
35089
- // does nothing
35269
+ // remove tooltip html when deactivating
35270
+ if (!_bool) {
35271
+ removeTooltipDiv();
35272
+ }
35273
+ };
35274
+
35275
+ /**
35276
+ * Set the tool live features: disaply tooltip.
35277
+ *
35278
+ * @param {object} features The list of features.
35279
+ */
35280
+ this.setFeatures = function (features) {
35281
+ if (typeof features.displayTooltip !== 'undefined') {
35282
+ displayTooltip = features.displayTooltip;
35283
+ }
35090
35284
  };
35091
35285
 
35092
35286
  /**
@@ -38283,6 +38477,23 @@ dwv.utils.stringToUint8Array = function (str) {
38283
38477
  return arr;
38284
38478
  };
38285
38479
 
38480
+ /**
38481
+ * Round a float number to a given precision.
38482
+ * Inspired from https://stackoverflow.com/a/49729715/3639892.
38483
+ * Can be a solution to not have trailing zero as when
38484
+ * using toFixed or toPrecision.
38485
+ * '+number.toFixed(precision)' does not pass all the tests...
38486
+ *
38487
+ * @param {number} number The number to round.
38488
+ * @param {number} precision The rounding precision.
38489
+ * @returns {number} The rounded number.
38490
+ */
38491
+ dwv.utils.precisionRound = function (number, precision) {
38492
+ var factor = Math.pow(10, precision);
38493
+ var delta = 0.01 / factor; // fixes precisionRound(1.005, 2)
38494
+ return Math.round(number * factor + delta) / factor;
38495
+ };
38496
+
38286
38497
  // namespaces
38287
38498
  var dwv = dwv || {};
38288
38499
  dwv.utils = dwv.utils || {};