dwv 0.31.0-beta.1 → 0.31.0-beta.10

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.1 2022-03-17 17:17:41 */
1
+ /*! dwv 0.31.0-beta.10 2022-08-03 14:54:29 */
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
  *
@@ -460,8 +513,6 @@ dwv.App = function () {
460
513
  * Load a list of files. Can be image files or a state file.
461
514
  *
462
515
  * @param {Array} files The list of files to load.
463
- * @param {object} options The options object, can contain:
464
- * - timepoint: an object with time information
465
516
  * @fires dwv.App#loadstart
466
517
  * @fires dwv.App#loadprogress
467
518
  * @fires dwv.App#loaditem
@@ -469,12 +520,12 @@ dwv.App = function () {
469
520
  * @fires dwv.App#error
470
521
  * @fires dwv.App#abort
471
522
  */
472
- this.loadFiles = function (files, options) {
523
+ this.loadFiles = function (files) {
473
524
  if (files.length === 0) {
474
525
  dwv.logger.warn('Ignoring empty input file list.');
475
526
  return;
476
527
  }
477
- loadController.loadFiles(files, options);
528
+ loadController.loadFiles(files);
478
529
  };
479
530
 
480
531
  /**
@@ -532,8 +583,7 @@ dwv.App = function () {
532
583
  this.fitToContainer = function () {
533
584
  for (var i = 0; i < stage.getNumberOfLayerGroups(); ++i) {
534
585
  var layerGroup = stage.getLayerGroup(i);
535
- var mazSize = getLayerGroupMaxSize(layerGroup);
536
- layerGroup.fitToContainer(mazSize);
586
+ layerGroup.fitToContainer();
537
587
  layerGroup.draw();
538
588
  }
539
589
  };
@@ -560,15 +610,25 @@ dwv.App = function () {
560
610
  typeof options.dataViewConfigs === 'undefined') {
561
611
  throw new Error('No available data view configuration');
562
612
  }
563
- var configs = null;
613
+ var configs = [];
564
614
  if (typeof options.dataViewConfigs['*'] !== 'undefined') {
565
615
  configs = options.dataViewConfigs['*'];
566
- } else {
616
+ } else if (typeof options.dataViewConfigs[dataIndex] !== 'undefined') {
567
617
  configs = options.dataViewConfigs[dataIndex];
568
618
  }
569
619
  return configs;
570
620
  }
571
621
 
622
+ /**
623
+ * Get the data view config.
624
+ * Carefull, returns a reference, do not modify without resetting.
625
+ *
626
+ * @returns {object} The configuration list.
627
+ */
628
+ this.getDataViewConfig = function () {
629
+ return options.dataViewConfigs;
630
+ };
631
+
572
632
  /**
573
633
  * Set the data view configuration (see the init options for details).
574
634
  *
@@ -579,9 +639,40 @@ dwv.App = function () {
579
639
  stage.empty();
580
640
  // set new
581
641
  options.dataViewConfigs = configs;
582
- // re-bind layers
583
- stage.bindLayerGroups();
584
- };
642
+ // create layer groups
643
+ createLayerGroups(configs);
644
+ };
645
+
646
+ /**
647
+ * Create layer groups according to a data view config:
648
+ * adds them to stage and bind them.
649
+ *
650
+ * @param {object} dataViewConfigs The data view config.
651
+ */
652
+ function createLayerGroups(dataViewConfigs) {
653
+ var dataKeys = Object.keys(dataViewConfigs);
654
+ var divIds = [];
655
+ for (var i = 0; i < dataKeys.length; ++i) {
656
+ var dataConfigs = dataViewConfigs[dataKeys[i]];
657
+ for (var j = 0; j < dataConfigs.length; ++j) {
658
+ var viewConfig = dataConfigs[j];
659
+ // view configs can contain the same divIds, avoid duplicating
660
+ if (!divIds.includes(viewConfig.divId)) {
661
+ // create new layer group
662
+ var element = document.getElementById(viewConfig.divId);
663
+ var layerGroup = stage.addLayerGroup(element);
664
+ // bind events
665
+ bindLayerGroupToApp(layerGroup);
666
+ // optional orientation
667
+ if (typeof viewConfig.orientation !== 'undefined') {
668
+ layerGroup.setTargetOrientation(
669
+ dwv.math.getMatrixFromName(viewConfig.orientation));
670
+ }
671
+ divIds.push(viewConfig.divId);
672
+ }
673
+ }
674
+ }
675
+ }
585
676
 
586
677
  /**
587
678
  * Set the layer groups binders.
@@ -601,30 +692,31 @@ dwv.App = function () {
601
692
  if (typeof dataIndex === 'undefined' || dataIndex === null) {
602
693
  throw new Error('Cannot render without data index');
603
694
  }
695
+
696
+ // create layer groups if not done yet
697
+ // (create all to allow for ratio sync)
698
+ if (stage.getNumberOfLayerGroups() === 0) {
699
+ createLayerGroups(options.dataViewConfigs);
700
+ }
701
+
604
702
  // loop on all configs
605
703
  var viewConfigs = getViewConfigs(dataIndex);
606
- if (!viewConfigs) {
607
- throw new Error('No view config for data: ' + dataIndex);
704
+ // nothing to do if no view config
705
+ if (viewConfigs.length === 0) {
706
+ dwv.logger.info('Not rendering data: ' + dataIndex +
707
+ ' (no data view config)');
708
+ return;
608
709
  }
609
710
  for (var i = 0; i < viewConfigs.length; ++i) {
610
711
  var config = viewConfigs[i];
611
- // create layer group if not done yet
612
- // warn: needs a loaded DOM
613
712
  var layerGroup =
614
713
  stage.getLayerGroupWithElementId(config.divId);
714
+ // layer group must exist
615
715
  if (!layerGroup) {
616
- // create new layer group
617
- var element = document.getElementById(config.divId);
618
- layerGroup = stage.addLayerGroup(element);
619
- // bind events
620
- bindLayerGroup(layerGroup);
621
- // optional orientation
622
- if (typeof config.orientation !== 'undefined') {
623
- layerGroup.setTargetOrientation(
624
- dwv.math.getMatrixFromName(config.orientation));
625
- }
716
+ throw new Error('No layer group for ' + config.divId);
626
717
  }
627
718
  // initialise or add view
719
+ // warn: needs a loaded DOM
628
720
  if (layerGroup.getViewLayersByDataIndex(dataIndex).length === 0) {
629
721
  if (layerGroup.getNumberOfLayers() === 0) {
630
722
  initialiseBaseLayers(dataIndex, config);
@@ -648,7 +740,8 @@ dwv.App = function () {
648
740
  var layerGroup = stage.getActiveLayerGroup();
649
741
  var viewController = layerGroup.getActiveViewLayer().getViewController();
650
742
  var k = viewController.getCurrentScrollPosition();
651
- layerGroup.addScale(step, {x: cx, y: cy, z: k});
743
+ var center = new dwv.math.Point3D(cx, cy, k);
744
+ layerGroup.addScale(step, center);
652
745
  layerGroup.draw();
653
746
  };
654
747
 
@@ -912,7 +1005,9 @@ dwv.App = function () {
912
1005
  } else {
913
1006
  layer = layerGroup.getActiveViewLayer();
914
1007
  }
915
- toolboxController.bindLayer(layer);
1008
+ if (layer) {
1009
+ toolboxController.bindLayer(layer);
1010
+ }
916
1011
  }
917
1012
 
918
1013
  // set toolbox tool
@@ -920,46 +1015,12 @@ dwv.App = function () {
920
1015
  };
921
1016
 
922
1017
  /**
923
- * Set the draw shape.
924
- *
925
- * @param {string} shape The draw shape.
926
- */
927
- this.setDrawShape = function (shape) {
928
- toolboxController.setSelectedShape(shape);
929
- };
930
-
931
- /**
932
- * Set the image filter
933
- *
934
- * @param {string} filter The image filter.
935
- */
936
- this.setImageFilter = function (filter) {
937
- toolboxController.setSelectedFilter(filter);
938
- };
939
-
940
- /**
941
- * Run the selected image filter.
942
- */
943
- this.runImageFilter = function () {
944
- toolboxController.runSelectedFilter();
945
- };
946
-
947
- /**
948
- * Set the draw line colour.
949
- *
950
- * @param {string} colour The line colour.
951
- */
952
- this.setDrawLineColour = function (colour) {
953
- toolboxController.setLineColour(colour);
954
- };
955
-
956
- /**
957
- * Set the filter min/max.
1018
+ * Set the tool live features.
958
1019
  *
959
- * @param {object} range The new range of the data: {min:a, max:b}.
1020
+ * @param {object} list The list of features.
960
1021
  */
961
- this.setFilterMinMax = function (range) {
962
- toolboxController.setRange(range);
1022
+ this.setToolFeatures = function (list) {
1023
+ toolboxController.setToolFeatures(list);
963
1024
  };
964
1025
 
965
1026
  /**
@@ -1053,21 +1114,15 @@ dwv.App = function () {
1053
1114
  }
1054
1115
 
1055
1116
  var isFirstLoadItem = event.isfirstitem;
1056
- var isTimepoint = typeof event.timepoint !== 'undefined';
1057
- var timeId = 0;
1058
- if (isTimepoint) {
1059
- timeId = event.timepoint.id;
1060
- }
1061
1117
 
1062
1118
  var eventMetaData = null;
1063
1119
  if (event.loadtype === 'image') {
1064
- if (isFirstLoadItem && timeId === 0) {
1120
+ if (isFirstLoadItem) {
1065
1121
  dataController.addNew(
1066
1122
  event.loadid, event.data.image, event.data.info);
1067
1123
  } else {
1068
1124
  dataController.update(
1069
- event.loadid, event.data.image, event.data.info,
1070
- timeId);
1125
+ event.loadid, event.data.image, event.data.info);
1071
1126
  }
1072
1127
  eventMetaData = event.data.info;
1073
1128
  } else if (event.loadtype === 'state') {
@@ -1091,11 +1146,14 @@ dwv.App = function () {
1091
1146
  type: 'loaditem',
1092
1147
  data: eventMetaData,
1093
1148
  source: event.source,
1094
- loadtype: event.loadtype
1149
+ loadtype: event.loadtype,
1150
+ loadid: event.loadid,
1151
+ isfirstitem: event.isfirstitem
1095
1152
  });
1096
1153
 
1097
1154
  // render if first and flag allows
1098
1155
  if (event.loadtype === 'image' &&
1156
+ getViewConfigs(event.loadid).length !== 0 &&
1099
1157
  isFirstLoadItem && options.viewOnFirstLoadItem) {
1100
1158
  self.render(event.loadid);
1101
1159
  }
@@ -1192,7 +1250,7 @@ dwv.App = function () {
1192
1250
  * @param {object} group The layer group.
1193
1251
  * @private
1194
1252
  */
1195
- function bindLayerGroup(group) {
1253
+ function bindLayerGroupToApp(group) {
1196
1254
  // propagate layer group events
1197
1255
  group.addEventListener('zoomchange', fireEvent);
1198
1256
  group.addEventListener('offsetchange', fireEvent);
@@ -1223,36 +1281,6 @@ dwv.App = function () {
1223
1281
  }
1224
1282
  }
1225
1283
 
1226
- /**
1227
- * Get the data max size for a layer group.
1228
- *
1229
- * @todo Filter for data of the layer group.
1230
- * @param {object} lg The layer group.
1231
- * @returns {object} The max size as {x,y}.
1232
- */
1233
- function getLayerGroupMaxSize(lg) {
1234
- var maxSize = {x: 0, y: 0};
1235
- for (var i = 0; i < dataController.length(); ++i) {
1236
- var dc = dataController.get(i);
1237
- var geometry = dc.image.getGeometry();
1238
- var viewOrient = dwv.gui.getViewOrientation(
1239
- geometry,
1240
- lg.getTargetOrientation()
1241
- );
1242
- var size = geometry.getSize(viewOrient).get2D();
1243
- var spacing = geometry.getSpacing(viewOrient).get2D();
1244
- var width = size.x * spacing.x;
1245
- if (width > maxSize.x) {
1246
- maxSize.x = width;
1247
- }
1248
- var height = size.y * spacing.y;
1249
- if (height > maxSize.y) {
1250
- maxSize.y = height;
1251
- }
1252
- }
1253
- return maxSize;
1254
- }
1255
-
1256
1284
  /**
1257
1285
  * Add a view layer.
1258
1286
  *
@@ -1280,7 +1308,7 @@ dwv.App = function () {
1280
1308
  new dwv.dicom.DicomElementsWrapper(data.meta),
1281
1309
  data.image);
1282
1310
  var viewOrientation = dwv.gui.getViewOrientation(
1283
- imageGeometry,
1311
+ imageGeometry.getOrientation(),
1284
1312
  layerGroup.getTargetOrientation()
1285
1313
  );
1286
1314
  view.setOrientation(viewOrientation);
@@ -1288,11 +1316,11 @@ dwv.App = function () {
1288
1316
  // make pixel of value 0 transparent for segmentation
1289
1317
  // (assuming RGB data)
1290
1318
  if (data.image.getMeta().Modality === 'SEG') {
1291
- view.setAlphaFunction(function (value) {
1319
+ view.setAlphaFunction(function (value /*, index*/) {
1292
1320
  if (value[0] === 0 &&
1293
1321
  value[1] === 0 &&
1294
1322
  value[2] === 0) {
1295
- return 1;
1323
+ return 0;
1296
1324
  } else {
1297
1325
  return 0xff;
1298
1326
  }
@@ -1307,6 +1335,7 @@ dwv.App = function () {
1307
1335
  // opacity
1308
1336
  var opacity = 1;
1309
1337
  // do we have more than one layer
1338
+ // (the layer has not been added to the layer group yet)
1310
1339
  if (layerGroup.getNumberOfLayers() !== 0) {
1311
1340
  opacity = 0.5;
1312
1341
  // set color map if non was provided
@@ -1320,21 +1349,9 @@ dwv.App = function () {
1320
1349
  viewLayer.setView(view);
1321
1350
  var size2D = imageGeometry.getSize(viewOrientation).get2D();
1322
1351
  var spacing2D = imageGeometry.getSpacing(viewOrientation).get2D();
1323
- viewLayer.initialise(size2D, spacing2D, dataIndex);
1324
- viewLayer.setOpacity(opacity);
1325
-
1326
- // compensate origin difference
1327
- var diff = null;
1328
- if (dataIndex !== 0) {
1329
- // offset from the top, use first origin
1330
- var data0 = dataController.get(0);
1331
- var origin0 = data0.image.getGeometry().getOrigins()[0];
1332
- var origin1 = imageGeometry.getOrigins()[0];
1333
- diff = origin0.minus(origin1);
1334
- // TODO: check why -z...
1335
- viewLayer.setBaseOffset(new dwv.math.Vector3D(
1336
- diff.getX(), diff.getY(), -1 * diff.getZ()));
1337
- }
1352
+ viewLayer.initialise(size2D, spacing2D, dataIndex, opacity);
1353
+ viewLayer.setScale(layerGroup.getScale());
1354
+ viewLayer.setOffset(layerGroup.getOffset());
1338
1355
 
1339
1356
  // listen to image changes
1340
1357
  dataController.addEventListener('imagechange', viewLayer.onimagechange);
@@ -1348,23 +1365,20 @@ dwv.App = function () {
1348
1365
  dl.initialise(size2D, spacing2D, dataIndex);
1349
1366
  dl.setPlaneHelper(viewLayer.getViewController().getPlaneHelper());
1350
1367
 
1368
+ // force positionchange to sync layers
1351
1369
  var vc = viewLayer.getViewController();
1352
- // positionchange event like data
1353
1370
  var value = [
1354
1371
  vc.getCurrentIndex().getValues(),
1355
1372
  vc.getCurrentPosition().getValues()
1356
1373
  ];
1357
- layerGroup.updateLayersToPositionChange({value: value});
1358
-
1359
- // compensate origin difference
1360
- if (dataIndex !== 0) {
1361
- dl.setBaseOffset(diff);
1362
- }
1374
+ layerGroup.updateLayersToPositionChange({
1375
+ value: value,
1376
+ srclayerid: viewLayer.getId()
1377
+ });
1363
1378
  }
1364
1379
 
1365
- // fit to the maximum size
1366
- var maxSize = getLayerGroupMaxSize(layerGroup);
1367
- layerGroup.fitToContainer(maxSize);
1380
+ // sync layer groups
1381
+ stage.syncLayerGroupScale();
1368
1382
  }
1369
1383
 
1370
1384
  };
@@ -1461,40 +1475,27 @@ dwv.ctrl.DataController = function () {
1461
1475
  * @param {number} index The index of the data.
1462
1476
  * @param {dwv.image.Image} image The image.
1463
1477
  * @param {object} meta The image meta.
1464
- * @param {number} timeId The time ID.
1465
1478
  */
1466
- this.update = function (index, image, meta, timeId) {
1479
+ this.update = function (index, image, meta) {
1467
1480
  var dataToUpdate = data[index];
1468
1481
 
1469
- // handle possible timepoint
1470
- if (typeof timeId !== 'undefined') {
1471
- var size = dataToUpdate.image.getGeometry().getSize();
1472
- // append frame for first frame (still 3D) or other frames
1473
- if ((size.length() === 3 && timeId !== 0) ||
1474
- (size.length() > 3 && timeId >= size.get(3))) {
1475
- dataToUpdate.image.appendFrame();
1476
- }
1477
- }
1478
-
1479
1482
  // add slice to current image
1480
- dataToUpdate.image.appendSlice(image, timeId);
1483
+ dataToUpdate.image.appendSlice(image);
1481
1484
 
1482
1485
  // update meta data
1483
1486
  // TODO add time support
1484
- if (timeId === 0) {
1485
- var idKey = '';
1486
- if (typeof meta.x00020010 !== 'undefined') {
1487
- // dicom case
1488
- idKey = 'InstanceNumber';
1489
- } else {
1490
- idKey = 'imageUid';
1491
- }
1492
- dataToUpdate.meta = dwv.utils.mergeObjects(
1493
- dataToUpdate.meta,
1494
- getMetaObject(meta),
1495
- idKey,
1496
- 'value');
1487
+ var idKey = '';
1488
+ if (typeof meta.x00020010 !== 'undefined') {
1489
+ // dicom case
1490
+ idKey = 'InstanceNumber';
1491
+ } else {
1492
+ idKey = 'imageUid';
1497
1493
  }
1494
+ dataToUpdate.meta = dwv.utils.mergeObjects(
1495
+ dataToUpdate.meta,
1496
+ getMetaObject(meta),
1497
+ idKey,
1498
+ 'value');
1498
1499
  };
1499
1500
 
1500
1501
  /**
@@ -2088,16 +2089,14 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2088
2089
  * Load a list of files. Can be image files or a state file.
2089
2090
  *
2090
2091
  * @param {Array} files The list of files to load.
2091
- * @param {object} options The options object, can contain:
2092
- * - timepoint: an object with time information
2093
2092
  */
2094
- this.loadFiles = function (files, options) {
2093
+ this.loadFiles = function (files) {
2095
2094
  // has been checked for emptiness.
2096
2095
  var ext = files[0].name.split('.').pop().toLowerCase();
2097
2096
  if (ext === 'json') {
2098
- loadStateFile(files[0], options);
2097
+ loadStateFile(files[0]);
2099
2098
  } else {
2100
- loadImageFiles(files, options);
2099
+ loadImageFiles(files);
2101
2100
  }
2102
2101
  };
2103
2102
 
@@ -2149,16 +2148,14 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2149
2148
  * Load a list of image files.
2150
2149
  *
2151
2150
  * @param {Array} files The list of image files to load.
2152
- * @param {object} options The options object, can contain:
2153
- * - timepoint: an object with time information
2154
2151
  * @private
2155
2152
  */
2156
- function loadImageFiles(files, options) {
2153
+ function loadImageFiles(files) {
2157
2154
  // create IO
2158
2155
  var fileIO = new dwv.io.FilesLoader();
2159
2156
  fileIO.setDefaultCharacterSet(defaultCharacterSet);
2160
2157
  // load data
2161
- loadData(files, fileIO, 'image', options);
2158
+ loadData(files, fileIO, 'image');
2162
2159
  }
2163
2160
 
2164
2161
  /**
@@ -2182,14 +2179,13 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2182
2179
  * Load a State file.
2183
2180
  *
2184
2181
  * @param {string} file The state file to load.
2185
- * @param {object} options The options object.
2186
2182
  * @private
2187
2183
  */
2188
- function loadStateFile(file, options) {
2184
+ function loadStateFile(file) {
2189
2185
  // create IO
2190
2186
  var fileIO = new dwv.io.FilesLoader();
2191
2187
  // load data
2192
- loadData([file], fileIO, 'state', options);
2188
+ loadData([file], fileIO, 'state');
2193
2189
  }
2194
2190
 
2195
2191
  /**
@@ -2222,20 +2218,8 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2222
2218
  loadtype: loadType,
2223
2219
  };
2224
2220
 
2225
- // check if timepoint
2226
- var hasTimepoint = false;
2227
- if (typeof options !== 'undefined' &&
2228
- typeof options.timepoint !== 'undefined') {
2229
- hasTimepoint = true;
2230
- }
2231
-
2232
- var loadId = null;
2233
- if (hasTimepoint) {
2234
- loadId = options.timepoint.dataId;
2235
- eventInfo.timepoint = options.timepoint;
2236
- } else {
2237
- loadId = getNextLoadId();
2238
- }
2221
+ // load id
2222
+ var loadId = getNextLoadId();
2239
2223
  eventInfo.loadid = loadId;
2240
2224
 
2241
2225
  // set callbacks
@@ -2257,9 +2241,6 @@ dwv.ctrl.LoadController = function (defaultCharacterSet) {
2257
2241
  if (typeof currentLoaders[loadId] !== 'undefined') {
2258
2242
  eventInfoItem.isfirstitem = currentLoaders[loadId].isFirstItem;
2259
2243
  }
2260
- if (hasTimepoint) {
2261
- eventInfoItem.timepoint = options.timepoint;
2262
- }
2263
2244
  // callback
2264
2245
  augmentCallbackEvent(self.onloaditem, eventInfoItem)(event);
2265
2246
  // update loader
@@ -2456,50 +2437,13 @@ dwv.ctrl.ToolboxController = function (toolList) {
2456
2437
  };
2457
2438
 
2458
2439
  /**
2459
- * Set the selected shape.
2460
- *
2461
- * @param {string} name The name of the shape.
2462
- */
2463
- this.setSelectedShape = function (name) {
2464
- this.getSelectedTool().setShapeName(name);
2465
- };
2466
-
2467
- /**
2468
- * Set the selected filter.
2469
- *
2470
- * @param {string} name The name of the filter.
2471
- */
2472
- this.setSelectedFilter = function (name) {
2473
- this.getSelectedTool().setSelectedFilter(name);
2474
- };
2475
-
2476
- /**
2477
- * Run the selected filter.
2478
- */
2479
- this.runSelectedFilter = function () {
2480
- this.getSelectedTool().getSelectedFilter().run();
2481
- };
2482
-
2483
- /**
2484
- * Set the tool line colour.
2485
- *
2486
- * @param {string} colour The colour.
2487
- */
2488
- this.setLineColour = function (colour) {
2489
- this.getSelectedTool().setLineColour(colour);
2490
- };
2491
-
2492
- /**
2493
- * Set the tool range.
2440
+ * Set the selected tool live features.
2494
2441
  *
2495
- * @param {object} range The new range of the data.
2442
+ * @param {object} list The list of features.
2496
2443
  */
2497
- this.setRange = function (range) {
2498
- // seems like jquery is checking if the method exists before it
2499
- // is used...
2500
- if (this.getSelectedTool() &&
2501
- this.getSelectedTool().getSelectedFilter()) {
2502
- this.getSelectedTool().getSelectedFilter().run(range);
2444
+ this.setToolFeatures = function (list) {
2445
+ if (this.getSelectedTool()) {
2446
+ this.getSelectedTool().setFeatures(list);
2503
2447
  }
2504
2448
  };
2505
2449
 
@@ -2616,9 +2560,15 @@ dwv.ctrl.ViewController = function (view) {
2616
2560
  // third dimension player ID (created by setInterval)
2617
2561
  var playerID = null;
2618
2562
 
2563
+ // check view
2564
+ if (typeof view.getImage() === 'undefined') {
2565
+ throw new Error('View does not have an image, cannot setup controller');
2566
+ }
2567
+
2619
2568
  // setup the plane helper
2620
2569
  var planeHelper = new dwv.image.PlaneHelper(
2621
- view.getImage().getGeometry().getSpacing(),
2570
+ view.getImage().getGeometry().getRealSpacing(),
2571
+ view.getImage().getGeometry().getOrientation(),
2622
2572
  view.getOrientation()
2623
2573
  );
2624
2574
 
@@ -2743,6 +2693,10 @@ dwv.ctrl.ViewController = function (view) {
2743
2693
  return view.getCurrentIndex().get(view.getScrollIndex());
2744
2694
  };
2745
2695
 
2696
+ this.getOrigin = function (position) {
2697
+ return view.getOrigin(position);
2698
+ };
2699
+
2746
2700
  /**
2747
2701
  * Get the current scroll position value.
2748
2702
  *
@@ -2888,7 +2842,63 @@ dwv.ctrl.ViewController = function (view) {
2888
2842
  * @returns {dwv.image.Size} The size.
2889
2843
  */
2890
2844
  this.getImageSize = function () {
2891
- return view.getImage().getGeometry().getSize();
2845
+ return view.getImage().getGeometry().getSize(view.getOrientation());
2846
+ };
2847
+
2848
+ /**
2849
+ * Get the image world (mm) 2D size.
2850
+ *
2851
+ * @returns {object} The 2D size as {x,y}.
2852
+ */
2853
+ this.getImageWorldSize = function () {
2854
+ var geometry = view.getImage().getGeometry();
2855
+ var size = geometry.getSize(view.getOrientation()).get2D();
2856
+ var spacing = geometry.getSpacing(view.getOrientation()).get2D();
2857
+ return {
2858
+ x: size.x * spacing.x,
2859
+ y: size.y * spacing.y
2860
+ };
2861
+ };
2862
+
2863
+ /**
2864
+ * Get the image rescaled data range.
2865
+ *
2866
+ * @returns {object} The range as {min, max}.
2867
+ */
2868
+ this.getImageRescaledDataRange = function () {
2869
+ return view.getImage().getRescaledDataRange();
2870
+ };
2871
+
2872
+ /**
2873
+ * Compare the input meta data to the associated image one.
2874
+ *
2875
+ * @param {object} meta The meta data.
2876
+ * @returns {boolean} True if the associated image has equal meta data.
2877
+ */
2878
+ this.equalImageMeta = function (meta) {
2879
+ var imageMeta = view.getImage().getMeta();
2880
+ // loop through input meta keys
2881
+ var metaKeys = Object.keys(meta);
2882
+ for (var i = 0; i < metaKeys.length; ++i) {
2883
+ var metaKey = metaKeys[i];
2884
+ if (typeof imageMeta[metaKey] === 'undefined') {
2885
+ return false;
2886
+ }
2887
+ if (imageMeta[metaKey] !== meta[metaKey]) {
2888
+ return false;
2889
+ }
2890
+ }
2891
+ return true;
2892
+ };
2893
+
2894
+ /**
2895
+ * Check is the provided position can be set.
2896
+ *
2897
+ * @param {dwv.math.Point} position The position.
2898
+ * @returns {boolean} True is the position is in bounds.
2899
+ */
2900
+ this.canSetPosition = function (position) {
2901
+ return view.canSetPosition(position);
2892
2902
  };
2893
2903
 
2894
2904
  /**
@@ -2910,8 +2920,18 @@ dwv.ctrl.ViewController = function (view) {
2910
2920
  * @returns {boolean} False if not in bounds.
2911
2921
  */
2912
2922
  this.setCurrentPosition2D = function (x, y) {
2913
- return view.setCurrentPosition(
2914
- this.getPositionFromPlanePoint({x: x, y: y}));
2923
+ // keep third direction
2924
+ var k = this.getCurrentScrollIndexValue();
2925
+ var planePoint = new dwv.math.Point3D(x, y, k);
2926
+ // de-orient
2927
+ var point = planeHelper.getImageOrientedVector3D(planePoint);
2928
+ // ~indexToWorld to not loose precision
2929
+ var geometry = view.getImage().getGeometry();
2930
+ var point3D = geometry.pointToWorld(point);
2931
+ // merge with current position to keep extra dimensions
2932
+ var position = this.getCurrentPosition().mergeWith3D(point3D);
2933
+
2934
+ return view.setCurrentPosition(position);
2915
2935
  };
2916
2936
 
2917
2937
  /**
@@ -2936,7 +2956,7 @@ dwv.ctrl.ViewController = function (view) {
2936
2956
  var k = this.getCurrentScrollIndexValue();
2937
2957
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2938
2958
  // de-orient
2939
- var point = planeHelper.getDeOrientedVector3D(planePoint);
2959
+ var point = planeHelper.getImageOrientedVector3D(planePoint);
2940
2960
  // ~indexToWorld to not loose precision
2941
2961
  var geometry = view.getImage().getGeometry();
2942
2962
  var point3D = geometry.pointToWorld(point);
@@ -2956,10 +2976,10 @@ dwv.ctrl.ViewController = function (view) {
2956
2976
  var k = this.getCurrentScrollIndexValue();
2957
2977
  var planePoint = new dwv.math.Point3D(point2D.x, point2D.y, k);
2958
2978
  // de-orient
2959
- var point = planeHelper.getDeOrientedVector3D(planePoint);
2979
+ var point = planeHelper.getTargetDeOrientedVector3D(planePoint);
2960
2980
  // ~indexToWorld to not loose precision
2961
2981
  var geometry = view.getImage().getGeometry();
2962
- var spacing = geometry.getSpacing();
2982
+ var spacing = geometry.getRealSpacing();
2963
2983
  return new dwv.math.Point3D(
2964
2984
  point.getX() * spacing.get(0),
2965
2985
  point.getY() * spacing.get(1),
@@ -4427,6 +4447,16 @@ dwv.dicom.DicomElementsWrapper.prototype.getPixelSpacing = function () {
4427
4447
  return new dwv.image.Spacing([columnSpacing, rowSpacing, 1]);
4428
4448
  };
4429
4449
 
4450
+ /**
4451
+ * Get the time.
4452
+ *
4453
+ * @returns {number|undefined} The time value if available.
4454
+ */
4455
+ dwv.dicom.DicomElementsWrapper.prototype.getTime = function () {
4456
+ // default returns undefined
4457
+ return undefined;
4458
+ };
4459
+
4430
4460
  /**
4431
4461
  * Get the file list from a DICOMDIR
4432
4462
  *
@@ -4503,7 +4533,7 @@ dwv.dicom = dwv.dicom || {};
4503
4533
  * @returns {string} The version of the library.
4504
4534
  */
4505
4535
  dwv.getVersion = function () {
4506
- return '0.31.0-beta.1';
4536
+ return '0.31.0-beta.10';
4507
4537
  };
4508
4538
 
4509
4539
  /**
@@ -5970,11 +6000,15 @@ dwv.dicom = dwv.dicom || {};
5970
6000
  * @returns {string} The dwv UID prefix.
5971
6001
  */
5972
6002
  dwv.dicom.getDwvUIDPrefix = function () {
5973
- return '1.2.826.0.1.3680043.9.7278.1.';
6003
+ return '1.2.826.0.1.3680043.9.7278.1';
5974
6004
  };
5975
6005
 
6006
+ // local generated uid counter
6007
+ var _uidCount = 0;
6008
+
5976
6009
  /**
5977
6010
  * Get a UID for a DICOM tag.
6011
+ * Note: Use https://github.com/uuidjs/uuid?
5978
6012
  *
5979
6013
  * @see http://dicom.nema.org/dicom/2013/output/chtml/part05/chapter_9.html
5980
6014
  * @see http://dicomiseasy.blogspot.com/2011/12/chapter-4-dicom-objects-in-chapter-3.html
@@ -5983,17 +6017,34 @@ dwv.dicom.getDwvUIDPrefix = function () {
5983
6017
  * @returns {string} The corresponding UID.
5984
6018
  */
5985
6019
  dwv.dicom.getUID = function (tagName) {
5986
- var uid = dwv.dicom.getDwvUIDPrefix();
6020
+ var prefix = dwv.dicom.getDwvUIDPrefix() + '.';
6021
+ var uid = '';
5987
6022
  if (tagName === 'ImplementationClassUID') {
5988
- uid += dwv.getVersion();
5989
- } else if (tagName === 'SOPInstanceUID') {
5990
- for (var i = 0; i < tagName.length; ++i) {
5991
- uid += tagName.charCodeAt(i);
5992
- }
5993
- // add date (only numbers)
5994
- uid += '.' + (new Date()).toISOString().replace(/\D/g, '');
6023
+ uid = prefix + dwv.getVersion();
5995
6024
  } else {
5996
- throw new Error('Don\'t know how to generate a UID for the tag ' + tagName);
6025
+ // date (only numbers), do not keep milliseconds
6026
+ var date = (new Date()).toISOString().replace(/\D/g, '');
6027
+ var datePart = '.' + date.substring(0, 14);
6028
+ // count
6029
+ _uidCount += 1;
6030
+ var countPart = '.' + _uidCount;
6031
+
6032
+ // uid = prefix . tag . date . count
6033
+ uid = prefix;
6034
+
6035
+ // limit tag part to not exceed 64 length
6036
+ var nonTagLength = prefix.length + countPart.length + datePart.length;
6037
+ var leni = Math.min(tagName.length, 64 - nonTagLength);
6038
+ if (leni > 1) {
6039
+ var tagNumber = '';
6040
+ for (var i = 0; i < leni; ++i) {
6041
+ tagNumber += tagName.charCodeAt(i);
6042
+ }
6043
+ uid += tagNumber.substring(0, leni);
6044
+ }
6045
+
6046
+ // finish
6047
+ uid += datePart + countPart;
5997
6048
  }
5998
6049
  return uid;
5999
6050
  };
@@ -6015,10 +6066,24 @@ dwv.dicom.isEven = function (number) {
6015
6066
  * @returns {boolean} True if the VR is a non string one.
6016
6067
  */
6017
6068
  dwv.dicom.isNonStringVr = function (vr) {
6018
- return vr === 'UN' || vr === 'OB' || vr === 'OW' ||
6019
- vr === 'OF' || vr === 'OD' || vr === 'US' || vr === 'SS' ||
6020
- vr === 'UL' || vr === 'SL' || vr === 'FL' || vr === 'FD' ||
6021
- vr === 'SQ' || vr === 'AT';
6069
+ return vr === 'UN' ||
6070
+ vr === 'SQ' ||
6071
+ vr === 'AT' ||
6072
+ dwv.dicom.isTypedArrayVr(vr);
6073
+ };
6074
+
6075
+ /**
6076
+ * Is the input VR a VR that stores data in a typed array.
6077
+ *
6078
+ * @param {string} vr The element VR.
6079
+ * @returns {boolean} True if the VR is a typed array one.
6080
+ */
6081
+ dwv.dicom.isTypedArrayVr = function (vr) {
6082
+ return vr === 'OB' || vr === 'OW' ||
6083
+ vr === 'OF' || vr === 'OD' ||
6084
+ vr === 'US' || vr === 'SS' ||
6085
+ vr === 'UL' || vr === 'SL' ||
6086
+ vr === 'FL' || vr === 'FD';
6022
6087
  };
6023
6088
 
6024
6089
  /**
@@ -6815,43 +6880,55 @@ dwv.dicom.setElementValue = function (element, value, isImplicit) {
6815
6880
  // set the value and calculate size
6816
6881
  size = 0;
6817
6882
  var paddedValue = dwv.dicom.padElementValue(element, value);
6818
- if (value instanceof Array) {
6883
+ if (element.vr === 'AT') {
6819
6884
  element.value = paddedValue;
6820
- for (var k = 0; k < paddedValue.length; ++k) {
6821
- // spearator
6822
- if (k !== 0) {
6823
- size += 1;
6824
- }
6825
- // value
6826
- size += paddedValue[k].toString().length;
6885
+ size = 4;
6886
+ } else if (dwv.dicom.isTypedArrayVr(element.vr)) {
6887
+ // convert non array (number) value to array
6888
+ if (typeof value.length === 'undefined') {
6889
+ element.value = [paddedValue];
6890
+ } else {
6891
+ element.value = paddedValue;
6892
+ }
6893
+ size = element.value.length;
6894
+
6895
+ // convert size to bytes
6896
+ if (element.vr === 'US' || element.vr === 'OW') {
6897
+ size *= Uint16Array.BYTES_PER_ELEMENT;
6898
+ } else if (element.vr === 'SS') {
6899
+ size *= Int16Array.BYTES_PER_ELEMENT;
6900
+ } else if (element.vr === 'UL') {
6901
+ size *= Uint32Array.BYTES_PER_ELEMENT;
6902
+ } else if (element.vr === 'SL') {
6903
+ size *= Int32Array.BYTES_PER_ELEMENT;
6904
+ } else if (element.vr === 'FL') {
6905
+ size *= Float32Array.BYTES_PER_ELEMENT;
6906
+ } else if (element.vr === 'FD') {
6907
+ size *= Float64Array.BYTES_PER_ELEMENT;
6827
6908
  }
6828
6909
  } else {
6829
- element.value = [paddedValue];
6830
- if (typeof paddedValue !== 'undefined' &&
6831
- typeof paddedValue.length !== 'undefined') {
6832
- size = paddedValue.length;
6910
+ if (value instanceof Array) {
6911
+ element.value = paddedValue;
6912
+ for (var k = 0; k < paddedValue.length; ++k) {
6913
+ // separator
6914
+ if (k !== 0) {
6915
+ size += 1;
6916
+ }
6917
+ // value
6918
+ size += paddedValue[k].toString().length;
6919
+ }
6833
6920
  } else {
6834
- // numbers
6835
- size = 1;
6836
- }
6837
- }
6838
-
6839
- // convert size to bytes
6840
- if (element.vr === 'US' || element.vr === 'OW') {
6841
- size *= Uint16Array.BYTES_PER_ELEMENT;
6842
- } else if (element.vr === 'SS') {
6843
- size *= Int16Array.BYTES_PER_ELEMENT;
6844
- } else if (element.vr === 'UL') {
6845
- size *= Uint32Array.BYTES_PER_ELEMENT;
6846
- } else if (element.vr === 'SL') {
6847
- size *= Int32Array.BYTES_PER_ELEMENT;
6848
- } else if (element.vr === 'FL') {
6849
- size *= Float32Array.BYTES_PER_ELEMENT;
6850
- } else if (element.vr === 'FD') {
6851
- size *= Float64Array.BYTES_PER_ELEMENT;
6852
- } else {
6853
- size *= Uint8Array.BYTES_PER_ELEMENT;
6921
+ element.value = [paddedValue];
6922
+ if (typeof paddedValue !== 'undefined' &&
6923
+ typeof paddedValue.length !== 'undefined') {
6924
+ size = paddedValue.length;
6925
+ } else {
6926
+ // numbers
6927
+ size = 1;
6928
+ }
6929
+ }
6854
6930
  }
6931
+
6855
6932
  element.vl = size;
6856
6933
  }
6857
6934
 
@@ -11250,6 +11327,22 @@ dwv.gui.DrawLayer = function (containerDiv) {
11250
11327
  */
11251
11328
  var baseOffset = {x: 0, y: 0};
11252
11329
 
11330
+ /**
11331
+ * The view offset.
11332
+ *
11333
+ * @private
11334
+ * @type {object}
11335
+ */
11336
+ var viewOffset = {x: 0, y: 0};
11337
+
11338
+ /**
11339
+ * The zoom offset.
11340
+ *
11341
+ * @private
11342
+ * @type {object}
11343
+ */
11344
+ var zoomOffset = {x: 0, y: 0};
11345
+
11253
11346
  /**
11254
11347
  * The draw controller.
11255
11348
  *
@@ -11369,16 +11462,59 @@ dwv.gui.DrawLayer = function (containerDiv) {
11369
11462
  * Set the layer scale.
11370
11463
  *
11371
11464
  * @param {object} newScale The scale as {x,y}.
11465
+ * @param {dwv.math.Point3D} center The scale center.
11372
11466
  */
11373
- this.setScale = function (newScale) {
11374
- var orientedNewScale = planeHelper.getOrientedXYZ(newScale);
11375
- var fullScale = {
11467
+ this.setScale = function (newScale, center) {
11468
+ var orientedNewScale = planeHelper.getTargetOrientedXYZ(newScale);
11469
+ var finalNewScale = {
11376
11470
  x: fitScale.x * orientedNewScale.x,
11377
11471
  y: fitScale.y * orientedNewScale.y
11378
11472
  };
11379
- konvaStage.scale(fullScale);
11380
- // update labelss
11381
- updateLabelScale(fullScale);
11473
+
11474
+ var offset = konvaStage.offset();
11475
+
11476
+ if (newScale.x === 1 &&
11477
+ newScale.y === 1 &&
11478
+ newScale.z === 1) {
11479
+ // reset zoom offset for scale=1
11480
+ var resetOffset = {
11481
+ x: offset.x - zoomOffset.x,
11482
+ y: offset.y - zoomOffset.y
11483
+ };
11484
+ // store new offset
11485
+ zoomOffset = {x: 0, y: 0};
11486
+ konvaStage.offset(resetOffset);
11487
+ } else {
11488
+ if (typeof center !== 'undefined') {
11489
+ var worldCenter = planeHelper.getPlaneOffsetFromOffset3D({
11490
+ x: center.getX(),
11491
+ y: center.getY(),
11492
+ z: center.getZ()
11493
+ });
11494
+ // center was obtained with viewLayer.displayToPlanePosNoBase
11495
+ // compensated for baseOffset
11496
+ // TODO: justify...
11497
+ worldCenter = {
11498
+ x: worldCenter.x + baseOffset.x,
11499
+ y: worldCenter.y + baseOffset.y
11500
+ };
11501
+
11502
+ var newOffset = dwv.gui.getScaledOffset(
11503
+ offset, konvaStage.scale(), finalNewScale, worldCenter);
11504
+
11505
+ var newZoomOffset = {
11506
+ x: zoomOffset.x + newOffset.x - offset.x,
11507
+ y: zoomOffset.y + newOffset.y - offset.y
11508
+ };
11509
+ // store new offset
11510
+ zoomOffset = newZoomOffset;
11511
+ konvaStage.offset(newOffset);
11512
+ }
11513
+ }
11514
+
11515
+ konvaStage.scale(finalNewScale);
11516
+ // update labels
11517
+ updateLabelScale(finalNewScale);
11382
11518
  };
11383
11519
 
11384
11520
  /**
@@ -11389,27 +11525,37 @@ dwv.gui.DrawLayer = function (containerDiv) {
11389
11525
  this.setOffset = function (newOffset) {
11390
11526
  var planeNewOffset = planeHelper.getPlaneOffsetFromOffset3D(newOffset);
11391
11527
  konvaStage.offset({
11392
- x: baseOffset.x + planeNewOffset.x,
11393
- y: baseOffset.y + planeNewOffset.y
11528
+ x: viewOffset.x + baseOffset.x + zoomOffset.x + planeNewOffset.x,
11529
+ y: viewOffset.y + baseOffset.y + zoomOffset.y + planeNewOffset.y
11394
11530
  });
11395
11531
  };
11396
11532
 
11397
11533
  /**
11398
- * Set the base layer offset. Resets the layer offset.
11534
+ * Set the base layer offset. Updates the layer offset.
11399
11535
  *
11400
- * @param {object} off The offset as {x,y}.
11536
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
11537
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
11538
+ * @returns {boolean} True if the offset was updated.
11401
11539
  */
11402
- this.setBaseOffset = function (off) {
11403
- baseOffset = planeHelper.getPlaneOffsetFromOffset3D({
11404
- x: off.getX(),
11405
- y: off.getY(),
11406
- z: off.getZ()
11407
- });
11408
- // reset offset
11409
- konvaStage.offset({
11410
- x: baseOffset.x,
11411
- y: baseOffset.y
11540
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
11541
+ var scrollIndex = planeHelper.getNativeScrollIndex();
11542
+ var newOffset = planeHelper.getPlaneOffsetFromOffset3D({
11543
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
11544
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
11545
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
11412
11546
  });
11547
+ var needsUpdate = baseOffset.x !== newOffset.x ||
11548
+ baseOffset.y !== newOffset.y;
11549
+ // reset offset if needed
11550
+ if (needsUpdate) {
11551
+ var offset = konvaStage.offset();
11552
+ konvaStage.offset({
11553
+ x: offset.x - baseOffset.x + newOffset.x,
11554
+ y: offset.y - baseOffset.y + newOffset.y
11555
+ });
11556
+ baseOffset = newOffset;
11557
+ }
11558
+ return needsUpdate;
11413
11559
  };
11414
11560
 
11415
11561
  /**
@@ -11478,18 +11624,38 @@ dwv.gui.DrawLayer = function (containerDiv) {
11478
11624
  *
11479
11625
  * @param {number} fitScale1D The 1D fit scale.
11480
11626
  * @param {object} fitSize The fit size as {x,y}.
11627
+ * @param {object} fitOffset The fit offset as {x,y}.
11481
11628
  */
11482
- this.fitToContainer = function (fitScale1D, fitSize) {
11629
+ this.fitToContainer = function (fitScale1D, fitSize, fitOffset) {
11630
+ // update konva
11631
+ konvaStage.setWidth(fitSize.x);
11632
+ konvaStage.setHeight(fitSize.y);
11633
+
11634
+ // previous scale without fit
11635
+ var previousScale = {
11636
+ x: konvaStage.scale().x / fitScale.x,
11637
+ y: konvaStage.scale().y / fitScale.y
11638
+ };
11483
11639
  // update fit scale
11484
11640
  fitScale = {
11485
11641
  x: fitScale1D * baseSpacing.x,
11486
11642
  y: fitScale1D * baseSpacing.y
11487
11643
  };
11488
- // update konva
11489
- konvaStage.setWidth(fitSize.x);
11490
- konvaStage.setHeight(fitSize.y);
11491
- // reset scale
11492
- this.setScale({x: 1, y: 1, z: 1});
11644
+ // update scale
11645
+ konvaStage.scale({
11646
+ x: previousScale.x * fitScale.x,
11647
+ y: previousScale.y * fitScale.y
11648
+ });
11649
+
11650
+ // update offsets
11651
+ viewOffset = {
11652
+ x: fitOffset.x / fitScale.x,
11653
+ y: fitOffset.y / fitScale.y
11654
+ };
11655
+ konvaStage.offset({
11656
+ x: viewOffset.x + baseOffset.x + zoomOffset.x,
11657
+ y: viewOffset.y + baseOffset.y + zoomOffset.y
11658
+ });
11493
11659
  };
11494
11660
 
11495
11661
  /**
@@ -11525,10 +11691,13 @@ dwv.gui.DrawLayer = function (containerDiv) {
11525
11691
  *
11526
11692
  * @param {dwv.math.Point} position The new position.
11527
11693
  * @param {dwv.math.Index} index The new index.
11694
+ * @returns {boolean} True if the position was updated.
11528
11695
  */
11529
11696
  this.setCurrentPosition = function (position, index) {
11530
11697
  this.getDrawController().activateDrawLayer(
11531
11698
  index, planeHelper.getScrollIndex());
11699
+ // TODO: add check
11700
+ return true;
11532
11701
  };
11533
11702
 
11534
11703
  /**
@@ -11817,27 +11986,74 @@ dwv.gui.getLayerDetailsFromEvent = function (event) {
11817
11986
  };
11818
11987
 
11819
11988
  /**
11820
- * Get a view orientation according to an image geometry (with its orientation)
11821
- * and target orientation.
11989
+ * Get the view orientation according to an image and target orientation.
11990
+ * The view orientation is used to go from target to image space.
11822
11991
  *
11823
- * @param {dwv.image.Geometry} imageGeometry The image geometry.
11992
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
11824
11993
  * @param {dwv.math.Matrix33} targetOrientation The target orientation.
11825
11994
  * @returns {dwv.math.Matrix33} The view orientation.
11826
11995
  */
11827
- dwv.gui.getViewOrientation = function (imageGeometry, targetOrientation) {
11996
+ dwv.gui.getViewOrientation = function (imageOrientation, targetOrientation) {
11828
11997
  var viewOrientation = dwv.math.getIdentityMat33();
11829
11998
  if (typeof targetOrientation !== 'undefined') {
11830
- // image orientation as one and zeros
11831
- // -> view orientation is one and zeros
11832
- var imgOrientation = imageGeometry.getOrientation().asOneAndZeros();
11833
- // imgOrientation * viewOrientation = targetOrientation
11834
- // -> viewOrientation = inv(imgOrientation) * targetOrientation
11999
+ // i: image, v: view, t: target, O: orientation, P: point
12000
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
12001
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
12002
+ // -> Ov = (Oi)-1 * Ot
12003
+ // TODO: asOneAndZeros simplifies but not nice...
11835
12004
  viewOrientation =
11836
- imgOrientation.getInverse().multiply(targetOrientation);
12005
+ imageOrientation.asOneAndZeros().getInverse().multiply(targetOrientation);
11837
12006
  }
11838
12007
  return viewOrientation;
11839
12008
  };
11840
12009
 
12010
+ /**
12011
+ * Get the target orientation according to an image and view orientation.
12012
+ * The target orientation is used to go from target to real space.
12013
+ *
12014
+ * @param {dwv.math.Matrix33} imageOrientation The image geometry.
12015
+ * @param {dwv.math.Matrix33} viewOrientation The view orientation.
12016
+ * @returns {dwv.math.Matrix33} The target orientation.
12017
+ */
12018
+ dwv.gui.getTargetOrientation = function (imageOrientation, viewOrientation) {
12019
+ // i: image, v: view, t: target, O: orientation, P: point
12020
+ // [Img] -- Oi --> [Real] <-- Ot -- [Target]
12021
+ // Pi = (Oi)-1 * Ot * Pt = Ov * Pt
12022
+ // -> Ot = Oi * Ov
12023
+ // note: asOneAndZeros as in dwv.gui.getViewOrientation...
12024
+ return imageOrientation.asOneAndZeros().multiply(viewOrientation);
12025
+ };
12026
+
12027
+ /**
12028
+ * Get a scaled offset to adapt to new scale and such as the input center
12029
+ * stays at the same position.
12030
+ *
12031
+ * @param {object} offset The previous offset as {x,y}.
12032
+ * @param {object} scale The previous scale as {x,y}.
12033
+ * @param {object} newScale The new scale as {x,y}.
12034
+ * @param {object} center The scale center as {x,y}.
12035
+ * @returns {object} The scaled offset as {x,y}.
12036
+ */
12037
+ dwv.gui.getScaledOffset = function (offset, scale, newScale, center) {
12038
+ // worldPoint = indexPoint / scale + offset
12039
+ //=> indexPoint = (worldPoint - offset ) * scale
12040
+
12041
+ // plane center should stay the same:
12042
+ // indexCenter / newScale + newOffset =
12043
+ // indexCenter / oldScale + oldOffset
12044
+ //=> newOffset = indexCenter / oldScale + oldOffset -
12045
+ // indexCenter / newScale
12046
+ //=> newOffset = worldCenter - indexCenter / newScale
12047
+ var indexCenter = {
12048
+ x: (center.x - offset.x) * scale.x,
12049
+ y: (center.y - offset.y) * scale.y
12050
+ };
12051
+ return {
12052
+ x: center.x - (indexCenter.x / newScale.x),
12053
+ y: center.y - (indexCenter.y / newScale.y)
12054
+ };
12055
+ };
12056
+
11841
12057
  /**
11842
12058
  * Layer group.
11843
12059
  *
@@ -12033,13 +12249,46 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12033
12249
  };
12034
12250
 
12035
12251
  /**
12036
- * Get the active draw layer.
12252
+ * Search view layers for equal imae meta data.
12037
12253
  *
12038
- * @returns {object} The layer.
12254
+ * @param {object} meta The meta data to find.
12255
+ * @returns {Array} The list of view layers that contain matched data.
12039
12256
  */
12040
- this.getActiveDrawLayer = function () {
12041
- return layers[activeDrawLayerIndex];
12042
- };
12257
+ this.searchViewLayers = function (meta) {
12258
+ var res = [];
12259
+ for (var i = 0; i < layers.length; ++i) {
12260
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12261
+ if (layers[i].getViewController().equalImageMeta(meta)) {
12262
+ res.push(layers[i]);
12263
+ }
12264
+ }
12265
+ }
12266
+ return res;
12267
+ };
12268
+
12269
+ /**
12270
+ * Get the view layers data indices.
12271
+ *
12272
+ * @returns {Array} The list of indices.
12273
+ */
12274
+ this.getViewDataIndices = function () {
12275
+ var res = [];
12276
+ for (var i = 0; i < layers.length; ++i) {
12277
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12278
+ res.push(layers[i].getDataIndex());
12279
+ }
12280
+ }
12281
+ return res;
12282
+ };
12283
+
12284
+ /**
12285
+ * Get the active draw layer.
12286
+ *
12287
+ * @returns {object} The layer.
12288
+ */
12289
+ this.getActiveDrawLayer = function () {
12290
+ return layers[activeDrawLayerIndex];
12291
+ };
12043
12292
 
12044
12293
  /**
12045
12294
  * Get the draw layers associated to a data index.
@@ -12215,10 +12464,53 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12215
12464
 
12216
12465
  var index = new dwv.math.Index(event.value[0]);
12217
12466
  var position = new dwv.math.Point(event.value[1]);
12467
+ // origin of the first view layer
12468
+ var baseViewLayerOrigin0 = null;
12469
+ var baseViewLayerOrigin = null;
12218
12470
  // update position for all layers except the source one
12219
12471
  for (var i = 0; i < layers.length; ++i) {
12472
+
12473
+ // update base offset (does not trigger redraw)
12474
+ // TODO check draw layers update
12475
+ var hasSetOffset = false;
12476
+ if (layers[i] instanceof dwv.gui.ViewLayer) {
12477
+ var vc = layers[i].getViewController();
12478
+ // origin0 should always be there
12479
+ var origin0 = vc.getOrigin();
12480
+ // depending on position, origin could be undefined
12481
+ var origin = vc.getOrigin(position);
12482
+
12483
+ if (!baseViewLayerOrigin) {
12484
+ baseViewLayerOrigin0 = origin0;
12485
+ baseViewLayerOrigin = origin;
12486
+ } else {
12487
+ if (vc.canSetPosition(position) &&
12488
+ typeof origin !== 'undefined') {
12489
+ // TODO: compensate for possible different orientation between views
12490
+ // TODO: check why -z...
12491
+
12492
+ var scrollDiff = baseViewLayerOrigin0.minus(origin0);
12493
+ var scrollOffset = new dwv.math.Vector3D(
12494
+ scrollDiff.getX(), scrollDiff.getY(), -1 * scrollDiff.getZ());
12495
+
12496
+ var planeDiff = baseViewLayerOrigin.minus(origin);
12497
+ var planeOffset = new dwv.math.Vector3D(
12498
+ planeDiff.getX(), planeDiff.getY(), -1 * planeDiff.getZ());
12499
+
12500
+ hasSetOffset = layers[i].setBaseOffset(scrollOffset, planeOffset);
12501
+ }
12502
+ }
12503
+ }
12504
+
12505
+ // update position (triggers redraw)
12506
+ var hasSetPos = false;
12220
12507
  if (layers[i].getId() !== event.srclayerid) {
12221
- layers[i].setCurrentPosition(position, index);
12508
+ hasSetPos = layers[i].setCurrentPosition(position, index);
12509
+ }
12510
+
12511
+ // force redraw if needed
12512
+ if (!hasSetPos && hasSetOffset) {
12513
+ layers[i].draw();
12222
12514
  }
12223
12515
  }
12224
12516
 
@@ -12233,32 +12525,81 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12233
12525
  };
12234
12526
 
12235
12527
  /**
12236
- * Fit the display to the size of the container.
12237
- * To be called once the image is loaded.
12528
+ * Calculate the fit scale: the scale that fits the largest data.
12238
12529
  *
12239
- * @param {object} realSize 2D real size (in mm) to fit provided as {x,y}.
12530
+ * @returns {number|undefined} The fit scale.
12240
12531
  */
12241
- this.fitToContainer = function (realSize) {
12242
- // check container size
12532
+ this.calculateFitScale = function () {
12533
+ // check container
12243
12534
  if (containerDiv.offsetWidth === 0 &&
12244
12535
  containerDiv.offsetHeight === 0) {
12245
12536
  throw new Error('Cannot fit to zero sized container.');
12246
12537
  }
12247
- // find best fit
12248
- var fitScale = Math.min(
12249
- containerDiv.offsetWidth / realSize.x,
12250
- containerDiv.offsetHeight / realSize.y
12538
+ // get max size
12539
+ var maxSize = this.getMaxSize();
12540
+ if (typeof maxSize === 'undefined') {
12541
+ return undefined;
12542
+ }
12543
+ // return best fit
12544
+ return Math.min(
12545
+ containerDiv.offsetWidth / maxSize.x,
12546
+ containerDiv.offsetHeight / maxSize.y
12251
12547
  );
12252
- var fitSize = {
12253
- x: Math.floor(realSize.x * fitScale),
12254
- y: Math.floor(realSize.y * fitScale)
12548
+ };
12549
+
12550
+ /**
12551
+ * Set the layer group fit scale.
12552
+ *
12553
+ * @param {number} scaleIn The fit scale.
12554
+ */
12555
+ this.setFitScale = function (scaleIn) {
12556
+ // get maximum size
12557
+ var maxSize = this.getMaxSize();
12558
+ // exit if none
12559
+ if (typeof maxSize === 'undefined') {
12560
+ return;
12561
+ }
12562
+
12563
+ var containerSize = {
12564
+ x: containerDiv.offsetWidth,
12565
+ y: containerDiv.offsetHeight
12566
+ };
12567
+ // offset to keep data centered
12568
+ var fitOffset = {
12569
+ x: -0.5 * (containerSize.x - Math.floor(maxSize.x * scaleIn)),
12570
+ y: -0.5 * (containerSize.y - Math.floor(maxSize.y * scaleIn))
12255
12571
  };
12572
+
12256
12573
  // apply to layers
12257
12574
  for (var j = 0; j < layers.length; ++j) {
12258
- layers[j].fitToContainer(fitScale, fitSize);
12575
+ layers[j].fitToContainer(scaleIn, containerSize, fitOffset);
12259
12576
  }
12260
12577
  };
12261
12578
 
12579
+ /**
12580
+ * Get the largest data size.
12581
+ *
12582
+ * @returns {object|undefined} The largest size as {x,y}.
12583
+ */
12584
+ this.getMaxSize = function () {
12585
+ var maxSize = {x: 0, y: 0};
12586
+ for (var j = 0; j < layers.length; ++j) {
12587
+ if (layers[j] instanceof dwv.gui.ViewLayer) {
12588
+ var size = layers[j].getImageWorldSize();
12589
+ if (size.x > maxSize.x) {
12590
+ maxSize.x = size.x;
12591
+ }
12592
+ if (size.y > maxSize.y) {
12593
+ maxSize.y = size.y;
12594
+ }
12595
+ }
12596
+ }
12597
+ if (maxSize.x === 0 && maxSize.y === 0) {
12598
+ maxSize = undefined;
12599
+ }
12600
+ return maxSize;
12601
+ };
12602
+
12262
12603
  /**
12263
12604
  * Add scale to the layers. Scale cannot go lower than 0.1.
12264
12605
  *
@@ -12271,35 +12612,33 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12271
12612
  y: scale.y * (1 + scaleStep),
12272
12613
  z: scale.z * (1 + scaleStep)
12273
12614
  };
12274
- var centerPlane = {
12275
- x: (center.getX() - offset.x) * scale.x,
12276
- y: (center.getY() - offset.y) * scale.y,
12277
- z: (center.getZ() - offset.z) * scale.z
12278
- };
12279
- // center should stay the same:
12280
- // center / newScale + newOffset = center / oldScale + oldOffset
12281
- // => newOffset = center / oldScale + oldOffset - center / newScale
12282
- var newOffset = {
12283
- x: (centerPlane.x / scale.x) + offset.x - (centerPlane.x / newScale.x),
12284
- y: (centerPlane.y / scale.y) + offset.y - (centerPlane.y / newScale.y),
12285
- z: (centerPlane.z / scale.z) + offset.z - (centerPlane.z / newScale.z)
12286
- };
12287
-
12288
- this.setOffset(newOffset);
12289
- this.setScale(newScale);
12615
+ this.setScale(newScale, center);
12290
12616
  };
12291
12617
 
12292
12618
  /**
12293
12619
  * Set the layers' scale.
12294
12620
  *
12295
12621
  * @param {object} newScale The scale to apply as {x,y,z}.
12622
+ * @param {dwv.math.Point3D} center The scale center Point3D.
12296
12623
  * @fires dwv.ctrl.LayerGroup#zoomchange
12297
12624
  */
12298
- this.setScale = function (newScale) {
12625
+ this.setScale = function (newScale, center) {
12299
12626
  scale = newScale;
12300
12627
  // apply to layers
12301
12628
  for (var i = 0; i < layers.length; ++i) {
12302
- layers[i].setScale(scale);
12629
+ layers[i].setScale(scale, center);
12630
+ }
12631
+
12632
+ // event value
12633
+ var value = [
12634
+ newScale.x,
12635
+ newScale.y,
12636
+ newScale.z
12637
+ ];
12638
+ if (typeof center !== 'undefined') {
12639
+ value.push(center.getX());
12640
+ value.push(center.getY());
12641
+ value.push(center.getZ());
12303
12642
  }
12304
12643
 
12305
12644
  /**
@@ -12311,7 +12650,7 @@ dwv.gui.LayerGroup = function (containerDiv, groupId) {
12311
12650
  */
12312
12651
  fireEvent({
12313
12652
  type: 'zoomchange',
12314
- value: [scale.x, scale.y, scale.z],
12653
+ value: value
12315
12654
  });
12316
12655
  };
12317
12656
 
@@ -12464,11 +12803,20 @@ dwv.gui.ZoomBinder = function () {
12464
12803
  };
12465
12804
  this.getCallback = function (layerGroup) {
12466
12805
  return function (event) {
12467
- layerGroup.setScale({
12806
+ var scale = {
12468
12807
  x: event.value[0],
12469
12808
  y: event.value[1],
12470
12809
  z: event.value[2]
12471
- });
12810
+ };
12811
+ var center;
12812
+ if (event.value.length === 6) {
12813
+ center = new dwv.math.Point3D(
12814
+ event.value[3],
12815
+ event.value[4],
12816
+ event.value[5]
12817
+ );
12818
+ }
12819
+ layerGroup.setScale(scale, center);
12472
12820
  layerGroup.draw();
12473
12821
  };
12474
12822
  };
@@ -12656,6 +13004,33 @@ dwv.gui.Stage = function () {
12656
13004
  }
12657
13005
  };
12658
13006
 
13007
+ /**
13008
+ * Synchronise the fit scale of the group layers.
13009
+ */
13010
+ this.syncLayerGroupScale = function () {
13011
+ var minScale;
13012
+ var hasScale = [];
13013
+ for (var i = 0; i < layerGroups.length; ++i) {
13014
+ var scale = layerGroups[i].calculateFitScale();
13015
+ if (typeof scale !== 'undefined') {
13016
+ hasScale.push(i);
13017
+ if (typeof minScale === 'undefined' || scale < minScale) {
13018
+ minScale = scale;
13019
+ }
13020
+ }
13021
+ }
13022
+ // exit if no scale
13023
+ if (typeof minScale === 'undefined') {
13024
+ return;
13025
+ }
13026
+ // apply min scale to layers
13027
+ for (var j = 0; j < layerGroups.length; ++j) {
13028
+ if (hasScale.includes(j)) {
13029
+ layerGroups[j].setFitScale(minScale);
13030
+ }
13031
+ }
13032
+ };
13033
+
12659
13034
  /**
12660
13035
  * Bind the layer groups of the stage.
12661
13036
  */
@@ -13086,12 +13461,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13086
13461
  var context = null;
13087
13462
 
13088
13463
  /**
13089
- * Flag to know if the context has been cleared.
13464
+ * Flag to know if the current position is valid.
13090
13465
  *
13091
13466
  * @private
13092
13467
  * @type {boolean}
13093
13468
  */
13094
- var isContextClear = true;
13469
+ var isValidPosition = true;
13095
13470
 
13096
13471
  /**
13097
13472
  * The image data array.
@@ -13157,6 +13532,22 @@ dwv.gui.ViewLayer = function (containerDiv) {
13157
13532
  */
13158
13533
  var baseOffset = {x: 0, y: 0};
13159
13534
 
13535
+ /**
13536
+ * The view offset.
13537
+ *
13538
+ * @private
13539
+ * @type {object}
13540
+ */
13541
+ var viewOffset = {x: 0, y: 0};
13542
+
13543
+ /**
13544
+ * The zoom offset.
13545
+ *
13546
+ * @private
13547
+ * @type {object}
13548
+ */
13549
+ var zoomOffset = {x: 0, y: 0};
13550
+
13160
13551
  /**
13161
13552
  * Data update flag.
13162
13553
  *
@@ -13236,6 +13627,7 @@ dwv.gui.ViewLayer = function (containerDiv) {
13236
13627
  // event.value = [index, image]
13237
13628
  if (dataIndex === event.value[0]) {
13238
13629
  viewController.setImage(event.value[1]);
13630
+ setBaseSize(viewController.getImageSize().get2D());
13239
13631
  needsDataUpdate = true;
13240
13632
  }
13241
13633
  };
@@ -13260,6 +13652,15 @@ dwv.gui.ViewLayer = function (containerDiv) {
13260
13652
  return baseSize;
13261
13653
  };
13262
13654
 
13655
+ /**
13656
+ * Get the image world (mm) 2D size.
13657
+ *
13658
+ * @returns {object} The 2D size as {x,y}.
13659
+ */
13660
+ this.getImageWorldSize = function () {
13661
+ return viewController.getImageWorldSize();
13662
+ };
13663
+
13263
13664
  /**
13264
13665
  * Get the layer opacity.
13265
13666
  *
@@ -13275,6 +13676,10 @@ dwv.gui.ViewLayer = function (containerDiv) {
13275
13676
  * @param {number} alpha The opacity ([0:1] range).
13276
13677
  */
13277
13678
  this.setOpacity = function (alpha) {
13679
+ if (alpha === opacity) {
13680
+ return;
13681
+ }
13682
+
13278
13683
  opacity = Math.min(Math.max(alpha, 0), 1);
13279
13684
 
13280
13685
  /**
@@ -13295,30 +13700,85 @@ dwv.gui.ViewLayer = function (containerDiv) {
13295
13700
  * Set the layer scale.
13296
13701
  *
13297
13702
  * @param {object} newScale The scale as {x,y}.
13703
+ * @param {dwv.math.Point3D} center The scale center.
13298
13704
  */
13299
- this.setScale = function (newScale) {
13705
+ this.setScale = function (newScale, center) {
13300
13706
  var helper = viewController.getPlaneHelper();
13301
- var orientedNewScale = helper.getOrientedXYZ(newScale);
13302
- scale = {
13707
+ var orientedNewScale = helper.getTargetOrientedXYZ(newScale);
13708
+ var finalNewScale = {
13303
13709
  x: fitScale.x * orientedNewScale.x,
13304
13710
  y: fitScale.y * orientedNewScale.y
13305
13711
  };
13712
+
13713
+ if (newScale.x === 1 &&
13714
+ newScale.y === 1 &&
13715
+ newScale.z === 1) {
13716
+ // reset zoom offset for scale=1
13717
+ var resetOffset = {
13718
+ x: offset.x - zoomOffset.x,
13719
+ y: offset.y - zoomOffset.y
13720
+ };
13721
+ // store new offset
13722
+ zoomOffset = {x: 0, y: 0};
13723
+ offset = resetOffset;
13724
+ } else {
13725
+ if (typeof center !== 'undefined') {
13726
+ var worldCenter = helper.getPlaneOffsetFromOffset3D({
13727
+ x: center.getX(),
13728
+ y: center.getY(),
13729
+ z: center.getZ()
13730
+ });
13731
+ // center was obtained with viewLayer.displayToPlanePosNoBase
13732
+ // compensated for baseOffset
13733
+ // TODO: justify...
13734
+ worldCenter = {
13735
+ x: worldCenter.x + baseOffset.x,
13736
+ y: worldCenter.y + baseOffset.y
13737
+ };
13738
+
13739
+ var newOffset = dwv.gui.getScaledOffset(
13740
+ offset, scale, finalNewScale, worldCenter);
13741
+
13742
+ var newZoomOffset = {
13743
+ x: zoomOffset.x + newOffset.x - offset.x,
13744
+ y: zoomOffset.y + newOffset.y - offset.y
13745
+ };
13746
+ // store new offset
13747
+ zoomOffset = newZoomOffset;
13748
+ offset = newOffset;
13749
+ }
13750
+ }
13751
+
13752
+ // store new scale
13753
+ scale = finalNewScale;
13306
13754
  };
13307
13755
 
13308
13756
  /**
13309
- * Set the base layer offset. Resets the layer offset.
13757
+ * Set the base layer offset. Updates the layer offset.
13310
13758
  *
13311
- * @param {object} off The offset as {x,y}.
13759
+ * @param {dwv.math.Vector3D} scrollOffset The scroll offset vector.
13760
+ * @param {dwv.math.Vector3D} planeOffset The plane offset vector.
13761
+ * @returns {boolean} True if the offset was updated.
13312
13762
  */
13313
- this.setBaseOffset = function (off) {
13763
+ this.setBaseOffset = function (scrollOffset, planeOffset) {
13314
13764
  var helper = viewController.getPlaneHelper();
13315
- baseOffset = helper.getPlaneOffsetFromOffset3D({
13316
- x: off.getX(),
13317
- y: off.getY(),
13318
- z: off.getZ()
13765
+ var scrollIndex = helper.getNativeScrollIndex();
13766
+ var newOffset = helper.getPlaneOffsetFromOffset3D({
13767
+ x: scrollIndex === 0 ? scrollOffset.getX() : planeOffset.getX(),
13768
+ y: scrollIndex === 1 ? scrollOffset.getY() : planeOffset.getY(),
13769
+ z: scrollIndex === 2 ? scrollOffset.getZ() : planeOffset.getZ(),
13319
13770
  });
13320
- // reset offset
13321
- offset = baseOffset;
13771
+ var needsUpdate = baseOffset.x !== newOffset.x ||
13772
+ baseOffset.y !== newOffset.y;
13773
+ // reset offset if needed
13774
+ if (needsUpdate) {
13775
+ offset = {
13776
+ x: offset.x - baseOffset.x + newOffset.x,
13777
+ y: offset.y - baseOffset.y + newOffset.y
13778
+ };
13779
+ baseOffset = newOffset;
13780
+ }
13781
+ return needsUpdate;
13322
13782
  };
13323
13783
 
13324
13784
  /**
@@ -13330,8 +13790,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13330
13790
  var helper = viewController.getPlaneHelper();
13331
13791
  var planeNewOffset = helper.getPlaneOffsetFromOffset3D(newOffset);
13332
13792
  offset = {
13333
- x: baseOffset.x + planeNewOffset.x,
13334
- y: baseOffset.y + planeNewOffset.y
13793
+ x: viewOffset.x + baseOffset.x + zoomOffset.x + planeNewOffset.x,
13794
+ y: viewOffset.y + baseOffset.y + zoomOffset.y + planeNewOffset.y
13335
13795
  };
13336
13796
  };
13337
13797
 
@@ -13420,6 +13880,11 @@ dwv.gui.ViewLayer = function (containerDiv) {
13420
13880
  * @fires dwv.App#renderend
13421
13881
  */
13422
13882
  this.draw = function () {
13883
+ // skip for non valid position
13884
+ if (!isValidPosition) {
13885
+ return;
13886
+ }
13887
+
13423
13888
  /**
13424
13889
  * Render start event.
13425
13890
  *
@@ -13429,7 +13894,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13429
13894
  */
13430
13895
  var event = {
13431
13896
  type: 'renderstart',
13432
- layerid: this.getId()
13897
+ layerid: this.getId(),
13898
+ dataid: this.getDataIndex()
13433
13899
  };
13434
13900
  fireEvent(event);
13435
13901
 
@@ -13464,9 +13930,6 @@ dwv.gui.ViewLayer = function (containerDiv) {
13464
13930
  // draw image
13465
13931
  context.drawImage(offscreenCanvas, 0, 0);
13466
13932
 
13467
- // set clear flag
13468
- isContextClear = false;
13469
-
13470
13933
  /**
13471
13934
  * Render end event.
13472
13935
  *
@@ -13476,7 +13939,8 @@ dwv.gui.ViewLayer = function (containerDiv) {
13476
13939
  */
13477
13940
  event = {
13478
13941
  type: 'renderend',
13479
- layerid: this.getId()
13942
+ layerid: this.getId(),
13943
+ dataid: this.getDataIndex()
13480
13944
  };
13481
13945
  fireEvent(event);
13482
13946
  };
@@ -13487,12 +13951,13 @@ dwv.gui.ViewLayer = function (containerDiv) {
13487
13951
  * @param {object} size The image size as {x,y}.
13488
13952
  * @param {object} spacing The image spacing as {x,y}.
13489
13953
  * @param {number} index The associated data index.
13954
+ * @param {number} alpha The initial data opacity.
13490
13955
  */
13491
- this.initialise = function (size, spacing, index) {
13956
+ this.initialise = function (size, spacing, index, alpha) {
13492
13957
  // set locals
13493
- baseSize = size;
13494
13958
  baseSpacing = spacing;
13495
13959
  dataIndex = index;
13960
+ opacity = Math.min(Math.max(alpha, 0), 1);
13496
13961
 
13497
13962
  // create canvas
13498
13963
  // (canvas size is set in fitToContainer)
@@ -13511,45 +13976,103 @@ dwv.gui.ViewLayer = function (containerDiv) {
13511
13976
  return;
13512
13977
  }
13513
13978
 
13514
- // check canvas
13515
- if (!dwv.gui.canCreateCanvas(baseSize.x, baseSize.y)) {
13516
- throw new Error('Cannot create canvas ' + baseSize.x + ', ' + baseSize.y);
13979
+ // off screen canvas
13980
+ offscreenCanvas = document.createElement('canvas');
13981
+
13982
+ // set base size: needs an existing context and off screen canvas
13983
+ setBaseSize(size);
13984
+
13985
+ // update data on first draw
13986
+ needsDataUpdate = true;
13987
+ };
13988
+
13989
+ /**
13990
+ * Set the base size of the layer.
13991
+ *
13992
+ * @param {object} size The size as {x,y}.
13993
+ */
13994
+ function setBaseSize(size) {
13995
+ // check canvas creation
13996
+ if (!dwv.gui.canCreateCanvas(size.x, size.y)) {
13997
+ throw new Error('Cannot create canvas ' + size.x + ', ' + size.y);
13517
13998
  }
13518
13999
 
14000
+ // set local
14001
+ baseSize = size;
14002
+
13519
14003
  // off screen canvas
13520
- offscreenCanvas = document.createElement('canvas');
13521
14004
  offscreenCanvas.width = baseSize.x;
13522
14005
  offscreenCanvas.height = baseSize.y;
13523
14006
  // original empty image data array
13524
14007
  context.clearRect(0, 0, baseSize.x, baseSize.y);
13525
14008
  imageData = context.createImageData(baseSize.x, baseSize.y);
13526
-
13527
- // update data on first draw
13528
- needsDataUpdate = true;
13529
- };
14009
+ }
13530
14010
 
13531
14011
  /**
13532
14012
  * Fit the layer to its parent container.
13533
14013
  *
13534
14014
  * @param {number} fitScale1D The 1D fit scale.
13535
14015
  * @param {object} fitSize The fit size as {x,y}.
14016
+ * @param {object} fitOffset The fit offset as {x,y}.
13536
14017
  */
13537
- this.fitToContainer = function (fitScale1D, fitSize) {
13538
- // update fit scale
13539
- fitScale = {
14018
+ this.fitToContainer = function (fitScale1D, fitSize, fitOffset) {
14019
+ var needsDraw = false;
14020
+
14021
+ // update canvas size if needed (triggers canvas reset)
14022
+ if (canvas.width !== fitSize.x || canvas.height !== fitSize.y) {
14023
+ if (!dwv.gui.canCreateCanvas(fitSize.x, fitSize.y)) {
14024
+ throw new Error('Cannot resize canvas ' + fitSize.x + ', ' + fitSize.y);
14025
+ }
14026
+ // canvas size change triggers canvas reset
14027
+ canvas.width = fitSize.x;
14028
+ canvas.height = fitSize.y;
14029
+ // update draw flag
14030
+ needsDraw = true;
14031
+ }
14032
+
14033
+ // previous scale without fit
14034
+ var previousScale = {
14035
+ x: scale.x / fitScale.x,
14036
+ y: scale.y / fitScale.y
14037
+ };
14038
+ // fit scale
14039
+ var newFitScale = {
13540
14040
  x: fitScale1D * baseSpacing.x,
13541
14041
  y: fitScale1D * baseSpacing.y
13542
14042
  };
13543
- // new canvas size
13544
- var width = fitSize.x;
13545
- var height = fitSize.y;
13546
- if (!dwv.gui.canCreateCanvas(width, height)) {
13547
- throw new Error('Cannot resize canvas ' + width + ', ' + height);
14043
+ // scale
14044
+ var newScale = {
14045
+ x: previousScale.x * newFitScale.x,
14046
+ y: previousScale.y * newFitScale.y
14047
+ };
14048
+ // check if different
14049
+ if (previousScale.x !== newScale.x || previousScale.y !== newScale.y) {
14050
+ fitScale = newFitScale;
14051
+ scale = newScale;
14052
+ // update draw flag
14053
+ needsDraw = true;
14054
+ }
14055
+
14056
+ // view offset
14057
+ var newViewOffset = {
14058
+ x: fitOffset.x / newFitScale.x,
14059
+ y: fitOffset.y / newFitScale.y
14060
+ };
14061
+ // check if different
14062
+ if (viewOffset.x !== newViewOffset.x || viewOffset.y !== newViewOffset.y) {
14063
+ viewOffset = newViewOffset;
14064
+ offset = {
14065
+ x: viewOffset.x + baseOffset.x + zoomOffset.x,
14066
+ y: viewOffset.y + baseOffset.y + zoomOffset.y
14067
+ };
14068
+ // update draw flag
14069
+ needsDraw = true;
14070
+ }
14071
+
14072
+ // draw if needed
14073
+ if (needsDraw) {
14074
+ this.draw();
13548
14075
  }
13549
- canvas.width = width;
13550
- canvas.height = height;
13551
- // reset scale
13552
- this.setScale({x: 1, y: 1, z: 1});
13553
14076
  };
13554
14077
 
13555
14078
  /**
@@ -13634,8 +14157,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13634
14157
  */
13635
14158
  function onWLChange(event) {
13636
14159
  // generate and draw if no skip flag
13637
- if (typeof event.skipGenerate === 'undefined' ||
13638
- event.skipGenerate === false) {
14160
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14161
+ event.skipGenerate === true;
14162
+ if (!skip) {
13639
14163
  needsDataUpdate = true;
13640
14164
  self.draw();
13641
14165
  }
@@ -13648,8 +14172,12 @@ dwv.gui.ViewLayer = function (containerDiv) {
13648
14172
  * @private
13649
14173
  */
13650
14174
  function onColourChange(_event) {
13651
- needsDataUpdate = true;
13652
- self.draw();
14175
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14176
+ event.skipGenerate === true;
14177
+ if (!skip) {
14178
+ needsDataUpdate = true;
14179
+ self.draw();
14180
+ }
13653
14181
  }
13654
14182
 
13655
14183
  /**
@@ -13659,26 +14187,35 @@ dwv.gui.ViewLayer = function (containerDiv) {
13659
14187
  * @private
13660
14188
  */
13661
14189
  function onPositionChange(event) {
13662
- if (typeof event.skipGenerate === 'undefined' ||
13663
- event.skipGenerate === false) {
14190
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14191
+ event.skipGenerate === true;
14192
+ if (!skip) {
14193
+ var valid = typeof event.valid === 'undefined' || event.valid;
13664
14194
  // clear for non valid events
13665
- if (typeof event.valid !== 'undefined' && !event.valid) {
13666
- self.clear();
13667
- return;
13668
- }
13669
- // 3D dimensions
13670
- var dims3D = [0, 1, 2];
13671
- // remove scroll index
13672
- var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
13673
- dims3D.splice(indexScrollIndex, 1);
13674
- // remove non scroll index from diff dims
13675
- var diffDims = event.diffDims.filter(function (item) {
13676
- return dims3D.indexOf(item) === -1;
13677
- });
13678
- // update if we have something left
13679
- if (diffDims.length !== 0) {
13680
- needsDataUpdate = true;
13681
- self.draw();
14195
+ if (!valid) {
14196
+ // clear only once
14197
+ if (isValidPosition) {
14198
+ isValidPosition = false;
14199
+ self.clear();
14200
+ }
14201
+ } else {
14202
+ // 3D dimensions
14203
+ var dims3D = [0, 1, 2];
14204
+ // remove scroll index
14205
+ var indexScrollIndex = dims3D.indexOf(viewController.getScrollIndex());
14206
+ dims3D.splice(indexScrollIndex, 1);
14207
+ // remove non scroll index from diff dims
14208
+ var diffDims = event.diffDims.filter(function (item) {
14209
+ return dims3D.indexOf(item) === -1;
14210
+ });
14211
+ // update if we have something left
14212
+ if (diffDims.length !== 0 || !isValidPosition) {
14213
+ // reset valid flag
14214
+ isValidPosition = true;
14215
+ // reset update flag
14216
+ needsDataUpdate = true;
14217
+ self.draw();
14218
+ }
13682
14219
  }
13683
14220
  }
13684
14221
  }
@@ -13690,8 +14227,9 @@ dwv.gui.ViewLayer = function (containerDiv) {
13690
14227
  * @private
13691
14228
  */
13692
14229
  function onAlphaFuncChange(event) {
13693
- if (typeof event.skipGenerate === 'undefined' ||
13694
- event.skipGenerate === false) {
14230
+ var skip = typeof event.skipGenerate !== 'undefined' &&
14231
+ event.skipGenerate === true;
14232
+ if (!skip) {
13695
14233
  needsDataUpdate = true;
13696
14234
  self.draw();
13697
14235
  }
@@ -13702,28 +14240,24 @@ dwv.gui.ViewLayer = function (containerDiv) {
13702
14240
  *
13703
14241
  * @param {dwv.math.Point} position The new position.
13704
14242
  * @param {dwv.math.Index} _index The new index.
14243
+ * @returns {boolean} True if the position was updated.
13705
14244
  */
13706
14245
  this.setCurrentPosition = function (position, _index) {
13707
- viewController.setCurrentPosition(position);
14246
+ return viewController.setCurrentPosition(position);
13708
14247
  };
13709
14248
 
13710
14249
  /**
13711
14250
  * Clear the context.
13712
14251
  */
13713
14252
  this.clear = function () {
13714
- if (!isContextClear) {
13715
- // clear the context: reset the transform first
13716
- // store the current transformation matrix
13717
- context.save();
13718
- // use the identity matrix while clearing the canvas
13719
- context.setTransform(1, 0, 0, 1, 0, 0);
13720
- context.clearRect(0, 0, canvas.width, canvas.height);
13721
- // restore the transform
13722
- context.restore();
13723
-
13724
- // update clear flag
13725
- isContextClear = true;
13726
- }
14253
+ // clear the context: reset the transform first
14254
+ // store the current transformation matrix
14255
+ context.save();
14256
+ // use the identity matrix while clearing the canvas
14257
+ context.setTransform(1, 0, 0, 1, 0, 0);
14258
+ context.clearRect(0, 0, canvas.width, canvas.height);
14259
+ // restore the transform
14260
+ context.restore();
13727
14261
  };
13728
14262
 
13729
14263
  /**
@@ -14962,13 +15496,17 @@ dwv.image = dwv.image || {};
14962
15496
  * @param {dwv.image.Spacing} spacing The object spacing.
14963
15497
  * @param {dwv.math.Matrix33} orientation The object orientation (3*3 matrix,
14964
15498
  * default to 3*3 identity).
15499
+ * @param {number} time Optional time index.
14965
15500
  */
14966
- dwv.image.Geometry = function (origin, size, spacing, orientation) {
14967
- // check input origin
14968
- if (typeof origin === 'undefined') {
14969
- origin = new dwv.math.Point3D(0, 0, 0);
14970
- }
15501
+ dwv.image.Geometry = function (origin, size, spacing, orientation, time) {
14971
15502
  var origins = [origin];
15503
+ // local helper object for time points
15504
+ var timeOrigins = {};
15505
+ var initialTime;
15506
+ if (typeof time !== 'undefined') {
15507
+ initialTime = time;
15508
+ timeOrigins[time] = [origin];
15509
+ }
14972
15510
  // check input orientation
14973
15511
  if (typeof orientation === 'undefined') {
14974
15512
  orientation = new dwv.math.getIdentityMat33();
@@ -14976,6 +15514,67 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
14976
15514
  // flag to know if new origins were added
14977
15515
  var newOrigins = false;
14978
15516
 
15517
+ /**
15518
+ * Get the time value that was passed at construction.
15519
+ *
15520
+ * @returns {number} The time value.
15521
+ */
15522
+ this.getInitialTime = function () {
15523
+ return initialTime;
15524
+ };
15525
+
15526
+ /**
15527
+ * Get the total number of slices.
15528
+ * Can be different from what is stored in the size object
15529
+ * during a volume with time points creation process.
15530
+ *
15531
+ * @returns {number} The total count.
15532
+ */
15533
+ this.getCurrentTotalNumberOfSlices = function () {
15534
+ var keys = Object.keys(timeOrigins);
15535
+ if (keys.length === 0) {
15536
+ return origins.length;
15537
+ }
15538
+ var count = 0;
15539
+ for (var i = 0; i < keys.length; ++i) {
15540
+ count += timeOrigins[keys[i]].length;
15541
+ }
15542
+ return count;
15543
+ };
15544
+
15545
+ /**
15546
+ * Check if a time point has associated slices.
15547
+ *
15548
+ * @param {number} time The time point to check.
15549
+ * @returns {boolean} True if slices are present.
15550
+ */
15551
+ this.hasSlicesAtTime = function (time) {
15552
+ return typeof timeOrigins[time] !== 'undefined';
15553
+ };
15554
+
15555
+ /**
15556
+ * Get the number of slices stored for time points preceding
15557
+ * the input one.
15558
+ *
15559
+ * @param {number} time The time point to check.
15560
+ * @returns {number} The count.
15561
+ */
15562
+ this.getCurrentNumberOfSlicesBeforeTime = function (time) {
15563
+ var keys = Object.keys(timeOrigins);
15564
+ if (keys.length === 0) {
15565
+ return;
15566
+ }
15567
+ var count = 0;
15568
+ for (var i = 0; i < keys.length; ++i) {
15569
+ var key = keys[i];
15570
+ if (parseInt(key, 10) === time) {
15571
+ break;
15572
+ }
15573
+ count += timeOrigins[key].length;
15574
+ }
15575
+ return count;
15576
+ };
15577
+
14979
15578
  /**
14980
15579
  * Get the object origin.
14981
15580
  * This should be the lowest origin to ease calculations (?).
@@ -14993,6 +15592,22 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
14993
15592
  this.getOrigins = function () {
14994
15593
  return origins;
14995
15594
  };
15595
+
15596
+ /**
15597
+ * Check if a point is in the origin list.
15598
+ *
15599
+ * @param {dwv.math.Point3D} point3D The point to check.
15600
+ * @returns {boolean} True if in list.
15601
+ */
15602
+ this.includesOrigin = function (point3D) {
15603
+ for (var i = 0; i < origins.length; ++i) {
15604
+ if (origins[i].isSimilar(point3D, dwv.math.BIG_EPSILON)) {
15605
+ return true;
15606
+ }
15607
+ }
15608
+ return false;
15609
+ };
15610
+
14996
15611
  /**
14997
15612
  * Get the object size.
14998
15613
  * Warning: the size comes as stored in DICOM, meaning that it could
@@ -15011,7 +15626,7 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15011
15626
  size.get(2)
15012
15627
  ],
15013
15628
  viewOrientation);
15014
- res = new dwv.image.Size(values);
15629
+ res = new dwv.image.Size(values.concat(size.getValues().slice(3)));
15015
15630
  }
15016
15631
  return res;
15017
15632
  };
@@ -15026,26 +15641,26 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15026
15641
  if (origins.length === 1) {
15027
15642
  return 1;
15028
15643
  }
15029
- var spacing = null;
15644
+ var sliceSpacing = null;
15030
15645
  // (x, y, z) = orientationMatrix * (i, j, k)
15031
15646
  // -> inv(orientationMatrix) * (x, y, z) = (i, j, k)
15032
15647
  // applied on the patient position, reorders indices
15033
15648
  // so that Z is the slice direction
15034
- var orientation2 = orientation.getInverse().asOneAndZeros();
15649
+ var invOrientation = orientation.getInverse();
15035
15650
  var deltas = [];
15036
15651
  for (var i = 0; i < origins.length - 1; ++i) {
15037
- var origin1 = orientation2.multiplyVector3D(origins[i]);
15038
- var origin2 = orientation2.multiplyVector3D(origins[i + 1]);
15652
+ var origin1 = invOrientation.multiplyVector3D(origins[i]);
15653
+ var origin2 = invOrientation.multiplyVector3D(origins[i + 1]);
15039
15654
  var diff = Math.abs(origin1.getZ() - origin2.getZ());
15040
15655
  if (diff === 0) {
15041
15656
  throw new Error('Zero slice spacing.' +
15042
15657
  origin1.toString() + ' ' + origin2.toString());
15043
15658
  }
15044
- if (spacing === null) {
15045
- spacing = diff;
15659
+ if (sliceSpacing === null) {
15660
+ sliceSpacing = diff;
15046
15661
  } else {
15047
- if (!dwv.math.isSimilar(spacing, diff, dwv.math.BIG_EPSILON)) {
15048
- deltas.push(Math.abs(spacing - diff));
15662
+ if (!dwv.math.isSimilar(sliceSpacing, diff, dwv.math.BIG_EPSILON)) {
15663
+ deltas.push(Math.abs(sliceSpacing - diff));
15049
15664
  }
15050
15665
  }
15051
15666
  }
@@ -15060,12 +15675,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15060
15675
  mean.toFixed(3) + ' (' + deltas.length + ' case(s))');
15061
15676
  }
15062
15677
  }
15063
- return spacing;
15678
+
15679
+ return sliceSpacing;
15064
15680
  };
15065
15681
 
15066
15682
  /**
15067
15683
  * Get the object spacing.
15068
- * Warning: the size comes as stored in DICOM, meaning that it could
15684
+ * Warning: the spacing comes as stored in DICOM, meaning that it could
15069
15685
  * be oriented.
15070
15686
  *
15071
15687
  * @param {dwv.math.Matrix33} viewOrientation The view orientation (optional)
@@ -15093,6 +15709,16 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15093
15709
  return res;
15094
15710
  };
15095
15711
 
15712
+ /**
15713
+ * Get the image spacing in real world.
15714
+ *
15715
+ * @returns {dwv.image.Spacing} The object spacing.
15716
+ */
15717
+ this.getRealSpacing = function () {
15718
+ // asOneAndZeros to not change spacing values...
15719
+ return this.getSpacing(orientation.getInverse().asOneAndZeros());
15720
+ };
15721
+
15096
15722
  /**
15097
15723
  * Get the object orientation.
15098
15724
  *
@@ -15112,24 +15738,30 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15112
15738
  * magic...
15113
15739
  *
15114
15740
  * @param {dwv.math.Point3D} point The point to evaluate.
15741
+ * @param {number} time Optional time index.
15115
15742
  * @returns {number} The slice index.
15116
15743
  */
15117
- this.getSliceIndex = function (point) {
15744
+ this.getSliceIndex = function (point, time) {
15118
15745
  // cannot use this.worldToIndex(point).getK() since
15119
15746
  // we cannot guaranty consecutive slices...
15120
15747
 
15748
+ var localOrigins = origins;
15749
+ if (typeof time !== 'undefined') {
15750
+ localOrigins = timeOrigins[time];
15751
+ }
15752
+
15121
15753
  // find the closest index
15122
15754
  var closestSliceIndex = 0;
15123
- var minDist = point.getDistance(origins[0]);
15755
+ var minDist = point.getDistance(localOrigins[0]);
15124
15756
  var dist = 0;
15125
- for (var i = 0; i < origins.length; ++i) {
15126
- dist = point.getDistance(origins[i]);
15757
+ for (var i = 0; i < localOrigins.length; ++i) {
15758
+ dist = point.getDistance(localOrigins[i]);
15127
15759
  if (dist < minDist) {
15128
15760
  minDist = dist;
15129
15761
  closestSliceIndex = i;
15130
15762
  }
15131
15763
  }
15132
- var closestOrigin = origins[closestSliceIndex];
15764
+ var closestOrigin = localOrigins[closestSliceIndex];
15133
15765
  // direction between the input point and the closest origin
15134
15766
  var pointDir = point.minus(closestOrigin);
15135
15767
  // use third orientation matrix column as base plane vector
@@ -15142,8 +15774,13 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15142
15774
  // -> >0 => vectors are codirectional
15143
15775
  // -> <0 => vectors are oposite
15144
15776
  var dotProd = normal.dotProduct(pointDir);
15777
+ var test = dotProd < 0;
15778
+ // TODO: check why coronal behaves differently...
15779
+ if (orientation.getThirdColMajorDirection() === 1) {
15780
+ test = dotProd > 0;
15781
+ }
15145
15782
  // oposite vectors get higher index
15146
- var sliceIndex = (dotProd < 0) ? closestSliceIndex + 1 : closestSliceIndex;
15783
+ var sliceIndex = test ? closestSliceIndex + 1 : closestSliceIndex;
15147
15784
  return sliceIndex;
15148
15785
  };
15149
15786
 
@@ -15152,22 +15789,32 @@ dwv.image.Geometry = function (origin, size, spacing, orientation) {
15152
15789
  *
15153
15790
  * @param {dwv.math.Point3D} origin The origin to append.
15154
15791
  * @param {number} index The index at which to append.
15792
+ * @param {number} time Optional time index.
15155
15793
  */
15156
- this.appendOrigin = function (origin, index) {
15157
- newOrigins = true;
15158
- // add in origin array
15159
- origins.splice(index, 0, origin);
15160
- // increment second dimension
15161
- var values = size.getValues();
15162
- values[2] += 1;
15163
- size = new dwv.image.Size(values);
15794
+ this.appendOrigin = function (origin, index, time) {
15795
+ if (typeof time !== 'undefined') {
15796
+ timeOrigins[time].splice(index, 0, origin);
15797
+ }
15798
+ if (typeof time === 'undefined' || time === initialTime) {
15799
+ newOrigins = true;
15800
+ // add in origin array
15801
+ origins.splice(index, 0, origin);
15802
+ // increment second dimension
15803
+ var values = size.getValues();
15804
+ values[2] += 1;
15805
+ size = new dwv.image.Size(values);
15806
+ }
15164
15807
  };
15165
15808
 
15166
15809
  /**
15167
15810
  * Append a frame to the geometry.
15168
15811
  *
15812
+ * @param {dwv.math.Point3D} origin The origin to append.
15813
+ * @param {number} time Optional time index.
15169
15814
  */
15170
- this.appendFrame = function () {
15815
+ this.appendFrame = function (origin, time) {
15816
+ // add origin to list
15817
+ timeOrigins[time] = [origin];
15171
15818
  // increment third dimension
15172
15819
  var sizeValues = size.getValues();
15173
15820
  var spacingValues = spacing.getValues();
@@ -15214,9 +15861,18 @@ dwv.image.Geometry.prototype.equals = function (rhs) {
15214
15861
  * @returns {boolean} True if the given coordinates are within bounds.
15215
15862
  */
15216
15863
  dwv.image.Geometry.prototype.isInBounds = function (point) {
15217
- // get the corresponding index
15218
- var index = this.worldToIndex(point);
15219
- return this.getSize().isInBounds(index);
15864
+ return this.isIndexInBounds(this.worldToIndex(point));
15865
+ };
15866
+
15867
+ /**
15868
+ * Check that a index is within bounds.
15869
+ *
15870
+ * @param {dwv.math.Index} index The index to check.
15871
+ * @param {Array} dirs Optional list of directions to check.
15872
+ * @returns {boolean} True if the given coordinates are within bounds.
15873
+ */
15874
+ dwv.image.Geometry.prototype.isIndexInBounds = function (index, dirs) {
15875
+ return this.getSize().isInBounds(index, dirs);
15220
15876
  };
15221
15877
 
15222
15878
  /**
@@ -15296,6 +15952,7 @@ dwv.image.Geometry.prototype.pointToWorld = function (point) {
15296
15952
  dwv.image.Geometry.prototype.worldToIndex = function (point) {
15297
15953
  // compensate for origin
15298
15954
  // (origin is not oriented, compensate before orientation)
15955
+ // TODO: use slice origin...
15299
15956
  var origin = this.getOrigin();
15300
15957
  var point3D = new dwv.math.Point3D(
15301
15958
  point.get(0) - origin.getX(),
@@ -15312,9 +15969,11 @@ dwv.image.Geometry.prototype.worldToIndex = function (point) {
15312
15969
  values[0] = Math.round(orientedPoint3D.getX() / spacing.get(0));
15313
15970
  values[1] = Math.round(orientedPoint3D.getY() / spacing.get(1));
15314
15971
  // flip K index (because of the slice order given by getSliceIndex)
15315
- values[2] = flipK(this.getSize(),
15316
- Math.round(orientedPoint3D.getZ() / spacing.get(2))
15317
- );
15972
+ var k = Math.round(orientedPoint3D.getZ() / spacing.get(2));
15973
+ // abs to fix #1163
15974
+ // TODO: find out why k can sometimes be negative...
15975
+ values[2] = flipK(this.getSize(), k);
15976
+
15318
15977
  // return index
15319
15978
  return new dwv.math.Index(values);
15320
15979
  };
@@ -15324,6 +15983,31 @@ var dwv = dwv || {};
15324
15983
  /** @namespace */
15325
15984
  dwv.image = dwv.image || {};
15326
15985
 
15986
+ /**
15987
+ * Get the slice index of an input slice into a volume geometry.
15988
+ *
15989
+ * @param {dwv.image.Geometry} volumeGeometry The volume geometry.
15990
+ * @param {dwv.image.Geometry} sliceGeometry The slice geometry.
15991
+ * @returns {dwv.math.Index} The index of the slice in the volume geomtry.
15992
+ */
15993
+ dwv.image.getSliceIndex = function (volumeGeometry, sliceGeometry) {
15994
+ // possible time
15995
+ var timeId = sliceGeometry.getInitialTime();
15996
+ // index values
15997
+ var values = [];
15998
+ // x, y
15999
+ values.push(0);
16000
+ values.push(0);
16001
+ // z
16002
+ values.push(volumeGeometry.getSliceIndex(sliceGeometry.getOrigin(), timeId));
16003
+ // time
16004
+ if (typeof timeId !== 'undefined') {
16005
+ values.push(timeId);
16006
+ }
16007
+ // return index
16008
+ return new dwv.math.Index(values);
16009
+ };
16010
+
15327
16011
  /**
15328
16012
  * Image class.
15329
16013
  * Usable once created, optional are:
@@ -15722,12 +16406,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15722
16406
  * Append a slice to the image.
15723
16407
  *
15724
16408
  * @param {Image} rhs The slice to append.
15725
- * @param {number} timeId An optional time ID.
15726
16409
  */
15727
- this.appendSlice = function (rhs, timeId) {
15728
- if (typeof timeId === 'undefined') {
15729
- timeId = 0;
15730
- }
16410
+ this.appendSlice = function (rhs) {
15731
16411
  // check input
15732
16412
  if (rhs === null) {
15733
16413
  throw new Error('Cannot append null slice');
@@ -15753,7 +16433,8 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15753
16433
  }
15754
16434
  // all meta should be equal
15755
16435
  for (var key in meta) {
15756
- if (key === 'windowPresets') {
16436
+ if (key === 'windowPresets' || key === 'numberOfFiles' ||
16437
+ key === 'custom') {
15757
16438
  continue;
15758
16439
  }
15759
16440
  if (meta[key] !== rhs.getMeta()[key]) {
@@ -15761,6 +16442,23 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15761
16442
  }
15762
16443
  }
15763
16444
 
16445
+ // possible time
16446
+ var timeId = rhs.getGeometry().getInitialTime();
16447
+
16448
+ // append frame if needed
16449
+ var isNewFrame = false;
16450
+ if (typeof timeId !== 'undefined' && !geometry.hasSlicesAtTime(timeId)) {
16451
+ // update grometry
16452
+ this.appendFrame(rhs.getGeometry().getOrigin(), timeId);
16453
+ // update size
16454
+ size = geometry.getSize();
16455
+ // update flag
16456
+ isNewFrame = true;
16457
+ }
16458
+
16459
+ // get slice index
16460
+ var index = dwv.image.getSliceIndex(geometry, rhs.getGeometry());
16461
+
15764
16462
  // calculate slice size
15765
16463
  var sliceSize = numberOfComponents * size.getDimSize(2);
15766
16464
 
@@ -15769,65 +16467,46 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15769
16467
  throw new Error('Missing number of files for buffer manipulation.');
15770
16468
  }
15771
16469
  var fullBufferSize = sliceSize * meta.numberOfFiles;
15772
- if (size.length() === 4) {
15773
- fullBufferSize *= size.get(3);
15774
- }
15775
16470
  if (buffer.length !== fullBufferSize) {
15776
16471
  realloc(fullBufferSize);
15777
16472
  }
15778
16473
 
15779
- var newSliceIndex = geometry.getSliceIndex(rhs.getGeometry().getOrigin());
15780
- var values = new Array(geometry.getSize().length());
15781
- values.fill(0);
15782
- values[2] = newSliceIndex;
15783
- if (size.length() === 4) {
15784
- values[3] = timeId;
15785
- }
15786
- var index = new dwv.math.Index(values);
15787
- var primaryOffset = size.indexToOffset(index);
15788
- var secondaryOffset = this.getSecondaryOffset(index);
15789
-
15790
- // first frame special slice by slice append
15791
- if (timeId === 0) {
15792
- // store slice
15793
- var oldNumberOfSlices = size.get(2);
15794
- // move content if needed
15795
- var start = primaryOffset;
15796
- var end;
15797
- if (newSliceIndex === 0) {
15798
- // insert slice before current data
15799
- end = start + oldNumberOfSlices * sliceSize;
15800
- buffer.set(
15801
- buffer.subarray(start, end),
15802
- primaryOffset + sliceSize
15803
- );
15804
- } else if (newSliceIndex < oldNumberOfSlices) {
15805
- // insert slice in between current data
15806
- end = start + (oldNumberOfSlices - newSliceIndex) * sliceSize;
15807
- buffer.set(
15808
- buffer.subarray(start, end),
15809
- primaryOffset + sliceSize
15810
- );
15811
- }
15812
- }
16474
+ // slice index
16475
+ var sliceIndex = index.get(2);
15813
16476
 
16477
+ // slice index including possible 4D
16478
+ var fullSliceIndex = sliceIndex;
16479
+ if (typeof timeId !== 'undefined') {
16480
+ fullSliceIndex += geometry.getCurrentNumberOfSlicesBeforeTime(timeId);
16481
+ }
16482
+ // offset of the input slice
16483
+ var indexOffset = fullSliceIndex * sliceSize;
16484
+ var maxOffset = geometry.getCurrentTotalNumberOfSlices() * sliceSize;
16485
+ // move content if needed
16486
+ if (indexOffset < maxOffset) {
16487
+ buffer.set(
16488
+ buffer.subarray(indexOffset, maxOffset),
16489
+ indexOffset + sliceSize
16490
+ );
16491
+ }
15814
16492
  // add new slice content
15815
- buffer.set(rhs.getBuffer(), primaryOffset);
16493
+ buffer.set(rhs.getBuffer(), indexOffset);
15816
16494
 
15817
16495
  // update geometry
15818
- if (timeId === 0) {
15819
- geometry.appendOrigin(rhs.getGeometry().getOrigin(), newSliceIndex);
16496
+ if (!isNewFrame) {
16497
+ geometry.appendOrigin(
16498
+ rhs.getGeometry().getOrigin(), sliceIndex, timeId);
15820
16499
  }
15821
16500
  // update rsi
15822
16501
  // (rhs should just have one rsi)
15823
16502
  this.setRescaleSlopeAndIntercept(
15824
- rhs.getRescaleSlopeAndIntercept(), secondaryOffset);
16503
+ rhs.getRescaleSlopeAndIntercept(), fullSliceIndex);
15825
16504
 
15826
16505
  // current number of images
15827
16506
  var numberOfImages = imageUids.length;
15828
16507
 
15829
16508
  // insert sop instance UIDs
15830
- imageUids.splice(secondaryOffset, 0, rhs.getImageUid());
16509
+ imageUids.splice(fullSliceIndex, 0, rhs.getImageUid());
15831
16510
 
15832
16511
  // update window presets
15833
16512
  if (typeof meta.windowPresets !== 'undefined') {
@@ -15857,7 +16536,7 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15857
16536
  if (typeof windowPreset.perslice !== 'undefined' &&
15858
16537
  windowPreset.perslice === true) {
15859
16538
  windowPresets[pkey].wl.splice(
15860
- secondaryOffset, 0, rhsPreset.wl[0]);
16539
+ fullSliceIndex, 0, rhsPreset.wl[0]);
15861
16540
  }
15862
16541
  } else {
15863
16542
  // if not defined (it should be), store all
@@ -15896,9 +16575,12 @@ dwv.image.Image = function (geometry, buffer, imageUids) {
15896
16575
 
15897
16576
  /**
15898
16577
  * Append a frame to the image.
16578
+ *
16579
+ * @param {number} time The frame time value.
16580
+ * @param {dwv.math.Point3D} origin The origin of the frame.
15899
16581
  */
15900
- this.appendFrame = function () {
15901
- geometry.appendFrame();
16582
+ this.appendFrame = function (time, origin) {
16583
+ geometry.appendFrame(time, origin);
15902
16584
  fireEvent({type: 'appendframe'});
15903
16585
  // memory will be updated at the first appendSlice or appendFrameBuffer
15904
16586
  };
@@ -16496,8 +17178,9 @@ dwv.image.ImageFactory.prototype.create = function (
16496
17178
  // geometry
16497
17179
  var origin = new dwv.math.Point3D(
16498
17180
  slicePosition[0], slicePosition[1], slicePosition[2]);
17181
+ var time = dicomElements.getTime();
16499
17182
  var geometry = new dwv.image.Geometry(
16500
- origin, size, spacing, orientationMatrix);
17183
+ origin, size, spacing, orientationMatrix, time);
16501
17184
 
16502
17185
  // sop instance UID
16503
17186
  var sopInstanceUid = dwv.dicom.cleanString(
@@ -16735,7 +17418,8 @@ dwv.image.simpleRange = function (dataAccessor, start, end, increment) {
16735
17418
  if (nextIndex < end) {
16736
17419
  var result = {
16737
17420
  value: dataAccessor(nextIndex),
16738
- done: false
17421
+ done: false,
17422
+ index: nextIndex
16739
17423
  };
16740
17424
  nextIndex += increment;
16741
17425
  return result;
@@ -16805,7 +17489,8 @@ dwv.image.range = function (dataAccessor, start, maxIter, increment,
16805
17489
  if (mainCount < maxIter) {
16806
17490
  var result = {
16807
17491
  value: dataAccessor(nextIndex),
16808
- done: false
17492
+ done: false,
17493
+ index: nextIndex
16809
17494
  };
16810
17495
  nextIndex += increment;
16811
17496
  ++mainCount;
@@ -16846,7 +17531,8 @@ dwv.image.rangeRegion = function (
16846
17531
  if (nextIndex < end) {
16847
17532
  var result = {
16848
17533
  value: dataAccessor(nextIndex),
16849
- done: false
17534
+ done: false,
17535
+ index: nextIndex
16850
17536
  };
16851
17537
  regionElementCount += 1;
16852
17538
  nextIndex += increment;
@@ -16886,7 +17572,8 @@ dwv.image.rangeRegions = function (
16886
17572
  if (nextIndex < end) {
16887
17573
  var result = {
16888
17574
  value: dataAccessor(nextIndex),
16889
- done: false
17575
+ done: false,
17576
+ index: nextIndex
16890
17577
  };
16891
17578
  regionElementCount += 1;
16892
17579
  nextIndex += increment;
@@ -16952,7 +17639,8 @@ dwv.image.simpleRange3d = function (
16952
17639
  dataAccessor(nextIndex1),
16953
17640
  dataAccessor(nextIndex2)
16954
17641
  ],
16955
- done: false
17642
+ done: false,
17643
+ index: [nextIndex, nextIndex1, nextIndex2]
16956
17644
  };
16957
17645
  nextIndex += increment;
16958
17646
  nextIndex1 += increment;
@@ -17034,7 +17722,12 @@ dwv.image.range3d = function (dataAccessor, start, maxIter, increment,
17034
17722
  r1.value,
17035
17723
  r2.value
17036
17724
  ],
17037
- done: false
17725
+ done: false,
17726
+ index: [
17727
+ r0.index,
17728
+ r1.index,
17729
+ r2.index
17730
+ ]
17038
17731
  };
17039
17732
  }
17040
17733
  return {
@@ -17763,7 +18456,7 @@ dwv.image.MaskFactory.prototype.create = function (
17763
18456
  }
17764
18457
  // create frame info object from per frame func
17765
18458
  var frameInfos = [];
17766
- var posPats = [];
18459
+ var framePosPats = [];
17767
18460
  for (var j = 0; j < perFrameFuncGroupSequence.length; ++j) {
17768
18461
  var frameFunc = perFrameFuncGroupSequence[j];
17769
18462
  // Derivation Image Sequence
@@ -17790,8 +18483,8 @@ dwv.image.MaskFactory.prototype.create = function (
17790
18483
  for (var p = 0; p < imagePosPat.length; ++p) {
17791
18484
  imagePosPat[p] = parseFloat(imagePosPat[p], 10);
17792
18485
  }
17793
- if (!includesPosPat(posPats, imagePosPat)) {
17794
- posPats.push(imagePosPat);
18486
+ if (!includesPosPat(framePosPats, imagePosPat)) {
18487
+ framePosPats.push(imagePosPat);
17795
18488
  }
17796
18489
  frameInfos.push({
17797
18490
  dimIndex: dimIndex,
@@ -17835,7 +18528,22 @@ dwv.image.MaskFactory.prototype.create = function (
17835
18528
  }
17836
18529
  }
17837
18530
  // sort positions patient
17838
- posPats.sort(comparePosPat);
18531
+ framePosPats.sort(comparePosPat);
18532
+
18533
+ // add missing posPats
18534
+ var posPats = [];
18535
+ var sliceSpacing = spacing.get(2);
18536
+ for (var g = 0; g < framePosPats.length - 1; ++g) {
18537
+ posPats.push(framePosPats[g]);
18538
+ var nextZ = framePosPats[g][2] - sliceSpacing;
18539
+ var diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18540
+ while (diff > sliceSpacing) {
18541
+ posPats.push([framePosPats[g][0], framePosPats[g][1], nextZ]);
18542
+ nextZ -= sliceSpacing;
18543
+ diff = Math.abs(nextZ - framePosPats[g + 1][2]);
18544
+ }
18545
+ }
18546
+ posPats.push(framePosPats[framePosPats.length - 1]);
17839
18547
 
17840
18548
  // create output buffer
17841
18549
  // as many slices as posPats -> gap slices between groups are not represented
@@ -17900,11 +18608,12 @@ dwv.image.MaskFactory.prototype.create = function (
17900
18608
  var meta = {
17901
18609
  Modality: 'SEG',
17902
18610
  BitsStored: 8,
17903
- labels: labels,
17904
- frameInfos: frameInfos,
17905
18611
  SeriesInstanceUID: dicomElements.getFromKey('x0020000E'),
17906
- SOPInstanceUID: dicomElements.getFromKey('x00080018'),
17907
- ImageOrientationPatient: imageOrientationPatient
18612
+ ImageOrientationPatient: imageOrientationPatient,
18613
+ custom: {
18614
+ labels: labels,
18615
+ frameInfos: frameInfos
18616
+ }
17908
18617
  };
17909
18618
  image.setMeta(meta);
17910
18619
 
@@ -17920,9 +18629,13 @@ dwv.image = dwv.image || {};
17920
18629
  *
17921
18630
  * @class
17922
18631
  * @param {dwv.image.Spacing} spacing The spacing.
17923
- * @param {dwv.math.Matrix} orientation The orientation.
18632
+ * @param {dwv.math.Matrix} imageOrientation The image oientation.
18633
+ * @param {dwv.math.Matrix} viewOrientation The view orientation.
17924
18634
  */
17925
- dwv.image.PlaneHelper = function (spacing, orientation) {
18635
+ dwv.image.PlaneHelper = function (spacing, imageOrientation, viewOrientation) {
18636
+
18637
+ var targetOrientation = dwv.gui.getTargetOrientation(
18638
+ imageOrientation, viewOrientation);
17926
18639
 
17927
18640
  /**
17928
18641
  * Get a 3D offset from a plane one.
@@ -17935,22 +18648,28 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
17935
18648
  var planeOffset = new dwv.math.Vector3D(
17936
18649
  offset2D.x, offset2D.y, 0);
17937
18650
  // de-orient
17938
- var pixelOffset = this.getDeOrientedVector3D(planeOffset);
17939
- // offset indexToWorld
17940
- return offsetIndexToWorld(pixelOffset);
18651
+ var pixelOffset = this.getTargetDeOrientedVector3D(planeOffset);
18652
+ // ~indexToWorld
18653
+ return new dwv.math.Vector3D(
18654
+ pixelOffset.getX() * spacing.get(0),
18655
+ pixelOffset.getY() * spacing.get(1),
18656
+ pixelOffset.getZ() * spacing.get(2));
17941
18657
  };
17942
18658
 
17943
18659
  /**
17944
18660
  * Get a plane offset from a 3D one.
17945
18661
  *
17946
- * @param {dwv.math.Point3D} offset3D The 3D offset.
18662
+ * @param {object} offset3D The 3D offset as {x,y,z}.
17947
18663
  * @returns {object} The plane offset as {x,y}.
17948
18664
  */
17949
18665
  this.getPlaneOffsetFromOffset3D = function (offset3D) {
17950
- // offset worldToIndex
17951
- var pixelOffset = offsetWorldToIndex(offset3D);
18666
+ // ~worldToIndex
18667
+ var pixelOffset = new dwv.math.Vector3D(
18668
+ offset3D.x / spacing.get(0),
18669
+ offset3D.y / spacing.get(1),
18670
+ offset3D.z / spacing.get(2));
17952
18671
  // orient
17953
- var planeOffset = this.getOrientedVector3D(pixelOffset);
18672
+ var planeOffset = this.getTargetOrientedVector3D(pixelOffset);
17954
18673
  // make 2D
17955
18674
  return {
17956
18675
  x: planeOffset.getX(),
@@ -17959,111 +18678,63 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
17959
18678
  };
17960
18679
 
17961
18680
  /**
17962
- * Apply spacing to an offset.
17963
- *
17964
- * @param {dwv.math.Point3D} off The 3D offset.
17965
- * @returns {dwv.math.Vector3D} The world offset.
17966
- */
17967
- function offsetIndexToWorld(off) {
17968
- return new dwv.math.Vector3D(
17969
- off.getX() * spacing.get(0),
17970
- off.getY() * spacing.get(1),
17971
- off.getZ() * spacing.get(2));
17972
- }
17973
-
17974
- /**
17975
- * Remove spacing from an offset.
17976
- *
17977
- * @param {object} off The world offset object as {x,y,z}.
17978
- * @returns {dwv.math.Vector3D} The 3D offset.
17979
- */
17980
- function offsetWorldToIndex(off) {
17981
- return new dwv.math.Vector3D(
17982
- off.x / spacing.get(0),
17983
- off.y / spacing.get(1),
17984
- off.z / spacing.get(2));
17985
- }
17986
-
17987
- /**
17988
- * Orient an input vector.
18681
+ * Orient an input vector from real to target space.
17989
18682
  *
17990
18683
  * @param {dwv.math.Vector3D} vector The input vector.
17991
- * @returns {dwv.math.Vector3D} The oriented vector.
17992
- */
17993
- this.getOrientedVector3D = function (vector) {
17994
- var planeVector = vector;
17995
- if (typeof orientation !== 'undefined') {
17996
- // abs? otherwise negative index...
17997
- // vector = orientation * planeVector
17998
- planeVector = orientation.getInverse().getAbs().multiplyVector3D(vector);
17999
- }
18000
- return planeVector;
18001
- };
18002
-
18003
- /**
18004
- * Orient an input index.
18005
- *
18006
- * @param {dwv.math.Index} index The input index.
18007
- * @returns {dwv.math.Index} The oriented index.
18684
+ * @returns {dwv.math.Vector3D} The oriented vector.
18008
18685
  */
18009
- this.getOrientedIndex = function (index) {
18010
- var planeIndex = index;
18011
- if (typeof orientation !== 'undefined') {
18012
- // abs? otherwise negative index...
18013
- // vector = orientation * planeVector
18014
- planeIndex = orientation.getInverse().getAbs().multiplyIndex3D(index);
18686
+ this.getTargetOrientedVector3D = function (vector) {
18687
+ var planeVector = vector;
18688
+ if (typeof targetOrientation !== 'undefined') {
18689
+ planeVector = targetOrientation.getInverse().multiplyVector3D(vector);
18015
18690
  }
18016
- return planeIndex;
18691
+ return planeVector;
18017
18692
  };
18018
18693
 
18019
18694
  /**
18020
- * Orient an input point.
18695
+ * De-orient an input vector from target to real space.
18021
18696
  *
18022
- * @param {dwv.math.Point3D} point The input point.
18023
- * @returns {dwv.math.Point3D} The oriented point.
18697
+ * @param {dwv.math.Vector3D} planeVector The input vector.
18698
+ * @returns {dwv.math.Vector3D} The de-orienteded vector.
18024
18699
  */
18025
- this.getOrientedPoint = function (point) {
18026
- var planePoint = point;
18027
- if (typeof orientation !== 'undefined') {
18028
- // abs? otherwise negative index...
18029
- // vector = orientation * planeVector
18030
- var point3D =
18031
- orientation.getInverse().getAbs().multiplyPoint3D(point.get3D());
18032
- planePoint = point.mergeWith3D(point3D);
18700
+ this.getTargetDeOrientedVector3D = function (planeVector) {
18701
+ var vector = planeVector;
18702
+ if (typeof targetOrientation !== 'undefined') {
18703
+ vector = targetOrientation.multiplyVector3D(planeVector);
18033
18704
  }
18034
- return planePoint;
18705
+ return vector;
18035
18706
  };
18036
18707
 
18037
18708
  /**
18038
- * De-orient an input vector.
18709
+ * Orient an input vector from target to image space.
18039
18710
  *
18040
18711
  * @param {dwv.math.Vector3D} planeVector The input vector.
18041
- * @returns {dwv.math.Vector3D} The de-orienteded vector.
18712
+ * @returns {dwv.math.Vector3D} The orienteded vector.
18042
18713
  */
18043
- this.getDeOrientedVector3D = function (planeVector) {
18714
+ this.getImageOrientedVector3D = function (planeVector) {
18044
18715
  var vector = planeVector;
18045
- if (typeof orientation !== 'undefined') {
18716
+ if (typeof viewOrientation !== 'undefined') {
18046
18717
  // abs? otherwise negative index...
18047
- // vector = orientation * planePoint
18048
- vector = orientation.getAbs().multiplyVector3D(planeVector);
18718
+ // vector = viewOrientation * planePoint
18719
+ vector = viewOrientation.getAbs().multiplyVector3D(planeVector);
18049
18720
  }
18050
18721
  return vector;
18051
18722
  };
18052
18723
 
18053
18724
  /**
18054
- * Reorder values to follow orientation.
18725
+ * Reorder values to follow target orientation.
18055
18726
  *
18056
18727
  * @param {object} values Values as {x,y,z}.
18057
18728
  * @returns {object} Reoriented values as {x,y,z}.
18058
18729
  */
18059
- this.getOrientedXYZ = function (values) {
18730
+ this.getTargetOrientedXYZ = function (values) {
18060
18731
  var orientedValues = dwv.math.getOrientedArray3D(
18061
18732
  [
18062
18733
  values.x,
18063
18734
  values.y,
18064
18735
  values.z
18065
18736
  ],
18066
- orientation);
18737
+ targetOrientation);
18067
18738
  return {
18068
18739
  x: orientedValues[0],
18069
18740
  y: orientedValues[1],
@@ -18072,36 +18743,29 @@ dwv.image.PlaneHelper = function (spacing, orientation) {
18072
18743
  };
18073
18744
 
18074
18745
  /**
18075
- * Reorder values to compensate for orientation.
18746
+ * Get the (view) scroll dimension index.
18076
18747
  *
18077
- * @param {object} values Values as {x,y,z}.
18078
- * @returns {object} 'Deoriented' values as {x,y,z}.
18748
+ * @returns {number} The index.
18079
18749
  */
18080
- this.getDeOrientedXYZ = function (values) {
18081
- var deOrientedValues = dwv.math.getDeOrientedArray3D(
18082
- [
18083
- values.x,
18084
- values.y,
18085
- values.z
18086
- ],
18087
- orientation
18088
- );
18089
- return {
18090
- x: deOrientedValues[0],
18091
- y: deOrientedValues[1],
18092
- z: deOrientedValues[2]
18093
- };
18750
+ this.getScrollIndex = function () {
18751
+ var index = null;
18752
+ if (typeof viewOrientation !== 'undefined') {
18753
+ index = viewOrientation.getThirdColMajorDirection();
18754
+ } else {
18755
+ index = 2;
18756
+ }
18757
+ return index;
18094
18758
  };
18095
18759
 
18096
18760
  /**
18097
- * Get the scroll dimension index.
18761
+ * Get the native (image) scroll dimension index.
18098
18762
  *
18099
18763
  * @returns {number} The index.
18100
18764
  */
18101
- this.getScrollIndex = function () {
18765
+ this.getNativeScrollIndex = function () {
18102
18766
  var index = null;
18103
- if (typeof orientation !== 'undefined') {
18104
- index = orientation.getThirdColMajorDirection();
18767
+ if (typeof imageOrientation !== 'undefined') {
18768
+ index = imageOrientation.getThirdColMajorDirection();
18105
18769
  } else {
18106
18770
  index = 2;
18107
18771
  }
@@ -18453,9 +19117,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
18453
19117
  * Check that an index is within bounds.
18454
19118
  *
18455
19119
  * @param {dwv.math.Index} index The index to check.
19120
+ * @param {Array} dirs Optional list of directions to check.
18456
19121
  * @returns {boolean} True if the given coordinates are within bounds.
18457
19122
  */
18458
- dwv.image.Size.prototype.isInBounds = function (index) {
19123
+ dwv.image.Size.prototype.isInBounds = function (index, dirs) {
18459
19124
  // check input
18460
19125
  if (!index) {
18461
19126
  return false;
@@ -18465,9 +19130,26 @@ dwv.image.Size.prototype.isInBounds = function (index) {
18465
19130
  if (length !== index.length()) {
18466
19131
  return false;
18467
19132
  }
18468
- // check values
18469
- for (var i = 0; i < length; ++i) {
18470
- if (index.get(i) < 0 || index.get(i) > this.get(i) - 1) {
19133
+ // create dirs if not there
19134
+ if (typeof dirs === 'undefined') {
19135
+ dirs = [];
19136
+ for (var j = 0; j < length; ++j) {
19137
+ dirs.push(j);
19138
+ }
19139
+ } else {
19140
+ for (var k = 0; k < length; ++k) {
19141
+ if (dirs[k] > length - 1) {
19142
+ throw new Error('Wrong input dir value: ' + dirs[k]);
19143
+ }
19144
+ }
19145
+ }
19146
+ // check values is 0 <= v < size
19147
+ var inBound = function (value, size) {
19148
+ return value >= 0 && value < size;
19149
+ };
19150
+ // check
19151
+ for (var i = 0; i < dirs.length; ++i) {
19152
+ if (!inBound(index.get(dirs[i]), this.get(dirs[i]))) {
18471
19153
  return false;
18472
19154
  }
18473
19155
  }
@@ -18669,12 +19351,12 @@ dwv.image.View = function (image) {
18669
19351
  // to add the extra dimension
18670
19352
  image.addEventListener('appendframe', function () {
18671
19353
  // update current position if first appendFrame
18672
- var position = self.getCurrentPosition();
18673
- if (position.length() === 3) {
19354
+ var index = self.getCurrentIndex();
19355
+ if (index.length() === 3) {
18674
19356
  // add dimension
18675
- var values = position.getValues();
19357
+ var values = index.getValues();
18676
19358
  values.push(0);
18677
- self.setCurrentPosition(new dwv.math.Point(values));
19359
+ self.setCurrentIndex(new dwv.math.Index(values));
18678
19360
  }
18679
19361
  });
18680
19362
 
@@ -18720,9 +19402,10 @@ dwv.image.View = function (image) {
18720
19402
  var colourMap = dwv.image.lut.plain;
18721
19403
  /**
18722
19404
  * Current position as a Point3D.
19405
+ * Store position and not index to stay geometry independent.
18723
19406
  *
18724
19407
  * @private
18725
- * @type {object}
19408
+ * @type {dwv.math.Index}
18726
19409
  */
18727
19410
  var currentPosition = null;
18728
19411
  /**
@@ -18777,20 +19460,20 @@ dwv.image.View = function (image) {
18777
19460
  };
18778
19461
 
18779
19462
  /**
18780
- * Set initial position.
19463
+ * Initialise the view: set initial index.
18781
19464
  */
18782
- this.setInitialPosition = function () {
18783
- var silent = true;
19465
+ this.init = function () {
19466
+ this.setInitialIndex();
19467
+ };
18784
19468
 
19469
+ /**
19470
+ * Set the initial index to 0.
19471
+ */
19472
+ this.setInitialIndex = function () {
18785
19473
  var geometry = image.getGeometry();
18786
19474
  var values = new Array(geometry.getSize().length());
18787
19475
  values.fill(0);
18788
- var index = new dwv.math.Index(values);
18789
-
18790
- this.setCurrentPosition(
18791
- geometry.indexToWorld(index),
18792
- silent
18793
- );
19476
+ this.setCurrentIndex(new dwv.math.Index(values), true);
18794
19477
  };
18795
19478
 
18796
19479
  /**
@@ -18813,9 +19496,10 @@ dwv.image.View = function (image) {
18813
19496
  *
18814
19497
  * @param {*} _value The pixel value. Can be a number for monochrome
18815
19498
  * data or an array for RGB data.
19499
+ * @param {number} _index The data index of the value.
18816
19500
  * @returns {number} The coresponding alpha [0,255].
18817
19501
  */
18818
- var alphaFunction = function (_value) {
19502
+ var alphaFunction = function (_value, _index) {
18819
19503
  // default always returns fully visible
18820
19504
  return 0xff;
18821
19505
  };
@@ -18859,8 +19543,8 @@ dwv.image.View = function (image) {
18859
19543
  */
18860
19544
  this.getCurrentWindowLut = function (rsi) {
18861
19545
  // check position
18862
- if (!this.getCurrentPosition()) {
18863
- this.setInitialPosition();
19546
+ if (!this.getCurrentIndex()) {
19547
+ this.setInitialIndex();
18864
19548
  }
18865
19549
  var currentIndex = this.getCurrentIndex();
18866
19550
  // use current rsi if not provided
@@ -19054,125 +19738,174 @@ dwv.image.View = function (image) {
19054
19738
  * @returns {dwv.math.Index} The current index.
19055
19739
  */
19056
19740
  this.getCurrentIndex = function () {
19741
+ var position = this.getCurrentPosition();
19742
+ if (!position) {
19743
+ return null;
19744
+ }
19057
19745
  var geometry = this.getImage().getGeometry();
19058
- return geometry.worldToIndex(currentPosition);
19746
+ return geometry.worldToIndex(position);
19747
+ };
19748
+
19749
+ /**
19750
+ * Check is the provided position can be set.
19751
+ *
19752
+ * @param {dwv.math.Point} position The position.
19753
+ * @returns {boolean} True is the position is in bounds.
19754
+ */
19755
+ this.canSetPosition = function (position) {
19756
+ var geometry = image.getGeometry();
19757
+ var index = geometry.worldToIndex(position);
19758
+ var dirs = [this.getScrollIndex()];
19759
+ if (index.length() === 4) {
19760
+ dirs.push(3);
19761
+ }
19762
+ return geometry.isIndexInBounds(index, dirs);
19763
+ };
19764
+
19765
+ /**
19766
+ * Get the origin at a given position.
19767
+ *
19768
+ * @param {dwv.math.Point} position The position.
19769
+ * @returns {dwv.math.Point} The origin.
19770
+ */
19771
+ this.getOrigin = function (position) {
19772
+ var geometry = image.getGeometry();
19773
+ var originIndex = 0;
19774
+ if (typeof position !== 'undefined') {
19775
+ var index = geometry.worldToIndex(position);
19776
+ // index is reoriented, 2 is scroll index
19777
+ originIndex = index.get(2);
19778
+ }
19779
+ return geometry.getOrigins()[originIndex];
19059
19780
  };
19060
19781
 
19061
19782
  /**
19062
19783
  * Set the current position.
19063
19784
  *
19064
- * @param {dwv.math.Point} newPosition The new position.
19785
+ * @param {dwv.math.Point} position The new position.
19065
19786
  * @param {boolean} silent Flag to fire event or not.
19066
19787
  * @returns {boolean} False if not in bounds
19067
19788
  * @fires dwv.image.View#positionchange
19068
19789
  */
19069
- this.setCurrentPosition = function (newPosition, silent) {
19070
- // check input
19071
- if (typeof silent === 'undefined') {
19072
- silent = false;
19073
- }
19074
-
19790
+ this.setCurrentPosition = function (position, silent) {
19791
+ // send invalid event if not in bounds
19075
19792
  var geometry = image.getGeometry();
19076
- var posIndex = geometry.worldToIndex(newPosition);
19077
-
19078
- // check if possible
19079
- if (!geometry.isInBounds(newPosition)) {
19793
+ var index = geometry.worldToIndex(position);
19794
+ var dirs = [this.getScrollIndex()];
19795
+ if (index.length() === 4) {
19796
+ dirs.push(3);
19797
+ }
19798
+ if (!geometry.isIndexInBounds(index, dirs)) {
19080
19799
  if (!silent) {
19081
19800
  // fire event with valid: false
19082
19801
  fireEvent({
19083
19802
  type: 'positionchange',
19084
19803
  value: [
19085
- posIndex.getValues(),
19086
- newPosition.getValues(),
19804
+ index.getValues(),
19805
+ position.getValues(),
19087
19806
  ],
19088
19807
  valid: false
19089
19808
  });
19090
19809
  }
19091
19810
  return false;
19092
19811
  }
19812
+ return this.setCurrentIndex(index, silent);
19813
+ };
19814
+
19815
+ /**
19816
+ * Set the current index.
19817
+ *
19818
+ * @param {dwv.math.Index} index The new index.
19819
+ * @param {boolean} silent Flag to fire event or not.
19820
+ * @returns {boolean} False if not in bounds.
19821
+ * @fires dwv.image.View#positionchange
19822
+ */
19823
+ this.setCurrentIndex = function (index, silent) {
19824
+ // check input
19825
+ if (typeof silent === 'undefined') {
19826
+ silent = false;
19827
+ }
19093
19828
 
19094
- var isNew = !currentPosition || !currentPosition.equals(newPosition);
19829
+ var geometry = image.getGeometry();
19830
+ var position = geometry.indexToWorld(index);
19095
19831
 
19096
- if (isNew) {
19097
- var diffDims = null;
19098
- if (currentPosition) {
19099
- if (currentPosition.canCompare(newPosition)) {
19100
- diffDims = currentPosition.compare(newPosition);
19101
- } else {
19102
- diffDims = [];
19103
- var minLen = Math.min(currentPosition.length(), newPosition.length());
19104
- for (var i = 0; i < minLen; ++i) {
19105
- if (currentPosition.get(i) !== newPosition.get(i)) {
19106
- diffDims.push(i);
19107
- }
19108
- }
19109
- var maxLen = Math.max(currentPosition.length(), newPosition.length());
19110
- for (var j = minLen; j < maxLen; ++j) {
19111
- diffDims.push(j);
19112
- }
19113
- }
19832
+ // check if possible
19833
+ var dirs = [this.getScrollIndex()];
19834
+ if (index.length() === 4) {
19835
+ dirs.push(3);
19836
+ }
19837
+ if (!geometry.isIndexInBounds(index, dirs)) {
19838
+ // do no send invalid positionchange event: avoid empty repaint
19839
+ return false;
19840
+ }
19841
+
19842
+ // calculate diff dims before updating internal
19843
+ var diffDims = null;
19844
+ var currentIndex = null;
19845
+ if (this.getCurrentPosition()) {
19846
+ currentIndex = this.getCurrentIndex();
19847
+ }
19848
+ if (currentIndex) {
19849
+ if (currentIndex.canCompare(index)) {
19850
+ diffDims = currentIndex.compare(index);
19114
19851
  } else {
19115
19852
  diffDims = [];
19116
- for (var k = 0; k < newPosition.length(); ++k) {
19117
- diffDims.push(k);
19853
+ var minLen = Math.min(currentIndex.length(), index.length());
19854
+ for (var i = 0; i < minLen; ++i) {
19855
+ if (currentIndex.get(i) !== index.get(i)) {
19856
+ diffDims.push(i);
19857
+ }
19858
+ }
19859
+ var maxLen = Math.max(currentIndex.length(), index.length());
19860
+ for (var j = minLen; j < maxLen; ++j) {
19861
+ diffDims.push(j);
19118
19862
  }
19119
19863
  }
19864
+ } else {
19865
+ diffDims = [];
19866
+ for (var k = 0; k < index.length(); ++k) {
19867
+ diffDims.push(k);
19868
+ }
19869
+ }
19120
19870
 
19121
- // assign
19122
- currentPosition = newPosition;
19123
-
19124
- if (!silent) {
19125
- /**
19126
- * Position change event.
19127
- *
19128
- * @event dwv.image.View#positionchange
19129
- * @type {object}
19130
- * @property {Array} value The changed value as [index, pixelValue].
19131
- * @property {Array} diffDims An array of modified indices.
19132
- */
19133
- var posEvent = {
19134
- type: 'positionchange',
19135
- value: [
19136
- posIndex.getValues(),
19137
- currentPosition.getValues(),
19138
- ],
19139
- diffDims: diffDims,
19140
- data: {
19141
- imageUid: image.getImageUid(posIndex)
19142
- }
19143
- };
19871
+ // assign
19872
+ currentPosition = position;
19144
19873
 
19145
- // add value if possible
19146
- if (image.canQuantify()) {
19147
- var pixValue = image.getRescaledValueAtIndex(posIndex);
19148
- posEvent.value.push(pixValue);
19874
+ if (!silent) {
19875
+ /**
19876
+ * Position change event.
19877
+ *
19878
+ * @event dwv.image.View#positionchange
19879
+ * @type {object}
19880
+ * @property {Array} value The changed value as [index, pixelValue].
19881
+ * @property {Array} diffDims An array of modified indices.
19882
+ */
19883
+ var posEvent = {
19884
+ type: 'positionchange',
19885
+ value: [
19886
+ index.getValues(),
19887
+ position.getValues(),
19888
+ ],
19889
+ diffDims: diffDims,
19890
+ data: {
19891
+ imageUid: image.getImageUid(index)
19149
19892
  }
19893
+ };
19150
19894
 
19151
- // fire
19152
- fireEvent(posEvent);
19895
+ // add value if possible
19896
+ if (image.canQuantify()) {
19897
+ var pixValue = image.getRescaledValueAtIndex(index);
19898
+ posEvent.value.push(pixValue);
19153
19899
  }
19900
+
19901
+ // fire
19902
+ fireEvent(posEvent);
19154
19903
  }
19155
19904
 
19156
19905
  // all good
19157
19906
  return true;
19158
19907
  };
19159
19908
 
19160
- /**
19161
- * Set the current index.
19162
- *
19163
- * @param {dwv.math.Index} index The index.
19164
- * @param {boolean} silent If true, does not fire a positionchange event.
19165
- * @returns {boolean} False if not in bounds.
19166
- */
19167
- this.setCurrentIndex = function (index, silent) {
19168
- var geometry = this.getImage().getGeometry();
19169
- var position = geometry.indexToWorld(index);
19170
- if (!geometry.isInBounds(position)) {
19171
- return false;
19172
- }
19173
- return this.setCurrentPosition(position, silent);
19174
- };
19175
-
19176
19909
  /**
19177
19910
  * Set the view window/level.
19178
19911
  *
@@ -19332,7 +20065,7 @@ dwv.image.View.prototype.getWindowLevelMinMax = function () {
19332
20065
  var width = max - min;
19333
20066
  // full black / white images, defaults to 1.
19334
20067
  if (width < 1) {
19335
- dwv.logger.warn('Zero or negative width, defaulting to one.');
20068
+ dwv.logger.warn('Zero or negative window width, defaulting to one.');
19336
20069
  width = 1;
19337
20070
  }
19338
20071
  var center = min + width / 2;
@@ -19354,21 +20087,21 @@ dwv.image.View.prototype.setWindowLevelMinMax = function () {
19354
20087
  * Generate display image data to be given to a canvas.
19355
20088
  *
19356
20089
  * @param {Array} array The array to fill in.
19357
- * @param {dwv.math.Point} position Optional position at which to generate,
19358
- * otherwise generates at current position.
20090
+ * @param {dwv.math.Index} index Optional index at which to generate,
20091
+ * otherwise generates at current index.
19359
20092
  */
19360
- dwv.image.View.prototype.generateImageData = function (array, position) {
19361
- // check position
19362
- if (typeof position === 'undefined') {
19363
- if (!this.getCurrentPosition()) {
19364
- this.setInitialPosition();
20093
+ dwv.image.View.prototype.generateImageData = function (array, index) {
20094
+ // check index
20095
+ if (typeof index === 'undefined') {
20096
+ if (!this.getCurrentIndex()) {
20097
+ this.setInitialIndex();
19365
20098
  }
19366
- position = this.getCurrentIndex();
20099
+ index = this.getCurrentIndex();
19367
20100
  }
19368
20101
 
19369
20102
  var image = this.getImage();
19370
20103
  var iterator = dwv.image.getSliceIterator(
19371
- image, position, false, this.getOrientation());
20104
+ image, index, false, this.getOrientation());
19372
20105
 
19373
20106
  var photoInterpretation = image.getPhotometricInterpretation();
19374
20107
  switch (photoInterpretation) {
@@ -19561,8 +20294,8 @@ dwv.image.ViewFactory.prototype.create = function (dicomElements, image) {
19561
20294
  // store
19562
20295
  view.setWindowPresets(windowPresets);
19563
20296
 
19564
- // set the initial position
19565
- view.setInitialPosition();
20297
+ // initialise the view
20298
+ view.init();
19566
20299
 
19567
20300
  return view;
19568
20301
  };
@@ -19596,7 +20329,7 @@ dwv.image.generateImageDataMonochrome = function (
19596
20329
  array.data[index] = colourMap.red[pxValue];
19597
20330
  array.data[index + 1] = colourMap.green[pxValue];
19598
20331
  array.data[index + 2] = colourMap.blue[pxValue];
19599
- array.data[index + 3] = alphaFunc(ival.value);
20332
+ array.data[index + 3] = alphaFunc(ival.value, ival.index);
19600
20333
  // increment
19601
20334
  index += 4;
19602
20335
  ival = iterator.next();
@@ -19648,7 +20381,7 @@ dwv.image.generateImageDataPaletteColor = function (
19648
20381
  array.data[index + 1] = colourMap.green[pxValue];
19649
20382
  array.data[index + 2] = colourMap.blue[pxValue];
19650
20383
  }
19651
- array.data[index + 3] = alphaFunc(pxValue);
20384
+ array.data[index + 3] = alphaFunc(pxValue, ival.index);
19652
20385
  // increment
19653
20386
  index += 4;
19654
20387
  ival = iterator.next();
@@ -19677,7 +20410,7 @@ dwv.image.generateImageDataRgb = function (
19677
20410
  array.data[index] = ival.value[0];
19678
20411
  array.data[index + 1] = ival.value[1];
19679
20412
  array.data[index + 2] = ival.value[2];
19680
- array.data[index + 3] = alphaFunc(ival.value);
20413
+ array.data[index + 3] = alphaFunc(ival.value, ival.index);
19681
20414
  // increment
19682
20415
  index += 4;
19683
20416
  ival = iterator.next();
@@ -19710,7 +20443,7 @@ dwv.image.generateImageDataYbrFull = function (
19710
20443
  array.data[index] = rgb.r;
19711
20444
  array.data[index + 1] = rgb.g;
19712
20445
  array.data[index + 2] = rgb.b;
19713
- array.data[index + 3] = alphaFunc(ival.value);
20446
+ array.data[index + 3] = alphaFunc(ival.value, ival.index);
19714
20447
  // increment
19715
20448
  index += 4;
19716
20449
  ival = iterator.next();
@@ -23669,6 +24402,60 @@ dwv.math.Ellipse.prototype.quantify = function (viewController, flags) {
23669
24402
  return quant;
23670
24403
  };
23671
24404
 
24405
+ /**
24406
+ * Get the indices that form a ellpise.
24407
+ *
24408
+ * @param {dwv.math.Index} center The ellipse center.
24409
+ * @param {Array} radius The 2 ellipse radiuses.
24410
+ * @param {Array} dir The 2 ellipse directions.
24411
+ * @returns {Array} The indices of the ellipse.
24412
+ */
24413
+ dwv.math.getEllipseIndices = function (center, radius, dir) {
24414
+ var centerValues = center.getValues();
24415
+ // keep all values for possible extra dimensions
24416
+ var values = centerValues.slice();
24417
+ var indices = [];
24418
+ var radiusI = radius[0];
24419
+ var radiusJ = radius[1];
24420
+ var radiusRatio = radiusI / radiusJ;
24421
+ var radiusJ2 = Math.pow(radiusJ, 2);
24422
+ var di = dir[0];
24423
+ var dj = dir[1];
24424
+ // deduce 4 positions from top right
24425
+ for (var j = 0; j < radiusJ; ++j) {
24426
+ // right triangle formed by radiuses, j and len
24427
+ // ellipse: i*i / a*a + j*j / b*b = 1
24428
+ // -> i = a/b * sqrt(b*b - j*j)
24429
+ var len = Math.round(
24430
+ radiusRatio * Math.sqrt(radiusJ2 - Math.pow(j, 2)));
24431
+ var jmax = centerValues[dj] + j;
24432
+ var jmin = centerValues[dj] - j;
24433
+ for (var i = 0; i < len; ++i) {
24434
+ var imax = centerValues[di] + i;
24435
+ var imin = centerValues[di] - i;
24436
+
24437
+ // right
24438
+ values[di] = imax;
24439
+ // right - top
24440
+ values[dj] = jmax;
24441
+ indices.push(new dwv.math.Index(values.slice()));
24442
+ // right - bottom
24443
+ values[dj] = jmin;
24444
+ indices.push(new dwv.math.Index(values.slice()));
24445
+
24446
+ // left
24447
+ values[di] = imin;
24448
+ // left - top
24449
+ values[dj] = jmax;
24450
+ indices.push(new dwv.math.Index(values.slice()));
24451
+ // left - bottom
24452
+ values[dj] = jmin;
24453
+ indices.push(new dwv.math.Index(values.slice()));
24454
+ }
24455
+ }
24456
+ return indices;
24457
+ };
24458
+
23672
24459
  // namespaces
23673
24460
  var dwv = dwv || {};
23674
24461
  dwv.math = dwv.math || {};
@@ -23699,7 +24486,7 @@ dwv.math.Index = function (values) {
23699
24486
  * Get the index value at the given array index.
23700
24487
  *
23701
24488
  * @param {number} i The index to get.
23702
- * @returns {number} The value.
24489
+ * @returns {number|undefined} The value or undefined if not in range.
23703
24490
  */
23704
24491
  this.get = function (i) {
23705
24492
  return values[i];
@@ -23774,6 +24561,27 @@ dwv.math.Index.prototype.equals = function (rhs) {
23774
24561
  return true;
23775
24562
  };
23776
24563
 
24564
+ /**
24565
+ * Compare indices and return different dimensions.
24566
+ *
24567
+ * @param {dwv.math.Index} rhs The index to compare to.
24568
+ * @returns {Array} The list of different dimensions.
24569
+ */
24570
+ dwv.math.Index.prototype.compare = function (rhs) {
24571
+ // check if can compare
24572
+ if (!this.canCompare(rhs)) {
24573
+ return null;
24574
+ }
24575
+ // check values
24576
+ var diffDims = [];
24577
+ for (var i = 0, leni = this.length(); i < leni; ++i) {
24578
+ if (this.get(i) !== rhs.get(i)) {
24579
+ diffDims.push(i);
24580
+ }
24581
+ }
24582
+ return diffDims;
24583
+ };
24584
+
23777
24585
  /**
23778
24586
  * Add another index to this one.
23779
24587
  *
@@ -23821,6 +24629,21 @@ dwv.math.getZeroIndex = function (size) {
23821
24629
  return new dwv.math.Index(values);
23822
24630
  };
23823
24631
 
24632
+ /**
24633
+ * Get an array sort callback.
24634
+ * f(a,b) > 0 -> b,a
24635
+ * f(a,b) < 0 -> a,b
24636
+ * f(a,b) = 0 -> original order
24637
+ *
24638
+ * @param {number} direction The direction to use to compare indices.
24639
+ * @returns {Function} A function that compares two dwv.math.Index.
24640
+ */
24641
+ dwv.math.getIndexCompareFunction = function (direction) {
24642
+ return function (a, b) {
24643
+ return a.get(direction) - b.get(direction);
24644
+ };
24645
+ };
24646
+
23824
24647
  /**
23825
24648
  * Get a string id from the index values in the form of: '#0-1_#1-2'.
23826
24649
  *
@@ -24150,6 +24973,9 @@ dwv.math.isSimilar = function (a, b, tol) {
24150
24973
  * @class
24151
24974
  */
24152
24975
  dwv.math.Matrix33 = function (values) {
24976
+ // matrix inverse, calculated at first ask
24977
+ var inverse = null;
24978
+
24153
24979
  /**
24154
24980
  * Get a value of the matrix.
24155
24981
  *
@@ -24160,6 +24986,20 @@ dwv.math.Matrix33 = function (values) {
24160
24986
  this.get = function (row, col) {
24161
24987
  return values[row * 3 + col];
24162
24988
  };
24989
+
24990
+ /**
24991
+ * Get the inverse of this matrix.
24992
+ *
24993
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
24994
+ * if the determinant is zero.
24995
+ */
24996
+ this.getInverse = function () {
24997
+ if (inverse === null) {
24998
+ inverse = dwv.math.getMatrixInverse(this);
24999
+ }
25000
+ return inverse;
25001
+ };
25002
+
24163
25003
  }; // Matrix33
24164
25004
 
24165
25005
  /**
@@ -24171,15 +25011,16 @@ dwv.math.Matrix33 = function (values) {
24171
25011
  * @returns {boolean} True if both matrices are equal.
24172
25012
  */
24173
25013
  dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24174
- return dwv.math.isSimilar(this.get(0, 0), rhs.get(0, 0), p) &&
24175
- dwv.math.isSimilar(this.get(0, 1), rhs.get(0, 1), p) &&
24176
- dwv.math.isSimilar(this.get(0, 2), rhs.get(0, 2), p) &&
24177
- dwv.math.isSimilar(this.get(1, 0), rhs.get(1, 0), p) &&
24178
- dwv.math.isSimilar(this.get(1, 1), rhs.get(1, 1), p) &&
24179
- dwv.math.isSimilar(this.get(1, 2), rhs.get(1, 2), p) &&
24180
- dwv.math.isSimilar(this.get(2, 0), rhs.get(2, 0), p) &&
24181
- dwv.math.isSimilar(this.get(2, 1), rhs.get(2, 1), p) &&
24182
- dwv.math.isSimilar(this.get(2, 2), rhs.get(2, 2), p);
25014
+ // TODO: add type check
25015
+ // check values
25016
+ for (var i = 0; i < 3; ++i) {
25017
+ for (var j = 0; j < 3; ++j) {
25018
+ if (!dwv.math.isSimilar(this.get(i, j), rhs.get(i, j), p)) {
25019
+ return false;
25020
+ }
25021
+ }
25022
+ }
25023
+ return true;
24183
25024
  };
24184
25025
 
24185
25026
  /**
@@ -24188,10 +25029,20 @@ dwv.math.Matrix33.prototype.equals = function (rhs, p) {
24188
25029
  * @returns {string} The matrix as a string.
24189
25030
  */
24190
25031
  dwv.math.Matrix33.prototype.toString = function () {
24191
- return '[' + this.get(0, 0) + ', ' + this.get(0, 1) + ', ' + this.get(0, 2) +
24192
- '\n ' + this.get(1, 0) + ', ' + this.get(1, 1) + ', ' + this.get(1, 2) +
24193
- '\n ' + this.get(2, 0) + ', ' + this.get(2, 1) + ', ' + this.get(2, 2) +
24194
- ']';
25032
+ var str = '[';
25033
+ for (var i = 0; i < 3; ++i) {
25034
+ if (i !== 0) {
25035
+ str += ', \n ';
25036
+ }
25037
+ for (var j = 0; j < 3; ++j) {
25038
+ if (j !== 0) {
25039
+ str += ', ';
25040
+ }
25041
+ str += this.get(i, j);
25042
+ }
25043
+ }
25044
+ str += ']';
25045
+ return str;
24195
25046
  };
24196
25047
 
24197
25048
  /**
@@ -24289,42 +25140,46 @@ dwv.math.Matrix33.prototype.multiplyIndex3D = function (index3D) {
24289
25140
  };
24290
25141
 
24291
25142
  /**
24292
- * Get the inverse of this matrix.
25143
+ * Get the inverse of an input 3*3 matrix.
24293
25144
  *
24294
- * @returns {dwv.math.Matrix33} The inverse matrix.
25145
+ * @param {dwv.math.Matrix33} m The input matrix.
25146
+ * @returns {dwv.math.Matrix33|undefined} The inverse matrix or undefined
25147
+ * if the determinant is zero.
24295
25148
  * @see https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
24296
- */
24297
- dwv.math.Matrix33.prototype.getInverse = function () {
24298
- var a = this.get(0, 0);
24299
- var b = this.get(0, 1);
24300
- var c = this.get(0, 2);
24301
- var d = this.get(1, 0);
24302
- var e = this.get(1, 1);
24303
- var f = this.get(1, 2);
24304
- var g = this.get(2, 0);
24305
- var h = this.get(2, 1);
24306
- var i = this.get(2, 2);
24307
-
24308
- var a2 = e * i - f * h;
24309
- var b2 = f * g - d * i;
24310
- var c2 = d * h - e * g;
24311
-
24312
- var det = a * a2 + b * b2 + c * c2;
25149
+ * @see https://github.com/willnode/N-Matrix-Programmer
25150
+ */
25151
+ dwv.math.getMatrixInverse = function (m) {
25152
+ var m00 = m.get(0, 0);
25153
+ var m01 = m.get(0, 1);
25154
+ var m02 = m.get(0, 2);
25155
+ var m10 = m.get(1, 0);
25156
+ var m11 = m.get(1, 1);
25157
+ var m12 = m.get(1, 2);
25158
+ var m20 = m.get(2, 0);
25159
+ var m21 = m.get(2, 1);
25160
+ var m22 = m.get(2, 2);
25161
+
25162
+ var a1212 = m11 * m22 - m12 * m21;
25163
+ var a2012 = m12 * m20 - m10 * m22;
25164
+ var a0112 = m10 * m21 - m11 * m20;
25165
+
25166
+ var det = m00 * a1212 + m01 * a2012 + m02 * a0112;
24313
25167
  if (det === 0) {
24314
- dwv.logger.warn('Cannot invert matrix with zero determinant.');
25168
+ dwv.logger.warn('Cannot invert 3*3 matrix with zero determinant.');
24315
25169
  return;
24316
25170
  }
25171
+ det = 1 / det;
24317
25172
 
24318
25173
  var values = [
24319
- a2 / det,
24320
- (c * h - b * i) / det,
24321
- (b * f - c * e) / det,
24322
- b2 / det,
24323
- (a * i - c * g) / det,
24324
- (c * d - a * f) / det,
24325
- c2 / det,
24326
- (b * g - a * h) / det,
24327
- (a * e - b * d) / det
25174
+ det * a1212,
25175
+ det * (m02 * m21 - m01 * m22),
25176
+ det * (m01 * m12 - m02 * m11),
25177
+ det * a2012,
25178
+ det * (m00 * m22 - m02 * m20),
25179
+ det * (m02 * m10 - m00 * m12),
25180
+ det * a0112,
25181
+ det * (m01 * m20 - m00 * m21),
25182
+ det * (m00 * m11 - m01 * m10)
24328
25183
  ];
24329
25184
 
24330
25185
  return new dwv.math.Matrix33(values);
@@ -24735,6 +25590,21 @@ dwv.math.Point3D.prototype.equals = function (rhs) {
24735
25590
  this.getZ() === rhs.getZ();
24736
25591
  };
24737
25592
 
25593
+ /**
25594
+ * Check for Point3D similarity.
25595
+ *
25596
+ * @param {dwv.math.Point3D} rhs The other point to compare to.
25597
+ * @param {number} tol Optional comparison tolerance,
25598
+ * default to Number.EPSILON.
25599
+ * @returns {boolean} True if both points are equal.
25600
+ */
25601
+ dwv.math.Point3D.prototype.isSimilar = function (rhs, tol) {
25602
+ return rhs !== null &&
25603
+ dwv.math.isSimilar(this.getX(), rhs.getX(), tol) &&
25604
+ dwv.math.isSimilar(this.getY(), rhs.getY(), tol) &&
25605
+ dwv.math.isSimilar(this.getZ(), rhs.getZ(), tol);
25606
+ };
25607
+
24738
25608
  /**
24739
25609
  * Get a string representation of the Point3D.
24740
25610
  *
@@ -24772,6 +25642,19 @@ dwv.math.Point3D.prototype.minus = function (point3D) {
24772
25642
  (this.getZ() - point3D.getZ()));
24773
25643
  };
24774
25644
 
25645
+ /**
25646
+ * Get an array find callback for a given point.
25647
+ *
25648
+ * @param {dwv.math.Point3D} point The point to compare to.
25649
+ * @returns {Function} A function that compares its input point to the one
25650
+ * given as input to this function.
25651
+ */
25652
+ dwv.math.getEqualPoint3DFunction = function (point) {
25653
+ return function (element) {
25654
+ return element.equals(point);
25655
+ };
25656
+ };
25657
+
24775
25658
  /**
24776
25659
  * Immutable point.
24777
25660
  * Warning: the input array is NOT cloned, modifying it will
@@ -27074,11 +27957,15 @@ dwv.tool.Draw = function (app) {
27074
27957
  };
27075
27958
 
27076
27959
  /**
27077
- * Handle double click event.
27960
+ * Handle double click event: some tools use it to finish interaction.
27078
27961
  *
27079
- * @param {object} event The mouse up event.
27962
+ * @param {object} event The double click event.
27080
27963
  */
27081
27964
  this.dblclick = function (event) {
27965
+ // only end by double click undefined NPoints
27966
+ if (typeof currentFactory.getNPoints() !== 'undefined') {
27967
+ return;
27968
+ }
27082
27969
  // exit if not started draw
27083
27970
  if (!started) {
27084
27971
  return;
@@ -27303,7 +28190,7 @@ dwv.tool.Draw = function (app) {
27303
28190
  updateDrawLayer(layerGroup);
27304
28191
  });
27305
28192
  // same for colour
27306
- this.setLineColour(this.style.getLineColour());
28193
+ this.setFeatures({lineColour: this.style.getLineColour()});
27307
28194
  } else {
27308
28195
  app.removeEventListener('positionchange', function () {
27309
28196
  updateDrawLayer(layerGroup);
@@ -27441,10 +28328,23 @@ dwv.tool.Draw = function (app) {
27441
28328
  var drawLayer = layerGroup.getActiveDrawLayer();
27442
28329
  // validate the group position
27443
28330
  dwv.tool.validateGroupPosition(drawLayer.getBaseSize(), this);
28331
+ // get appropriate factory
28332
+ var factory;
28333
+ var keys = Object.keys(self.shapeFactoryList);
28334
+ for (var i = 0; i < keys.length; ++i) {
28335
+ factory = new self.shapeFactoryList[keys[i]];
28336
+ if (factory.isFactoryGroup(shapeGroup)) {
28337
+ // stop at first find
28338
+ break;
28339
+ }
28340
+ }
28341
+ if (typeof factory === 'undefined') {
28342
+ throw new Error('Cannot find factory to update quantification.');
28343
+ }
27444
28344
  // update quantification if possible
27445
- if (typeof currentFactory.updateQuantification !== 'undefined') {
28345
+ if (typeof factory.updateQuantification !== 'undefined') {
27446
28346
  var vc = layerGroup.getActiveViewLayer().getViewController();
27447
- currentFactory.updateQuantification(this, vc);
28347
+ factory.updateQuantification(this, vc);
27448
28348
  }
27449
28349
  // highlight trash when on it
27450
28350
  var offset = dwv.gui.getEventOffset(event.evt)[0];
@@ -27576,7 +28476,7 @@ dwv.tool.Draw = function (app) {
27576
28476
  };
27577
28477
 
27578
28478
  /**
27579
- * Set the tool options.
28479
+ * Set the tool configuration options.
27580
28480
  *
27581
28481
  * @param {object} options The list of shape names amd classes.
27582
28482
  */
@@ -27587,6 +28487,24 @@ dwv.tool.Draw = function (app) {
27587
28487
  shapeEditor.setFactoryList(options);
27588
28488
  };
27589
28489
 
28490
+ /**
28491
+ * Set the tool live features: shape colour and shape name.
28492
+ *
28493
+ * @param {object} features The list of features.
28494
+ */
28495
+ this.setFeatures = function (features) {
28496
+ if (typeof features.shapeColour !== 'undefined') {
28497
+ this.style.setLineColour(features.shapeColour);
28498
+ }
28499
+ if (typeof features.shapeName !== 'undefined') {
28500
+ // check if we have it
28501
+ if (!this.hasShape(features.shapeName)) {
28502
+ throw new Error('Unknown shape: \'' + features.shapeName + '\'');
28503
+ }
28504
+ this.shapeName = features.shapeName;
28505
+ }
28506
+ };
28507
+
27590
28508
  /**
27591
28509
  * Initialise the tool.
27592
28510
  */
@@ -27626,15 +28544,6 @@ dwv.tool.Draw = function (app) {
27626
28544
  }
27627
28545
  };
27628
28546
 
27629
- /**
27630
- * Set the line colour of the drawing.
27631
- *
27632
- * @param {string} colour The colour to set
27633
- */
27634
- this.setLineColour = function (colour) {
27635
- this.style.setLineColour(colour);
27636
- };
27637
-
27638
28547
  // Private Methods -----------------------------------------------------------
27639
28548
 
27640
28549
  /**
@@ -27672,19 +28581,6 @@ dwv.tool.Draw.prototype.getHelpKeys = function () {
27672
28581
  };
27673
28582
  };
27674
28583
 
27675
- /**
27676
- * Set the shape name of the drawing.
27677
- *
27678
- * @param {string} name The name of the shape.
27679
- */
27680
- dwv.tool.Draw.prototype.setShapeName = function (name) {
27681
- // check if we have it
27682
- if (!this.hasShape(name)) {
27683
- throw new Error('Unknown shape: \'' + name + '\'');
27684
- }
27685
- this.shapeName = name;
27686
- };
27687
-
27688
28584
  /**
27689
28585
  * Check if the shape is in the shape list.
27690
28586
  *
@@ -29059,23 +29955,32 @@ dwv.tool.Filter.prototype.getSelectedFilter = function () {
29059
29955
  };
29060
29956
 
29061
29957
  /**
29062
- * Set the selected filter.
29958
+ * Set the tool live features: filter name.
29063
29959
  *
29064
- * @param {string} name The name of the filter to select.
29960
+ * @param {object} features The list of features.
29065
29961
  */
29066
- dwv.tool.Filter.prototype.setSelectedFilter = function (name) {
29067
- // check if we have it
29068
- if (!this.hasFilter(name)) {
29069
- throw new Error('Unknown filter: \'' + name + '\'');
29962
+ dwv.tool.Filter.prototype.setFeatures = function (features) {
29963
+ if (typeof features.filterName !== 'undefined') {
29964
+ // check if we have it
29965
+ if (!this.hasFilter(features.filterName)) {
29966
+ throw new Error('Unknown filter: \'' + features.filterName + '\'');
29967
+ }
29968
+ // de-activate last selected
29969
+ if (this.selectedFilter) {
29970
+ this.selectedFilter.activate(false);
29971
+ }
29972
+ // enable new one
29973
+ this.selectedFilter = this.filterList[features.filterName];
29974
+ // activate the selected filter
29975
+ this.selectedFilter.activate(true);
29070
29976
  }
29071
- // de-activate last selected
29072
- if (this.selectedFilter) {
29073
- this.selectedFilter.activate(false);
29977
+ if (typeof features.run !== 'undefined' && features.run) {
29978
+ var args = {};
29979
+ if (typeof features.runArgs !== 'undefined') {
29980
+ args = features.runArgs;
29981
+ }
29982
+ this.getSelectedFilter().run(args);
29074
29983
  }
29075
- // enable new one
29076
- this.selectedFilter = this.filterList[name];
29077
- // activate the selected filter
29078
- this.selectedFilter.activate(true);
29079
29984
  };
29080
29985
 
29081
29986
  /**
@@ -29915,7 +30820,7 @@ dwv.tool.Floodfill = function (app) {
29915
30820
  // init with the app window scale
29916
30821
  this.style.setBaseScale(app.getBaseScale());
29917
30822
  // set the default to the first in the list
29918
- this.setLineColour(this.style.getLineColour());
30823
+ this.setFeatures({shapeColour: this.style.getLineColour()});
29919
30824
  }
29920
30825
  };
29921
30826
 
@@ -29977,13 +30882,14 @@ dwv.tool.Floodfill.prototype.getHelpKeys = function () {
29977
30882
  };
29978
30883
 
29979
30884
  /**
29980
- * Set the line colour of the drawing.
30885
+ * Set the tool live features: shape colour.
29981
30886
  *
29982
- * @param {string} colour The colour to set.
30887
+ * @param {object} features The list of features.
29983
30888
  */
29984
- dwv.tool.Floodfill.prototype.setLineColour = function (colour) {
29985
- // set style var
29986
- this.style.setLineColour(colour);
30889
+ dwv.tool.Floodfill.prototype.setFeatures = function (features) {
30890
+ if (typeof features.shapeColour !== 'undefined') {
30891
+ this.style.setLineColour(features.shapeColour);
30892
+ }
29987
30893
  };
29988
30894
 
29989
30895
  // namespaces
@@ -30020,10 +30926,11 @@ dwv.tool.draw.FreeHandFactory = function () {
30020
30926
  /**
30021
30927
  * Get the number of points needed to build the shape.
30022
30928
  *
30023
- * @returns {number} The number of points.
30929
+ * @returns {number|undefined} The number of points.
30024
30930
  */
30025
30931
  this.getNPoints = function () {
30026
- return 1000;
30932
+ // undefined to end with double click
30933
+ return undefined;
30027
30934
  };
30028
30935
  /**
30029
30936
  * Get the timeout between point storage.
@@ -30526,7 +31433,7 @@ dwv.tool.Livewire = function (app) {
30526
31433
  // init with the app window scale
30527
31434
  this.style.setBaseScale(app.getBaseScale());
30528
31435
  // set the default to the first in the list
30529
- this.setLineColour(this.style.getLineColour());
31436
+ this.setFeatures({shapeColour: this.style.getLineColour()});
30530
31437
  }
30531
31438
  };
30532
31439
 
@@ -30582,15 +31489,17 @@ dwv.tool.Livewire.prototype.getHelpKeys = function () {
30582
31489
  };
30583
31490
 
30584
31491
  /**
30585
- * Set the line colour of the drawing.
31492
+ * Set the tool live features: shape colour.
30586
31493
  *
30587
- * @param {string} colour The colour to set.
31494
+ * @param {object} features The list of features.
30588
31495
  */
30589
- dwv.tool.Livewire.prototype.setLineColour = function (colour) {
30590
- // set style var
30591
- this.style.setLineColour(colour);
31496
+ dwv.tool.Livewire.prototype.setFeatures = function (features) {
31497
+ if (typeof features.shapeColour !== 'undefined') {
31498
+ this.style.setLineColour(features.shapeColour);
31499
+ }
30592
31500
  };
30593
31501
 
31502
+
30594
31503
  // namespaces
30595
31504
  var dwv = dwv || {};
30596
31505
  dwv.tool = dwv.tool || {};
@@ -31451,10 +32360,11 @@ dwv.tool.draw.RoiFactory = function () {
31451
32360
  /**
31452
32361
  * Get the number of points needed to build the shape.
31453
32362
  *
31454
- * @returns {number} The number of points.
32363
+ * @returns {number|undefined} The number of points.
31455
32364
  */
31456
32365
  this.getNPoints = function () {
31457
- return 50;
32366
+ // undefined to end with double click
32367
+ return undefined;
31458
32368
  };
31459
32369
  /**
31460
32370
  * Get the timeout between point storage.
@@ -31940,6 +32850,13 @@ dwv.tool.Scroll = function (app) {
31940
32850
  // touch timer ID (created by setTimeout)
31941
32851
  var touchTimerID = null;
31942
32852
 
32853
+ /**
32854
+ * Accumulated wheel event deltaY.
32855
+ *
32856
+ * @type {number}
32857
+ */
32858
+ var wheelDeltaY = 0;
32859
+
31943
32860
  /**
31944
32861
  * Handle mouse down event.
31945
32862
  *
@@ -32085,6 +33002,22 @@ dwv.tool.Scroll = function (app) {
32085
33002
  * @param {object} event The mouse wheel event.
32086
33003
  */
32087
33004
  this.wheel = function (event) {
33005
+ // deltaMode (deltaY values on my machine...):
33006
+ // - 0 (DOM_DELTA_PIXEL): chrome, deltaY mouse scroll = 53
33007
+ // - 1 (DOM_DELTA_LINE): firefox, deltaY mouse scroll = 6
33008
+ // - 2 (DOM_DELTA_PAGE): ??
33009
+ // TODO: check scroll event
33010
+ var scrollMin = 52;
33011
+ if (event.deltaMode === 1) {
33012
+ scrollMin = 5.99;
33013
+ }
33014
+ wheelDeltaY += event.deltaY;
33015
+ if (Math.abs(wheelDeltaY) < scrollMin) {
33016
+ return;
33017
+ } else {
33018
+ wheelDeltaY = 0;
33019
+ }
33020
+
32088
33021
  var up = false;
32089
33022
  if (event.deltaY < 0) {
32090
33023
  up = true;
@@ -32371,11 +33304,16 @@ dwv.tool.WindowLevel = function (app) {
32371
33304
  // difference to last position
32372
33305
  var diffX = event._x - self.x0;
32373
33306
  var diffY = self.y0 - event._y;
33307
+ // data range
33308
+ var range = viewController.getImageRescaledDataRange();
33309
+ // 1/1000 seems to give reasonable results...
33310
+ var pixelToIntensity = (range.max - range.min) * 0.01;
33311
+
32374
33312
  // calculate new window level
32375
- var windowCenter =
32376
- parseInt(viewController.getWindowLevel().center, 10) + diffY;
32377
- var windowWidth =
32378
- parseInt(viewController.getWindowLevel().width, 10) + diffX;
33313
+ var center = parseInt(viewController.getWindowLevel().center, 10);
33314
+ var width = parseInt(viewController.getWindowLevel().width, 10);
33315
+ var windowCenter = center + Math.round(diffY * pixelToIntensity);
33316
+ var windowWidth = width + Math.round(diffX * pixelToIntensity);
32379
33317
  // bound window width
32380
33318
  windowWidth = dwv.image.validateWindowWidth(windowWidth);
32381
33319
 
@@ -32635,7 +33573,7 @@ dwv.tool.ZoomAndPan = function (app) {
32635
33573
  }
32636
33574
  } else {
32637
33575
  // zoom mode
32638
- var zoom = (lineRatio - 1) / 2;
33576
+ var zoom = (lineRatio - 1) / 10;
32639
33577
  if (Math.abs(zoom) % 0.1 <= 0.05) {
32640
33578
  var planePos = viewLayer.displayToMainPlanePos(
32641
33579
  self.midPoint.getX(), self.midPoint.getY());