dwv 0.31.0-beta.16 → 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.16 2022-11-08 14:56:16 */
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.16';
4660
+ return '0.31.0-beta.18';
4606
4661
  };
4607
4662
 
4608
4663
  /**
@@ -4745,6 +4800,27 @@ dwv.dicom.getReverseOrientation = function (ori) {
4745
4800
  return rori;
4746
4801
  };
4747
4802
 
4803
+ /**
4804
+ * Get the name of an image orientation patient.
4805
+ *
4806
+ * @param {Array} orientation The image orientation patient.
4807
+ * @returns {string} The orientation name: axial, coronal or sagittal.
4808
+ */
4809
+ dwv.dicom.getOrientationName = function (orientation) {
4810
+ var axialOrientation = [1, 0, 0, 0, 1, 0];
4811
+ var coronalOrientation = [1, 0, 0, 0, 0, -1];
4812
+ var sagittalOrientation = [0, 1, 0, 0, 0, -1];
4813
+ var name;
4814
+ if (dwv.utils.arrayEquals(orientation, axialOrientation)) {
4815
+ name = 'axial';
4816
+ } else if (dwv.utils.arrayEquals(orientation, coronalOrientation)) {
4817
+ name = 'coronal';
4818
+ } else if (dwv.utils.arrayEquals(orientation, sagittalOrientation)) {
4819
+ name = 'sagittal';
4820
+ }
4821
+ return name;
4822
+ };
4823
+
4748
4824
  /**
4749
4825
  * Tell if a given syntax is an implicit one (element with no VR).
4750
4826
  *
@@ -5708,25 +5784,28 @@ dwv.dicom.DicomParser.prototype.parse = function (buffer) {
5708
5784
  //-------------------------------------------------
5709
5785
  // values needed for data interpretation
5710
5786
 
5711
- // PixelRepresentation 0->unsigned, 1->signed
5712
- var pixelRepresentation = 0;
5713
- dataElement = this.dicomElements.x00280103;
5714
- if (typeof dataElement !== 'undefined') {
5715
- dataElement.value = this.interpretElement(dataElement, dataReader);
5716
- pixelRepresentation = dataElement.value[0];
5717
- } else {
5718
- dwv.logger.warn(
5719
- 'Reading DICOM pixel data with default pixelRepresentation.');
5720
- }
5787
+ // pixel specific
5788
+ if (typeof this.dicomElements.x7FE00010 !== 'undefined') {
5789
+ // PixelRepresentation 0->unsigned, 1->signed
5790
+ var pixelRepresentation = 0;
5791
+ dataElement = this.dicomElements.x00280103;
5792
+ if (typeof dataElement !== 'undefined') {
5793
+ dataElement.value = this.interpretElement(dataElement, dataReader);
5794
+ pixelRepresentation = dataElement.value[0];
5795
+ } else {
5796
+ dwv.logger.warn(
5797
+ 'Reading DICOM pixel data with default pixelRepresentation.');
5798
+ }
5721
5799
 
5722
- // BitsAllocated
5723
- var bitsAllocated = 16;
5724
- dataElement = this.dicomElements.x00280100;
5725
- if (typeof dataElement !== 'undefined') {
5726
- dataElement.value = this.interpretElement(dataElement, dataReader);
5727
- bitsAllocated = dataElement.value[0];
5728
- } else {
5729
- dwv.logger.warn('Reading DICOM pixel data with default bitsAllocated.');
5800
+ // BitsAllocated
5801
+ var bitsAllocated = 16;
5802
+ dataElement = this.dicomElements.x00280100;
5803
+ if (typeof dataElement !== 'undefined') {
5804
+ dataElement.value = this.interpretElement(dataElement, dataReader);
5805
+ bitsAllocated = dataElement.value[0];
5806
+ } else {
5807
+ dwv.logger.warn('Reading DICOM pixel data with default bitsAllocated.');
5808
+ }
5730
5809
  }
5731
5810
 
5732
5811
  // default character set
@@ -6804,8 +6883,6 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6804
6883
  var syntax = dwv.dicom.cleanString(dicomElements.x00020010.value[0]);
6805
6884
  var isImplicit = dwv.dicom.isImplicitTransferSyntax(syntax);
6806
6885
  var isBigEndian = dwv.dicom.isBigEndianTransferSyntax(syntax);
6807
- // Bits Allocated
6808
- var bitsAllocated = dicomElements.x00280100.value[0];
6809
6886
  // Specific CharacterSet
6810
6887
  if (typeof dicomElements.x00080005 !== 'undefined') {
6811
6888
  var oldscs = dwv.dicom.cleanString(dicomElements.x00080005.value[0]);
@@ -6816,6 +6893,11 @@ dwv.dicom.DicomWriter.prototype.getBuffer = function (dicomElements) {
6816
6893
  dicomElements.x00080005.value = ['ISO_IR 192'];
6817
6894
  }
6818
6895
  }
6896
+ // Bits Allocated (for image data)
6897
+ var bitsAllocated;
6898
+ if (typeof dicomElements.x00280100 !== 'undefined') {
6899
+ bitsAllocated = dicomElements.x00280100.value[0];
6900
+ }
6819
6901
 
6820
6902
  // calculate buffer size and split elements (meta and non meta)
6821
6903
  var totalSize = 128 + 4; // DICM
@@ -14153,9 +14235,22 @@ dwv.gui.PositionBinder = function () {
14153
14235
  };
14154
14236
  this.getCallback = function (layerGroup) {
14155
14237
  return function (event) {
14156
- var pos = new dwv.math.Point(event.value[1]);
14238
+ var pointValues = event.value[1];
14157
14239
  var vc = layerGroup.getActiveViewLayer().getViewController();
14158
- vc.setCurrentPosition(pos);
14240
+ // handle different number of dimensions
14241
+ var currentPos = vc.getCurrentPosition();
14242
+ var currentDims = currentPos.length();
14243
+ var inputDims = pointValues.length;
14244
+ if (inputDims !== currentDims) {
14245
+ if (inputDims === currentDims - 1) {
14246
+ // add missing dim, for ex: input 3D -> current 4D
14247
+ pointValues.push(currentPos.get(currentDims - 1));
14248
+ } else if (inputDims === currentDims + 1) {
14249
+ // remove extra dim, for ex: input 4D -> current 3D
14250
+ pointValues.pop();
14251
+ }
14252
+ }
14253
+ vc.setCurrentPosition(new dwv.math.Point(pointValues));
14159
14254
  };
14160
14255
  };
14161
14256
  };
@@ -16984,11 +17079,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
16984
17079
  * Check if a point is in the origin list.
16985
17080
  *
16986
17081
  * @param {dwv.math.Point3D} point3D The point to check.
17082
+ * @param {number} tol The comparison tolerance
17083
+ * default to Number.EPSILON.
16987
17084
  * @returns {boolean} True if in list.
16988
17085
  */
