Skip to content

Commit

Permalink
Change isInBounds to accept a list of directions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmartel committed Jul 12, 2022
1 parent 041b278 commit 17a9a68
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
28 changes: 18 additions & 10 deletions src/image/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ dwv.image.Size.prototype.equals = function (rhs) {
* Check that an index is within bounds.
*
* @param {dwv.math.Index} index The index to check.
* @param {number} dir Optional direction to check.
* @param {Array} dirs Optional list of directions to check.
* @returns {boolean} True if the given coordinates are within bounds.
*/
dwv.image.Size.prototype.isInBounds = function (index, dir) {
dwv.image.Size.prototype.isInBounds = function (index, dirs) {
// check input
if (!index) {
return false;
Expand All @@ -181,20 +181,28 @@ dwv.image.Size.prototype.isInBounds = function (index, dir) {
if (length !== index.length()) {
return false;
}
// create dirs if not there
if (typeof dirs === 'undefined') {
dirs = [];
for (var j = 0; j < length; ++j) {
dirs.push(j);
}
} else {
for (var k = 0; k < length; ++k) {
if (dirs[k] > length - 1) {
throw new Error('Wrong input dir value: ' + dirs[k]);
}
}
}
// check values is 0 <= v < size
var inBound = function (value, size) {
return value >= 0 && value < size;
};
if (typeof dir !== 'undefined') {
if (!inBound(index.get(dir), this.get(dir))) {
// check
for (var i = 0; i < dirs.length; ++i) {
if (!inBound(index.get(dirs[i]), this.get(dirs[i]))) {
return false;
}
} else {
for (var i = 0; i < length; ++i) {
if (!inBound(index.get(i), this.get(i))) {
return false;
}
}
}
// seems ok!
return true;
Expand Down
18 changes: 15 additions & 3 deletions src/image/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ dwv.image.View = function (image) {
this.canSetPosition = function (position) {
var geometry = image.getGeometry();
var index = geometry.worldToIndex(position);
return geometry.isIndexInBounds(index, this.getScrollIndex());
var dirs = [this.getScrollIndex()];
if (index.length() === 4) {
dirs.push(3);
}
return geometry.isIndexInBounds(index, dirs);
};

/**
Expand Down Expand Up @@ -467,7 +471,11 @@ dwv.image.View = function (image) {
// send invalid event if not in bounds
var geometry = image.getGeometry();
var index = geometry.worldToIndex(position);
if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
var dirs = [this.getScrollIndex()];
if (index.length() === 4) {
dirs.push(3);
}
if (!geometry.isIndexInBounds(index, dirs)) {
if (!silent) {
// fire event with valid: false
fireEvent({
Expand Down Expand Up @@ -502,7 +510,11 @@ dwv.image.View = function (image) {
var position = geometry.indexToWorld(index);

// check if possible
if (!geometry.isIndexInBounds(index, this.getScrollIndex())) {
var dirs = [this.getScrollIndex()];
if (index.length() === 4) {
dirs.push(3);
}
if (!geometry.isIndexInBounds(index, dirs)) {
// do no send invalid positionchange event: avoid empty repaint
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/image/size.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ QUnit.test('Test Size.', function (assert) {
assert.equal(size0.isInBounds(index0), false, 'isInBounds too big');
index0 = new dwv.math.Index([-1, 2, 3]);
assert.equal(size0.isInBounds(index0), false, 'isInBounds too small');
// with dirs
index0 = new dwv.math.Index([0, 0, 0]);
assert.equal(size0.isInBounds(index0, [0, 1]), true,
'isInBounds [0, 1] 0,0,0');
assert.equal(size0.isInBounds(index0, [1, 2]), true,
'isInBounds [1, 2] 0,0,0');
assert.throws(function () {
size0.isInBounds(index0, [1, 3]);
},
new Error('Wrong input dir value: 3'),
'isInBounds bad dir');
index0 = new dwv.math.Index([2, 3, 4]);
assert.equal(size0.isInBounds(index0, [1, 2]), false,
'isInBounds [0, 1] 2,3,4');

// can scroll
var size20 = new dwv.image.Size([2, 1, 2]);
Expand Down

0 comments on commit 17a9a68

Please sign in to comment.