16989
- this.includesOrigin = function (point3D) {
17086
+ this.includesOrigin = function (point3D, tol) {
16990
17087
  for (var i = 0; i < origins.length; ++i) {
16991
- if (origins[i].isSimilar(point3D, dwv.math.BIG_EPSILON)) {
17088
+ if (origins[i].isSimilar(point3D, tol)) {
16992
17089
  return true;
16993
17090
  }
16994
17091
  }
@@ -17019,52 +17116,21 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
17019
17116
  };
17020
17117
 
17021
17118
  /**
17022
- * Get the slice spacing from the difference in the Z directions
17023
- * of the origins.
17024
- *
17025
- * @returns {number} The spacing.
17119
+ * Calculate slice spacing from origins and replace current
17120
+ * if needed.
17026
17121
  */
17027
- this.getSliceGeometrySpacing = function () {
17028
- if (origins.length === 1) {
17029
- return 1;
17030
- }
17031
- var sliceSpacing = null;
17032
- // (x, y, z) = orientationMatrix * (i, j, k)
17033
- // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
17034
- // applied on the patient position, reorders indices
17035
- // so that Z is the slice direction
17036
- var invOrientation = orientation.getInverse();
17037
- var deltas = [];
17038
- for (var i = 0; i < origins.length - 1; ++i) {
17039
- var origin1 = invOrientation.multiplyVector3D(origins[i]);
17040
- var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
17041
- var diff = Math.abs(origin1.getZ() - origin2.getZ());
17042
- if (diff === 0) {
17043
- throw new Error('Zero slice spacing.' +
17044
- origin1.toString() + ' ' + origin2.toString());
17045
- }
17046
- if (sliceSpacing === null) {
17047
- sliceSpacing = diff;
17048
- } else {
17049
- if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
17050
- deltas.push(Math.abs(sliceSpacing - diff));
17051
- }
17052
- }
17053
- }
17054
- // warn if non constant
17055
- if (deltas.length !== 0) {
17056
- var sumReducer = function (sum, value) {
17057
- return sum + value;
17058
- };
17059
- var mean = deltas.reduce(sumReducer) / deltas.length;
17060
- if (mean > 1e-4) {
17061
- dwv.logger.warn('Varying slice spacing, mean delta: ' +
17062
- mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
17063
- }
17122
+ function updateSliceSpacing() {
17123
+ var geoSliceSpacing = dwv.image.getSliceGeometrySpacing(
17124
+ origins, orientation);
17125
+ // update local if needed
17126
+ if (typeof geoSliceSpacing !== 'undefined' &&
17127
+ spacing.get(2) !== geoSliceSpacing) {
17128
+ dwv.logger.trace('Updating slice spacing.');
17129
+ var values = spacing.getValues();
17130
+ values[2] = geoSliceSpacing;
17131
+ spacing = new dwv.image.Spacing(values);
17064
17132
  }
17065
-
17066
- return sliceSpacing;
17067
- };
17133
+ }
17068
17134
 
17069
17135
  /**
17070
17136
  * Get the object spacing.
@@ -17077,9 +17143,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
17077
17143
  this.getSpacing = function (viewOrientation) {
17078
17144
  // update slice spacing after appendSlice
17079
17145
  if (newOrigins) {
17080
- var values = spacing.getValues();
17081
- values[2] = this.getSliceGeometrySpacing();
17082
- spacing = new dwv.image.Spacing(values);
17146
+ updateSliceSpacing();
17083
17147
  newOrigins = false;
17084
17148
  }
17085
17149
  var res = spacing;
@@ -17153,13 +17217,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
17153
17217
  var pointDir = point.minus(closestOrigin);
17154
17218
  // use third orientation matrix column as base plane vector
17155
17219
  var normal = new dwv.math.Vector3D(
17156
- orientation.get(2, 0), orientation.get(2, 1), orientation.get(2, 2));
17220
+ orientation.get(0, 2), orientation.get(1, 2), orientation.get(2, 2));
17157
17221
  // a.dot(b) = ||a|| * ||b|| * cos(theta)
17158
17222
  // (https://en.wikipedia.org/wiki/Dot_product#Geometric_definition)
17159
17223
  // -> the sign of the dot product depends on the cosinus of
17160
17224
  // the angle between the vectors
17161
17225
  // -> >0 => vectors are codirectional
17162
- // -> <0 => vectors are oposite
17226
+ // -> <0 => vectors are opposite
17163
17227
  var dotProd = normal.dotProduct(pointDir);
17164
17228
  var test = dotProd < 0;
17165
17229
  // TODO: check why coronal behaves differently...
@@ -17225,7 +17289,8 @@ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
17225
17289
  dwv.image.Geometry.prototype.toString = function () {
17226
17290
  return 'Origin: ' + this.getOrigin() +
17227
17291
  ', Size: ' + this.getSize() +
17228
- ', Spacing: ' + this.getSpacing();
17292
+ ', Spacing: ' + this.getSpacing() +
17293
+ ', Orientation: ' + this.getOrientation();
17229
17294
  };
17230
17295
 
17231
17296
  /**
@@ -17365,6 +17430,66 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
17365
17430
  return new dwv.math.Index(values);
17366
17431
  };
17367
17432
 
17433
+ /**
17434
+ * Get the slice spacing from the difference in the Z directions
17435
+ * of input origins.
17436
+ *
17437
+ * @param {Array} origins An array of dwv.math.Point3D.
17438
+ * @param {dwv.math.Matrix} orientation The oritentation matrix.
17439
+ * @param {boolean} withCheck Flag to activate spacing variation check,
17440
+ * default to true.
17441
+ * @returns {number|undefined} The spacing.
17442
+ */
17443
+ dwv.image.getSliceGeometrySpacing = function (origins, orientation, withCheck) {
17444
+ if (typeof withCheck === 'undefined') {
17445
+ withCheck = true;
17446
+ }
17447
+ // check origins
17448
+ if (origins.length <= 1) {
17449
+ return;
17450
+ }
17451
+ // (x, y, z) = orientationMatrix * (i, j, k)
17452
+ // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
17453
+ // applied on the patient position, reorders indices
17454
+ // so that Z is the slice direction
17455
+ var invOrientation = orientation.getInverse();
17456
+ var origin1 = invOrientation.multiplyVector3D(origins[0]);
17457
+ var origin2 = invOrientation.multiplyVector3D(origins[1]);
17458
+ var sliceSpacing = Math.abs(origin1.getZ() - origin2.getZ());
17459
+ var deltas = [];
17460
+ for (var i = 0; i < origins.length - 1; ++i) {
17461
+ origin1 = invOrientation.multiplyVector3D(origins[i]);
17462
+ origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
17463
+ var diff = Math.abs(origin1.getZ() - origin2.getZ());
17464
+ if (diff === 0) {
17465
+ throw new Error('Zero slice spacing.' +
17466
+ origin1.toString() + ' ' + origin2.toString());
17467
+ }
17468
+ // keep smallest
17469
+ if (diff < sliceSpacing) {
17470
+ sliceSpacing = diff;
17471
+ }
17472
+ if (withCheck) {
17473
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
17474
+ deltas.push(Math.abs(sliceSpacing - diff));
17475
+ }
17476
+ }
17477
+ }
17478
+ // warn if non constant
17479
+ if (withCheck && deltas.length !== 0) {
17480
+ var sumReducer = function (sum, value) {
17481
+ return sum + value;
17482
+ };
17483
+ var mean = deltas.reduce(sumReducer) / deltas.length;
17484
+ if (mean > 1e-4) {
17485
+ dwv.logger.warn('Varying slice spacing, mean delta: ' +
17486
+ mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
17487
+ }
17488
+ }
17489
+
17490
+ return sliceSpacing;
17491
+ };
17492
+
17368
17493
  // namespaces
17369
17494
  var dwv = dwv || {};
17370
17495
  /** @namespace */
@@ -18642,6 +18767,11 @@ dwv.image.ImageFactory.prototype.create = function (
18642
18767
  };
18643
18768
  // PixelRepresentation -> is signed
18644
18769
  meta.IsSigned = meta.PixelRepresentation === 1;
18770
+ // local pixel unit
18771
+ var pixelUnit = dicomElements.getPixelUnit();
18772
+ if (pixelUnit) {
18773
+ meta.pixelUnit = pixelUnit;
18774
+ }
18645
18775
  // FrameOfReferenceUID (optional)
18646
18776
  var frameOfReferenceUID = dicomElements.getFromKey('x00200052');
18647
18777
  if (frameOfReferenceUID) {
@@ -19635,24 +19765,19 @@ dwv.dicom.equalPosPat = function (pos1, pos2) {
19635
19765
  };
19636
19766
 
19637
19767
  /**
19638
- * Compare two position patients.
19768
+ * Get a position patient compare function accroding to an
19769
+ * input orientation.
19639
19770
  *
19640
- * @param {*} pos1 The first position patient.
19641
- * @param {*} pos2 The second position patient.
19642
- * @returns {number|null} A number used to sort elements.
19771
+ * @param {dwv.math.Matrix33} orientation The orientation matrix.
19772
+ * @returns {Function} The position compare function.
19643
19773
  */
19644
- dwv.dicom.comparePosPat = function (pos1, pos2) {
19645
- var diff = null;
19646
- var posLen = pos1.length;
19647
- var index = posLen;
19648
- for (var i = 0; i < posLen; ++i) {
19649
- --index;
19650
- diff = pos2[index] - pos1[index];
19651
- if (diff !== 0) {
19652
- return diff;
19653
- }
19654
- }
19655
- return diff;
19774
+ dwv.dicom.getComparePosPat = function (orientation) {
19775
+ var invOrientation = orientation.getInverse();
19776
+ return function (pos1, pos2) {
19777
+ var p1 = invOrientation.multiplyArray3D(pos1);
19778
+ var p2 = invOrientation.multiplyArray3D(pos2);
19779
+ return p2[2] - p1[2];
19780
+ };
19656
19781
  };
19657
19782
 
19658
19783
  /**
@@ -19684,7 +19809,7 @@ dwv.dicom.checkTag = function (rootElement, tagDefinition) {
19684
19809
  if (!Array.isArray(tagDefinition.enum[i])) {
19685
19810
  throw new Error('Cannot compare array and non array tag value.');
19686
19811
  }
19687
- if (dwv.utils.arrayEquals(tagDefinition.enum[i], tagValue)) {
19812
+ if (dwv.utils.arraySortEquals(tagDefinition.enum[i], tagValue)) {
19688
19813
  includes = true;
19689
19814
  break;
19690
19815
  }
@@ -20058,10 +20183,12 @@ dwv.dicom.getSpacingFromMeasure = function (measure) {
20058
20183
  parseFloat(pixelSpacing.value[0]),
20059
20184
  parseFloat(pixelSpacing.value[1])
20060
20185
  ];
20061
- // Spacing Between Slices
20062
- if (typeof measure.x00180088 !== 'undefined') {
20063
- var sliceThickness = measure.x00180088;
20064
- spacingValues.push(parseFloat(sliceThickness.value[0]));
20186
+ // Slice Thickness
20187
+ if (typeof measure.x00180050 !== 'undefined') {
20188
+ spacingValues.push(parseFloat(measure.x00180050.value[0]));
20189
+ } else if (typeof measure.x00180088 !== 'undefined') {
20190
+ // Spacing Between Slices
20191
+ spacingValues.push(parseFloat(measure.x00180088.value[0]));
20065
20192
  }
20066
20193
  return new dwv.image.Spacing(spacingValues);
20067
20194
  };
@@ -20295,7 +20422,7 @@ dwv.image.MaskFactory.prototype.create = function (
20295
20422
  if (typeof imageOrientationPatient === 'undefined') {
20296
20423
  imageOrientationPatient = frameInfos[ii].imageOrientationPatient;
20297
20424
  } else {
20298
- if (!dwv.utils.arrayEquals(
20425
+ if (!dwv.utils.arraySortEquals(
20299
20426
  imageOrientationPatient, frameInfos[ii].imageOrientationPatient)) {
20300
20427
  throw new Error('Unsupported multi orientation dicom seg.');
20301
20428
  }
@@ -20312,8 +20439,6 @@ dwv.image.MaskFactory.prototype.create = function (
20312
20439
  }
20313
20440
  }
20314
20441
  }
20315
- // sort positions patient
20316
- framePosPats.sort(dwv.dicom.comparePosPat);
20317
20442
 
20318
20443
  // check spacing and orientation
20319
20444
  if (typeof spacing === 'undefined') {
@@ -20323,12 +20448,7 @@ dwv.image.MaskFactory.prototype.create = function (
20323
20448
  throw new Error('No imageOrientationPatient found for DICOM SEG');
20324
20449
  }
20325
20450
 
20326
- var point3DFromArray = function (arr) {
20327
- return new dwv.math.Point3D(arr[0], arr[1], arr[2]);
20328
- };
20329
-
20330
- // create geometry
20331
- var origin = point3DFromArray(framePosPats[0]);
20451
+ // orientation
20332
20452
  var rowCosines = new dwv.math.Vector3D(
20333
20453
  parseFloat(imageOrientationPatient[0]),
20334
20454
  parseFloat(imageOrientationPatient[1]),
@@ -20344,8 +20464,34 @@ dwv.image.MaskFactory.prototype.create = function (
20344
20464
  rowCosines.getY(), colCosines.getY(), normal.getY(),
20345
20465
  rowCosines.getZ(), colCosines.getZ(), normal.getZ()
20346
20466
  ]);
20347
- var geometry = new dwv.image.Geometry(
20348
- origin, size, spacing, orientationMatrix);
20467
+
20468
+ // sort positions patient
20469
+ framePosPats.sort(dwv.dicom.getComparePosPat(orientationMatrix));
20470
+
20471
+ var point3DFromArray = function (arr) {
20472
+ return new dwv.math.Point3D(arr[0], arr[1], arr[2]);
20473
+ };
20474
+
20475
+ // frame origins
20476
+ var frameOrigins = [];
20477
+ for (var n = 0; n < framePosPats.length; ++n) {
20478
+ frameOrigins.push(point3DFromArray(framePosPats[n]));
20479
+ }
20480
+
20481
+ // use calculated spacing
20482
+ var newSpacing = spacing;
20483
+ var geoSliceSpacing = dwv.image.getSliceGeometrySpacing(
20484
+ frameOrigins, orientationMatrix, false);
20485
+ var spacingValues = spacing.getValues();
20486
+ if (typeof geoSliceSpacing !== 'undefined' &&
20487
+ geoSliceSpacing !== spacingValues[2]) {
20488
+ spacingValues[2] = geoSliceSpacing;
20489
+ newSpacing = new dwv.image.Spacing(spacingValues);
20490
+ }
20491
+
20492
+ // tmp geometry with correct spacing but only one slice
20493
+ var tmpGeometry = new dwv.image.Geometry(
20494
+ frameOrigins[0], size, newSpacing, orientationMatrix);
20349
20495
 
20350
20496
  // add possibly missing posPats
20351
20497
  var posPats = [];
@@ -20354,23 +20500,36 @@ dwv.image.MaskFactory.prototype.create = function (
20354
20500
  for (var g = 1; g < framePosPats.length; ++g) {
20355
20501
  ++sliceIndex;
20356
20502
  var index = new dwv.math.Index([0, 0, sliceIndex]);
20357
- var point = geometry.indexToWorld(index).get3D();
20358
- var framePosPat = point3DFromArray(framePosPats[g]);
20503
+ var point = tmpGeometry.indexToWorld(index).get3D();
20504
+ var frameOrigin = frameOrigins[g];
20359
20505
  // check if more pos pats are needed
20360
- var dist = framePosPat.getDistance(point);
20506
+ var dist = frameOrigin.getDistance(point);
20361
20507
  // TODO: good threshold?
20362
- while (dist > 1e-4) {
20363
- dwv.logger.debug('Adding intermediate pos pats for DICOM seg');
20508
+ while (dist > dwv.math.REAL_WORLD_EPSILON) {
20509
+ dwv.logger.debug('Adding intermediate pos pats for DICOM seg at ' +
20510
+ point.toString());
20364
20511
  posPats.push([point.getX(), point.getY(), point.getZ()]);
20365
20512
  ++sliceIndex;
20366
20513
  index = new dwv.math.Index([0, 0, sliceIndex]);
20367
- point = geometry.indexToWorld(index).get3D();
20368
- dist = framePosPat.getDistance(point);
20514
+ point = tmpGeometry.indexToWorld(index).get3D();
20515
+ dist = frameOrigin.getDistance(point);
20369
20516
  }
20370
20517
  // add frame pos pat
20371
20518
  posPats.push(framePosPats[g]);
20372
20519
  }
20373
20520
 
20521
+ // as many slices as posPats
20522
+ var numberOfSlices = posPats.length;
20523
+
20524
+ // final geometry
20525
+ var geometry = new dwv.image.Geometry(
20526
+ frameOrigins[0], size, newSpacing, orientationMatrix);
20527
+ var uids = [0];
20528
+ for (var m = 1; m < numberOfSlices; ++m) {
20529
+ geometry.appendOrigin(point3DFromArray(posPats[m]), m);
20530
+ uids.push(m);
20531
+ }
20532
+
20374
20533
  var getFindSegmentFunc = function (number) {
20375
20534
  return function (item) {
20376
20535
  return item.number === number;
@@ -20378,8 +20537,6 @@ dwv.image.MaskFactory.prototype.create = function (
20378
20537
  };
20379
20538
 
20380
20539
  // create output buffer
20381
- // as many slices as posPats
20382
- var numberOfSlices = posPats.length;
20383
20540
  var mul = storeAsRGB ? 3 : 1;
20384
20541
  var buffer = new pixelBuffer.constructor(mul * sliceSize * numberOfSlices);
20385
20542
  buffer.fill(0);
@@ -20410,13 +20567,6 @@ dwv.image.MaskFactory.prototype.create = function (
20410
20567
  }
20411
20568
  }
20412
20569
 
20413
- var uids = [0];
20414
- for (var m = 1; m < numberOfSlices; ++m) {
20415
- // args: origin, volumeNumber, uid, index, increment
20416
- geometry.appendOrigin(point3DFromArray(posPats[m]), m);
20417
- uids.push(m);
20418
- }
20419
-
20420
20570
  // create image
20421
20571
  var image = new dwv.image.Image(geometry, buffer, uids);
20422
20572
  if (storeAsRGB) {
@@ -21260,7 +21410,7 @@ dwv.image.View = function (image) {
21260
21410
  * Store position and not index to stay geometry independent.
21261
21411
  *
21262
21412
  * @private
21263
- * @type {dwv.math.Index}
21413
+ * @type {dwv.math.Point3D}
21264
21414
  */
21265
21415
  var currentPosition = null;
21266
21416
  /**
@@ -21758,6 +21908,7 @@ dwv.image.View = function (image) {
21758
21908
  }
21759
21909
 
21760
21910
  // all good
21911
+ //console.log('[v] set cur index OUT');
21761
21912
  return true;
21762
21913
  };
21763
21914
 
@@ -26806,13 +26957,16 @@ if (typeof Number.EPSILON === 'undefined') {
26806
26957
  }
26807
26958
  // -> ~2e-12
26808
26959
  dwv.math.BIG_EPSILON = Number.EPSILON * 1e4;
26960
+ // 'real world', for example when comparing positions
26961
+ dwv.math.REAL_WORLD_EPSILON = 1e-4;
26809
26962
 
26810
26963
  /**
26811
26964
  * Check if two numbers are similar.
26812
26965
  *
26813
26966
  * @param {number} a The first number.
26814
26967
  * @param {number} b The second number.
26815
- * @param {number} tol The comparison tolerance.
26968
+ * @param {number} tol The comparison tolerance
26969
+ * default to Number.EPSILON.
26816
26970
  * @returns {boolean} True if similar.
26817
26971
  */
26818
26972
  dwv.math.isSimilar = function (a, b, tol) {
@@ -27499,11 +27653,11 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
27499
27653
  };
27500
27654
 
27501
27655
  /**
27502
- * Get an array find callback for a given point.
27656
+ * Get an array find callback for an equal input point.
27503
27657
  *
27504
27658
  * @param {dwv.math.Point3D} point The point to compare to.
27505
- * @returns {Function} A function that compares its input point to the one
27506
- * given as input to this function.
27659
+ * @returns {Function} A function that compares, using `equals`,
27660
+ * its input point to the one given as input to this function.
27507
27661
  */
27508
27662
  dwv.math.getEqualPoint3DFunction = function (point) {
27509
27663
  return function (element) {
@@ -27511,6 +27665,21 @@ dwv.math.getEqualPoint3DFunction = function (point) {
27511
27665
  };
27512
27666
  };
27513
27667
 
27668
+ /**
27669
+ * Get an array find callback for a similar input point.
27670
+ *
27671
+ * @param {dwv.math.Point3D} point The point to compare to.
27672
+ * @param {number} tol The comparison tolerance
27673
+ * default to Number.EPSILON.
27674
+ * @returns {Function} A function that compares, using `isSimilar`,
27675
+ * its input point to the one given as input to this function.
27676
+ */
27677
+ dwv.math.getSimilarPoint3DFunction = function (point, tol) {
27678
+ return function (element) {
27679
+ return element.isSimilar(point, tol);
27680
+ };
27681
+ };
27682
+
27514
27683
  /**
27515
27684
  * Immutable point.
27516
27685
  * Warning: the input array is NOT cloned, modifying it will
@@ -30343,6 +30512,16 @@ dwv.tool.Draw = function (app) {
30343
30512
  shapeEditor.setFactoryList(options);
30344
30513
  };
30345
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
+
30346
30525
  /**
30347
30526
  * Set the tool live features: shape colour and shape name.
30348
30527
  *
@@ -30368,6 +30547,15 @@ dwv.tool.Draw = function (app) {
30368
30547
  // does nothing
30369
30548
  };
30370
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
+
30371
30559
  /**
30372
30560
  * Add an event listener on the app.
30373
30561
  *
@@ -31737,6 +31925,16 @@ dwv.tool.Filter = function (app) {
31737
31925
  }
31738
31926
  };
31739
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
+
31740
31938
  /**
31741
31939
  * Initialise the filter. Called once the image is loaded.
31742
31940
  */
@@ -31757,6 +31955,15 @@ dwv.tool.Filter = function (app) {
31757
31955
  app.onKeydown(event);
31758
31956
  };
31759
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
+
31760
31967
  /**
31761
31968
  * Add an event listener to this class.
31762
31969
  *
@@ -32687,6 +32894,15 @@ dwv.tool.Floodfill = function (app) {
32687
32894
  // does nothing
32688
32895
  };
32689
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
+
32690
32906
  /**
32691
32907
  * Add an event listener to this class.
32692
32908
  *
@@ -33300,6 +33516,15 @@ dwv.tool.Livewire = function (app) {
33300
33516
  // does nothing
33301
33517
  };
33302
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
+
33303
33528
  /**
33304
33529
  * Add an event listener to this class.
33305
33530
  *
@@ -33355,7 +33580,6 @@ dwv.tool.Livewire.prototype.setFeatures = function (features) {
33355
33580
  }
33356
33581
  };
33357
33582
 
33358
-
33359
33583
  // namespaces
33360
33584
  var dwv = dwv || {};
33361
33585
  dwv.tool = dwv.tool || {};
@@ -34772,12 +34996,22 @@ dwv.tool.Scroll = function (app) {
34772
34996
  */
34773
34997
  var wheelDeltaY = 0;
34774
34998
 
34999
+ /**
35000
+ * Option to show or not a value tooltip on mousemove.
35001
+ *
35002
+ * @type {boolean}
35003
+ */
35004
+ var displayTooltip = false;
35005
+
34775
35006
  /**
34776
35007
  * Handle mouse down event.
34777
35008
  *
34778
35009
  * @param {object} event The mouse down event.
34779
35010
  */
34780
35011
  this.mousedown = function (event) {
35012
+ // optional tooltip
35013
+ removeTooltipDiv();
35014
+
34781
35015
  // stop viewer if playing
34782
35016
  var layerDetails = dwv.gui.getLayerDetailsFromEvent(event);
34783
35017
  var layerGroup = app.getLayerGroupById(layerDetails.groupId);
@@ -34794,7 +35028,8 @@ dwv.tool.Scroll = function (app) {
34794
35028
 
34795
35029
  // update controller position
34796
35030
  var planePos = viewLayer.displayToPlanePos(event._x, event._y);
34797
- viewController.setCurrentPosition2D(planePos.x, planePos.y);
35031
+ var position = viewController.getPositionFrom2D(planePos.x, planePos.y);
35032
+ viewController.setCurrentPosition(position);
34798
35033
  };
34799
35034
 
34800
35035
  /**
@@ -34804,6 +35039,10 @@ dwv.tool.Scroll = function (app) {
34804
35039
  */
34805
35040
  this.mousemove = function (event) {
34806
35041
  if (!self.started) {
35042
+ // optional tooltip
35043
+ if (displayTooltip) {
35044
+ showTooltip(event);
35045
+ }
34807
35046
  return;
34808
35047
  }
34809
35048
 
@@ -34867,6 +35106,8 @@ dwv.tool.Scroll = function (app) {
34867
35106
  */
34868
35107
  this.mouseout = function (event) {
34869
35108
  self.mouseup(event);
35109
+ // remove possible tooltip div
35110
+ removeTooltipDiv();
34870
35111
  };
34871
35112
 
34872
35113
  /**
@@ -34958,6 +35199,7 @@ dwv.tool.Scroll = function (app) {
34958
35199
  event.context = 'dwv.tool.Scroll';
34959
35200
  app.onKeydown(event);
34960
35201
  };
35202
+
34961
35203
  /**
34962
35204
  * Handle double click.
34963
35205
  *
@@ -34971,13 +35213,74 @@ dwv.tool.Scroll = function (app) {
34971
35213
  viewController.play();
34972
35214
  };
34973
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
+
34974
35263
  /**
34975
35264
  * Activate the tool.
34976
35265
  *
34977
35266
  * @param {boolean} _bool The flag to activate or not.
34978
35267
  */
34979
35268
  this.activate = function (_bool) {
34980
- // 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
+ }
34981
35284
  };
34982
35285
 
34983
35286
  /**
@@ -35627,6 +35930,25 @@ dwv.tool.ZoomAndPan.prototype.init = function () {
35627
35930
  var dwv = dwv || {};
35628
35931
  dwv.utils = dwv.utils || {};
35629
35932
 
35933
+ /**
35934
+ * Check for array equality after sorting.
35935
+ *
35936
+ * @param {Array} arr0 First array.
35937
+ * @param {*} arr1 Second array.
35938
+ * @returns {boolean} True if both array are defined and contain same values.
35939
+ */
35940
+ dwv.utils.arraySortEquals = function (arr0, arr1) {
35941
+ if (arr0 === null ||
35942
+ arr1 === null ||
35943
+ typeof arr0 === 'undefined' ||
35944
+ typeof arr1 === 'undefined') {
35945
+ return false;
35946
+ }
35947
+ var arr0sorted = arr0.slice().sort();
35948
+ var arr1sorted = arr1.slice().sort();
35949
+ return dwv.utils.arrayEquals(arr0sorted, arr1sorted);
35950
+ };
35951
+
35630
35952
  /**
35631
35953
  * Check for array equality.
35632
35954
  *
@@ -35644,10 +35966,8 @@ dwv.utils.arrayEquals = function (arr0, arr1) {
35644
35966
  if (arr0.length !== arr1.length) {
35645
35967
  return false;
35646
35968
  }
35647
- var arr0sorted = arr0.slice().sort();
35648
- var arr1sorted = arr1.slice().sort();
35649
- return arr0sorted.every(function (element, index) {
35650
- return element === arr1sorted[index];
35969
+ return arr0.every(function (element, index) {
35970
+ return element === arr1[index];
35651
35971
  });
35652
35972
  };
35653
35973
 
@@ -38157,6 +38477,23 @@ dwv.utils.stringToUint8Array = function (str) {
38157
38477
  return arr;
38158
38478
  };
38159
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
+
38160
38497
  // namespaces
38161
38498
  var dwv = dwv || {};
38162
38499
  dwv.utils = dwv.utils || {};